blob: fd1928a6564e8fa5bd4383accd65df0fb05441c2 [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 Peskine35ef36b2019-05-16 19:42:05 +0200122 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200123 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200124 *
Gilles Peskine20628592019-04-19 19:29:50 +0200125 * This function may be declared as `static` (i.e. without external
126 * linkage). This function may be provided as a function-like macro,
127 * but in this case it must evaluate each of its arguments exactly once.
128 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200129 * \param[out] attributes The attribute structure to write to.
130 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200131 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132static void psa_set_key_id(psa_key_attributes_t *attributes,
133 mbedtls_svc_key_id_t key);
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200134
Ronald Cron6b5ff532020-10-16 14:38:19 +0200135#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
136/** Set the owner identifier of a key.
137 *
138 * When key identifiers encode key owner identifiers, psa_set_key_id() does
139 * not allow to define in key attributes the owner of volatile keys as
140 * psa_set_key_id() enforces the key to be persistent.
141 *
142 * This function allows to set in key attributes the owner identifier of a
143 * key. It is intended to be used for volatile keys. For persistent keys,
144 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
145 * the owner of a key.
146 *
147 * \param[out] attributes The attribute structure to write to.
Antonio de Angelise2b68662021-11-30 12:21:44 +0000148 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
151 mbedtls_key_owner_id_t owner);
Ronald Cron6b5ff532020-10-16 14:38:19 +0200152#endif
153
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200154/** Set the location of a persistent key.
155 *
156 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200157 * with psa_set_key_id(). By default, a key that has a persistent identifier
158 * is stored in the default storage area identifier by
159 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
160 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200162 * This function does not access storage, it merely stores the given
163 * value in the structure.
164 * The persistent key will be written to storage when the attribute
165 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200166 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200167 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate each of its arguments exactly once.
172 *
173 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200174 * \param lifetime The lifetime for the key.
175 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200176 * key will be volatile, and the key identifier
177 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200178 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200179static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
180 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200181
Gilles Peskine20628592019-04-19 19:29:50 +0200182/** Retrieve the key identifier from key attributes.
183 *
184 * This function may be declared as `static` (i.e. without external
185 * linkage). This function may be provided as a function-like macro,
186 * but in this case it must evaluate its argument exactly once.
187 *
188 * \param[in] attributes The key attribute structure to query.
189 *
190 * \return The persistent identifier stored in the attribute structure.
191 * This value is unspecified if the attribute structure declares
192 * the key as volatile.
193 */
Ronald Cron71016a92020-08-28 19:01:50 +0200194static mbedtls_svc_key_id_t psa_get_key_id(
195 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200196
Gilles Peskine20628592019-04-19 19:29:50 +0200197/** Retrieve the lifetime from key attributes.
198 *
199 * This function may be declared as `static` (i.e. without external
200 * linkage). This function may be provided as a function-like macro,
201 * but in this case it must evaluate its argument exactly once.
202 *
203 * \param[in] attributes The key attribute structure to query.
204 *
205 * \return The lifetime value stored in the attribute structure.
206 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200207static psa_key_lifetime_t psa_get_key_lifetime(
208 const psa_key_attributes_t *attributes);
209
Gilles Peskine20628592019-04-19 19:29:50 +0200210/** Declare usage flags for a key.
211 *
212 * Usage flags are part of a key's usage policy. They encode what
213 * kind of operations are permitted on the key. For more details,
214 * refer to the documentation of the type #psa_key_usage_t.
215 *
216 * This function overwrites any usage flags
217 * previously set in \p attributes.
218 *
219 * This function may be declared as `static` (i.e. without external
220 * linkage). This function may be provided as a function-like macro,
221 * but in this case it must evaluate each of its arguments exactly once.
222 *
223 * \param[out] attributes The attribute structure to write to.
224 * \param usage_flags The usage flags to write.
225 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200226static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
227 psa_key_usage_t usage_flags);
228
Gilles Peskine20628592019-04-19 19:29:50 +0200229/** Retrieve the usage flags from key attributes.
230 *
231 * This function may be declared as `static` (i.e. without external
232 * linkage). This function may be provided as a function-like macro,
233 * but in this case it must evaluate its argument exactly once.
234 *
235 * \param[in] attributes The key attribute structure to query.
236 *
237 * \return The usage flags stored in the attribute structure.
238 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200239static psa_key_usage_t psa_get_key_usage_flags(
240 const psa_key_attributes_t *attributes);
241
Gilles Peskine20628592019-04-19 19:29:50 +0200242/** Declare the permitted algorithm policy for a key.
243 *
244 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200245 * algorithms are permitted to be used with this key. The following
246 * algorithm policies are supported:
247 * - 0 does not allow any cryptographic operation with the key. The key
248 * may be used for non-cryptographic actions such as exporting (if
249 * permitted by the usage flags).
250 * - An algorithm value permits this particular algorithm.
251 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
252 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100253 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100254 * any MAC algorithm from the same base class (e.g. CMAC) which
255 * generates/verifies a MAC length greater than or equal to the length
256 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100257 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
258 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100259 * generates/verifies a tag length greater than or equal to the length
260 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200261 *
262 * This function overwrites any algorithm policy
263 * previously set in \p attributes.
264 *
265 * This function may be declared as `static` (i.e. without external
266 * linkage). This function may be provided as a function-like macro,
267 * but in this case it must evaluate each of its arguments exactly once.
268 *
269 * \param[out] attributes The attribute structure to write to.
270 * \param alg The permitted algorithm policy to write.
271 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200272static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
273 psa_algorithm_t alg);
274
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100275
Gilles Peskine20628592019-04-19 19:29:50 +0200276/** Retrieve the algorithm policy from key attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate its argument exactly once.
281 *
282 * \param[in] attributes The key attribute structure to query.
283 *
284 * \return The algorithm stored in the attribute structure.
285 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200286static psa_algorithm_t psa_get_key_algorithm(
287 const psa_key_attributes_t *attributes);
288
Gilles Peskine20628592019-04-19 19:29:50 +0200289/** Declare the type of a key.
290 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200291 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200292 * previously set in \p attributes.
293 *
294 * This function may be declared as `static` (i.e. without external
295 * linkage). This function may be provided as a function-like macro,
296 * but in this case it must evaluate each of its arguments exactly once.
297 *
298 * \param[out] attributes The attribute structure to write to.
299 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200300 * If this is 0, the key type in \p attributes
301 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200302 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200303static void psa_set_key_type(psa_key_attributes_t *attributes,
304 psa_key_type_t type);
305
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100306
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200307/** Declare the size of a key.
308 *
309 * This function overwrites any key size previously set in \p attributes.
310 *
311 * This function may be declared as `static` (i.e. without external
312 * linkage). This function may be provided as a function-like macro,
313 * but in this case it must evaluate each of its arguments exactly once.
314 *
315 * \param[out] attributes The attribute structure to write to.
316 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200317 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200318 * becomes unspecified. Keys of size 0 are
319 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200320 */
321static void psa_set_key_bits(psa_key_attributes_t *attributes,
322 size_t bits);
323
Gilles Peskine20628592019-04-19 19:29:50 +0200324/** Retrieve the key type from key attributes.
325 *
326 * This function may be declared as `static` (i.e. without external
327 * linkage). This function may be provided as a function-like macro,
328 * but in this case it must evaluate its argument exactly once.
329 *
330 * \param[in] attributes The key attribute structure to query.
331 *
332 * \return The key type stored in the attribute structure.
333 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200334static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
335
Gilles Peskine20628592019-04-19 19:29:50 +0200336/** Retrieve the key size from key attributes.
337 *
338 * This function may be declared as `static` (i.e. without external
339 * linkage). This function may be provided as a function-like macro,
340 * but in this case it must evaluate its argument exactly once.
341 *
342 * \param[in] attributes The key attribute structure to query.
343 *
344 * \return The key size stored in the attribute structure, in bits.
345 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200346static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
347
Gilles Peskine20628592019-04-19 19:29:50 +0200348/** Retrieve the attributes of a key.
349 *
350 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200351 * psa_reset_key_attributes(). It then copies the attributes of
352 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200353 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200354 * \note This function may allocate memory or other resources.
355 * Once you have called this function on an attribute structure,
356 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200357 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200358 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200359 * \param[in,out] attributes On success, the attributes of the key.
360 * On failure, equivalent to a
361 * freshly-initialized structure.
362 *
Gilles Peskineed733552023-02-14 19:21:09 +0100363 * \retval #PSA_SUCCESS \emptydescription
364 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
365 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
367 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
368 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
369 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
370 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100371 * \retval #PSA_ERROR_BAD_STATE
372 * The library has not been previously initialized by psa_crypto_init().
373 * It is implementation-dependent whether a failure to initialize
374 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200375 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200376psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200377 psa_key_attributes_t *attributes);
378
Gilles Peskine20628592019-04-19 19:29:50 +0200379/** Reset a key attribute structure to a freshly initialized state.
380 *
381 * You must initialize the attribute structure as described in the
382 * documentation of the type #psa_key_attributes_t before calling this
383 * function. Once the structure has been initialized, you may call this
384 * function at any time.
385 *
386 * This function frees any auxiliary resources that the structure
387 * may contain.
388 *
389 * \param[in,out] attributes The attribute structure to reset.
390 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200391void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200392
Gilles Peskine87a5e562019-04-17 12:28:25 +0200393/**@}*/
394
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395/** \defgroup key_management Key management
396 * @{
397 */
398
Ronald Cron277a85f2020-08-04 15:49:48 +0200399/** Remove non-essential copies of key material from memory.
400 *
401 * If the key identifier designates a volatile key, this functions does not do
402 * anything and returns successfully.
403 *
404 * If the key identifier designates a persistent key, then this function will
405 * free all resources associated with the key in volatile memory. The key
406 * data in persistent storage is not affected and the key can still be used.
407 *
408 * \param key Identifier of the key to purge.
409 *
410 * \retval #PSA_SUCCESS
411 * The key material will have been removed from memory if it is not
412 * currently required.
413 * \retval #PSA_ERROR_INVALID_ARGUMENT
414 * \p key is not a valid key identifier.
415 * \retval #PSA_ERROR_BAD_STATE
416 * The library has not been previously initialized by psa_crypto_init().
417 * It is implementation-dependent whether a failure to initialize
Ryan Everettc70ce572024-01-03 16:04:33 +0000418 * results in this error code. Or,
419 * this call was operating on a key slot and found the slot in
420 * an invalid state for the operation.
Ronald Cron277a85f2020-08-04 15:49:48 +0200421 */
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 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200530 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200531 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100532 *
533 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200534 * \p key was a valid identifier and the key material that it
535 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100536 * \retval #PSA_ERROR_NOT_PERMITTED
537 * The key cannot be erased because it is
538 * read-only, either due to a policy or due to physical restrictions.
539 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200540 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100542 * There was a failure in communication with the cryptoprocessor.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100543 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100544 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100545 * This error is typically a result of either storage corruption on a
546 * cleartext storage backend, or an attempt to read data that was
547 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100548 * \retval #PSA_ERROR_STORAGE_FAILURE
549 * The storage is corrupted. Implementations shall make a best effort
550 * to erase key material even in this stage, however applications
551 * should be aware that it may be impossible to guarantee that the
552 * key material is not recoverable in such cases.
553 * \retval #PSA_ERROR_CORRUPTION_DETECTED
554 * An unexpected condition which is not a storage corruption or
555 * a communication failure occurred. The cryptoprocessor may have
556 * been compromised.
557 * \retval #PSA_ERROR_BAD_STATE
558 * The library has not been previously initialized by psa_crypto_init().
559 * It is implementation-dependent whether a failure to initialize
Ryan Everettc70ce572024-01-03 16:04:33 +0000560 * results in this error code. Or,
561 * this call was operating on a key slot and found the slot in
562 * an invalid state for the operation.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100563 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200564psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100565
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100566/**@}*/
567
568/** \defgroup import_export Key import and export
569 * @{
570 */
571
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572/**
573 * \brief Import a key in binary format.
574 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100575 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100576 * documentation of psa_export_public_key() for the format of public keys
577 * and to the documentation of psa_export_key() for the format for
578 * other key types.
579 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200580 * The key data determines the key size. The attributes may optionally
581 * specify a key size; in this case it must match the size determined
582 * from the key data. A key size of 0 in \p attributes indicates that
583 * the key size is solely determined by the key data.
584 *
585 * Implementations must reject an attempt to import a key of size 0.
586 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100587 * This specification supports a single format for each key type.
588 * Implementations may support other formats as long as the standard
589 * format is supported. Implementations that support other formats
590 * should ensure that the formats are clearly unambiguous so as to
591 * minimize the risk that an invalid input is accidentally interpreted
592 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593 *
Gilles Peskine20628592019-04-19 19:29:50 +0200594 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200595 * The key size is always determined from the
596 * \p data buffer.
597 * If the key size in \p attributes is nonzero,
598 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200599 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200600 * For persistent keys, this is the key identifier
601 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100603 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200604 * buffer is interpreted according to the type declared
605 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200606 * All implementations must support at least the format
607 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100608 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * the chosen type. Implementations may allow other
610 * formats, but should be conservative: implementations
611 * should err on the side of rejecting content if it
612 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200613 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100614 *
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100616 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100617 * If the key is persistent, the key material and the key's metadata
618 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200619 * \retval #PSA_ERROR_ALREADY_EXISTS
620 * This is an attempt to create a persistent key, and there is
621 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200622 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200623 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200624 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200625 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500626 * The key attributes, as a whole, are invalid, or
627 * the key data is not correctly formatted, or
628 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200629 * of the key data.
Gilles Peskineed733552023-02-14 19:21:09 +0100630 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
631 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
633 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
634 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
635 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
636 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
637 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300638 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300639 * The library has not been previously initialized by psa_crypto_init().
640 * It is implementation-dependent whether a failure to initialize
641 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200643psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200645 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200646 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100648
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649
650/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 * \brief Export a key in binary format.
652 *
653 * The output of this function can be passed to psa_import_key() to
654 * create an equivalent object.
655 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100656 * If the implementation of psa_import_key() supports other formats
657 * beyond the format specified here, the output from psa_export_key()
658 * must use the representation specified here, not the original
659 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100661 * For standard key types, the output format is as follows:
662 *
663 * - For symmetric keys (including MAC keys), the format is the
664 * raw bytes of the key.
665 * - For DES, the key data consists of 8 bytes. The parity bits must be
666 * correct.
667 * - For Triple-DES, the format is the concatenation of the
668 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200669 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200670 * is the non-encrypted DER encoding of the representation defined by
671 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
672 * ```
673 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200674 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200675 * modulus INTEGER, -- n
676 * publicExponent INTEGER, -- e
677 * privateExponent INTEGER, -- d
678 * prime1 INTEGER, -- p
679 * prime2 INTEGER, -- q
680 * exponent1 INTEGER, -- d mod (p-1)
681 * exponent2 INTEGER, -- d mod (q-1)
682 * coefficient INTEGER, -- (inverse of q) mod p
683 * }
684 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200685 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200686 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100687 * a representation of the private value as a `ceiling(m/8)`-byte string
688 * where `m` is the bit size associated with the curve, i.e. the bit size
689 * of the order of the curve's coordinate field. This byte string is
690 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100691 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
692 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
693 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200694 * For Weierstrass curves, this is the content of the `privateKey` field of
695 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
696 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100697 * For twisted Edwards curves, the private key is as defined by RFC 8032
698 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200699 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200700 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000701 * format is the representation of the private key `x` as a big-endian byte
702 * string. The length of the byte string is the private key size in bytes
703 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200704 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
705 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100706 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200707 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
708 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200709 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200710 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200711 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200712 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200713 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200714 * \param[out] data_length On success, the number of bytes
715 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716 *
Gilles Peskineed733552023-02-14 19:21:09 +0100717 * \retval #PSA_SUCCESS \emptydescription
718 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200719 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200720 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Gilles Peskineed733552023-02-14 19:21:09 +0100721 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200722 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
723 * The size of the \p data buffer is too small. You can determine a
724 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100725 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200726 * where \c type is the key type
727 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100728 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
729 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
730 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
731 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
732 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300733 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300734 * The library has not been previously initialized by psa_crypto_init().
735 * It is implementation-dependent whether a failure to initialize
736 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200738psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 uint8_t *data,
740 size_t data_size,
741 size_t *data_length);
742
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100743/**
744 * \brief Export a public key or the public part of a key pair in binary format.
745 *
746 * The output of this function can be passed to psa_import_key() to
747 * create an object that is equivalent to the public key.
748 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000749 * This specification supports a single format for each key type.
750 * Implementations may support other formats as long as the standard
751 * format is supported. Implementations that support other formats
752 * should ensure that the formats are clearly unambiguous so as to
753 * minimize the risk that an invalid input is accidentally interpreted
754 * according to a different format.
755 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000756 * For standard key types, the output format is as follows:
757 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
758 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
759 * ```
760 * RSAPublicKey ::= SEQUENCE {
761 * modulus INTEGER, -- n
762 * publicExponent INTEGER } -- e
763 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100764 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200765 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100766 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
767 * by RFC 8032
768 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
769 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000770 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
771 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
772 * Let `m` be the bit size associated with the curve, i.e. the bit size of
773 * `q` for a curve over `F_q`. The representation consists of:
774 * - The byte 0x04;
775 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
776 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200777 * - For Diffie-Hellman key exchange public keys (key types for which
778 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000779 * the format is the representation of the public key `y = g^x mod p` as a
780 * big-endian byte string. The length of the byte string is the length of the
781 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100782 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200783 * Exporting a public key object or the public part of a key pair is
784 * always permitted, regardless of the key's usage flags.
785 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200786 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200787 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200788 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200789 * \param[out] data_length On success, the number of bytes
790 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100791 *
Gilles Peskineed733552023-02-14 19:21:09 +0100792 * \retval #PSA_SUCCESS \emptydescription
793 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200794 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200795 * The key is neither a public key nor a key pair.
Gilles Peskineed733552023-02-14 19:21:09 +0100796 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
798 * The size of the \p data buffer is too small. You can determine a
799 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100800 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200801 * where \c type is the key type
802 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
804 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
805 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
806 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
807 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300808 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300809 * The library has not been previously initialized by psa_crypto_init().
810 * It is implementation-dependent whether a failure to initialize
811 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100812 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200813psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100814 uint8_t *data,
815 size_t data_size,
816 size_t *data_length);
817
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100818
Gilles Peskine20035e32018-02-03 22:44:14 +0100819
820/**@}*/
821
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100822/** \defgroup hash Message digests
823 * @{
824 */
825
Gilles Peskine69647a42019-01-14 20:18:12 +0100826/** Calculate the hash (digest) of a message.
827 *
828 * \note To verify the hash of a message against an
829 * expected value, use psa_hash_compare() instead.
830 *
831 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
832 * such that #PSA_ALG_IS_HASH(\p alg) is true).
833 * \param[in] input Buffer containing the message to hash.
834 * \param input_length Size of the \p input buffer in bytes.
835 * \param[out] hash Buffer where the hash is to be written.
836 * \param hash_size Size of the \p hash buffer in bytes.
837 * \param[out] hash_length On success, the number of bytes
838 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100839 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100840 *
841 * \retval #PSA_SUCCESS
842 * Success.
843 * \retval #PSA_ERROR_NOT_SUPPORTED
844 * \p alg is not supported or is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100845 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100846 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
847 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +0100848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
850 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
851 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100852 * \retval #PSA_ERROR_BAD_STATE
853 * The library has not been previously initialized by psa_crypto_init().
854 * It is implementation-dependent whether a failure to initialize
855 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100856 */
857psa_status_t psa_hash_compute(psa_algorithm_t alg,
858 const uint8_t *input,
859 size_t input_length,
860 uint8_t *hash,
861 size_t hash_size,
862 size_t *hash_length);
863
864/** Calculate the hash (digest) of a message and compare it with a
865 * reference value.
866 *
867 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
868 * such that #PSA_ALG_IS_HASH(\p alg) is true).
869 * \param[in] input Buffer containing the message to hash.
870 * \param input_length Size of the \p input buffer in bytes.
871 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100872 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100873 *
874 * \retval #PSA_SUCCESS
875 * The expected hash is identical to the actual hash of the input.
876 * \retval #PSA_ERROR_INVALID_SIGNATURE
877 * The hash of the message was calculated successfully, but it
878 * differs from the expected hash.
879 * \retval #PSA_ERROR_NOT_SUPPORTED
880 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100881 * \retval #PSA_ERROR_INVALID_ARGUMENT
882 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskineed733552023-02-14 19:21:09 +0100883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
885 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
886 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100887 * \retval #PSA_ERROR_BAD_STATE
888 * The library has not been previously initialized by psa_crypto_init().
889 * It is implementation-dependent whether a failure to initialize
890 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100891 */
892psa_status_t psa_hash_compare(psa_algorithm_t alg,
893 const uint8_t *input,
894 size_t input_length,
895 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100896 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100897
Gilles Peskine308b91d2018-02-08 09:47:44 +0100898/** The type of the state data structure for multipart hash operations.
899 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000900 * Before calling any function on a hash operation object, the application must
901 * initialize it by any of the following means:
902 * - Set the structure to all-bits-zero, for example:
903 * \code
904 * psa_hash_operation_t operation;
905 * memset(&operation, 0, sizeof(operation));
906 * \endcode
907 * - Initialize the structure to logical zero values, for example:
908 * \code
909 * psa_hash_operation_t operation = {0};
910 * \endcode
911 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
912 * for example:
913 * \code
914 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
915 * \endcode
916 * - Assign the result of the function psa_hash_operation_init()
917 * to the structure, for example:
918 * \code
919 * psa_hash_operation_t operation;
920 * operation = psa_hash_operation_init();
921 * \endcode
922 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100923 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +0100924 * make any assumptions about the content of this structure.
925 * Implementation details can change in future versions without notice. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100926typedef struct psa_hash_operation_s psa_hash_operation_t;
927
Jaeden Amero6a25b412019-01-04 11:47:44 +0000928/** \def PSA_HASH_OPERATION_INIT
929 *
930 * This macro returns a suitable initializer for a hash operation object
931 * of type #psa_hash_operation_t.
932 */
Jaeden Amero6a25b412019-01-04 11:47:44 +0000933
934/** Return an initial value for a hash operation object.
935 */
936static psa_hash_operation_t psa_hash_operation_init(void);
937
Gilles Peskinef45adda2019-01-14 18:29:18 +0100938/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939 *
940 * The sequence of operations to calculate a hash (message digest)
941 * is as follows:
942 * -# Allocate an operation object which will be passed to all the functions
943 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000944 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100945 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200946 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100947 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * of the message each time. The hash that is calculated is the hash
949 * of the concatenation of these messages in order.
950 * -# To calculate the hash, call psa_hash_finish().
951 * To compare the hash with an expected value, call psa_hash_verify().
952 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100953 * If an error occurs at any step after a call to psa_hash_setup(), the
954 * operation will need to be reset by a call to psa_hash_abort(). The
955 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000956 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200958 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100959 * eventually terminate the operation. The following events terminate an
960 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100961 * - A successful call to psa_hash_finish() or psa_hash_verify().
962 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000964 * \param[in,out] operation The operation object to set up. It must have
965 * been initialized as per the documentation for
966 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200967 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
968 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100969 *
Gilles Peskine28538492018-07-11 17:34:00 +0200970 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200972 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100973 * \p alg is not a supported hash algorithm.
974 * \retval #PSA_ERROR_INVALID_ARGUMENT
975 * \p alg is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
978 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
979 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100980 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500981 * The operation state is not valid (it must be inactive), or
982 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100983 * It is implementation-dependent whether a failure to initialize
984 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200986psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100987 psa_algorithm_t alg);
988
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989/** Add a message fragment to a multipart hash operation.
990 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100993 * If this function returns an error status, the operation enters an error
994 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200996 * \param[in,out] operation Active hash operation.
997 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200998 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 *
Gilles Peskine28538492018-07-11 17:34:00 +02001000 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1004 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1005 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001006 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001007 * The operation state is not valid (it must be active), or
1008 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001009 * It is implementation-dependent whether a failure to initialize
1010 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001011 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001012psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1013 const uint8_t *input,
1014 size_t input_length);
1015
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016/** Finish the calculation of the hash of a message.
1017 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001018 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 * This function calculates the hash of the message formed by concatenating
1020 * the inputs passed to preceding calls to psa_hash_update().
1021 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001022 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001023 * If this function returns an error status, the operation enters an error
1024 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 *
1026 * \warning Applications should not call this function if they expect
1027 * a specific value for the hash. Call psa_hash_verify() instead.
1028 * Beware that comparing integrity or authenticity data such as
1029 * hash values with a function such as \c memcmp is risky
1030 * because the time taken by the comparison may leak information
1031 * about the hashed data which could allow an attacker to guess
1032 * a valid hash and thereby bypass security controls.
1033 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001034 * \param[in,out] operation Active hash operation.
1035 * \param[out] hash Buffer where the hash is to be written.
1036 * \param hash_size Size of the \p hash buffer in bytes.
1037 * \param[out] hash_length On success, the number of bytes
1038 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001039 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001040 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 *
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001044 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001045 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001046 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +01001048 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1049 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1050 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1051 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001052 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001053 * The operation state is not valid (it must be active), or
1054 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001055 * It is implementation-dependent whether a failure to initialize
1056 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001058psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1059 uint8_t *hash,
1060 size_t hash_size,
1061 size_t *hash_length);
1062
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063/** Finish the calculation of the hash of a message and compare it with
1064 * an expected value.
1065 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001066 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * This function calculates the hash of the message formed by concatenating
1068 * the inputs passed to preceding calls to psa_hash_update(). It then
1069 * compares the calculated hash with the expected hash passed as a
1070 * parameter to this function.
1071 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001072 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001073 * If this function returns an error status, the operation enters an error
1074 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 *
Gilles Peskine19067982018-03-20 17:54:53 +01001076 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077 * comparison between the actual hash and the expected hash is performed
1078 * in constant time.
1079 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001080 * \param[in,out] operation Active hash operation.
1081 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001082 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 *
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001086 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 * The hash of the message was calculated successfully, but it
1088 * differs from the expected hash.
Gilles Peskineed733552023-02-14 19:21:09 +01001089 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1090 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1091 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1092 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001093 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001094 * The operation state is not valid (it must be active), or
1095 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001096 * It is implementation-dependent whether a failure to initialize
1097 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001099psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1100 const uint8_t *hash,
1101 size_t hash_length);
1102
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103/** Abort a hash operation.
1104 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001106 * \p operation structure itself. Once aborted, the operation object
1107 * can be reused for another operation by calling
1108 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001110 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001111 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * In particular, calling psa_hash_abort() after the operation has been
1114 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1115 * psa_hash_verify() is safe and has no effect.
1116 *
1117 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001118 *
Gilles Peskineed733552023-02-14 19:21:09 +01001119 * \retval #PSA_SUCCESS \emptydescription
1120 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1121 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1122 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001123 * \retval #PSA_ERROR_BAD_STATE
1124 * The library has not been previously initialized by psa_crypto_init().
1125 * It is implementation-dependent whether a failure to initialize
1126 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 */
1128psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001130/** Clone a hash operation.
1131 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001132 * This function copies the state of an ongoing hash operation to
1133 * a new operation object. In other words, this function is equivalent
1134 * to calling psa_hash_setup() on \p target_operation with the same
1135 * algorithm that \p source_operation was set up for, then
1136 * psa_hash_update() on \p target_operation with the same input that
1137 * that was passed to \p source_operation. After this function returns, the
1138 * two objects are independent, i.e. subsequent calls involving one of
1139 * the objects do not affect the other object.
1140 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001141 * \param[in] source_operation The active hash operation to clone.
1142 * \param[in,out] target_operation The operation object to set up.
1143 * It must be initialized but not active.
1144 *
Gilles Peskineed733552023-02-14 19:21:09 +01001145 * \retval #PSA_SUCCESS \emptydescription
1146 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1147 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1148 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001150 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001151 * The \p source_operation state is not valid (it must be active), or
1152 * the \p target_operation state is not valid (it must be inactive), or
1153 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001154 * It is implementation-dependent whether a failure to initialize
1155 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 */
1157psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1158 psa_hash_operation_t *target_operation);
1159
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001160/**@}*/
1161
Gilles Peskine8c9def32018-02-08 10:02:12 +01001162/** \defgroup MAC Message authentication codes
1163 * @{
1164 */
1165
Gilles Peskine69647a42019-01-14 20:18:12 +01001166/** Calculate the MAC (message authentication code) of a message.
1167 *
1168 * \note To verify the MAC of a message against an
1169 * expected value, use psa_mac_verify() instead.
1170 * Beware that comparing integrity or authenticity data such as
1171 * MAC values with a function such as \c memcmp is risky
1172 * because the time taken by the comparison may leak information
1173 * about the MAC value which could allow an attacker to guess
1174 * a valid MAC and thereby bypass security controls.
1175 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001176 * \param key Identifier of the key to use for the operation. It
1177 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001178 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001179 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001180 * \param[in] input Buffer containing the input message.
1181 * \param input_length Size of the \p input buffer in bytes.
1182 * \param[out] mac Buffer where the MAC value is to be written.
1183 * \param mac_size Size of the \p mac buffer in bytes.
1184 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001185 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 *
1187 * \retval #PSA_SUCCESS
1188 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001189 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1190 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001191 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001192 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 * \retval #PSA_ERROR_NOT_SUPPORTED
1194 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001195 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1196 * \p mac_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +01001197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1199 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1200 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001201 * \retval #PSA_ERROR_STORAGE_FAILURE
1202 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_BAD_STATE
1204 * The library has not been previously initialized by psa_crypto_init().
1205 * It is implementation-dependent whether a failure to initialize
1206 * results in this error code.
1207 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001208psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 psa_algorithm_t alg,
1210 const uint8_t *input,
1211 size_t input_length,
1212 uint8_t *mac,
1213 size_t mac_size,
1214 size_t *mac_length);
1215
1216/** Calculate the MAC of a message and compare it with a reference value.
1217 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001218 * \param key Identifier of the key to use for the operation. It
1219 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001220 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001221 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001222 * \param[in] input Buffer containing the input message.
1223 * \param input_length Size of the \p input buffer in bytes.
1224 * \param[out] mac Buffer containing the expected MAC value.
1225 * \param mac_length Size of the \p mac buffer in bytes.
1226 *
1227 * \retval #PSA_SUCCESS
1228 * The expected MAC is identical to the actual MAC of the input.
1229 * \retval #PSA_ERROR_INVALID_SIGNATURE
1230 * The MAC of the message was calculated successfully, but it
1231 * differs from the expected value.
Gilles Peskineed733552023-02-14 19:21:09 +01001232 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1233 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001234 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001235 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001236 * \retval #PSA_ERROR_NOT_SUPPORTED
1237 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001238 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1239 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1240 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1241 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001242 * \retval #PSA_ERROR_STORAGE_FAILURE
1243 * The key could not be retrieved from storage.
1244 * \retval #PSA_ERROR_BAD_STATE
1245 * The library has not been previously initialized by psa_crypto_init().
1246 * It is implementation-dependent whether a failure to initialize
1247 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001248 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001249psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001250 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001251 const uint8_t *input,
1252 size_t input_length,
1253 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001254 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001255
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256/** The type of the state data structure for multipart MAC operations.
1257 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001258 * Before calling any function on a MAC operation object, the application must
1259 * initialize it by any of the following means:
1260 * - Set the structure to all-bits-zero, for example:
1261 * \code
1262 * psa_mac_operation_t operation;
1263 * memset(&operation, 0, sizeof(operation));
1264 * \endcode
1265 * - Initialize the structure to logical zero values, for example:
1266 * \code
1267 * psa_mac_operation_t operation = {0};
1268 * \endcode
1269 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1270 * for example:
1271 * \code
1272 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1273 * \endcode
1274 * - Assign the result of the function psa_mac_operation_init()
1275 * to the structure, for example:
1276 * \code
1277 * psa_mac_operation_t operation;
1278 * operation = psa_mac_operation_init();
1279 * \endcode
1280 *
Janos Follath2ba60792021-04-28 09:37:34 +01001281 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001282 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001283 * make any assumptions about the content of this structure.
1284 * Implementation details can change in future versions without notice. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001285typedef struct psa_mac_operation_s psa_mac_operation_t;
1286
Jaeden Amero769ce272019-01-04 11:48:03 +00001287/** \def PSA_MAC_OPERATION_INIT
1288 *
1289 * This macro returns a suitable initializer for a MAC operation object of type
1290 * #psa_mac_operation_t.
1291 */
Jaeden Amero769ce272019-01-04 11:48:03 +00001292
1293/** Return an initial value for a MAC operation object.
1294 */
1295static psa_mac_operation_t psa_mac_operation_init(void);
1296
Gilles Peskinef45adda2019-01-14 18:29:18 +01001297/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001298 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001299 * This function sets up the calculation of the MAC
1300 * (message authentication code) of a byte string.
1301 * To verify the MAC of a message against an
1302 * expected value, use psa_mac_verify_setup() instead.
1303 *
1304 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001305 * -# Allocate an operation object which will be passed to all the functions
1306 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001307 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001308 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001310 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1311 * of the message each time. The MAC that is calculated is the MAC
1312 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001313 * -# At the end of the message, call psa_mac_sign_finish() to finish
1314 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001316 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1317 * operation will need to be reset by a call to psa_mac_abort(). The
1318 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001319 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001320 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 * After a successful call to psa_mac_sign_setup(), the application must
1322 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001323 * - A successful call to psa_mac_sign_finish().
1324 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001326 * \param[in,out] operation The operation object to set up. It must have
1327 * been initialized as per the documentation for
1328 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001329 * \param key Identifier of the key to use for the operation. It
1330 * must remain valid until the operation terminates.
1331 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001332 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001333 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 *
Gilles Peskine28538492018-07-11 17:34:00 +02001335 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001336 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001337 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1338 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001339 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001340 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001341 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001342 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001343 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1345 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1346 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001347 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001348 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001349 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001350 * The operation state is not valid (it must be inactive), or
1351 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001352 * It is implementation-dependent whether a failure to initialize
1353 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001354 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001356 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357 psa_algorithm_t alg);
1358
Gilles Peskinef45adda2019-01-14 18:29:18 +01001359/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001360 *
1361 * This function sets up the verification of the MAC
1362 * (message authentication code) of a byte string against an expected value.
1363 *
1364 * The sequence of operations to verify a MAC is as follows:
1365 * -# Allocate an operation object which will be passed to all the functions
1366 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001367 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001368 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001370 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1371 * of the message each time. The MAC that is calculated is the MAC
1372 * of the concatenation of these messages in order.
1373 * -# At the end of the message, call psa_mac_verify_finish() to finish
1374 * calculating the actual MAC of the message and verify it against
1375 * the expected value.
1376 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001377 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1378 * operation will need to be reset by a call to psa_mac_abort(). The
1379 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001380 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 *
1382 * After a successful call to psa_mac_verify_setup(), the application must
1383 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001384 * - A successful call to psa_mac_verify_finish().
1385 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001387 * \param[in,out] operation The operation object to set up. It must have
1388 * been initialized as per the documentation for
1389 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001390 * \param key Identifier of the key to use for the operation. It
1391 * must remain valid until the operation terminates.
1392 * It must allow the usage
1393 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001394 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1395 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 *
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001398 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001399 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1400 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001403 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1407 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1408 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001409 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001410 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001411 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001412 * The operation state is not valid (it must be inactive), or
1413 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001414 * It is implementation-dependent whether a failure to initialize
1415 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 */
1417psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001418 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001420
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421/** Add a message fragment to a multipart MAC operation.
1422 *
1423 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1424 * before calling this function.
1425 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001426 * If this function returns an error status, the operation enters an error
1427 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001428 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001429 * \param[in,out] operation Active MAC operation.
1430 * \param[in] input Buffer containing the message fragment to add to
1431 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001432 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433 *
1434 * \retval #PSA_SUCCESS
1435 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1438 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1439 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1440 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001441 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001442 * The operation state is not valid (it must be active), or
1443 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001444 * It is implementation-dependent whether a failure to initialize
1445 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001447psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1448 const uint8_t *input,
1449 size_t input_length);
1450
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451/** Finish the calculation of the MAC of a message.
1452 *
1453 * The application must call psa_mac_sign_setup() before calling this function.
1454 * This function calculates the MAC of the message formed by concatenating
1455 * the inputs passed to preceding calls to psa_mac_update().
1456 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001457 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001458 * If this function returns an error status, the operation enters an error
1459 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
1461 * \warning Applications should not call this function if they expect
1462 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1463 * Beware that comparing integrity or authenticity data such as
1464 * MAC values with a function such as \c memcmp is risky
1465 * because the time taken by the comparison may leak information
1466 * about the MAC value which could allow an attacker to guess
1467 * a valid MAC and thereby bypass security controls.
1468 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001469 * \param[in,out] operation Active MAC operation.
1470 * \param[out] mac Buffer where the MAC value is to be written.
1471 * \param mac_size Size of the \p mac buffer in bytes.
1472 * \param[out] mac_length On success, the number of bytes
1473 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001474 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001475 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001476 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 *
1479 * \retval #PSA_SUCCESS
1480 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001482 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001483 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskineed733552023-02-14 19:21:09 +01001484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1486 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1487 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1488 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001489 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001490 * The operation state is not valid (it must be an active mac sign
1491 * operation), or the library has not been previously initialized
1492 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001493 * It is implementation-dependent whether a failure to initialize
1494 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001495 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001496psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1497 uint8_t *mac,
1498 size_t mac_size,
1499 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501/** Finish the calculation of the MAC of a message and compare it with
1502 * an expected value.
1503 *
1504 * The application must call psa_mac_verify_setup() before calling this function.
1505 * This function calculates the MAC of the message formed by concatenating
1506 * the inputs passed to preceding calls to psa_mac_update(). It then
1507 * compares the calculated MAC with the expected MAC passed as a
1508 * parameter to this function.
1509 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001510 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001511 * If this function returns an error status, the operation enters an error
1512 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513 *
1514 * \note Implementations shall make the best effort to ensure that the
1515 * comparison between the actual MAC and the expected MAC is performed
1516 * in constant time.
1517 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001518 * \param[in,out] operation Active MAC operation.
1519 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001520 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 *
1522 * \retval #PSA_SUCCESS
1523 * The expected MAC is identical to the actual MAC of the message.
1524 * \retval #PSA_ERROR_INVALID_SIGNATURE
1525 * The MAC of the message was calculated successfully, but it
1526 * differs from the expected MAC.
Gilles Peskineed733552023-02-14 19:21:09 +01001527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1528 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1529 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1530 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1531 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001532 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001533 * The operation state is not valid (it must be an active mac verify
1534 * operation), or the library has not been previously initialized
1535 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001536 * It is implementation-dependent whether a failure to initialize
1537 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001539psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1540 const uint8_t *mac,
1541 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543/** Abort a MAC operation.
1544 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001546 * \p operation structure itself. Once aborted, the operation object
1547 * can be reused for another operation by calling
1548 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001550 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001551 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001553 * In particular, calling psa_mac_abort() after the operation has been
1554 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1555 * psa_mac_verify_finish() is safe and has no effect.
1556 *
1557 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 *
Gilles Peskineed733552023-02-14 19:21:09 +01001559 * \retval #PSA_SUCCESS \emptydescription
1560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1561 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1562 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001563 * \retval #PSA_ERROR_BAD_STATE
1564 * The library has not been previously initialized by psa_crypto_init().
1565 * It is implementation-dependent whether a failure to initialize
1566 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1569
1570/**@}*/
1571
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001572/** \defgroup cipher Symmetric ciphers
1573 * @{
1574 */
1575
Gilles Peskine69647a42019-01-14 20:18:12 +01001576/** Encrypt a message using a symmetric cipher.
1577 *
1578 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001579 * vector). Use the multipart operation interface with a
1580 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001581 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001582 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001583 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001584 * \param alg The cipher algorithm to compute
1585 * (\c PSA_ALG_XXX value such that
1586 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1587 * \param[in] input Buffer containing the message to encrypt.
1588 * \param input_length Size of the \p input buffer in bytes.
1589 * \param[out] output Buffer where the output is to be written.
1590 * The output contains the IV followed by
1591 * the ciphertext proper.
1592 * \param output_size Size of the \p output buffer in bytes.
1593 * \param[out] output_length On success, the number of bytes
1594 * that make up the output.
1595 *
1596 * \retval #PSA_SUCCESS
1597 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001598 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1599 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001600 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001601 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001602 * \retval #PSA_ERROR_NOT_SUPPORTED
1603 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001604 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1607 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1608 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1609 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001610 * \retval #PSA_ERROR_BAD_STATE
1611 * The library has not been previously initialized by psa_crypto_init().
1612 * It is implementation-dependent whether a failure to initialize
1613 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001614 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001615psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001616 psa_algorithm_t alg,
1617 const uint8_t *input,
1618 size_t input_length,
1619 uint8_t *output,
1620 size_t output_size,
1621 size_t *output_length);
1622
1623/** Decrypt a message using a symmetric cipher.
1624 *
1625 * This function decrypts a message encrypted with a symmetric cipher.
1626 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001627 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001628 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001629 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001630 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001631 * \param alg The cipher algorithm to compute
1632 * (\c PSA_ALG_XXX value such that
1633 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1634 * \param[in] input Buffer containing the message to decrypt.
1635 * This consists of the IV followed by the
1636 * ciphertext proper.
1637 * \param input_length Size of the \p input buffer in bytes.
1638 * \param[out] output Buffer where the plaintext is to be written.
1639 * \param output_size Size of the \p output buffer in bytes.
1640 * \param[out] output_length On success, the number of bytes
1641 * that make up the output.
1642 *
1643 * \retval #PSA_SUCCESS
1644 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001645 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1646 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001647 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001648 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001649 * \retval #PSA_ERROR_NOT_SUPPORTED
1650 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001651 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1652 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1654 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1655 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1656 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001657 * \retval #PSA_ERROR_BAD_STATE
1658 * The library has not been previously initialized by psa_crypto_init().
1659 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001660 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001661 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001662psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001663 psa_algorithm_t alg,
1664 const uint8_t *input,
1665 size_t input_length,
1666 uint8_t *output,
1667 size_t output_size,
1668 size_t *output_length);
1669
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001670/** The type of the state data structure for multipart cipher operations.
1671 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001672 * Before calling any function on a cipher operation object, the application
1673 * must initialize it by any of the following means:
1674 * - Set the structure to all-bits-zero, for example:
1675 * \code
1676 * psa_cipher_operation_t operation;
1677 * memset(&operation, 0, sizeof(operation));
1678 * \endcode
1679 * - Initialize the structure to logical zero values, for example:
1680 * \code
1681 * psa_cipher_operation_t operation = {0};
1682 * \endcode
1683 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1684 * for example:
1685 * \code
1686 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1687 * \endcode
1688 * - Assign the result of the function psa_cipher_operation_init()
1689 * to the structure, for example:
1690 * \code
1691 * psa_cipher_operation_t operation;
1692 * operation = psa_cipher_operation_init();
1693 * \endcode
1694 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001695 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001696 * make any assumptions about the content of this structure.
1697 * Implementation details can change in future versions without notice. */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001698typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1699
Jaeden Amero5bae2272019-01-04 11:48:27 +00001700/** \def PSA_CIPHER_OPERATION_INIT
1701 *
1702 * This macro returns a suitable initializer for a cipher operation object of
1703 * type #psa_cipher_operation_t.
1704 */
Jaeden Amero5bae2272019-01-04 11:48:27 +00001705
1706/** Return an initial value for a cipher operation object.
1707 */
1708static psa_cipher_operation_t psa_cipher_operation_init(void);
1709
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001710/** Set the key for a multipart symmetric encryption operation.
1711 *
1712 * The sequence of operations to encrypt a message with a symmetric cipher
1713 * is as follows:
1714 * -# Allocate an operation object which will be passed to all the functions
1715 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001716 * -# Initialize the operation object with one of the methods described in the
1717 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001718 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001719 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001720 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001721 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001722 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001723 * requires a specific IV value.
1724 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1725 * of the message each time.
1726 * -# Call psa_cipher_finish().
1727 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001728 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1729 * the operation will need to be reset by a call to psa_cipher_abort(). The
1730 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001731 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001733 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001734 * eventually terminate the operation. The following events terminate an
1735 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001736 * - A successful call to psa_cipher_finish().
1737 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739 * \param[in,out] operation The operation object to set up. It must have
1740 * been initialized as per the documentation for
1741 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001742 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001743 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001744 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001745 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001746 * \param alg The cipher algorithm to compute
1747 * (\c PSA_ALG_XXX value such that
1748 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 *
Gilles Peskine28538492018-07-11 17:34:00 +02001750 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001751 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001752 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1753 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001754 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001755 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001756 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001757 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1760 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1761 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1762 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001763 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001764 * The operation state is not valid (it must be inactive), or
1765 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001766 * It is implementation-dependent whether a failure to initialize
1767 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001769psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001770 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772
1773/** Set the key for a multipart symmetric decryption operation.
1774 *
1775 * The sequence of operations to decrypt a message with a symmetric cipher
1776 * is as follows:
1777 * -# Allocate an operation object which will be passed to all the functions
1778 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001779 * -# Initialize the operation object with one of the methods described in the
1780 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001781 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001782 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001783 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 * decryption. If the IV is prepended to the ciphertext, you can call
1785 * psa_cipher_update() on a buffer containing the IV followed by the
1786 * beginning of the message.
1787 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1788 * of the message each time.
1789 * -# Call psa_cipher_finish().
1790 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001791 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1792 * the operation will need to be reset by a call to psa_cipher_abort(). The
1793 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001794 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001796 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001797 * eventually terminate the operation. The following events terminate an
1798 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001799 * - A successful call to psa_cipher_finish().
1800 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * \param[in,out] operation The operation object to set up. It must have
1803 * been initialized as per the documentation for
1804 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001805 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001806 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001807 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001808 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001809 * \param alg The cipher algorithm to compute
1810 * (\c PSA_ALG_XXX value such that
1811 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812 *
Gilles Peskine28538492018-07-11 17:34:00 +02001813 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001815 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1816 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001818 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001819 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001820 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1823 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1824 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1825 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001826 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001827 * The operation state is not valid (it must be inactive), or
1828 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001829 * It is implementation-dependent whether a failure to initialize
1830 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001831 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001832psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001833 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001834 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836/** Generate an IV for a symmetric encryption operation.
1837 *
1838 * This function generates a random IV (initialization vector), nonce
1839 * or initial counter value for the encryption operation as appropriate
1840 * for the chosen algorithm, key type and key size.
1841 *
1842 * The application must call psa_cipher_encrypt_setup() before
1843 * calling this function.
1844 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001845 * If this function returns an error status, the operation enters an error
1846 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001847 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001848 * \param[in,out] operation Active cipher operation.
1849 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001850 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * \param[out] iv_length On success, the number of bytes of the
1852 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001853 *
1854 * \retval #PSA_SUCCESS
1855 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001856 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001857 * The size of the \p iv buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1860 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1861 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1862 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001863 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001864 * The operation state is not valid (it must be active, with no IV set),
1865 * or the library has not been previously initialized
1866 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001867 * It is implementation-dependent whether a failure to initialize
1868 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001869 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001870psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001871 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001872 size_t iv_size,
1873 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001874
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875/** Set the IV for a symmetric encryption or decryption operation.
1876 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001877 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878 * or initial counter value for the encryption or decryption operation.
1879 *
1880 * The application must call psa_cipher_encrypt_setup() before
1881 * calling this function.
1882 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001883 * If this function returns an error status, the operation enters an error
1884 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 *
1886 * \note When encrypting, applications should use psa_cipher_generate_iv()
1887 * instead of this function, unless implementing a protocol that requires
1888 * a non-random IV.
1889 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[in,out] operation Active cipher operation.
1891 * \param[in] iv Buffer containing the IV to use.
1892 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 *
1894 * \retval #PSA_SUCCESS
1895 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001897 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 * or the chosen algorithm does not use an IV.
Gilles Peskineed733552023-02-14 19:21:09 +01001899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1900 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1901 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1902 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1903 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001904 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001905 * The operation state is not valid (it must be an active cipher
1906 * encrypt operation, with no IV set), or the library has not been
1907 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001908 * It is implementation-dependent whether a failure to initialize
1909 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001911psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001912 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001913 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915/** Encrypt or decrypt a message fragment in an active cipher operation.
1916 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001917 * Before calling this function, you must:
1918 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1919 * The choice of setup function determines whether this function
1920 * encrypts or decrypts its input.
1921 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1922 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001924 * If this function returns an error status, the operation enters an error
1925 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[in,out] operation Active cipher operation.
1928 * \param[in] input Buffer containing the message fragment to
1929 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001930 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001932 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[out] output_length On success, the number of bytes
1934 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 *
1936 * \retval #PSA_SUCCESS
1937 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001938 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1939 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001940 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1941 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1942 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1943 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1944 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001945 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001946 * The operation state is not valid (it must be active, with an IV set
1947 * if required for the algorithm), or the library has not been
1948 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001949 * It is implementation-dependent whether a failure to initialize
1950 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001952psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1953 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001954 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001955 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001956 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001957 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001958
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959/** Finish encrypting or decrypting a message in a cipher operation.
1960 *
1961 * The application must call psa_cipher_encrypt_setup() or
1962 * psa_cipher_decrypt_setup() before calling this function. The choice
1963 * of setup function determines whether this function encrypts or
1964 * decrypts its input.
1965 *
1966 * This function finishes the encryption or decryption of the message
1967 * formed by concatenating the inputs passed to preceding calls to
1968 * psa_cipher_update().
1969 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001970 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001971 * If this function returns an error status, the operation enters an error
1972 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001973 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001974 * \param[in,out] operation Active cipher operation.
1975 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001976 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[out] output_length On success, the number of bytes
1978 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 *
1980 * \retval #PSA_SUCCESS
1981 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001982 * \retval #PSA_ERROR_INVALID_ARGUMENT
1983 * The total input size passed to this operation is not valid for
1984 * this particular algorithm. For example, the algorithm is a based
1985 * on block cipher and requires a whole number of blocks, but the
1986 * total input size is not a multiple of the block size.
1987 * \retval #PSA_ERROR_INVALID_PADDING
1988 * This is a decryption operation for an algorithm that includes
1989 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001990 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1991 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001992 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1994 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1995 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1996 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001997 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001998 * The operation state is not valid (it must be active, with an IV set
1999 * if required for the algorithm), or the library has not been
2000 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002001 * It is implementation-dependent whether a failure to initialize
2002 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002004psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002005 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002006 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002007 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002008
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009/** Abort a cipher operation.
2010 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002011 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002012 * \p operation structure itself. Once aborted, the operation object
2013 * can be reused for another operation by calling
2014 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002016 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002017 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002019 * In particular, calling psa_cipher_abort() after the operation has been
2020 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2021 * is safe and has no effect.
2022 *
2023 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002024 *
Gilles Peskineed733552023-02-14 19:21:09 +01002025 * \retval #PSA_SUCCESS \emptydescription
2026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2027 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2028 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002029 * \retval #PSA_ERROR_BAD_STATE
2030 * The library has not been previously initialized by psa_crypto_init().
2031 * It is implementation-dependent whether a failure to initialize
2032 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002033 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002034psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2035
2036/**@}*/
2037
Gilles Peskine3b555712018-03-03 21:27:57 +01002038/** \defgroup aead Authenticated encryption with associated data (AEAD)
2039 * @{
2040 */
2041
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002042/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002043 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002044 * \param key Identifier of the key to use for the
2045 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002046 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002047 * \param alg The AEAD algorithm to compute
2048 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002049 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002050 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002052 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002053 * but not encrypted.
2054 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056 * encrypted.
2057 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002058 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002059 * encrypted data. The additional data is not
2060 * part of this output. For algorithms where the
2061 * encrypted data and the authentication tag
2062 * are defined as separate outputs, the
2063 * authentication tag is appended to the
2064 * encrypted data.
2065 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002066 * This must be appropriate for the selected
2067 * algorithm and key:
2068 * - A sufficient output size is
2069 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2070 * \p alg, \p plaintext_length) where
2071 * \c key_type is the type of \p key.
2072 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2073 * plaintext_length) evaluates to the maximum
2074 * ciphertext size of any supported AEAD
2075 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002077 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 *
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002080 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002081 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2082 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002084 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002085 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002086 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002088 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002089 * \p ciphertext_size is too small.
2090 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2091 * \p plaintext_length) or
2092 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2093 * determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2095 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2096 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2097 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002098 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002099 * The library has not been previously initialized by psa_crypto_init().
2100 * It is implementation-dependent whether a failure to initialize
2101 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002102 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002103psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002104 psa_algorithm_t alg,
2105 const uint8_t *nonce,
2106 size_t nonce_length,
2107 const uint8_t *additional_data,
2108 size_t additional_data_length,
2109 const uint8_t *plaintext,
2110 size_t plaintext_length,
2111 uint8_t *ciphertext,
2112 size_t ciphertext_size,
2113 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002114
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002115/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002116 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002117 * \param key Identifier of the key to use for the
2118 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002119 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002120 * \param alg The AEAD algorithm to compute
2121 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002122 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002123 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * but not encrypted.
2127 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * encrypted. For algorithms where the
2130 * encrypted data and the authentication tag
2131 * are defined as separate inputs, the buffer
2132 * must contain the encrypted data followed
2133 * by the authentication tag.
2134 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002135 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002136 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002137 * This must be appropriate for the selected
2138 * algorithm and key:
2139 * - A sufficient output size is
2140 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2141 * \p alg, \p ciphertext_length) where
2142 * \c key_type is the type of \p key.
2143 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2144 * ciphertext_length) evaluates to the maximum
2145 * plaintext size of any supported AEAD
2146 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002147 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002148 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002149 *
Gilles Peskine28538492018-07-11 17:34:00 +02002150 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002151 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002152 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * The ciphertext is not authentic.
Gilles Peskineed733552023-02-14 19:21:09 +01002155 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002156 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002157 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002158 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002159 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002161 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002162 * \p plaintext_size is too small.
2163 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2164 * \p ciphertext_length) or
2165 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2166 * to determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2168 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2169 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2170 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002171 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002172 * The library has not been previously initialized by psa_crypto_init().
2173 * It is implementation-dependent whether a failure to initialize
2174 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002175 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002176psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002177 psa_algorithm_t alg,
2178 const uint8_t *nonce,
2179 size_t nonce_length,
2180 const uint8_t *additional_data,
2181 size_t additional_data_length,
2182 const uint8_t *ciphertext,
2183 size_t ciphertext_length,
2184 uint8_t *plaintext,
2185 size_t plaintext_size,
2186 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002187
Gilles Peskine30a9e412019-01-14 18:36:12 +01002188/** The type of the state data structure for multipart AEAD operations.
2189 *
2190 * Before calling any function on an AEAD operation object, the application
2191 * must initialize it by any of the following means:
2192 * - Set the structure to all-bits-zero, for example:
2193 * \code
2194 * psa_aead_operation_t operation;
2195 * memset(&operation, 0, sizeof(operation));
2196 * \endcode
2197 * - Initialize the structure to logical zero values, for example:
2198 * \code
2199 * psa_aead_operation_t operation = {0};
2200 * \endcode
2201 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2202 * for example:
2203 * \code
2204 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2205 * \endcode
2206 * - Assign the result of the function psa_aead_operation_init()
2207 * to the structure, for example:
2208 * \code
2209 * psa_aead_operation_t operation;
2210 * operation = psa_aead_operation_init();
2211 * \endcode
2212 *
2213 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01002214 * make any assumptions about the content of this structure.
2215 * Implementation details can change in future versions without notice. */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002216typedef struct psa_aead_operation_s psa_aead_operation_t;
2217
2218/** \def PSA_AEAD_OPERATION_INIT
2219 *
2220 * This macro returns a suitable initializer for an AEAD operation object of
2221 * type #psa_aead_operation_t.
2222 */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002223
2224/** Return an initial value for an AEAD operation object.
2225 */
2226static psa_aead_operation_t psa_aead_operation_init(void);
2227
2228/** Set the key for a multipart authenticated encryption operation.
2229 *
2230 * The sequence of operations to encrypt a message with authentication
2231 * is as follows:
2232 * -# Allocate an operation object which will be passed to all the functions
2233 * listed here.
2234 * -# Initialize the operation object with one of the methods described in the
2235 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002236 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002237 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002238 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2239 * inputs to the subsequent calls to psa_aead_update_ad() and
2240 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2241 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002242 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2243 * generate or set the nonce. You should use
2244 * psa_aead_generate_nonce() unless the protocol you are implementing
2245 * requires a specific nonce value.
2246 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2247 * of the non-encrypted additional authenticated data each time.
2248 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002249 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002250 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002251 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002252 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2253 * the operation will need to be reset by a call to psa_aead_abort(). The
2254 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 * has been initialized.
2256 *
2257 * After a successful call to psa_aead_encrypt_setup(), the application must
2258 * eventually terminate the operation. The following events terminate an
2259 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002260 * - A successful call to psa_aead_finish().
2261 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002262 *
2263 * \param[in,out] operation The operation object to set up. It must have
2264 * been initialized as per the documentation for
2265 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002266 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002267 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002268 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002269 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 * \param alg The AEAD algorithm to compute
2271 * (\c PSA_ALG_XXX value such that
2272 * #PSA_ALG_IS_AEAD(\p alg) is true).
2273 *
2274 * \retval #PSA_SUCCESS
2275 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002276 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002277 * The operation state is not valid (it must be inactive), or
2278 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskineed733552023-02-14 19:21:09 +01002279 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2280 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002282 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002283 * \retval #PSA_ERROR_NOT_SUPPORTED
2284 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002285 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2287 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2288 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002289 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002290 * The library has not been previously initialized by psa_crypto_init().
2291 * It is implementation-dependent whether a failure to initialize
2292 * results in this error code.
2293 */
2294psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002295 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002296 psa_algorithm_t alg);
2297
2298/** Set the key for a multipart authenticated decryption operation.
2299 *
2300 * The sequence of operations to decrypt a message with authentication
2301 * is as follows:
2302 * -# Allocate an operation object which will be passed to all the functions
2303 * listed here.
2304 * -# Initialize the operation object with one of the methods described in the
2305 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002306 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002307 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002308 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2309 * inputs to the subsequent calls to psa_aead_update_ad() and
2310 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2311 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2313 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2314 * of the non-encrypted additional authenticated data each time.
2315 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002316 * of the ciphertext to decrypt each time.
2317 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002319 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2320 * the operation will need to be reset by a call to psa_aead_abort(). The
2321 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002322 * has been initialized.
2323 *
2324 * After a successful call to psa_aead_decrypt_setup(), the application must
2325 * eventually terminate the operation. The following events terminate an
2326 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002327 * - A successful call to psa_aead_verify().
2328 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 *
2330 * \param[in,out] operation The operation object to set up. It must have
2331 * been initialized as per the documentation for
2332 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002333 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002335 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002336 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 * \param alg The AEAD algorithm to compute
2338 * (\c PSA_ALG_XXX value such that
2339 * #PSA_ALG_IS_AEAD(\p alg) is true).
2340 *
2341 * \retval #PSA_SUCCESS
2342 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002343 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2344 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002346 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002347 * \retval #PSA_ERROR_NOT_SUPPORTED
2348 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2351 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2352 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2353 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002354 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002355 * The operation state is not valid (it must be inactive), or the
2356 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002357 * It is implementation-dependent whether a failure to initialize
2358 * results in this error code.
2359 */
2360psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002361 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002362 psa_algorithm_t alg);
2363
2364/** Generate a random nonce for an authenticated encryption operation.
2365 *
2366 * This function generates a random nonce for the authenticated encryption
2367 * operation with an appropriate size for the chosen algorithm, key type
2368 * and key size.
2369 *
2370 * The application must call psa_aead_encrypt_setup() before
2371 * calling this function.
2372 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002373 * If this function returns an error status, the operation enters an error
2374 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002375 *
2376 * \param[in,out] operation Active AEAD operation.
2377 * \param[out] nonce Buffer where the generated nonce is to be
2378 * written.
2379 * \param nonce_size Size of the \p nonce buffer in bytes.
2380 * \param[out] nonce_length On success, the number of bytes of the
2381 * generated nonce.
2382 *
2383 * \retval #PSA_SUCCESS
2384 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2386 * The size of the \p nonce buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01002387 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2389 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2390 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2391 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002392 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002393 * The operation state is not valid (it must be an active aead encrypt
2394 * operation, with no nonce set), or the library has not been
2395 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002396 * It is implementation-dependent whether a failure to initialize
2397 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 */
2399psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002400 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 size_t nonce_size,
2402 size_t *nonce_length);
2403
2404/** Set the nonce for an authenticated encryption or decryption operation.
2405 *
2406 * This function sets the nonce for the authenticated
2407 * encryption or decryption operation.
2408 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002409 * The application must call psa_aead_encrypt_setup() or
2410 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002411 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002412 * If this function returns an error status, the operation enters an error
2413 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002415 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002416 * instead of this function, unless implementing a protocol that requires
2417 * a non-random IV.
2418 *
2419 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002420 * \param[in] nonce Buffer containing the nonce to use.
2421 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 *
2423 * \retval #PSA_SUCCESS
2424 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 * \retval #PSA_ERROR_INVALID_ARGUMENT
2426 * The size of \p nonce is not acceptable for the chosen algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002427 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2428 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2429 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2430 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2431 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002432 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002433 * The operation state is not valid (it must be active, with no nonce
2434 * set), or the library has not been previously initialized
2435 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002436 * It is implementation-dependent whether a failure to initialize
2437 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002438 */
2439psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002440 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002441 size_t nonce_length);
2442
Gilles Peskinebc59c852019-01-17 15:26:08 +01002443/** Declare the lengths of the message and additional data for AEAD.
2444 *
2445 * The application must call this function before calling
2446 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2447 * the operation requires it. If the algorithm does not require it,
2448 * calling this function is optional, but if this function is called
2449 * then the implementation must enforce the lengths.
2450 *
2451 * You may call this function before or after setting the nonce with
2452 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2453 *
2454 * - For #PSA_ALG_CCM, calling this function is required.
2455 * - For the other AEAD algorithms defined in this specification, calling
2456 * this function is not required.
2457 * - For vendor-defined algorithm, refer to the vendor documentation.
2458 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002459 * If this function returns an error status, the operation enters an error
2460 * state and must be aborted by calling psa_aead_abort().
2461 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002462 * \param[in,out] operation Active AEAD operation.
2463 * \param ad_length Size of the non-encrypted additional
2464 * authenticated data in bytes.
2465 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2466 *
2467 * \retval #PSA_SUCCESS
2468 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002469 * \retval #PSA_ERROR_INVALID_ARGUMENT
2470 * At least one of the lengths is not acceptable for the chosen
2471 * algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002472 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2474 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2475 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002476 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002477 * The operation state is not valid (it must be active, and
2478 * psa_aead_update_ad() and psa_aead_update() must not have been
2479 * called yet), or the library has not been previously initialized
2480 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002481 * It is implementation-dependent whether a failure to initialize
2482 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002483 */
2484psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2485 size_t ad_length,
2486 size_t plaintext_length);
2487
Gilles Peskine30a9e412019-01-14 18:36:12 +01002488/** Pass additional data to an active AEAD operation.
2489 *
2490 * Additional data is authenticated, but not encrypted.
2491 *
2492 * You may call this function multiple times to pass successive fragments
2493 * of the additional data. You may not call this function after passing
2494 * data to encrypt or decrypt with psa_aead_update().
2495 *
2496 * Before calling this function, you must:
2497 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2498 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2499 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002500 * If this function returns an error status, the operation enters an error
2501 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002502 *
2503 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2504 * there is no guarantee that the input is valid. Therefore, until
2505 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2506 * treat the input as untrusted and prepare to undo any action that
2507 * depends on the input if psa_aead_verify() returns an error status.
2508 *
2509 * \param[in,out] operation Active AEAD operation.
2510 * \param[in] input Buffer containing the fragment of
2511 * additional data.
2512 * \param input_length Size of the \p input buffer in bytes.
2513 *
2514 * \retval #PSA_SUCCESS
2515 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002516 * \retval #PSA_ERROR_INVALID_ARGUMENT
2517 * The total input length overflows the additional data length that
2518 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2520 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2521 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2522 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2523 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002524 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002525 * The operation state is not valid (it must be active, have a nonce
2526 * set, have lengths set if required by the algorithm, and
2527 * psa_aead_update() must not have been called yet), or the library
2528 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002529 * It is implementation-dependent whether a failure to initialize
2530 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002531 */
2532psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2533 const uint8_t *input,
2534 size_t input_length);
2535
2536/** Encrypt or decrypt a message fragment in an active AEAD operation.
2537 *
2538 * Before calling this function, you must:
2539 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2540 * The choice of setup function determines whether this function
2541 * encrypts or decrypts its input.
2542 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2543 * 3. Call psa_aead_update_ad() to pass all the additional data.
2544 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002545 * If this function returns an error status, the operation enters an error
2546 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002547 *
2548 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2549 * there is no guarantee that the input is valid. Therefore, until
2550 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2551 * - Do not use the output in any way other than storing it in a
2552 * confidential location. If you take any action that depends
2553 * on the tentative decrypted data, this action will need to be
2554 * undone if the input turns out not to be valid. Furthermore,
2555 * if an adversary can observe that this action took place
2556 * (for example through timing), they may be able to use this
2557 * fact as an oracle to decrypt any message encrypted with the
2558 * same key.
2559 * - In particular, do not copy the output anywhere but to a
2560 * memory or storage space that you have exclusive access to.
2561 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002562 * This function does not require the input to be aligned to any
2563 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002564 * a whole block at a time, it must consume all the input provided, but
2565 * it may delay the end of the corresponding output until a subsequent
2566 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2567 * provides sufficient input. The amount of data that can be delayed
2568 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002569 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002570 * \param[in,out] operation Active AEAD operation.
2571 * \param[in] input Buffer containing the message fragment to
2572 * encrypt or decrypt.
2573 * \param input_length Size of the \p input buffer in bytes.
2574 * \param[out] output Buffer where the output is to be written.
2575 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002576 * This must be appropriate for the selected
2577 * algorithm and key:
2578 * - A sufficient output size is
2579 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2580 * \c alg, \p input_length) where
2581 * \c key_type is the type of key and \c alg is
2582 * the algorithm that were used to set up the
2583 * operation.
2584 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2585 * input_length) evaluates to the maximum
2586 * output size of any supported AEAD
2587 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002588 * \param[out] output_length On success, the number of bytes
2589 * that make up the returned output.
2590 *
2591 * \retval #PSA_SUCCESS
2592 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002593 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2594 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002595 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2596 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2597 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002598 * \retval #PSA_ERROR_INVALID_ARGUMENT
2599 * The total length of input to psa_aead_update_ad() so far is
2600 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002601 * specified with psa_aead_set_lengths(), or
2602 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002603 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002604 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2605 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2606 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2607 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2608 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002609 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002610 * The operation state is not valid (it must be active, have a nonce
2611 * set, and have lengths set if required by the algorithm), or the
2612 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002613 * It is implementation-dependent whether a failure to initialize
2614 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002615 */
2616psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2617 const uint8_t *input,
2618 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002619 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 size_t output_size,
2621 size_t *output_length);
2622
2623/** Finish encrypting a message in an AEAD operation.
2624 *
2625 * The operation must have been set up with psa_aead_encrypt_setup().
2626 *
2627 * This function finishes the authentication of the additional data
2628 * formed by concatenating the inputs passed to preceding calls to
2629 * psa_aead_update_ad() with the plaintext formed by concatenating the
2630 * inputs passed to preceding calls to psa_aead_update().
2631 *
2632 * This function has two output buffers:
2633 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002634 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002635 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002636 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002637 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002638 * If this function returns an error status, the operation enters an error
2639 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002640 *
2641 * \param[in,out] operation Active AEAD operation.
2642 * \param[out] ciphertext Buffer where the last part of the ciphertext
2643 * is to be written.
2644 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002645 * This must be appropriate for the selected
2646 * algorithm and key:
2647 * - A sufficient output size is
2648 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2649 * \c alg) where \c key_type is the type of key
2650 * and \c alg is the algorithm that were used to
2651 * set up the operation.
2652 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2653 * the maximum output size of any supported AEAD
2654 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002655 * \param[out] ciphertext_length On success, the number of bytes of
2656 * returned ciphertext.
2657 * \param[out] tag Buffer where the authentication tag is
2658 * to be written.
2659 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002660 * This must be appropriate for the selected
2661 * algorithm and key:
2662 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2663 * key_type, \c key_bits, \c alg) where
2664 * \c key_type and \c key_bits are the type and
2665 * bit-size of the key, and \c alg is the
2666 * algorithm that were used in the call to
2667 * psa_aead_encrypt_setup().
2668 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2669 * maximum tag size of any supported AEAD
2670 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002671 * \param[out] tag_length On success, the number of bytes
2672 * that make up the returned tag.
2673 *
2674 * \retval #PSA_SUCCESS
2675 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002676 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002677 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002678 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2679 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2680 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2681 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2682 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002683 * \retval #PSA_ERROR_INVALID_ARGUMENT
2684 * The total length of input to psa_aead_update_ad() so far is
2685 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002686 * specified with psa_aead_set_lengths(), or
2687 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002688 * less than the plaintext length that was previously
2689 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2692 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2693 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2694 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002695 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002696 * The operation state is not valid (it must be an active encryption
2697 * operation with a nonce set), or the library has not been previously
2698 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002699 * It is implementation-dependent whether a failure to initialize
2700 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002701 */
2702psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002703 uint8_t *ciphertext,
2704 size_t ciphertext_size,
2705 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002706 uint8_t *tag,
2707 size_t tag_size,
2708 size_t *tag_length);
2709
2710/** Finish authenticating and decrypting a message in an AEAD operation.
2711 *
2712 * The operation must have been set up with psa_aead_decrypt_setup().
2713 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002714 * This function finishes the authenticated decryption of the message
2715 * components:
2716 *
2717 * - The additional data consisting of the concatenation of the inputs
2718 * passed to preceding calls to psa_aead_update_ad().
2719 * - The ciphertext consisting of the concatenation of the inputs passed to
2720 * preceding calls to psa_aead_update().
2721 * - The tag passed to this function call.
2722 *
2723 * If the authentication tag is correct, this function outputs any remaining
2724 * plaintext and reports success. If the authentication tag is not correct,
2725 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002726 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002727 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002728 * If this function returns an error status, the operation enters an error
2729 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002731 * \note Implementations shall make the best effort to ensure that the
2732 * comparison between the actual tag and the expected tag is performed
2733 * in constant time.
2734 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002735 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002736 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002737 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002738 * from previous calls to psa_aead_update()
2739 * that could not be processed until the end
2740 * of the input.
2741 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002742 * This must be appropriate for the selected algorithm and key:
2743 * - A sufficient output size is
2744 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2745 * \c alg) where \c key_type is the type of key
2746 * and \c alg is the algorithm that were used to
2747 * set up the operation.
2748 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2749 * the maximum output size of any supported AEAD
2750 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002751 * \param[out] plaintext_length On success, the number of bytes of
2752 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002753 * \param[in] tag Buffer containing the authentication tag.
2754 * \param tag_length Size of the \p tag buffer in bytes.
2755 *
2756 * \retval #PSA_SUCCESS
2757 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002758 * \retval #PSA_ERROR_INVALID_SIGNATURE
2759 * The calculations were successful, but the authentication tag is
2760 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002761 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2762 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002763 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2764 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2765 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002766 * \retval #PSA_ERROR_INVALID_ARGUMENT
2767 * The total length of input to psa_aead_update_ad() so far is
2768 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002769 * specified with psa_aead_set_lengths(), or
2770 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002771 * less than the plaintext length that was previously
2772 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002773 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2774 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2775 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2776 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2777 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002778 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002779 * The operation state is not valid (it must be an active decryption
2780 * operation with a nonce set), or the library has not been previously
2781 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002782 * It is implementation-dependent whether a failure to initialize
2783 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002784 */
2785psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002786 uint8_t *plaintext,
2787 size_t plaintext_size,
2788 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002789 const uint8_t *tag,
2790 size_t tag_length);
2791
2792/** Abort an AEAD operation.
2793 *
2794 * Aborting an operation frees all associated resources except for the
2795 * \p operation structure itself. Once aborted, the operation object
2796 * can be reused for another operation by calling
2797 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2798 *
2799 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002800 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002801 *
2802 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002803 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2804 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002805 *
2806 * \param[in,out] operation Initialized AEAD operation.
2807 *
Gilles Peskineed733552023-02-14 19:21:09 +01002808 * \retval #PSA_SUCCESS \emptydescription
2809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2810 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2811 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002812 * \retval #PSA_ERROR_BAD_STATE
2813 * The library has not been previously initialized by psa_crypto_init().
2814 * It is implementation-dependent whether a failure to initialize
2815 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002816 */
2817psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2818
Gilles Peskine3b555712018-03-03 21:27:57 +01002819/**@}*/
2820
Gilles Peskine20035e32018-02-03 22:44:14 +01002821/** \defgroup asymmetric Asymmetric cryptography
2822 * @{
2823 */
2824
2825/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002826 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2827 * this includes the hashing step.
2828 *
2829 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2830 * a multi-part hash operation and then pass the resulting hash to
2831 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2832 * hash algorithm to use.
2833 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002834 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835 * It must be an asymmetric key pair. The key must
2836 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002837 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2839 * is true), that is compatible with the type of
2840 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002841 * \param[in] input The input message to sign.
2842 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002844 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002845 * must be appropriate for the selected
2846 * algorithm and key:
2847 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002848 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2849 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850 * bit-size respectively of key.
2851 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2852 * maximum signature size of any supported
2853 * signature algorithm.
2854 * \param[out] signature_length On success, the number of bytes that make up
2855 * the returned signature value.
2856 *
Gilles Peskineed733552023-02-14 19:21:09 +01002857 * \retval #PSA_SUCCESS \emptydescription
2858 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002859 * \retval #PSA_ERROR_NOT_PERMITTED
2860 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2861 * or it does not permit the requested algorithm.
2862 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2863 * The size of the \p signature buffer is too small. You can
2864 * determine a sufficient buffer size by calling
2865 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2866 * where \c key_type and \c key_bits are the type and bit-size
2867 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002868 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2869 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2872 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2873 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2874 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2875 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2876 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2877 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002878 * \retval #PSA_ERROR_BAD_STATE
2879 * The library has not been previously initialized by psa_crypto_init().
2880 * It is implementation-dependent whether a failure to initialize
2881 * results in this error code.
2882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2884 psa_algorithm_t alg,
2885 const uint8_t *input,
2886 size_t input_length,
2887 uint8_t *signature,
2888 size_t signature_size,
2889 size_t *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890
2891/** \brief Verify the signature of a message with a public key, using
2892 * a hash-and-sign verification algorithm.
2893 *
2894 * \note To perform a multi-part hash-and-sign signature verification
2895 * algorithm, first use a multi-part hash operation to hash the message
2896 * and then pass the resulting hash to psa_verify_hash().
2897 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2898 * to use.
2899 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002900 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002901 * It must be a public key or an asymmetric key
2902 * pair. The key must allow the usage
2903 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002904 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2906 * is true), that is compatible with the type of
2907 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002908 * \param[in] input The message whose signature is to be verified.
2909 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002911 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912 *
Gilles Peskineed733552023-02-14 19:21:09 +01002913 * \retval #PSA_SUCCESS \emptydescription
2914 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915 * \retval #PSA_ERROR_NOT_PERMITTED
2916 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2917 * or it does not permit the requested algorithm.
2918 * \retval #PSA_ERROR_INVALID_SIGNATURE
2919 * The calculation was performed successfully, but the passed signature
2920 * is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01002921 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2922 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2925 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2926 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2927 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2928 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2929 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930 * \retval #PSA_ERROR_BAD_STATE
2931 * The library has not been previously initialized by psa_crypto_init().
2932 * It is implementation-dependent whether a failure to initialize
2933 * results in this error code.
2934 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2936 psa_algorithm_t alg,
2937 const uint8_t *input,
2938 size_t input_length,
2939 const uint8_t *signature,
2940 size_t signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002941
2942/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002943 * \brief Sign a hash or short message with a private key.
2944 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002945 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002946 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002947 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2948 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002949 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2950 * to determine the hash algorithm to use.
2951 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002952 * \param key Identifier of the key to use for the operation.
2953 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002954 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002955 * \param alg A signature algorithm (PSA_ALG_XXX
2956 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2957 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002958 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002960 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002961 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002962 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002963 * \param[out] signature_length On success, the number of bytes
2964 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002965 *
Gilles Peskineed733552023-02-14 19:21:09 +01002966 * \retval #PSA_SUCCESS \emptydescription
2967 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2968 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002969 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002970 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002971 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002972 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002973 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002974 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002975 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2976 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2977 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2978 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2979 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2980 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2981 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2982 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002983 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002984 * The library has not been previously initialized by psa_crypto_init().
2985 * It is implementation-dependent whether a failure to initialize
2986 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002987 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002988psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002989 psa_algorithm_t alg,
2990 const uint8_t *hash,
2991 size_t hash_length,
2992 uint8_t *signature,
2993 size_t signature_size,
2994 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002995
2996/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002997 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01002998 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002999 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003000 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003001 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3002 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003003 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3004 * to determine the hash algorithm to use.
3005 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003006 * \param key Identifier of the key to use for the operation. It
3007 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003008 * key must allow the usage
3009 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02003010 * \param alg A signature algorithm (PSA_ALG_XXX
3011 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3012 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003013 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003014 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003015 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003016 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003017 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003018 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003019 *
Gilles Peskine28538492018-07-11 17:34:00 +02003020 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003021 * The signature is valid.
Gilles Peskineed733552023-02-14 19:21:09 +01003022 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3023 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003024 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003025 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003026 * signature is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01003027 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3028 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3029 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3030 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3031 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3032 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3033 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003034 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003035 * The library has not been previously initialized by psa_crypto_init().
3036 * It is implementation-dependent whether a failure to initialize
3037 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003038 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003039psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003040 psa_algorithm_t alg,
3041 const uint8_t *hash,
3042 size_t hash_length,
3043 const uint8_t *signature,
3044 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003045
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003046/**
3047 * \brief Encrypt a short message with a public key.
3048 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003049 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003050 * It must be a public key or an asymmetric key
3051 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003052 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003053 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003054 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003055 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003056 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003057 * \param[in] salt A salt or label, if supported by the
3058 * encryption algorithm.
3059 * If the algorithm does not support a
3060 * salt, pass \c NULL.
3061 * If the algorithm supports an optional
3062 * salt and you do not want to pass a salt,
3063 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003064 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003065 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3066 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003067 * \param salt_length Size of the \p salt buffer in bytes.
3068 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003069 * \param[out] output Buffer where the encrypted message is to
3070 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003071 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003072 * \param[out] output_length On success, the number of bytes
3073 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003074 *
Gilles Peskineed733552023-02-14 19:21:09 +01003075 * \retval #PSA_SUCCESS \emptydescription
3076 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3077 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003078 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003079 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003080 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003081 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003082 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003083 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003084 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3085 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3086 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3087 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3088 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3089 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3090 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3091 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003092 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003093 * The library has not been previously initialized by psa_crypto_init().
3094 * It is implementation-dependent whether a failure to initialize
3095 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003096 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003097psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003098 psa_algorithm_t alg,
3099 const uint8_t *input,
3100 size_t input_length,
3101 const uint8_t *salt,
3102 size_t salt_length,
3103 uint8_t *output,
3104 size_t output_size,
3105 size_t *output_length);
3106
3107/**
3108 * \brief Decrypt a short message with a private key.
3109 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003110 * \param key Identifier of the key to use for the operation.
3111 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003112 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003113 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003114 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003115 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003116 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003117 * \param[in] salt A salt or label, if supported by the
3118 * encryption algorithm.
3119 * If the algorithm does not support a
3120 * salt, pass \c NULL.
3121 * If the algorithm supports an optional
3122 * salt and you do not want to pass a salt,
3123 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003124 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003125 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3126 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003127 * \param salt_length Size of the \p salt buffer in bytes.
3128 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003129 * \param[out] output Buffer where the decrypted message is to
3130 * be written.
3131 * \param output_size Size of the \c output buffer in bytes.
3132 * \param[out] output_length On success, the number of bytes
3133 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003134 *
Gilles Peskineed733552023-02-14 19:21:09 +01003135 * \retval #PSA_SUCCESS \emptydescription
3136 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3137 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003138 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003139 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003140 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003141 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003142 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003143 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003144 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3145 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3146 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3148 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3149 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3150 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3151 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3152 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003153 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003154 * The library has not been previously initialized by psa_crypto_init().
3155 * It is implementation-dependent whether a failure to initialize
3156 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003157 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003158psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003159 psa_algorithm_t alg,
3160 const uint8_t *input,
3161 size_t input_length,
3162 const uint8_t *salt,
3163 size_t salt_length,
3164 uint8_t *output,
3165 size_t output_size,
3166 size_t *output_length);
3167
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003168/**@}*/
3169
Gilles Peskine35675b62019-05-16 17:26:11 +02003170/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003171 * @{
3172 */
3173
Gilles Peskine35675b62019-05-16 17:26:11 +02003174/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003176 * Before calling any function on a key derivation operation object, the
3177 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003178 * - Set the structure to all-bits-zero, for example:
3179 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003180 * psa_key_derivation_operation_t operation;
3181 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003182 * \endcode
3183 * - Initialize the structure to logical zero values, for example:
3184 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003185 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003187 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003188 * for example:
3189 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003190 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003191 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003192 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003193 * to the structure, for example:
3194 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003195 * psa_key_derivation_operation_t operation;
3196 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197 * \endcode
3198 *
3199 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01003200 * make any assumptions about the content of this structure.
3201 * Implementation details can change in future versions without notice.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003203typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003204
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003205/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003207 * This macro returns a suitable initializer for a key derivation operation
3208 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210
Gilles Peskine35675b62019-05-16 17:26:11 +02003211/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003213static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003214
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003215/** Set up a key derivation operation.
3216 *
3217 * A key derivation algorithm takes some inputs and uses them to generate
3218 * a byte stream in a deterministic way.
3219 * This byte stream can be used to produce keys and other
3220 * cryptographic material.
3221 *
3222 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003223 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3224 * -# Call psa_key_derivation_setup() to select the algorithm.
3225 * -# Provide the inputs for the key derivation by calling
3226 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3227 * as appropriate. Which inputs are needed, in what order, and whether
3228 * they may be keys and if so of what type depends on the algorithm.
3229 * -# Optionally set the operation's maximum capacity with
3230 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3231 * of or after providing inputs. For some algorithms, this step is mandatory
3232 * because the output depends on the maximum capacity.
3233 * -# To derive a key, call psa_key_derivation_output_key().
3234 * To derive a byte string for a different purpose, call
3235 * psa_key_derivation_output_bytes().
3236 * Successive calls to these functions use successive output bytes
3237 * calculated by the key derivation algorithm.
3238 * -# Clean up the key derivation operation object with
3239 * psa_key_derivation_abort().
3240 *
3241 * If this function returns an error, the key derivation operation object is
3242 * not changed.
3243 *
3244 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3245 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003246 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003247 * Implementations must reject an attempt to derive a key of size 0.
3248 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003249 * \param[in,out] operation The key derivation operation object
3250 * to set up. It must
3251 * have been initialized but not set up yet.
3252 * \param alg The key derivation algorithm to compute
3253 * (\c PSA_ALG_XXX value such that
3254 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3255 *
3256 * \retval #PSA_SUCCESS
3257 * Success.
3258 * \retval #PSA_ERROR_INVALID_ARGUMENT
3259 * \c alg is not a key derivation algorithm.
3260 * \retval #PSA_ERROR_NOT_SUPPORTED
3261 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3264 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3265 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3266 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003267 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003268 * The operation state is not valid (it must be inactive), or
3269 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003270 * It is implementation-dependent whether a failure to initialize
3271 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003272 */
3273psa_status_t psa_key_derivation_setup(
3274 psa_key_derivation_operation_t *operation,
3275 psa_algorithm_t alg);
3276
Gilles Peskine35675b62019-05-16 17:26:11 +02003277/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003278 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003279 * The capacity of a key derivation is the maximum number of bytes that it can
3280 * return. When you get *N* bytes of output from a key derivation operation,
3281 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003282 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003283 * \param[in] operation The operation to query.
3284 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003285 *
Gilles Peskineed733552023-02-14 19:21:09 +01003286 * \retval #PSA_SUCCESS \emptydescription
3287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3288 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3289 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003290 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003291 * The operation state is not valid (it must be active), or
3292 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003293 * It is implementation-dependent whether a failure to initialize
3294 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003295 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003296psa_status_t psa_key_derivation_get_capacity(
3297 const psa_key_derivation_operation_t *operation,
3298 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003299
Gilles Peskine35675b62019-05-16 17:26:11 +02003300/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003301 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003302 * The capacity of a key derivation operation is the maximum number of bytes
3303 * that the key derivation operation can return from this point onwards.
3304 *
3305 * \param[in,out] operation The key derivation operation object to modify.
3306 * \param capacity The new capacity of the operation.
3307 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003308 * current capacity.
3309 *
Gilles Peskineed733552023-02-14 19:21:09 +01003310 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003311 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003312 * \p capacity is larger than the operation's current capacity.
3313 * In this case, the operation object remains valid and its capacity
3314 * remains unchanged.
Gilles Peskineed733552023-02-14 19:21:09 +01003315 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3316 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3317 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003318 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003319 * The operation state is not valid (it must be active), or the
3320 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003321 * It is implementation-dependent whether a failure to initialize
3322 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003323 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003324psa_status_t psa_key_derivation_set_capacity(
3325 psa_key_derivation_operation_t *operation,
3326 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003327
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003328/** Use the maximum possible capacity for a key derivation operation.
3329 *
3330 * Use this value as the capacity argument when setting up a key derivation
3331 * to indicate that the operation should have the maximum possible capacity.
3332 * The value of the maximum possible capacity depends on the key derivation
3333 * algorithm.
3334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003336
3337/** Provide an input for key derivation or key agreement.
3338 *
3339 * Which inputs are required and in what order depends on the algorithm.
3340 * Refer to the documentation of each key derivation or key agreement
3341 * algorithm for information.
3342 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003343 * This function passes direct inputs, which is usually correct for
3344 * non-secret inputs. To pass a secret input, which should be in a key
3345 * object, call psa_key_derivation_input_key() instead of this function.
3346 * Refer to the documentation of individual step types
3347 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3348 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003349 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003350 * If this function returns an error status, the operation enters an error
3351 * state and must be aborted by calling psa_key_derivation_abort().
3352 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003353 * \param[in,out] operation The key derivation operation object to use.
3354 * It must have been set up with
3355 * psa_key_derivation_setup() and must not
3356 * have produced any output yet.
3357 * \param step Which step the input data is for.
3358 * \param[in] data Input data to use.
3359 * \param data_length Size of the \p data buffer in bytes.
3360 *
3361 * \retval #PSA_SUCCESS
3362 * Success.
3363 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003364 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003365 * \c step does not allow direct inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3368 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3369 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3370 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003371 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003372 * The operation state is not valid for this input \p step, or
3373 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003374 * It is implementation-dependent whether a failure to initialize
3375 * results in this error code.
3376 */
3377psa_status_t psa_key_derivation_input_bytes(
3378 psa_key_derivation_operation_t *operation,
3379 psa_key_derivation_step_t step,
3380 const uint8_t *data,
3381 size_t data_length);
3382
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003383/** Provide a numeric input for key derivation or key agreement.
3384 *
3385 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003386 * However, when an algorithm requires a particular order, numeric inputs
3387 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003388 * Refer to the documentation of each key derivation or key agreement
3389 * algorithm for information.
3390 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003391 * This function is used for inputs which are fixed-size non-negative
3392 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003393 *
3394 * If this function returns an error status, the operation enters an error
3395 * state and must be aborted by calling psa_key_derivation_abort().
3396 *
3397 * \param[in,out] operation The key derivation operation object to use.
3398 * It must have been set up with
3399 * psa_key_derivation_setup() and must not
3400 * have produced any output yet.
3401 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003402 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003403 *
3404 * \retval #PSA_SUCCESS
3405 * Success.
3406 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003407 * \c step is not compatible with the operation's algorithm, or
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003408 * \c step does not allow numeric inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3411 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3412 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3413 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003414 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003415 * The operation state is not valid for this input \p step, or
3416 * the library has not been previously initialized by psa_crypto_init().
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003417 * It is implementation-dependent whether a failure to initialize
3418 * results in this error code.
3419 */
3420psa_status_t psa_key_derivation_input_integer(
3421 psa_key_derivation_operation_t *operation,
3422 psa_key_derivation_step_t step,
3423 uint64_t value);
3424
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003425/** Provide an input for key derivation in the form of a key.
3426 *
3427 * Which inputs are required and in what order depends on the algorithm.
3428 * Refer to the documentation of each key derivation or key agreement
3429 * algorithm for information.
3430 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003431 * This function obtains input from a key object, which is usually correct for
3432 * secret inputs or for non-secret personalization strings kept in the key
3433 * store. To pass a non-secret parameter which is not in the key store,
3434 * call psa_key_derivation_input_bytes() instead of this function.
3435 * Refer to the documentation of individual step types
3436 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3437 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003438 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003439 * If this function returns an error status, the operation enters an error
3440 * state and must be aborted by calling psa_key_derivation_abort().
3441 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003442 * \param[in,out] operation The key derivation operation object to use.
3443 * It must have been set up with
3444 * psa_key_derivation_setup() and must not
3445 * have produced any output yet.
3446 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003447 * \param key Identifier of the key. It must have an
3448 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003449 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003450 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3451 * and the algorithm used by the operation.
3452 *
3453 * \note Once all inputs steps are completed, the operations will allow:
3454 * - psa_key_derivation_output_bytes() if each input was either a direct input
3455 * or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003456 * - psa_key_derivation_output_key() if the input for step
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003457 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003458 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3459 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003460 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3461 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003462 * - psa_key_derivation_verify_key() under the same conditions as
3463 * psa_key_derivation_verify_bytes().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003464 *
3465 * \retval #PSA_SUCCESS
3466 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003467 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003468 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003469 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3470 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3471 * algorithm.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003472 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003473 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine224b0d62019-09-23 18:13:17 +02003474 * \c step does not allow key inputs of the given type
3475 * or does not allow key inputs at all.
Gilles Peskineed733552023-02-14 19:21:09 +01003476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3478 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3479 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3480 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003481 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003482 * The operation state is not valid for this input \p step, or
3483 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003484 * It is implementation-dependent whether a failure to initialize
3485 * results in this error code.
3486 */
3487psa_status_t psa_key_derivation_input_key(
3488 psa_key_derivation_operation_t *operation,
3489 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003490 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003491
3492/** Perform a key agreement and use the shared secret as input to a key
3493 * derivation.
3494 *
3495 * A key agreement algorithm takes two inputs: a private key \p private_key
3496 * a public key \p peer_key.
3497 * The result of this function is passed as input to a key derivation.
3498 * The output of this key derivation can be extracted by reading from the
3499 * resulting operation to produce keys and other cryptographic material.
3500 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003501 * If this function returns an error status, the operation enters an error
3502 * state and must be aborted by calling psa_key_derivation_abort().
3503 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003504 * \param[in,out] operation The key derivation operation object to use.
3505 * It must have been set up with
3506 * psa_key_derivation_setup() with a
3507 * key agreement and derivation algorithm
3508 * \c alg (\c PSA_ALG_XXX value such that
3509 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3510 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3511 * is false).
3512 * The operation must be ready for an
3513 * input of the type given by \p step.
3514 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003515 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003516 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003517 * \param[in] peer_key Public key of the peer. The peer key must be in the
3518 * same format that psa_import_key() accepts for the
3519 * public key type corresponding to the type of
3520 * private_key. That is, this function performs the
3521 * equivalent of
3522 * #psa_import_key(...,
3523 * `peer_key`, `peer_key_length`) where
3524 * with key attributes indicating the public key
3525 * type corresponding to the type of `private_key`.
3526 * For example, for EC keys, this means that peer_key
3527 * is interpreted as a point on the curve that the
3528 * private key is on. The standard formats for public
3529 * keys are documented in the documentation of
3530 * psa_export_public_key().
3531 * \param peer_key_length Size of \p peer_key in bytes.
3532 *
3533 * \retval #PSA_SUCCESS
3534 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003535 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3536 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003537 * \retval #PSA_ERROR_INVALID_ARGUMENT
3538 * \c private_key is not compatible with \c alg,
3539 * or \p peer_key is not valid for \c alg or not compatible with
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003540 * \c private_key, or \c step does not allow an input resulting
3541 * from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003542 * \retval #PSA_ERROR_NOT_SUPPORTED
3543 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003544 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3545 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3546 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3547 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3548 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003549 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003550 * The operation state is not valid for this key agreement \p step,
3551 * or the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003552 * It is implementation-dependent whether a failure to initialize
3553 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003554 */
3555psa_status_t psa_key_derivation_key_agreement(
3556 psa_key_derivation_operation_t *operation,
3557 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003558 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003559 const uint8_t *peer_key,
3560 size_t peer_key_length);
3561
Gilles Peskine35675b62019-05-16 17:26:11 +02003562/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003563 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003564 * This function calculates output bytes from a key derivation algorithm and
3565 * return those bytes.
3566 * If you view the key derivation's output as a stream of bytes, this
3567 * function destructively reads the requested number of bytes from the
3568 * stream.
3569 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003570 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003571 * If this function returns an error status other than
3572 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003573 * state and must be aborted by calling psa_key_derivation_abort().
3574 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003575 * \param[in,out] operation The key derivation operation object to read from.
3576 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003577 * \param output_length Number of bytes to output.
3578 *
Gilles Peskineed733552023-02-14 19:21:09 +01003579 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003580 * \retval #PSA_ERROR_NOT_PERMITTED
3581 * One of the inputs was a key whose policy didn't allow
3582 * #PSA_KEY_USAGE_DERIVE.
David Saadab4ecc272019-02-14 13:48:10 +02003583 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003584 * The operation's capacity was less than
3585 * \p output_length bytes. Note that in this case,
3586 * no output is written to the output buffer.
3587 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003588 * subsequent calls to this function will not
3589 * succeed, even with a smaller output buffer.
Gilles Peskineed733552023-02-14 19:21:09 +01003590 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3592 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3593 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3594 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003595 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003596 * The operation state is not valid (it must be active and completed
3597 * all required input steps), or the library has not been previously
3598 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003599 * It is implementation-dependent whether a failure to initialize
3600 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003601 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003602psa_status_t psa_key_derivation_output_bytes(
3603 psa_key_derivation_operation_t *operation,
3604 uint8_t *output,
3605 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003606
Gilles Peskine35675b62019-05-16 17:26:11 +02003607/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003608 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003609 * This function calculates output bytes from a key derivation algorithm
3610 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003611 * The key's location, usage policy, type and size are taken from
3612 * \p attributes.
3613 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003614 * If you view the key derivation's output as a stream of bytes, this
3615 * function destructively reads as many bytes as required from the
3616 * stream.
3617 * The operation's capacity decreases by the number of bytes read.
3618 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003619 * If this function returns an error status other than
3620 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003621 * state and must be aborted by calling psa_key_derivation_abort().
3622 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003623 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003624 * the key is derived, depends on the key type and on the key size
3625 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003626 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003627 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003628 * of a given size, this function is functionally equivalent to
3629 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003630 * and passing the resulting output to #psa_import_key.
3631 * However, this function has a security benefit:
3632 * if the implementation provides an isolation boundary then
3633 * the key material is not exposed outside the isolation boundary.
3634 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003635 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003636 * The following key types defined in this specification follow this scheme:
3637 *
3638 * - #PSA_KEY_TYPE_AES;
Gilles Peskine6c12a1e2021-09-21 11:59:39 +02003639 * - #PSA_KEY_TYPE_ARIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003640 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003641 * - #PSA_KEY_TYPE_DERIVE;
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003642 * - #PSA_KEY_TYPE_HMAC;
3643 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003644 *
3645 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003646 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003647 * Montgomery curve), this function always draws a byte string whose
3648 * length is determined by the curve, and sets the mandatory bits
3649 * accordingly. That is:
3650 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003651 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003652 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003653 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003654 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003655 *
3656 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003657 * \c bits bits with constraints as to which bit sequences are acceptable,
3658 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003659 * up to the nearest whole number of bytes. If the resulting byte string
3660 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3661 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003662 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003663 * for the output produced by psa_export_key().
3664 * The following key types defined in this specification follow this scheme:
3665 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003666 * - #PSA_KEY_TYPE_DES.
3667 * Force-set the parity bits, but discard forbidden weak keys.
3668 * For 2-key and 3-key triple-DES, the three keys are generated
3669 * successively (for example, for 3-key triple-DES,
3670 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3671 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003672 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003673 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003674 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003675 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003676 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003677 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003678 * Weierstrass curve).
3679 * For these key types, interpret the byte string as integer
3680 * in big-endian order. Discard it if it is not in the range
3681 * [0, *N* - 2] where *N* is the boundary of the private key domain
3682 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003683 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003684 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003685 * This method allows compliance to NIST standards, specifically
3686 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003687 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3688 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3689 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3690 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003691 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003692 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003693 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003694 * implementation-defined.
3695 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003696 * In all cases, the data that is read is discarded from the operation.
3697 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003698 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003699 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3700 * the input to that step must be provided with psa_key_derivation_input_key().
3701 * Future versions of this specification may include additional restrictions
3702 * on the derived key based on the attributes and strength of the secret key.
3703 *
Gilles Peskine20628592019-04-19 19:29:50 +02003704 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003705 * If the key type to be created is
3706 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3707 * the policy must be the same as in the current
3708 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003709 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003710 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003711 * key. For persistent keys, this is the key
3712 * identifier defined in \p attributes.
3713 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003714 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003715 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003716 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003717 * If the key is persistent, the key material and the key's metadata
3718 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003719 * \retval #PSA_ERROR_ALREADY_EXISTS
3720 * This is an attempt to create a persistent key, and there is
3721 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003722 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003723 * There was not enough data to create the desired key.
3724 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003725 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003726 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003727 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003728 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003729 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003730 * \retval #PSA_ERROR_INVALID_ARGUMENT
3731 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003732 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003733 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3734 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3735 * key; or one of the inputs was a key whose policy didn't allow
3736 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskineed733552023-02-14 19:21:09 +01003737 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3738 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3739 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3740 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3741 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3742 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3743 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3744 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003745 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003746 * The operation state is not valid (it must be active and completed
3747 * all required input steps), or the library has not been previously
3748 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003749 * It is implementation-dependent whether a failure to initialize
3750 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003751 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003752psa_status_t psa_key_derivation_output_key(
3753 const psa_key_attributes_t *attributes,
3754 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003755 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003756
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003757/** Compare output data from a key derivation operation to an expected value.
3758 *
3759 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003760 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003761 * If you view the key derivation's output as a stream of bytes, this
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003762 * function destructively reads the expected number of bytes from the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003763 * stream before comparing them.
3764 * The operation's capacity decreases by the number of bytes read.
3765 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003766 * This is functionally equivalent to the following code:
3767 * \code
3768 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3769 * if (memcmp(output, tmp, output_length) != 0)
3770 * return PSA_ERROR_INVALID_SIGNATURE;
3771 * \endcode
3772 * except (1) it works even if the key's policy does not allow outputting the
3773 * bytes, and (2) the comparison will be done in constant time.
3774 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003775 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003776 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3777 * the operation enters an error state and must be aborted by calling
3778 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003779 *
3780 * \param[in,out] operation The key derivation operation object to read from.
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003781 * \param[in] expected_output Buffer containing the expected derivation output.
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003782 * \param output_length Length of the expected output; this is also the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003783 * number of bytes that will be read.
3784 *
Gilles Peskineed733552023-02-14 19:21:09 +01003785 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003786 * \retval #PSA_ERROR_INVALID_SIGNATURE
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003787 * The output was read successfully, but it differs from the expected
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003788 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003789 * \retval #PSA_ERROR_NOT_PERMITTED
3790 * One of the inputs was a key whose policy didn't allow
3791 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003792 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3793 * The operation's capacity was less than
3794 * \p output_length bytes. Note that in this case,
3795 * the operation's capacity is set to 0, thus
3796 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003797 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3800 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3801 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3802 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003803 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003804 * The operation state is not valid (it must be active and completed
3805 * all required input steps), or the library has not been previously
3806 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003807 * It is implementation-dependent whether a failure to initialize
3808 * results in this error code.
3809 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003810psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003811 psa_key_derivation_operation_t *operation,
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003812 const uint8_t *expected_output,
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003813 size_t output_length);
3814
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003815/** Compare output data from a key derivation operation to an expected value
3816 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003817 *
3818 * This function calculates output bytes from a key derivation algorithm and
3819 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003820 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003821 * If you view the key derivation's output as a stream of bytes, this
Tom Cosgrove1797b052022-12-04 17:19:59 +00003822 * function destructively reads the number of bytes corresponding to the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003823 * length of the expected value from the stream before comparing them.
3824 * The operation's capacity decreases by the number of bytes read.
3825 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003826 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003827 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003828 * works even if the key cannot be exported.
3829 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003830 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003831 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3832 * the operation enters an error state and must be aborted by calling
3833 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003834 *
3835 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003836 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3837 * containing the expected output. Its policy must
3838 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3839 * and the permitted algorithm must match the
3840 * operation. The value of this key was likely
3841 * computed by a previous call to
3842 * psa_key_derivation_output_key().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003843 *
Gilles Peskineed733552023-02-14 19:21:09 +01003844 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003845 * \retval #PSA_ERROR_INVALID_SIGNATURE
3846 * The output was read successfully, but if differs from the expected
3847 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003848 * \retval #PSA_ERROR_INVALID_HANDLE
3849 * The key passed as the expected value does not exist.
3850 * \retval #PSA_ERROR_INVALID_ARGUMENT
3851 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003852 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02003853 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003854 * this algorithm; or one of the inputs was a key whose policy didn't
3855 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003856 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3857 * The operation's capacity was less than
3858 * the length of the expected value. In this case,
3859 * the operation's capacity is set to 0, thus
3860 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003861 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3864 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3865 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3866 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003867 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003868 * The operation state is not valid (it must be active and completed
3869 * all required input steps), or the library has not been previously
3870 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003871 * It is implementation-dependent whether a failure to initialize
3872 * results in this error code.
3873 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003874psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003875 psa_key_derivation_operation_t *operation,
3876 psa_key_id_t expected);
3877
Gilles Peskine35675b62019-05-16 17:26:11 +02003878/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003879 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003880 * Aborting an operation frees all associated resources except for the \c
3881 * operation structure itself. Once aborted, the operation object can be reused
3882 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003883 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003884 * This function may be called at any time after the operation
3885 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003886 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003887 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3888 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003890 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003891 *
Gilles Peskineed733552023-02-14 19:21:09 +01003892 * \retval #PSA_SUCCESS \emptydescription
3893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3894 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3895 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003896 * \retval #PSA_ERROR_BAD_STATE
3897 * The library has not been previously initialized by psa_crypto_init().
3898 * It is implementation-dependent whether a failure to initialize
3899 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003901psa_status_t psa_key_derivation_abort(
3902 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003903
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003904/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003905 *
3906 * \warning The raw result of a key agreement algorithm such as finite-field
3907 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3908 * not be used directly as key material. It should instead be passed as
3909 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003910 * a key derivation, use psa_key_derivation_key_agreement() and other
3911 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003912 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003913 * \param alg The key agreement algorithm to compute
3914 * (\c PSA_ALG_XXX value such that
3915 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3916 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003917 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003918 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003919 * \param[in] peer_key Public key of the peer. It must be
3920 * in the same format that psa_import_key()
3921 * accepts. The standard formats for public
3922 * keys are documented in the documentation
3923 * of psa_export_public_key().
3924 * \param peer_key_length Size of \p peer_key in bytes.
3925 * \param[out] output Buffer where the decrypted message is to
3926 * be written.
3927 * \param output_size Size of the \c output buffer in bytes.
3928 * \param[out] output_length On success, the number of bytes
3929 * that make up the returned output.
3930 *
3931 * \retval #PSA_SUCCESS
3932 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003933 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3934 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine769c7a62019-01-18 16:42:29 +01003935 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003936 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01003937 * \p private_key is not compatible with \p alg,
3938 * or \p peer_key is not valid for \p alg or not compatible with
3939 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003940 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3941 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003942 * \retval #PSA_ERROR_NOT_SUPPORTED
3943 * \p alg is not a supported key agreement algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3945 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3946 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3947 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3948 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003949 * \retval #PSA_ERROR_BAD_STATE
3950 * The library has not been previously initialized by psa_crypto_init().
3951 * It is implementation-dependent whether a failure to initialize
3952 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003953 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003954psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003955 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003956 const uint8_t *peer_key,
3957 size_t peer_key_length,
3958 uint8_t *output,
3959 size_t output_size,
3960 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003961
Gilles Peskineea0fb492018-07-12 17:17:20 +02003962/**@}*/
3963
Gilles Peskineedd76872018-07-20 17:42:05 +02003964/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003965 * @{
3966 */
3967
3968/**
3969 * \brief Generate random bytes.
3970 *
3971 * \warning This function **can** fail! Callers MUST check the return status
3972 * and MUST NOT use the content of the output buffer if the return
3973 * status is not #PSA_SUCCESS.
3974 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003975 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003976 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003977 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003978 * \param output_size Number of bytes to generate and output.
3979 *
Gilles Peskineed733552023-02-14 19:21:09 +01003980 * \retval #PSA_SUCCESS \emptydescription
3981 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3982 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3985 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3986 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir0adf0fc2018-09-06 16:24:41 +03003987 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003988 * The library has not been previously initialized by psa_crypto_init().
3989 * It is implementation-dependent whether a failure to initialize
3990 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003991 */
3992psa_status_t psa_generate_random(uint8_t *output,
3993 size_t output_size);
3994
3995/**
3996 * \brief Generate a key or key pair.
3997 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003998 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003999 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004000 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004001 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004002 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004003 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004004 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004005 * the public exponent is 65537.
4006 * The modulus is a product of two probabilistic primes
4007 * between 2^{n-1} and 2^n where n is the bit size specified in the
4008 * attributes.
4009 *
Gilles Peskine20628592019-04-19 19:29:50 +02004010 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004011 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004012 * key. For persistent keys, this is the key
4013 * identifier defined in \p attributes.
4014 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004015 *
Gilles Peskine28538492018-07-11 17:34:00 +02004016 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004017 * Success.
4018 * If the key is persistent, the key material and the key's metadata
4019 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004020 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004021 * This is an attempt to create a persistent key, and there is
4022 * already a persistent key with the given identifier.
Gilles Peskineed733552023-02-14 19:21:09 +01004023 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4024 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4025 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4026 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4027 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4028 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4029 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4030 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4031 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4032 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4033 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03004034 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004035 * The library has not been previously initialized by psa_crypto_init().
4036 * It is implementation-dependent whether a failure to initialize
4037 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004038 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004039psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004040 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004041
4042/**@}*/
4043
Paul Elliott1265f002022-09-09 17:15:43 +01004044/** \defgroup interruptible_hash Interruptible sign/verify hash
4045 * @{
4046 */
4047
4048/** The type of the state data structure for interruptible hash
4049 * signing operations.
4050 *
4051 * Before calling any function on a sign hash operation object, the
4052 * application must initialize it by any of the following means:
4053 * - Set the structure to all-bits-zero, for example:
4054 * \code
4055 * psa_sign_hash_interruptible_operation_t operation;
4056 * memset(&operation, 0, sizeof(operation));
4057 * \endcode
4058 * - Initialize the structure to logical zero values, for example:
4059 * \code
4060 * psa_sign_hash_interruptible_operation_t operation = {0};
4061 * \endcode
4062 * - Initialize the structure to the initializer
4063 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4064 * \code
4065 * psa_sign_hash_interruptible_operation_t operation =
4066 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4067 * \endcode
4068 * - Assign the result of the function
4069 * psa_sign_hash_interruptible_operation_init() to the structure, for
4070 * example:
4071 * \code
4072 * psa_sign_hash_interruptible_operation_t operation;
4073 * operation = psa_sign_hash_interruptible_operation_init();
4074 * \endcode
4075 *
4076 * This is an implementation-defined \c struct. Applications should not
4077 * make any assumptions about the content of this structure.
4078 * Implementation details can change in future versions without notice. */
4079typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4080
4081/** The type of the state data structure for interruptible hash
4082 * verification operations.
4083 *
4084 * Before calling any function on a sign hash operation object, the
4085 * application must initialize it by any of the following means:
4086 * - Set the structure to all-bits-zero, for example:
4087 * \code
4088 * psa_verify_hash_interruptible_operation_t operation;
4089 * memset(&operation, 0, sizeof(operation));
4090 * \endcode
4091 * - Initialize the structure to logical zero values, for example:
4092 * \code
4093 * psa_verify_hash_interruptible_operation_t operation = {0};
4094 * \endcode
4095 * - Initialize the structure to the initializer
4096 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4097 * \code
4098 * psa_verify_hash_interruptible_operation_t operation =
4099 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4100 * \endcode
4101 * - Assign the result of the function
4102 * psa_verify_hash_interruptible_operation_init() to the structure, for
4103 * example:
4104 * \code
4105 * psa_verify_hash_interruptible_operation_t operation;
4106 * operation = psa_verify_hash_interruptible_operation_init();
4107 * \endcode
4108 *
4109 * This is an implementation-defined \c struct. Applications should not
4110 * make any assumptions about the content of this structure.
4111 * Implementation details can change in future versions without notice. */
4112typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4113
4114/**
4115 * \brief Set the maximum number of ops allowed to be
4116 * executed by an interruptible function in a
4117 * single call.
4118 *
4119 * \warning This is a beta API, and thus subject to change
4120 * at any point. It is not bound by the usual
4121 * interface stability promises.
4122 *
4123 * \note The time taken to execute a single op is
4124 * implementation specific and depends on
4125 * software, hardware, the algorithm, key type and
4126 * curve chosen. Even within a single operation,
4127 * successive ops can take differing amounts of
4128 * time. The only guarantee is that lower values
4129 * for \p max_ops means functions will block for a
4130 * lesser maximum amount of time. The functions
4131 * \c psa_sign_interruptible_get_num_ops() and
4132 * \c psa_verify_interruptible_get_num_ops() are
4133 * provided to help with tuning this value.
4134 *
4135 * \note This value defaults to
4136 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4137 * means the whole operation will be done in one
4138 * go, regardless of the number of ops required.
4139 *
4140 * \note If more ops are needed to complete a
4141 * computation, #PSA_OPERATION_INCOMPLETE will be
4142 * returned by the function performing the
4143 * computation. It is then the caller's
4144 * responsibility to either call again with the
4145 * same operation context until it returns 0 or an
4146 * error code; or to call the relevant abort
4147 * function if the answer is no longer required.
4148 *
4149 * \note The interpretation of \p max_ops is also
4150 * implementation defined. On a hard real time
4151 * system, this can indicate a hard deadline, as a
4152 * real-time system needs a guarantee of not
4153 * spending more than X time, however care must be
4154 * taken in such an implementation to avoid the
4155 * situation whereby calls just return, not being
4156 * able to do any actual work within the allotted
4157 * time. On a non-real-time system, the
4158 * implementation can be more relaxed, but again
4159 * whether this number should be interpreted as as
4160 * hard or soft limit or even whether a less than
4161 * or equals as regards to ops executed in a
4162 * single call is implementation defined.
4163 *
Paul Elliott21c39512023-02-15 19:47:39 +00004164 * \note For keys in local storage when no accelerator
4165 * driver applies, please see also the
4166 * documentation for \c mbedtls_ecp_set_max_ops(),
4167 * which is the internal implementation in these
4168 * cases.
4169 *
Paul Elliott1265f002022-09-09 17:15:43 +01004170 * \warning With implementations that interpret this number
4171 * as a hard limit, setting this number too small
4172 * may result in an infinite loop, whereby each
4173 * call results in immediate return with no ops
4174 * done (as there is not enough time to execute
4175 * any), and thus no result will ever be achieved.
4176 *
4177 * \note This only applies to functions whose
4178 * documentation mentions they may return
4179 * #PSA_OPERATION_INCOMPLETE.
4180 *
4181 * \param max_ops The maximum number of ops to be executed in a
4182 * single call. This can be a number from 0 to
4183 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4184 * is the least amount of work done per call.
4185 */
4186void psa_interruptible_set_max_ops(uint32_t max_ops);
4187
4188/**
4189 * \brief Get the maximum number of ops allowed to be
4190 * executed by an interruptible function in a
4191 * single call. This will return the last
4192 * value set by
4193 * \c psa_interruptible_set_max_ops() or
4194 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4195 * that function has never been called.
4196 *
4197 * \warning This is a beta API, and thus subject to change
4198 * at any point. It is not bound by the usual
4199 * interface stability promises.
4200 *
4201 * \return Maximum number of ops allowed to be
4202 * executed by an interruptible function in a
4203 * single call.
4204 */
4205uint32_t psa_interruptible_get_max_ops(void);
4206
4207/**
4208 * \brief Get the number of ops that a hash signing
4209 * operation has taken so far. If the operation
4210 * has completed, then this will represent the
4211 * number of ops required for the entire
4212 * operation. After initialization or calling
4213 * \c psa_sign_hash_interruptible_abort() on
4214 * the operation, a value of 0 will be returned.
4215 *
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004216 * \note This interface is guaranteed re-entrant and
4217 * thus may be called from driver code.
4218 *
Paul Elliott1265f002022-09-09 17:15:43 +01004219 * \warning This is a beta API, and thus subject to change
4220 * at any point. It is not bound by the usual
4221 * interface stability promises.
4222 *
4223 * This is a helper provided to help you tune the
4224 * value passed to \c
4225 * psa_interruptible_set_max_ops().
4226 *
4227 * \param operation The \c psa_sign_hash_interruptible_operation_t
4228 * to use. This must be initialized first.
4229 *
4230 * \return Number of ops that the operation has taken so
4231 * far.
4232 */
4233uint32_t psa_sign_hash_get_num_ops(
4234 const psa_sign_hash_interruptible_operation_t *operation);
4235
4236/**
4237 * \brief Get the number of ops that a hash verification
4238 * operation has taken so far. If the operation
4239 * has completed, then this will represent the
4240 * number of ops required for the entire
4241 * operation. After initialization or calling \c
4242 * psa_verify_hash_interruptible_abort() on the
4243 * operation, a value of 0 will be returned.
4244 *
4245 * \warning This is a beta API, and thus subject to change
4246 * at any point. It is not bound by the usual
4247 * interface stability promises.
4248 *
4249 * This is a helper provided to help you tune the
4250 * value passed to \c
4251 * psa_interruptible_set_max_ops().
4252 *
4253 * \param operation The \c
4254 * psa_verify_hash_interruptible_operation_t to
4255 * use. This must be initialized first.
4256 *
4257 * \return Number of ops that the operation has taken so
4258 * far.
4259 */
4260uint32_t psa_verify_hash_get_num_ops(
4261 const psa_verify_hash_interruptible_operation_t *operation);
4262
4263/**
4264 * \brief Start signing a hash or short message with a
4265 * private key, in an interruptible manner.
4266 *
4267 * \see \c psa_sign_hash_complete()
4268 *
4269 * \warning This is a beta API, and thus subject to change
4270 * at any point. It is not bound by the usual
4271 * interface stability promises.
4272 *
4273 * \note This function combined with \c
4274 * psa_sign_hash_complete() is equivalent to
4275 * \c psa_sign_hash() but
4276 * \c psa_sign_hash_complete() can return early and
4277 * resume according to the limit set with \c
4278 * psa_interruptible_set_max_ops() to reduce the
4279 * maximum time spent in a function call.
4280 *
4281 * \note Users should call \c psa_sign_hash_complete()
4282 * repeatedly on the same context after a
4283 * successful call to this function until \c
4284 * psa_sign_hash_complete() either returns 0 or an
4285 * error. \c psa_sign_hash_complete() will return
4286 * #PSA_OPERATION_INCOMPLETE if there is more work
4287 * to do. Alternatively users can call
4288 * \c psa_sign_hash_abort() at any point if they no
4289 * longer want the result.
4290 *
4291 * \note If this function returns an error status, the
4292 * operation enters an error state and must be
4293 * aborted by calling \c psa_sign_hash_abort().
4294 *
4295 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4296 * to use. This must be initialized first.
4297 *
4298 * \param key Identifier of the key to use for the operation.
4299 * It must be an asymmetric key pair. The key must
4300 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4301 * \param alg A signature algorithm (\c PSA_ALG_XXX
4302 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4303 * is true), that is compatible with
4304 * the type of \p key.
4305 * \param[in] hash The hash or message to sign.
4306 * \param hash_length Size of the \p hash buffer in bytes.
4307 *
4308 * \retval #PSA_SUCCESS
4309 * The operation started successfully - call \c psa_sign_hash_complete()
4310 * with the same context to complete the operation
4311 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004312 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004313 * \retval #PSA_ERROR_NOT_PERMITTED
4314 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4315 * not permit the requested algorithm.
4316 * \retval #PSA_ERROR_BAD_STATE
4317 * An operation has previously been started on this context, and is
4318 * still in progress.
Paul Elliott15d7d432023-02-27 17:17:56 +00004319 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4320 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4323 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4324 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4325 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4326 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4327 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4328 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004329 * \retval #PSA_ERROR_BAD_STATE
4330 * The library has not been previously initialized by psa_crypto_init().
4331 * It is implementation-dependent whether a failure to initialize
4332 * results in this error code.
4333 */
4334psa_status_t psa_sign_hash_start(
4335 psa_sign_hash_interruptible_operation_t *operation,
4336 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4337 const uint8_t *hash, size_t hash_length);
4338
4339/**
4340 * \brief Continue and eventually complete the action of
4341 * signing a hash or short message with a private
4342 * key, in an interruptible manner.
4343 *
4344 * \see \c psa_sign_hash_start()
4345 *
4346 * \warning This is a beta API, and thus subject to change
4347 * at any point. It is not bound by the usual
4348 * interface stability promises.
4349 *
4350 * \note This function combined with \c
4351 * psa_sign_hash_start() is equivalent to
4352 * \c psa_sign_hash() but this function can return
4353 * early and resume according to the limit set with
4354 * \c psa_interruptible_set_max_ops() to reduce the
4355 * maximum time spent in a function call.
4356 *
4357 * \note Users should call this function on the same
4358 * operation object repeatedly until it either
4359 * returns 0 or an error. This function will return
4360 * #PSA_OPERATION_INCOMPLETE if there is more work
4361 * to do. Alternatively users can call
4362 * \c psa_sign_hash_abort() at any point if they no
4363 * longer want the result.
4364 *
4365 * \note When this function returns successfully, the
4366 * operation becomes inactive. If this function
4367 * returns an error status, the operation enters an
4368 * error state and must be aborted by calling
4369 * \c psa_sign_hash_abort().
4370 *
4371 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4372 * to use. This must be initialized first, and have
4373 * had \c psa_sign_hash_start() called with it
4374 * first.
4375 *
4376 * \param[out] signature Buffer where the signature is to be written.
4377 * \param signature_size Size of the \p signature buffer in bytes. This
4378 * must be appropriate for the selected
4379 * algorithm and key:
4380 * - The required signature size is
4381 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4382 * key_bits, \c alg) where \c key_type and \c
4383 * key_bits are the type and bit-size
4384 * respectively of key.
4385 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4386 * maximum signature size of any supported
4387 * signature algorithm.
4388 * \param[out] signature_length On success, the number of bytes that make up
4389 * the returned signature value.
4390 *
4391 * \retval #PSA_SUCCESS
4392 * Operation completed successfully
4393 *
4394 * \retval #PSA_OPERATION_INCOMPLETE
4395 * Operation was interrupted due to the setting of \c
4396 * psa_interruptible_set_max_ops(). There is still work to be done.
4397 * Call this function again with the same operation object.
4398 *
4399 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4400 * The size of the \p signature buffer is too small. You can
4401 * determine a sufficient buffer size by calling
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004402 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
Paul Elliott1265f002022-09-09 17:15:43 +01004403 * where \c key_type and \c key_bits are the type and bit-size
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004404 * respectively of \c key.
Paul Elliott1265f002022-09-09 17:15:43 +01004405 *
4406 * \retval #PSA_ERROR_BAD_STATE
4407 * An operation was not previously started on this context via
4408 * \c psa_sign_hash_start().
4409 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004410 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4411 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4412 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4413 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4414 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4415 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4416 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4417 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4418 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4419 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004420 * \retval #PSA_ERROR_BAD_STATE
4421 * The library has either not been previously initialized by
Paul Elliott15d7d432023-02-27 17:17:56 +00004422 * psa_crypto_init() or you did not previously call
Paul Elliott1265f002022-09-09 17:15:43 +01004423 * psa_sign_hash_start() with this operation object. It is
4424 * implementation-dependent whether a failure to initialize results in
4425 * this error code.
4426 */
4427psa_status_t psa_sign_hash_complete(
4428 psa_sign_hash_interruptible_operation_t *operation,
4429 uint8_t *signature, size_t signature_size,
4430 size_t *signature_length);
4431
4432/**
4433 * \brief Abort a sign hash operation.
4434 *
4435 * \warning This is a beta API, and thus subject to change
4436 * at any point. It is not bound by the usual
4437 * interface stability promises.
Paul Elliott1b49ef52023-02-03 14:27:32 +00004438 *
4439 * \note This function is the only function that clears
4440 * the number of ops completed as part of the
4441 * operation. Please ensure you copy this value via
4442 * \c psa_sign_hash_get_num_ops() if required
4443 * before calling.
4444 *
Paul Elliott1265f002022-09-09 17:15:43 +01004445 * \note Aborting an operation frees all associated
4446 * resources except for the \p operation structure
4447 * itself. Once aborted, the operation object can
4448 * be reused for another operation by calling \c
4449 * psa_sign_hash_start() again.
4450 *
4451 * \note You may call this function any time after the
4452 * operation object has been initialized. In
4453 * particular, calling \c psa_sign_hash_abort()
4454 * after the operation has already been terminated
4455 * by a call to \c psa_sign_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004456 * psa_sign_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004457 *
4458 * \param[in,out] operation Initialized sign hash operation.
4459 *
4460 * \retval #PSA_SUCCESS
4461 * The operation was aborted successfully.
4462 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004463 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004464 * \retval #PSA_ERROR_BAD_STATE
4465 * The library has not been previously initialized by psa_crypto_init().
4466 * It is implementation-dependent whether a failure to initialize
4467 * results in this error code.
4468 */
4469psa_status_t psa_sign_hash_abort(
4470 psa_sign_hash_interruptible_operation_t *operation);
4471
4472/**
4473 * \brief Start reading and verifying a hash or short
4474 * message, in an interruptible manner.
4475 *
4476 * \see \c psa_verify_hash_complete()
4477 *
4478 * \warning This is a beta API, and thus subject to change
4479 * at any point. It is not bound by the usual
4480 * interface stability promises.
4481 *
4482 * \note This function combined with \c
4483 * psa_verify_hash_complete() is equivalent to
4484 * \c psa_verify_hash() but \c
4485 * psa_verify_hash_complete() can return early and
4486 * resume according to the limit set with \c
4487 * psa_interruptible_set_max_ops() to reduce the
4488 * maximum time spent in a function.
4489 *
4490 * \note Users should call \c psa_verify_hash_complete()
4491 * repeatedly on the same operation object after a
4492 * successful call to this function until \c
4493 * psa_verify_hash_complete() either returns 0 or
4494 * an error. \c psa_verify_hash_complete() will
4495 * return #PSA_OPERATION_INCOMPLETE if there is
4496 * more work to do. Alternatively users can call
4497 * \c psa_verify_hash_abort() at any point if they
4498 * no longer want the result.
4499 *
4500 * \note If this function returns an error status, the
4501 * operation enters an error state and must be
4502 * aborted by calling \c psa_verify_hash_abort().
4503 *
4504 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4505 * to use. This must be initialized first.
4506 *
4507 * \param key Identifier of the key to use for the operation.
4508 * The key must allow the usage
4509 * #PSA_KEY_USAGE_VERIFY_HASH.
4510 * \param alg A signature algorithm (\c PSA_ALG_XXX
4511 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4512 * is true), that is compatible with
4513 * the type of \p key.
4514 * \param[in] hash The hash whose signature is to be verified.
4515 * \param hash_length Size of the \p hash buffer in bytes.
4516 * \param[in] signature Buffer containing the signature to verify.
4517 * \param signature_length Size of the \p signature buffer in bytes.
4518 *
4519 * \retval #PSA_SUCCESS
4520 * The operation started successfully - please call \c
4521 * psa_verify_hash_complete() with the same context to complete the
4522 * operation.
4523 *
4524 * \retval #PSA_ERROR_BAD_STATE
4525 * Another operation has already been started on this context, and is
4526 * still in progress.
4527 *
4528 * \retval #PSA_ERROR_NOT_PERMITTED
4529 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4530 * not permit the requested algorithm.
4531 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004532 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4533 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4536 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4537 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4538 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4539 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4540 * \retval PSA_ERROR_DATA_INVALID \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004541 * \retval #PSA_ERROR_BAD_STATE
4542 * The library has not been previously initialized by psa_crypto_init().
4543 * It is implementation-dependent whether a failure to initialize
4544 * results in this error code.
4545 */
4546psa_status_t psa_verify_hash_start(
4547 psa_verify_hash_interruptible_operation_t *operation,
4548 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4549 const uint8_t *hash, size_t hash_length,
4550 const uint8_t *signature, size_t signature_length);
4551
4552/**
4553 * \brief Continue and eventually complete the action of
4554 * reading and verifying a hash or short message
4555 * signed with a private key, in an interruptible
4556 * manner.
4557 *
4558 * \see \c psa_verify_hash_start()
4559 *
4560 * \warning This is a beta API, and thus subject to change
4561 * at any point. It is not bound by the usual
4562 * interface stability promises.
4563 *
4564 * \note This function combined with \c
4565 * psa_verify_hash_start() is equivalent to
4566 * \c psa_verify_hash() but this function can
4567 * return early and resume according to the limit
4568 * set with \c psa_interruptible_set_max_ops() to
4569 * reduce the maximum time spent in a function
4570 * call.
4571 *
4572 * \note Users should call this function on the same
4573 * operation object repeatedly until it either
4574 * returns 0 or an error. This function will return
4575 * #PSA_OPERATION_INCOMPLETE if there is more work
4576 * to do. Alternatively users can call
4577 * \c psa_verify_hash_abort() at any point if they
4578 * no longer want the result.
4579 *
4580 * \note When this function returns successfully, the
4581 * operation becomes inactive. If this function
4582 * returns an error status, the operation enters an
4583 * error state and must be aborted by calling
4584 * \c psa_verify_hash_abort().
4585 *
4586 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4587 * to use. This must be initialized first, and have
4588 * had \c psa_verify_hash_start() called with it
4589 * first.
4590 *
4591 * \retval #PSA_SUCCESS
4592 * Operation completed successfully, and the passed signature is valid.
4593 *
4594 * \retval #PSA_OPERATION_INCOMPLETE
4595 * Operation was interrupted due to the setting of \c
4596 * psa_interruptible_set_max_ops(). There is still work to be done.
4597 * Call this function again with the same operation object.
4598 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004599 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004600 * \retval #PSA_ERROR_INVALID_SIGNATURE
4601 * The calculation was performed successfully, but the passed
4602 * signature is not a valid signature.
Paul Elliott15d7d432023-02-27 17:17:56 +00004603 * \retval #PSA_ERROR_BAD_STATE
Paul Elliott1265f002022-09-09 17:15:43 +01004604 * An operation was not previously started on this context via
4605 * \c psa_verify_hash_start().
Paul Elliott15d7d432023-02-27 17:17:56 +00004606 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4607 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4608 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4610 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4611 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4612 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4613 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4614 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4615 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004616 * \retval #PSA_ERROR_BAD_STATE
4617 * The library has either not been previously initialized by
4618 * psa_crypto_init() or you did not previously call
4619 * psa_verify_hash_start() on this object. It is
4620 * implementation-dependent whether a failure to initialize results in
4621 * this error code.
4622 */
4623psa_status_t psa_verify_hash_complete(
4624 psa_verify_hash_interruptible_operation_t *operation);
4625
4626/**
4627 * \brief Abort a verify hash operation.
4628 *
4629 * \warning This is a beta API, and thus subject to change at
4630 * any point. It is not bound by the usual interface
4631 * stability promises.
4632 *
Paul Elliott1b49ef52023-02-03 14:27:32 +00004633 * \note This function is the only function that clears the
4634 * number of ops completed as part of the operation.
4635 * Please ensure you copy this value via
4636 * \c psa_verify_hash_get_num_ops() if required
4637 * before calling.
4638 *
Paul Elliott1265f002022-09-09 17:15:43 +01004639 * \note Aborting an operation frees all associated
4640 * resources except for the operation structure
4641 * itself. Once aborted, the operation object can be
4642 * reused for another operation by calling \c
4643 * psa_verify_hash_start() again.
4644 *
4645 * \note You may call this function any time after the
4646 * operation object has been initialized.
4647 * In particular, calling \c psa_verify_hash_abort()
4648 * after the operation has already been terminated by
4649 * a call to \c psa_verify_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004650 * psa_verify_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004651 *
4652 * \param[in,out] operation Initialized verify hash operation.
4653 *
4654 * \retval #PSA_SUCCESS
4655 * The operation was aborted successfully.
4656 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004657 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004658 * \retval #PSA_ERROR_BAD_STATE
4659 * The library has not been previously initialized by psa_crypto_init().
4660 * It is implementation-dependent whether a failure to initialize
4661 * results in this error code.
4662 */
4663psa_status_t psa_verify_hash_abort(
4664 psa_verify_hash_interruptible_operation_t *operation);
4665
4666
4667/**@}*/
4668
Gilles Peskinee59236f2018-01-27 23:32:46 +01004669#ifdef __cplusplus
4670}
4671#endif
4672
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004673/* The file "crypto_sizes.h" contains definitions for size calculation
4674 * macros whose definitions are implementation-specific. */
4675#include "crypto_sizes.h"
4676
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004677/* The file "crypto_struct.h" contains definitions for
4678 * implementation-specific structs that are declared above. */
Gilles Peskineb1176f22023-02-22 22:07:28 +01004679#if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)
4680#include MBEDTLS_PSA_CRYPTO_STRUCT_FILE
4681#else
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004682#include "crypto_struct.h"
Gilles Peskineb1176f22023-02-22 22:07:28 +01004683#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004684
4685/* The file "crypto_extra.h" contains vendor-specific definitions. This
4686 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004687#include "crypto_extra.h"
4688
4689#endif /* PSA_CRYPTO_H */