blob: 18c783cef6b353406edc860c3be392020eeff286 [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 Peskine6d81cbc2024-02-12 16:25:19 +0100122 * psa_import_key(), psa_generate_key(), psa_generate_key_ext(),
123 * psa_key_derivation_output_key(), psa_key_derivation_output_key_ext()
124 * or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200125 *
Gilles Peskine20628592019-04-19 19:29:50 +0200126 * This function may be declared as `static` (i.e. without external
127 * linkage). This function may be provided as a function-like macro,
128 * but in this case it must evaluate each of its arguments exactly once.
129 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200130 * \param[out] attributes The attribute structure to write to.
131 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133static void psa_set_key_id(psa_key_attributes_t *attributes,
134 mbedtls_svc_key_id_t key);
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135
Ronald Cron6b5ff532020-10-16 14:38:19 +0200136#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
137/** Set the owner identifier of a key.
138 *
139 * When key identifiers encode key owner identifiers, psa_set_key_id() does
140 * not allow to define in key attributes the owner of volatile keys as
141 * psa_set_key_id() enforces the key to be persistent.
142 *
143 * This function allows to set in key attributes the owner identifier of a
144 * key. It is intended to be used for volatile keys. For persistent keys,
145 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
146 * the owner of a key.
147 *
148 * \param[out] attributes The attribute structure to write to.
Antonio de Angelise2b68662021-11-30 12:21:44 +0000149 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
152 mbedtls_key_owner_id_t owner);
Ronald Cron6b5ff532020-10-16 14:38:19 +0200153#endif
154
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200155/** Set the location of a persistent key.
156 *
157 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200158 * with psa_set_key_id(). By default, a key that has a persistent identifier
159 * is stored in the default storage area identifier by
160 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
161 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200162 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200163 * This function does not access storage, it merely stores the given
164 * value in the structure.
165 * The persistent key will be written to storage when the attribute
166 * structure is passed to a key creation function such as
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100167 * psa_import_key(), psa_generate_key(), psa_generate_key_ext(),
168 * psa_key_derivation_output_key(), psa_key_derivation_output_key_ext()
169 * or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200170 *
171 * This function may be declared as `static` (i.e. without external
172 * linkage). This function may be provided as a function-like macro,
173 * but in this case it must evaluate each of its arguments exactly once.
174 *
175 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200176 * \param lifetime The lifetime for the key.
177 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200178 * key will be volatile, and the key identifier
179 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200180 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200181static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
182 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200183
Gilles Peskine20628592019-04-19 19:29:50 +0200184/** Retrieve the key identifier from key attributes.
185 *
186 * This function may be declared as `static` (i.e. without external
187 * linkage). This function may be provided as a function-like macro,
188 * but in this case it must evaluate its argument exactly once.
189 *
190 * \param[in] attributes The key attribute structure to query.
191 *
192 * \return The persistent identifier stored in the attribute structure.
193 * This value is unspecified if the attribute structure declares
194 * the key as volatile.
195 */
Ronald Cron71016a92020-08-28 19:01:50 +0200196static mbedtls_svc_key_id_t psa_get_key_id(
197 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200198
Gilles Peskine20628592019-04-19 19:29:50 +0200199/** Retrieve the lifetime from key attributes.
200 *
201 * This function may be declared as `static` (i.e. without external
202 * linkage). This function may be provided as a function-like macro,
203 * but in this case it must evaluate its argument exactly once.
204 *
205 * \param[in] attributes The key attribute structure to query.
206 *
207 * \return The lifetime value stored in the attribute structure.
208 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200209static psa_key_lifetime_t psa_get_key_lifetime(
210 const psa_key_attributes_t *attributes);
211
Gilles Peskine20628592019-04-19 19:29:50 +0200212/** Declare usage flags for a key.
213 *
214 * Usage flags are part of a key's usage policy. They encode what
215 * kind of operations are permitted on the key. For more details,
216 * refer to the documentation of the type #psa_key_usage_t.
217 *
218 * This function overwrites any usage flags
219 * previously set in \p attributes.
220 *
221 * This function may be declared as `static` (i.e. without external
222 * linkage). This function may be provided as a function-like macro,
223 * but in this case it must evaluate each of its arguments exactly once.
224 *
225 * \param[out] attributes The attribute structure to write to.
226 * \param usage_flags The usage flags to write.
227 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200228static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
229 psa_key_usage_t usage_flags);
230
Gilles Peskine20628592019-04-19 19:29:50 +0200231/** Retrieve the usage flags from key attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate its argument exactly once.
236 *
237 * \param[in] attributes The key attribute structure to query.
238 *
239 * \return The usage flags stored in the attribute structure.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static psa_key_usage_t psa_get_key_usage_flags(
242 const psa_key_attributes_t *attributes);
243
Gilles Peskine20628592019-04-19 19:29:50 +0200244/** Declare the permitted algorithm policy for a key.
245 *
246 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200247 * algorithms are permitted to be used with this key. The following
248 * algorithm policies are supported:
249 * - 0 does not allow any cryptographic operation with the key. The key
250 * may be used for non-cryptographic actions such as exporting (if
251 * permitted by the usage flags).
252 * - An algorithm value permits this particular algorithm.
253 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
254 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100255 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100256 * any MAC algorithm from the same base class (e.g. CMAC) which
257 * generates/verifies a MAC length greater than or equal to the length
258 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100259 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
260 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100261 * generates/verifies a tag length greater than or equal to the length
262 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200263 *
264 * This function overwrites any algorithm policy
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param alg The permitted algorithm policy to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
275 psa_algorithm_t alg);
276
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100277
Gilles Peskine20628592019-04-19 19:29:50 +0200278/** Retrieve the algorithm policy from key attributes.
279 *
280 * This function may be declared as `static` (i.e. without external
281 * linkage). This function may be provided as a function-like macro,
282 * but in this case it must evaluate its argument exactly once.
283 *
284 * \param[in] attributes The key attribute structure to query.
285 *
286 * \return The algorithm stored in the attribute structure.
287 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200288static psa_algorithm_t psa_get_key_algorithm(
289 const psa_key_attributes_t *attributes);
290
Gilles Peskine20628592019-04-19 19:29:50 +0200291/** Declare the type of a key.
292 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200293 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200294 * previously set in \p attributes.
295 *
296 * This function may be declared as `static` (i.e. without external
297 * linkage). This function may be provided as a function-like macro,
298 * but in this case it must evaluate each of its arguments exactly once.
299 *
300 * \param[out] attributes The attribute structure to write to.
301 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200302 * If this is 0, the key type in \p attributes
303 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_type(psa_key_attributes_t *attributes,
306 psa_key_type_t type);
307
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100308
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200309/** Declare the size of a key.
310 *
311 * This function overwrites any key size previously set in \p attributes.
312 *
313 * This function may be declared as `static` (i.e. without external
314 * linkage). This function may be provided as a function-like macro,
315 * but in this case it must evaluate each of its arguments exactly once.
316 *
317 * \param[out] attributes The attribute structure to write to.
318 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200319 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200320 * becomes unspecified. Keys of size 0 are
321 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200322 */
323static void psa_set_key_bits(psa_key_attributes_t *attributes,
324 size_t bits);
325
Gilles Peskine20628592019-04-19 19:29:50 +0200326/** Retrieve the key type from key attributes.
327 *
328 * This function may be declared as `static` (i.e. without external
329 * linkage). This function may be provided as a function-like macro,
330 * but in this case it must evaluate its argument exactly once.
331 *
332 * \param[in] attributes The key attribute structure to query.
333 *
334 * \return The key type stored in the attribute structure.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
337
Gilles Peskine20628592019-04-19 19:29:50 +0200338/** Retrieve the key size from key attributes.
339 *
340 * This function may be declared as `static` (i.e. without external
341 * linkage). This function may be provided as a function-like macro,
342 * but in this case it must evaluate its argument exactly once.
343 *
344 * \param[in] attributes The key attribute structure to query.
345 *
346 * \return The key size stored in the attribute structure, in bits.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Retrieve the attributes of a key.
351 *
352 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200353 * psa_reset_key_attributes(). It then copies the attributes of
354 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200355 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200356 * \note This function may allocate memory or other resources.
357 * Once you have called this function on an attribute structure,
358 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200359 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200360 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200361 * \param[in,out] attributes On success, the attributes of the key.
362 * On failure, equivalent to a
363 * freshly-initialized structure.
364 *
Gilles Peskineed733552023-02-14 19:21:09 +0100365 * \retval #PSA_SUCCESS \emptydescription
366 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
369 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
370 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
371 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
372 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100373 * \retval #PSA_ERROR_BAD_STATE
374 * The library has not been previously initialized by psa_crypto_init().
375 * It is implementation-dependent whether a failure to initialize
376 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200377 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200378psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200379 psa_key_attributes_t *attributes);
380
Gilles Peskine20628592019-04-19 19:29:50 +0200381/** Reset a key attribute structure to a freshly initialized state.
382 *
383 * You must initialize the attribute structure as described in the
384 * documentation of the type #psa_key_attributes_t before calling this
385 * function. Once the structure has been initialized, you may call this
386 * function at any time.
387 *
388 * This function frees any auxiliary resources that the structure
389 * may contain.
390 *
391 * \param[in,out] attributes The attribute structure to reset.
392 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200393void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200394
Gilles Peskine87a5e562019-04-17 12:28:25 +0200395/**@}*/
396
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397/** \defgroup key_management Key management
398 * @{
399 */
400
Ronald Cron277a85f2020-08-04 15:49:48 +0200401/** Remove non-essential copies of key material from memory.
402 *
403 * If the key identifier designates a volatile key, this functions does not do
404 * anything and returns successfully.
405 *
406 * If the key identifier designates a persistent key, then this function will
407 * free all resources associated with the key in volatile memory. The key
408 * data in persistent storage is not affected and the key can still be used.
409 *
410 * \param key Identifier of the key to purge.
411 *
412 * \retval #PSA_SUCCESS
413 * The key material will have been removed from memory if it is not
414 * currently required.
415 * \retval #PSA_ERROR_INVALID_ARGUMENT
416 * \p key is not a valid key identifier.
417 * \retval #PSA_ERROR_BAD_STATE
418 * The library has not been previously initialized by psa_crypto_init().
419 * It is implementation-dependent whether a failure to initialize
420 * results in this error code.
421 */
422psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
423
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100424/** Make a copy of a key.
425 *
426 * Copy key material from one location to another.
427 *
428 * This function is primarily useful to copy a key from one location
429 * to another, since it populates a key using the material from
430 * another key which may have a different lifetime.
431 *
432 * This function may be used to share a key with a different party,
433 * subject to implementation-defined restrictions on key sharing.
434 *
435 * The policy on the source key must have the usage flag
436 * #PSA_KEY_USAGE_COPY set.
437 * This flag is sufficient to permit the copy if the key has the lifetime
438 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
439 * Some secure elements do not provide a way to copy a key without
440 * making it extractable from the secure element. If a key is located
441 * in such a secure element, then the key must have both usage flags
442 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
443 * a copy of the key outside the secure element.
444 *
445 * The resulting key may only be used in a way that conforms to
446 * both the policy of the original key and the policy specified in
447 * the \p attributes parameter:
448 * - The usage flags on the resulting key are the bitwise-and of the
449 * usage flags on the source policy and the usage flags in \p attributes.
450 * - If both allow the same algorithm or wildcard-based
451 * algorithm policy, the resulting key has the same algorithm policy.
452 * - If either of the policies allows an algorithm and the other policy
453 * allows a wildcard-based algorithm policy that includes this algorithm,
454 * the resulting key allows the same algorithm.
455 * - If the policies do not allow any algorithm in common, this function
456 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
457 *
458 * The effect of this function on implementation-defined attributes is
459 * implementation-defined.
460 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200461 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200462 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200463 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200464 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100465 * \param[in] attributes The attributes for the new key.
466 * They are used as follows:
467 * - The key type and size may be 0. If either is
468 * nonzero, it must match the corresponding
469 * attribute of the source key.
470 * - The key location (the lifetime and, for
471 * persistent keys, the key identifier) is
472 * used directly.
473 * - The policy constraints (usage flags and
474 * algorithm policy) are combined from
475 * the source key and \p attributes so that
476 * both sets of restrictions apply, as
477 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200478 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200479 * key. For persistent keys, this is the key
480 * identifier defined in \p attributes.
481 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100482 *
Gilles Peskineed733552023-02-14 19:21:09 +0100483 * \retval #PSA_SUCCESS \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100484 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200485 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100486 * \retval #PSA_ERROR_ALREADY_EXISTS
487 * This is an attempt to create a persistent key, and there is
488 * already a persistent key with the given identifier.
489 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500490 * The lifetime or identifier in \p attributes are invalid, or
491 * the policy constraints on the source and specified in
492 * \p attributes are incompatible, or
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100493 * \p attributes specifies a key type or key size
494 * which does not match the attributes of the source key.
495 * \retval #PSA_ERROR_NOT_PERMITTED
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500496 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
497 * the source key is not exportable and its lifetime does not
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100498 * allow copying it to the target's lifetime.
Gilles Peskineed733552023-02-14 19:21:09 +0100499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
500 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
502 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
503 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
504 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
505 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
506 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100507 * \retval #PSA_ERROR_BAD_STATE
508 * The library has not been previously initialized by psa_crypto_init().
509 * It is implementation-dependent whether a failure to initialize
510 * results in this error code.
511 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200512psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100513 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200514 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100515
516
517/**
518 * \brief Destroy a key.
519 *
520 * This function destroys a key from both volatile
521 * memory and, if applicable, non-volatile storage. Implementations shall
Tom Cosgrove1797b052022-12-04 17:19:59 +0000522 * make a best effort to ensure that the key material cannot be recovered.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100523 *
524 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200525 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100526 *
527 * If a key is currently in use in a multipart operation, then destroying the
528 * key will cause the multipart operation to fail.
529 *
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
560 * results in this error code.
561 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200562psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100563
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100564/**@}*/
565
566/** \defgroup import_export Key import and export
567 * @{
568 */
569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570/**
571 * \brief Import a key in binary format.
572 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100573 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100574 * documentation of psa_export_public_key() for the format of public keys
575 * and to the documentation of psa_export_key() for the format for
576 * other key types.
577 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200578 * The key data determines the key size. The attributes may optionally
579 * specify a key size; in this case it must match the size determined
580 * from the key data. A key size of 0 in \p attributes indicates that
581 * the key size is solely determined by the key data.
582 *
583 * Implementations must reject an attempt to import a key of size 0.
584 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100585 * This specification supports a single format for each key type.
586 * Implementations may support other formats as long as the standard
587 * format is supported. Implementations that support other formats
588 * should ensure that the formats are clearly unambiguous so as to
589 * minimize the risk that an invalid input is accidentally interpreted
590 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 *
Gilles Peskine20628592019-04-19 19:29:50 +0200592 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200593 * The key size is always determined from the
594 * \p data buffer.
595 * If the key size in \p attributes is nonzero,
596 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200597 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200598 * For persistent keys, this is the key identifier
599 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200600 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100601 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200602 * buffer is interpreted according to the type declared
603 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200604 * All implementations must support at least the format
605 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100606 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * the chosen type. Implementations may allow other
608 * formats, but should be conservative: implementations
609 * should err on the side of rejecting content if it
610 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200611 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100612 *
Gilles Peskine28538492018-07-11 17:34:00 +0200613 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100614 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100615 * If the key is persistent, the key material and the key's metadata
616 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200617 * \retval #PSA_ERROR_ALREADY_EXISTS
618 * This is an attempt to create a persistent key, and there is
619 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200620 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200621 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200622 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200623 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500624 * The key attributes, as a whole, are invalid, or
625 * the key data is not correctly formatted, or
626 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200627 * of the key data.
Gilles Peskineed733552023-02-14 19:21:09 +0100628 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
629 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
631 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
632 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
633 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
634 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
635 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300636 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300637 * The library has not been previously initialized by psa_crypto_init().
638 * It is implementation-dependent whether a failure to initialize
639 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200641psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200643 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200644 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100646
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647
648/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 * \brief Export a key in binary format.
650 *
651 * The output of this function can be passed to psa_import_key() to
652 * create an equivalent object.
653 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100654 * If the implementation of psa_import_key() supports other formats
655 * beyond the format specified here, the output from psa_export_key()
656 * must use the representation specified here, not the original
657 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100659 * For standard key types, the output format is as follows:
660 *
661 * - For symmetric keys (including MAC keys), the format is the
662 * raw bytes of the key.
663 * - For DES, the key data consists of 8 bytes. The parity bits must be
664 * correct.
665 * - For Triple-DES, the format is the concatenation of the
666 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200667 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200668 * is the non-encrypted DER encoding of the representation defined by
669 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
670 * ```
671 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200672 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200673 * modulus INTEGER, -- n
674 * publicExponent INTEGER, -- e
675 * privateExponent INTEGER, -- d
676 * prime1 INTEGER, -- p
677 * prime2 INTEGER, -- q
678 * exponent1 INTEGER, -- d mod (p-1)
679 * exponent2 INTEGER, -- d mod (q-1)
680 * coefficient INTEGER, -- (inverse of q) mod p
681 * }
682 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200683 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200684 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100685 * a representation of the private value as a `ceiling(m/8)`-byte string
686 * where `m` is the bit size associated with the curve, i.e. the bit size
687 * of the order of the curve's coordinate field. This byte string is
688 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100689 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
690 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
691 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200692 * For Weierstrass curves, this is the content of the `privateKey` field of
693 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
694 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100695 * For twisted Edwards curves, the private key is as defined by RFC 8032
696 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200697 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200698 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000699 * format is the representation of the private key `x` as a big-endian byte
700 * string. The length of the byte string is the private key size in bytes
701 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200702 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
703 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100704 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200705 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
706 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200707 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200708 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200709 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200710 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200711 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200712 * \param[out] data_length On success, the number of bytes
713 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714 *
Gilles Peskineed733552023-02-14 19:21:09 +0100715 * \retval #PSA_SUCCESS \emptydescription
716 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200717 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200718 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Gilles Peskineed733552023-02-14 19:21:09 +0100719 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200720 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
721 * The size of the \p data buffer is too small. You can determine a
722 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100723 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200724 * where \c type is the key type
725 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100726 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
727 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
728 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
729 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300731 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300732 * The library has not been previously initialized by psa_crypto_init().
733 * It is implementation-dependent whether a failure to initialize
734 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200736psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737 uint8_t *data,
738 size_t data_size,
739 size_t *data_length);
740
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100741/**
742 * \brief Export a public key or the public part of a key pair in binary format.
743 *
744 * The output of this function can be passed to psa_import_key() to
745 * create an object that is equivalent to the public key.
746 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000747 * This specification supports a single format for each key type.
748 * Implementations may support other formats as long as the standard
749 * format is supported. Implementations that support other formats
750 * should ensure that the formats are clearly unambiguous so as to
751 * minimize the risk that an invalid input is accidentally interpreted
752 * according to a different format.
753 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000754 * For standard key types, the output format is as follows:
755 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
756 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
757 * ```
758 * RSAPublicKey ::= SEQUENCE {
759 * modulus INTEGER, -- n
760 * publicExponent INTEGER } -- e
761 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100762 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200763 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100764 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
765 * by RFC 8032
766 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
767 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000768 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
769 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
770 * Let `m` be the bit size associated with the curve, i.e. the bit size of
771 * `q` for a curve over `F_q`. The representation consists of:
772 * - The byte 0x04;
773 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
774 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200775 * - For Diffie-Hellman key exchange public keys (key types for which
776 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000777 * the format is the representation of the public key `y = g^x mod p` as a
778 * big-endian byte string. The length of the byte string is the length of the
779 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100780 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200781 * Exporting a public key object or the public part of a key pair is
782 * always permitted, regardless of the key's usage flags.
783 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200784 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200785 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200786 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200787 * \param[out] data_length On success, the number of bytes
788 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100789 *
Gilles Peskineed733552023-02-14 19:21:09 +0100790 * \retval #PSA_SUCCESS \emptydescription
791 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200792 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200793 * The key is neither a public key nor a key pair.
Gilles Peskineed733552023-02-14 19:21:09 +0100794 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200795 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
796 * The size of the \p data buffer is too small. You can determine a
797 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100798 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200799 * where \c type is the key type
800 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
802 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
803 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
804 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
805 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300806 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300807 * The library has not been previously initialized by psa_crypto_init().
808 * It is implementation-dependent whether a failure to initialize
809 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100810 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200811psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100812 uint8_t *data,
813 size_t data_size,
814 size_t *data_length);
815
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100816
Gilles Peskine20035e32018-02-03 22:44:14 +0100817
818/**@}*/
819
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100820/** \defgroup hash Message digests
821 * @{
822 */
823
Gilles Peskine69647a42019-01-14 20:18:12 +0100824/** Calculate the hash (digest) of a message.
825 *
826 * \note To verify the hash of a message against an
827 * expected value, use psa_hash_compare() instead.
828 *
829 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
830 * such that #PSA_ALG_IS_HASH(\p alg) is true).
831 * \param[in] input Buffer containing the message to hash.
832 * \param input_length Size of the \p input buffer in bytes.
833 * \param[out] hash Buffer where the hash is to be written.
834 * \param hash_size Size of the \p hash buffer in bytes.
835 * \param[out] hash_length On success, the number of bytes
836 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100837 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100838 *
839 * \retval #PSA_SUCCESS
840 * Success.
841 * \retval #PSA_ERROR_NOT_SUPPORTED
842 * \p alg is not supported or is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100843 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100844 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
845 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +0100846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
848 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
849 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100850 * \retval #PSA_ERROR_BAD_STATE
851 * The library has not been previously initialized by psa_crypto_init().
852 * It is implementation-dependent whether a failure to initialize
853 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100854 */
855psa_status_t psa_hash_compute(psa_algorithm_t alg,
856 const uint8_t *input,
857 size_t input_length,
858 uint8_t *hash,
859 size_t hash_size,
860 size_t *hash_length);
861
862/** Calculate the hash (digest) of a message and compare it with a
863 * reference value.
864 *
865 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
866 * such that #PSA_ALG_IS_HASH(\p alg) is true).
867 * \param[in] input Buffer containing the message to hash.
868 * \param input_length Size of the \p input buffer in bytes.
869 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100870 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100871 *
872 * \retval #PSA_SUCCESS
873 * The expected hash is identical to the actual hash of the input.
874 * \retval #PSA_ERROR_INVALID_SIGNATURE
875 * The hash of the message was calculated successfully, but it
876 * differs from the expected hash.
877 * \retval #PSA_ERROR_NOT_SUPPORTED
878 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100879 * \retval #PSA_ERROR_INVALID_ARGUMENT
880 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskineed733552023-02-14 19:21:09 +0100881 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
882 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
883 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
884 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100885 * \retval #PSA_ERROR_BAD_STATE
886 * The library has not been previously initialized by psa_crypto_init().
887 * It is implementation-dependent whether a failure to initialize
888 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100889 */
890psa_status_t psa_hash_compare(psa_algorithm_t alg,
891 const uint8_t *input,
892 size_t input_length,
893 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100894 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100895
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896/** The type of the state data structure for multipart hash operations.
897 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000898 * Before calling any function on a hash operation object, the application must
899 * initialize it by any of the following means:
900 * - Set the structure to all-bits-zero, for example:
901 * \code
902 * psa_hash_operation_t operation;
903 * memset(&operation, 0, sizeof(operation));
904 * \endcode
905 * - Initialize the structure to logical zero values, for example:
906 * \code
907 * psa_hash_operation_t operation = {0};
908 * \endcode
909 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
910 * for example:
911 * \code
912 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
913 * \endcode
914 * - Assign the result of the function psa_hash_operation_init()
915 * to the structure, for example:
916 * \code
917 * psa_hash_operation_t operation;
918 * operation = psa_hash_operation_init();
919 * \endcode
920 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100921 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +0100922 * make any assumptions about the content of this structure.
923 * Implementation details can change in future versions without notice. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100924typedef struct psa_hash_operation_s psa_hash_operation_t;
925
Jaeden Amero6a25b412019-01-04 11:47:44 +0000926/** \def PSA_HASH_OPERATION_INIT
927 *
928 * This macro returns a suitable initializer for a hash operation object
929 * of type #psa_hash_operation_t.
930 */
Jaeden Amero6a25b412019-01-04 11:47:44 +0000931
932/** Return an initial value for a hash operation object.
933 */
934static psa_hash_operation_t psa_hash_operation_init(void);
935
Gilles Peskinef45adda2019-01-14 18:29:18 +0100936/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937 *
938 * The sequence of operations to calculate a hash (message digest)
939 * is as follows:
940 * -# Allocate an operation object which will be passed to all the functions
941 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000942 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100943 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200944 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100945 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 * of the message each time. The hash that is calculated is the hash
947 * of the concatenation of these messages in order.
948 * -# To calculate the hash, call psa_hash_finish().
949 * To compare the hash with an expected value, call psa_hash_verify().
950 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100951 * If an error occurs at any step after a call to psa_hash_setup(), the
952 * operation will need to be reset by a call to psa_hash_abort(). The
953 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000954 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200956 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100957 * eventually terminate the operation. The following events terminate an
958 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100959 * - A successful call to psa_hash_finish() or psa_hash_verify().
960 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000962 * \param[in,out] operation The operation object to set up. It must have
963 * been initialized as per the documentation for
964 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200965 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
966 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Gilles Peskine28538492018-07-11 17:34:00 +0200968 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100969 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200970 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100971 * \p alg is not a supported hash algorithm.
972 * \retval #PSA_ERROR_INVALID_ARGUMENT
973 * \p alg is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100974 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
976 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
977 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100978 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500979 * The operation state is not valid (it must be inactive), or
980 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100981 * It is implementation-dependent whether a failure to initialize
982 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200984psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100985 psa_algorithm_t alg);
986
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987/** Add a message fragment to a multipart hash operation.
988 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200989 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100991 * If this function returns an error status, the operation enters an error
992 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200994 * \param[in,out] operation Active hash operation.
995 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200996 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 *
Gilles Peskine28538492018-07-11 17:34:00 +0200998 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001000 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1001 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1002 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1003 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001004 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001005 * The operation state is not valid (it must be active), or
1006 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001007 * It is implementation-dependent whether a failure to initialize
1008 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001010psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1011 const uint8_t *input,
1012 size_t input_length);
1013
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014/** Finish the calculation of the hash of a message.
1015 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001016 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 * This function calculates the hash of the message formed by concatenating
1018 * the inputs passed to preceding calls to psa_hash_update().
1019 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001020 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001021 * If this function returns an error status, the operation enters an error
1022 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 *
1024 * \warning Applications should not call this function if they expect
1025 * a specific value for the hash. Call psa_hash_verify() instead.
1026 * Beware that comparing integrity or authenticity data such as
1027 * hash values with a function such as \c memcmp is risky
1028 * because the time taken by the comparison may leak information
1029 * about the hashed data which could allow an attacker to guess
1030 * a valid hash and thereby bypass security controls.
1031 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001032 * \param[in,out] operation Active hash operation.
1033 * \param[out] hash Buffer where the hash is to be written.
1034 * \param hash_size Size of the \p hash buffer in bytes.
1035 * \param[out] hash_length On success, the number of bytes
1036 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001037 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001038 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 *
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001043 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001044 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +01001046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1048 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1049 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001050 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001051 * The operation state is not valid (it must be active), or
1052 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001053 * It is implementation-dependent whether a failure to initialize
1054 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1057 uint8_t *hash,
1058 size_t hash_size,
1059 size_t *hash_length);
1060
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061/** Finish the calculation of the hash of a message and compare it with
1062 * an expected value.
1063 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001064 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 * This function calculates the hash of the message formed by concatenating
1066 * the inputs passed to preceding calls to psa_hash_update(). It then
1067 * compares the calculated hash with the expected hash passed as a
1068 * parameter to this function.
1069 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001070 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001071 * If this function returns an error status, the operation enters an error
1072 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 *
Gilles Peskine19067982018-03-20 17:54:53 +01001074 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * comparison between the actual hash and the expected hash is performed
1076 * in constant time.
1077 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001078 * \param[in,out] operation Active hash operation.
1079 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001080 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 *
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 * The hash of the message was calculated successfully, but it
1086 * differs from the expected hash.
Gilles Peskineed733552023-02-14 19:21:09 +01001087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1089 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1090 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001091 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001092 * The operation state is not valid (it must be active), or
1093 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001094 * It is implementation-dependent whether a failure to initialize
1095 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001097psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1098 const uint8_t *hash,
1099 size_t hash_length);
1100
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101/** Abort a hash operation.
1102 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001104 * \p operation structure itself. Once aborted, the operation object
1105 * can be reused for another operation by calling
1106 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001108 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001109 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001111 * In particular, calling psa_hash_abort() after the operation has been
1112 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1113 * psa_hash_verify() is safe and has no effect.
1114 *
1115 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineed733552023-02-14 19:21:09 +01001117 * \retval #PSA_SUCCESS \emptydescription
1118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1120 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001121 * \retval #PSA_ERROR_BAD_STATE
1122 * The library has not been previously initialized by psa_crypto_init().
1123 * It is implementation-dependent whether a failure to initialize
1124 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 */
1126psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001127
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001128/** Clone a hash operation.
1129 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001130 * This function copies the state of an ongoing hash operation to
1131 * a new operation object. In other words, this function is equivalent
1132 * to calling psa_hash_setup() on \p target_operation with the same
1133 * algorithm that \p source_operation was set up for, then
1134 * psa_hash_update() on \p target_operation with the same input that
1135 * that was passed to \p source_operation. After this function returns, the
1136 * two objects are independent, i.e. subsequent calls involving one of
1137 * the objects do not affect the other object.
1138 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001139 * \param[in] source_operation The active hash operation to clone.
1140 * \param[in,out] target_operation The operation object to set up.
1141 * It must be initialized but not active.
1142 *
Gilles Peskineed733552023-02-14 19:21:09 +01001143 * \retval #PSA_SUCCESS \emptydescription
1144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1145 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1146 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001148 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001149 * The \p source_operation state is not valid (it must be active), or
1150 * the \p target_operation state is not valid (it must be inactive), or
1151 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001152 * It is implementation-dependent whether a failure to initialize
1153 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001154 */
1155psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1156 psa_hash_operation_t *target_operation);
1157
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001158/**@}*/
1159
Gilles Peskine8c9def32018-02-08 10:02:12 +01001160/** \defgroup MAC Message authentication codes
1161 * @{
1162 */
1163
Gilles Peskine69647a42019-01-14 20:18:12 +01001164/** Calculate the MAC (message authentication code) of a message.
1165 *
1166 * \note To verify the MAC of a message against an
1167 * expected value, use psa_mac_verify() instead.
1168 * Beware that comparing integrity or authenticity data such as
1169 * MAC values with a function such as \c memcmp is risky
1170 * because the time taken by the comparison may leak information
1171 * about the MAC value which could allow an attacker to guess
1172 * a valid MAC and thereby bypass security controls.
1173 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001174 * \param key Identifier of the key to use for the operation. It
1175 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001176 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001177 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001178 * \param[in] input Buffer containing the input message.
1179 * \param input_length Size of the \p input buffer in bytes.
1180 * \param[out] mac Buffer where the MAC value is to be written.
1181 * \param mac_size Size of the \p mac buffer in bytes.
1182 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001183 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001184 *
1185 * \retval #PSA_SUCCESS
1186 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001187 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1188 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001190 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001191 * \retval #PSA_ERROR_NOT_SUPPORTED
1192 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001193 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1194 * \p mac_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +01001195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1197 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1198 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001199 * \retval #PSA_ERROR_STORAGE_FAILURE
1200 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001201 * \retval #PSA_ERROR_BAD_STATE
1202 * The library has not been previously initialized by psa_crypto_init().
1203 * It is implementation-dependent whether a failure to initialize
1204 * results in this error code.
1205 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001206psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 psa_algorithm_t alg,
1208 const uint8_t *input,
1209 size_t input_length,
1210 uint8_t *mac,
1211 size_t mac_size,
1212 size_t *mac_length);
1213
1214/** Calculate the MAC of a message and compare it with a reference value.
1215 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001216 * \param key Identifier of the key to use for the operation. It
1217 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001218 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001219 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001220 * \param[in] input Buffer containing the input message.
1221 * \param input_length Size of the \p input buffer in bytes.
1222 * \param[out] mac Buffer containing the expected MAC value.
1223 * \param mac_length Size of the \p mac buffer in bytes.
1224 *
1225 * \retval #PSA_SUCCESS
1226 * The expected MAC is identical to the actual MAC of the input.
1227 * \retval #PSA_ERROR_INVALID_SIGNATURE
1228 * The MAC of the message was calculated successfully, but it
1229 * differs from the expected value.
Gilles Peskineed733552023-02-14 19:21:09 +01001230 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1231 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001233 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001234 * \retval #PSA_ERROR_NOT_SUPPORTED
1235 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001236 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1238 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1239 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001240 * \retval #PSA_ERROR_STORAGE_FAILURE
1241 * The key could not be retrieved from storage.
1242 * \retval #PSA_ERROR_BAD_STATE
1243 * The library has not been previously initialized by psa_crypto_init().
1244 * It is implementation-dependent whether a failure to initialize
1245 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001246 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001247psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001248 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 const uint8_t *input,
1250 size_t input_length,
1251 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001252 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001253
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001254/** The type of the state data structure for multipart MAC operations.
1255 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001256 * Before calling any function on a MAC operation object, the application must
1257 * initialize it by any of the following means:
1258 * - Set the structure to all-bits-zero, for example:
1259 * \code
1260 * psa_mac_operation_t operation;
1261 * memset(&operation, 0, sizeof(operation));
1262 * \endcode
1263 * - Initialize the structure to logical zero values, for example:
1264 * \code
1265 * psa_mac_operation_t operation = {0};
1266 * \endcode
1267 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1268 * for example:
1269 * \code
1270 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1271 * \endcode
1272 * - Assign the result of the function psa_mac_operation_init()
1273 * to the structure, for example:
1274 * \code
1275 * psa_mac_operation_t operation;
1276 * operation = psa_mac_operation_init();
1277 * \endcode
1278 *
Janos Follath2ba60792021-04-28 09:37:34 +01001279 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001280 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001281 * make any assumptions about the content of this structure.
1282 * Implementation details can change in future versions without notice. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283typedef struct psa_mac_operation_s psa_mac_operation_t;
1284
Jaeden Amero769ce272019-01-04 11:48:03 +00001285/** \def PSA_MAC_OPERATION_INIT
1286 *
1287 * This macro returns a suitable initializer for a MAC operation object of type
1288 * #psa_mac_operation_t.
1289 */
Jaeden Amero769ce272019-01-04 11:48:03 +00001290
1291/** Return an initial value for a MAC operation object.
1292 */
1293static psa_mac_operation_t psa_mac_operation_init(void);
1294
Gilles Peskinef45adda2019-01-14 18:29:18 +01001295/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001296 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001297 * This function sets up the calculation of the MAC
1298 * (message authentication code) of a byte string.
1299 * To verify the MAC of a message against an
1300 * expected value, use psa_mac_verify_setup() instead.
1301 *
1302 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001303 * -# Allocate an operation object which will be passed to all the functions
1304 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001305 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001306 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001307 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1309 * of the message each time. The MAC that is calculated is the MAC
1310 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001311 * -# At the end of the message, call psa_mac_sign_finish() to finish
1312 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001314 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1315 * operation will need to be reset by a call to psa_mac_abort(). The
1316 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001317 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * After a successful call to psa_mac_sign_setup(), the application must
1320 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001321 * - A successful call to psa_mac_sign_finish().
1322 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * \param[in,out] operation The operation object to set up. It must have
1325 * been initialized as per the documentation for
1326 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001327 * \param key Identifier of the key to use for the operation. It
1328 * must remain valid until the operation terminates.
1329 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001330 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001331 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
Gilles Peskine28538492018-07-11 17:34:00 +02001333 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001335 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1336 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001337 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001338 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001339 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001340 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1343 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1344 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001345 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001346 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001347 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001348 * The operation state is not valid (it must be inactive), or
1349 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001350 * It is implementation-dependent whether a failure to initialize
1351 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001353psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001354 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355 psa_algorithm_t alg);
1356
Gilles Peskinef45adda2019-01-14 18:29:18 +01001357/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001358 *
1359 * This function sets up the verification of the MAC
1360 * (message authentication code) of a byte string against an expected value.
1361 *
1362 * The sequence of operations to verify a MAC is as follows:
1363 * -# Allocate an operation object which will be passed to all the functions
1364 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001365 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001366 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1369 * of the message each time. The MAC that is calculated is the MAC
1370 * of the concatenation of these messages in order.
1371 * -# At the end of the message, call psa_mac_verify_finish() to finish
1372 * calculating the actual MAC of the message and verify it against
1373 * the expected value.
1374 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001375 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1376 * operation will need to be reset by a call to psa_mac_abort(). The
1377 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001378 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379 *
1380 * After a successful call to psa_mac_verify_setup(), the application must
1381 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001382 * - A successful call to psa_mac_verify_finish().
1383 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001385 * \param[in,out] operation The operation object to set up. It must have
1386 * been initialized as per the documentation for
1387 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001388 * \param key Identifier of the key to use for the operation. It
1389 * must remain valid until the operation terminates.
1390 * It must allow the usage
1391 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001392 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1393 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001394 *
Gilles Peskine28538492018-07-11 17:34:00 +02001395 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001397 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1398 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1404 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1405 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1406 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001407 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001408 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001409 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001410 * The operation state is not valid (it must be inactive), or
1411 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001412 * It is implementation-dependent whether a failure to initialize
1413 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 */
1415psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001416 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001418
Gilles Peskinedcd14942018-07-12 00:30:52 +02001419/** Add a message fragment to a multipart MAC operation.
1420 *
1421 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1422 * before calling this function.
1423 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001424 * If this function returns an error status, the operation enters an error
1425 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001427 * \param[in,out] operation Active MAC operation.
1428 * \param[in] input Buffer containing the message fragment to add to
1429 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001430 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001431 *
1432 * \retval #PSA_SUCCESS
1433 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001434 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1435 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1436 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1437 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1438 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001439 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001440 * The operation state is not valid (it must be active), or
1441 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001442 * It is implementation-dependent whether a failure to initialize
1443 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001444 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001445psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1446 const uint8_t *input,
1447 size_t input_length);
1448
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449/** Finish the calculation of the MAC of a message.
1450 *
1451 * The application must call psa_mac_sign_setup() before calling this function.
1452 * This function calculates the MAC of the message formed by concatenating
1453 * the inputs passed to preceding calls to psa_mac_update().
1454 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001455 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001456 * If this function returns an error status, the operation enters an error
1457 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001458 *
1459 * \warning Applications should not call this function if they expect
1460 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1461 * Beware that comparing integrity or authenticity data such as
1462 * MAC values with a function such as \c memcmp is risky
1463 * because the time taken by the comparison may leak information
1464 * about the MAC value which could allow an attacker to guess
1465 * a valid MAC and thereby bypass security controls.
1466 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001467 * \param[in,out] operation Active MAC operation.
1468 * \param[out] mac Buffer where the MAC value is to be written.
1469 * \param mac_size Size of the \p mac buffer in bytes.
1470 * \param[out] mac_length On success, the number of bytes
1471 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001472 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001473 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001474 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001475 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 *
1477 * \retval #PSA_SUCCESS
1478 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001480 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001481 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskineed733552023-02-14 19:21:09 +01001482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1484 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1485 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1486 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001487 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001488 * The operation state is not valid (it must be an active mac sign
1489 * operation), or the library has not been previously initialized
1490 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001491 * It is implementation-dependent whether a failure to initialize
1492 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001494psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1495 uint8_t *mac,
1496 size_t mac_size,
1497 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499/** Finish the calculation of the MAC of a message and compare it with
1500 * an expected value.
1501 *
1502 * The application must call psa_mac_verify_setup() before calling this function.
1503 * This function calculates the MAC of the message formed by concatenating
1504 * the inputs passed to preceding calls to psa_mac_update(). It then
1505 * compares the calculated MAC with the expected MAC passed as a
1506 * parameter to this function.
1507 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001508 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001509 * If this function returns an error status, the operation enters an error
1510 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 *
1512 * \note Implementations shall make the best effort to ensure that the
1513 * comparison between the actual MAC and the expected MAC is performed
1514 * in constant time.
1515 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001516 * \param[in,out] operation Active MAC operation.
1517 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001518 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001519 *
1520 * \retval #PSA_SUCCESS
1521 * The expected MAC is identical to the actual MAC of the message.
1522 * \retval #PSA_ERROR_INVALID_SIGNATURE
1523 * The MAC of the message was calculated successfully, but it
1524 * differs from the expected MAC.
Gilles Peskineed733552023-02-14 19:21:09 +01001525 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1527 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1528 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1529 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001530 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001531 * The operation state is not valid (it must be an active mac verify
1532 * operation), or the library has not been previously initialized
1533 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001534 * It is implementation-dependent whether a failure to initialize
1535 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001537psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1538 const uint8_t *mac,
1539 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541/** Abort a MAC operation.
1542 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001544 * \p operation structure itself. Once aborted, the operation object
1545 * can be reused for another operation by calling
1546 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001548 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001549 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001551 * In particular, calling psa_mac_abort() after the operation has been
1552 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1553 * psa_mac_verify_finish() is safe and has no effect.
1554 *
1555 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 *
Gilles Peskineed733552023-02-14 19:21:09 +01001557 * \retval #PSA_SUCCESS \emptydescription
1558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1559 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1560 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001561 * \retval #PSA_ERROR_BAD_STATE
1562 * The library has not been previously initialized by psa_crypto_init().
1563 * It is implementation-dependent whether a failure to initialize
1564 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1567
1568/**@}*/
1569
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001570/** \defgroup cipher Symmetric ciphers
1571 * @{
1572 */
1573
Gilles Peskine69647a42019-01-14 20:18:12 +01001574/** Encrypt a message using a symmetric cipher.
1575 *
1576 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001577 * vector). Use the multipart operation interface with a
1578 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001579 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001580 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001581 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001582 * \param alg The cipher algorithm to compute
1583 * (\c PSA_ALG_XXX value such that
1584 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1585 * \param[in] input Buffer containing the message to encrypt.
1586 * \param input_length Size of the \p input buffer in bytes.
1587 * \param[out] output Buffer where the output is to be written.
1588 * The output contains the IV followed by
1589 * the ciphertext proper.
1590 * \param output_size Size of the \p output buffer in bytes.
1591 * \param[out] output_length On success, the number of bytes
1592 * that make up the output.
1593 *
1594 * \retval #PSA_SUCCESS
1595 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001596 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1597 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001598 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001599 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001600 * \retval #PSA_ERROR_NOT_SUPPORTED
1601 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001602 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1603 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1604 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1605 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1606 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1607 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001608 * \retval #PSA_ERROR_BAD_STATE
1609 * The library has not been previously initialized by psa_crypto_init().
1610 * It is implementation-dependent whether a failure to initialize
1611 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001612 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001613psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001614 psa_algorithm_t alg,
1615 const uint8_t *input,
1616 size_t input_length,
1617 uint8_t *output,
1618 size_t output_size,
1619 size_t *output_length);
1620
1621/** Decrypt a message using a symmetric cipher.
1622 *
1623 * This function decrypts a message encrypted with a symmetric cipher.
1624 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001625 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001626 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001627 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001628 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001629 * \param alg The cipher algorithm to compute
1630 * (\c PSA_ALG_XXX value such that
1631 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1632 * \param[in] input Buffer containing the message to decrypt.
1633 * This consists of the IV followed by the
1634 * ciphertext proper.
1635 * \param input_length Size of the \p input buffer in bytes.
1636 * \param[out] output Buffer where the plaintext is to be written.
1637 * \param output_size Size of the \p output buffer in bytes.
1638 * \param[out] output_length On success, the number of bytes
1639 * that make up the output.
1640 *
1641 * \retval #PSA_SUCCESS
1642 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001643 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1644 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001645 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001646 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001647 * \retval #PSA_ERROR_NOT_SUPPORTED
1648 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001649 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1650 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1652 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1653 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1654 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001655 * \retval #PSA_ERROR_BAD_STATE
1656 * The library has not been previously initialized by psa_crypto_init().
1657 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001658 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001659 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001660psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001661 psa_algorithm_t alg,
1662 const uint8_t *input,
1663 size_t input_length,
1664 uint8_t *output,
1665 size_t output_size,
1666 size_t *output_length);
1667
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001668/** The type of the state data structure for multipart cipher operations.
1669 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001670 * Before calling any function on a cipher operation object, the application
1671 * must initialize it by any of the following means:
1672 * - Set the structure to all-bits-zero, for example:
1673 * \code
1674 * psa_cipher_operation_t operation;
1675 * memset(&operation, 0, sizeof(operation));
1676 * \endcode
1677 * - Initialize the structure to logical zero values, for example:
1678 * \code
1679 * psa_cipher_operation_t operation = {0};
1680 * \endcode
1681 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1682 * for example:
1683 * \code
1684 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1685 * \endcode
1686 * - Assign the result of the function psa_cipher_operation_init()
1687 * to the structure, for example:
1688 * \code
1689 * psa_cipher_operation_t operation;
1690 * operation = psa_cipher_operation_init();
1691 * \endcode
1692 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001693 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001694 * make any assumptions about the content of this structure.
1695 * Implementation details can change in future versions without notice. */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001696typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1697
Jaeden Amero5bae2272019-01-04 11:48:27 +00001698/** \def PSA_CIPHER_OPERATION_INIT
1699 *
1700 * This macro returns a suitable initializer for a cipher operation object of
1701 * type #psa_cipher_operation_t.
1702 */
Jaeden Amero5bae2272019-01-04 11:48:27 +00001703
1704/** Return an initial value for a cipher operation object.
1705 */
1706static psa_cipher_operation_t psa_cipher_operation_init(void);
1707
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001708/** Set the key for a multipart symmetric encryption operation.
1709 *
1710 * The sequence of operations to encrypt a message with a symmetric cipher
1711 * is as follows:
1712 * -# Allocate an operation object which will be passed to all the functions
1713 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001714 * -# Initialize the operation object with one of the methods described in the
1715 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001716 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001717 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001718 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001720 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001721 * requires a specific IV value.
1722 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1723 * of the message each time.
1724 * -# Call psa_cipher_finish().
1725 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001726 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1727 * the operation will need to be reset by a call to psa_cipher_abort(). The
1728 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001729 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001730 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001731 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001732 * eventually terminate the operation. The following events terminate an
1733 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001734 * - A successful call to psa_cipher_finish().
1735 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001736 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001737 * \param[in,out] operation The operation object to set up. It must have
1738 * been initialized as per the documentation for
1739 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001740 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001741 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001742 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001743 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001744 * \param alg The cipher algorithm to compute
1745 * (\c PSA_ALG_XXX value such that
1746 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 *
Gilles Peskine28538492018-07-11 17:34:00 +02001748 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001750 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1751 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001752 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001753 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001754 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001755 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001756 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1758 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1759 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1760 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001761 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001762 * The operation state is not valid (it must be inactive), or
1763 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001764 * It is implementation-dependent whether a failure to initialize
1765 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001766 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001767psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001768 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001769 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770
1771/** Set the key for a multipart symmetric decryption operation.
1772 *
1773 * The sequence of operations to decrypt a message with a symmetric cipher
1774 * is as follows:
1775 * -# Allocate an operation object which will be passed to all the functions
1776 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001777 * -# Initialize the operation object with one of the methods described in the
1778 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001779 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001780 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001781 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 * decryption. If the IV is prepended to the ciphertext, you can call
1783 * psa_cipher_update() on a buffer containing the IV followed by the
1784 * beginning of the message.
1785 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1786 * of the message each time.
1787 * -# Call psa_cipher_finish().
1788 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001789 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1790 * the operation will need to be reset by a call to psa_cipher_abort(). The
1791 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001792 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001794 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001795 * eventually terminate the operation. The following events terminate an
1796 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001797 * - A successful call to psa_cipher_finish().
1798 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001800 * \param[in,out] operation The operation object to set up. It must have
1801 * been initialized as per the documentation for
1802 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001803 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001804 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001805 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001806 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001807 * \param alg The cipher algorithm to compute
1808 * (\c PSA_ALG_XXX value such that
1809 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001810 *
Gilles Peskine28538492018-07-11 17:34:00 +02001811 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001813 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1814 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001815 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001816 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001818 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1821 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1822 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1823 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001824 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001825 * The operation state is not valid (it must be inactive), or
1826 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001827 * It is implementation-dependent whether a failure to initialize
1828 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001830psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001831 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001832 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001833
Gilles Peskinedcd14942018-07-12 00:30:52 +02001834/** Generate an IV for a symmetric encryption operation.
1835 *
1836 * This function generates a random IV (initialization vector), nonce
1837 * or initial counter value for the encryption operation as appropriate
1838 * for the chosen algorithm, key type and key size.
1839 *
1840 * The application must call psa_cipher_encrypt_setup() before
1841 * calling this function.
1842 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001843 * If this function returns an error status, the operation enters an error
1844 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001846 * \param[in,out] operation Active cipher operation.
1847 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001848 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001849 * \param[out] iv_length On success, the number of bytes of the
1850 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 *
1852 * \retval #PSA_SUCCESS
1853 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001855 * The size of the \p iv buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1858 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1859 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1860 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001861 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001862 * The operation state is not valid (it must be active, with no IV set),
1863 * or the library has not been previously initialized
1864 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001865 * It is implementation-dependent whether a failure to initialize
1866 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001868psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001869 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001870 size_t iv_size,
1871 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001872
Gilles Peskinedcd14942018-07-12 00:30:52 +02001873/** Set the IV for a symmetric encryption or decryption operation.
1874 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001875 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 * or initial counter value for the encryption or decryption operation.
1877 *
1878 * The application must call psa_cipher_encrypt_setup() before
1879 * calling this function.
1880 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001881 * If this function returns an error status, the operation enters an error
1882 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 *
1884 * \note When encrypting, applications should use psa_cipher_generate_iv()
1885 * instead of this function, unless implementing a protocol that requires
1886 * a non-random IV.
1887 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001888 * \param[in,out] operation Active cipher operation.
1889 * \param[in] iv Buffer containing the IV to use.
1890 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001891 *
1892 * \retval #PSA_SUCCESS
1893 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001895 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 * or the chosen algorithm does not use an IV.
Gilles Peskineed733552023-02-14 19:21:09 +01001897 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1898 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1899 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1900 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1901 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001902 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001903 * The operation state is not valid (it must be an active cipher
1904 * encrypt operation, with no IV set), or the library has not been
1905 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001906 * It is implementation-dependent whether a failure to initialize
1907 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001909psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001910 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001911 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001912
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913/** Encrypt or decrypt a message fragment in an active cipher operation.
1914 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001915 * Before calling this function, you must:
1916 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1917 * The choice of setup function determines whether this function
1918 * encrypts or decrypts its input.
1919 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1920 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001922 * If this function returns an error status, the operation enters an error
1923 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001925 * \param[in,out] operation Active cipher operation.
1926 * \param[in] input Buffer containing the message fragment to
1927 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001928 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001930 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[out] output_length On success, the number of bytes
1932 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 *
1934 * \retval #PSA_SUCCESS
1935 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1937 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001938 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1939 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1940 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1941 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1942 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001943 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001944 * The operation state is not valid (it must be active, with an IV set
1945 * if required for the algorithm), or the library has not been
1946 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001947 * It is implementation-dependent whether a failure to initialize
1948 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001950psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1951 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001952 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001953 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001954 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001955 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001956
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957/** Finish encrypting or decrypting a message in a cipher operation.
1958 *
1959 * The application must call psa_cipher_encrypt_setup() or
1960 * psa_cipher_decrypt_setup() before calling this function. The choice
1961 * of setup function determines whether this function encrypts or
1962 * decrypts its input.
1963 *
1964 * This function finishes the encryption or decryption of the message
1965 * formed by concatenating the inputs passed to preceding calls to
1966 * psa_cipher_update().
1967 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001968 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001969 * If this function returns an error status, the operation enters an error
1970 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001972 * \param[in,out] operation Active cipher operation.
1973 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001974 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001975 * \param[out] output_length On success, the number of bytes
1976 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 *
1978 * \retval #PSA_SUCCESS
1979 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001980 * \retval #PSA_ERROR_INVALID_ARGUMENT
1981 * The total input size passed to this operation is not valid for
1982 * this particular algorithm. For example, the algorithm is a based
1983 * on block cipher and requires a whole number of blocks, but the
1984 * total input size is not a multiple of the block size.
1985 * \retval #PSA_ERROR_INVALID_PADDING
1986 * This is a decryption operation for an algorithm that includes
1987 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001988 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1989 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1992 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1993 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1994 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001995 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001996 * The operation state is not valid (it must be active, with an IV set
1997 * if required for the algorithm), or the library has not been
1998 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001999 * It is implementation-dependent whether a failure to initialize
2000 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002002psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002003 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002004 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002005 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002006
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007/** Abort a cipher operation.
2008 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002010 * \p operation structure itself. Once aborted, the operation object
2011 * can be reused for another operation by calling
2012 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002014 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002015 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002016 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002017 * In particular, calling psa_cipher_abort() after the operation has been
2018 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2019 * is safe and has no effect.
2020 *
2021 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 *
Gilles Peskineed733552023-02-14 19:21:09 +01002023 * \retval #PSA_SUCCESS \emptydescription
2024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2025 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2026 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002027 * \retval #PSA_ERROR_BAD_STATE
2028 * The library has not been previously initialized by psa_crypto_init().
2029 * It is implementation-dependent whether a failure to initialize
2030 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002031 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002032psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2033
2034/**@}*/
2035
Gilles Peskine3b555712018-03-03 21:27:57 +01002036/** \defgroup aead Authenticated encryption with associated data (AEAD)
2037 * @{
2038 */
2039
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002040/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002041 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002042 * \param key Identifier of the key to use for the
2043 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002044 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002045 * \param alg The AEAD algorithm to compute
2046 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002047 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002048 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002049 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002050 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * but not encrypted.
2052 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002053 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054 * encrypted.
2055 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * encrypted data. The additional data is not
2058 * part of this output. For algorithms where the
2059 * encrypted data and the authentication tag
2060 * are defined as separate outputs, the
2061 * authentication tag is appended to the
2062 * encrypted data.
2063 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002064 * This must be appropriate for the selected
2065 * algorithm and key:
2066 * - A sufficient output size is
2067 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2068 * \p alg, \p plaintext_length) where
2069 * \c key_type is the type of \p key.
2070 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2071 * plaintext_length) evaluates to the maximum
2072 * ciphertext size of any supported AEAD
2073 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002075 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002076 *
Gilles Peskine28538492018-07-11 17:34:00 +02002077 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002079 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2080 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002081 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002082 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002084 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002085 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002086 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002087 * \p ciphertext_size is too small.
2088 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2089 * \p plaintext_length) or
2090 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2091 * determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002092 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2093 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2094 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2095 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002096 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002097 * The library has not been previously initialized by psa_crypto_init().
2098 * It is implementation-dependent whether a failure to initialize
2099 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002100 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002101psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002102 psa_algorithm_t alg,
2103 const uint8_t *nonce,
2104 size_t nonce_length,
2105 const uint8_t *additional_data,
2106 size_t additional_data_length,
2107 const uint8_t *plaintext,
2108 size_t plaintext_length,
2109 uint8_t *ciphertext,
2110 size_t ciphertext_size,
2111 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002112
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002113/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002114 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002115 * \param key Identifier of the key to use for the
2116 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002117 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002118 * \param alg The AEAD algorithm to compute
2119 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002120 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002121 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002122 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002123 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * but not encrypted.
2125 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002126 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002127 * encrypted. For algorithms where the
2128 * encrypted data and the authentication tag
2129 * are defined as separate inputs, the buffer
2130 * must contain the encrypted data followed
2131 * by the authentication tag.
2132 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002134 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002135 * This must be appropriate for the selected
2136 * algorithm and key:
2137 * - A sufficient output size is
2138 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2139 * \p alg, \p ciphertext_length) where
2140 * \c key_type is the type of \p key.
2141 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2142 * ciphertext_length) evaluates to the maximum
2143 * plaintext size of any supported AEAD
2144 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002145 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002146 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002147 *
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002149 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002150 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002151 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002152 * The ciphertext is not authentic.
Gilles Peskineed733552023-02-14 19:21:09 +01002153 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002154 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002155 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002156 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002157 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002158 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002159 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002160 * \p plaintext_size is too small.
2161 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2162 * \p ciphertext_length) or
2163 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2164 * to determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2166 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2167 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2168 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002169 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002170 * The library has not been previously initialized by psa_crypto_init().
2171 * It is implementation-dependent whether a failure to initialize
2172 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002173 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002174psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002175 psa_algorithm_t alg,
2176 const uint8_t *nonce,
2177 size_t nonce_length,
2178 const uint8_t *additional_data,
2179 size_t additional_data_length,
2180 const uint8_t *ciphertext,
2181 size_t ciphertext_length,
2182 uint8_t *plaintext,
2183 size_t plaintext_size,
2184 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002185
Gilles Peskine30a9e412019-01-14 18:36:12 +01002186/** The type of the state data structure for multipart AEAD operations.
2187 *
2188 * Before calling any function on an AEAD operation object, the application
2189 * must initialize it by any of the following means:
2190 * - Set the structure to all-bits-zero, for example:
2191 * \code
2192 * psa_aead_operation_t operation;
2193 * memset(&operation, 0, sizeof(operation));
2194 * \endcode
2195 * - Initialize the structure to logical zero values, for example:
2196 * \code
2197 * psa_aead_operation_t operation = {0};
2198 * \endcode
2199 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2200 * for example:
2201 * \code
2202 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2203 * \endcode
2204 * - Assign the result of the function psa_aead_operation_init()
2205 * to the structure, for example:
2206 * \code
2207 * psa_aead_operation_t operation;
2208 * operation = psa_aead_operation_init();
2209 * \endcode
2210 *
2211 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01002212 * make any assumptions about the content of this structure.
2213 * Implementation details can change in future versions without notice. */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002214typedef struct psa_aead_operation_s psa_aead_operation_t;
2215
2216/** \def PSA_AEAD_OPERATION_INIT
2217 *
2218 * This macro returns a suitable initializer for an AEAD operation object of
2219 * type #psa_aead_operation_t.
2220 */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002221
2222/** Return an initial value for an AEAD operation object.
2223 */
2224static psa_aead_operation_t psa_aead_operation_init(void);
2225
2226/** Set the key for a multipart authenticated encryption operation.
2227 *
2228 * The sequence of operations to encrypt a message with authentication
2229 * is as follows:
2230 * -# Allocate an operation object which will be passed to all the functions
2231 * listed here.
2232 * -# Initialize the operation object with one of the methods described in the
2233 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002234 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002235 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002236 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2237 * inputs to the subsequent calls to psa_aead_update_ad() and
2238 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2239 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002240 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2241 * generate or set the nonce. You should use
2242 * psa_aead_generate_nonce() unless the protocol you are implementing
2243 * requires a specific nonce value.
2244 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2245 * of the non-encrypted additional authenticated data each time.
2246 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002247 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002248 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002249 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002250 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2251 * the operation will need to be reset by a call to psa_aead_abort(). The
2252 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002253 * has been initialized.
2254 *
2255 * After a successful call to psa_aead_encrypt_setup(), the application must
2256 * eventually terminate the operation. The following events terminate an
2257 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002258 * - A successful call to psa_aead_finish().
2259 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 *
2261 * \param[in,out] operation The operation object to set up. It must have
2262 * been initialized as per the documentation for
2263 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002264 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002266 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002267 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002268 * \param alg The AEAD algorithm to compute
2269 * (\c PSA_ALG_XXX value such that
2270 * #PSA_ALG_IS_AEAD(\p alg) is true).
2271 *
2272 * \retval #PSA_SUCCESS
2273 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002274 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002275 * The operation state is not valid (it must be inactive), or
2276 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskineed733552023-02-14 19:21:09 +01002277 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2278 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002279 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002280 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * \retval #PSA_ERROR_NOT_SUPPORTED
2282 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002283 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2284 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2285 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2286 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002287 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002288 * The library has not been previously initialized by psa_crypto_init().
2289 * It is implementation-dependent whether a failure to initialize
2290 * results in this error code.
2291 */
2292psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002293 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 psa_algorithm_t alg);
2295
2296/** Set the key for a multipart authenticated decryption operation.
2297 *
2298 * The sequence of operations to decrypt a message with authentication
2299 * is as follows:
2300 * -# Allocate an operation object which will be passed to all the functions
2301 * listed here.
2302 * -# Initialize the operation object with one of the methods described in the
2303 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002304 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002306 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2307 * inputs to the subsequent calls to psa_aead_update_ad() and
2308 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2309 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2311 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2312 * of the non-encrypted additional authenticated data each time.
2313 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002314 * of the ciphertext to decrypt each time.
2315 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002316 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002317 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2318 * the operation will need to be reset by a call to psa_aead_abort(). The
2319 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 * has been initialized.
2321 *
2322 * After a successful call to psa_aead_decrypt_setup(), the application must
2323 * eventually terminate the operation. The following events terminate an
2324 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002325 * - A successful call to psa_aead_verify().
2326 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 *
2328 * \param[in,out] operation The operation object to set up. It must have
2329 * been initialized as per the documentation for
2330 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002331 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002332 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002333 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002334 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002335 * \param alg The AEAD algorithm to compute
2336 * (\c PSA_ALG_XXX value such that
2337 * #PSA_ALG_IS_AEAD(\p alg) is true).
2338 *
2339 * \retval #PSA_SUCCESS
2340 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002341 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2342 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002343 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002344 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * \retval #PSA_ERROR_NOT_SUPPORTED
2346 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002347 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2348 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2349 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2350 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2351 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002352 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002353 * The operation state is not valid (it must be inactive), or the
2354 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002355 * It is implementation-dependent whether a failure to initialize
2356 * results in this error code.
2357 */
2358psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002359 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 psa_algorithm_t alg);
2361
2362/** Generate a random nonce for an authenticated encryption operation.
2363 *
2364 * This function generates a random nonce for the authenticated encryption
2365 * operation with an appropriate size for the chosen algorithm, key type
2366 * and key size.
2367 *
2368 * The application must call psa_aead_encrypt_setup() before
2369 * calling this function.
2370 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002371 * If this function returns an error status, the operation enters an error
2372 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002373 *
2374 * \param[in,out] operation Active AEAD operation.
2375 * \param[out] nonce Buffer where the generated nonce is to be
2376 * written.
2377 * \param nonce_size Size of the \p nonce buffer in bytes.
2378 * \param[out] nonce_length On success, the number of bytes of the
2379 * generated nonce.
2380 *
2381 * \retval #PSA_SUCCESS
2382 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002383 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2384 * The size of the \p nonce buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01002385 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2386 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2387 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2388 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2389 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002390 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002391 * The operation state is not valid (it must be an active aead encrypt
2392 * operation, with no nonce set), or the library has not been
2393 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002394 * It is implementation-dependent whether a failure to initialize
2395 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002396 */
2397psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002398 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 size_t nonce_size,
2400 size_t *nonce_length);
2401
2402/** Set the nonce for an authenticated encryption or decryption operation.
2403 *
2404 * This function sets the nonce for the authenticated
2405 * encryption or decryption operation.
2406 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002407 * The application must call psa_aead_encrypt_setup() or
2408 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002409 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002410 * If this function returns an error status, the operation enters an error
2411 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002412 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002413 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 * instead of this function, unless implementing a protocol that requires
2415 * a non-random IV.
2416 *
2417 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002418 * \param[in] nonce Buffer containing the nonce to use.
2419 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002420 *
2421 * \retval #PSA_SUCCESS
2422 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 * \retval #PSA_ERROR_INVALID_ARGUMENT
2424 * The size of \p nonce is not acceptable for the chosen algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002425 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2427 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2428 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2429 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002430 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002431 * The operation state is not valid (it must be active, with no nonce
2432 * set), or the library has not been previously initialized
2433 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002434 * It is implementation-dependent whether a failure to initialize
2435 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 */
2437psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002438 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002439 size_t nonce_length);
2440
Gilles Peskinebc59c852019-01-17 15:26:08 +01002441/** Declare the lengths of the message and additional data for AEAD.
2442 *
2443 * The application must call this function before calling
2444 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2445 * the operation requires it. If the algorithm does not require it,
2446 * calling this function is optional, but if this function is called
2447 * then the implementation must enforce the lengths.
2448 *
2449 * You may call this function before or after setting the nonce with
2450 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2451 *
2452 * - For #PSA_ALG_CCM, calling this function is required.
2453 * - For the other AEAD algorithms defined in this specification, calling
2454 * this function is not required.
2455 * - For vendor-defined algorithm, refer to the vendor documentation.
2456 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002457 * If this function returns an error status, the operation enters an error
2458 * state and must be aborted by calling psa_aead_abort().
2459 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002460 * \param[in,out] operation Active AEAD operation.
2461 * \param ad_length Size of the non-encrypted additional
2462 * authenticated data in bytes.
2463 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2464 *
2465 * \retval #PSA_SUCCESS
2466 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002467 * \retval #PSA_ERROR_INVALID_ARGUMENT
2468 * At least one of the lengths is not acceptable for the chosen
2469 * algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2472 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2473 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002474 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002475 * The operation state is not valid (it must be active, and
2476 * psa_aead_update_ad() and psa_aead_update() must not have been
2477 * called yet), or the library has not been previously initialized
2478 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002479 * It is implementation-dependent whether a failure to initialize
2480 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002481 */
2482psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2483 size_t ad_length,
2484 size_t plaintext_length);
2485
Gilles Peskine30a9e412019-01-14 18:36:12 +01002486/** Pass additional data to an active AEAD operation.
2487 *
2488 * Additional data is authenticated, but not encrypted.
2489 *
2490 * You may call this function multiple times to pass successive fragments
2491 * of the additional data. You may not call this function after passing
2492 * data to encrypt or decrypt with psa_aead_update().
2493 *
2494 * Before calling this function, you must:
2495 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2496 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2497 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002498 * If this function returns an error status, the operation enters an error
2499 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002500 *
2501 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2502 * there is no guarantee that the input is valid. Therefore, until
2503 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2504 * treat the input as untrusted and prepare to undo any action that
2505 * depends on the input if psa_aead_verify() returns an error status.
2506 *
2507 * \param[in,out] operation Active AEAD operation.
2508 * \param[in] input Buffer containing the fragment of
2509 * additional data.
2510 * \param input_length Size of the \p input buffer in bytes.
2511 *
2512 * \retval #PSA_SUCCESS
2513 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002514 * \retval #PSA_ERROR_INVALID_ARGUMENT
2515 * The total input length overflows the additional data length that
2516 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2519 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2520 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2521 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002522 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002523 * The operation state is not valid (it must be active, have a nonce
2524 * set, have lengths set if required by the algorithm, and
2525 * psa_aead_update() must not have been called yet), or the library
2526 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002527 * It is implementation-dependent whether a failure to initialize
2528 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002529 */
2530psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2531 const uint8_t *input,
2532 size_t input_length);
2533
2534/** Encrypt or decrypt a message fragment in an active AEAD operation.
2535 *
2536 * Before calling this function, you must:
2537 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2538 * The choice of setup function determines whether this function
2539 * encrypts or decrypts its input.
2540 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2541 * 3. Call psa_aead_update_ad() to pass all the additional data.
2542 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002543 * If this function returns an error status, the operation enters an error
2544 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002545 *
2546 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2547 * there is no guarantee that the input is valid. Therefore, until
2548 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2549 * - Do not use the output in any way other than storing it in a
2550 * confidential location. If you take any action that depends
2551 * on the tentative decrypted data, this action will need to be
2552 * undone if the input turns out not to be valid. Furthermore,
2553 * if an adversary can observe that this action took place
2554 * (for example through timing), they may be able to use this
2555 * fact as an oracle to decrypt any message encrypted with the
2556 * same key.
2557 * - In particular, do not copy the output anywhere but to a
2558 * memory or storage space that you have exclusive access to.
2559 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002560 * This function does not require the input to be aligned to any
2561 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002562 * a whole block at a time, it must consume all the input provided, but
2563 * it may delay the end of the corresponding output until a subsequent
2564 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2565 * provides sufficient input. The amount of data that can be delayed
2566 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002567 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002568 * \param[in,out] operation Active AEAD operation.
2569 * \param[in] input Buffer containing the message fragment to
2570 * encrypt or decrypt.
2571 * \param input_length Size of the \p input buffer in bytes.
2572 * \param[out] output Buffer where the output is to be written.
2573 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002574 * This must be appropriate for the selected
2575 * algorithm and key:
2576 * - A sufficient output size is
2577 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2578 * \c alg, \p input_length) where
2579 * \c key_type is the type of key and \c alg is
2580 * the algorithm that were used to set up the
2581 * operation.
2582 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2583 * input_length) evaluates to the maximum
2584 * output size of any supported AEAD
2585 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002586 * \param[out] output_length On success, the number of bytes
2587 * that make up the returned output.
2588 *
2589 * \retval #PSA_SUCCESS
2590 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002591 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2592 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002593 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2594 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2595 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002596 * \retval #PSA_ERROR_INVALID_ARGUMENT
2597 * The total length of input to psa_aead_update_ad() so far is
2598 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002599 * specified with psa_aead_set_lengths(), or
2600 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002601 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002602 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2603 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2604 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2605 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2606 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002607 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002608 * The operation state is not valid (it must be active, have a nonce
2609 * set, and have lengths set if required by the algorithm), or the
2610 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002611 * It is implementation-dependent whether a failure to initialize
2612 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002613 */
2614psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2615 const uint8_t *input,
2616 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002617 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002618 size_t output_size,
2619 size_t *output_length);
2620
2621/** Finish encrypting a message in an AEAD operation.
2622 *
2623 * The operation must have been set up with psa_aead_encrypt_setup().
2624 *
2625 * This function finishes the authentication of the additional data
2626 * formed by concatenating the inputs passed to preceding calls to
2627 * psa_aead_update_ad() with the plaintext formed by concatenating the
2628 * inputs passed to preceding calls to psa_aead_update().
2629 *
2630 * This function has two output buffers:
2631 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002632 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002633 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002634 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002635 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002636 * If this function returns an error status, the operation enters an error
2637 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 *
2639 * \param[in,out] operation Active AEAD operation.
2640 * \param[out] ciphertext Buffer where the last part of the ciphertext
2641 * is to be written.
2642 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002643 * This must be appropriate for the selected
2644 * algorithm and key:
2645 * - A sufficient output size is
2646 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2647 * \c alg) where \c key_type is the type of key
2648 * and \c alg is the algorithm that were used to
2649 * set up the operation.
2650 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2651 * the maximum output size of any supported AEAD
2652 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002653 * \param[out] ciphertext_length On success, the number of bytes of
2654 * returned ciphertext.
2655 * \param[out] tag Buffer where the authentication tag is
2656 * to be written.
2657 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002658 * This must be appropriate for the selected
2659 * algorithm and key:
2660 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2661 * key_type, \c key_bits, \c alg) where
2662 * \c key_type and \c key_bits are the type and
2663 * bit-size of the key, and \c alg is the
2664 * algorithm that were used in the call to
2665 * psa_aead_encrypt_setup().
2666 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2667 * maximum tag size of any supported AEAD
2668 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 * \param[out] tag_length On success, the number of bytes
2670 * that make up the returned tag.
2671 *
2672 * \retval #PSA_SUCCESS
2673 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002674 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002675 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002676 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2677 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2678 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2679 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2680 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002681 * \retval #PSA_ERROR_INVALID_ARGUMENT
2682 * The total length of input to psa_aead_update_ad() so far is
2683 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002684 * specified with psa_aead_set_lengths(), or
2685 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002686 * less than the plaintext length that was previously
2687 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002688 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2689 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2690 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2691 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2692 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002693 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002694 * The operation state is not valid (it must be an active encryption
2695 * operation with a nonce set), or the library has not been previously
2696 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002697 * It is implementation-dependent whether a failure to initialize
2698 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002699 */
2700psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002701 uint8_t *ciphertext,
2702 size_t ciphertext_size,
2703 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002704 uint8_t *tag,
2705 size_t tag_size,
2706 size_t *tag_length);
2707
2708/** Finish authenticating and decrypting a message in an AEAD operation.
2709 *
2710 * The operation must have been set up with psa_aead_decrypt_setup().
2711 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002712 * This function finishes the authenticated decryption of the message
2713 * components:
2714 *
2715 * - The additional data consisting of the concatenation of the inputs
2716 * passed to preceding calls to psa_aead_update_ad().
2717 * - The ciphertext consisting of the concatenation of the inputs passed to
2718 * preceding calls to psa_aead_update().
2719 * - The tag passed to this function call.
2720 *
2721 * If the authentication tag is correct, this function outputs any remaining
2722 * plaintext and reports success. If the authentication tag is not correct,
2723 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002724 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002725 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002726 * If this function returns an error status, the operation enters an error
2727 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002728 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002729 * \note Implementations shall make the best effort to ensure that the
2730 * comparison between the actual tag and the expected tag is performed
2731 * in constant time.
2732 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002733 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002734 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002735 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002736 * from previous calls to psa_aead_update()
2737 * that could not be processed until the end
2738 * of the input.
2739 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002740 * This must be appropriate for the selected algorithm and key:
2741 * - A sufficient output size is
2742 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2743 * \c alg) where \c key_type is the type of key
2744 * and \c alg is the algorithm that were used to
2745 * set up the operation.
2746 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2747 * the maximum output size of any supported AEAD
2748 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002749 * \param[out] plaintext_length On success, the number of bytes of
2750 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002751 * \param[in] tag Buffer containing the authentication tag.
2752 * \param tag_length Size of the \p tag buffer in bytes.
2753 *
2754 * \retval #PSA_SUCCESS
2755 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002756 * \retval #PSA_ERROR_INVALID_SIGNATURE
2757 * The calculations were successful, but the authentication tag is
2758 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002759 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2760 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002761 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2762 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2763 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002764 * \retval #PSA_ERROR_INVALID_ARGUMENT
2765 * The total length of input to psa_aead_update_ad() so far is
2766 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002767 * specified with psa_aead_set_lengths(), or
2768 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002769 * less than the plaintext length that was previously
2770 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002771 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2772 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2773 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2774 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2775 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002776 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002777 * The operation state is not valid (it must be an active decryption
2778 * operation with a nonce set), or the library has not been previously
2779 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002780 * It is implementation-dependent whether a failure to initialize
2781 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002782 */
2783psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002784 uint8_t *plaintext,
2785 size_t plaintext_size,
2786 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002787 const uint8_t *tag,
2788 size_t tag_length);
2789
2790/** Abort an AEAD operation.
2791 *
2792 * Aborting an operation frees all associated resources except for the
2793 * \p operation structure itself. Once aborted, the operation object
2794 * can be reused for another operation by calling
2795 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2796 *
2797 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002798 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002799 *
2800 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002801 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2802 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002803 *
2804 * \param[in,out] operation Initialized AEAD operation.
2805 *
Gilles Peskineed733552023-02-14 19:21:09 +01002806 * \retval #PSA_SUCCESS \emptydescription
2807 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2808 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2809 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002810 * \retval #PSA_ERROR_BAD_STATE
2811 * The library has not been previously initialized by psa_crypto_init().
2812 * It is implementation-dependent whether a failure to initialize
2813 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002814 */
2815psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2816
Gilles Peskine3b555712018-03-03 21:27:57 +01002817/**@}*/
2818
Gilles Peskine20035e32018-02-03 22:44:14 +01002819/** \defgroup asymmetric Asymmetric cryptography
2820 * @{
2821 */
2822
2823/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002824 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2825 * this includes the hashing step.
2826 *
2827 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2828 * a multi-part hash operation and then pass the resulting hash to
2829 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2830 * hash algorithm to use.
2831 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002832 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833 * It must be an asymmetric key pair. The key must
2834 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002835 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2837 * is true), that is compatible with the type of
2838 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002839 * \param[in] input The input message to sign.
2840 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002842 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843 * must be appropriate for the selected
2844 * algorithm and key:
2845 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002846 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2847 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848 * bit-size respectively of key.
2849 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2850 * maximum signature size of any supported
2851 * signature algorithm.
2852 * \param[out] signature_length On success, the number of bytes that make up
2853 * the returned signature value.
2854 *
Gilles Peskineed733552023-02-14 19:21:09 +01002855 * \retval #PSA_SUCCESS \emptydescription
2856 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002857 * \retval #PSA_ERROR_NOT_PERMITTED
2858 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2859 * or it does not permit the requested algorithm.
2860 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2861 * The size of the \p signature buffer is too small. You can
2862 * determine a sufficient buffer size by calling
2863 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2864 * where \c key_type and \c key_bits are the type and bit-size
2865 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002866 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2867 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2870 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2871 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2872 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2873 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2874 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2875 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002876 * \retval #PSA_ERROR_BAD_STATE
2877 * The library has not been previously initialized by psa_crypto_init().
2878 * It is implementation-dependent whether a failure to initialize
2879 * results in this error code.
2880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002881psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2882 psa_algorithm_t alg,
2883 const uint8_t *input,
2884 size_t input_length,
2885 uint8_t *signature,
2886 size_t signature_size,
2887 size_t *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888
2889/** \brief Verify the signature of a message with a public key, using
2890 * a hash-and-sign verification algorithm.
2891 *
2892 * \note To perform a multi-part hash-and-sign signature verification
2893 * algorithm, first use a multi-part hash operation to hash the message
2894 * and then pass the resulting hash to psa_verify_hash().
2895 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2896 * to use.
2897 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002898 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899 * It must be a public key or an asymmetric key
2900 * pair. The key must allow the usage
2901 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002902 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2904 * is true), that is compatible with the type of
2905 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002906 * \param[in] input The message whose signature is to be verified.
2907 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002908 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002909 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910 *
Gilles Peskineed733552023-02-14 19:21:09 +01002911 * \retval #PSA_SUCCESS \emptydescription
2912 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002913 * \retval #PSA_ERROR_NOT_PERMITTED
2914 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2915 * or it does not permit the requested algorithm.
2916 * \retval #PSA_ERROR_INVALID_SIGNATURE
2917 * The calculation was performed successfully, but the passed signature
2918 * is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01002919 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2920 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2921 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2922 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2923 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2924 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2925 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2926 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2927 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928 * \retval #PSA_ERROR_BAD_STATE
2929 * The library has not been previously initialized by psa_crypto_init().
2930 * It is implementation-dependent whether a failure to initialize
2931 * results in this error code.
2932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2934 psa_algorithm_t alg,
2935 const uint8_t *input,
2936 size_t input_length,
2937 const uint8_t *signature,
2938 size_t signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002939
2940/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002941 * \brief Sign a hash or short message with a private key.
2942 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002943 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002944 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002945 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2946 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002947 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2948 * to determine the hash algorithm to use.
2949 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002950 * \param key Identifier of the key to use for the operation.
2951 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002952 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002953 * \param alg A signature algorithm (PSA_ALG_XXX
2954 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2955 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002956 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002957 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002958 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002960 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002961 * \param[out] signature_length On success, the number of bytes
2962 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002963 *
Gilles Peskineed733552023-02-14 19:21:09 +01002964 * \retval #PSA_SUCCESS \emptydescription
2965 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2966 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002967 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002968 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002969 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002970 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002971 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002972 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002973 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2974 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2975 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2976 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2977 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2978 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2979 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2980 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002981 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002982 * The library has not been previously initialized by psa_crypto_init().
2983 * It is implementation-dependent whether a failure to initialize
2984 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002985 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002986psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002987 psa_algorithm_t alg,
2988 const uint8_t *hash,
2989 size_t hash_length,
2990 uint8_t *signature,
2991 size_t signature_size,
2992 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002993
2994/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002995 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01002996 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002997 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002998 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002999 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3000 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003001 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3002 * to determine the hash algorithm to use.
3003 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003004 * \param key Identifier of the key to use for the operation. It
3005 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003006 * key must allow the usage
3007 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02003008 * \param alg A signature algorithm (PSA_ALG_XXX
3009 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3010 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003011 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003012 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003013 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003014 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003015 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003016 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003017 *
Gilles Peskine28538492018-07-11 17:34:00 +02003018 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003019 * The signature is valid.
Gilles Peskineed733552023-02-14 19:21:09 +01003020 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3021 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003022 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003023 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003024 * signature is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01003025 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3026 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3029 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3030 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3031 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003032 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003033 * The library has not been previously initialized by psa_crypto_init().
3034 * It is implementation-dependent whether a failure to initialize
3035 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003036 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003037psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003038 psa_algorithm_t alg,
3039 const uint8_t *hash,
3040 size_t hash_length,
3041 const uint8_t *signature,
3042 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003043
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003044/**
3045 * \brief Encrypt a short message with a public key.
3046 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003047 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003048 * It must be a public key or an asymmetric key
3049 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003050 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003051 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003052 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003053 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003054 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003055 * \param[in] salt A salt or label, if supported by the
3056 * encryption algorithm.
3057 * If the algorithm does not support a
3058 * salt, pass \c NULL.
3059 * If the algorithm supports an optional
3060 * salt and you do not want to pass a salt,
3061 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003062 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003063 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3064 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003065 * \param salt_length Size of the \p salt buffer in bytes.
3066 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003067 * \param[out] output Buffer where the encrypted message is to
3068 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003069 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003070 * \param[out] output_length On success, the number of bytes
3071 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003072 *
Gilles Peskineed733552023-02-14 19:21:09 +01003073 * \retval #PSA_SUCCESS \emptydescription
3074 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3075 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003076 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003077 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003078 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003079 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003080 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003081 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003082 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3083 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3086 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3087 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3088 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3089 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003090 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003091 * The library has not been previously initialized by psa_crypto_init().
3092 * It is implementation-dependent whether a failure to initialize
3093 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003094 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003095psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003096 psa_algorithm_t alg,
3097 const uint8_t *input,
3098 size_t input_length,
3099 const uint8_t *salt,
3100 size_t salt_length,
3101 uint8_t *output,
3102 size_t output_size,
3103 size_t *output_length);
3104
3105/**
3106 * \brief Decrypt a short message with a private key.
3107 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003108 * \param key Identifier of the key to use for the operation.
3109 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003110 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003111 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003112 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003113 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003114 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003115 * \param[in] salt A salt or label, if supported by the
3116 * encryption algorithm.
3117 * If the algorithm does not support a
3118 * salt, pass \c NULL.
3119 * If the algorithm supports an optional
3120 * salt and you do not want to pass a salt,
3121 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003122 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003123 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3124 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003125 * \param salt_length Size of the \p salt buffer in bytes.
3126 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003127 * \param[out] output Buffer where the decrypted message is to
3128 * be written.
3129 * \param output_size Size of the \c output buffer in bytes.
3130 * \param[out] output_length On success, the number of bytes
3131 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003132 *
Gilles Peskineed733552023-02-14 19:21:09 +01003133 * \retval #PSA_SUCCESS \emptydescription
3134 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3135 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003136 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003137 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003138 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003139 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003140 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003141 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003142 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3143 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3144 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3146 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3147 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3148 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3149 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3150 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003151 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003152 * The library has not been previously initialized by psa_crypto_init().
3153 * It is implementation-dependent whether a failure to initialize
3154 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003155 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003156psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003157 psa_algorithm_t alg,
3158 const uint8_t *input,
3159 size_t input_length,
3160 const uint8_t *salt,
3161 size_t salt_length,
3162 uint8_t *output,
3163 size_t output_size,
3164 size_t *output_length);
3165
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003166/**@}*/
3167
Gilles Peskine35675b62019-05-16 17:26:11 +02003168/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003169 * @{
3170 */
3171
Gilles Peskine35675b62019-05-16 17:26:11 +02003172/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003173 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003174 * Before calling any function on a key derivation operation object, the
3175 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003176 * - Set the structure to all-bits-zero, for example:
3177 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003178 * psa_key_derivation_operation_t operation;
3179 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003180 * \endcode
3181 * - Initialize the structure to logical zero values, for example:
3182 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003183 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003184 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003185 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 * for example:
3187 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003188 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003189 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003190 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003191 * to the structure, for example:
3192 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * psa_key_derivation_operation_t operation;
3194 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195 * \endcode
3196 *
3197 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01003198 * make any assumptions about the content of this structure.
3199 * Implementation details can change in future versions without notice.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003200 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003201typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003203/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003204 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003205 * This macro returns a suitable initializer for a key derivation operation
3206 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003207 */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208
Gilles Peskine35675b62019-05-16 17:26:11 +02003209/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003211static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003213/** Set up a key derivation operation.
3214 *
3215 * A key derivation algorithm takes some inputs and uses them to generate
3216 * a byte stream in a deterministic way.
3217 * This byte stream can be used to produce keys and other
3218 * cryptographic material.
3219 *
3220 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003221 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3222 * -# Call psa_key_derivation_setup() to select the algorithm.
3223 * -# Provide the inputs for the key derivation by calling
3224 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3225 * as appropriate. Which inputs are needed, in what order, and whether
3226 * they may be keys and if so of what type depends on the algorithm.
3227 * -# Optionally set the operation's maximum capacity with
3228 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3229 * of or after providing inputs. For some algorithms, this step is mandatory
3230 * because the output depends on the maximum capacity.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003231 * -# To derive a key, call psa_key_derivation_output_key() or
3232 * psa_key_derivation_output_key_ext().
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003233 * To derive a byte string for a different purpose, call
3234 * psa_key_derivation_output_bytes().
3235 * Successive calls to these functions use successive output bytes
3236 * calculated by the key derivation algorithm.
3237 * -# Clean up the key derivation operation object with
3238 * psa_key_derivation_abort().
3239 *
3240 * If this function returns an error, the key derivation operation object is
3241 * not changed.
3242 *
3243 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3244 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003245 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003246 * Implementations must reject an attempt to derive a key of size 0.
3247 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003248 * \param[in,out] operation The key derivation operation object
3249 * to set up. It must
3250 * have been initialized but not set up yet.
3251 * \param alg The key derivation algorithm to compute
3252 * (\c PSA_ALG_XXX value such that
3253 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3254 *
3255 * \retval #PSA_SUCCESS
3256 * Success.
3257 * \retval #PSA_ERROR_INVALID_ARGUMENT
3258 * \c alg is not a key derivation algorithm.
3259 * \retval #PSA_ERROR_NOT_SUPPORTED
3260 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003261 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3262 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3263 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3264 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3265 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003266 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003267 * The operation state is not valid (it must be inactive), or
3268 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003269 * It is implementation-dependent whether a failure to initialize
3270 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003271 */
3272psa_status_t psa_key_derivation_setup(
3273 psa_key_derivation_operation_t *operation,
3274 psa_algorithm_t alg);
3275
Gilles Peskine35675b62019-05-16 17:26:11 +02003276/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003277 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003278 * The capacity of a key derivation is the maximum number of bytes that it can
3279 * return. When you get *N* bytes of output from a key derivation operation,
3280 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003281 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003282 * \param[in] operation The operation to query.
3283 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003284 *
Gilles Peskineed733552023-02-14 19:21:09 +01003285 * \retval #PSA_SUCCESS \emptydescription
3286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3287 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3288 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003289 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003290 * The operation state is not valid (it must be active), or
3291 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003292 * It is implementation-dependent whether a failure to initialize
3293 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003294 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003295psa_status_t psa_key_derivation_get_capacity(
3296 const psa_key_derivation_operation_t *operation,
3297 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003298
Gilles Peskine35675b62019-05-16 17:26:11 +02003299/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003300 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003301 * The capacity of a key derivation operation is the maximum number of bytes
3302 * that the key derivation operation can return from this point onwards.
3303 *
3304 * \param[in,out] operation The key derivation operation object to modify.
3305 * \param capacity The new capacity of the operation.
3306 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003307 * current capacity.
3308 *
Gilles Peskineed733552023-02-14 19:21:09 +01003309 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003310 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003311 * \p capacity is larger than the operation's current capacity.
3312 * In this case, the operation object remains valid and its capacity
3313 * remains unchanged.
Gilles Peskineed733552023-02-14 19:21:09 +01003314 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3315 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3316 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003317 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003318 * The operation state is not valid (it must be active), or the
3319 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003320 * It is implementation-dependent whether a failure to initialize
3321 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003322 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003323psa_status_t psa_key_derivation_set_capacity(
3324 psa_key_derivation_operation_t *operation,
3325 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003326
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003327/** Use the maximum possible capacity for a key derivation operation.
3328 *
3329 * Use this value as the capacity argument when setting up a key derivation
3330 * to indicate that the operation should have the maximum possible capacity.
3331 * The value of the maximum possible capacity depends on the key derivation
3332 * algorithm.
3333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003335
3336/** Provide an input for key derivation or key agreement.
3337 *
3338 * Which inputs are required and in what order depends on the algorithm.
3339 * Refer to the documentation of each key derivation or key agreement
3340 * algorithm for information.
3341 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003342 * This function passes direct inputs, which is usually correct for
3343 * non-secret inputs. To pass a secret input, which should be in a key
3344 * object, call psa_key_derivation_input_key() instead of this function.
3345 * Refer to the documentation of individual step types
3346 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3347 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003348 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003349 * If this function returns an error status, the operation enters an error
3350 * state and must be aborted by calling psa_key_derivation_abort().
3351 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003352 * \param[in,out] operation The key derivation operation object to use.
3353 * It must have been set up with
3354 * psa_key_derivation_setup() and must not
3355 * have produced any output yet.
3356 * \param step Which step the input data is for.
3357 * \param[in] data Input data to use.
3358 * \param data_length Size of the \p data buffer in bytes.
3359 *
3360 * \retval #PSA_SUCCESS
3361 * Success.
3362 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003363 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003364 * \c step does not allow direct inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003365 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3367 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3368 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3369 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003370 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003371 * The operation state is not valid for this input \p step, or
3372 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003373 * It is implementation-dependent whether a failure to initialize
3374 * results in this error code.
3375 */
3376psa_status_t psa_key_derivation_input_bytes(
3377 psa_key_derivation_operation_t *operation,
3378 psa_key_derivation_step_t step,
3379 const uint8_t *data,
3380 size_t data_length);
3381
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003382/** Provide a numeric input for key derivation or key agreement.
3383 *
3384 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003385 * However, when an algorithm requires a particular order, numeric inputs
3386 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003387 * Refer to the documentation of each key derivation or key agreement
3388 * algorithm for information.
3389 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003390 * This function is used for inputs which are fixed-size non-negative
3391 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003392 *
3393 * If this function returns an error status, the operation enters an error
3394 * state and must be aborted by calling psa_key_derivation_abort().
3395 *
3396 * \param[in,out] operation The key derivation operation object to use.
3397 * It must have been set up with
3398 * psa_key_derivation_setup() and must not
3399 * have produced any output yet.
3400 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003401 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003402 *
3403 * \retval #PSA_SUCCESS
3404 * Success.
3405 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003406 * \c step is not compatible with the operation's algorithm, or
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003407 * \c step does not allow numeric inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3410 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3411 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3412 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003413 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003414 * The operation state is not valid for this input \p step, or
3415 * the library has not been previously initialized by psa_crypto_init().
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003416 * It is implementation-dependent whether a failure to initialize
3417 * results in this error code.
3418 */
3419psa_status_t psa_key_derivation_input_integer(
3420 psa_key_derivation_operation_t *operation,
3421 psa_key_derivation_step_t step,
3422 uint64_t value);
3423
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003424/** Provide an input for key derivation in the form of a key.
3425 *
3426 * Which inputs are required and in what order depends on the algorithm.
3427 * Refer to the documentation of each key derivation or key agreement
3428 * algorithm for information.
3429 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003430 * This function obtains input from a key object, which is usually correct for
3431 * secret inputs or for non-secret personalization strings kept in the key
3432 * store. To pass a non-secret parameter which is not in the key store,
3433 * call psa_key_derivation_input_bytes() instead of this function.
3434 * Refer to the documentation of individual step types
3435 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3436 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003437 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003438 * If this function returns an error status, the operation enters an error
3439 * state and must be aborted by calling psa_key_derivation_abort().
3440 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003441 * \param[in,out] operation The key derivation operation object to use.
3442 * It must have been set up with
3443 * psa_key_derivation_setup() and must not
3444 * have produced any output yet.
3445 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003446 * \param key Identifier of the key. It must have an
3447 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003448 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003449 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3450 * and the algorithm used by the operation.
3451 *
3452 * \note Once all inputs steps are completed, the operations will allow:
3453 * - psa_key_derivation_output_bytes() if each input was either a direct input
3454 * or a key with #PSA_KEY_USAGE_DERIVE set;
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003455 * - psa_key_derivation_output_key() or psa_key_derivation_output_key_ext()
3456 * 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 Peskine6d81cbc2024-02-12 16:25:19 +01003704 * \note This function is equivalent to calling
3705 * psa_key_derivation_output_key_ext()
Gilles Peskine092ce512024-02-20 12:31:24 +01003706 * with the production parameters #PSA_KEY_PRODUCTION_PARAMETERS_INIT
3707 * and `params_data_length == 0` (i.e. `params->data` is empty).
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003708 *
Gilles Peskine20628592019-04-19 19:29:50 +02003709 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003710 * If the key type to be created is
3711 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3712 * the policy must be the same as in the current
3713 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003714 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003715 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003716 * key. For persistent keys, this is the key
3717 * identifier defined in \p attributes.
3718 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003719 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003720 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003721 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003722 * If the key is persistent, the key material and the key's metadata
3723 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003724 * \retval #PSA_ERROR_ALREADY_EXISTS
3725 * This is an attempt to create a persistent key, and there is
3726 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003727 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003728 * There was not enough data to create the desired key.
3729 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003730 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003731 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003732 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003733 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003734 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003735 * \retval #PSA_ERROR_INVALID_ARGUMENT
3736 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003737 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003738 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3739 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3740 * key; or one of the inputs was a key whose policy didn't allow
3741 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskineed733552023-02-14 19:21:09 +01003742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3743 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3744 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3745 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3746 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3747 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3748 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3749 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003750 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003751 * The operation state is not valid (it must be active and completed
3752 * all required input steps), or the library has not been previously
3753 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003754 * It is implementation-dependent whether a failure to initialize
3755 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003756 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003757psa_status_t psa_key_derivation_output_key(
3758 const psa_key_attributes_t *attributes,
3759 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003760 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003761
Gilles Peskine092ce512024-02-20 12:31:24 +01003762/** Derive a key from an ongoing key derivation operation with custom
3763 * production parameters.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003764 *
3765 * See the description of psa_key_derivation_out_key() for the operation of
Gilles Peskine092ce512024-02-20 12:31:24 +01003766 * this function with the default production parameters.
3767 * Mbed TLS currently does not currently support any non-default production
3768 * parameters.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003769 *
3770 * \note This function is experimental and may change in future minor
3771 * versions of Mbed TLS.
3772 *
3773 * \param[in] attributes The attributes for the new key.
3774 * If the key type to be created is
3775 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3776 * the policy must be the same as in the current
3777 * operation.
3778 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine092ce512024-02-20 12:31:24 +01003779 * \param[in] params Customization parameters for the key derivation.
3780 * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
3781 * with \p params_data_length = 0,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003782 * this function is equivalent to
3783 * psa_key_derivation_output_key().
3784 * Mbed TLS currently only supports the default
Gilles Peskine092ce512024-02-20 12:31:24 +01003785 * method, i.e. #PSA_KEY_PRODUCTION_PARAMETERS_INIT,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003786 * for all key types.
Gilles Peskine092ce512024-02-20 12:31:24 +01003787 * \param params_data_length
3788 * Length of `params->data` in bytes.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003789 * \param[out] key On success, an identifier for the newly created
3790 * key. For persistent keys, this is the key
3791 * identifier defined in \p attributes.
3792 * \c 0 on failure.
3793 *
3794 * \retval #PSA_SUCCESS
3795 * Success.
3796 * If the key is persistent, the key material and the key's metadata
3797 * have been saved to persistent storage.
3798 * \retval #PSA_ERROR_ALREADY_EXISTS
3799 * This is an attempt to create a persistent key, and there is
3800 * already a persistent key with the given identifier.
3801 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3802 * There was not enough data to create the desired key.
3803 * Note that in this case, no output is written to the output buffer.
3804 * The operation's capacity is set to 0, thus subsequent calls to
3805 * this function will not succeed, even with a smaller output buffer.
3806 * \retval #PSA_ERROR_NOT_SUPPORTED
3807 * The key type or key size is not supported, either by the
3808 * implementation in general or in this particular location.
3809 * \retval #PSA_ERROR_INVALID_ARGUMENT
3810 * The provided key attributes are not valid for the operation.
3811 * \retval #PSA_ERROR_NOT_PERMITTED
3812 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3813 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3814 * key; or one of the inputs was a key whose policy didn't allow
3815 * #PSA_KEY_USAGE_DERIVE.
3816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3817 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3818 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3819 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3820 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3821 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3822 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3823 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3824 * \retval #PSA_ERROR_BAD_STATE
3825 * The operation state is not valid (it must be active and completed
3826 * all required input steps), or the library has not been previously
3827 * initialized by psa_crypto_init().
3828 * It is implementation-dependent whether a failure to initialize
3829 * results in this error code.
3830 */
3831psa_status_t psa_key_derivation_output_key_ext(
3832 const psa_key_attributes_t *attributes,
3833 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01003834 const psa_key_production_parameters_t *params,
3835 size_t params_data_length,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003836 mbedtls_svc_key_id_t *key);
3837
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003838/** Compare output data from a key derivation operation to an expected value.
3839 *
3840 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003841 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003842 * If you view the key derivation's output as a stream of bytes, this
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003843 * function destructively reads the expected number of bytes from the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003844 * stream before comparing them.
3845 * The operation's capacity decreases by the number of bytes read.
3846 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003847 * This is functionally equivalent to the following code:
3848 * \code
3849 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3850 * if (memcmp(output, tmp, output_length) != 0)
3851 * return PSA_ERROR_INVALID_SIGNATURE;
3852 * \endcode
3853 * except (1) it works even if the key's policy does not allow outputting the
3854 * bytes, and (2) the comparison will be done in constant time.
3855 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003856 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003857 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3858 * the operation enters an error state and must be aborted by calling
3859 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003860 *
3861 * \param[in,out] operation The key derivation operation object to read from.
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003862 * \param[in] expected_output Buffer containing the expected derivation output.
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003863 * \param output_length Length of the expected output; this is also the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003864 * number of bytes that will be read.
3865 *
Gilles Peskineed733552023-02-14 19:21:09 +01003866 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003867 * \retval #PSA_ERROR_INVALID_SIGNATURE
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003868 * The output was read successfully, but it differs from the expected
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003869 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003870 * \retval #PSA_ERROR_NOT_PERMITTED
3871 * One of the inputs was a key whose policy didn't allow
3872 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003873 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3874 * The operation's capacity was less than
3875 * \p output_length bytes. Note that in this case,
3876 * the operation's capacity is set to 0, thus
3877 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003878 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003879 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3880 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3881 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3882 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3883 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003884 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003885 * The operation state is not valid (it must be active and completed
3886 * all required input steps), or the library has not been previously
3887 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003888 * It is implementation-dependent whether a failure to initialize
3889 * results in this error code.
3890 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003891psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003892 psa_key_derivation_operation_t *operation,
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003893 const uint8_t *expected_output,
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003894 size_t output_length);
3895
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003896/** Compare output data from a key derivation operation to an expected value
3897 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003898 *
3899 * This function calculates output bytes from a key derivation algorithm and
3900 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003901 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003902 * If you view the key derivation's output as a stream of bytes, this
Tom Cosgrove1797b052022-12-04 17:19:59 +00003903 * function destructively reads the number of bytes corresponding to the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003904 * length of the expected value from the stream before comparing them.
3905 * The operation's capacity decreases by the number of bytes read.
3906 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003907 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003908 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003909 * works even if the key cannot be exported.
3910 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003911 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003912 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3913 * the operation enters an error state and must be aborted by calling
3914 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003915 *
3916 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003917 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3918 * containing the expected output. Its policy must
3919 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3920 * and the permitted algorithm must match the
3921 * operation. The value of this key was likely
3922 * computed by a previous call to
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01003923 * psa_key_derivation_output_key() or
3924 * psa_key_derivation_output_key_ext().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003925 *
Gilles Peskineed733552023-02-14 19:21:09 +01003926 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003927 * \retval #PSA_ERROR_INVALID_SIGNATURE
3928 * The output was read successfully, but if differs from the expected
3929 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003930 * \retval #PSA_ERROR_INVALID_HANDLE
3931 * The key passed as the expected value does not exist.
3932 * \retval #PSA_ERROR_INVALID_ARGUMENT
3933 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003934 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02003935 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003936 * this algorithm; or one of the inputs was a key whose policy didn't
3937 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003938 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3939 * The operation's capacity was less than
3940 * the length of the expected value. In this case,
3941 * the operation's capacity is set to 0, thus
3942 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003943 * succeed, even with a smaller expected output.
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
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003949 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003950 * The operation state is not valid (it must be active and completed
3951 * all required input steps), or the library has not been previously
3952 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003953 * It is implementation-dependent whether a failure to initialize
3954 * results in this error code.
3955 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003956psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003957 psa_key_derivation_operation_t *operation,
3958 psa_key_id_t expected);
3959
Gilles Peskine35675b62019-05-16 17:26:11 +02003960/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003961 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003962 * Aborting an operation frees all associated resources except for the \c
3963 * operation structure itself. Once aborted, the operation object can be reused
3964 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003965 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003966 * This function may be called at any time after the operation
3967 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003968 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003969 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3970 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003971 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003972 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003973 *
Gilles Peskineed733552023-02-14 19:21:09 +01003974 * \retval #PSA_SUCCESS \emptydescription
3975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3976 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3977 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003978 * \retval #PSA_ERROR_BAD_STATE
3979 * The library has not been previously initialized by psa_crypto_init().
3980 * It is implementation-dependent whether a failure to initialize
3981 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003982 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003983psa_status_t psa_key_derivation_abort(
3984 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003985
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003986/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003987 *
3988 * \warning The raw result of a key agreement algorithm such as finite-field
3989 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3990 * not be used directly as key material. It should instead be passed as
3991 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003992 * a key derivation, use psa_key_derivation_key_agreement() and other
3993 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003994 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003995 * \param alg The key agreement algorithm to compute
3996 * (\c PSA_ALG_XXX value such that
3997 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3998 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003999 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02004000 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004001 * \param[in] peer_key Public key of the peer. It must be
4002 * in the same format that psa_import_key()
4003 * accepts. The standard formats for public
4004 * keys are documented in the documentation
4005 * of psa_export_public_key().
4006 * \param peer_key_length Size of \p peer_key in bytes.
4007 * \param[out] output Buffer where the decrypted message is to
4008 * be written.
4009 * \param output_size Size of the \c output buffer in bytes.
4010 * \param[out] output_length On success, the number of bytes
4011 * that make up the returned output.
4012 *
4013 * \retval #PSA_SUCCESS
4014 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01004015 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4016 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine769c7a62019-01-18 16:42:29 +01004017 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05004018 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01004019 * \p private_key is not compatible with \p alg,
4020 * or \p peer_key is not valid for \p alg or not compatible with
4021 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01004022 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4023 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01004024 * \retval #PSA_ERROR_NOT_SUPPORTED
4025 * \p alg is not a supported key agreement algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01004026 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \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_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01004031 * \retval #PSA_ERROR_BAD_STATE
4032 * The library has not been previously initialized by psa_crypto_init().
4033 * It is implementation-dependent whether a failure to initialize
4034 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004035 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02004036psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004037 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004038 const uint8_t *peer_key,
4039 size_t peer_key_length,
4040 uint8_t *output,
4041 size_t output_size,
4042 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004043
Gilles Peskineea0fb492018-07-12 17:17:20 +02004044/**@}*/
4045
Gilles Peskineedd76872018-07-20 17:42:05 +02004046/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004047 * @{
4048 */
4049
4050/**
4051 * \brief Generate random bytes.
4052 *
4053 * \warning This function **can** fail! Callers MUST check the return status
4054 * and MUST NOT use the content of the output buffer if the return
4055 * status is not #PSA_SUCCESS.
4056 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004057 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004058 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02004059 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004060 * \param output_size Number of bytes to generate and output.
4061 *
Gilles Peskineed733552023-02-14 19:21:09 +01004062 * \retval #PSA_SUCCESS \emptydescription
4063 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4064 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4065 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4067 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4068 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir0adf0fc2018-09-06 16:24:41 +03004069 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004070 * The library has not been previously initialized by psa_crypto_init().
4071 * It is implementation-dependent whether a failure to initialize
4072 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004073 */
4074psa_status_t psa_generate_random(uint8_t *output,
4075 size_t output_size);
4076
4077/**
4078 * \brief Generate a key or key pair.
4079 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02004080 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02004081 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004082 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004083 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004084 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004085 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004086 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004087 * the public exponent is 65537.
4088 * The modulus is a product of two probabilistic primes
4089 * between 2^{n-1} and 2^n where n is the bit size specified in the
4090 * attributes.
4091 *
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004092 * \note This function is equivalent to calling psa_generate_key_ext()
Gilles Peskine092ce512024-02-20 12:31:24 +01004093 * with the production parameters #PSA_KEY_PRODUCTION_PARAMETERS_INIT
4094 * and `params_data_length == 0` (i.e. `params->data` is empty).
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004095 *
Gilles Peskine20628592019-04-19 19:29:50 +02004096 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004097 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004098 * key. For persistent keys, this is the key
4099 * identifier defined in \p attributes.
4100 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004101 *
Gilles Peskine28538492018-07-11 17:34:00 +02004102 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004103 * Success.
4104 * If the key is persistent, the key material and the key's metadata
4105 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004106 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004107 * This is an attempt to create a persistent key, and there is
4108 * already a persistent key with the given identifier.
Gilles Peskineed733552023-02-14 19:21:09 +01004109 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4110 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4112 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4113 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4114 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4115 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4116 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4117 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4118 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4119 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03004120 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004121 * The library has not been previously initialized by psa_crypto_init().
4122 * It is implementation-dependent whether a failure to initialize
4123 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004124 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004125psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004126 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004127
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004128/**
Gilles Peskine092ce512024-02-20 12:31:24 +01004129 * \brief Generate a key or key pair using custom production parameters.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004130 *
4131 * See the description of psa_generate_key() for the operation of this
Gilles Peskine092ce512024-02-20 12:31:24 +01004132 * function with the default production parameters. In addition, this function
4133 * supports the following production customizations, described in more detail
4134 * in the documentation of ::psa_key_production_parameters_t:
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004135 *
4136 * - RSA keys: generation with a custom public exponent.
4137 *
4138 * \note This function is experimental and may change in future minor
4139 * versions of Mbed TLS.
4140 *
4141 * \param[in] attributes The attributes for the new key.
Gilles Peskine092ce512024-02-20 12:31:24 +01004142 * \param[in] params Customization parameters for the key generation.
4143 * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
4144 * with \p params_data_length = 0,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004145 * this function is equivalent to
Gilles Peskinedc5597b2024-02-20 11:42:18 +01004146 * psa_key_generation_output_key().
Gilles Peskine092ce512024-02-20 12:31:24 +01004147 * \param params_data_length
4148 * Length of `params->data` in bytes.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004149 * \param[out] key On success, an identifier for the newly created
4150 * key. For persistent keys, this is the key
4151 * identifier defined in \p attributes.
4152 * \c 0 on failure.
4153 *
4154 * \retval #PSA_SUCCESS
4155 * Success.
4156 * If the key is persistent, the key material and the key's metadata
4157 * have been saved to persistent storage.
4158 * \retval #PSA_ERROR_ALREADY_EXISTS
4159 * This is an attempt to create a persistent key, and there is
4160 * already a persistent key with the given identifier.
4161 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4162 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4164 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4166 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4167 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4168 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4169 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4170 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4171 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4172 * \retval #PSA_ERROR_BAD_STATE
4173 * The library has not been previously initialized by psa_crypto_init().
4174 * It is implementation-dependent whether a failure to initialize
4175 * results in this error code.
4176 */
4177psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01004178 const psa_key_production_parameters_t *params,
4179 size_t params_data_length,
Gilles Peskine6d81cbc2024-02-12 16:25:19 +01004180 mbedtls_svc_key_id_t *key);
4181
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004182/**@}*/
4183
Paul Elliott1265f002022-09-09 17:15:43 +01004184/** \defgroup interruptible_hash Interruptible sign/verify hash
4185 * @{
4186 */
4187
4188/** The type of the state data structure for interruptible hash
4189 * signing operations.
4190 *
4191 * Before calling any function on a sign hash operation object, the
4192 * application must initialize it by any of the following means:
4193 * - Set the structure to all-bits-zero, for example:
4194 * \code
4195 * psa_sign_hash_interruptible_operation_t operation;
4196 * memset(&operation, 0, sizeof(operation));
4197 * \endcode
4198 * - Initialize the structure to logical zero values, for example:
4199 * \code
4200 * psa_sign_hash_interruptible_operation_t operation = {0};
4201 * \endcode
4202 * - Initialize the structure to the initializer
4203 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4204 * \code
4205 * psa_sign_hash_interruptible_operation_t operation =
4206 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4207 * \endcode
4208 * - Assign the result of the function
4209 * psa_sign_hash_interruptible_operation_init() to the structure, for
4210 * example:
4211 * \code
4212 * psa_sign_hash_interruptible_operation_t operation;
4213 * operation = psa_sign_hash_interruptible_operation_init();
4214 * \endcode
4215 *
4216 * This is an implementation-defined \c struct. Applications should not
4217 * make any assumptions about the content of this structure.
4218 * Implementation details can change in future versions without notice. */
4219typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4220
4221/** The type of the state data structure for interruptible hash
4222 * verification operations.
4223 *
4224 * Before calling any function on a sign hash operation object, the
4225 * application must initialize it by any of the following means:
4226 * - Set the structure to all-bits-zero, for example:
4227 * \code
4228 * psa_verify_hash_interruptible_operation_t operation;
4229 * memset(&operation, 0, sizeof(operation));
4230 * \endcode
4231 * - Initialize the structure to logical zero values, for example:
4232 * \code
4233 * psa_verify_hash_interruptible_operation_t operation = {0};
4234 * \endcode
4235 * - Initialize the structure to the initializer
4236 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4237 * \code
4238 * psa_verify_hash_interruptible_operation_t operation =
4239 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4240 * \endcode
4241 * - Assign the result of the function
4242 * psa_verify_hash_interruptible_operation_init() to the structure, for
4243 * example:
4244 * \code
4245 * psa_verify_hash_interruptible_operation_t operation;
4246 * operation = psa_verify_hash_interruptible_operation_init();
4247 * \endcode
4248 *
4249 * This is an implementation-defined \c struct. Applications should not
4250 * make any assumptions about the content of this structure.
4251 * Implementation details can change in future versions without notice. */
4252typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4253
4254/**
4255 * \brief Set the maximum number of ops allowed to be
4256 * executed by an interruptible function in a
4257 * single call.
4258 *
4259 * \warning This is a beta API, and thus subject to change
4260 * at any point. It is not bound by the usual
4261 * interface stability promises.
4262 *
4263 * \note The time taken to execute a single op is
4264 * implementation specific and depends on
4265 * software, hardware, the algorithm, key type and
4266 * curve chosen. Even within a single operation,
4267 * successive ops can take differing amounts of
4268 * time. The only guarantee is that lower values
4269 * for \p max_ops means functions will block for a
4270 * lesser maximum amount of time. The functions
4271 * \c psa_sign_interruptible_get_num_ops() and
4272 * \c psa_verify_interruptible_get_num_ops() are
4273 * provided to help with tuning this value.
4274 *
4275 * \note This value defaults to
4276 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4277 * means the whole operation will be done in one
4278 * go, regardless of the number of ops required.
4279 *
4280 * \note If more ops are needed to complete a
4281 * computation, #PSA_OPERATION_INCOMPLETE will be
4282 * returned by the function performing the
4283 * computation. It is then the caller's
4284 * responsibility to either call again with the
4285 * same operation context until it returns 0 or an
4286 * error code; or to call the relevant abort
4287 * function if the answer is no longer required.
4288 *
4289 * \note The interpretation of \p max_ops is also
4290 * implementation defined. On a hard real time
4291 * system, this can indicate a hard deadline, as a
4292 * real-time system needs a guarantee of not
4293 * spending more than X time, however care must be
4294 * taken in such an implementation to avoid the
4295 * situation whereby calls just return, not being
4296 * able to do any actual work within the allotted
4297 * time. On a non-real-time system, the
4298 * implementation can be more relaxed, but again
4299 * whether this number should be interpreted as as
4300 * hard or soft limit or even whether a less than
4301 * or equals as regards to ops executed in a
4302 * single call is implementation defined.
4303 *
Paul Elliott21c39512023-02-15 19:47:39 +00004304 * \note For keys in local storage when no accelerator
4305 * driver applies, please see also the
4306 * documentation for \c mbedtls_ecp_set_max_ops(),
4307 * which is the internal implementation in these
4308 * cases.
4309 *
Paul Elliott1265f002022-09-09 17:15:43 +01004310 * \warning With implementations that interpret this number
4311 * as a hard limit, setting this number too small
4312 * may result in an infinite loop, whereby each
4313 * call results in immediate return with no ops
4314 * done (as there is not enough time to execute
4315 * any), and thus no result will ever be achieved.
4316 *
4317 * \note This only applies to functions whose
4318 * documentation mentions they may return
4319 * #PSA_OPERATION_INCOMPLETE.
4320 *
4321 * \param max_ops The maximum number of ops to be executed in a
4322 * single call. This can be a number from 0 to
4323 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4324 * is the least amount of work done per call.
4325 */
4326void psa_interruptible_set_max_ops(uint32_t max_ops);
4327
4328/**
4329 * \brief Get the maximum number of ops allowed to be
4330 * executed by an interruptible function in a
4331 * single call. This will return the last
4332 * value set by
4333 * \c psa_interruptible_set_max_ops() or
4334 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4335 * that function has never been called.
4336 *
4337 * \warning This is a beta API, and thus subject to change
4338 * at any point. It is not bound by the usual
4339 * interface stability promises.
4340 *
4341 * \return Maximum number of ops allowed to be
4342 * executed by an interruptible function in a
4343 * single call.
4344 */
4345uint32_t psa_interruptible_get_max_ops(void);
4346
4347/**
4348 * \brief Get the number of ops that a hash signing
4349 * operation has taken so far. If the operation
4350 * has completed, then this will represent the
4351 * number of ops required for the entire
4352 * operation. After initialization or calling
4353 * \c psa_sign_hash_interruptible_abort() on
4354 * the operation, a value of 0 will be returned.
4355 *
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004356 * \note This interface is guaranteed re-entrant and
4357 * thus may be called from driver code.
4358 *
Paul Elliott1265f002022-09-09 17:15:43 +01004359 * \warning This is a beta API, and thus subject to change
4360 * at any point. It is not bound by the usual
4361 * interface stability promises.
4362 *
4363 * This is a helper provided to help you tune the
4364 * value passed to \c
4365 * psa_interruptible_set_max_ops().
4366 *
4367 * \param operation The \c psa_sign_hash_interruptible_operation_t
4368 * to use. This must be initialized first.
4369 *
4370 * \return Number of ops that the operation has taken so
4371 * far.
4372 */
4373uint32_t psa_sign_hash_get_num_ops(
4374 const psa_sign_hash_interruptible_operation_t *operation);
4375
4376/**
4377 * \brief Get the number of ops that a hash verification
4378 * operation has taken so far. If the operation
4379 * has completed, then this will represent the
4380 * number of ops required for the entire
4381 * operation. After initialization or calling \c
4382 * psa_verify_hash_interruptible_abort() on the
4383 * operation, a value of 0 will be returned.
4384 *
4385 * \warning This is a beta API, and thus subject to change
4386 * at any point. It is not bound by the usual
4387 * interface stability promises.
4388 *
4389 * This is a helper provided to help you tune the
4390 * value passed to \c
4391 * psa_interruptible_set_max_ops().
4392 *
4393 * \param operation The \c
4394 * psa_verify_hash_interruptible_operation_t to
4395 * use. This must be initialized first.
4396 *
4397 * \return Number of ops that the operation has taken so
4398 * far.
4399 */
4400uint32_t psa_verify_hash_get_num_ops(
4401 const psa_verify_hash_interruptible_operation_t *operation);
4402
4403/**
4404 * \brief Start signing a hash or short message with a
4405 * private key, in an interruptible manner.
4406 *
4407 * \see \c psa_sign_hash_complete()
4408 *
4409 * \warning This is a beta API, and thus subject to change
4410 * at any point. It is not bound by the usual
4411 * interface stability promises.
4412 *
4413 * \note This function combined with \c
4414 * psa_sign_hash_complete() is equivalent to
4415 * \c psa_sign_hash() but
4416 * \c psa_sign_hash_complete() can return early and
4417 * resume according to the limit set with \c
4418 * psa_interruptible_set_max_ops() to reduce the
4419 * maximum time spent in a function call.
4420 *
4421 * \note Users should call \c psa_sign_hash_complete()
4422 * repeatedly on the same context after a
4423 * successful call to this function until \c
4424 * psa_sign_hash_complete() either returns 0 or an
4425 * error. \c psa_sign_hash_complete() will return
4426 * #PSA_OPERATION_INCOMPLETE if there is more work
4427 * to do. Alternatively users can call
4428 * \c psa_sign_hash_abort() at any point if they no
4429 * longer want the result.
4430 *
4431 * \note If this function returns an error status, the
4432 * operation enters an error state and must be
4433 * aborted by calling \c psa_sign_hash_abort().
4434 *
4435 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4436 * to use. This must be initialized first.
4437 *
4438 * \param key Identifier of the key to use for the operation.
4439 * It must be an asymmetric key pair. The key must
4440 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4441 * \param alg A signature algorithm (\c PSA_ALG_XXX
4442 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4443 * is true), that is compatible with
4444 * the type of \p key.
4445 * \param[in] hash The hash or message to sign.
4446 * \param hash_length Size of the \p hash buffer in bytes.
4447 *
4448 * \retval #PSA_SUCCESS
4449 * The operation started successfully - call \c psa_sign_hash_complete()
4450 * with the same context to complete the operation
4451 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004452 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004453 * \retval #PSA_ERROR_NOT_PERMITTED
4454 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4455 * not permit the requested algorithm.
4456 * \retval #PSA_ERROR_BAD_STATE
4457 * An operation has previously been started on this context, and is
4458 * still in progress.
Paul Elliott15d7d432023-02-27 17:17:56 +00004459 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4460 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4461 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4462 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4463 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4464 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4465 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4466 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4467 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4468 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004469 * \retval #PSA_ERROR_BAD_STATE
4470 * The library has not been previously initialized by psa_crypto_init().
4471 * It is implementation-dependent whether a failure to initialize
4472 * results in this error code.
4473 */
4474psa_status_t psa_sign_hash_start(
4475 psa_sign_hash_interruptible_operation_t *operation,
4476 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4477 const uint8_t *hash, size_t hash_length);
4478
4479/**
4480 * \brief Continue and eventually complete the action of
4481 * signing a hash or short message with a private
4482 * key, in an interruptible manner.
4483 *
4484 * \see \c psa_sign_hash_start()
4485 *
4486 * \warning This is a beta API, and thus subject to change
4487 * at any point. It is not bound by the usual
4488 * interface stability promises.
4489 *
4490 * \note This function combined with \c
4491 * psa_sign_hash_start() is equivalent to
4492 * \c psa_sign_hash() but this function can return
4493 * early and resume according to the limit set with
4494 * \c psa_interruptible_set_max_ops() to reduce the
4495 * maximum time spent in a function call.
4496 *
4497 * \note Users should call this function on the same
4498 * operation object repeatedly until it either
4499 * returns 0 or an error. This function will return
4500 * #PSA_OPERATION_INCOMPLETE if there is more work
4501 * to do. Alternatively users can call
4502 * \c psa_sign_hash_abort() at any point if they no
4503 * longer want the result.
4504 *
4505 * \note When this function returns successfully, the
4506 * operation becomes inactive. If this function
4507 * returns an error status, the operation enters an
4508 * error state and must be aborted by calling
4509 * \c psa_sign_hash_abort().
4510 *
4511 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4512 * to use. This must be initialized first, and have
4513 * had \c psa_sign_hash_start() called with it
4514 * first.
4515 *
4516 * \param[out] signature Buffer where the signature is to be written.
4517 * \param signature_size Size of the \p signature buffer in bytes. This
4518 * must be appropriate for the selected
4519 * algorithm and key:
4520 * - The required signature size is
4521 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4522 * key_bits, \c alg) where \c key_type and \c
4523 * key_bits are the type and bit-size
4524 * respectively of key.
4525 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4526 * maximum signature size of any supported
4527 * signature algorithm.
4528 * \param[out] signature_length On success, the number of bytes that make up
4529 * the returned signature value.
4530 *
4531 * \retval #PSA_SUCCESS
4532 * Operation completed successfully
4533 *
4534 * \retval #PSA_OPERATION_INCOMPLETE
4535 * Operation was interrupted due to the setting of \c
4536 * psa_interruptible_set_max_ops(). There is still work to be done.
4537 * Call this function again with the same operation object.
4538 *
4539 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4540 * The size of the \p signature buffer is too small. You can
4541 * determine a sufficient buffer size by calling
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004542 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
Paul Elliott1265f002022-09-09 17:15:43 +01004543 * where \c key_type and \c key_bits are the type and bit-size
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004544 * respectively of \c key.
Paul Elliott1265f002022-09-09 17:15:43 +01004545 *
4546 * \retval #PSA_ERROR_BAD_STATE
4547 * An operation was not previously started on this context via
4548 * \c psa_sign_hash_start().
4549 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004550 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4551 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4554 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4555 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4556 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4557 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4558 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4559 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004560 * \retval #PSA_ERROR_BAD_STATE
4561 * The library has either not been previously initialized by
Paul Elliott15d7d432023-02-27 17:17:56 +00004562 * psa_crypto_init() or you did not previously call
Paul Elliott1265f002022-09-09 17:15:43 +01004563 * psa_sign_hash_start() with this operation object. It is
4564 * implementation-dependent whether a failure to initialize results in
4565 * this error code.
4566 */
4567psa_status_t psa_sign_hash_complete(
4568 psa_sign_hash_interruptible_operation_t *operation,
4569 uint8_t *signature, size_t signature_size,
4570 size_t *signature_length);
4571
4572/**
4573 * \brief Abort a sign hash operation.
4574 *
4575 * \warning This is a beta API, and thus subject to change
4576 * at any point. It is not bound by the usual
4577 * interface stability promises.
Paul Elliott1b49ef52023-02-03 14:27:32 +00004578 *
4579 * \note This function is the only function that clears
4580 * the number of ops completed as part of the
4581 * operation. Please ensure you copy this value via
4582 * \c psa_sign_hash_get_num_ops() if required
4583 * before calling.
4584 *
Paul Elliott1265f002022-09-09 17:15:43 +01004585 * \note Aborting an operation frees all associated
4586 * resources except for the \p operation structure
4587 * itself. Once aborted, the operation object can
4588 * be reused for another operation by calling \c
4589 * psa_sign_hash_start() again.
4590 *
4591 * \note You may call this function any time after the
4592 * operation object has been initialized. In
4593 * particular, calling \c psa_sign_hash_abort()
4594 * after the operation has already been terminated
4595 * by a call to \c psa_sign_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004596 * psa_sign_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004597 *
4598 * \param[in,out] operation Initialized sign hash operation.
4599 *
4600 * \retval #PSA_SUCCESS
4601 * The operation was aborted successfully.
4602 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004603 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004604 * \retval #PSA_ERROR_BAD_STATE
4605 * The library has not been previously initialized by psa_crypto_init().
4606 * It is implementation-dependent whether a failure to initialize
4607 * results in this error code.
4608 */
4609psa_status_t psa_sign_hash_abort(
4610 psa_sign_hash_interruptible_operation_t *operation);
4611
4612/**
4613 * \brief Start reading and verifying a hash or short
4614 * message, in an interruptible manner.
4615 *
4616 * \see \c psa_verify_hash_complete()
4617 *
4618 * \warning This is a beta API, and thus subject to change
4619 * at any point. It is not bound by the usual
4620 * interface stability promises.
4621 *
4622 * \note This function combined with \c
4623 * psa_verify_hash_complete() is equivalent to
4624 * \c psa_verify_hash() but \c
4625 * psa_verify_hash_complete() can return early and
4626 * resume according to the limit set with \c
4627 * psa_interruptible_set_max_ops() to reduce the
4628 * maximum time spent in a function.
4629 *
4630 * \note Users should call \c psa_verify_hash_complete()
4631 * repeatedly on the same operation object after a
4632 * successful call to this function until \c
4633 * psa_verify_hash_complete() either returns 0 or
4634 * an error. \c psa_verify_hash_complete() will
4635 * return #PSA_OPERATION_INCOMPLETE if there is
4636 * more work to do. Alternatively users can call
4637 * \c psa_verify_hash_abort() at any point if they
4638 * no longer want the result.
4639 *
4640 * \note If this function returns an error status, the
4641 * operation enters an error state and must be
4642 * aborted by calling \c psa_verify_hash_abort().
4643 *
4644 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4645 * to use. This must be initialized first.
4646 *
4647 * \param key Identifier of the key to use for the operation.
4648 * The key must allow the usage
4649 * #PSA_KEY_USAGE_VERIFY_HASH.
4650 * \param alg A signature algorithm (\c PSA_ALG_XXX
4651 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4652 * is true), that is compatible with
4653 * the type of \p key.
4654 * \param[in] hash The hash whose signature is to be verified.
4655 * \param hash_length Size of the \p hash buffer in bytes.
4656 * \param[in] signature Buffer containing the signature to verify.
4657 * \param signature_length Size of the \p signature buffer in bytes.
4658 *
4659 * \retval #PSA_SUCCESS
4660 * The operation started successfully - please call \c
4661 * psa_verify_hash_complete() with the same context to complete the
4662 * operation.
4663 *
4664 * \retval #PSA_ERROR_BAD_STATE
4665 * Another operation has already been started on this context, and is
4666 * still in progress.
4667 *
4668 * \retval #PSA_ERROR_NOT_PERMITTED
4669 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4670 * not permit the requested algorithm.
4671 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004672 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4673 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4676 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4677 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4678 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4679 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4680 * \retval PSA_ERROR_DATA_INVALID \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004681 * \retval #PSA_ERROR_BAD_STATE
4682 * The library has not been previously initialized by psa_crypto_init().
4683 * It is implementation-dependent whether a failure to initialize
4684 * results in this error code.
4685 */
4686psa_status_t psa_verify_hash_start(
4687 psa_verify_hash_interruptible_operation_t *operation,
4688 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4689 const uint8_t *hash, size_t hash_length,
4690 const uint8_t *signature, size_t signature_length);
4691
4692/**
4693 * \brief Continue and eventually complete the action of
4694 * reading and verifying a hash or short message
4695 * signed with a private key, in an interruptible
4696 * manner.
4697 *
4698 * \see \c psa_verify_hash_start()
4699 *
4700 * \warning This is a beta API, and thus subject to change
4701 * at any point. It is not bound by the usual
4702 * interface stability promises.
4703 *
4704 * \note This function combined with \c
4705 * psa_verify_hash_start() is equivalent to
4706 * \c psa_verify_hash() but this function can
4707 * return early and resume according to the limit
4708 * set with \c psa_interruptible_set_max_ops() to
4709 * reduce the maximum time spent in a function
4710 * call.
4711 *
4712 * \note Users should call this function on the same
4713 * operation object repeatedly until it either
4714 * returns 0 or an error. This function will return
4715 * #PSA_OPERATION_INCOMPLETE if there is more work
4716 * to do. Alternatively users can call
4717 * \c psa_verify_hash_abort() at any point if they
4718 * no longer want the result.
4719 *
4720 * \note When this function returns successfully, the
4721 * operation becomes inactive. If this function
4722 * returns an error status, the operation enters an
4723 * error state and must be aborted by calling
4724 * \c psa_verify_hash_abort().
4725 *
4726 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4727 * to use. This must be initialized first, and have
4728 * had \c psa_verify_hash_start() called with it
4729 * first.
4730 *
4731 * \retval #PSA_SUCCESS
4732 * Operation completed successfully, and the passed signature is valid.
4733 *
4734 * \retval #PSA_OPERATION_INCOMPLETE
4735 * Operation was interrupted due to the setting of \c
4736 * psa_interruptible_set_max_ops(). There is still work to be done.
4737 * Call this function again with the same operation object.
4738 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004739 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004740 * \retval #PSA_ERROR_INVALID_SIGNATURE
4741 * The calculation was performed successfully, but the passed
4742 * signature is not a valid signature.
Paul Elliott15d7d432023-02-27 17:17:56 +00004743 * \retval #PSA_ERROR_BAD_STATE
Paul Elliott1265f002022-09-09 17:15:43 +01004744 * An operation was not previously started on this context via
4745 * \c psa_verify_hash_start().
Paul Elliott15d7d432023-02-27 17:17:56 +00004746 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4747 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4748 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4749 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4750 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4751 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4752 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4753 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4754 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4755 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004756 * \retval #PSA_ERROR_BAD_STATE
4757 * The library has either not been previously initialized by
4758 * psa_crypto_init() or you did not previously call
4759 * psa_verify_hash_start() on this object. It is
4760 * implementation-dependent whether a failure to initialize results in
4761 * this error code.
4762 */
4763psa_status_t psa_verify_hash_complete(
4764 psa_verify_hash_interruptible_operation_t *operation);
4765
4766/**
4767 * \brief Abort a verify hash operation.
4768 *
4769 * \warning This is a beta API, and thus subject to change at
4770 * any point. It is not bound by the usual interface
4771 * stability promises.
4772 *
Paul Elliott1b49ef52023-02-03 14:27:32 +00004773 * \note This function is the only function that clears the
4774 * number of ops completed as part of the operation.
4775 * Please ensure you copy this value via
4776 * \c psa_verify_hash_get_num_ops() if required
4777 * before calling.
4778 *
Paul Elliott1265f002022-09-09 17:15:43 +01004779 * \note Aborting an operation frees all associated
4780 * resources except for the operation structure
4781 * itself. Once aborted, the operation object can be
4782 * reused for another operation by calling \c
4783 * psa_verify_hash_start() again.
4784 *
4785 * \note You may call this function any time after the
4786 * operation object has been initialized.
4787 * In particular, calling \c psa_verify_hash_abort()
4788 * after the operation has already been terminated by
4789 * a call to \c psa_verify_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004790 * psa_verify_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004791 *
4792 * \param[in,out] operation Initialized verify hash operation.
4793 *
4794 * \retval #PSA_SUCCESS
4795 * The operation was aborted successfully.
4796 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004797 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004798 * \retval #PSA_ERROR_BAD_STATE
4799 * The library has not been previously initialized by psa_crypto_init().
4800 * It is implementation-dependent whether a failure to initialize
4801 * results in this error code.
4802 */
4803psa_status_t psa_verify_hash_abort(
4804 psa_verify_hash_interruptible_operation_t *operation);
4805
4806
4807/**@}*/
4808
Gilles Peskinee59236f2018-01-27 23:32:46 +01004809#ifdef __cplusplus
4810}
4811#endif
4812
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004813/* The file "crypto_sizes.h" contains definitions for size calculation
4814 * macros whose definitions are implementation-specific. */
4815#include "crypto_sizes.h"
4816
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004817/* The file "crypto_struct.h" contains definitions for
4818 * implementation-specific structs that are declared above. */
Gilles Peskineb1176f22023-02-22 22:07:28 +01004819#if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)
4820#include MBEDTLS_PSA_CRYPTO_STRUCT_FILE
4821#else
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004822#include "crypto_struct.h"
Gilles Peskineb1176f22023-02-22 22:07:28 +01004823#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004824
4825/* The file "crypto_extra.h" contains vendor-specific definitions. This
4826 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004827#include "crypto_extra.h"
4828
4829#endif /* PSA_CRYPTO_H */