blob: 16dd038e3416fddb1c21f0b41b46ad063b1339cf [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00007 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jaeden Amerocab54942018-07-25 13:26:13 +01008 */
Gilles Peskinee59236f2018-01-27 23:32:46 +01009
10#ifndef PSA_CRYPTO_H
11#define PSA_CRYPTO_H
12
Gilles Peskineb1176f22023-02-22 22:07:28 +010013#if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)
14#include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
15#else
Gilles Peskinee59236f2018-01-27 23:32:46 +010016#include "crypto_platform.h"
Gilles Peskineb1176f22023-02-22 22:07:28 +010017#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010018
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010019#include <stddef.h>
20
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010021#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010022/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
23 * must be defined in the crypto_platform.h header. These mock definitions
24 * are present in this file as a convenience to generate pretty-printed
25 * documentation that includes those definitions. */
26
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010027/** \defgroup platform Implementation-specific definitions
28 * @{
29 */
30
31/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010032#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010033
Gilles Peskinee59236f2018-01-27 23:32:46 +010034#ifdef __cplusplus
35extern "C" {
36#endif
37
Gilles Peskinef3b731e2018-12-12 13:38:31 +010038/* The file "crypto_types.h" declares types that encode errors,
39 * algorithms, key types, policies, etc. */
40#include "crypto_types.h"
41
Andrew Thoelke02b372b2019-10-02 09:32:21 +010042/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010043 * @{
44 */
45
46/**
47 * The major version of this implementation of the PSA Crypto API
48 */
49#define PSA_CRYPTO_API_VERSION_MAJOR 1
50
51/**
52 * The minor version of this implementation of the PSA Crypto API
53 */
54#define PSA_CRYPTO_API_VERSION_MINOR 0
55
56/**@}*/
57
Gilles Peskinef3b731e2018-12-12 13:38:31 +010058/* The file "crypto_values.h" declares macros to build and analyze values
59 * of integral types defined in "crypto_types.h". */
60#include "crypto_values.h"
61
62/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010063 * @{
64 */
65
66/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010067 * \brief Library initialization.
68 *
69 * Applications must call this function before calling any other
70 * function in this module.
71 *
72 * Applications may call this function more than once. Once a call
73 * succeeds, subsequent calls are guaranteed to succeed.
74 *
itayzafrir18617092018-09-16 12:22:41 +030075 * If the application calls other functions before calling psa_crypto_init(),
76 * the behavior is undefined. Implementations are encouraged to either perform
77 * the operation as if the library had been initialized or to return
78 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
79 * implementations should not return a success status if the lack of
80 * initialization may have security implications, for example due to improper
81 * seeding of the random number generator.
82 *
Gilles Peskineed733552023-02-14 19:21:09 +010083 * \retval #PSA_SUCCESS \emptydescription
84 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
85 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
86 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
87 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
88 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
89 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
90 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
91 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
92 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskinee59236f2018-01-27 23:32:46 +010093 */
94psa_status_t psa_crypto_init(void);
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/**@}*/
97
Gilles Peskine105f67f2019-07-23 18:16:05 +020098/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020099 * @{
100 */
101
Gilles Peskinea0c06552019-05-21 15:54:54 +0200102/** \def PSA_KEY_ATTRIBUTES_INIT
103 *
104 * This macro returns a suitable initializer for a key attribute structure
105 * of type #psa_key_attributes_t.
106 */
Gilles Peskinea0c06552019-05-21 15:54:54 +0200107
108/** Return an initial value for a key attributes structure.
109 */
110static psa_key_attributes_t psa_key_attributes_init(void);
111
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200112/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200113 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200114 * If the attribute structure currently declares the key as volatile (which
115 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * This function does not access storage, it merely stores the given
119 * value in the structure.
120 * The persistent key will be written to storage when the attribute
121 * structure is passed to a key creation function such as
Gilles Peskine603b5b82024-06-06 21:23:00 +0200122 * psa_import_key(), psa_generate_key(), psa_generate_key_custom(),
123 * psa_key_derivation_output_key(), psa_key_derivation_output_key_custom()
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100124 * or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200125 *
Gilles Peskine20628592019-04-19 19:29:50 +0200126 * This function may be declared as `static` (i.e. without external
127 * linkage). This function may be provided as a function-like macro,
128 * but in this case it must evaluate each of its arguments exactly once.
129 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200130 * \param[out] attributes The attribute structure to write to.
131 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133static void psa_set_key_id(psa_key_attributes_t *attributes,
134 mbedtls_svc_key_id_t key);
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135
Ronald Cron6b5ff532020-10-16 14:38:19 +0200136#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
137/** Set the owner identifier of a key.
138 *
139 * When key identifiers encode key owner identifiers, psa_set_key_id() does
140 * not allow to define in key attributes the owner of volatile keys as
141 * psa_set_key_id() enforces the key to be persistent.
142 *
143 * This function allows to set in key attributes the owner identifier of a
144 * key. It is intended to be used for volatile keys. For persistent keys,
145 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
146 * the owner of a key.
147 *
148 * \param[out] attributes The attribute structure to write to.
Antonio de Angelise2b68662021-11-30 12:21:44 +0000149 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
152 mbedtls_key_owner_id_t owner);
Ronald Cron6b5ff532020-10-16 14:38:19 +0200153#endif
154
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200155/** Set the location of a persistent key.
156 *
157 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200158 * with psa_set_key_id(). By default, a key that has a persistent identifier
159 * is stored in the default storage area identifier by
160 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
161 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200162 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200163 * This function does not access storage, it merely stores the given
164 * value in the structure.
165 * The persistent key will be written to storage when the attribute
166 * structure is passed to a key creation function such as
Gilles Peskine603b5b82024-06-06 21:23:00 +0200167 * psa_import_key(), psa_generate_key(), psa_generate_key_custom(),
168 * psa_key_derivation_output_key(), psa_key_derivation_output_key_custom()
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100169 * or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200170 *
171 * This function may be declared as `static` (i.e. without external
172 * linkage). This function may be provided as a function-like macro,
173 * but in this case it must evaluate each of its arguments exactly once.
174 *
175 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200176 * \param lifetime The lifetime for the key.
177 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200178 * key will be volatile, and the key identifier
179 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200180 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200181static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
182 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200183
Gilles Peskine20628592019-04-19 19:29:50 +0200184/** Retrieve the key identifier from key attributes.
185 *
186 * This function may be declared as `static` (i.e. without external
187 * linkage). This function may be provided as a function-like macro,
188 * but in this case it must evaluate its argument exactly once.
189 *
190 * \param[in] attributes The key attribute structure to query.
191 *
192 * \return The persistent identifier stored in the attribute structure.
193 * This value is unspecified if the attribute structure declares
194 * the key as volatile.
195 */
Ronald Cron71016a92020-08-28 19:01:50 +0200196static mbedtls_svc_key_id_t psa_get_key_id(
197 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200198
Gilles Peskine20628592019-04-19 19:29:50 +0200199/** Retrieve the lifetime from key attributes.
200 *
201 * This function may be declared as `static` (i.e. without external
202 * linkage). This function may be provided as a function-like macro,
203 * but in this case it must evaluate its argument exactly once.
204 *
205 * \param[in] attributes The key attribute structure to query.
206 *
207 * \return The lifetime value stored in the attribute structure.
208 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200209static psa_key_lifetime_t psa_get_key_lifetime(
210 const psa_key_attributes_t *attributes);
211
Gilles Peskine20628592019-04-19 19:29:50 +0200212/** Declare usage flags for a key.
213 *
214 * Usage flags are part of a key's usage policy. They encode what
215 * kind of operations are permitted on the key. For more details,
216 * refer to the documentation of the type #psa_key_usage_t.
217 *
218 * This function overwrites any usage flags
219 * previously set in \p attributes.
220 *
221 * This function may be declared as `static` (i.e. without external
222 * linkage). This function may be provided as a function-like macro,
223 * but in this case it must evaluate each of its arguments exactly once.
224 *
225 * \param[out] attributes The attribute structure to write to.
226 * \param usage_flags The usage flags to write.
227 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200228static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
229 psa_key_usage_t usage_flags);
230
Gilles Peskine20628592019-04-19 19:29:50 +0200231/** Retrieve the usage flags from key attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate its argument exactly once.
236 *
237 * \param[in] attributes The key attribute structure to query.
238 *
239 * \return The usage flags stored in the attribute structure.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static psa_key_usage_t psa_get_key_usage_flags(
242 const psa_key_attributes_t *attributes);
243
Gilles Peskine20628592019-04-19 19:29:50 +0200244/** Declare the permitted algorithm policy for a key.
245 *
246 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200247 * algorithms are permitted to be used with this key. The following
248 * algorithm policies are supported:
249 * - 0 does not allow any cryptographic operation with the key. The key
250 * may be used for non-cryptographic actions such as exporting (if
251 * permitted by the usage flags).
252 * - An algorithm value permits this particular algorithm.
253 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
254 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100255 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100256 * any MAC algorithm from the same base class (e.g. CMAC) which
257 * generates/verifies a MAC length greater than or equal to the length
258 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100259 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
260 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100261 * generates/verifies a tag length greater than or equal to the length
262 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200263 *
264 * This function overwrites any algorithm policy
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param alg The permitted algorithm policy to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
275 psa_algorithm_t alg);
276
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100277
Gilles Peskine20628592019-04-19 19:29:50 +0200278/** Retrieve the algorithm policy from key attributes.
279 *
280 * This function may be declared as `static` (i.e. without external
281 * linkage). This function may be provided as a function-like macro,
282 * but in this case it must evaluate its argument exactly once.
283 *
284 * \param[in] attributes The key attribute structure to query.
285 *
286 * \return The algorithm stored in the attribute structure.
287 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200288static psa_algorithm_t psa_get_key_algorithm(
289 const psa_key_attributes_t *attributes);
290
Gilles Peskine20628592019-04-19 19:29:50 +0200291/** Declare the type of a key.
292 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200293 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200294 * previously set in \p attributes.
295 *
296 * This function may be declared as `static` (i.e. without external
297 * linkage). This function may be provided as a function-like macro,
298 * but in this case it must evaluate each of its arguments exactly once.
299 *
300 * \param[out] attributes The attribute structure to write to.
301 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200302 * If this is 0, the key type in \p attributes
303 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_type(psa_key_attributes_t *attributes,
306 psa_key_type_t type);
307
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100308
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200309/** Declare the size of a key.
310 *
311 * This function overwrites any key size previously set in \p attributes.
312 *
313 * This function may be declared as `static` (i.e. without external
314 * linkage). This function may be provided as a function-like macro,
315 * but in this case it must evaluate each of its arguments exactly once.
316 *
317 * \param[out] attributes The attribute structure to write to.
318 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200319 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200320 * becomes unspecified. Keys of size 0 are
321 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200322 */
323static void psa_set_key_bits(psa_key_attributes_t *attributes,
324 size_t bits);
325
Gilles Peskine20628592019-04-19 19:29:50 +0200326/** Retrieve the key type from key attributes.
327 *
328 * This function may be declared as `static` (i.e. without external
329 * linkage). This function may be provided as a function-like macro,
330 * but in this case it must evaluate its argument exactly once.
331 *
332 * \param[in] attributes The key attribute structure to query.
333 *
334 * \return The key type stored in the attribute structure.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
337
Gilles Peskine20628592019-04-19 19:29:50 +0200338/** Retrieve the key size from key attributes.
339 *
340 * This function may be declared as `static` (i.e. without external
341 * linkage). This function may be provided as a function-like macro,
342 * but in this case it must evaluate its argument exactly once.
343 *
344 * \param[in] attributes The key attribute structure to query.
345 *
346 * \return The key size stored in the attribute structure, in bits.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Retrieve the attributes of a key.
351 *
352 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200353 * psa_reset_key_attributes(). It then copies the attributes of
354 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200355 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200356 * \note This function may allocate memory or other resources.
357 * Once you have called this function on an attribute structure,
358 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200359 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200360 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200361 * \param[in,out] attributes On success, the attributes of the key.
362 * On failure, equivalent to a
363 * freshly-initialized structure.
364 *
Gilles Peskineed733552023-02-14 19:21:09 +0100365 * \retval #PSA_SUCCESS \emptydescription
366 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
369 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
370 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
371 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
372 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100373 * \retval #PSA_ERROR_BAD_STATE
374 * The library has not been previously initialized by psa_crypto_init().
375 * It is implementation-dependent whether a failure to initialize
376 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200377 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200378psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200379 psa_key_attributes_t *attributes);
380
Gilles Peskine20628592019-04-19 19:29:50 +0200381/** Reset a key attribute structure to a freshly initialized state.
382 *
383 * You must initialize the attribute structure as described in the
384 * documentation of the type #psa_key_attributes_t before calling this
385 * function. Once the structure has been initialized, you may call this
386 * function at any time.
387 *
388 * This function frees any auxiliary resources that the structure
389 * may contain.
390 *
391 * \param[in,out] attributes The attribute structure to reset.
392 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200393void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200394
Gilles Peskine87a5e562019-04-17 12:28:25 +0200395/**@}*/
396
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397/** \defgroup key_management Key management
398 * @{
399 */
400
Ronald Cron277a85f2020-08-04 15:49:48 +0200401/** Remove non-essential copies of key material from memory.
402 *
403 * If the key identifier designates a volatile key, this functions does not do
404 * anything and returns successfully.
405 *
406 * If the key identifier designates a persistent key, then this function will
407 * free all resources associated with the key in volatile memory. The key
408 * data in persistent storage is not affected and the key can still be used.
409 *
410 * \param key Identifier of the key to purge.
411 *
412 * \retval #PSA_SUCCESS
413 * The key material will have been removed from memory if it is not
414 * currently required.
415 * \retval #PSA_ERROR_INVALID_ARGUMENT
416 * \p key is not a valid key identifier.
417 * \retval #PSA_ERROR_BAD_STATE
418 * The library has not been previously initialized by psa_crypto_init().
419 * It is implementation-dependent whether a failure to initialize
420 * results in this error code.
421 */
422psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
423
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100424/** Make a copy of a key.
425 *
426 * Copy key material from one location to another.
427 *
428 * This function is primarily useful to copy a key from one location
429 * to another, since it populates a key using the material from
430 * another key which may have a different lifetime.
431 *
432 * This function may be used to share a key with a different party,
433 * subject to implementation-defined restrictions on key sharing.
434 *
435 * The policy on the source key must have the usage flag
436 * #PSA_KEY_USAGE_COPY set.
437 * This flag is sufficient to permit the copy if the key has the lifetime
438 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
439 * Some secure elements do not provide a way to copy a key without
440 * making it extractable from the secure element. If a key is located
441 * in such a secure element, then the key must have both usage flags
442 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
443 * a copy of the key outside the secure element.
444 *
445 * The resulting key may only be used in a way that conforms to
446 * both the policy of the original key and the policy specified in
447 * the \p attributes parameter:
448 * - The usage flags on the resulting key are the bitwise-and of the
449 * usage flags on the source policy and the usage flags in \p attributes.
450 * - If both allow the same algorithm or wildcard-based
451 * algorithm policy, the resulting key has the same algorithm policy.
452 * - If either of the policies allows an algorithm and the other policy
453 * allows a wildcard-based algorithm policy that includes this algorithm,
454 * the resulting key allows the same algorithm.
455 * - If the policies do not allow any algorithm in common, this function
456 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
457 *
458 * The effect of this function on implementation-defined attributes is
459 * implementation-defined.
460 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200461 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200462 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200463 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200464 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100465 * \param[in] attributes The attributes for the new key.
466 * They are used as follows:
467 * - The key type and size may be 0. If either is
468 * nonzero, it must match the corresponding
469 * attribute of the source key.
470 * - The key location (the lifetime and, for
471 * persistent keys, the key identifier) is
472 * used directly.
473 * - The policy constraints (usage flags and
474 * algorithm policy) are combined from
475 * the source key and \p attributes so that
476 * both sets of restrictions apply, as
477 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200478 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200479 * key. For persistent keys, this is the key
480 * identifier defined in \p attributes.
481 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100482 *
Gilles Peskineed733552023-02-14 19:21:09 +0100483 * \retval #PSA_SUCCESS \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100484 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200485 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100486 * \retval #PSA_ERROR_ALREADY_EXISTS
487 * This is an attempt to create a persistent key, and there is
488 * already a persistent key with the given identifier.
489 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500490 * The lifetime or identifier in \p attributes are invalid, or
491 * the policy constraints on the source and specified in
492 * \p attributes are incompatible, or
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100493 * \p attributes specifies a key type or key size
494 * which does not match the attributes of the source key.
495 * \retval #PSA_ERROR_NOT_PERMITTED
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500496 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
497 * the source key is not exportable and its lifetime does not
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100498 * allow copying it to the target's lifetime.
Gilles Peskineed733552023-02-14 19:21:09 +0100499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
500 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
502 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
503 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
504 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
505 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
506 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100507 * \retval #PSA_ERROR_BAD_STATE
508 * The library has not been previously initialized by psa_crypto_init().
509 * It is implementation-dependent whether a failure to initialize
510 * results in this error code.
511 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200512psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100513 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200514 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100515
516
517/**
518 * \brief Destroy a key.
519 *
520 * This function destroys a key from both volatile
521 * memory and, if applicable, non-volatile storage. Implementations shall
Tom Cosgrove1797b052022-12-04 17:19:59 +0000522 * make a best effort to ensure that the key material cannot be recovered.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100523 *
524 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200525 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100526 *
527 * If a key is currently in use in a multipart operation, then destroying the
528 * key will cause the multipart operation to fail.
529 *
Ryan Everettf6f973c2024-03-14 15:54:07 +0000530 * \warning We can only guarantee that the the key material will
531 * eventually be wiped from memory. With threading enabled
532 * and during concurrent execution, copies of the key material may
533 * still exist until all threads have finished using the key.
534 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200535 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200536 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100537 *
538 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200539 * \p key was a valid identifier and the key material that it
540 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 * \retval #PSA_ERROR_NOT_PERMITTED
542 * The key cannot be erased because it is
543 * read-only, either due to a policy or due to physical restrictions.
544 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200545 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100547 * There was a failure in communication with the cryptoprocessor.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100548 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100549 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100550 * This error is typically a result of either storage corruption on a
551 * cleartext storage backend, or an attempt to read data that was
552 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100553 * \retval #PSA_ERROR_STORAGE_FAILURE
554 * The storage is corrupted. Implementations shall make a best effort
555 * to erase key material even in this stage, however applications
556 * should be aware that it may be impossible to guarantee that the
557 * key material is not recoverable in such cases.
558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
559 * An unexpected condition which is not a storage corruption or
560 * a communication failure occurred. The cryptoprocessor may have
561 * been compromised.
562 * \retval #PSA_ERROR_BAD_STATE
563 * The library has not been previously initialized by psa_crypto_init().
564 * It is implementation-dependent whether a failure to initialize
565 * results in this error code.
566 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200567psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100568
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100569/**@}*/
570
571/** \defgroup import_export Key import and export
572 * @{
573 */
574
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575/**
576 * \brief Import a key in binary format.
577 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100578 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100579 * documentation of psa_export_public_key() for the format of public keys
580 * and to the documentation of psa_export_key() for the format for
581 * other key types.
582 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200583 * The key data determines the key size. The attributes may optionally
584 * specify a key size; in this case it must match the size determined
585 * from the key data. A key size of 0 in \p attributes indicates that
586 * the key size is solely determined by the key data.
587 *
588 * Implementations must reject an attempt to import a key of size 0.
589 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100590 * This specification supports a single format for each key type.
591 * Implementations may support other formats as long as the standard
592 * format is supported. Implementations that support other formats
593 * should ensure that the formats are clearly unambiguous so as to
594 * minimize the risk that an invalid input is accidentally interpreted
595 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596 *
Gilles Peskine20628592019-04-19 19:29:50 +0200597 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200598 * The key size is always determined from the
599 * \p data buffer.
600 * If the key size in \p attributes is nonzero,
601 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200602 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200603 * For persistent keys, this is the key identifier
604 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200605 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100606 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200607 * buffer is interpreted according to the type declared
608 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * All implementations must support at least the format
610 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100611 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * the chosen type. Implementations may allow other
613 * formats, but should be conservative: implementations
614 * should err on the side of rejecting content if it
615 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200616 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100617 *
Gilles Peskine28538492018-07-11 17:34:00 +0200618 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100619 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100620 * If the key is persistent, the key material and the key's metadata
621 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200622 * \retval #PSA_ERROR_ALREADY_EXISTS
623 * This is an attempt to create a persistent key, and there is
624 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200625 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200626 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200627 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500629 * The key attributes, as a whole, are invalid, or
630 * the key data is not correctly formatted, or
631 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200632 * of the key data.
Gilles Peskineed733552023-02-14 19:21:09 +0100633 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
634 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
635 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
636 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
637 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
638 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
639 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
640 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300641 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300642 * The library has not been previously initialized by psa_crypto_init().
643 * It is implementation-dependent whether a failure to initialize
644 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200646psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200648 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200649 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100651
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652
653/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 * \brief Export a key in binary format.
655 *
656 * The output of this function can be passed to psa_import_key() to
657 * create an equivalent object.
658 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100659 * If the implementation of psa_import_key() supports other formats
660 * beyond the format specified here, the output from psa_export_key()
661 * must use the representation specified here, not the original
662 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100664 * For standard key types, the output format is as follows:
665 *
666 * - For symmetric keys (including MAC keys), the format is the
667 * raw bytes of the key.
668 * - For DES, the key data consists of 8 bytes. The parity bits must be
669 * correct.
670 * - For Triple-DES, the format is the concatenation of the
671 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200672 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200673 * is the non-encrypted DER encoding of the representation defined by
674 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
675 * ```
676 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200677 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200678 * modulus INTEGER, -- n
679 * publicExponent INTEGER, -- e
680 * privateExponent INTEGER, -- d
681 * prime1 INTEGER, -- p
682 * prime2 INTEGER, -- q
683 * exponent1 INTEGER, -- d mod (p-1)
684 * exponent2 INTEGER, -- d mod (q-1)
685 * coefficient INTEGER, -- (inverse of q) mod p
686 * }
687 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200688 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200689 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100690 * a representation of the private value as a `ceiling(m/8)`-byte string
691 * where `m` is the bit size associated with the curve, i.e. the bit size
692 * of the order of the curve's coordinate field. This byte string is
693 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100694 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
695 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
696 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200697 * For Weierstrass curves, this is the content of the `privateKey` field of
698 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
699 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100700 * For twisted Edwards curves, the private key is as defined by RFC 8032
701 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200702 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200703 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000704 * format is the representation of the private key `x` as a big-endian byte
705 * string. The length of the byte string is the private key size in bytes
706 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200707 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
708 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100709 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200710 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
711 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200712 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200713 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200714 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200716 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200717 * \param[out] data_length On success, the number of bytes
718 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100719 *
Gilles Peskineed733552023-02-14 19:21:09 +0100720 * \retval #PSA_SUCCESS \emptydescription
721 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200722 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200723 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Gilles Peskineed733552023-02-14 19:21:09 +0100724 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200725 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
726 * The size of the \p data buffer is too small. You can determine a
727 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100728 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200729 * where \c type is the key type
730 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
732 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
733 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
734 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300736 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300737 * The library has not been previously initialized by psa_crypto_init().
738 * It is implementation-dependent whether a failure to initialize
739 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200741psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 uint8_t *data,
743 size_t data_size,
744 size_t *data_length);
745
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100746/**
747 * \brief Export a public key or the public part of a key pair in binary format.
748 *
749 * The output of this function can be passed to psa_import_key() to
750 * create an object that is equivalent to the public key.
751 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000752 * This specification supports a single format for each key type.
753 * Implementations may support other formats as long as the standard
754 * format is supported. Implementations that support other formats
755 * should ensure that the formats are clearly unambiguous so as to
756 * minimize the risk that an invalid input is accidentally interpreted
757 * according to a different format.
758 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000759 * For standard key types, the output format is as follows:
760 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
761 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
762 * ```
763 * RSAPublicKey ::= SEQUENCE {
764 * modulus INTEGER, -- n
765 * publicExponent INTEGER } -- e
766 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100767 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200768 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100769 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
770 * by RFC 8032
771 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
772 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000773 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
774 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
775 * Let `m` be the bit size associated with the curve, i.e. the bit size of
776 * `q` for a curve over `F_q`. The representation consists of:
777 * - The byte 0x04;
778 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
779 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200780 * - For Diffie-Hellman key exchange public keys (key types for which
781 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000782 * the format is the representation of the public key `y = g^x mod p` as a
783 * big-endian byte string. The length of the byte string is the length of the
784 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200786 * Exporting a public key object or the public part of a key pair is
787 * always permitted, regardless of the key's usage flags.
788 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200789 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200790 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200791 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200792 * \param[out] data_length On success, the number of bytes
793 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100794 *
Gilles Peskineed733552023-02-14 19:21:09 +0100795 * \retval #PSA_SUCCESS \emptydescription
796 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200797 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200798 * The key is neither a public key nor a key pair.
Gilles Peskineed733552023-02-14 19:21:09 +0100799 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200800 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
801 * The size of the \p data buffer is too small. You can determine a
802 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100803 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200804 * where \c type is the key type
805 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100806 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
807 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
808 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
809 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
810 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300811 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300812 * The library has not been previously initialized by psa_crypto_init().
813 * It is implementation-dependent whether a failure to initialize
814 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100815 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200816psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100817 uint8_t *data,
818 size_t data_size,
819 size_t *data_length);
820
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100821
Gilles Peskine20035e32018-02-03 22:44:14 +0100822
823/**@}*/
824
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100825/** \defgroup hash Message digests
826 * @{
827 */
828
Gilles Peskine69647a42019-01-14 20:18:12 +0100829/** Calculate the hash (digest) of a message.
830 *
831 * \note To verify the hash of a message against an
832 * expected value, use psa_hash_compare() instead.
833 *
834 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
835 * such that #PSA_ALG_IS_HASH(\p alg) is true).
836 * \param[in] input Buffer containing the message to hash.
837 * \param input_length Size of the \p input buffer in bytes.
838 * \param[out] hash Buffer where the hash is to be written.
839 * \param hash_size Size of the \p hash buffer in bytes.
840 * \param[out] hash_length On success, the number of bytes
841 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100842 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100843 *
844 * \retval #PSA_SUCCESS
845 * Success.
846 * \retval #PSA_ERROR_NOT_SUPPORTED
847 * \p alg is not supported or is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100848 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100849 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
850 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +0100851 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
852 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
853 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
854 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100855 * \retval #PSA_ERROR_BAD_STATE
856 * The library has not been previously initialized by psa_crypto_init().
857 * It is implementation-dependent whether a failure to initialize
858 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100859 */
860psa_status_t psa_hash_compute(psa_algorithm_t alg,
861 const uint8_t *input,
862 size_t input_length,
863 uint8_t *hash,
864 size_t hash_size,
865 size_t *hash_length);
866
867/** Calculate the hash (digest) of a message and compare it with a
868 * reference value.
869 *
870 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
871 * such that #PSA_ALG_IS_HASH(\p alg) is true).
872 * \param[in] input Buffer containing the message to hash.
873 * \param input_length Size of the \p input buffer in bytes.
Valerio Setti1cb31cc2024-06-03 07:05:23 +0200874 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100875 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100876 *
877 * \retval #PSA_SUCCESS
878 * The expected hash is identical to the actual hash of the input.
879 * \retval #PSA_ERROR_INVALID_SIGNATURE
880 * The hash of the message was calculated successfully, but it
881 * differs from the expected hash.
882 * \retval #PSA_ERROR_NOT_SUPPORTED
883 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100884 * \retval #PSA_ERROR_INVALID_ARGUMENT
885 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskineed733552023-02-14 19:21:09 +0100886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
887 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
888 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
889 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100890 * \retval #PSA_ERROR_BAD_STATE
891 * The library has not been previously initialized by psa_crypto_init().
892 * It is implementation-dependent whether a failure to initialize
893 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100894 */
895psa_status_t psa_hash_compare(psa_algorithm_t alg,
896 const uint8_t *input,
897 size_t input_length,
898 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100899 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100900
Gilles Peskine308b91d2018-02-08 09:47:44 +0100901/** The type of the state data structure for multipart hash operations.
902 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000903 * Before calling any function on a hash operation object, the application must
904 * initialize it by any of the following means:
905 * - Set the structure to all-bits-zero, for example:
906 * \code
907 * psa_hash_operation_t operation;
908 * memset(&operation, 0, sizeof(operation));
909 * \endcode
910 * - Initialize the structure to logical zero values, for example:
911 * \code
912 * psa_hash_operation_t operation = {0};
913 * \endcode
914 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
915 * for example:
916 * \code
917 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
918 * \endcode
919 * - Assign the result of the function psa_hash_operation_init()
920 * to the structure, for example:
921 * \code
922 * psa_hash_operation_t operation;
923 * operation = psa_hash_operation_init();
924 * \endcode
925 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100926 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +0100927 * make any assumptions about the content of this structure.
928 * Implementation details can change in future versions without notice. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100929typedef struct psa_hash_operation_s psa_hash_operation_t;
930
Jaeden Amero6a25b412019-01-04 11:47:44 +0000931/** \def PSA_HASH_OPERATION_INIT
932 *
933 * This macro returns a suitable initializer for a hash operation object
934 * of type #psa_hash_operation_t.
935 */
Jaeden Amero6a25b412019-01-04 11:47:44 +0000936
937/** Return an initial value for a hash operation object.
938 */
939static psa_hash_operation_t psa_hash_operation_init(void);
940
Gilles Peskinef45adda2019-01-14 18:29:18 +0100941/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 *
943 * The sequence of operations to calculate a hash (message digest)
944 * is as follows:
945 * -# Allocate an operation object which will be passed to all the functions
946 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000947 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100948 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200949 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100950 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 * of the message each time. The hash that is calculated is the hash
952 * of the concatenation of these messages in order.
953 * -# To calculate the hash, call psa_hash_finish().
954 * To compare the hash with an expected value, call psa_hash_verify().
955 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100956 * If an error occurs at any step after a call to psa_hash_setup(), the
957 * operation will need to be reset by a call to psa_hash_abort(). The
958 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000959 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100960 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200961 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100962 * eventually terminate the operation. The following events terminate an
963 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100964 * - A successful call to psa_hash_finish() or psa_hash_verify().
965 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100966 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967 * \param[in,out] operation The operation object to set up. It must have
968 * been initialized as per the documentation for
969 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200970 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
971 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100972 *
Gilles Peskine28538492018-07-11 17:34:00 +0200973 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200975 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100976 * \p alg is not a supported hash algorithm.
977 * \retval #PSA_ERROR_INVALID_ARGUMENT
978 * \p alg is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
981 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
982 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100983 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500984 * The operation state is not valid (it must be inactive), or
985 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100986 * It is implementation-dependent whether a failure to initialize
987 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200989psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100990 psa_algorithm_t alg);
991
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992/** Add a message fragment to a multipart hash operation.
993 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200994 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100996 * If this function returns an error status, the operation enters an error
997 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200999 * \param[in,out] operation Active hash operation.
1000 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001001 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 *
Gilles Peskine28538492018-07-11 17:34:00 +02001003 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001005 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1006 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1007 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1008 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001009 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001010 * The operation state is not valid (it must be active), or
1011 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001012 * It is implementation-dependent whether a failure to initialize
1013 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001015psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1016 const uint8_t *input,
1017 size_t input_length);
1018
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019/** Finish the calculation of the hash of a message.
1020 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001021 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 * This function calculates the hash of the message formed by concatenating
1023 * the inputs passed to preceding calls to psa_hash_update().
1024 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001025 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001026 * If this function returns an error status, the operation enters an error
1027 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 *
1029 * \warning Applications should not call this function if they expect
1030 * a specific value for the hash. Call psa_hash_verify() instead.
1031 * Beware that comparing integrity or authenticity data such as
1032 * hash values with a function such as \c memcmp is risky
1033 * because the time taken by the comparison may leak information
1034 * about the hashed data which could allow an attacker to guess
1035 * a valid hash and thereby bypass security controls.
1036 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001037 * \param[in,out] operation Active hash operation.
1038 * \param[out] hash Buffer where the hash is to be written.
1039 * \param hash_size Size of the \p hash buffer in bytes.
1040 * \param[out] hash_length On success, the number of bytes
1041 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001042 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001043 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 *
Gilles Peskine28538492018-07-11 17:34:00 +02001045 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001047 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001048 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001049 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +01001051 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1053 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1054 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001055 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001056 * The operation state is not valid (it must be active), or
1057 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001058 * It is implementation-dependent whether a failure to initialize
1059 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001061psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1062 uint8_t *hash,
1063 size_t hash_size,
1064 size_t *hash_length);
1065
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066/** Finish the calculation of the hash of a message and compare it with
1067 * an expected value.
1068 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001069 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * This function calculates the hash of the message formed by concatenating
1071 * the inputs passed to preceding calls to psa_hash_update(). It then
1072 * compares the calculated hash with the expected hash passed as a
1073 * parameter to this function.
1074 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001075 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001076 * If this function returns an error status, the operation enters an error
1077 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 *
Gilles Peskine19067982018-03-20 17:54:53 +01001079 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 * comparison between the actual hash and the expected hash is performed
1081 * in constant time.
1082 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001083 * \param[in,out] operation Active hash operation.
1084 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001085 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 *
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001089 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * The hash of the message was calculated successfully, but it
1091 * differs from the expected hash.
Gilles Peskineed733552023-02-14 19:21:09 +01001092 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1094 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1095 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001096 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001097 * The operation state is not valid (it must be active), or
1098 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001099 * It is implementation-dependent whether a failure to initialize
1100 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001102psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1103 const uint8_t *hash,
1104 size_t hash_length);
1105
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106/** Abort a hash operation.
1107 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001109 * \p operation structure itself. Once aborted, the operation object
1110 * can be reused for another operation by calling
1111 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001114 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001116 * In particular, calling psa_hash_abort() after the operation has been
1117 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1118 * psa_hash_verify() is safe and has no effect.
1119 *
1120 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 *
Gilles Peskineed733552023-02-14 19:21:09 +01001122 * \retval #PSA_SUCCESS \emptydescription
1123 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1124 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1125 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001126 * \retval #PSA_ERROR_BAD_STATE
1127 * The library has not been previously initialized by psa_crypto_init().
1128 * It is implementation-dependent whether a failure to initialize
1129 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 */
1131psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001132
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001133/** Clone a hash operation.
1134 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001135 * This function copies the state of an ongoing hash operation to
1136 * a new operation object. In other words, this function is equivalent
1137 * to calling psa_hash_setup() on \p target_operation with the same
1138 * algorithm that \p source_operation was set up for, then
1139 * psa_hash_update() on \p target_operation with the same input that
1140 * that was passed to \p source_operation. After this function returns, the
1141 * two objects are independent, i.e. subsequent calls involving one of
1142 * the objects do not affect the other object.
1143 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001144 * \param[in] source_operation The active hash operation to clone.
1145 * \param[in,out] target_operation The operation object to set up.
1146 * It must be initialized but not active.
1147 *
Gilles Peskineed733552023-02-14 19:21:09 +01001148 * \retval #PSA_SUCCESS \emptydescription
1149 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1150 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1151 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001153 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001154 * The \p source_operation state is not valid (it must be active), or
1155 * the \p target_operation state is not valid (it must be inactive), or
1156 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001157 * It is implementation-dependent whether a failure to initialize
1158 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001159 */
1160psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1161 psa_hash_operation_t *target_operation);
1162
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001163/**@}*/
1164
Gilles Peskine8c9def32018-02-08 10:02:12 +01001165/** \defgroup MAC Message authentication codes
1166 * @{
1167 */
1168
Gilles Peskine69647a42019-01-14 20:18:12 +01001169/** Calculate the MAC (message authentication code) of a message.
1170 *
1171 * \note To verify the MAC of a message against an
1172 * expected value, use psa_mac_verify() instead.
1173 * Beware that comparing integrity or authenticity data such as
1174 * MAC values with a function such as \c memcmp is risky
1175 * because the time taken by the comparison may leak information
1176 * about the MAC value which could allow an attacker to guess
1177 * a valid MAC and thereby bypass security controls.
1178 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001179 * \param key Identifier of the key to use for the operation. It
1180 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001181 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001182 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001183 * \param[in] input Buffer containing the input message.
1184 * \param input_length Size of the \p input buffer in bytes.
1185 * \param[out] mac Buffer where the MAC value is to be written.
1186 * \param mac_size Size of the \p mac buffer in bytes.
1187 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001188 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 *
1190 * \retval #PSA_SUCCESS
1191 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001192 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1193 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001194 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001195 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 * \retval #PSA_ERROR_NOT_SUPPORTED
1197 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001198 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1199 * \p mac_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +01001200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1203 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001204 * \retval #PSA_ERROR_STORAGE_FAILURE
1205 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_BAD_STATE
1207 * The library has not been previously initialized by psa_crypto_init().
1208 * It is implementation-dependent whether a failure to initialize
1209 * results in this error code.
1210 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001211psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001212 psa_algorithm_t alg,
1213 const uint8_t *input,
1214 size_t input_length,
1215 uint8_t *mac,
1216 size_t mac_size,
1217 size_t *mac_length);
1218
1219/** Calculate the MAC of a message and compare it with a reference value.
1220 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001221 * \param key Identifier of the key to use for the operation. It
1222 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001224 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001225 * \param[in] input Buffer containing the input message.
1226 * \param input_length Size of the \p input buffer in bytes.
Valerio Setti1cb31cc2024-06-03 07:05:23 +02001227 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001228 * \param mac_length Size of the \p mac buffer in bytes.
1229 *
1230 * \retval #PSA_SUCCESS
1231 * The expected MAC is identical to the actual MAC of the input.
1232 * \retval #PSA_ERROR_INVALID_SIGNATURE
1233 * The MAC of the message was calculated successfully, but it
1234 * differs from the expected value.
Gilles Peskineed733552023-02-14 19:21:09 +01001235 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1236 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001237 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001238 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001239 * \retval #PSA_ERROR_NOT_SUPPORTED
1240 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001241 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1242 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1243 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1244 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001245 * \retval #PSA_ERROR_STORAGE_FAILURE
1246 * The key could not be retrieved from storage.
1247 * \retval #PSA_ERROR_BAD_STATE
1248 * The library has not been previously initialized by psa_crypto_init().
1249 * It is implementation-dependent whether a failure to initialize
1250 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001251 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001252psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001253 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001254 const uint8_t *input,
1255 size_t input_length,
1256 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001257 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001258
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001259/** The type of the state data structure for multipart MAC operations.
1260 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001261 * Before calling any function on a MAC operation object, the application must
1262 * initialize it by any of the following means:
1263 * - Set the structure to all-bits-zero, for example:
1264 * \code
1265 * psa_mac_operation_t operation;
1266 * memset(&operation, 0, sizeof(operation));
1267 * \endcode
1268 * - Initialize the structure to logical zero values, for example:
1269 * \code
1270 * psa_mac_operation_t operation = {0};
1271 * \endcode
1272 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1273 * for example:
1274 * \code
1275 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1276 * \endcode
1277 * - Assign the result of the function psa_mac_operation_init()
1278 * to the structure, for example:
1279 * \code
1280 * psa_mac_operation_t operation;
1281 * operation = psa_mac_operation_init();
1282 * \endcode
1283 *
Janos Follath2ba60792021-04-28 09:37:34 +01001284 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001285 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001286 * make any assumptions about the content of this structure.
1287 * Implementation details can change in future versions without notice. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001288typedef struct psa_mac_operation_s psa_mac_operation_t;
1289
Jaeden Amero769ce272019-01-04 11:48:03 +00001290/** \def PSA_MAC_OPERATION_INIT
1291 *
1292 * This macro returns a suitable initializer for a MAC operation object of type
1293 * #psa_mac_operation_t.
1294 */
Jaeden Amero769ce272019-01-04 11:48:03 +00001295
1296/** Return an initial value for a MAC operation object.
1297 */
1298static psa_mac_operation_t psa_mac_operation_init(void);
1299
Gilles Peskinef45adda2019-01-14 18:29:18 +01001300/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001301 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001302 * This function sets up the calculation of the MAC
1303 * (message authentication code) of a byte string.
1304 * To verify the MAC of a message against an
1305 * expected value, use psa_mac_verify_setup() instead.
1306 *
1307 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 * -# Allocate an operation object which will be passed to all the functions
1309 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001310 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001311 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1314 * of the message each time. The MAC that is calculated is the MAC
1315 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * -# At the end of the message, call psa_mac_sign_finish() to finish
1317 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001319 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1320 * operation will need to be reset by a call to psa_mac_abort(). The
1321 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001322 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * After a successful call to psa_mac_sign_setup(), the application must
1325 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001326 * - A successful call to psa_mac_sign_finish().
1327 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001329 * \param[in,out] operation The operation object to set up. It must have
1330 * been initialized as per the documentation for
1331 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001332 * \param key Identifier of the key to use for the operation. It
1333 * must remain valid until the operation terminates.
1334 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001335 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001336 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 *
Gilles Peskine28538492018-07-11 17:34:00 +02001338 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001340 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1341 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001342 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001343 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001344 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001345 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1348 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1349 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001350 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001351 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001352 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001353 * The operation state is not valid (it must be inactive), or
1354 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001355 * It is implementation-dependent whether a failure to initialize
1356 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001357 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001358psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001359 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001360 psa_algorithm_t alg);
1361
Gilles Peskinef45adda2019-01-14 18:29:18 +01001362/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 *
1364 * This function sets up the verification of the MAC
1365 * (message authentication code) of a byte string against an expected value.
1366 *
1367 * The sequence of operations to verify a MAC is as follows:
1368 * -# Allocate an operation object which will be passed to all the functions
1369 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001370 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001371 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1374 * of the message each time. The MAC that is calculated is the MAC
1375 * of the concatenation of these messages in order.
1376 * -# At the end of the message, call psa_mac_verify_finish() to finish
1377 * calculating the actual MAC of the message and verify it against
1378 * the expected value.
1379 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001380 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1381 * operation will need to be reset by a call to psa_mac_abort(). The
1382 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001383 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 *
1385 * After a successful call to psa_mac_verify_setup(), the application must
1386 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001387 * - A successful call to psa_mac_verify_finish().
1388 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001390 * \param[in,out] operation The operation object to set up. It must have
1391 * been initialized as per the documentation for
1392 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001393 * \param key Identifier of the key to use for the operation. It
1394 * must remain valid until the operation terminates.
1395 * It must allow the usage
1396 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001397 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1398 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 *
Gilles Peskine28538492018-07-11 17:34:00 +02001400 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001402 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1403 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001405 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001406 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001407 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1410 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1411 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001412 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001413 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001414 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001415 * The operation state is not valid (it must be inactive), or
1416 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001417 * It is implementation-dependent whether a failure to initialize
1418 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 */
1420psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001421 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001423
Gilles Peskinedcd14942018-07-12 00:30:52 +02001424/** Add a message fragment to a multipart MAC operation.
1425 *
1426 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1427 * before calling this function.
1428 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001429 * If this function returns an error status, the operation enters an error
1430 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001431 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001432 * \param[in,out] operation Active MAC operation.
1433 * \param[in] input Buffer containing the message fragment to add to
1434 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001435 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436 *
1437 * \retval #PSA_SUCCESS
1438 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1441 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1442 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1443 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001444 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001445 * The operation state is not valid (it must be active), or
1446 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001447 * It is implementation-dependent whether a failure to initialize
1448 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001450psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1451 const uint8_t *input,
1452 size_t input_length);
1453
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454/** Finish the calculation of the MAC of a message.
1455 *
1456 * The application must call psa_mac_sign_setup() before calling this function.
1457 * This function calculates the MAC of the message formed by concatenating
1458 * the inputs passed to preceding calls to psa_mac_update().
1459 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001460 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001461 * If this function returns an error status, the operation enters an error
1462 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001463 *
1464 * \warning Applications should not call this function if they expect
1465 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1466 * Beware that comparing integrity or authenticity data such as
1467 * MAC values with a function such as \c memcmp is risky
1468 * because the time taken by the comparison may leak information
1469 * about the MAC value which could allow an attacker to guess
1470 * a valid MAC and thereby bypass security controls.
1471 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * \param[in,out] operation Active MAC operation.
1473 * \param[out] mac Buffer where the MAC value is to be written.
1474 * \param mac_size Size of the \p mac buffer in bytes.
1475 * \param[out] mac_length On success, the number of bytes
1476 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001477 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001478 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001479 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001480 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 *
1482 * \retval #PSA_SUCCESS
1483 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001484 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001485 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001486 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskineed733552023-02-14 19:21:09 +01001487 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1489 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1490 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1491 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001492 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001493 * The operation state is not valid (it must be an active mac sign
1494 * operation), or the library has not been previously initialized
1495 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001496 * It is implementation-dependent whether a failure to initialize
1497 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001499psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1500 uint8_t *mac,
1501 size_t mac_size,
1502 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504/** Finish the calculation of the MAC of a message and compare it with
1505 * an expected value.
1506 *
1507 * The application must call psa_mac_verify_setup() before calling this function.
1508 * This function calculates the MAC of the message formed by concatenating
1509 * the inputs passed to preceding calls to psa_mac_update(). It then
1510 * compares the calculated MAC with the expected MAC passed as a
1511 * parameter to this function.
1512 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001513 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001514 * If this function returns an error status, the operation enters an error
1515 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 *
1517 * \note Implementations shall make the best effort to ensure that the
1518 * comparison between the actual MAC and the expected MAC is performed
1519 * in constant time.
1520 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001521 * \param[in,out] operation Active MAC operation.
1522 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001523 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 *
1525 * \retval #PSA_SUCCESS
1526 * The expected MAC is identical to the actual MAC of the message.
1527 * \retval #PSA_ERROR_INVALID_SIGNATURE
1528 * The MAC of the message was calculated successfully, but it
1529 * differs from the expected MAC.
Gilles Peskineed733552023-02-14 19:21:09 +01001530 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1532 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1533 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1534 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001535 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001536 * The operation state is not valid (it must be an active mac verify
1537 * operation), or the library has not been previously initialized
1538 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001539 * It is implementation-dependent whether a failure to initialize
1540 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001542psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1543 const uint8_t *mac,
1544 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546/** Abort a MAC operation.
1547 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001549 * \p operation structure itself. Once aborted, the operation object
1550 * can be reused for another operation by calling
1551 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001553 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001554 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001556 * In particular, calling psa_mac_abort() after the operation has been
1557 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1558 * psa_mac_verify_finish() is safe and has no effect.
1559 *
1560 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561 *
Gilles Peskineed733552023-02-14 19:21:09 +01001562 * \retval #PSA_SUCCESS \emptydescription
1563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1564 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1565 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001566 * \retval #PSA_ERROR_BAD_STATE
1567 * The library has not been previously initialized by psa_crypto_init().
1568 * It is implementation-dependent whether a failure to initialize
1569 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1572
1573/**@}*/
1574
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001575/** \defgroup cipher Symmetric ciphers
1576 * @{
1577 */
1578
Gilles Peskine69647a42019-01-14 20:18:12 +01001579/** Encrypt a message using a symmetric cipher.
1580 *
1581 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001582 * vector). Use the multipart operation interface with a
1583 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001584 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001585 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001586 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001587 * \param alg The cipher algorithm to compute
1588 * (\c PSA_ALG_XXX value such that
1589 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1590 * \param[in] input Buffer containing the message to encrypt.
1591 * \param input_length Size of the \p input buffer in bytes.
1592 * \param[out] output Buffer where the output is to be written.
1593 * The output contains the IV followed by
1594 * the ciphertext proper.
1595 * \param output_size Size of the \p output buffer in bytes.
1596 * \param[out] output_length On success, the number of bytes
1597 * that make up the output.
1598 *
1599 * \retval #PSA_SUCCESS
1600 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001601 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1602 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001603 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001604 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001605 * \retval #PSA_ERROR_NOT_SUPPORTED
1606 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001607 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1608 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1610 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1611 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1612 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001613 * \retval #PSA_ERROR_BAD_STATE
1614 * The library has not been previously initialized by psa_crypto_init().
1615 * It is implementation-dependent whether a failure to initialize
1616 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001617 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001618psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001619 psa_algorithm_t alg,
1620 const uint8_t *input,
1621 size_t input_length,
1622 uint8_t *output,
1623 size_t output_size,
1624 size_t *output_length);
1625
1626/** Decrypt a message using a symmetric cipher.
1627 *
1628 * This function decrypts a message encrypted with a symmetric cipher.
1629 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001630 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001631 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001632 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001633 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001634 * \param alg The cipher algorithm to compute
1635 * (\c PSA_ALG_XXX value such that
1636 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1637 * \param[in] input Buffer containing the message to decrypt.
1638 * This consists of the IV followed by the
1639 * ciphertext proper.
1640 * \param input_length Size of the \p input buffer in bytes.
1641 * \param[out] output Buffer where the plaintext is to be written.
1642 * \param output_size Size of the \p output buffer in bytes.
1643 * \param[out] output_length On success, the number of bytes
1644 * that make up the output.
1645 *
1646 * \retval #PSA_SUCCESS
1647 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001648 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1649 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001650 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001651 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001652 * \retval #PSA_ERROR_NOT_SUPPORTED
1653 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001654 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1655 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1656 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1657 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1658 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1659 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001660 * \retval #PSA_ERROR_BAD_STATE
1661 * The library has not been previously initialized by psa_crypto_init().
1662 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001663 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001664 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001665psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 psa_algorithm_t alg,
1667 const uint8_t *input,
1668 size_t input_length,
1669 uint8_t *output,
1670 size_t output_size,
1671 size_t *output_length);
1672
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001673/** The type of the state data structure for multipart cipher operations.
1674 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001675 * Before calling any function on a cipher operation object, the application
1676 * must initialize it by any of the following means:
1677 * - Set the structure to all-bits-zero, for example:
1678 * \code
1679 * psa_cipher_operation_t operation;
1680 * memset(&operation, 0, sizeof(operation));
1681 * \endcode
1682 * - Initialize the structure to logical zero values, for example:
1683 * \code
1684 * psa_cipher_operation_t operation = {0};
1685 * \endcode
1686 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1687 * for example:
1688 * \code
1689 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1690 * \endcode
1691 * - Assign the result of the function psa_cipher_operation_init()
1692 * to the structure, for example:
1693 * \code
1694 * psa_cipher_operation_t operation;
1695 * operation = psa_cipher_operation_init();
1696 * \endcode
1697 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001698 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001699 * make any assumptions about the content of this structure.
1700 * Implementation details can change in future versions without notice. */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1702
Jaeden Amero5bae2272019-01-04 11:48:27 +00001703/** \def PSA_CIPHER_OPERATION_INIT
1704 *
1705 * This macro returns a suitable initializer for a cipher operation object of
1706 * type #psa_cipher_operation_t.
1707 */
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708
1709/** Return an initial value for a cipher operation object.
1710 */
1711static psa_cipher_operation_t psa_cipher_operation_init(void);
1712
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713/** Set the key for a multipart symmetric encryption operation.
1714 *
1715 * The sequence of operations to encrypt a message with a symmetric cipher
1716 * is as follows:
1717 * -# Allocate an operation object which will be passed to all the functions
1718 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001719 * -# Initialize the operation object with one of the methods described in the
1720 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001721 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001722 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001723 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001724 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001725 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726 * requires a specific IV value.
1727 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1728 * of the message each time.
1729 * -# Call psa_cipher_finish().
1730 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001731 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1732 * the operation will need to be reset by a call to psa_cipher_abort(). The
1733 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001734 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001736 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001737 * eventually terminate the operation. The following events terminate an
1738 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001739 * - A successful call to psa_cipher_finish().
1740 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001742 * \param[in,out] operation The operation object to set up. It must have
1743 * been initialized as per the documentation for
1744 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001745 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001746 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001747 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001748 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param alg The cipher algorithm to compute
1750 * (\c PSA_ALG_XXX value such that
1751 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752 *
Gilles Peskine28538492018-07-11 17:34:00 +02001753 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001755 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1756 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001757 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001758 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001759 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001760 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1762 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1763 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1764 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1765 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001766 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001767 * The operation state is not valid (it must be inactive), or
1768 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001769 * It is implementation-dependent whether a failure to initialize
1770 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001772psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001773 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001774 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775
1776/** Set the key for a multipart symmetric decryption operation.
1777 *
1778 * The sequence of operations to decrypt a message with a symmetric cipher
1779 * is as follows:
1780 * -# Allocate an operation object which will be passed to all the functions
1781 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001782 * -# Initialize the operation object with one of the methods described in the
1783 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001784 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001785 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001786 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001787 * decryption. If the IV is prepended to the ciphertext, you can call
1788 * psa_cipher_update() on a buffer containing the IV followed by the
1789 * beginning of the message.
1790 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1791 * of the message each time.
1792 * -# Call psa_cipher_finish().
1793 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001794 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1795 * the operation will need to be reset by a call to psa_cipher_abort(). The
1796 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001797 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001798 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001799 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001800 * eventually terminate the operation. The following events terminate an
1801 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001802 * - A successful call to psa_cipher_finish().
1803 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001804 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001805 * \param[in,out] operation The operation object to set up. It must have
1806 * been initialized as per the documentation for
1807 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001808 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001809 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001810 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001811 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001812 * \param alg The cipher algorithm to compute
1813 * (\c PSA_ALG_XXX value such that
1814 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 *
Gilles Peskine28538492018-07-11 17:34:00 +02001816 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001817 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001818 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1819 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001820 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001821 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001822 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001823 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001824 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1825 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1826 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1827 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1828 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001829 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001830 * The operation state is not valid (it must be inactive), or
1831 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001832 * It is implementation-dependent whether a failure to initialize
1833 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001835psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001836 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001837 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001838
Gilles Peskinedcd14942018-07-12 00:30:52 +02001839/** Generate an IV for a symmetric encryption operation.
1840 *
1841 * This function generates a random IV (initialization vector), nonce
1842 * or initial counter value for the encryption operation as appropriate
1843 * for the chosen algorithm, key type and key size.
1844 *
1845 * The application must call psa_cipher_encrypt_setup() before
1846 * calling this function.
1847 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001848 * If this function returns an error status, the operation enters an error
1849 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001850 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * \param[in,out] operation Active cipher operation.
1852 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001853 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001854 * \param[out] iv_length On success, the number of bytes of the
1855 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001856 *
1857 * \retval #PSA_SUCCESS
1858 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001860 * The size of the \p iv buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1862 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1863 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1864 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1865 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001866 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001867 * The operation state is not valid (it must be active, with no IV set),
1868 * or the library has not been previously initialized
1869 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001870 * It is implementation-dependent whether a failure to initialize
1871 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001873psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001874 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001875 size_t iv_size,
1876 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878/** Set the IV for a symmetric encryption or decryption operation.
1879 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001880 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * or initial counter value for the encryption or decryption operation.
1882 *
1883 * The application must call psa_cipher_encrypt_setup() before
1884 * calling this function.
1885 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001886 * If this function returns an error status, the operation enters an error
1887 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 *
1889 * \note When encrypting, applications should use psa_cipher_generate_iv()
1890 * instead of this function, unless implementing a protocol that requires
1891 * a non-random IV.
1892 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001893 * \param[in,out] operation Active cipher operation.
1894 * \param[in] iv Buffer containing the IV to use.
1895 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 *
1897 * \retval #PSA_SUCCESS
1898 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001900 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 * or the chosen algorithm does not use an IV.
Gilles Peskineed733552023-02-14 19:21:09 +01001902 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1904 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1905 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1906 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001907 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001908 * The operation state is not valid (it must be an active cipher
1909 * encrypt operation, with no IV set), or the library has not been
1910 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001911 * It is implementation-dependent whether a failure to initialize
1912 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001914psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001915 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001916 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001917
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918/** Encrypt or decrypt a message fragment in an active cipher operation.
1919 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001920 * Before calling this function, you must:
1921 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1922 * The choice of setup function determines whether this function
1923 * encrypts or decrypts its input.
1924 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1925 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001927 * If this function returns an error status, the operation enters an error
1928 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[in,out] operation Active cipher operation.
1931 * \param[in] input Buffer containing the message fragment to
1932 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001933 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001934 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001935 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001936 * \param[out] output_length On success, the number of bytes
1937 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001938 *
1939 * \retval #PSA_SUCCESS
1940 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1942 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001943 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1944 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1945 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1946 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1947 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001948 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001949 * The operation state is not valid (it must be active, with an IV set
1950 * if required for the algorithm), or the library has not been
1951 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001952 * It is implementation-dependent whether a failure to initialize
1953 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001954 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001955psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1956 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001957 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001958 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001959 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001960 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961
Gilles Peskinedcd14942018-07-12 00:30:52 +02001962/** Finish encrypting or decrypting a message in a cipher operation.
1963 *
1964 * The application must call psa_cipher_encrypt_setup() or
1965 * psa_cipher_decrypt_setup() before calling this function. The choice
1966 * of setup function determines whether this function encrypts or
1967 * decrypts its input.
1968 *
1969 * This function finishes the encryption or decryption of the message
1970 * formed by concatenating the inputs passed to preceding calls to
1971 * psa_cipher_update().
1972 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001973 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001974 * If this function returns an error status, the operation enters an error
1975 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[in,out] operation Active cipher operation.
1978 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001979 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001980 * \param[out] output_length On success, the number of bytes
1981 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982 *
1983 * \retval #PSA_SUCCESS
1984 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001985 * \retval #PSA_ERROR_INVALID_ARGUMENT
1986 * The total input size passed to this operation is not valid for
1987 * this particular algorithm. For example, the algorithm is a based
1988 * on block cipher and requires a whole number of blocks, but the
1989 * total input size is not a multiple of the block size.
1990 * \retval #PSA_ERROR_INVALID_PADDING
1991 * This is a decryption operation for an algorithm that includes
1992 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1994 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001995 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1996 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1997 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1998 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1999 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002000 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002001 * The operation state is not valid (it must be active, with an IV set
2002 * if required for the algorithm), or the library has not been
2003 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002004 * It is implementation-dependent whether a failure to initialize
2005 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002007psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002008 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002009 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002010 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002011
Gilles Peskinedcd14942018-07-12 00:30:52 +02002012/** Abort a cipher operation.
2013 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002014 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002015 * \p operation structure itself. Once aborted, the operation object
2016 * can be reused for another operation by calling
2017 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002019 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002020 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002022 * In particular, calling psa_cipher_abort() after the operation has been
2023 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2024 * is safe and has no effect.
2025 *
2026 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002027 *
Gilles Peskineed733552023-02-14 19:21:09 +01002028 * \retval #PSA_SUCCESS \emptydescription
2029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2030 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2031 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002032 * \retval #PSA_ERROR_BAD_STATE
2033 * The library has not been previously initialized by psa_crypto_init().
2034 * It is implementation-dependent whether a failure to initialize
2035 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002037psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2038
2039/**@}*/
2040
Gilles Peskine3b555712018-03-03 21:27:57 +01002041/** \defgroup aead Authenticated encryption with associated data (AEAD)
2042 * @{
2043 */
2044
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002045/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002046 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002047 * \param key Identifier of the key to use for the
2048 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002049 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002050 * \param alg The AEAD algorithm to compute
2051 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002052 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002053 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056 * but not encrypted.
2057 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002058 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002059 * encrypted.
2060 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002061 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002062 * encrypted data. The additional data is not
2063 * part of this output. For algorithms where the
2064 * encrypted data and the authentication tag
2065 * are defined as separate outputs, the
2066 * authentication tag is appended to the
2067 * encrypted data.
2068 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002069 * This must be appropriate for the selected
2070 * algorithm and key:
2071 * - A sufficient output size is
2072 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2073 * \p alg, \p plaintext_length) where
2074 * \c key_type is the type of \p key.
2075 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2076 * plaintext_length) evaluates to the maximum
2077 * ciphertext size of any supported AEAD
2078 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002080 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002081 *
Gilles Peskine28538492018-07-11 17:34:00 +02002082 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002083 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002084 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2085 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002087 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002088 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002089 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002090 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002091 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002092 * \p ciphertext_size is too small.
2093 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2094 * \p plaintext_length) or
2095 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2096 * determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002097 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2098 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2099 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2100 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002101 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002102 * The library has not been previously initialized by psa_crypto_init().
2103 * It is implementation-dependent whether a failure to initialize
2104 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002105 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002106psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002107 psa_algorithm_t alg,
2108 const uint8_t *nonce,
2109 size_t nonce_length,
2110 const uint8_t *additional_data,
2111 size_t additional_data_length,
2112 const uint8_t *plaintext,
2113 size_t plaintext_length,
2114 uint8_t *ciphertext,
2115 size_t ciphertext_size,
2116 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002117
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002118/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002119 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002120 * \param key Identifier of the key to use for the
2121 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002122 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123 * \param alg The AEAD algorithm to compute
2124 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002125 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002126 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002127 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * but not encrypted.
2130 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002131 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002132 * encrypted. For algorithms where the
2133 * encrypted data and the authentication tag
2134 * are defined as separate inputs, the buffer
2135 * must contain the encrypted data followed
2136 * by the authentication tag.
2137 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002138 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002139 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002140 * This must be appropriate for the selected
2141 * algorithm and key:
2142 * - A sufficient output size is
2143 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2144 * \p alg, \p ciphertext_length) where
2145 * \c key_type is the type of \p key.
2146 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2147 * ciphertext_length) evaluates to the maximum
2148 * plaintext size of any supported AEAD
2149 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002150 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002151 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002152 *
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002154 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002155 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002156 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002157 * The ciphertext is not authentic.
Gilles Peskineed733552023-02-14 19:21:09 +01002158 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002159 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002160 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002162 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002164 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002165 * \p plaintext_size is too small.
2166 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2167 * \p ciphertext_length) or
2168 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2169 * to determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2171 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2172 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2173 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002174 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002175 * The library has not been previously initialized by psa_crypto_init().
2176 * It is implementation-dependent whether a failure to initialize
2177 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002178 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002179psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002180 psa_algorithm_t alg,
2181 const uint8_t *nonce,
2182 size_t nonce_length,
2183 const uint8_t *additional_data,
2184 size_t additional_data_length,
2185 const uint8_t *ciphertext,
2186 size_t ciphertext_length,
2187 uint8_t *plaintext,
2188 size_t plaintext_size,
2189 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002190
Gilles Peskine30a9e412019-01-14 18:36:12 +01002191/** The type of the state data structure for multipart AEAD operations.
2192 *
2193 * Before calling any function on an AEAD operation object, the application
2194 * must initialize it by any of the following means:
2195 * - Set the structure to all-bits-zero, for example:
2196 * \code
2197 * psa_aead_operation_t operation;
2198 * memset(&operation, 0, sizeof(operation));
2199 * \endcode
2200 * - Initialize the structure to logical zero values, for example:
2201 * \code
2202 * psa_aead_operation_t operation = {0};
2203 * \endcode
2204 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2205 * for example:
2206 * \code
2207 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2208 * \endcode
2209 * - Assign the result of the function psa_aead_operation_init()
2210 * to the structure, for example:
2211 * \code
2212 * psa_aead_operation_t operation;
2213 * operation = psa_aead_operation_init();
2214 * \endcode
2215 *
2216 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01002217 * make any assumptions about the content of this structure.
2218 * Implementation details can change in future versions without notice. */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002219typedef struct psa_aead_operation_s psa_aead_operation_t;
2220
2221/** \def PSA_AEAD_OPERATION_INIT
2222 *
2223 * This macro returns a suitable initializer for an AEAD operation object of
2224 * type #psa_aead_operation_t.
2225 */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002226
2227/** Return an initial value for an AEAD operation object.
2228 */
2229static psa_aead_operation_t psa_aead_operation_init(void);
2230
2231/** Set the key for a multipart authenticated encryption operation.
2232 *
2233 * The sequence of operations to encrypt a message with authentication
2234 * is as follows:
2235 * -# Allocate an operation object which will be passed to all the functions
2236 * listed here.
2237 * -# Initialize the operation object with one of the methods described in the
2238 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002239 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002240 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002241 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2242 * inputs to the subsequent calls to psa_aead_update_ad() and
2243 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2244 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002245 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2246 * generate or set the nonce. You should use
2247 * psa_aead_generate_nonce() unless the protocol you are implementing
2248 * requires a specific nonce value.
2249 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2250 * of the non-encrypted additional authenticated data each time.
2251 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002252 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002253 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002254 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002255 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2256 * the operation will need to be reset by a call to psa_aead_abort(). The
2257 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002258 * has been initialized.
2259 *
2260 * After a successful call to psa_aead_encrypt_setup(), the application must
2261 * eventually terminate the operation. The following events terminate an
2262 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002263 * - A successful call to psa_aead_finish().
2264 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 *
2266 * \param[in,out] operation The operation object to set up. It must have
2267 * been initialized as per the documentation for
2268 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002269 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002271 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002272 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002273 * \param alg The AEAD algorithm to compute
2274 * (\c PSA_ALG_XXX value such that
2275 * #PSA_ALG_IS_AEAD(\p alg) is true).
2276 *
2277 * \retval #PSA_SUCCESS
2278 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002279 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002280 * The operation state is not valid (it must be inactive), or
2281 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskineed733552023-02-14 19:21:09 +01002282 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2283 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002285 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 * \retval #PSA_ERROR_NOT_SUPPORTED
2287 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002288 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2289 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2290 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2291 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002292 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002293 * The library has not been previously initialized by psa_crypto_init().
2294 * It is implementation-dependent whether a failure to initialize
2295 * results in this error code.
2296 */
2297psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002298 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002299 psa_algorithm_t alg);
2300
2301/** Set the key for a multipart authenticated decryption operation.
2302 *
2303 * The sequence of operations to decrypt a message with authentication
2304 * is as follows:
2305 * -# Allocate an operation object which will be passed to all the functions
2306 * listed here.
2307 * -# Initialize the operation object with one of the methods described in the
2308 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002309 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002311 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2312 * inputs to the subsequent calls to psa_aead_update_ad() and
2313 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2314 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2316 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2317 * of the non-encrypted additional authenticated data each time.
2318 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002319 * of the ciphertext to decrypt each time.
2320 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002321 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002322 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2323 * the operation will need to be reset by a call to psa_aead_abort(). The
2324 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002325 * has been initialized.
2326 *
2327 * After a successful call to psa_aead_decrypt_setup(), the application must
2328 * eventually terminate the operation. The following events terminate an
2329 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002330 * - A successful call to psa_aead_verify().
2331 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002332 *
2333 * \param[in,out] operation The operation object to set up. It must have
2334 * been initialized as per the documentation for
2335 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002336 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002338 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002339 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002340 * \param alg The AEAD algorithm to compute
2341 * (\c PSA_ALG_XXX value such that
2342 * #PSA_ALG_IS_AEAD(\p alg) is true).
2343 *
2344 * \retval #PSA_SUCCESS
2345 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002346 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2347 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002349 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002350 * \retval #PSA_ERROR_NOT_SUPPORTED
2351 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002352 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2353 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2354 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2355 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2356 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002357 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002358 * The operation state is not valid (it must be inactive), or the
2359 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 * It is implementation-dependent whether a failure to initialize
2361 * results in this error code.
2362 */
2363psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002364 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 psa_algorithm_t alg);
2366
2367/** Generate a random nonce for an authenticated encryption operation.
2368 *
2369 * This function generates a random nonce for the authenticated encryption
2370 * operation with an appropriate size for the chosen algorithm, key type
2371 * and key size.
2372 *
2373 * The application must call psa_aead_encrypt_setup() before
2374 * calling this function.
2375 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002376 * If this function returns an error status, the operation enters an error
2377 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002378 *
2379 * \param[in,out] operation Active AEAD operation.
2380 * \param[out] nonce Buffer where the generated nonce is to be
2381 * written.
2382 * \param nonce_size Size of the \p nonce buffer in bytes.
2383 * \param[out] nonce_length On success, the number of bytes of the
2384 * generated nonce.
2385 *
2386 * \retval #PSA_SUCCESS
2387 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002388 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2389 * The size of the \p nonce buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01002390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2392 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2393 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2394 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002395 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002396 * The operation state is not valid (it must be an active aead encrypt
2397 * operation, with no nonce set), or the library has not been
2398 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002399 * It is implementation-dependent whether a failure to initialize
2400 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 */
2402psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002403 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 size_t nonce_size,
2405 size_t *nonce_length);
2406
2407/** Set the nonce for an authenticated encryption or decryption operation.
2408 *
2409 * This function sets the nonce for the authenticated
2410 * encryption or decryption operation.
2411 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002412 * The application must call psa_aead_encrypt_setup() or
2413 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002415 * If this function returns an error status, the operation enters an error
2416 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002418 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 * instead of this function, unless implementing a protocol that requires
2420 * a non-random IV.
2421 *
2422 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002423 * \param[in] nonce Buffer containing the nonce to use.
2424 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 *
2426 * \retval #PSA_SUCCESS
2427 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002428 * \retval #PSA_ERROR_INVALID_ARGUMENT
2429 * The size of \p nonce is not acceptable for the chosen algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002430 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2432 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2433 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2434 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002435 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002436 * The operation state is not valid (it must be active, with no nonce
2437 * set), or the library has not been previously initialized
2438 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002439 * It is implementation-dependent whether a failure to initialize
2440 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002441 */
2442psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002443 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002444 size_t nonce_length);
2445
Gilles Peskinebc59c852019-01-17 15:26:08 +01002446/** Declare the lengths of the message and additional data for AEAD.
2447 *
2448 * The application must call this function before calling
2449 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2450 * the operation requires it. If the algorithm does not require it,
2451 * calling this function is optional, but if this function is called
2452 * then the implementation must enforce the lengths.
2453 *
2454 * You may call this function before or after setting the nonce with
2455 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2456 *
2457 * - For #PSA_ALG_CCM, calling this function is required.
2458 * - For the other AEAD algorithms defined in this specification, calling
2459 * this function is not required.
2460 * - For vendor-defined algorithm, refer to the vendor documentation.
2461 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002462 * If this function returns an error status, the operation enters an error
2463 * state and must be aborted by calling psa_aead_abort().
2464 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002465 * \param[in,out] operation Active AEAD operation.
2466 * \param ad_length Size of the non-encrypted additional
2467 * authenticated data in bytes.
2468 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2469 *
2470 * \retval #PSA_SUCCESS
2471 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002472 * \retval #PSA_ERROR_INVALID_ARGUMENT
2473 * At least one of the lengths is not acceptable for the chosen
2474 * algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2476 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2477 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2478 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002479 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002480 * The operation state is not valid (it must be active, and
2481 * psa_aead_update_ad() and psa_aead_update() must not have been
2482 * called yet), or the library has not been previously initialized
2483 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002484 * It is implementation-dependent whether a failure to initialize
2485 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002486 */
2487psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2488 size_t ad_length,
2489 size_t plaintext_length);
2490
Gilles Peskine30a9e412019-01-14 18:36:12 +01002491/** Pass additional data to an active AEAD operation.
2492 *
2493 * Additional data is authenticated, but not encrypted.
2494 *
2495 * You may call this function multiple times to pass successive fragments
2496 * of the additional data. You may not call this function after passing
2497 * data to encrypt or decrypt with psa_aead_update().
2498 *
2499 * Before calling this function, you must:
2500 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2501 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2502 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002503 * If this function returns an error status, the operation enters an error
2504 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505 *
2506 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2507 * there is no guarantee that the input is valid. Therefore, until
2508 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2509 * treat the input as untrusted and prepare to undo any action that
2510 * depends on the input if psa_aead_verify() returns an error status.
2511 *
2512 * \param[in,out] operation Active AEAD operation.
2513 * \param[in] input Buffer containing the fragment of
2514 * additional data.
2515 * \param input_length Size of the \p input buffer in bytes.
2516 *
2517 * \retval #PSA_SUCCESS
2518 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002519 * \retval #PSA_ERROR_INVALID_ARGUMENT
2520 * The total input length overflows the additional data length that
2521 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002522 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2523 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2524 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2525 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2526 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002527 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002528 * The operation state is not valid (it must be active, have a nonce
2529 * set, have lengths set if required by the algorithm, and
2530 * psa_aead_update() must not have been called yet), or the library
2531 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002532 * It is implementation-dependent whether a failure to initialize
2533 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002534 */
2535psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2536 const uint8_t *input,
2537 size_t input_length);
2538
2539/** Encrypt or decrypt a message fragment in an active AEAD operation.
2540 *
2541 * Before calling this function, you must:
2542 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2543 * The choice of setup function determines whether this function
2544 * encrypts or decrypts its input.
2545 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2546 * 3. Call psa_aead_update_ad() to pass all the additional data.
2547 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002548 * If this function returns an error status, the operation enters an error
2549 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002550 *
2551 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2552 * there is no guarantee that the input is valid. Therefore, until
2553 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2554 * - Do not use the output in any way other than storing it in a
2555 * confidential location. If you take any action that depends
2556 * on the tentative decrypted data, this action will need to be
2557 * undone if the input turns out not to be valid. Furthermore,
2558 * if an adversary can observe that this action took place
2559 * (for example through timing), they may be able to use this
2560 * fact as an oracle to decrypt any message encrypted with the
2561 * same key.
2562 * - In particular, do not copy the output anywhere but to a
2563 * memory or storage space that you have exclusive access to.
2564 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002565 * This function does not require the input to be aligned to any
2566 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002567 * a whole block at a time, it must consume all the input provided, but
2568 * it may delay the end of the corresponding output until a subsequent
2569 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2570 * provides sufficient input. The amount of data that can be delayed
2571 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002572 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002573 * \param[in,out] operation Active AEAD operation.
2574 * \param[in] input Buffer containing the message fragment to
2575 * encrypt or decrypt.
2576 * \param input_length Size of the \p input buffer in bytes.
2577 * \param[out] output Buffer where the output is to be written.
2578 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002579 * This must be appropriate for the selected
2580 * algorithm and key:
2581 * - A sufficient output size is
2582 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2583 * \c alg, \p input_length) where
2584 * \c key_type is the type of key and \c alg is
2585 * the algorithm that were used to set up the
2586 * operation.
2587 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2588 * input_length) evaluates to the maximum
2589 * output size of any supported AEAD
2590 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002591 * \param[out] output_length On success, the number of bytes
2592 * that make up the returned output.
2593 *
2594 * \retval #PSA_SUCCESS
2595 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002596 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2597 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002598 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2599 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2600 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002601 * \retval #PSA_ERROR_INVALID_ARGUMENT
2602 * The total length of input to psa_aead_update_ad() so far is
2603 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002604 * specified with psa_aead_set_lengths(), or
2605 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002606 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002607 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2608 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2609 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2610 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2611 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002612 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002613 * The operation state is not valid (it must be active, have a nonce
2614 * set, and have lengths set if required by the algorithm), or the
2615 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002616 * It is implementation-dependent whether a failure to initialize
2617 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002618 */
2619psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2620 const uint8_t *input,
2621 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002622 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002623 size_t output_size,
2624 size_t *output_length);
2625
2626/** Finish encrypting a message in an AEAD operation.
2627 *
2628 * The operation must have been set up with psa_aead_encrypt_setup().
2629 *
2630 * This function finishes the authentication of the additional data
2631 * formed by concatenating the inputs passed to preceding calls to
2632 * psa_aead_update_ad() with the plaintext formed by concatenating the
2633 * inputs passed to preceding calls to psa_aead_update().
2634 *
2635 * This function has two output buffers:
2636 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002637 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002638 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002639 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002640 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002641 * If this function returns an error status, the operation enters an error
2642 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002643 *
2644 * \param[in,out] operation Active AEAD operation.
2645 * \param[out] ciphertext Buffer where the last part of the ciphertext
2646 * is to be written.
2647 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002648 * This must be appropriate for the selected
2649 * algorithm and key:
2650 * - A sufficient output size is
2651 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2652 * \c alg) where \c key_type is the type of key
2653 * and \c alg is the algorithm that were used to
2654 * set up the operation.
2655 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2656 * the maximum output size of any supported AEAD
2657 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002658 * \param[out] ciphertext_length On success, the number of bytes of
2659 * returned ciphertext.
2660 * \param[out] tag Buffer where the authentication tag is
2661 * to be written.
2662 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002663 * This must be appropriate for the selected
2664 * algorithm and key:
2665 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2666 * key_type, \c key_bits, \c alg) where
2667 * \c key_type and \c key_bits are the type and
2668 * bit-size of the key, and \c alg is the
2669 * algorithm that were used in the call to
2670 * psa_aead_encrypt_setup().
2671 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2672 * maximum tag size of any supported AEAD
2673 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002674 * \param[out] tag_length On success, the number of bytes
2675 * that make up the returned tag.
2676 *
2677 * \retval #PSA_SUCCESS
2678 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002680 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002681 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2682 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2683 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2684 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2685 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002686 * \retval #PSA_ERROR_INVALID_ARGUMENT
2687 * The total length of input to psa_aead_update_ad() so far is
2688 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002689 * specified with psa_aead_set_lengths(), or
2690 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002691 * less than the plaintext length that was previously
2692 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2694 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2695 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2696 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2697 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002698 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002699 * The operation state is not valid (it must be an active encryption
2700 * operation with a nonce set), or the library has not been previously
2701 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002702 * It is implementation-dependent whether a failure to initialize
2703 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002704 */
2705psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002706 uint8_t *ciphertext,
2707 size_t ciphertext_size,
2708 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002709 uint8_t *tag,
2710 size_t tag_size,
2711 size_t *tag_length);
2712
2713/** Finish authenticating and decrypting a message in an AEAD operation.
2714 *
2715 * The operation must have been set up with psa_aead_decrypt_setup().
2716 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002717 * This function finishes the authenticated decryption of the message
2718 * components:
2719 *
2720 * - The additional data consisting of the concatenation of the inputs
2721 * passed to preceding calls to psa_aead_update_ad().
2722 * - The ciphertext consisting of the concatenation of the inputs passed to
2723 * preceding calls to psa_aead_update().
2724 * - The tag passed to this function call.
2725 *
2726 * If the authentication tag is correct, this function outputs any remaining
2727 * plaintext and reports success. If the authentication tag is not correct,
2728 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002729 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002730 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002731 * If this function returns an error status, the operation enters an error
2732 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002733 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002734 * \note Implementations shall make the best effort to ensure that the
2735 * comparison between the actual tag and the expected tag is performed
2736 * in constant time.
2737 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002738 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002739 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002740 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002741 * from previous calls to psa_aead_update()
2742 * that could not be processed until the end
2743 * of the input.
2744 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002745 * This must be appropriate for the selected algorithm and key:
2746 * - A sufficient output size is
2747 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2748 * \c alg) where \c key_type is the type of key
2749 * and \c alg is the algorithm that were used to
2750 * set up the operation.
2751 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2752 * the maximum output size of any supported AEAD
2753 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002754 * \param[out] plaintext_length On success, the number of bytes of
2755 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002756 * \param[in] tag Buffer containing the authentication tag.
2757 * \param tag_length Size of the \p tag buffer in bytes.
2758 *
2759 * \retval #PSA_SUCCESS
2760 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002761 * \retval #PSA_ERROR_INVALID_SIGNATURE
2762 * The calculations were successful, but the authentication tag is
2763 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002764 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2765 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002766 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2767 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2768 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002769 * \retval #PSA_ERROR_INVALID_ARGUMENT
2770 * The total length of input to psa_aead_update_ad() so far is
2771 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002772 * specified with psa_aead_set_lengths(), or
2773 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002774 * less than the plaintext length that was previously
2775 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002776 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2777 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2778 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2779 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2780 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002781 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002782 * The operation state is not valid (it must be an active decryption
2783 * operation with a nonce set), or the library has not been previously
2784 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002785 * It is implementation-dependent whether a failure to initialize
2786 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002787 */
2788psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002789 uint8_t *plaintext,
2790 size_t plaintext_size,
2791 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002792 const uint8_t *tag,
2793 size_t tag_length);
2794
2795/** Abort an AEAD operation.
2796 *
2797 * Aborting an operation frees all associated resources except for the
2798 * \p operation structure itself. Once aborted, the operation object
2799 * can be reused for another operation by calling
2800 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2801 *
2802 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002803 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002804 *
2805 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002806 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2807 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002808 *
2809 * \param[in,out] operation Initialized AEAD operation.
2810 *
Gilles Peskineed733552023-02-14 19:21:09 +01002811 * \retval #PSA_SUCCESS \emptydescription
2812 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2813 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2814 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002815 * \retval #PSA_ERROR_BAD_STATE
2816 * The library has not been previously initialized by psa_crypto_init().
2817 * It is implementation-dependent whether a failure to initialize
2818 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002819 */
2820psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2821
Gilles Peskine3b555712018-03-03 21:27:57 +01002822/**@}*/
2823
Gilles Peskine20035e32018-02-03 22:44:14 +01002824/** \defgroup asymmetric Asymmetric cryptography
2825 * @{
2826 */
2827
2828/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002829 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2830 * this includes the hashing step.
2831 *
2832 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2833 * a multi-part hash operation and then pass the resulting hash to
2834 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2835 * hash algorithm to use.
2836 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002837 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838 * It must be an asymmetric key pair. The key must
2839 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002840 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2842 * is true), that is compatible with the type of
2843 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002844 * \param[in] input The input message to sign.
2845 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002847 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848 * must be appropriate for the selected
2849 * algorithm and key:
2850 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002851 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2852 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853 * bit-size respectively of key.
2854 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2855 * maximum signature size of any supported
2856 * signature algorithm.
2857 * \param[out] signature_length On success, the number of bytes that make up
2858 * the returned signature value.
2859 *
Gilles Peskineed733552023-02-14 19:21:09 +01002860 * \retval #PSA_SUCCESS \emptydescription
2861 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002862 * \retval #PSA_ERROR_NOT_PERMITTED
2863 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2864 * or it does not permit the requested algorithm.
2865 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2866 * The size of the \p signature buffer is too small. You can
2867 * determine a sufficient buffer size by calling
2868 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2869 * where \c key_type and \c key_bits are the type and bit-size
2870 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002871 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2872 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2873 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2874 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2875 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2876 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2877 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2878 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2879 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2880 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002881 * \retval #PSA_ERROR_BAD_STATE
2882 * The library has not been previously initialized by psa_crypto_init().
2883 * It is implementation-dependent whether a failure to initialize
2884 * results in this error code.
2885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2887 psa_algorithm_t alg,
2888 const uint8_t *input,
2889 size_t input_length,
2890 uint8_t *signature,
2891 size_t signature_size,
2892 size_t *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002893
2894/** \brief Verify the signature of a message with a public key, using
2895 * a hash-and-sign verification algorithm.
2896 *
2897 * \note To perform a multi-part hash-and-sign signature verification
2898 * algorithm, first use a multi-part hash operation to hash the message
2899 * and then pass the resulting hash to psa_verify_hash().
2900 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2901 * to use.
2902 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002903 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002904 * It must be a public key or an asymmetric key
2905 * pair. The key must allow the usage
2906 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002907 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002908 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2909 * is true), that is compatible with the type of
2910 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002911 * \param[in] input The message whose signature is to be verified.
2912 * \param[in] input_length Size of the \p input buffer in bytes.
Valerio Setti1cb31cc2024-06-03 07:05:23 +02002913 * \param[in] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002914 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915 *
Gilles Peskineed733552023-02-14 19:21:09 +01002916 * \retval #PSA_SUCCESS \emptydescription
2917 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918 * \retval #PSA_ERROR_NOT_PERMITTED
2919 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2920 * or it does not permit the requested algorithm.
2921 * \retval #PSA_ERROR_INVALID_SIGNATURE
2922 * The calculation was performed successfully, but the passed signature
2923 * is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01002924 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2925 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2928 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2929 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2930 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2931 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2932 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002933 * \retval #PSA_ERROR_BAD_STATE
2934 * The library has not been previously initialized by psa_crypto_init().
2935 * It is implementation-dependent whether a failure to initialize
2936 * results in this error code.
2937 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2939 psa_algorithm_t alg,
2940 const uint8_t *input,
2941 size_t input_length,
2942 const uint8_t *signature,
2943 size_t signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002944
2945/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002946 * \brief Sign a hash or short message with a private key.
2947 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002948 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002949 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002950 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2951 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002952 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2953 * to determine the hash algorithm to use.
2954 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002955 * \param key Identifier of the key to use for the operation.
2956 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002957 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002958 * \param alg A signature algorithm (PSA_ALG_XXX
2959 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2960 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002961 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002962 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002963 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002964 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002965 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002966 * \param[out] signature_length On success, the number of bytes
2967 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002968 *
Gilles Peskineed733552023-02-14 19:21:09 +01002969 * \retval #PSA_SUCCESS \emptydescription
2970 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2971 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002972 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002973 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002974 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002975 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002976 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002977 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002978 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2979 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2980 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2981 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2982 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2983 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2984 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2985 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002986 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002987 * The library has not been previously initialized by psa_crypto_init().
2988 * It is implementation-dependent whether a failure to initialize
2989 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002990 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002991psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002992 psa_algorithm_t alg,
2993 const uint8_t *hash,
2994 size_t hash_length,
2995 uint8_t *signature,
2996 size_t signature_size,
2997 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002998
2999/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003000 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003001 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003002 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003003 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003004 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3005 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003006 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3007 * to determine the hash algorithm to use.
3008 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003009 * \param key Identifier of the key to use for the operation. It
3010 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003011 * key must allow the usage
3012 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02003013 * \param alg A signature algorithm (PSA_ALG_XXX
3014 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3015 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003016 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003017 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003018 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003019 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003020 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003021 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003022 *
Gilles Peskine28538492018-07-11 17:34:00 +02003023 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003024 * The signature is valid.
Gilles Peskineed733552023-02-14 19:21:09 +01003025 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3026 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003027 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003028 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003029 * signature is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01003030 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3031 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3032 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3033 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3034 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3035 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3036 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003037 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003038 * The library has not been previously initialized by psa_crypto_init().
3039 * It is implementation-dependent whether a failure to initialize
3040 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003041 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003042psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003043 psa_algorithm_t alg,
3044 const uint8_t *hash,
3045 size_t hash_length,
3046 const uint8_t *signature,
3047 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003048
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003049/**
3050 * \brief Encrypt a short message with a public key.
3051 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003052 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003053 * It must be a public key or an asymmetric key
3054 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003055 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003056 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003057 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003058 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003059 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003060 * \param[in] salt A salt or label, if supported by the
3061 * encryption algorithm.
3062 * If the algorithm does not support a
3063 * salt, pass \c NULL.
3064 * If the algorithm supports an optional
3065 * salt and you do not want to pass a salt,
3066 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003067 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003068 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3069 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003070 * \param salt_length Size of the \p salt buffer in bytes.
3071 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003072 * \param[out] output Buffer where the encrypted message is to
3073 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003074 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003075 * \param[out] output_length On success, the number of bytes
3076 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003077 *
Gilles Peskineed733552023-02-14 19:21:09 +01003078 * \retval #PSA_SUCCESS \emptydescription
3079 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3080 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003081 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003082 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003083 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003084 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003085 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003086 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003087 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3088 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3089 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3090 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3091 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3092 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3093 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3094 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003095 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003096 * The library has not been previously initialized by psa_crypto_init().
3097 * It is implementation-dependent whether a failure to initialize
3098 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003099 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003100psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003101 psa_algorithm_t alg,
3102 const uint8_t *input,
3103 size_t input_length,
3104 const uint8_t *salt,
3105 size_t salt_length,
3106 uint8_t *output,
3107 size_t output_size,
3108 size_t *output_length);
3109
3110/**
3111 * \brief Decrypt a short message with a private key.
3112 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003113 * \param key Identifier of the key to use for the operation.
3114 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003115 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003116 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003117 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003118 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003119 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003120 * \param[in] salt A salt or label, if supported by the
3121 * encryption algorithm.
3122 * If the algorithm does not support a
3123 * salt, pass \c NULL.
3124 * If the algorithm supports an optional
3125 * salt and you do not want to pass a salt,
3126 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003127 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003128 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3129 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003130 * \param salt_length Size of the \p salt buffer in bytes.
3131 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003132 * \param[out] output Buffer where the decrypted message is to
3133 * be written.
3134 * \param output_size Size of the \c output buffer in bytes.
3135 * \param[out] output_length On success, the number of bytes
3136 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003137 *
Gilles Peskineed733552023-02-14 19:21:09 +01003138 * \retval #PSA_SUCCESS \emptydescription
3139 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3140 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003141 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003142 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003143 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003144 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003145 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003146 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003147 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3151 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3152 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3153 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3154 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3155 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003156 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003157 * The library has not been previously initialized by psa_crypto_init().
3158 * It is implementation-dependent whether a failure to initialize
3159 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003160 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003161psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003162 psa_algorithm_t alg,
3163 const uint8_t *input,
3164 size_t input_length,
3165 const uint8_t *salt,
3166 size_t salt_length,
3167 uint8_t *output,
3168 size_t output_size,
3169 size_t *output_length);
3170
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003171/**@}*/
3172
Gilles Peskine35675b62019-05-16 17:26:11 +02003173/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003174 * @{
3175 */
3176
Gilles Peskine35675b62019-05-16 17:26:11 +02003177/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003178 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003179 * Before calling any function on a key derivation operation object, the
3180 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003181 * - Set the structure to all-bits-zero, for example:
3182 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003183 * psa_key_derivation_operation_t operation;
3184 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003185 * \endcode
3186 * - Initialize the structure to logical zero values, for example:
3187 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003188 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003189 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003190 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003191 * for example:
3192 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003195 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003196 * to the structure, for example:
3197 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003198 * psa_key_derivation_operation_t operation;
3199 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003200 * \endcode
3201 *
3202 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01003203 * make any assumptions about the content of this structure.
3204 * Implementation details can change in future versions without notice.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003206typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003207
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003208/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003210 * This macro returns a suitable initializer for a key derivation operation
3211 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212 */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003213
Gilles Peskine35675b62019-05-16 17:26:11 +02003214/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003215 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003216static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003217
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003218/** Set up a key derivation operation.
3219 *
3220 * A key derivation algorithm takes some inputs and uses them to generate
3221 * a byte stream in a deterministic way.
3222 * This byte stream can be used to produce keys and other
3223 * cryptographic material.
3224 *
3225 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003226 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3227 * -# Call psa_key_derivation_setup() to select the algorithm.
3228 * -# Provide the inputs for the key derivation by calling
3229 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3230 * as appropriate. Which inputs are needed, in what order, and whether
3231 * they may be keys and if so of what type depends on the algorithm.
3232 * -# Optionally set the operation's maximum capacity with
3233 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3234 * of or after providing inputs. For some algorithms, this step is mandatory
3235 * because the output depends on the maximum capacity.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003236 * -# To derive a key, call psa_key_derivation_output_key() or
Gilles Peskine603b5b82024-06-06 21:23:00 +02003237 * psa_key_derivation_output_key_custom().
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003238 * To derive a byte string for a different purpose, call
3239 * psa_key_derivation_output_bytes().
3240 * Successive calls to these functions use successive output bytes
3241 * calculated by the key derivation algorithm.
3242 * -# Clean up the key derivation operation object with
3243 * psa_key_derivation_abort().
3244 *
3245 * If this function returns an error, the key derivation operation object is
3246 * not changed.
3247 *
3248 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3249 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003250 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003251 * Implementations must reject an attempt to derive a key of size 0.
3252 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003253 * \param[in,out] operation The key derivation operation object
3254 * to set up. It must
3255 * have been initialized but not set up yet.
3256 * \param alg The key derivation algorithm to compute
3257 * (\c PSA_ALG_XXX value such that
3258 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3259 *
3260 * \retval #PSA_SUCCESS
3261 * Success.
3262 * \retval #PSA_ERROR_INVALID_ARGUMENT
3263 * \c alg is not a key derivation algorithm.
3264 * \retval #PSA_ERROR_NOT_SUPPORTED
3265 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3268 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3269 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3270 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003271 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003272 * The operation state is not valid (it must be inactive), or
3273 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003274 * It is implementation-dependent whether a failure to initialize
3275 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003276 */
3277psa_status_t psa_key_derivation_setup(
3278 psa_key_derivation_operation_t *operation,
3279 psa_algorithm_t alg);
3280
Gilles Peskine35675b62019-05-16 17:26:11 +02003281/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003282 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003283 * The capacity of a key derivation is the maximum number of bytes that it can
3284 * return. When you get *N* bytes of output from a key derivation operation,
3285 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003286 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003287 * \param[in] operation The operation to query.
3288 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003289 *
Gilles Peskineed733552023-02-14 19:21:09 +01003290 * \retval #PSA_SUCCESS \emptydescription
3291 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3292 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3293 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003294 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003295 * The operation state is not valid (it must be active), or
3296 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003297 * It is implementation-dependent whether a failure to initialize
3298 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003299 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003300psa_status_t psa_key_derivation_get_capacity(
3301 const psa_key_derivation_operation_t *operation,
3302 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003303
Gilles Peskine35675b62019-05-16 17:26:11 +02003304/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003305 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003306 * The capacity of a key derivation operation is the maximum number of bytes
3307 * that the key derivation operation can return from this point onwards.
3308 *
3309 * \param[in,out] operation The key derivation operation object to modify.
3310 * \param capacity The new capacity of the operation.
3311 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003312 * current capacity.
3313 *
Gilles Peskineed733552023-02-14 19:21:09 +01003314 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003315 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003316 * \p capacity is larger than the operation's current capacity.
3317 * In this case, the operation object remains valid and its capacity
3318 * remains unchanged.
Gilles Peskineed733552023-02-14 19:21:09 +01003319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3320 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3321 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003322 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003323 * The operation state is not valid (it must be active), or the
3324 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003325 * It is implementation-dependent whether a failure to initialize
3326 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003327 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003328psa_status_t psa_key_derivation_set_capacity(
3329 psa_key_derivation_operation_t *operation,
3330 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003331
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003332/** Use the maximum possible capacity for a key derivation operation.
3333 *
3334 * Use this value as the capacity argument when setting up a key derivation
3335 * to indicate that the operation should have the maximum possible capacity.
3336 * The value of the maximum possible capacity depends on the key derivation
3337 * algorithm.
3338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003339#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003340
3341/** Provide an input for key derivation or key agreement.
3342 *
3343 * Which inputs are required and in what order depends on the algorithm.
3344 * Refer to the documentation of each key derivation or key agreement
3345 * algorithm for information.
3346 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003347 * This function passes direct inputs, which is usually correct for
3348 * non-secret inputs. To pass a secret input, which should be in a key
3349 * object, call psa_key_derivation_input_key() instead of this function.
3350 * Refer to the documentation of individual step types
3351 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3352 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003353 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003354 * If this function returns an error status, the operation enters an error
3355 * state and must be aborted by calling psa_key_derivation_abort().
3356 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003357 * \param[in,out] operation The key derivation operation object to use.
3358 * It must have been set up with
3359 * psa_key_derivation_setup() and must not
3360 * have produced any output yet.
3361 * \param step Which step the input data is for.
3362 * \param[in] data Input data to use.
3363 * \param data_length Size of the \p data buffer in bytes.
3364 *
3365 * \retval #PSA_SUCCESS
3366 * Success.
3367 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003368 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003369 * \c step does not allow direct inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3372 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3373 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3374 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003375 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003376 * The operation state is not valid for this input \p step, or
3377 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003378 * It is implementation-dependent whether a failure to initialize
3379 * results in this error code.
3380 */
3381psa_status_t psa_key_derivation_input_bytes(
3382 psa_key_derivation_operation_t *operation,
3383 psa_key_derivation_step_t step,
3384 const uint8_t *data,
3385 size_t data_length);
3386
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003387/** Provide a numeric input for key derivation or key agreement.
3388 *
3389 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003390 * However, when an algorithm requires a particular order, numeric inputs
3391 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003392 * Refer to the documentation of each key derivation or key agreement
3393 * algorithm for information.
3394 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003395 * This function is used for inputs which are fixed-size non-negative
3396 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003397 *
3398 * If this function returns an error status, the operation enters an error
3399 * state and must be aborted by calling psa_key_derivation_abort().
3400 *
3401 * \param[in,out] operation The key derivation operation object to use.
3402 * It must have been set up with
3403 * psa_key_derivation_setup() and must not
3404 * have produced any output yet.
3405 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003406 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003407 *
3408 * \retval #PSA_SUCCESS
3409 * Success.
3410 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003411 * \c step is not compatible with the operation's algorithm, or
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003412 * \c step does not allow numeric inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3415 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3416 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3417 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003418 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003419 * The operation state is not valid for this input \p step, or
3420 * the library has not been previously initialized by psa_crypto_init().
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003421 * It is implementation-dependent whether a failure to initialize
3422 * results in this error code.
3423 */
3424psa_status_t psa_key_derivation_input_integer(
3425 psa_key_derivation_operation_t *operation,
3426 psa_key_derivation_step_t step,
3427 uint64_t value);
3428
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003429/** Provide an input for key derivation in the form of a key.
3430 *
3431 * Which inputs are required and in what order depends on the algorithm.
3432 * Refer to the documentation of each key derivation or key agreement
3433 * algorithm for information.
3434 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003435 * This function obtains input from a key object, which is usually correct for
3436 * secret inputs or for non-secret personalization strings kept in the key
3437 * store. To pass a non-secret parameter which is not in the key store,
3438 * call psa_key_derivation_input_bytes() instead of this function.
3439 * Refer to the documentation of individual step types
3440 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3441 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003442 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003443 * If this function returns an error status, the operation enters an error
3444 * state and must be aborted by calling psa_key_derivation_abort().
3445 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003446 * \param[in,out] operation The key derivation operation object to use.
3447 * It must have been set up with
3448 * psa_key_derivation_setup() and must not
3449 * have produced any output yet.
3450 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003451 * \param key Identifier of the key. It must have an
3452 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003453 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003454 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3455 * and the algorithm used by the operation.
3456 *
3457 * \note Once all inputs steps are completed, the operations will allow:
3458 * - psa_key_derivation_output_bytes() if each input was either a direct input
3459 * or a key with #PSA_KEY_USAGE_DERIVE set;
Gilles Peskine603b5b82024-06-06 21:23:00 +02003460 * - psa_key_derivation_output_key() or psa_key_derivation_output_key_custom()
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003461 * if the input for step
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003462 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003463 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3464 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003465 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3466 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003467 * - psa_key_derivation_verify_key() under the same conditions as
3468 * psa_key_derivation_verify_bytes().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003469 *
3470 * \retval #PSA_SUCCESS
3471 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003472 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003473 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003474 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3475 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3476 * algorithm.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003477 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003478 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine224b0d62019-09-23 18:13:17 +02003479 * \c step does not allow key inputs of the given type
3480 * or does not allow key inputs at all.
Gilles Peskineed733552023-02-14 19:21:09 +01003481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3483 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3484 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3485 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003486 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003487 * The operation state is not valid for this input \p step, or
3488 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003489 * It is implementation-dependent whether a failure to initialize
3490 * results in this error code.
3491 */
3492psa_status_t psa_key_derivation_input_key(
3493 psa_key_derivation_operation_t *operation,
3494 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003495 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003496
3497/** Perform a key agreement and use the shared secret as input to a key
3498 * derivation.
3499 *
3500 * A key agreement algorithm takes two inputs: a private key \p private_key
3501 * a public key \p peer_key.
3502 * The result of this function is passed as input to a key derivation.
3503 * The output of this key derivation can be extracted by reading from the
3504 * resulting operation to produce keys and other cryptographic material.
3505 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003506 * If this function returns an error status, the operation enters an error
3507 * state and must be aborted by calling psa_key_derivation_abort().
3508 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003509 * \param[in,out] operation The key derivation operation object to use.
3510 * It must have been set up with
3511 * psa_key_derivation_setup() with a
3512 * key agreement and derivation algorithm
3513 * \c alg (\c PSA_ALG_XXX value such that
3514 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3515 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3516 * is false).
3517 * The operation must be ready for an
3518 * input of the type given by \p step.
3519 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003520 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003521 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003522 * \param[in] peer_key Public key of the peer. The peer key must be in the
3523 * same format that psa_import_key() accepts for the
3524 * public key type corresponding to the type of
3525 * private_key. That is, this function performs the
3526 * equivalent of
3527 * #psa_import_key(...,
3528 * `peer_key`, `peer_key_length`) where
3529 * with key attributes indicating the public key
3530 * type corresponding to the type of `private_key`.
3531 * For example, for EC keys, this means that peer_key
3532 * is interpreted as a point on the curve that the
3533 * private key is on. The standard formats for public
3534 * keys are documented in the documentation of
3535 * psa_export_public_key().
3536 * \param peer_key_length Size of \p peer_key in bytes.
3537 *
3538 * \retval #PSA_SUCCESS
3539 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003540 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3541 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003542 * \retval #PSA_ERROR_INVALID_ARGUMENT
3543 * \c private_key is not compatible with \c alg,
3544 * or \p peer_key is not valid for \c alg or not compatible with
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003545 * \c private_key, or \c step does not allow an input resulting
3546 * from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003547 * \retval #PSA_ERROR_NOT_SUPPORTED
3548 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3551 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3552 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3553 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003554 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003555 * The operation state is not valid for this key agreement \p step,
3556 * or the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003557 * It is implementation-dependent whether a failure to initialize
3558 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003559 */
3560psa_status_t psa_key_derivation_key_agreement(
3561 psa_key_derivation_operation_t *operation,
3562 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003563 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003564 const uint8_t *peer_key,
3565 size_t peer_key_length);
3566
Gilles Peskine35675b62019-05-16 17:26:11 +02003567/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003568 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003569 * This function calculates output bytes from a key derivation algorithm and
3570 * return those bytes.
3571 * If you view the key derivation's output as a stream of bytes, this
3572 * function destructively reads the requested number of bytes from the
3573 * stream.
3574 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003575 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003576 * If this function returns an error status other than
3577 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003578 * state and must be aborted by calling psa_key_derivation_abort().
3579 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003580 * \param[in,out] operation The key derivation operation object to read from.
3581 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003582 * \param output_length Number of bytes to output.
3583 *
Gilles Peskineed733552023-02-14 19:21:09 +01003584 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003585 * \retval #PSA_ERROR_NOT_PERMITTED
3586 * One of the inputs was a key whose policy didn't allow
3587 * #PSA_KEY_USAGE_DERIVE.
David Saadab4ecc272019-02-14 13:48:10 +02003588 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003589 * The operation's capacity was less than
3590 * \p output_length bytes. Note that in this case,
3591 * no output is written to the output buffer.
3592 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003593 * subsequent calls to this function will not
3594 * succeed, even with a smaller output buffer.
Gilles Peskineed733552023-02-14 19:21:09 +01003595 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3597 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3598 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3599 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003600 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003601 * The operation state is not valid (it must be active and completed
3602 * all required input steps), or the library has not been previously
3603 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003604 * It is implementation-dependent whether a failure to initialize
3605 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003606 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003607psa_status_t psa_key_derivation_output_bytes(
3608 psa_key_derivation_operation_t *operation,
3609 uint8_t *output,
3610 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003611
Gilles Peskine35675b62019-05-16 17:26:11 +02003612/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003613 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003614 * This function calculates output bytes from a key derivation algorithm
3615 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003616 * The key's location, usage policy, type and size are taken from
3617 * \p attributes.
3618 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003619 * If you view the key derivation's output as a stream of bytes, this
3620 * function destructively reads as many bytes as required from the
3621 * stream.
3622 * The operation's capacity decreases by the number of bytes read.
3623 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003624 * If this function returns an error status other than
3625 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003626 * state and must be aborted by calling psa_key_derivation_abort().
3627 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003628 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003629 * the key is derived, depends on the key type and on the key size
3630 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003631 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003632 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003633 * of a given size, this function is functionally equivalent to
3634 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003635 * and passing the resulting output to #psa_import_key.
3636 * However, this function has a security benefit:
3637 * if the implementation provides an isolation boundary then
3638 * the key material is not exposed outside the isolation boundary.
3639 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003640 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003641 * The following key types defined in this specification follow this scheme:
3642 *
3643 * - #PSA_KEY_TYPE_AES;
Gilles Peskine6c12a1e2021-09-21 11:59:39 +02003644 * - #PSA_KEY_TYPE_ARIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003645 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003646 * - #PSA_KEY_TYPE_DERIVE;
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003647 * - #PSA_KEY_TYPE_HMAC;
3648 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003649 *
3650 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003651 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003652 * Montgomery curve), this function always draws a byte string whose
3653 * length is determined by the curve, and sets the mandatory bits
3654 * accordingly. That is:
3655 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003656 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003657 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003658 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003659 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003660 *
3661 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003662 * \c bits bits with constraints as to which bit sequences are acceptable,
3663 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003664 * up to the nearest whole number of bytes. If the resulting byte string
3665 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3666 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003667 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003668 * for the output produced by psa_export_key().
3669 * The following key types defined in this specification follow this scheme:
3670 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003671 * - #PSA_KEY_TYPE_DES.
3672 * Force-set the parity bits, but discard forbidden weak keys.
3673 * For 2-key and 3-key triple-DES, the three keys are generated
3674 * successively (for example, for 3-key triple-DES,
3675 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3676 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003677 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003678 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003679 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003680 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003681 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003682 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003683 * Weierstrass curve).
3684 * For these key types, interpret the byte string as integer
3685 * in big-endian order. Discard it if it is not in the range
3686 * [0, *N* - 2] where *N* is the boundary of the private key domain
3687 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003688 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003689 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003690 * This method allows compliance to NIST standards, specifically
3691 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003692 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3693 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3694 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3695 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003696 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003697 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003698 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003699 * implementation-defined.
3700 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003701 * In all cases, the data that is read is discarded from the operation.
3702 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003703 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003704 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3705 * the input to that step must be provided with psa_key_derivation_input_key().
3706 * Future versions of this specification may include additional restrictions
3707 * on the derived key based on the attributes and strength of the secret key.
3708 *
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003709 * \note This function is equivalent to calling
Gilles Peskine603b5b82024-06-06 21:23:00 +02003710 * psa_key_derivation_output_key_custom()
3711 * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
3712 * and `custom_data_length == 0` (i.e. `custom_data` is empty).
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003713 *
Gilles Peskine20628592019-04-19 19:29:50 +02003714 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003715 * If the key type to be created is
3716 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3717 * the policy must be the same as in the current
3718 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003719 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003720 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003721 * key. For persistent keys, this is the key
3722 * identifier defined in \p attributes.
3723 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003724 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003725 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003726 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003727 * If the key is persistent, the key material and the key's metadata
3728 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003729 * \retval #PSA_ERROR_ALREADY_EXISTS
3730 * This is an attempt to create a persistent key, and there is
3731 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003732 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003733 * There was not enough data to create the desired key.
3734 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003735 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003736 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003737 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003738 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003739 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003740 * \retval #PSA_ERROR_INVALID_ARGUMENT
3741 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003742 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003743 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3744 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3745 * key; or one of the inputs was a key whose policy didn't allow
3746 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskineed733552023-02-14 19:21:09 +01003747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3748 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3749 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3750 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3751 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3752 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3753 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3754 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003755 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003756 * The operation state is not valid (it must be active and completed
3757 * all required input steps), or the library has not been previously
3758 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003759 * It is implementation-dependent whether a failure to initialize
3760 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003761 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003762psa_status_t psa_key_derivation_output_key(
3763 const psa_key_attributes_t *attributes,
3764 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003765 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003766
Gilles Peskine092ce512024-02-20 12:31:24 +01003767/** Derive a key from an ongoing key derivation operation with custom
3768 * production parameters.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003769 *
3770 * See the description of psa_key_derivation_out_key() for the operation of
Gilles Peskine092ce512024-02-20 12:31:24 +01003771 * this function with the default production parameters.
3772 * Mbed TLS currently does not currently support any non-default production
3773 * parameters.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003774 *
3775 * \note This function is experimental and may change in future minor
3776 * versions of Mbed TLS.
3777 *
3778 * \param[in] attributes The attributes for the new key.
3779 * If the key type to be created is
3780 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3781 * the policy must be the same as in the current
3782 * operation.
3783 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskinef36d7852024-06-06 21:11:44 +02003784 * \param[in] custom Customization parameters for the key generation.
Gilles Peskine603b5b82024-06-06 21:23:00 +02003785 * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
Gilles Peskinef36d7852024-06-06 21:11:44 +02003786 * with \p custom_data_length = 0,
3787 * this function is equivalent to
3788 * psa_key_derivation_output_key().
3789 * \param[in] custom_data Variable-length data associated with \c custom.
3790 * \param custom_data_length
3791 * Length of `custom_data` in bytes.
3792 * \param[out] key On success, an identifier for the newly created
3793 * key. For persistent keys, this is the key
3794 * identifier defined in \p attributes.
3795 * \c 0 on failure.
3796 *
3797 * \retval #PSA_SUCCESS
3798 * Success.
3799 * If the key is persistent, the key material and the key's metadata
3800 * have been saved to persistent storage.
3801 * \retval #PSA_ERROR_ALREADY_EXISTS
3802 * This is an attempt to create a persistent key, and there is
3803 * already a persistent key with the given identifier.
3804 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3805 * There was not enough data to create the desired key.
3806 * Note that in this case, no output is written to the output buffer.
3807 * The operation's capacity is set to 0, thus subsequent calls to
3808 * this function will not succeed, even with a smaller output buffer.
3809 * \retval #PSA_ERROR_NOT_SUPPORTED
3810 * The key type or key size is not supported, either by the
3811 * implementation in general or in this particular location.
3812 * \retval #PSA_ERROR_INVALID_ARGUMENT
3813 * The provided key attributes are not valid for the operation.
3814 * \retval #PSA_ERROR_NOT_PERMITTED
3815 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3816 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3817 * key; or one of the inputs was a key whose policy didn't allow
3818 * #PSA_KEY_USAGE_DERIVE.
3819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3820 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3821 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3822 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3823 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3824 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3825 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3826 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3827 * \retval #PSA_ERROR_BAD_STATE
3828 * The operation state is not valid (it must be active and completed
3829 * all required input steps), or the library has not been previously
3830 * initialized by psa_crypto_init().
3831 * It is implementation-dependent whether a failure to initialize
3832 * results in this error code.
3833 */
3834psa_status_t psa_key_derivation_output_key_custom(
3835 const psa_key_attributes_t *attributes,
3836 psa_key_derivation_operation_t *operation,
3837 const psa_custom_key_parameters_t *custom,
3838 const uint8_t *custom_data,
3839 size_t custom_data_length,
3840 mbedtls_svc_key_id_t *key);
3841
Gilles Peskine14134e72024-06-07 08:31:41 +02003842#ifndef __cplusplus
3843/* Omitted when compiling in C++, because one of the parameters is a
3844 * pointer to a struct with a flexible array member, and that is not
3845 * standard C++.
3846 * https://github.com/Mbed-TLS/mbedtls/issues/9020
3847 */
Gilles Peskinef36d7852024-06-06 21:11:44 +02003848/** Derive a key from an ongoing key derivation operation with custom
3849 * production parameters.
3850 *
Gilles Peskinee65cca22024-06-06 21:27:16 +02003851 * \note
3852 * This is a deprecated variant of psa_key_derivation_output_key_custom().
3853 * It is equivalent except that the associated variable-length data
3854 * is passed in `params->data` instead of a separate parameter.
3855 * This function will be removed in a future version of Mbed TLS.
Gilles Peskinef36d7852024-06-06 21:11:44 +02003856 *
3857 * \param[in] attributes The attributes for the new key.
3858 * If the key type to be created is
3859 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3860 * the policy must be the same as in the current
3861 * operation.
3862 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine092ce512024-02-20 12:31:24 +01003863 * \param[in] params Customization parameters for the key derivation.
3864 * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
3865 * with \p params_data_length = 0,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003866 * this function is equivalent to
3867 * psa_key_derivation_output_key().
3868 * Mbed TLS currently only supports the default
Gilles Peskine23605d12024-02-20 17:10:56 +01003869 * production parameters, i.e.
3870 * #PSA_KEY_PRODUCTION_PARAMETERS_INIT,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003871 * for all key types.
Gilles Peskine092ce512024-02-20 12:31:24 +01003872 * \param params_data_length
3873 * Length of `params->data` in bytes.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003874 * \param[out] key On success, an identifier for the newly created
3875 * key. For persistent keys, this is the key
3876 * identifier defined in \p attributes.
3877 * \c 0 on failure.
3878 *
3879 * \retval #PSA_SUCCESS
3880 * Success.
3881 * If the key is persistent, the key material and the key's metadata
3882 * have been saved to persistent storage.
3883 * \retval #PSA_ERROR_ALREADY_EXISTS
3884 * This is an attempt to create a persistent key, and there is
3885 * already a persistent key with the given identifier.
3886 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3887 * There was not enough data to create the desired key.
3888 * Note that in this case, no output is written to the output buffer.
3889 * The operation's capacity is set to 0, thus subsequent calls to
3890 * this function will not succeed, even with a smaller output buffer.
3891 * \retval #PSA_ERROR_NOT_SUPPORTED
3892 * The key type or key size is not supported, either by the
3893 * implementation in general or in this particular location.
3894 * \retval #PSA_ERROR_INVALID_ARGUMENT
3895 * The provided key attributes are not valid for the operation.
3896 * \retval #PSA_ERROR_NOT_PERMITTED
3897 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3898 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3899 * key; or one of the inputs was a key whose policy didn't allow
3900 * #PSA_KEY_USAGE_DERIVE.
3901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3902 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3904 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3905 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3906 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3907 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3908 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3909 * \retval #PSA_ERROR_BAD_STATE
3910 * The operation state is not valid (it must be active and completed
3911 * all required input steps), or the library has not been previously
3912 * initialized by psa_crypto_init().
3913 * It is implementation-dependent whether a failure to initialize
3914 * results in this error code.
3915 */
3916psa_status_t psa_key_derivation_output_key_ext(
3917 const psa_key_attributes_t *attributes,
3918 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01003919 const psa_key_production_parameters_t *params,
3920 size_t params_data_length,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003921 mbedtls_svc_key_id_t *key);
Gilles Peskine14134e72024-06-07 08:31:41 +02003922#endif /* !__cplusplus */
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003923
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003924/** Compare output data from a key derivation operation to an expected value.
3925 *
3926 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003927 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003928 * If you view the key derivation's output as a stream of bytes, this
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003929 * function destructively reads the expected number of bytes from the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003930 * stream before comparing them.
3931 * The operation's capacity decreases by the number of bytes read.
3932 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003933 * This is functionally equivalent to the following code:
3934 * \code
3935 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3936 * if (memcmp(output, tmp, output_length) != 0)
3937 * return PSA_ERROR_INVALID_SIGNATURE;
3938 * \endcode
3939 * except (1) it works even if the key's policy does not allow outputting the
3940 * bytes, and (2) the comparison will be done in constant time.
3941 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003942 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003943 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3944 * the operation enters an error state and must be aborted by calling
3945 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003946 *
3947 * \param[in,out] operation The key derivation operation object to read from.
Valerio Settid30cc092024-06-24 15:27:51 +02003948 * \param[in] expected Buffer containing the expected derivation output.
3949 * \param expected_length Length of the expected output; this is also the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003950 * number of bytes that will be read.
3951 *
Gilles Peskineed733552023-02-14 19:21:09 +01003952 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003953 * \retval #PSA_ERROR_INVALID_SIGNATURE
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003954 * The output was read successfully, but it differs from the expected
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003955 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003956 * \retval #PSA_ERROR_NOT_PERMITTED
3957 * One of the inputs was a key whose policy didn't allow
3958 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003959 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3960 * The operation's capacity was less than
3961 * \p output_length bytes. Note that in this case,
3962 * the operation's capacity is set to 0, thus
3963 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003964 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003965 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3966 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3967 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3968 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3969 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003970 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003971 * The operation state is not valid (it must be active and completed
3972 * all required input steps), or the library has not been previously
3973 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003974 * It is implementation-dependent whether a failure to initialize
3975 * results in this error code.
3976 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003977psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003978 psa_key_derivation_operation_t *operation,
Valerio Settid30cc092024-06-24 15:27:51 +02003979 const uint8_t *expected,
3980 size_t expected_length);
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003981
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003982/** Compare output data from a key derivation operation to an expected value
3983 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003984 *
3985 * This function calculates output bytes from a key derivation algorithm and
3986 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003987 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003988 * If you view the key derivation's output as a stream of bytes, this
Tom Cosgrove1797b052022-12-04 17:19:59 +00003989 * function destructively reads the number of bytes corresponding to the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003990 * length of the expected value from the stream before comparing them.
3991 * The operation's capacity decreases by the number of bytes read.
3992 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003993 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003994 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003995 * works even if the key cannot be exported.
3996 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003997 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003998 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3999 * the operation enters an error state and must be aborted by calling
4000 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004001 *
4002 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02004003 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
4004 * containing the expected output. Its policy must
4005 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
4006 * and the permitted algorithm must match the
4007 * operation. The value of this key was likely
4008 * computed by a previous call to
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004009 * psa_key_derivation_output_key() or
Gilles Peskine603b5b82024-06-06 21:23:00 +02004010 * psa_key_derivation_output_key_custom().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004011 *
Gilles Peskineed733552023-02-14 19:21:09 +01004012 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004013 * \retval #PSA_ERROR_INVALID_SIGNATURE
4014 * The output was read successfully, but if differs from the expected
4015 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02004016 * \retval #PSA_ERROR_INVALID_HANDLE
4017 * The key passed as the expected value does not exist.
4018 * \retval #PSA_ERROR_INVALID_ARGUMENT
4019 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004020 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02004021 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02004022 * this algorithm; or one of the inputs was a key whose policy didn't
4023 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004024 * \retval #PSA_ERROR_INSUFFICIENT_DATA
4025 * The operation's capacity was less than
4026 * the length of the expected value. In this case,
4027 * the operation's capacity is set to 0, thus
4028 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01004029 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01004030 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4031 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4032 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4033 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4034 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004035 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05004036 * The operation state is not valid (it must be active and completed
4037 * all required input steps), or the library has not been previously
4038 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004039 * It is implementation-dependent whether a failure to initialize
4040 * results in this error code.
4041 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02004042psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02004043 psa_key_derivation_operation_t *operation,
4044 psa_key_id_t expected);
4045
Gilles Peskine35675b62019-05-16 17:26:11 +02004046/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02004047 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01004048 * Aborting an operation frees all associated resources except for the \c
4049 * operation structure itself. Once aborted, the operation object can be reused
4050 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02004051 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01004052 * This function may be called at any time after the operation
4053 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02004054 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01004055 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
4056 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02004057 *
Gilles Peskine35675b62019-05-16 17:26:11 +02004058 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02004059 *
Gilles Peskineed733552023-02-14 19:21:09 +01004060 * \retval #PSA_SUCCESS \emptydescription
4061 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4062 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4063 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01004064 * \retval #PSA_ERROR_BAD_STATE
4065 * The library has not been previously initialized by psa_crypto_init().
4066 * It is implementation-dependent whether a failure to initialize
4067 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02004068 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004069psa_status_t psa_key_derivation_abort(
4070 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004071
Gilles Peskine58fe9e82019-05-16 18:01:45 +02004072/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004073 *
4074 * \warning The raw result of a key agreement algorithm such as finite-field
4075 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
4076 * not be used directly as key material. It should instead be passed as
4077 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004078 * a key derivation, use psa_key_derivation_key_agreement() and other
4079 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004080 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01004081 * \param alg The key agreement algorithm to compute
4082 * (\c PSA_ALG_XXX value such that
4083 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
4084 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02004085 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02004086 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004087 * \param[in] peer_key Public key of the peer. It must be
4088 * in the same format that psa_import_key()
4089 * accepts. The standard formats for public
4090 * keys are documented in the documentation
4091 * of psa_export_public_key().
4092 * \param peer_key_length Size of \p peer_key in bytes.
4093 * \param[out] output Buffer where the decrypted message is to
4094 * be written.
4095 * \param output_size Size of the \c output buffer in bytes.
4096 * \param[out] output_length On success, the number of bytes
4097 * that make up the returned output.
4098 *
4099 * \retval #PSA_SUCCESS
4100 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01004101 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4102 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine769c7a62019-01-18 16:42:29 +01004103 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05004104 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01004105 * \p private_key is not compatible with \p alg,
4106 * or \p peer_key is not valid for \p alg or not compatible with
4107 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01004108 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4109 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01004110 * \retval #PSA_ERROR_NOT_SUPPORTED
4111 * \p alg is not a supported key agreement algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01004112 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4113 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4114 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4115 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4116 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01004117 * \retval #PSA_ERROR_BAD_STATE
4118 * The library has not been previously initialized by psa_crypto_init().
4119 * It is implementation-dependent whether a failure to initialize
4120 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004121 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02004122psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004123 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004124 const uint8_t *peer_key,
4125 size_t peer_key_length,
4126 uint8_t *output,
4127 size_t output_size,
4128 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004129
Gilles Peskineea0fb492018-07-12 17:17:20 +02004130/**@}*/
4131
Gilles Peskineedd76872018-07-20 17:42:05 +02004132/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004133 * @{
4134 */
4135
4136/**
4137 * \brief Generate random bytes.
4138 *
4139 * \warning This function **can** fail! Callers MUST check the return status
4140 * and MUST NOT use the content of the output buffer if the return
4141 * status is not #PSA_SUCCESS.
4142 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004143 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004144 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02004145 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004146 * \param output_size Number of bytes to generate and output.
4147 *
Gilles Peskineed733552023-02-14 19:21:09 +01004148 * \retval #PSA_SUCCESS \emptydescription
4149 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4150 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4151 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4153 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4154 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir0adf0fc2018-09-06 16:24:41 +03004155 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004156 * The library has not been previously initialized by psa_crypto_init().
4157 * It is implementation-dependent whether a failure to initialize
4158 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004159 */
4160psa_status_t psa_generate_random(uint8_t *output,
4161 size_t output_size);
4162
4163/**
4164 * \brief Generate a key or key pair.
4165 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02004166 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02004167 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004168 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004169 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004170 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004171 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004172 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004173 * the public exponent is 65537.
4174 * The modulus is a product of two probabilistic primes
4175 * between 2^{n-1} and 2^n where n is the bit size specified in the
4176 * attributes.
4177 *
Gilles Peskine603b5b82024-06-06 21:23:00 +02004178 * \note This function is equivalent to calling psa_generate_key_custom()
4179 * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
4180 * and `custom_data_length == 0` (i.e. `custom_data` is empty).
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004181 *
Gilles Peskine20628592019-04-19 19:29:50 +02004182 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004183 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004184 * key. For persistent keys, this is the key
4185 * identifier defined in \p attributes.
4186 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004187 *
Gilles Peskine28538492018-07-11 17:34:00 +02004188 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004189 * Success.
4190 * If the key is persistent, the key material and the key's metadata
4191 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004192 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004193 * This is an attempt to create a persistent key, and there is
4194 * already a persistent key with the given identifier.
Gilles Peskineed733552023-02-14 19:21:09 +01004195 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4196 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4198 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4200 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4201 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4202 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4203 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4204 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4205 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03004206 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004207 * The library has not been previously initialized by psa_crypto_init().
4208 * It is implementation-dependent whether a failure to initialize
4209 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004210 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004211psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004212 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004213
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004214/**
Gilles Peskine092ce512024-02-20 12:31:24 +01004215 * \brief Generate a key or key pair using custom production parameters.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004216 *
4217 * See the description of psa_generate_key() for the operation of this
Gilles Peskine092ce512024-02-20 12:31:24 +01004218 * function with the default production parameters. In addition, this function
4219 * supports the following production customizations, described in more detail
Gilles Peskinef36d7852024-06-06 21:11:44 +02004220 * in the documentation of ::psa_custom_key_parameters_t:
4221 *
4222 * - RSA keys: generation with a custom public exponent.
4223 *
4224 * \note This function is experimental and may change in future minor
4225 * versions of Mbed TLS.
4226 *
4227 * \param[in] attributes The attributes for the new key.
4228 * \param[in] custom Customization parameters for the key generation.
Gilles Peskine603b5b82024-06-06 21:23:00 +02004229 * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
Gilles Peskinef36d7852024-06-06 21:11:44 +02004230 * with \p custom_data_length = 0,
4231 * this function is equivalent to
4232 * psa_generate_key().
4233 * \param[in] custom_data Variable-length data associated with \c custom.
4234 * \param custom_data_length
4235 * Length of `custom_data` in bytes.
4236 * \param[out] key On success, an identifier for the newly created
4237 * key. For persistent keys, this is the key
4238 * identifier defined in \p attributes.
4239 * \c 0 on failure.
4240 *
4241 * \retval #PSA_SUCCESS
4242 * Success.
4243 * If the key is persistent, the key material and the key's metadata
4244 * have been saved to persistent storage.
4245 * \retval #PSA_ERROR_ALREADY_EXISTS
4246 * This is an attempt to create a persistent key, and there is
4247 * already a persistent key with the given identifier.
4248 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4249 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4250 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4251 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4252 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4253 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4254 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4255 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4256 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4257 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4258 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4259 * \retval #PSA_ERROR_BAD_STATE
4260 * The library has not been previously initialized by psa_crypto_init().
4261 * It is implementation-dependent whether a failure to initialize
4262 * results in this error code.
4263 */
4264psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
4265 const psa_custom_key_parameters_t *custom,
4266 const uint8_t *custom_data,
4267 size_t custom_data_length,
4268 mbedtls_svc_key_id_t *key);
4269
Gilles Peskine14134e72024-06-07 08:31:41 +02004270#ifndef __cplusplus
4271/* Omitted when compiling in C++, because one of the parameters is a
4272 * pointer to a struct with a flexible array member, and that is not
4273 * standard C++.
4274 * https://github.com/Mbed-TLS/mbedtls/issues/9020
4275 */
Gilles Peskinef36d7852024-06-06 21:11:44 +02004276/**
4277 * \brief Generate a key or key pair using custom production parameters.
4278 *
Gilles Peskinee65cca22024-06-06 21:27:16 +02004279 * \note
4280 * This is a deprecated variant of psa_key_derivation_output_key_custom().
4281 * It is equivalent except that the associated variable-length data
4282 * is passed in `params->data` instead of a separate parameter.
4283 * This function will be removed in a future version of Mbed TLS.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004284 *
4285 * \param[in] attributes The attributes for the new key.
Gilles Peskine092ce512024-02-20 12:31:24 +01004286 * \param[in] params Customization parameters for the key generation.
4287 * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
4288 * with \p params_data_length = 0,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004289 * this function is equivalent to
Gilles Peskine6a2c4002024-02-26 08:34:06 +01004290 * psa_generate_key().
Gilles Peskine092ce512024-02-20 12:31:24 +01004291 * \param params_data_length
4292 * Length of `params->data` in bytes.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004293 * \param[out] key On success, an identifier for the newly created
4294 * key. For persistent keys, this is the key
4295 * identifier defined in \p attributes.
4296 * \c 0 on failure.
4297 *
4298 * \retval #PSA_SUCCESS
4299 * Success.
4300 * If the key is persistent, the key material and the key's metadata
4301 * have been saved to persistent storage.
4302 * \retval #PSA_ERROR_ALREADY_EXISTS
4303 * This is an attempt to create a persistent key, and there is
4304 * already a persistent key with the given identifier.
4305 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4306 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4307 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4308 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4309 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4310 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4311 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4312 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4313 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4314 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4315 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4316 * \retval #PSA_ERROR_BAD_STATE
4317 * The library has not been previously initialized by psa_crypto_init().
4318 * It is implementation-dependent whether a failure to initialize
4319 * results in this error code.
4320 */
4321psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01004322 const psa_key_production_parameters_t *params,
4323 size_t params_data_length,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004324 mbedtls_svc_key_id_t *key);
Gilles Peskine14134e72024-06-07 08:31:41 +02004325#endif /* !__cplusplus */
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004326
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004327/**@}*/
4328
Paul Elliott1265f002022-09-09 17:15:43 +01004329/** \defgroup interruptible_hash Interruptible sign/verify hash
4330 * @{
4331 */
4332
4333/** The type of the state data structure for interruptible hash
4334 * signing operations.
4335 *
4336 * Before calling any function on a sign hash operation object, the
4337 * application must initialize it by any of the following means:
4338 * - Set the structure to all-bits-zero, for example:
4339 * \code
4340 * psa_sign_hash_interruptible_operation_t operation;
4341 * memset(&operation, 0, sizeof(operation));
4342 * \endcode
4343 * - Initialize the structure to logical zero values, for example:
4344 * \code
4345 * psa_sign_hash_interruptible_operation_t operation = {0};
4346 * \endcode
4347 * - Initialize the structure to the initializer
4348 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4349 * \code
4350 * psa_sign_hash_interruptible_operation_t operation =
4351 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4352 * \endcode
4353 * - Assign the result of the function
4354 * psa_sign_hash_interruptible_operation_init() to the structure, for
4355 * example:
4356 * \code
4357 * psa_sign_hash_interruptible_operation_t operation;
4358 * operation = psa_sign_hash_interruptible_operation_init();
4359 * \endcode
4360 *
4361 * This is an implementation-defined \c struct. Applications should not
4362 * make any assumptions about the content of this structure.
4363 * Implementation details can change in future versions without notice. */
4364typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4365
4366/** The type of the state data structure for interruptible hash
4367 * verification operations.
4368 *
4369 * Before calling any function on a sign hash operation object, the
4370 * application must initialize it by any of the following means:
4371 * - Set the structure to all-bits-zero, for example:
4372 * \code
4373 * psa_verify_hash_interruptible_operation_t operation;
4374 * memset(&operation, 0, sizeof(operation));
4375 * \endcode
4376 * - Initialize the structure to logical zero values, for example:
4377 * \code
4378 * psa_verify_hash_interruptible_operation_t operation = {0};
4379 * \endcode
4380 * - Initialize the structure to the initializer
4381 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4382 * \code
4383 * psa_verify_hash_interruptible_operation_t operation =
4384 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4385 * \endcode
4386 * - Assign the result of the function
4387 * psa_verify_hash_interruptible_operation_init() to the structure, for
4388 * example:
4389 * \code
4390 * psa_verify_hash_interruptible_operation_t operation;
4391 * operation = psa_verify_hash_interruptible_operation_init();
4392 * \endcode
4393 *
4394 * This is an implementation-defined \c struct. Applications should not
4395 * make any assumptions about the content of this structure.
4396 * Implementation details can change in future versions without notice. */
4397typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4398
4399/**
4400 * \brief Set the maximum number of ops allowed to be
4401 * executed by an interruptible function in a
4402 * single call.
4403 *
4404 * \warning This is a beta API, and thus subject to change
4405 * at any point. It is not bound by the usual
4406 * interface stability promises.
4407 *
4408 * \note The time taken to execute a single op is
4409 * implementation specific and depends on
4410 * software, hardware, the algorithm, key type and
4411 * curve chosen. Even within a single operation,
4412 * successive ops can take differing amounts of
4413 * time. The only guarantee is that lower values
4414 * for \p max_ops means functions will block for a
4415 * lesser maximum amount of time. The functions
4416 * \c psa_sign_interruptible_get_num_ops() and
4417 * \c psa_verify_interruptible_get_num_ops() are
4418 * provided to help with tuning this value.
4419 *
4420 * \note This value defaults to
4421 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4422 * means the whole operation will be done in one
4423 * go, regardless of the number of ops required.
4424 *
4425 * \note If more ops are needed to complete a
4426 * computation, #PSA_OPERATION_INCOMPLETE will be
4427 * returned by the function performing the
4428 * computation. It is then the caller's
4429 * responsibility to either call again with the
4430 * same operation context until it returns 0 or an
4431 * error code; or to call the relevant abort
4432 * function if the answer is no longer required.
4433 *
4434 * \note The interpretation of \p max_ops is also
4435 * implementation defined. On a hard real time
4436 * system, this can indicate a hard deadline, as a
4437 * real-time system needs a guarantee of not
4438 * spending more than X time, however care must be
4439 * taken in such an implementation to avoid the
4440 * situation whereby calls just return, not being
4441 * able to do any actual work within the allotted
4442 * time. On a non-real-time system, the
4443 * implementation can be more relaxed, but again
4444 * whether this number should be interpreted as as
4445 * hard or soft limit or even whether a less than
4446 * or equals as regards to ops executed in a
4447 * single call is implementation defined.
4448 *
Paul Elliott21c39512023-02-15 19:47:39 +00004449 * \note For keys in local storage when no accelerator
4450 * driver applies, please see also the
4451 * documentation for \c mbedtls_ecp_set_max_ops(),
4452 * which is the internal implementation in these
4453 * cases.
4454 *
Paul Elliott1265f002022-09-09 17:15:43 +01004455 * \warning With implementations that interpret this number
4456 * as a hard limit, setting this number too small
4457 * may result in an infinite loop, whereby each
4458 * call results in immediate return with no ops
4459 * done (as there is not enough time to execute
4460 * any), and thus no result will ever be achieved.
4461 *
4462 * \note This only applies to functions whose
4463 * documentation mentions they may return
4464 * #PSA_OPERATION_INCOMPLETE.
4465 *
4466 * \param max_ops The maximum number of ops to be executed in a
4467 * single call. This can be a number from 0 to
4468 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4469 * is the least amount of work done per call.
4470 */
4471void psa_interruptible_set_max_ops(uint32_t max_ops);
4472
4473/**
4474 * \brief Get the maximum number of ops allowed to be
4475 * executed by an interruptible function in a
4476 * single call. This will return the last
4477 * value set by
4478 * \c psa_interruptible_set_max_ops() or
4479 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4480 * that function has never been called.
4481 *
4482 * \warning This is a beta API, and thus subject to change
4483 * at any point. It is not bound by the usual
4484 * interface stability promises.
4485 *
4486 * \return Maximum number of ops allowed to be
4487 * executed by an interruptible function in a
4488 * single call.
4489 */
4490uint32_t psa_interruptible_get_max_ops(void);
4491
4492/**
4493 * \brief Get the number of ops that a hash signing
4494 * operation has taken so far. If the operation
4495 * has completed, then this will represent the
4496 * number of ops required for the entire
4497 * operation. After initialization or calling
4498 * \c psa_sign_hash_interruptible_abort() on
4499 * the operation, a value of 0 will be returned.
4500 *
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004501 * \note This interface is guaranteed re-entrant and
4502 * thus may be called from driver code.
4503 *
Paul Elliott1265f002022-09-09 17:15:43 +01004504 * \warning This is a beta API, and thus subject to change
4505 * at any point. It is not bound by the usual
4506 * interface stability promises.
4507 *
4508 * This is a helper provided to help you tune the
4509 * value passed to \c
4510 * psa_interruptible_set_max_ops().
4511 *
4512 * \param operation The \c psa_sign_hash_interruptible_operation_t
4513 * to use. This must be initialized first.
4514 *
4515 * \return Number of ops that the operation has taken so
4516 * far.
4517 */
4518uint32_t psa_sign_hash_get_num_ops(
4519 const psa_sign_hash_interruptible_operation_t *operation);
4520
4521/**
4522 * \brief Get the number of ops that a hash verification
4523 * operation has taken so far. If the operation
4524 * has completed, then this will represent the
4525 * number of ops required for the entire
4526 * operation. After initialization or calling \c
4527 * psa_verify_hash_interruptible_abort() on the
4528 * operation, a value of 0 will be returned.
4529 *
4530 * \warning This is a beta API, and thus subject to change
4531 * at any point. It is not bound by the usual
4532 * interface stability promises.
4533 *
4534 * This is a helper provided to help you tune the
4535 * value passed to \c
4536 * psa_interruptible_set_max_ops().
4537 *
4538 * \param operation The \c
4539 * psa_verify_hash_interruptible_operation_t to
4540 * use. This must be initialized first.
4541 *
4542 * \return Number of ops that the operation has taken so
4543 * far.
4544 */
4545uint32_t psa_verify_hash_get_num_ops(
4546 const psa_verify_hash_interruptible_operation_t *operation);
4547
4548/**
4549 * \brief Start signing a hash or short message with a
4550 * private key, in an interruptible manner.
4551 *
4552 * \see \c psa_sign_hash_complete()
4553 *
4554 * \warning This is a beta API, and thus subject to change
4555 * at any point. It is not bound by the usual
4556 * interface stability promises.
4557 *
4558 * \note This function combined with \c
4559 * psa_sign_hash_complete() is equivalent to
4560 * \c psa_sign_hash() but
4561 * \c psa_sign_hash_complete() can return early and
4562 * resume according to the limit set with \c
4563 * psa_interruptible_set_max_ops() to reduce the
4564 * maximum time spent in a function call.
4565 *
4566 * \note Users should call \c psa_sign_hash_complete()
4567 * repeatedly on the same context after a
4568 * successful call to this function until \c
4569 * psa_sign_hash_complete() either returns 0 or an
4570 * error. \c psa_sign_hash_complete() will return
4571 * #PSA_OPERATION_INCOMPLETE if there is more work
4572 * to do. Alternatively users can call
4573 * \c psa_sign_hash_abort() at any point if they no
4574 * longer want the result.
4575 *
4576 * \note If this function returns an error status, the
4577 * operation enters an error state and must be
4578 * aborted by calling \c psa_sign_hash_abort().
4579 *
4580 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4581 * to use. This must be initialized first.
4582 *
4583 * \param key Identifier of the key to use for the operation.
4584 * It must be an asymmetric key pair. The key must
4585 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4586 * \param alg A signature algorithm (\c PSA_ALG_XXX
4587 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4588 * is true), that is compatible with
4589 * the type of \p key.
4590 * \param[in] hash The hash or message to sign.
4591 * \param hash_length Size of the \p hash buffer in bytes.
4592 *
4593 * \retval #PSA_SUCCESS
4594 * The operation started successfully - call \c psa_sign_hash_complete()
4595 * with the same context to complete the operation
4596 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004597 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004598 * \retval #PSA_ERROR_NOT_PERMITTED
4599 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4600 * not permit the requested algorithm.
4601 * \retval #PSA_ERROR_BAD_STATE
4602 * An operation has previously been started on this context, and is
4603 * still in progress.
Paul Elliott15d7d432023-02-27 17:17:56 +00004604 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4605 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4606 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4607 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4608 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4609 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4610 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4611 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4612 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4613 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004614 * \retval #PSA_ERROR_BAD_STATE
4615 * The library has not been previously initialized by psa_crypto_init().
4616 * It is implementation-dependent whether a failure to initialize
4617 * results in this error code.
4618 */
4619psa_status_t psa_sign_hash_start(
4620 psa_sign_hash_interruptible_operation_t *operation,
4621 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4622 const uint8_t *hash, size_t hash_length);
4623
4624/**
4625 * \brief Continue and eventually complete the action of
4626 * signing a hash or short message with a private
4627 * key, in an interruptible manner.
4628 *
4629 * \see \c psa_sign_hash_start()
4630 *
4631 * \warning This is a beta API, and thus subject to change
4632 * at any point. It is not bound by the usual
4633 * interface stability promises.
4634 *
4635 * \note This function combined with \c
4636 * psa_sign_hash_start() is equivalent to
4637 * \c psa_sign_hash() but this function can return
4638 * early and resume according to the limit set with
4639 * \c psa_interruptible_set_max_ops() to reduce the
4640 * maximum time spent in a function call.
4641 *
4642 * \note Users should call this function on the same
4643 * operation object repeatedly until it either
4644 * returns 0 or an error. This function will return
4645 * #PSA_OPERATION_INCOMPLETE if there is more work
4646 * to do. Alternatively users can call
4647 * \c psa_sign_hash_abort() at any point if they no
4648 * longer want the result.
4649 *
4650 * \note When this function returns successfully, the
4651 * operation becomes inactive. If this function
4652 * returns an error status, the operation enters an
4653 * error state and must be aborted by calling
4654 * \c psa_sign_hash_abort().
4655 *
4656 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4657 * to use. This must be initialized first, and have
4658 * had \c psa_sign_hash_start() called with it
4659 * first.
4660 *
4661 * \param[out] signature Buffer where the signature is to be written.
4662 * \param signature_size Size of the \p signature buffer in bytes. This
4663 * must be appropriate for the selected
4664 * algorithm and key:
4665 * - The required signature size is
4666 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4667 * key_bits, \c alg) where \c key_type and \c
4668 * key_bits are the type and bit-size
4669 * respectively of key.
4670 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4671 * maximum signature size of any supported
4672 * signature algorithm.
4673 * \param[out] signature_length On success, the number of bytes that make up
4674 * the returned signature value.
4675 *
4676 * \retval #PSA_SUCCESS
4677 * Operation completed successfully
4678 *
4679 * \retval #PSA_OPERATION_INCOMPLETE
4680 * Operation was interrupted due to the setting of \c
4681 * psa_interruptible_set_max_ops(). There is still work to be done.
4682 * Call this function again with the same operation object.
4683 *
4684 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4685 * The size of the \p signature buffer is too small. You can
4686 * determine a sufficient buffer size by calling
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004687 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
Paul Elliott1265f002022-09-09 17:15:43 +01004688 * where \c key_type and \c key_bits are the type and bit-size
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004689 * respectively of \c key.
Paul Elliott1265f002022-09-09 17:15:43 +01004690 *
4691 * \retval #PSA_ERROR_BAD_STATE
4692 * An operation was not previously started on this context via
4693 * \c psa_sign_hash_start().
4694 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004695 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4696 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4697 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4698 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4699 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4700 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4701 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4702 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4703 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4704 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004705 * \retval #PSA_ERROR_BAD_STATE
4706 * The library has either not been previously initialized by
Paul Elliott15d7d432023-02-27 17:17:56 +00004707 * psa_crypto_init() or you did not previously call
Paul Elliott1265f002022-09-09 17:15:43 +01004708 * psa_sign_hash_start() with this operation object. It is
4709 * implementation-dependent whether a failure to initialize results in
4710 * this error code.
4711 */
4712psa_status_t psa_sign_hash_complete(
4713 psa_sign_hash_interruptible_operation_t *operation,
4714 uint8_t *signature, size_t signature_size,
4715 size_t *signature_length);
4716
4717/**
4718 * \brief Abort a sign hash operation.
4719 *
4720 * \warning This is a beta API, and thus subject to change
4721 * at any point. It is not bound by the usual
4722 * interface stability promises.
Paul Elliott1b49ef52023-02-03 14:27:32 +00004723 *
4724 * \note This function is the only function that clears
4725 * the number of ops completed as part of the
4726 * operation. Please ensure you copy this value via
4727 * \c psa_sign_hash_get_num_ops() if required
4728 * before calling.
4729 *
Paul Elliott1265f002022-09-09 17:15:43 +01004730 * \note Aborting an operation frees all associated
4731 * resources except for the \p operation structure
4732 * itself. Once aborted, the operation object can
4733 * be reused for another operation by calling \c
4734 * psa_sign_hash_start() again.
4735 *
4736 * \note You may call this function any time after the
4737 * operation object has been initialized. In
4738 * particular, calling \c psa_sign_hash_abort()
4739 * after the operation has already been terminated
4740 * by a call to \c psa_sign_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004741 * psa_sign_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004742 *
4743 * \param[in,out] operation Initialized sign hash operation.
4744 *
4745 * \retval #PSA_SUCCESS
4746 * The operation was aborted successfully.
4747 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004748 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004749 * \retval #PSA_ERROR_BAD_STATE
4750 * The library has not been previously initialized by psa_crypto_init().
4751 * It is implementation-dependent whether a failure to initialize
4752 * results in this error code.
4753 */
4754psa_status_t psa_sign_hash_abort(
4755 psa_sign_hash_interruptible_operation_t *operation);
4756
4757/**
4758 * \brief Start reading and verifying a hash or short
4759 * message, in an interruptible manner.
4760 *
4761 * \see \c psa_verify_hash_complete()
4762 *
4763 * \warning This is a beta API, and thus subject to change
4764 * at any point. It is not bound by the usual
4765 * interface stability promises.
4766 *
4767 * \note This function combined with \c
4768 * psa_verify_hash_complete() is equivalent to
4769 * \c psa_verify_hash() but \c
4770 * psa_verify_hash_complete() can return early and
4771 * resume according to the limit set with \c
4772 * psa_interruptible_set_max_ops() to reduce the
4773 * maximum time spent in a function.
4774 *
4775 * \note Users should call \c psa_verify_hash_complete()
4776 * repeatedly on the same operation object after a
4777 * successful call to this function until \c
4778 * psa_verify_hash_complete() either returns 0 or
4779 * an error. \c psa_verify_hash_complete() will
4780 * return #PSA_OPERATION_INCOMPLETE if there is
4781 * more work to do. Alternatively users can call
4782 * \c psa_verify_hash_abort() at any point if they
4783 * no longer want the result.
4784 *
4785 * \note If this function returns an error status, the
4786 * operation enters an error state and must be
4787 * aborted by calling \c psa_verify_hash_abort().
4788 *
4789 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4790 * to use. This must be initialized first.
4791 *
4792 * \param key Identifier of the key to use for the operation.
4793 * The key must allow the usage
4794 * #PSA_KEY_USAGE_VERIFY_HASH.
4795 * \param alg A signature algorithm (\c PSA_ALG_XXX
4796 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4797 * is true), that is compatible with
4798 * the type of \p key.
4799 * \param[in] hash The hash whose signature is to be verified.
4800 * \param hash_length Size of the \p hash buffer in bytes.
4801 * \param[in] signature Buffer containing the signature to verify.
4802 * \param signature_length Size of the \p signature buffer in bytes.
4803 *
4804 * \retval #PSA_SUCCESS
4805 * The operation started successfully - please call \c
4806 * psa_verify_hash_complete() with the same context to complete the
4807 * operation.
4808 *
4809 * \retval #PSA_ERROR_BAD_STATE
4810 * Another operation has already been started on this context, and is
4811 * still in progress.
4812 *
4813 * \retval #PSA_ERROR_NOT_PERMITTED
4814 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4815 * not permit the requested algorithm.
4816 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004817 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4818 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4821 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4822 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4823 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4824 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4825 * \retval PSA_ERROR_DATA_INVALID \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004826 * \retval #PSA_ERROR_BAD_STATE
4827 * The library has not been previously initialized by psa_crypto_init().
4828 * It is implementation-dependent whether a failure to initialize
4829 * results in this error code.
4830 */
4831psa_status_t psa_verify_hash_start(
4832 psa_verify_hash_interruptible_operation_t *operation,
4833 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4834 const uint8_t *hash, size_t hash_length,
4835 const uint8_t *signature, size_t signature_length);
4836
4837/**
4838 * \brief Continue and eventually complete the action of
4839 * reading and verifying a hash or short message
4840 * signed with a private key, in an interruptible
4841 * manner.
4842 *
4843 * \see \c psa_verify_hash_start()
4844 *
4845 * \warning This is a beta API, and thus subject to change
4846 * at any point. It is not bound by the usual
4847 * interface stability promises.
4848 *
4849 * \note This function combined with \c
4850 * psa_verify_hash_start() is equivalent to
4851 * \c psa_verify_hash() but this function can
4852 * return early and resume according to the limit
4853 * set with \c psa_interruptible_set_max_ops() to
4854 * reduce the maximum time spent in a function
4855 * call.
4856 *
4857 * \note Users should call this function on the same
4858 * operation object repeatedly until it either
4859 * returns 0 or an error. This function will return
4860 * #PSA_OPERATION_INCOMPLETE if there is more work
4861 * to do. Alternatively users can call
4862 * \c psa_verify_hash_abort() at any point if they
4863 * no longer want the result.
4864 *
4865 * \note When this function returns successfully, the
4866 * operation becomes inactive. If this function
4867 * returns an error status, the operation enters an
4868 * error state and must be aborted by calling
4869 * \c psa_verify_hash_abort().
4870 *
4871 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4872 * to use. This must be initialized first, and have
4873 * had \c psa_verify_hash_start() called with it
4874 * first.
4875 *
4876 * \retval #PSA_SUCCESS
4877 * Operation completed successfully, and the passed signature is valid.
4878 *
4879 * \retval #PSA_OPERATION_INCOMPLETE
4880 * Operation was interrupted due to the setting of \c
4881 * psa_interruptible_set_max_ops(). There is still work to be done.
4882 * Call this function again with the same operation object.
4883 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004884 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004885 * \retval #PSA_ERROR_INVALID_SIGNATURE
4886 * The calculation was performed successfully, but the passed
4887 * signature is not a valid signature.
Paul Elliott15d7d432023-02-27 17:17:56 +00004888 * \retval #PSA_ERROR_BAD_STATE
Paul Elliott1265f002022-09-09 17:15:43 +01004889 * An operation was not previously started on this context via
4890 * \c psa_verify_hash_start().
Paul Elliott15d7d432023-02-27 17:17:56 +00004891 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4892 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4895 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4896 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4897 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4898 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4899 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4900 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004901 * \retval #PSA_ERROR_BAD_STATE
4902 * The library has either not been previously initialized by
4903 * psa_crypto_init() or you did not previously call
4904 * psa_verify_hash_start() on this object. It is
4905 * implementation-dependent whether a failure to initialize results in
4906 * this error code.
4907 */
4908psa_status_t psa_verify_hash_complete(
4909 psa_verify_hash_interruptible_operation_t *operation);
4910
4911/**
4912 * \brief Abort a verify hash operation.
4913 *
4914 * \warning This is a beta API, and thus subject to change at
4915 * any point. It is not bound by the usual interface
4916 * stability promises.
4917 *
Paul Elliott1b49ef52023-02-03 14:27:32 +00004918 * \note This function is the only function that clears the
4919 * number of ops completed as part of the operation.
4920 * Please ensure you copy this value via
4921 * \c psa_verify_hash_get_num_ops() if required
4922 * before calling.
4923 *
Paul Elliott1265f002022-09-09 17:15:43 +01004924 * \note Aborting an operation frees all associated
4925 * resources except for the operation structure
4926 * itself. Once aborted, the operation object can be
4927 * reused for another operation by calling \c
4928 * psa_verify_hash_start() again.
4929 *
4930 * \note You may call this function any time after the
4931 * operation object has been initialized.
4932 * In particular, calling \c psa_verify_hash_abort()
4933 * after the operation has already been terminated by
4934 * a call to \c psa_verify_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004935 * psa_verify_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004936 *
4937 * \param[in,out] operation Initialized verify hash operation.
4938 *
4939 * \retval #PSA_SUCCESS
4940 * The operation was aborted successfully.
4941 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004942 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004943 * \retval #PSA_ERROR_BAD_STATE
4944 * The library has not been previously initialized by psa_crypto_init().
4945 * It is implementation-dependent whether a failure to initialize
4946 * results in this error code.
4947 */
4948psa_status_t psa_verify_hash_abort(
4949 psa_verify_hash_interruptible_operation_t *operation);
4950
4951
4952/**@}*/
4953
Gilles Peskinee59236f2018-01-27 23:32:46 +01004954#ifdef __cplusplus
4955}
4956#endif
4957
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004958/* The file "crypto_sizes.h" contains definitions for size calculation
4959 * macros whose definitions are implementation-specific. */
4960#include "crypto_sizes.h"
4961
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004962/* The file "crypto_struct.h" contains definitions for
4963 * implementation-specific structs that are declared above. */
Gilles Peskineb1176f22023-02-22 22:07:28 +01004964#if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)
4965#include MBEDTLS_PSA_CRYPTO_STRUCT_FILE
4966#else
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004967#include "crypto_struct.h"
Gilles Peskineb1176f22023-02-22 22:07:28 +01004968#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004969
4970/* The file "crypto_extra.h" contains vendor-specific definitions. This
4971 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004972#include "crypto_extra.h"
4973
4974#endif /* PSA_CRYPTO_H */