blob: 5096748e924b12357e7aa0925ab638c0db6d0454 [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 Rodgman7ff79652023-11-03 12:04:52 +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
13#include "crypto_platform.h"
14
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010015#include <stddef.h>
16
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010017#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010018/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
19 * must be defined in the crypto_platform.h header. These mock definitions
20 * are present in this file as a convenience to generate pretty-printed
21 * documentation that includes those definitions. */
22
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010023/** \defgroup platform Implementation-specific definitions
24 * @{
25 */
26
27/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010028#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#ifdef __cplusplus
31extern "C" {
32#endif
33
Gilles Peskinef3b731e2018-12-12 13:38:31 +010034/* The file "crypto_types.h" declares types that encode errors,
35 * algorithms, key types, policies, etc. */
36#include "crypto_types.h"
37
Andrew Thoelke02b372b2019-10-02 09:32:21 +010038/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010039 * @{
40 */
41
42/**
43 * The major version of this implementation of the PSA Crypto API
44 */
45#define PSA_CRYPTO_API_VERSION_MAJOR 1
46
47/**
48 * The minor version of this implementation of the PSA Crypto API
49 */
50#define PSA_CRYPTO_API_VERSION_MINOR 0
51
52/**@}*/
53
Gilles Peskinef3b731e2018-12-12 13:38:31 +010054/* The file "crypto_values.h" declares macros to build and analyze values
55 * of integral types defined in "crypto_types.h". */
56#include "crypto_values.h"
57
58/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010059 * @{
60 */
61
62/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010063 * \brief Library initialization.
64 *
65 * Applications must call this function before calling any other
66 * function in this module.
67 *
68 * Applications may call this function more than once. Once a call
69 * succeeds, subsequent calls are guaranteed to succeed.
70 *
itayzafrir18617092018-09-16 12:22:41 +030071 * If the application calls other functions before calling psa_crypto_init(),
72 * the behavior is undefined. Implementations are encouraged to either perform
73 * the operation as if the library had been initialized or to return
74 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
75 * implementations should not return a success status if the lack of
76 * initialization may have security implications, for example due to improper
77 * seeding of the random number generator.
78 *
Gilles Peskineec1eff32023-02-14 19:21:09 +010079 * \retval #PSA_SUCCESS \emptydescription
80 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
81 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
82 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
83 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
84 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
85 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
86 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
87 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
88 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskinee59236f2018-01-27 23:32:46 +010089 */
90psa_status_t psa_crypto_init(void);
91
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010092/**@}*/
93
Gilles Peskine105f67f2019-07-23 18:16:05 +020094/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020095 * @{
96 */
97
Gilles Peskinea0c06552019-05-21 15:54:54 +020098/** \def PSA_KEY_ATTRIBUTES_INIT
99 *
100 * This macro returns a suitable initializer for a key attribute structure
101 * of type #psa_key_attributes_t.
102 */
103#ifdef __DOXYGEN_ONLY__
104/* This is an example definition for documentation purposes.
105 * Implementations should define a suitable value in `crypto_struct.h`.
106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107#define PSA_KEY_ATTRIBUTES_INIT { 0 }
Gilles Peskinea0c06552019-05-21 15:54:54 +0200108#endif
109
110/** Return an initial value for a key attributes structure.
111 */
112static psa_key_attributes_t psa_key_attributes_init(void);
113
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200114/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200115 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200116 * If the attribute structure currently declares the key as volatile (which
117 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200118 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200119 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200120 * This function does not access storage, it merely stores the given
121 * value in the structure.
122 * The persistent key will be written to storage when the attribute
123 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200124 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200125 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200126 *
Gilles Peskine20628592019-04-19 19:29:50 +0200127 * This function may be declared as `static` (i.e. without external
128 * linkage). This function may be provided as a function-like macro,
129 * but in this case it must evaluate each of its arguments exactly once.
130 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200131 * \param[out] attributes The attribute structure to write to.
132 * \param key The persistent identifier for the key.
Gilles Peskine37a4fcc2024-06-13 16:06:45 +0200133 * This can be any value in the range from
134 * #PSA_KEY_ID_USER_MIN to #PSA_KEY_ID_USER_MAX
135 * inclusive.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137static void psa_set_key_id(psa_key_attributes_t *attributes,
138 mbedtls_svc_key_id_t key);
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200139
Ronald Cron6b5ff532020-10-16 14:38:19 +0200140#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
141/** Set the owner identifier of a key.
142 *
143 * When key identifiers encode key owner identifiers, psa_set_key_id() does
144 * not allow to define in key attributes the owner of volatile keys as
145 * psa_set_key_id() enforces the key to be persistent.
146 *
147 * This function allows to set in key attributes the owner identifier of a
148 * key. It is intended to be used for volatile keys. For persistent keys,
149 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
150 * the owner of a key.
151 *
152 * \param[out] attributes The attribute structure to write to.
Antonio de Angelisbf672332021-11-30 12:21:44 +0000153 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200154 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
156 mbedtls_key_owner_id_t owner);
Ronald Cron6b5ff532020-10-16 14:38:19 +0200157#endif
158
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200159/** Set the location of a persistent key.
160 *
161 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200162 * with psa_set_key_id(). By default, a key that has a persistent identifier
163 * is stored in the default storage area identifier by
164 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
165 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200166 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200167 * This function does not access storage, it merely stores the given
168 * value in the structure.
169 * The persistent key will be written to storage when the attribute
170 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200171 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200172 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200173 *
174 * This function may be declared as `static` (i.e. without external
175 * linkage). This function may be provided as a function-like macro,
176 * but in this case it must evaluate each of its arguments exactly once.
177 *
178 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200179 * \param lifetime The lifetime for the key.
180 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200181 * key will be volatile, and the key identifier
182 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200183 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200184static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
185 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200186
Gilles Peskine20628592019-04-19 19:29:50 +0200187/** Retrieve the key identifier from key attributes.
188 *
189 * This function may be declared as `static` (i.e. without external
190 * linkage). This function may be provided as a function-like macro,
191 * but in this case it must evaluate its argument exactly once.
192 *
193 * \param[in] attributes The key attribute structure to query.
194 *
195 * \return The persistent identifier stored in the attribute structure.
196 * This value is unspecified if the attribute structure declares
197 * the key as volatile.
198 */
Ronald Cron71016a92020-08-28 19:01:50 +0200199static mbedtls_svc_key_id_t psa_get_key_id(
200 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200201
Gilles Peskine20628592019-04-19 19:29:50 +0200202/** Retrieve the lifetime from key attributes.
203 *
204 * This function may be declared as `static` (i.e. without external
205 * linkage). This function may be provided as a function-like macro,
206 * but in this case it must evaluate its argument exactly once.
207 *
208 * \param[in] attributes The key attribute structure to query.
209 *
210 * \return The lifetime value stored in the attribute structure.
211 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200212static psa_key_lifetime_t psa_get_key_lifetime(
213 const psa_key_attributes_t *attributes);
214
Gilles Peskine20628592019-04-19 19:29:50 +0200215/** Declare usage flags for a key.
216 *
217 * Usage flags are part of a key's usage policy. They encode what
218 * kind of operations are permitted on the key. For more details,
219 * refer to the documentation of the type #psa_key_usage_t.
220 *
221 * This function overwrites any usage flags
222 * previously set in \p attributes.
223 *
224 * This function may be declared as `static` (i.e. without external
225 * linkage). This function may be provided as a function-like macro,
226 * but in this case it must evaluate each of its arguments exactly once.
227 *
228 * \param[out] attributes The attribute structure to write to.
229 * \param usage_flags The usage flags to write.
230 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200231static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
232 psa_key_usage_t usage_flags);
233
Gilles Peskine20628592019-04-19 19:29:50 +0200234/** Retrieve the usage flags from key attributes.
235 *
236 * This function may be declared as `static` (i.e. without external
237 * linkage). This function may be provided as a function-like macro,
238 * but in this case it must evaluate its argument exactly once.
239 *
240 * \param[in] attributes The key attribute structure to query.
241 *
242 * \return The usage flags stored in the attribute structure.
243 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200244static psa_key_usage_t psa_get_key_usage_flags(
245 const psa_key_attributes_t *attributes);
246
Gilles Peskine20628592019-04-19 19:29:50 +0200247/** Declare the permitted algorithm policy for a key.
248 *
249 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200250 * algorithms are permitted to be used with this key. The following
251 * algorithm policies are supported:
252 * - 0 does not allow any cryptographic operation with the key. The key
253 * may be used for non-cryptographic actions such as exporting (if
254 * permitted by the usage flags).
255 * - An algorithm value permits this particular algorithm.
256 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
257 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100258 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100259 * any MAC algorithm from the same base class (e.g. CMAC) which
260 * generates/verifies a MAC length greater than or equal to the length
261 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100262 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
263 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100264 * generates/verifies a tag length greater than or equal to the length
265 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200266 *
267 * This function overwrites any algorithm policy
268 * previously set in \p attributes.
269 *
270 * This function may be declared as `static` (i.e. without external
271 * linkage). This function may be provided as a function-like macro,
272 * but in this case it must evaluate each of its arguments exactly once.
273 *
274 * \param[out] attributes The attribute structure to write to.
275 * \param alg The permitted algorithm policy to write.
276 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200277static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
278 psa_algorithm_t alg);
279
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100280
Gilles Peskine20628592019-04-19 19:29:50 +0200281/** Retrieve the algorithm policy from key attributes.
282 *
283 * This function may be declared as `static` (i.e. without external
284 * linkage). This function may be provided as a function-like macro,
285 * but in this case it must evaluate its argument exactly once.
286 *
287 * \param[in] attributes The key attribute structure to query.
288 *
289 * \return The algorithm stored in the attribute structure.
290 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200291static psa_algorithm_t psa_get_key_algorithm(
292 const psa_key_attributes_t *attributes);
293
Gilles Peskine20628592019-04-19 19:29:50 +0200294/** Declare the type of a key.
295 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200296 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200297 * previously set in \p attributes.
298 *
299 * This function may be declared as `static` (i.e. without external
300 * linkage). This function may be provided as a function-like macro,
301 * but in this case it must evaluate each of its arguments exactly once.
302 *
303 * \param[out] attributes The attribute structure to write to.
304 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200305 * If this is 0, the key type in \p attributes
306 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200307 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200308static void psa_set_key_type(psa_key_attributes_t *attributes,
309 psa_key_type_t type);
310
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100311
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200312/** Declare the size of a key.
313 *
314 * This function overwrites any key size previously set in \p attributes.
315 *
316 * This function may be declared as `static` (i.e. without external
317 * linkage). This function may be provided as a function-like macro,
318 * but in this case it must evaluate each of its arguments exactly once.
319 *
320 * \param[out] attributes The attribute structure to write to.
321 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200322 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200323 * becomes unspecified. Keys of size 0 are
324 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200325 */
326static void psa_set_key_bits(psa_key_attributes_t *attributes,
327 size_t bits);
328
Gilles Peskine20628592019-04-19 19:29:50 +0200329/** Retrieve the key type from key attributes.
330 *
331 * This function may be declared as `static` (i.e. without external
332 * linkage). This function may be provided as a function-like macro,
333 * but in this case it must evaluate its argument exactly once.
334 *
335 * \param[in] attributes The key attribute structure to query.
336 *
337 * \return The key type stored in the attribute structure.
338 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200339static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Retrieve the key size from key attributes.
342 *
343 * This function may be declared as `static` (i.e. without external
344 * linkage). This function may be provided as a function-like macro,
345 * but in this case it must evaluate its argument exactly once.
346 *
347 * \param[in] attributes The key attribute structure to query.
348 *
349 * \return The key size stored in the attribute structure, in bits.
350 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200351static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
352
Gilles Peskine20628592019-04-19 19:29:50 +0200353/** Retrieve the attributes of a key.
354 *
355 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200356 * psa_reset_key_attributes(). It then copies the attributes of
357 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200358 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200359 * \note This function may allocate memory or other resources.
360 * Once you have called this function on an attribute structure,
361 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200362 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200363 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200364 * \param[in,out] attributes On success, the attributes of the key.
365 * On failure, equivalent to a
366 * freshly-initialized structure.
367 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100368 * \retval #PSA_SUCCESS \emptydescription
369 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
372 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
373 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
374 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
375 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100376 * \retval #PSA_ERROR_BAD_STATE
377 * The library has not been previously initialized by psa_crypto_init().
378 * It is implementation-dependent whether a failure to initialize
379 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200380 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200381psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200382 psa_key_attributes_t *attributes);
383
Gilles Peskine20628592019-04-19 19:29:50 +0200384/** Reset a key attribute structure to a freshly initialized state.
385 *
386 * You must initialize the attribute structure as described in the
387 * documentation of the type #psa_key_attributes_t before calling this
388 * function. Once the structure has been initialized, you may call this
389 * function at any time.
390 *
391 * This function frees any auxiliary resources that the structure
392 * may contain.
393 *
394 * \param[in,out] attributes The attribute structure to reset.
395 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200396void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200397
Gilles Peskine87a5e562019-04-17 12:28:25 +0200398/**@}*/
399
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100400/** \defgroup key_management Key management
401 * @{
402 */
403
Ronald Cron277a85f2020-08-04 15:49:48 +0200404/** Remove non-essential copies of key material from memory.
405 *
406 * If the key identifier designates a volatile key, this functions does not do
407 * anything and returns successfully.
408 *
409 * If the key identifier designates a persistent key, then this function will
410 * free all resources associated with the key in volatile memory. The key
411 * data in persistent storage is not affected and the key can still be used.
412 *
413 * \param key Identifier of the key to purge.
414 *
415 * \retval #PSA_SUCCESS
416 * The key material will have been removed from memory if it is not
417 * currently required.
418 * \retval #PSA_ERROR_INVALID_ARGUMENT
419 * \p key is not a valid key identifier.
420 * \retval #PSA_ERROR_BAD_STATE
421 * The library has not been previously initialized by psa_crypto_init().
422 * It is implementation-dependent whether a failure to initialize
423 * results in this error code.
424 */
425psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
426
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100427/** Make a copy of a key.
428 *
429 * Copy key material from one location to another.
430 *
431 * This function is primarily useful to copy a key from one location
432 * to another, since it populates a key using the material from
433 * another key which may have a different lifetime.
434 *
435 * This function may be used to share a key with a different party,
436 * subject to implementation-defined restrictions on key sharing.
437 *
438 * The policy on the source key must have the usage flag
439 * #PSA_KEY_USAGE_COPY set.
440 * This flag is sufficient to permit the copy if the key has the lifetime
441 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
442 * Some secure elements do not provide a way to copy a key without
443 * making it extractable from the secure element. If a key is located
444 * in such a secure element, then the key must have both usage flags
445 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
446 * a copy of the key outside the secure element.
447 *
448 * The resulting key may only be used in a way that conforms to
449 * both the policy of the original key and the policy specified in
450 * the \p attributes parameter:
451 * - The usage flags on the resulting key are the bitwise-and of the
452 * usage flags on the source policy and the usage flags in \p attributes.
453 * - If both allow the same algorithm or wildcard-based
454 * algorithm policy, the resulting key has the same algorithm policy.
455 * - If either of the policies allows an algorithm and the other policy
456 * allows a wildcard-based algorithm policy that includes this algorithm,
457 * the resulting key allows the same algorithm.
458 * - If the policies do not allow any algorithm in common, this function
459 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
460 *
461 * The effect of this function on implementation-defined attributes is
462 * implementation-defined.
463 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200464 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200465 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200466 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200467 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100468 * \param[in] attributes The attributes for the new key.
469 * They are used as follows:
470 * - The key type and size may be 0. If either is
471 * nonzero, it must match the corresponding
472 * attribute of the source key.
473 * - The key location (the lifetime and, for
474 * persistent keys, the key identifier) is
475 * used directly.
476 * - The policy constraints (usage flags and
477 * algorithm policy) are combined from
478 * the source key and \p attributes so that
479 * both sets of restrictions apply, as
480 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200481 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200482 * key. For persistent keys, this is the key
483 * identifier defined in \p attributes.
484 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100485 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100486 * \retval #PSA_SUCCESS \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100487 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200488 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100489 * \retval #PSA_ERROR_ALREADY_EXISTS
490 * This is an attempt to create a persistent key, and there is
491 * already a persistent key with the given identifier.
492 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500493 * The lifetime or identifier in \p attributes are invalid, or
494 * the policy constraints on the source and specified in
495 * \p attributes are incompatible, or
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100496 * \p attributes specifies a key type or key size
497 * which does not match the attributes of the source key.
498 * \retval #PSA_ERROR_NOT_PERMITTED
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500499 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
500 * the source key is not exportable and its lifetime does not
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100501 * allow copying it to the target's lifetime.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
503 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
505 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
506 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
507 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
508 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
509 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100510 * \retval #PSA_ERROR_BAD_STATE
511 * The library has not been previously initialized by psa_crypto_init().
512 * It is implementation-dependent whether a failure to initialize
513 * results in this error code.
514 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200515psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100516 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200517 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100518
519
520/**
521 * \brief Destroy a key.
522 *
523 * This function destroys a key from both volatile
524 * memory and, if applicable, non-volatile storage. Implementations shall
525 * make a best effort to ensure that that the key material cannot be recovered.
526 *
527 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200528 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100529 *
530 * If a key is currently in use in a multipart operation, then destroying the
531 * key will cause the multipart operation to fail.
532 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200533 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200534 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100535 *
536 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200537 * \p key was a valid identifier and the key material that it
538 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100539 * \retval #PSA_ERROR_NOT_PERMITTED
540 * The key cannot be erased because it is
541 * read-only, either due to a policy or due to physical restrictions.
542 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200543 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100544 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Tom Cosgrove5205c972022-07-28 06:12:08 +0100545 * There was a failure in communication with the cryptoprocessor.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100546 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100547 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100548 * This error is typically a result of either storage corruption on a
549 * cleartext storage backend, or an attempt to read data that was
550 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100551 * \retval #PSA_ERROR_STORAGE_FAILURE
552 * The storage is corrupted. Implementations shall make a best effort
553 * to erase key material even in this stage, however applications
554 * should be aware that it may be impossible to guarantee that the
555 * key material is not recoverable in such cases.
556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
557 * An unexpected condition which is not a storage corruption or
558 * a communication failure occurred. The cryptoprocessor may have
559 * been compromised.
560 * \retval #PSA_ERROR_BAD_STATE
561 * The library has not been previously initialized by psa_crypto_init().
562 * It is implementation-dependent whether a failure to initialize
563 * results in this error code.
564 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200565psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100566
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100567/**@}*/
568
569/** \defgroup import_export Key import and export
570 * @{
571 */
572
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573/**
574 * \brief Import a key in binary format.
575 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100576 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100577 * documentation of psa_export_public_key() for the format of public keys
578 * and to the documentation of psa_export_key() for the format for
579 * other key types.
580 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200581 * The key data determines the key size. The attributes may optionally
582 * specify a key size; in this case it must match the size determined
583 * from the key data. A key size of 0 in \p attributes indicates that
584 * the key size is solely determined by the key data.
585 *
586 * Implementations must reject an attempt to import a key of size 0.
587 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100588 * This specification supports a single format for each key type.
589 * Implementations may support other formats as long as the standard
590 * format is supported. Implementations that support other formats
591 * should ensure that the formats are clearly unambiguous so as to
592 * minimize the risk that an invalid input is accidentally interpreted
593 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 *
Gilles Peskine20628592019-04-19 19:29:50 +0200595 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200596 * The key size is always determined from the
597 * \p data buffer.
598 * If the key size in \p attributes is nonzero,
599 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200600 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200601 * For persistent keys, this is the key identifier
602 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200603 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100604 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200605 * buffer is interpreted according to the type declared
606 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * All implementations must support at least the format
608 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100609 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200610 * the chosen type. Implementations may allow other
611 * formats, but should be conservative: implementations
612 * should err on the side of rejecting content if it
613 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200614 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100615 *
Gilles Peskine28538492018-07-11 17:34:00 +0200616 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100617 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100618 * If the key is persistent, the key material and the key's metadata
619 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200620 * \retval #PSA_ERROR_ALREADY_EXISTS
621 * This is an attempt to create a persistent key, and there is
622 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200623 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200624 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200625 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200626 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500627 * The key attributes, as a whole, are invalid, or
628 * the key data is not correctly formatted, or
629 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200630 * of the key data.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100631 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
632 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
634 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
635 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
636 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
637 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
638 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300639 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300640 * The library has not been previously initialized by psa_crypto_init().
641 * It is implementation-dependent whether a failure to initialize
642 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200644psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200646 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200647 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100649
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650
651/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 * \brief Export a key in binary format.
653 *
654 * The output of this function can be passed to psa_import_key() to
655 * create an equivalent object.
656 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100657 * If the implementation of psa_import_key() supports other formats
658 * beyond the format specified here, the output from psa_export_key()
659 * must use the representation specified here, not the original
660 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100662 * For standard key types, the output format is as follows:
663 *
664 * - For symmetric keys (including MAC keys), the format is the
665 * raw bytes of the key.
666 * - For DES, the key data consists of 8 bytes. The parity bits must be
667 * correct.
668 * - For Triple-DES, the format is the concatenation of the
669 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200670 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200671 * is the non-encrypted DER encoding of the representation defined by
672 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
673 * ```
674 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200675 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200676 * modulus INTEGER, -- n
677 * publicExponent INTEGER, -- e
678 * privateExponent INTEGER, -- d
679 * prime1 INTEGER, -- p
680 * prime2 INTEGER, -- q
681 * exponent1 INTEGER, -- d mod (p-1)
682 * exponent2 INTEGER, -- d mod (q-1)
683 * coefficient INTEGER, -- (inverse of q) mod p
684 * }
685 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200686 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200687 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100688 * a representation of the private value as a `ceiling(m/8)`-byte string
689 * where `m` is the bit size associated with the curve, i.e. the bit size
690 * of the order of the curve's coordinate field. This byte string is
691 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100692 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
693 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
694 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200695 * For Weierstrass curves, this is the content of the `privateKey` field of
696 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
697 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100698 * For twisted Edwards curves, the private key is as defined by RFC 8032
699 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200700 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200701 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000702 * format is the representation of the private key `x` as a big-endian byte
703 * string. The length of the byte string is the private key size in bytes
704 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200705 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
706 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100707 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200708 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
709 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200710 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200711 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200712 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200714 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data_length On success, the number of bytes
716 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100717 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100718 * \retval #PSA_SUCCESS \emptydescription
719 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200720 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200721 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100722 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
724 * The size of the \p data buffer is too small. You can determine a
725 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100726 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200727 * where \c type is the key type
728 * and \c bits is the key size in bits.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
730 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
731 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
732 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300735 * The library has not been previously initialized by psa_crypto_init().
736 * It is implementation-dependent whether a failure to initialize
737 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200739psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 uint8_t *data,
741 size_t data_size,
742 size_t *data_length);
743
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744/**
745 * \brief Export a public key or the public part of a key pair in binary format.
746 *
747 * The output of this function can be passed to psa_import_key() to
748 * create an object that is equivalent to the public key.
749 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000750 * This specification supports a single format for each key type.
751 * Implementations may support other formats as long as the standard
752 * format is supported. Implementations that support other formats
753 * should ensure that the formats are clearly unambiguous so as to
754 * minimize the risk that an invalid input is accidentally interpreted
755 * according to a different format.
756 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000757 * For standard key types, the output format is as follows:
758 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
759 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
760 * ```
761 * RSAPublicKey ::= SEQUENCE {
762 * modulus INTEGER, -- n
763 * publicExponent INTEGER } -- e
764 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100765 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200766 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100767 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
768 * by RFC 8032
769 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
770 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000771 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
772 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
773 * Let `m` be the bit size associated with the curve, i.e. the bit size of
774 * `q` for a curve over `F_q`. The representation consists of:
775 * - The byte 0x04;
776 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
777 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200778 * - For Diffie-Hellman key exchange public keys (key types for which
779 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000780 * the format is the representation of the public key `y = g^x mod p` as a
781 * big-endian byte string. The length of the byte string is the length of the
782 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100783 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200784 * Exporting a public key object or the public part of a key pair is
785 * always permitted, regardless of the key's usage flags.
786 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200787 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200788 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200789 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200790 * \param[out] data_length On success, the number of bytes
791 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100792 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100793 * \retval #PSA_SUCCESS \emptydescription
794 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200795 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200796 * The key is neither a public key nor a key pair.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100797 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200798 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
799 * The size of the \p data buffer is too small. You can determine a
800 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100801 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200802 * where \c type is the key type
803 * and \c bits is the key size in bits.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
805 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
806 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
807 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300809 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300810 * The library has not been previously initialized by psa_crypto_init().
811 * It is implementation-dependent whether a failure to initialize
812 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100813 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200814psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100815 uint8_t *data,
816 size_t data_size,
817 size_t *data_length);
818
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100819
Gilles Peskine20035e32018-02-03 22:44:14 +0100820
821/**@}*/
822
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100823/** \defgroup hash Message digests
824 * @{
825 */
826
Gilles Peskine69647a42019-01-14 20:18:12 +0100827/** Calculate the hash (digest) of a message.
828 *
829 * \note To verify the hash of a message against an
830 * expected value, use psa_hash_compare() instead.
831 *
832 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
833 * such that #PSA_ALG_IS_HASH(\p alg) is true).
834 * \param[in] input Buffer containing the message to hash.
835 * \param input_length Size of the \p input buffer in bytes.
836 * \param[out] hash Buffer where the hash is to be written.
837 * \param hash_size Size of the \p hash buffer in bytes.
838 * \param[out] hash_length On success, the number of bytes
839 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100840 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100841 *
842 * \retval #PSA_SUCCESS
843 * Success.
844 * \retval #PSA_ERROR_NOT_SUPPORTED
845 * \p alg is not supported or is not a hash algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100846 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100847 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
848 * \p hash_size is too small
Gilles Peskineec1eff32023-02-14 19:21:09 +0100849 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
850 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
851 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
852 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100853 * \retval #PSA_ERROR_BAD_STATE
854 * The library has not been previously initialized by psa_crypto_init().
855 * It is implementation-dependent whether a failure to initialize
856 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100857 */
858psa_status_t psa_hash_compute(psa_algorithm_t alg,
859 const uint8_t *input,
860 size_t input_length,
861 uint8_t *hash,
862 size_t hash_size,
863 size_t *hash_length);
864
865/** Calculate the hash (digest) of a message and compare it with a
866 * reference value.
867 *
868 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
869 * such that #PSA_ALG_IS_HASH(\p alg) is true).
870 * \param[in] input Buffer containing the message to hash.
871 * \param input_length Size of the \p input buffer in bytes.
Valerio Settie5995e22024-06-03 07:05:23 +0200872 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100873 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100874 *
875 * \retval #PSA_SUCCESS
876 * The expected hash is identical to the actual hash of the input.
877 * \retval #PSA_ERROR_INVALID_SIGNATURE
878 * The hash of the message was calculated successfully, but it
879 * differs from the expected hash.
880 * \retval #PSA_ERROR_NOT_SUPPORTED
881 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100882 * \retval #PSA_ERROR_INVALID_ARGUMENT
883 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskineec1eff32023-02-14 19:21:09 +0100884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
886 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
887 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100888 * \retval #PSA_ERROR_BAD_STATE
889 * The library has not been previously initialized by psa_crypto_init().
890 * It is implementation-dependent whether a failure to initialize
891 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100892 */
893psa_status_t psa_hash_compare(psa_algorithm_t alg,
894 const uint8_t *input,
895 size_t input_length,
896 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100897 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100898
Gilles Peskine308b91d2018-02-08 09:47:44 +0100899/** The type of the state data structure for multipart hash operations.
900 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000901 * Before calling any function on a hash operation object, the application must
902 * initialize it by any of the following means:
903 * - Set the structure to all-bits-zero, for example:
904 * \code
905 * psa_hash_operation_t operation;
906 * memset(&operation, 0, sizeof(operation));
907 * \endcode
908 * - Initialize the structure to logical zero values, for example:
909 * \code
910 * psa_hash_operation_t operation = {0};
911 * \endcode
912 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
913 * for example:
914 * \code
915 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
916 * \endcode
917 * - Assign the result of the function psa_hash_operation_init()
918 * to the structure, for example:
919 * \code
920 * psa_hash_operation_t operation;
921 * operation = psa_hash_operation_init();
922 * \endcode
923 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100924 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100925 * make any assumptions about the content of this structure except
926 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100927typedef struct psa_hash_operation_s psa_hash_operation_t;
928
Jaeden Amero6a25b412019-01-04 11:47:44 +0000929/** \def PSA_HASH_OPERATION_INIT
930 *
931 * This macro returns a suitable initializer for a hash operation object
932 * of type #psa_hash_operation_t.
933 */
934#ifdef __DOXYGEN_ONLY__
935/* This is an example definition for documentation purposes.
936 * Implementations should define a suitable value in `crypto_struct.h`.
937 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938#define PSA_HASH_OPERATION_INIT { 0 }
Jaeden Amero6a25b412019-01-04 11:47:44 +0000939#endif
940
941/** Return an initial value for a hash operation object.
942 */
943static psa_hash_operation_t psa_hash_operation_init(void);
944
Gilles Peskinef45adda2019-01-14 18:29:18 +0100945/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 *
947 * The sequence of operations to calculate a hash (message digest)
948 * is as follows:
949 * -# Allocate an operation object which will be passed to all the functions
950 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000951 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100952 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200953 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100954 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 * of the message each time. The hash that is calculated is the hash
956 * of the concatenation of these messages in order.
957 * -# To calculate the hash, call psa_hash_finish().
958 * To compare the hash with an expected value, call psa_hash_verify().
959 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100960 * If an error occurs at any step after a call to psa_hash_setup(), the
961 * operation will need to be reset by a call to psa_hash_abort(). The
962 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000963 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200965 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100966 * eventually terminate the operation. The following events terminate an
967 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100968 * - A successful call to psa_hash_finish() or psa_hash_verify().
969 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000971 * \param[in,out] operation The operation object to set up. It must have
972 * been initialized as per the documentation for
973 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200974 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
975 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976 *
Gilles Peskine28538492018-07-11 17:34:00 +0200977 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100978 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200979 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100980 * \p alg is not a supported hash algorithm.
981 * \retval #PSA_ERROR_INVALID_ARGUMENT
982 * \p alg is not a hash algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
985 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
986 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100987 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500988 * The operation state is not valid (it must be inactive), or
989 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100990 * It is implementation-dependent whether a failure to initialize
991 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200993psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100994 psa_algorithm_t alg);
995
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996/** Add a message fragment to a multipart hash operation.
997 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200998 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001000 * If this function returns an error status, the operation enters an error
1001 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001003 * \param[in,out] operation Active hash operation.
1004 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001005 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 *
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1011 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1012 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001014 * The operation state is not valid (it must be active), or
1015 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001016 * It is implementation-dependent whether a failure to initialize
1017 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001019psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1020 const uint8_t *input,
1021 size_t input_length);
1022
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023/** Finish the calculation of the hash of a message.
1024 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001025 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 * This function calculates the hash of the message formed by concatenating
1027 * the inputs passed to preceding calls to psa_hash_update().
1028 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001029 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001030 * If this function returns an error status, the operation enters an error
1031 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 *
1033 * \warning Applications should not call this function if they expect
1034 * a specific value for the hash. Call psa_hash_verify() instead.
1035 * Beware that comparing integrity or authenticity data such as
1036 * hash values with a function such as \c memcmp is risky
1037 * because the time taken by the comparison may leak information
1038 * about the hashed data which could allow an attacker to guess
1039 * a valid hash and thereby bypass security controls.
1040 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001041 * \param[in,out] operation Active hash operation.
1042 * \param[out] hash Buffer where the hash is to be written.
1043 * \param hash_size Size of the \p hash buffer in bytes.
1044 * \param[out] hash_length On success, the number of bytes
1045 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001046 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001047 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 *
Gilles Peskine28538492018-07-11 17:34:00 +02001049 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001051 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001052 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001053 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001055 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1056 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1057 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1058 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001059 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001060 * The operation state is not valid (it must be active), or
1061 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001062 * It is implementation-dependent whether a failure to initialize
1063 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1066 uint8_t *hash,
1067 size_t hash_size,
1068 size_t *hash_length);
1069
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070/** Finish the calculation of the hash of a message and compare it with
1071 * an expected value.
1072 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * This function calculates the hash of the message formed by concatenating
1075 * the inputs passed to preceding calls to psa_hash_update(). It then
1076 * compares the calculated hash with the expected hash passed as a
1077 * parameter to this function.
1078 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001079 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001080 * If this function returns an error status, the operation enters an error
1081 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine19067982018-03-20 17:54:53 +01001083 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * comparison between the actual hash and the expected hash is performed
1085 * in constant time.
1086 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * \param[in,out] operation Active hash operation.
1088 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The hash of the message was calculated successfully, but it
1095 * differs from the expected hash.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001096 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1097 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1098 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1099 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001100 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001101 * The operation state is not valid (it must be active), or
1102 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001103 * It is implementation-dependent whether a failure to initialize
1104 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001106psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1107 const uint8_t *hash,
1108 size_t hash_length);
1109
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110/** Abort a hash operation.
1111 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * \p operation structure itself. Once aborted, the operation object
1114 * can be reused for another operation by calling
1115 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001118 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001120 * In particular, calling psa_hash_abort() after the operation has been
1121 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1122 * psa_hash_verify() is safe and has no effect.
1123 *
1124 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01001126 * \retval #PSA_SUCCESS \emptydescription
1127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1128 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1129 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001130 * \retval #PSA_ERROR_BAD_STATE
1131 * The library has not been previously initialized by psa_crypto_init().
1132 * It is implementation-dependent whether a failure to initialize
1133 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 */
1135psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001136
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001137/** Clone a hash operation.
1138 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001139 * This function copies the state of an ongoing hash operation to
1140 * a new operation object. In other words, this function is equivalent
1141 * to calling psa_hash_setup() on \p target_operation with the same
1142 * algorithm that \p source_operation was set up for, then
1143 * psa_hash_update() on \p target_operation with the same input that
1144 * that was passed to \p source_operation. After this function returns, the
1145 * two objects are independent, i.e. subsequent calls involving one of
1146 * the objects do not affect the other object.
1147 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001148 * \param[in] source_operation The active hash operation to clone.
1149 * \param[in,out] target_operation The operation object to set up.
1150 * It must be initialized but not active.
1151 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01001152 * \retval #PSA_SUCCESS \emptydescription
1153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1154 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1155 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1156 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001157 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001158 * The \p source_operation state is not valid (it must be active), or
1159 * the \p target_operation state is not valid (it must be inactive), or
1160 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001161 * It is implementation-dependent whether a failure to initialize
1162 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001163 */
1164psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1165 psa_hash_operation_t *target_operation);
1166
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001167/**@}*/
1168
Gilles Peskine8c9def32018-02-08 10:02:12 +01001169/** \defgroup MAC Message authentication codes
1170 * @{
1171 */
1172
Gilles Peskine69647a42019-01-14 20:18:12 +01001173/** Calculate the MAC (message authentication code) of a message.
1174 *
1175 * \note To verify the MAC of a message against an
1176 * expected value, use psa_mac_verify() instead.
1177 * Beware that comparing integrity or authenticity data such as
1178 * MAC values with a function such as \c memcmp is risky
1179 * because the time taken by the comparison may leak information
1180 * about the MAC value which could allow an attacker to guess
1181 * a valid MAC and thereby bypass security controls.
1182 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001183 * \param key Identifier of the key to use for the operation. It
1184 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001185 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001186 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001187 * \param[in] input Buffer containing the input message.
1188 * \param input_length Size of the \p input buffer in bytes.
1189 * \param[out] mac Buffer where the MAC value is to be written.
1190 * \param mac_size Size of the \p mac buffer in bytes.
1191 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001192 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 *
1194 * \retval #PSA_SUCCESS
1195 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001196 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1197 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001198 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001199 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \retval #PSA_ERROR_NOT_SUPPORTED
1201 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001202 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1203 * \p mac_size is too small
Gilles Peskineec1eff32023-02-14 19:21:09 +01001204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1206 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1207 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001208 * \retval #PSA_ERROR_STORAGE_FAILURE
1209 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001210 * \retval #PSA_ERROR_BAD_STATE
1211 * The library has not been previously initialized by psa_crypto_init().
1212 * It is implementation-dependent whether a failure to initialize
1213 * results in this error code.
1214 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001215psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001216 psa_algorithm_t alg,
1217 const uint8_t *input,
1218 size_t input_length,
1219 uint8_t *mac,
1220 size_t mac_size,
1221 size_t *mac_length);
1222
1223/** Calculate the MAC of a message and compare it with a reference value.
1224 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001225 * \param key Identifier of the key to use for the operation. It
1226 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001227 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001228 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001229 * \param[in] input Buffer containing the input message.
1230 * \param input_length Size of the \p input buffer in bytes.
Valerio Settie5995e22024-06-03 07:05:23 +02001231 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \param mac_length Size of the \p mac buffer in bytes.
1233 *
1234 * \retval #PSA_SUCCESS
1235 * The expected MAC is identical to the actual MAC of the input.
1236 * \retval #PSA_ERROR_INVALID_SIGNATURE
1237 * The MAC of the message was calculated successfully, but it
1238 * differs from the expected value.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001239 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1240 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001242 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001243 * \retval #PSA_ERROR_NOT_SUPPORTED
1244 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1247 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1248 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001249 * \retval #PSA_ERROR_STORAGE_FAILURE
1250 * The key could not be retrieved from storage.
1251 * \retval #PSA_ERROR_BAD_STATE
1252 * The library has not been previously initialized by psa_crypto_init().
1253 * It is implementation-dependent whether a failure to initialize
1254 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001256psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001257 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001258 const uint8_t *input,
1259 size_t input_length,
1260 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001261 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001262
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001263/** The type of the state data structure for multipart MAC operations.
1264 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001265 * Before calling any function on a MAC operation object, the application must
1266 * initialize it by any of the following means:
1267 * - Set the structure to all-bits-zero, for example:
1268 * \code
1269 * psa_mac_operation_t operation;
1270 * memset(&operation, 0, sizeof(operation));
1271 * \endcode
1272 * - Initialize the structure to logical zero values, for example:
1273 * \code
1274 * psa_mac_operation_t operation = {0};
1275 * \endcode
1276 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1277 * for example:
1278 * \code
1279 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1280 * \endcode
1281 * - Assign the result of the function psa_mac_operation_init()
1282 * to the structure, for example:
1283 * \code
1284 * psa_mac_operation_t operation;
1285 * operation = psa_mac_operation_init();
1286 * \endcode
1287 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001288 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001289 * make any assumptions about the content of this structure except
1290 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001291typedef struct psa_mac_operation_s psa_mac_operation_t;
1292
Jaeden Amero769ce272019-01-04 11:48:03 +00001293/** \def PSA_MAC_OPERATION_INIT
1294 *
1295 * This macro returns a suitable initializer for a MAC operation object of type
1296 * #psa_mac_operation_t.
1297 */
1298#ifdef __DOXYGEN_ONLY__
1299/* This is an example definition for documentation purposes.
1300 * Implementations should define a suitable value in `crypto_struct.h`.
1301 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302#define PSA_MAC_OPERATION_INIT { 0 }
Jaeden Amero769ce272019-01-04 11:48:03 +00001303#endif
1304
1305/** Return an initial value for a MAC operation object.
1306 */
1307static psa_mac_operation_t psa_mac_operation_init(void);
1308
Gilles Peskinef45adda2019-01-14 18:29:18 +01001309/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001310 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001311 * This function sets up the calculation of the MAC
1312 * (message authentication code) of a byte string.
1313 * To verify the MAC of a message against an
1314 * expected value, use psa_mac_verify_setup() instead.
1315 *
1316 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 * -# Allocate an operation object which will be passed to all the functions
1318 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001319 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001320 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1323 * of the message each time. The MAC that is calculated is the MAC
1324 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * -# At the end of the message, call psa_mac_sign_finish() to finish
1326 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001328 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1329 * operation will need to be reset by a call to psa_mac_abort(). The
1330 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001331 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001333 * After a successful call to psa_mac_sign_setup(), the application must
1334 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001335 * - A successful call to psa_mac_sign_finish().
1336 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001338 * \param[in,out] operation The operation object to set up. It must have
1339 * been initialized as per the documentation for
1340 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001341 * \param key Identifier of the key to use for the operation. It
1342 * must remain valid until the operation terminates.
1343 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001344 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001345 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001346 *
Gilles Peskine28538492018-07-11 17:34:00 +02001347 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001348 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001349 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1350 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001352 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001353 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001354 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001355 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1357 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1358 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001359 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001360 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001361 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001362 * The operation state is not valid (it must be inactive), or
1363 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001364 * It is implementation-dependent whether a failure to initialize
1365 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001366 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001368 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 psa_algorithm_t alg);
1370
Gilles Peskinef45adda2019-01-14 18:29:18 +01001371/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 *
1373 * This function sets up the verification of the MAC
1374 * (message authentication code) of a byte string against an expected value.
1375 *
1376 * The sequence of operations to verify a MAC is as follows:
1377 * -# Allocate an operation object which will be passed to all the functions
1378 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001379 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001380 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001382 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1383 * of the message each time. The MAC that is calculated is the MAC
1384 * of the concatenation of these messages in order.
1385 * -# At the end of the message, call psa_mac_verify_finish() to finish
1386 * calculating the actual MAC of the message and verify it against
1387 * the expected value.
1388 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001389 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1390 * operation will need to be reset by a call to psa_mac_abort(). The
1391 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001392 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393 *
1394 * After a successful call to psa_mac_verify_setup(), the application must
1395 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001396 * - A successful call to psa_mac_verify_finish().
1397 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001398 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001399 * \param[in,out] operation The operation object to set up. It must have
1400 * been initialized as per the documentation for
1401 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001402 * \param key Identifier of the key to use for the operation. It
1403 * must remain valid until the operation terminates.
1404 * It must allow the usage
1405 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001406 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1407 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 *
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001411 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1412 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001413 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001415 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1419 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1420 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001421 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001422 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001423 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001424 * The operation state is not valid (it must be inactive), or
1425 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001426 * It is implementation-dependent whether a failure to initialize
1427 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428 */
1429psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001430 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001431 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001432
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433/** Add a message fragment to a multipart MAC operation.
1434 *
1435 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1436 * before calling this function.
1437 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001438 * If this function returns an error status, the operation enters an error
1439 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001440 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001441 * \param[in,out] operation Active MAC operation.
1442 * \param[in] input Buffer containing the message fragment to add to
1443 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001444 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 *
1446 * \retval #PSA_SUCCESS
1447 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001448 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1450 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1451 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1452 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001453 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001454 * The operation state is not valid (it must be active), or
1455 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001456 * It is implementation-dependent whether a failure to initialize
1457 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001458 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001459psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1460 const uint8_t *input,
1461 size_t input_length);
1462
Gilles Peskinedcd14942018-07-12 00:30:52 +02001463/** Finish the calculation of the MAC of a message.
1464 *
1465 * The application must call psa_mac_sign_setup() before calling this function.
1466 * This function calculates the MAC of the message formed by concatenating
1467 * the inputs passed to preceding calls to psa_mac_update().
1468 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001469 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001470 * If this function returns an error status, the operation enters an error
1471 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001472 *
1473 * \warning Applications should not call this function if they expect
1474 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1475 * Beware that comparing integrity or authenticity data such as
1476 * MAC values with a function such as \c memcmp is risky
1477 * because the time taken by the comparison may leak information
1478 * about the MAC value which could allow an attacker to guess
1479 * a valid MAC and thereby bypass security controls.
1480 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001481 * \param[in,out] operation Active MAC operation.
1482 * \param[out] mac Buffer where the MAC value is to be written.
1483 * \param mac_size Size of the \p mac buffer in bytes.
1484 * \param[out] mac_length On success, the number of bytes
1485 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001486 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001487 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001488 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001489 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490 *
1491 * \retval #PSA_SUCCESS
1492 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001494 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001495 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskineec1eff32023-02-14 19:21:09 +01001496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1498 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1499 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1500 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001501 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001502 * The operation state is not valid (it must be an active mac sign
1503 * operation), or the library has not been previously initialized
1504 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001505 * It is implementation-dependent whether a failure to initialize
1506 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001508psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1509 uint8_t *mac,
1510 size_t mac_size,
1511 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513/** Finish the calculation of the MAC of a message and compare it with
1514 * an expected value.
1515 *
1516 * The application must call psa_mac_verify_setup() before calling this function.
1517 * This function calculates the MAC of the message formed by concatenating
1518 * the inputs passed to preceding calls to psa_mac_update(). It then
1519 * compares the calculated MAC with the expected MAC passed as a
1520 * parameter to this function.
1521 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001522 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001523 * If this function returns an error status, the operation enters an error
1524 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001525 *
1526 * \note Implementations shall make the best effort to ensure that the
1527 * comparison between the actual MAC and the expected MAC is performed
1528 * in constant time.
1529 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001530 * \param[in,out] operation Active MAC operation.
1531 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001532 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533 *
1534 * \retval #PSA_SUCCESS
1535 * The expected MAC is identical to the actual MAC of the message.
1536 * \retval #PSA_ERROR_INVALID_SIGNATURE
1537 * The MAC of the message was calculated successfully, but it
1538 * differs from the expected MAC.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001539 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1541 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1542 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1543 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001544 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001545 * The operation state is not valid (it must be an active mac verify
1546 * operation), or the library has not been previously initialized
1547 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001548 * It is implementation-dependent whether a failure to initialize
1549 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001551psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1552 const uint8_t *mac,
1553 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555/** Abort a MAC operation.
1556 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001558 * \p operation structure itself. Once aborted, the operation object
1559 * can be reused for another operation by calling
1560 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001562 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001563 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001565 * In particular, calling psa_mac_abort() after the operation has been
1566 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1567 * psa_mac_verify_finish() is safe and has no effect.
1568 *
1569 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01001571 * \retval #PSA_SUCCESS \emptydescription
1572 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1573 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1574 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001575 * \retval #PSA_ERROR_BAD_STATE
1576 * The library has not been previously initialized by psa_crypto_init().
1577 * It is implementation-dependent whether a failure to initialize
1578 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001580psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1581
1582/**@}*/
1583
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001584/** \defgroup cipher Symmetric ciphers
1585 * @{
1586 */
1587
Gilles Peskine69647a42019-01-14 20:18:12 +01001588/** Encrypt a message using a symmetric cipher.
1589 *
1590 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001591 * vector). Use the multipart operation interface with a
1592 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001593 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001594 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001595 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001596 * \param alg The cipher algorithm to compute
1597 * (\c PSA_ALG_XXX value such that
1598 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1599 * \param[in] input Buffer containing the message to encrypt.
1600 * \param input_length Size of the \p input buffer in bytes.
1601 * \param[out] output Buffer where the output is to be written.
1602 * The output contains the IV followed by
1603 * the ciphertext proper.
1604 * \param output_size Size of the \p output buffer in bytes.
1605 * \param[out] output_length On success, the number of bytes
1606 * that make up the output.
1607 *
1608 * \retval #PSA_SUCCESS
1609 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001610 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1611 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001612 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001613 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001614 * \retval #PSA_ERROR_NOT_SUPPORTED
1615 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001616 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1619 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1620 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1621 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001622 * \retval #PSA_ERROR_BAD_STATE
1623 * The library has not been previously initialized by psa_crypto_init().
1624 * It is implementation-dependent whether a failure to initialize
1625 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001626 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001627psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001628 psa_algorithm_t alg,
1629 const uint8_t *input,
1630 size_t input_length,
1631 uint8_t *output,
1632 size_t output_size,
1633 size_t *output_length);
1634
1635/** Decrypt a message using a symmetric cipher.
1636 *
1637 * This function decrypts a message encrypted with a symmetric cipher.
1638 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001639 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001640 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001641 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001642 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001643 * \param alg The cipher algorithm to compute
1644 * (\c PSA_ALG_XXX value such that
1645 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1646 * \param[in] input Buffer containing the message to decrypt.
1647 * This consists of the IV followed by the
1648 * ciphertext proper.
1649 * \param input_length Size of the \p input buffer in bytes.
1650 * \param[out] output Buffer where the plaintext is to be written.
1651 * \param output_size Size of the \p output buffer in bytes.
1652 * \param[out] output_length On success, the number of bytes
1653 * that make up the output.
1654 *
1655 * \retval #PSA_SUCCESS
1656 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001657 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1658 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001659 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001660 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001661 * \retval #PSA_ERROR_NOT_SUPPORTED
1662 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001663 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1664 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1665 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1666 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1667 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1668 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001669 * \retval #PSA_ERROR_BAD_STATE
1670 * The library has not been previously initialized by psa_crypto_init().
1671 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001672 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001673 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001674psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001675 psa_algorithm_t alg,
1676 const uint8_t *input,
1677 size_t input_length,
1678 uint8_t *output,
1679 size_t output_size,
1680 size_t *output_length);
1681
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001682/** The type of the state data structure for multipart cipher operations.
1683 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001684 * Before calling any function on a cipher operation object, the application
1685 * must initialize it by any of the following means:
1686 * - Set the structure to all-bits-zero, for example:
1687 * \code
1688 * psa_cipher_operation_t operation;
1689 * memset(&operation, 0, sizeof(operation));
1690 * \endcode
1691 * - Initialize the structure to logical zero values, for example:
1692 * \code
1693 * psa_cipher_operation_t operation = {0};
1694 * \endcode
1695 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1696 * for example:
1697 * \code
1698 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1699 * \endcode
1700 * - Assign the result of the function psa_cipher_operation_init()
1701 * to the structure, for example:
1702 * \code
1703 * psa_cipher_operation_t operation;
1704 * operation = psa_cipher_operation_init();
1705 * \endcode
1706 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001707 * This is an implementation-defined \c struct. Applications should not
1708 * make any assumptions about the content of this structure except
1709 * as directed by the documentation of a specific implementation. */
1710typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1711
Jaeden Amero5bae2272019-01-04 11:48:27 +00001712/** \def PSA_CIPHER_OPERATION_INIT
1713 *
1714 * This macro returns a suitable initializer for a cipher operation object of
1715 * type #psa_cipher_operation_t.
1716 */
1717#ifdef __DOXYGEN_ONLY__
1718/* This is an example definition for documentation purposes.
1719 * Implementations should define a suitable value in `crypto_struct.h`.
1720 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001721#define PSA_CIPHER_OPERATION_INIT { 0 }
Jaeden Amero5bae2272019-01-04 11:48:27 +00001722#endif
1723
1724/** Return an initial value for a cipher operation object.
1725 */
1726static psa_cipher_operation_t psa_cipher_operation_init(void);
1727
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001728/** Set the key for a multipart symmetric encryption operation.
1729 *
1730 * The sequence of operations to encrypt a message with a symmetric cipher
1731 * is as follows:
1732 * -# Allocate an operation object which will be passed to all the functions
1733 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001734 * -# Initialize the operation object with one of the methods described in the
1735 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001736 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001737 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001738 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001739 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001740 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 * requires a specific IV value.
1742 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1743 * of the message each time.
1744 * -# Call psa_cipher_finish().
1745 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001746 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1747 * the operation will need to be reset by a call to psa_cipher_abort(). The
1748 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001749 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001750 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001751 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001752 * eventually terminate the operation. The following events terminate an
1753 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001754 * - A successful call to psa_cipher_finish().
1755 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001756 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001757 * \param[in,out] operation The operation object to set up. It must have
1758 * been initialized as per the documentation for
1759 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001760 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001761 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001762 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001763 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001764 * \param alg The cipher algorithm to compute
1765 * (\c PSA_ALG_XXX value such that
1766 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767 *
Gilles Peskine28538492018-07-11 17:34:00 +02001768 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001769 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001770 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1771 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001773 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001774 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001775 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001776 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1777 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1778 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1779 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1780 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001781 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001782 * The operation state is not valid (it must be inactive), or
1783 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001784 * It is implementation-dependent whether a failure to initialize
1785 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001787psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001788 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790
1791/** Set the key for a multipart symmetric decryption operation.
1792 *
1793 * The sequence of operations to decrypt a message with a symmetric cipher
1794 * is as follows:
1795 * -# Allocate an operation object which will be passed to all the functions
1796 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001797 * -# Initialize the operation object with one of the methods described in the
1798 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001799 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001800 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001801 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802 * decryption. If the IV is prepended to the ciphertext, you can call
1803 * psa_cipher_update() on a buffer containing the IV followed by the
1804 * beginning of the message.
1805 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1806 * of the message each time.
1807 * -# Call psa_cipher_finish().
1808 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001809 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1810 * the operation will need to be reset by a call to psa_cipher_abort(). The
1811 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001812 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001813 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001814 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001815 * eventually terminate the operation. The following events terminate an
1816 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001817 * - A successful call to psa_cipher_finish().
1818 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001820 * \param[in,out] operation The operation object to set up. It must have
1821 * been initialized as per the documentation for
1822 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001823 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001824 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001825 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001826 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001827 * \param alg The cipher algorithm to compute
1828 * (\c PSA_ALG_XXX value such that
1829 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001830 *
Gilles Peskine28538492018-07-11 17:34:00 +02001831 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001832 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001833 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1834 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001836 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001837 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001838 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001839 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1840 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1841 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1842 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1843 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001844 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001845 * The operation state is not valid (it must be inactive), or
1846 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001847 * It is implementation-dependent whether a failure to initialize
1848 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001849 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001850psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001851 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001852 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001853
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854/** Generate an IV for a symmetric encryption operation.
1855 *
1856 * This function generates a random IV (initialization vector), nonce
1857 * or initial counter value for the encryption operation as appropriate
1858 * for the chosen algorithm, key type and key size.
1859 *
1860 * The application must call psa_cipher_encrypt_setup() before
1861 * calling this function.
1862 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001863 * If this function returns an error status, the operation enters an error
1864 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001866 * \param[in,out] operation Active cipher operation.
1867 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001868 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001869 * \param[out] iv_length On success, the number of bytes of the
1870 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001871 *
1872 * \retval #PSA_SUCCESS
1873 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001875 * The size of the \p iv buffer is too small.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1878 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1879 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1880 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001881 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001882 * The operation state is not valid (it must be active, with no IV set),
1883 * or the library has not been previously initialized
1884 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001885 * It is implementation-dependent whether a failure to initialize
1886 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001888psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001889 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001890 size_t iv_size,
1891 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001892
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893/** Set the IV for a symmetric encryption or decryption operation.
1894 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001895 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 * or initial counter value for the encryption or decryption operation.
1897 *
1898 * The application must call psa_cipher_encrypt_setup() before
1899 * calling this function.
1900 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001901 * If this function returns an error status, the operation enters an error
1902 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 *
1904 * \note When encrypting, applications should use psa_cipher_generate_iv()
1905 * instead of this function, unless implementing a protocol that requires
1906 * a non-random IV.
1907 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param[in,out] operation Active cipher operation.
1909 * \param[in] iv Buffer containing the IV to use.
1910 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 *
1912 * \retval #PSA_SUCCESS
1913 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001915 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 * or the chosen algorithm does not use an IV.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1919 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1920 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1921 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001922 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001923 * The operation state is not valid (it must be an active cipher
1924 * encrypt operation, with no IV set), or the library has not been
1925 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001926 * It is implementation-dependent whether a failure to initialize
1927 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001928 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001929psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001930 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001931 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001932
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933/** Encrypt or decrypt a message fragment in an active cipher operation.
1934 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001935 * Before calling this function, you must:
1936 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1937 * The choice of setup function determines whether this function
1938 * encrypts or decrypts its input.
1939 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1940 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001942 * If this function returns an error status, the operation enters an error
1943 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001945 * \param[in,out] operation Active cipher operation.
1946 * \param[in] input Buffer containing the message fragment to
1947 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001948 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001949 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001950 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001951 * \param[out] output_length On success, the number of bytes
1952 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 *
1954 * \retval #PSA_SUCCESS
1955 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1957 * The size of the \p output buffer is too small.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001958 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1959 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1960 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1961 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1962 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001963 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001964 * The operation state is not valid (it must be active, with an IV set
1965 * if required for the algorithm), or the library has not been
1966 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001967 * It is implementation-dependent whether a failure to initialize
1968 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001969 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001970psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1971 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001972 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001973 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001974 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001975 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001976
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977/** Finish encrypting or decrypting a message in a cipher operation.
1978 *
1979 * The application must call psa_cipher_encrypt_setup() or
1980 * psa_cipher_decrypt_setup() before calling this function. The choice
1981 * of setup function determines whether this function encrypts or
1982 * decrypts its input.
1983 *
1984 * This function finishes the encryption or decryption of the message
1985 * formed by concatenating the inputs passed to preceding calls to
1986 * psa_cipher_update().
1987 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001988 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001989 * If this function returns an error status, the operation enters an error
1990 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001991 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001992 * \param[in,out] operation Active cipher operation.
1993 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001994 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001995 * \param[out] output_length On success, the number of bytes
1996 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001997 *
1998 * \retval #PSA_SUCCESS
1999 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002000 * \retval #PSA_ERROR_INVALID_ARGUMENT
2001 * The total input size passed to this operation is not valid for
2002 * this particular algorithm. For example, the algorithm is a based
2003 * on block cipher and requires a whole number of blocks, but the
2004 * total input size is not a multiple of the block size.
2005 * \retval #PSA_ERROR_INVALID_PADDING
2006 * This is a decryption operation for an algorithm that includes
2007 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002008 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2009 * The size of the \p output buffer is too small.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2012 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2013 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2014 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002015 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002016 * The operation state is not valid (it must be active, with an IV set
2017 * if required for the algorithm), or the library has not been
2018 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002019 * It is implementation-dependent whether a failure to initialize
2020 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002022psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002023 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002024 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002025 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002026
Gilles Peskinedcd14942018-07-12 00:30:52 +02002027/** Abort a cipher operation.
2028 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002029 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002030 * \p operation structure itself. Once aborted, the operation object
2031 * can be reused for another operation by calling
2032 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002033 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002034 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002035 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002037 * In particular, calling psa_cipher_abort() after the operation has been
2038 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2039 * is safe and has no effect.
2040 *
2041 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01002043 * \retval #PSA_SUCCESS \emptydescription
2044 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2045 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2046 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002047 * \retval #PSA_ERROR_BAD_STATE
2048 * The library has not been previously initialized by psa_crypto_init().
2049 * It is implementation-dependent whether a failure to initialize
2050 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002051 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002052psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2053
2054/**@}*/
2055
Gilles Peskine3b555712018-03-03 21:27:57 +01002056/** \defgroup aead Authenticated encryption with associated data (AEAD)
2057 * @{
2058 */
2059
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002061 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002062 * \param key Identifier of the key to use for the
2063 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002064 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002065 * \param alg The AEAD algorithm to compute
2066 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002067 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002068 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002070 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002071 * but not encrypted.
2072 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * encrypted.
2075 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077 * encrypted data. The additional data is not
2078 * part of this output. For algorithms where the
2079 * encrypted data and the authentication tag
2080 * are defined as separate outputs, the
2081 * authentication tag is appended to the
2082 * encrypted data.
2083 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002084 * This must be appropriate for the selected
2085 * algorithm and key:
2086 * - A sufficient output size is
2087 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2088 * \p alg, \p plaintext_length) where
2089 * \c key_type is the type of \p key.
2090 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2091 * plaintext_length) evaluates to the maximum
2092 * ciphertext size of any supported AEAD
2093 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002094 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002095 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002096 *
Gilles Peskine28538492018-07-11 17:34:00 +02002097 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002098 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002099 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2100 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002101 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002102 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002103 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002104 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002106 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002107 * \p ciphertext_size is too small.
2108 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2109 * \p plaintext_length) or
2110 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2111 * determine the required buffer size.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2113 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2114 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2115 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002116 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002117 * The library has not been previously initialized by psa_crypto_init().
2118 * It is implementation-dependent whether a failure to initialize
2119 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002121psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002122 psa_algorithm_t alg,
2123 const uint8_t *nonce,
2124 size_t nonce_length,
2125 const uint8_t *additional_data,
2126 size_t additional_data_length,
2127 const uint8_t *plaintext,
2128 size_t plaintext_length,
2129 uint8_t *ciphertext,
2130 size_t ciphertext_size,
2131 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002132
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002133/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002134 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002135 * \param key Identifier of the key to use for the
2136 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002137 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002138 * \param alg The AEAD algorithm to compute
2139 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002140 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * but not encrypted.
2145 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * encrypted. For algorithms where the
2148 * encrypted data and the authentication tag
2149 * are defined as separate inputs, the buffer
2150 * must contain the encrypted data followed
2151 * by the authentication tag.
2152 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002153 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002155 * This must be appropriate for the selected
2156 * algorithm and key:
2157 * - A sufficient output size is
2158 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2159 * \p alg, \p ciphertext_length) where
2160 * \c key_type is the type of \p key.
2161 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2162 * ciphertext_length) evaluates to the maximum
2163 * plaintext size of any supported AEAD
2164 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002165 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002166 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002167 *
Gilles Peskine28538492018-07-11 17:34:00 +02002168 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002169 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002170 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002172 * The ciphertext is not authentic.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002173 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002174 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002175 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002176 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002177 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002179 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002180 * \p plaintext_size is too small.
2181 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2182 * \p ciphertext_length) or
2183 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2184 * to determine the required buffer size.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2186 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2187 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2188 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002189 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002190 * The library has not been previously initialized by psa_crypto_init().
2191 * It is implementation-dependent whether a failure to initialize
2192 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002193 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002194psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002195 psa_algorithm_t alg,
2196 const uint8_t *nonce,
2197 size_t nonce_length,
2198 const uint8_t *additional_data,
2199 size_t additional_data_length,
2200 const uint8_t *ciphertext,
2201 size_t ciphertext_length,
2202 uint8_t *plaintext,
2203 size_t plaintext_size,
2204 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002205
Gilles Peskine30a9e412019-01-14 18:36:12 +01002206/** The type of the state data structure for multipart AEAD operations.
2207 *
2208 * Before calling any function on an AEAD operation object, the application
2209 * must initialize it by any of the following means:
2210 * - Set the structure to all-bits-zero, for example:
2211 * \code
2212 * psa_aead_operation_t operation;
2213 * memset(&operation, 0, sizeof(operation));
2214 * \endcode
2215 * - Initialize the structure to logical zero values, for example:
2216 * \code
2217 * psa_aead_operation_t operation = {0};
2218 * \endcode
2219 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2220 * for example:
2221 * \code
2222 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2223 * \endcode
2224 * - Assign the result of the function psa_aead_operation_init()
2225 * to the structure, for example:
2226 * \code
2227 * psa_aead_operation_t operation;
2228 * operation = psa_aead_operation_init();
2229 * \endcode
2230 *
2231 * This is an implementation-defined \c struct. Applications should not
2232 * make any assumptions about the content of this structure except
2233 * as directed by the documentation of a specific implementation. */
2234typedef struct psa_aead_operation_s psa_aead_operation_t;
2235
2236/** \def PSA_AEAD_OPERATION_INIT
2237 *
2238 * This macro returns a suitable initializer for an AEAD operation object of
2239 * type #psa_aead_operation_t.
2240 */
2241#ifdef __DOXYGEN_ONLY__
2242/* This is an example definition for documentation purposes.
2243 * Implementations should define a suitable value in `crypto_struct.h`.
2244 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245#define PSA_AEAD_OPERATION_INIT { 0 }
Gilles Peskine30a9e412019-01-14 18:36:12 +01002246#endif
2247
2248/** Return an initial value for an AEAD operation object.
2249 */
2250static psa_aead_operation_t psa_aead_operation_init(void);
2251
2252/** Set the key for a multipart authenticated encryption operation.
2253 *
2254 * The sequence of operations to encrypt a message with authentication
2255 * is as follows:
2256 * -# Allocate an operation object which will be passed to all the functions
2257 * listed here.
2258 * -# Initialize the operation object with one of the methods described in the
2259 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002260 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002261 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002262 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2263 * inputs to the subsequent calls to psa_aead_update_ad() and
2264 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2265 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2267 * generate or set the nonce. You should use
2268 * psa_aead_generate_nonce() unless the protocol you are implementing
2269 * requires a specific nonce value.
2270 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2271 * of the non-encrypted additional authenticated data each time.
2272 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002273 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002274 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002276 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2277 * the operation will need to be reset by a call to psa_aead_abort(). The
2278 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002279 * has been initialized.
2280 *
2281 * After a successful call to psa_aead_encrypt_setup(), the application must
2282 * eventually terminate the operation. The following events terminate an
2283 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002284 * - A successful call to psa_aead_finish().
2285 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 *
2287 * \param[in,out] operation The operation object to set up. It must have
2288 * been initialized as per the documentation for
2289 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002290 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002291 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002292 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002293 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * \param alg The AEAD algorithm to compute
2295 * (\c PSA_ALG_XXX value such that
2296 * #PSA_ALG_IS_AEAD(\p alg) is true).
2297 *
2298 * \retval #PSA_SUCCESS
2299 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002300 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002301 * The operation state is not valid (it must be inactive), or
2302 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskineec1eff32023-02-14 19:21:09 +01002303 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2304 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002306 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002307 * \retval #PSA_ERROR_NOT_SUPPORTED
2308 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002309 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2310 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2311 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2312 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002313 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002314 * The library has not been previously initialized by psa_crypto_init().
2315 * It is implementation-dependent whether a failure to initialize
2316 * results in this error code.
2317 */
2318psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002319 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 psa_algorithm_t alg);
2321
2322/** Set the key for a multipart authenticated decryption operation.
2323 *
2324 * The sequence of operations to decrypt a message with authentication
2325 * is as follows:
2326 * -# Allocate an operation object which will be passed to all the functions
2327 * listed here.
2328 * -# Initialize the operation object with one of the methods described in the
2329 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002330 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002331 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002332 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2333 * inputs to the subsequent calls to psa_aead_update_ad() and
2334 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2335 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002336 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2337 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2338 * of the non-encrypted additional authenticated data each time.
2339 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002340 * of the ciphertext to decrypt each time.
2341 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002343 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2344 * the operation will need to be reset by a call to psa_aead_abort(). The
2345 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002346 * has been initialized.
2347 *
2348 * After a successful call to psa_aead_decrypt_setup(), the application must
2349 * eventually terminate the operation. The following events terminate an
2350 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002351 * - A successful call to psa_aead_verify().
2352 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 *
2354 * \param[in,out] operation The operation object to set up. It must have
2355 * been initialized as per the documentation for
2356 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002357 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002358 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002359 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002360 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * \param alg The AEAD algorithm to compute
2362 * (\c PSA_ALG_XXX value such that
2363 * #PSA_ALG_IS_AEAD(\p alg) is true).
2364 *
2365 * \retval #PSA_SUCCESS
2366 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002367 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2368 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002369 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002370 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002371 * \retval #PSA_ERROR_NOT_SUPPORTED
2372 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2375 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2376 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2377 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002378 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002379 * The operation state is not valid (it must be inactive), or the
2380 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002381 * It is implementation-dependent whether a failure to initialize
2382 * results in this error code.
2383 */
2384psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002385 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002386 psa_algorithm_t alg);
2387
2388/** Generate a random nonce for an authenticated encryption operation.
2389 *
2390 * This function generates a random nonce for the authenticated encryption
2391 * operation with an appropriate size for the chosen algorithm, key type
2392 * and key size.
2393 *
2394 * The application must call psa_aead_encrypt_setup() before
2395 * calling this function.
2396 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002397 * If this function returns an error status, the operation enters an error
2398 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 *
2400 * \param[in,out] operation Active AEAD operation.
2401 * \param[out] nonce Buffer where the generated nonce is to be
2402 * written.
2403 * \param nonce_size Size of the \p nonce buffer in bytes.
2404 * \param[out] nonce_length On success, the number of bytes of the
2405 * generated nonce.
2406 *
2407 * \retval #PSA_SUCCESS
2408 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002409 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2410 * The size of the \p nonce buffer is too small.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002411 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2412 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2413 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2414 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2415 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002416 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002417 * The operation state is not valid (it must be an active aead encrypt
2418 * operation, with no nonce set), or the library has not been
2419 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002420 * It is implementation-dependent whether a failure to initialize
2421 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 */
2423psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002424 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 size_t nonce_size,
2426 size_t *nonce_length);
2427
2428/** Set the nonce for an authenticated encryption or decryption operation.
2429 *
2430 * This function sets the nonce for the authenticated
2431 * encryption or decryption operation.
2432 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002433 * The application must call psa_aead_encrypt_setup() or
2434 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002436 * If this function returns an error status, the operation enters an error
2437 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002438 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002439 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 * instead of this function, unless implementing a protocol that requires
2441 * a non-random IV.
2442 *
2443 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002444 * \param[in] nonce Buffer containing the nonce to use.
2445 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002446 *
2447 * \retval #PSA_SUCCESS
2448 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002449 * \retval #PSA_ERROR_INVALID_ARGUMENT
2450 * The size of \p nonce is not acceptable for the chosen algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002451 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2452 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2453 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2454 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2455 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002456 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002457 * The operation state is not valid (it must be active, with no nonce
2458 * set), or the library has not been previously initialized
2459 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002460 * It is implementation-dependent whether a failure to initialize
2461 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002462 */
2463psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002464 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002465 size_t nonce_length);
2466
Gilles Peskinebc59c852019-01-17 15:26:08 +01002467/** Declare the lengths of the message and additional data for AEAD.
2468 *
2469 * The application must call this function before calling
2470 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2471 * the operation requires it. If the algorithm does not require it,
2472 * calling this function is optional, but if this function is called
2473 * then the implementation must enforce the lengths.
2474 *
2475 * You may call this function before or after setting the nonce with
2476 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2477 *
2478 * - For #PSA_ALG_CCM, calling this function is required.
2479 * - For the other AEAD algorithms defined in this specification, calling
2480 * this function is not required.
2481 * - For vendor-defined algorithm, refer to the vendor documentation.
2482 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002483 * If this function returns an error status, the operation enters an error
2484 * state and must be aborted by calling psa_aead_abort().
2485 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002486 * \param[in,out] operation Active AEAD operation.
2487 * \param ad_length Size of the non-encrypted additional
2488 * authenticated data in bytes.
2489 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2490 *
2491 * \retval #PSA_SUCCESS
2492 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002493 * \retval #PSA_ERROR_INVALID_ARGUMENT
2494 * At least one of the lengths is not acceptable for the chosen
2495 * algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2498 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2499 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002500 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002501 * The operation state is not valid (it must be active, and
2502 * psa_aead_update_ad() and psa_aead_update() must not have been
2503 * called yet), or the library has not been previously initialized
2504 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002505 * It is implementation-dependent whether a failure to initialize
2506 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002507 */
2508psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2509 size_t ad_length,
2510 size_t plaintext_length);
2511
Gilles Peskine30a9e412019-01-14 18:36:12 +01002512/** Pass additional data to an active AEAD operation.
2513 *
2514 * Additional data is authenticated, but not encrypted.
2515 *
2516 * You may call this function multiple times to pass successive fragments
2517 * of the additional data. You may not call this function after passing
2518 * data to encrypt or decrypt with psa_aead_update().
2519 *
2520 * Before calling this function, you must:
2521 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2522 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2523 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002524 * If this function returns an error status, the operation enters an error
2525 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002526 *
2527 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2528 * there is no guarantee that the input is valid. Therefore, until
2529 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2530 * treat the input as untrusted and prepare to undo any action that
2531 * depends on the input if psa_aead_verify() returns an error status.
2532 *
2533 * \param[in,out] operation Active AEAD operation.
2534 * \param[in] input Buffer containing the fragment of
2535 * additional data.
2536 * \param input_length Size of the \p input buffer in bytes.
2537 *
2538 * \retval #PSA_SUCCESS
2539 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002540 * \retval #PSA_ERROR_INVALID_ARGUMENT
2541 * The total input length overflows the additional data length that
2542 * was previously specified with psa_aead_set_lengths().
Gilles Peskineec1eff32023-02-14 19:21:09 +01002543 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2544 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2545 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2546 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2547 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002548 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002549 * The operation state is not valid (it must be active, have a nonce
2550 * set, have lengths set if required by the algorithm, and
2551 * psa_aead_update() must not have been called yet), or the library
2552 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002553 * It is implementation-dependent whether a failure to initialize
2554 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002555 */
2556psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2557 const uint8_t *input,
2558 size_t input_length);
2559
2560/** Encrypt or decrypt a message fragment in an active AEAD operation.
2561 *
2562 * Before calling this function, you must:
2563 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2564 * The choice of setup function determines whether this function
2565 * encrypts or decrypts its input.
2566 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2567 * 3. Call psa_aead_update_ad() to pass all the additional data.
2568 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002569 * If this function returns an error status, the operation enters an error
2570 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002571 *
2572 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2573 * there is no guarantee that the input is valid. Therefore, until
2574 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2575 * - Do not use the output in any way other than storing it in a
2576 * confidential location. If you take any action that depends
2577 * on the tentative decrypted data, this action will need to be
2578 * undone if the input turns out not to be valid. Furthermore,
2579 * if an adversary can observe that this action took place
2580 * (for example through timing), they may be able to use this
2581 * fact as an oracle to decrypt any message encrypted with the
2582 * same key.
2583 * - In particular, do not copy the output anywhere but to a
2584 * memory or storage space that you have exclusive access to.
2585 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002586 * This function does not require the input to be aligned to any
2587 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002588 * a whole block at a time, it must consume all the input provided, but
2589 * it may delay the end of the corresponding output until a subsequent
2590 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2591 * provides sufficient input. The amount of data that can be delayed
2592 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002593 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002594 * \param[in,out] operation Active AEAD operation.
2595 * \param[in] input Buffer containing the message fragment to
2596 * encrypt or decrypt.
2597 * \param input_length Size of the \p input buffer in bytes.
2598 * \param[out] output Buffer where the output is to be written.
2599 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002600 * This must be appropriate for the selected
2601 * algorithm and key:
2602 * - A sufficient output size is
2603 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2604 * \c alg, \p input_length) where
2605 * \c key_type is the type of key and \c alg is
2606 * the algorithm that were used to set up the
2607 * operation.
2608 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2609 * input_length) evaluates to the maximum
2610 * output size of any supported AEAD
2611 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002612 * \param[out] output_length On success, the number of bytes
2613 * that make up the returned output.
2614 *
2615 * \retval #PSA_SUCCESS
2616 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002617 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2618 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002619 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2620 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2621 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002622 * \retval #PSA_ERROR_INVALID_ARGUMENT
2623 * The total length of input to psa_aead_update_ad() so far is
2624 * less than the additional data length that was previously
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002625 * specified with psa_aead_set_lengths(), or
2626 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002627 * was previously specified with psa_aead_set_lengths().
Gilles Peskineec1eff32023-02-14 19:21:09 +01002628 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2630 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2631 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2632 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002633 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002634 * The operation state is not valid (it must be active, have a nonce
2635 * set, and have lengths set if required by the algorithm), or the
2636 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002637 * It is implementation-dependent whether a failure to initialize
2638 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002639 */
2640psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2641 const uint8_t *input,
2642 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002643 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002644 size_t output_size,
2645 size_t *output_length);
2646
2647/** Finish encrypting a message in an AEAD operation.
2648 *
2649 * The operation must have been set up with psa_aead_encrypt_setup().
2650 *
2651 * This function finishes the authentication of the additional data
2652 * formed by concatenating the inputs passed to preceding calls to
2653 * psa_aead_update_ad() with the plaintext formed by concatenating the
2654 * inputs passed to preceding calls to psa_aead_update().
2655 *
2656 * This function has two output buffers:
2657 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002658 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002659 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002660 *
Shaun Case0e7791f2021-12-20 21:14:10 -08002661 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002662 * If this function returns an error status, the operation enters an error
2663 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002664 *
2665 * \param[in,out] operation Active AEAD operation.
2666 * \param[out] ciphertext Buffer where the last part of the ciphertext
2667 * is to be written.
2668 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002669 * This must be appropriate for the selected
2670 * algorithm and key:
2671 * - A sufficient output size is
2672 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2673 * \c alg) where \c key_type is the type of key
2674 * and \c alg is the algorithm that were used to
2675 * set up the operation.
2676 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2677 * the maximum output size of any supported AEAD
2678 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 * \param[out] ciphertext_length On success, the number of bytes of
2680 * returned ciphertext.
2681 * \param[out] tag Buffer where the authentication tag is
2682 * to be written.
2683 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002684 * This must be appropriate for the selected
2685 * algorithm and key:
2686 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2687 * key_type, \c key_bits, \c alg) where
2688 * \c key_type and \c key_bits are the type and
2689 * bit-size of the key, and \c alg is the
2690 * algorithm that were used in the call to
2691 * psa_aead_encrypt_setup().
2692 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2693 * maximum tag size of any supported AEAD
2694 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002695 * \param[out] tag_length On success, the number of bytes
2696 * that make up the returned tag.
2697 *
2698 * \retval #PSA_SUCCESS
2699 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002700 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002701 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002702 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2703 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2704 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2705 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2706 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002707 * \retval #PSA_ERROR_INVALID_ARGUMENT
2708 * The total length of input to psa_aead_update_ad() so far is
2709 * less than the additional data length that was previously
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002710 * specified with psa_aead_set_lengths(), or
2711 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002712 * less than the plaintext length that was previously
2713 * specified with psa_aead_set_lengths().
Gilles Peskineec1eff32023-02-14 19:21:09 +01002714 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2715 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2716 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2717 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2718 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002719 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002720 * The operation state is not valid (it must be an active encryption
2721 * operation with a nonce set), or the library has not been previously
2722 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002723 * It is implementation-dependent whether a failure to initialize
2724 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002725 */
2726psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002727 uint8_t *ciphertext,
2728 size_t ciphertext_size,
2729 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 uint8_t *tag,
2731 size_t tag_size,
2732 size_t *tag_length);
2733
2734/** Finish authenticating and decrypting a message in an AEAD operation.
2735 *
2736 * The operation must have been set up with psa_aead_decrypt_setup().
2737 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002738 * This function finishes the authenticated decryption of the message
2739 * components:
2740 *
2741 * - The additional data consisting of the concatenation of the inputs
2742 * passed to preceding calls to psa_aead_update_ad().
2743 * - The ciphertext consisting of the concatenation of the inputs passed to
2744 * preceding calls to psa_aead_update().
2745 * - The tag passed to this function call.
2746 *
2747 * If the authentication tag is correct, this function outputs any remaining
2748 * plaintext and reports success. If the authentication tag is not correct,
2749 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002750 *
Shaun Case0e7791f2021-12-20 21:14:10 -08002751 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002752 * If this function returns an error status, the operation enters an error
2753 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002754 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002755 * \note Implementations shall make the best effort to ensure that the
2756 * comparison between the actual tag and the expected tag is performed
2757 * in constant time.
2758 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002759 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002760 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002761 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002762 * from previous calls to psa_aead_update()
2763 * that could not be processed until the end
2764 * of the input.
2765 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002766 * This must be appropriate for the selected algorithm and key:
2767 * - A sufficient output size is
2768 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2769 * \c alg) where \c key_type is the type of key
2770 * and \c alg is the algorithm that were used to
2771 * set up the operation.
2772 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2773 * the maximum output size of any supported AEAD
2774 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002775 * \param[out] plaintext_length On success, the number of bytes of
2776 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002777 * \param[in] tag Buffer containing the authentication tag.
2778 * \param tag_length Size of the \p tag buffer in bytes.
2779 *
2780 * \retval #PSA_SUCCESS
2781 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002782 * \retval #PSA_ERROR_INVALID_SIGNATURE
2783 * The calculations were successful, but the authentication tag is
2784 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002785 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2786 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002787 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2788 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2789 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002790 * \retval #PSA_ERROR_INVALID_ARGUMENT
2791 * The total length of input to psa_aead_update_ad() so far is
2792 * less than the additional data length that was previously
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002793 * specified with psa_aead_set_lengths(), or
2794 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002795 * less than the plaintext length that was previously
2796 * specified with psa_aead_set_lengths().
Gilles Peskineec1eff32023-02-14 19:21:09 +01002797 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2798 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2799 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2800 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2801 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002802 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002803 * The operation state is not valid (it must be an active decryption
2804 * operation with a nonce set), or the library has not been previously
2805 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002806 * It is implementation-dependent whether a failure to initialize
2807 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002808 */
2809psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002810 uint8_t *plaintext,
2811 size_t plaintext_size,
2812 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002813 const uint8_t *tag,
2814 size_t tag_length);
2815
2816/** Abort an AEAD operation.
2817 *
2818 * Aborting an operation frees all associated resources except for the
2819 * \p operation structure itself. Once aborted, the operation object
2820 * can be reused for another operation by calling
2821 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2822 *
2823 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002824 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002825 *
2826 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002827 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2828 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002829 *
2830 * \param[in,out] operation Initialized AEAD operation.
2831 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01002832 * \retval #PSA_SUCCESS \emptydescription
2833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2834 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2835 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002836 * \retval #PSA_ERROR_BAD_STATE
2837 * The library has not been previously initialized by psa_crypto_init().
2838 * It is implementation-dependent whether a failure to initialize
2839 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002840 */
2841psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2842
Gilles Peskine3b555712018-03-03 21:27:57 +01002843/**@}*/
2844
Gilles Peskine20035e32018-02-03 22:44:14 +01002845/** \defgroup asymmetric Asymmetric cryptography
2846 * @{
2847 */
2848
2849/**
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002850 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2851 * this includes the hashing step.
2852 *
2853 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2854 * a multi-part hash operation and then pass the resulting hash to
Pengyu Lvf5131972022-11-08 18:17:00 +08002855 * psa_sign_hash(). PSA_ALG_SIGN_GET_HASH(\p alg) can be used to determine the
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002856 * hash algorithm to use.
2857 *
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002858 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002859 * It must be an asymmetric key pair. The key must
2860 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002861 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002862 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2863 * is true), that is compatible with the type of
2864 * \p key.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002865 * \param[in] input The input message to sign.
2866 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002867 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002868 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002869 * must be appropriate for the selected
2870 * algorithm and key:
2871 * - The required signature size is
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002872 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2873 * where \c key_type and \c key_bits are the type and
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002874 * bit-size respectively of key.
2875 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2876 * maximum signature size of any supported
2877 * signature algorithm.
2878 * \param[out] signature_length On success, the number of bytes that make up
2879 * the returned signature value.
2880 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01002881 * \retval #PSA_SUCCESS \emptydescription
2882 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002883 * \retval #PSA_ERROR_NOT_PERMITTED
2884 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2885 * or it does not permit the requested algorithm.
2886 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2887 * The size of the \p signature buffer is too small. You can
2888 * determine a sufficient buffer size by calling
2889 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2890 * where \c key_type and \c key_bits are the type and bit-size
2891 * respectively of \p key.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002892 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2893 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2894 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2895 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2896 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2897 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2898 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2899 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2900 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2901 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002902 * \retval #PSA_ERROR_BAD_STATE
2903 * The library has not been previously initialized by psa_crypto_init().
2904 * It is implementation-dependent whether a failure to initialize
2905 * results in this error code.
2906 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2908 psa_algorithm_t alg,
2909 const uint8_t *input,
2910 size_t input_length,
2911 uint8_t *signature,
2912 size_t signature_size,
2913 size_t *signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002914
2915/** \brief Verify the signature of a message with a public key, using
2916 * a hash-and-sign verification algorithm.
2917 *
2918 * \note To perform a multi-part hash-and-sign signature verification
2919 * algorithm, first use a multi-part hash operation to hash the message
2920 * and then pass the resulting hash to psa_verify_hash().
Pengyu Lvf5131972022-11-08 18:17:00 +08002921 * PSA_ALG_SIGN_GET_HASH(\p alg) can be used to determine the hash algorithm
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002922 * to use.
2923 *
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002924 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002925 * It must be a public key or an asymmetric key
2926 * pair. The key must allow the usage
2927 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002928 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002929 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2930 * is true), that is compatible with the type of
2931 * \p key.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002932 * \param[in] input The message whose signature is to be verified.
2933 * \param[in] input_length Size of the \p input buffer in bytes.
Valerio Settie5995e22024-06-03 07:05:23 +02002934 * \param[in] signature Buffer containing the signature to verify.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002935 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002936 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01002937 * \retval #PSA_SUCCESS \emptydescription
2938 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002939 * \retval #PSA_ERROR_NOT_PERMITTED
2940 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2941 * or it does not permit the requested algorithm.
2942 * \retval #PSA_ERROR_INVALID_SIGNATURE
2943 * The calculation was performed successfully, but the passed signature
2944 * is not a valid signature.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002945 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2946 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2949 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2950 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2951 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2952 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2953 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002954 * \retval #PSA_ERROR_BAD_STATE
2955 * The library has not been previously initialized by psa_crypto_init().
2956 * It is implementation-dependent whether a failure to initialize
2957 * results in this error code.
2958 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2960 psa_algorithm_t alg,
2961 const uint8_t *input,
2962 size_t input_length,
2963 const uint8_t *signature,
2964 size_t signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002965
2966/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002967 * \brief Sign a hash or short message with a private key.
2968 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002969 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002970 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002971 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2972 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002973 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2974 * to determine the hash algorithm to use.
2975 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002976 * \param key Identifier of the key to use for the operation.
2977 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002978 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzykd22362c2021-08-26 11:46:14 +02002979 * \param alg A signature algorithm (PSA_ALG_XXX
2980 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2981 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002982 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002983 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002984 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002985 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002986 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002987 * \param[out] signature_length On success, the number of bytes
2988 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002989 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01002990 * \retval #PSA_SUCCESS \emptydescription
2991 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2992 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002993 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002994 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002995 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002996 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002997 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002998 * respectively of \p key.
Gilles Peskineec1eff32023-02-14 19:21:09 +01002999 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3000 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3003 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3004 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3005 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3006 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003007 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003008 * The library has not been previously initialized by psa_crypto_init().
3009 * It is implementation-dependent whether a failure to initialize
3010 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003011 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003012psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003013 psa_algorithm_t alg,
3014 const uint8_t *hash,
3015 size_t hash_length,
3016 uint8_t *signature,
3017 size_t signature_size,
3018 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003019
3020/**
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003021 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003022 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003023 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003024 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003025 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3026 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003027 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3028 * to determine the hash algorithm to use.
3029 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003030 * \param key Identifier of the key to use for the operation. It
3031 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003032 * key must allow the usage
3033 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzykd22362c2021-08-26 11:46:14 +02003034 * \param alg A signature algorithm (PSA_ALG_XXX
3035 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3036 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003037 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003038 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003039 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003040 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003041 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003042 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003043 *
Gilles Peskine28538492018-07-11 17:34:00 +02003044 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003045 * The signature is valid.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003046 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3047 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003048 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case0e7791f2021-12-20 21:14:10 -08003049 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003050 * signature is not a valid signature.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003051 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3052 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3053 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3055 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3056 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3057 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003058 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003059 * The library has not been previously initialized by psa_crypto_init().
3060 * It is implementation-dependent whether a failure to initialize
3061 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003062 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003063psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003064 psa_algorithm_t alg,
3065 const uint8_t *hash,
3066 size_t hash_length,
3067 const uint8_t *signature,
3068 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003069
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003070/**
3071 * \brief Encrypt a short message with a public key.
3072 *
Shaun Case0e7791f2021-12-20 21:14:10 -08003073 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003074 * It must be a public key or an asymmetric key
3075 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003076 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003077 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003078 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003079 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003080 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003081 * \param[in] salt A salt or label, if supported by the
3082 * encryption algorithm.
3083 * If the algorithm does not support a
3084 * salt, pass \c NULL.
3085 * If the algorithm supports an optional
3086 * salt and you do not want to pass a salt,
3087 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003088 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003089 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3090 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003091 * \param salt_length Size of the \p salt buffer in bytes.
3092 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003093 * \param[out] output Buffer where the encrypted message is to
3094 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003095 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003096 * \param[out] output_length On success, the number of bytes
3097 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003098 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003099 * \retval #PSA_SUCCESS \emptydescription
3100 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3101 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003102 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003103 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003104 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003105 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003106 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003107 * respectively of \p key.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003108 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3109 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3111 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3112 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3113 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3114 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3115 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003116 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003117 * The library has not been previously initialized by psa_crypto_init().
3118 * It is implementation-dependent whether a failure to initialize
3119 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003120 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003121psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003122 psa_algorithm_t alg,
3123 const uint8_t *input,
3124 size_t input_length,
3125 const uint8_t *salt,
3126 size_t salt_length,
3127 uint8_t *output,
3128 size_t output_size,
3129 size_t *output_length);
3130
3131/**
3132 * \brief Decrypt a short message with a private key.
3133 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003134 * \param key Identifier of the key to use for the operation.
3135 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003136 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003137 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003138 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003139 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003140 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003141 * \param[in] salt A salt or label, if supported by the
3142 * encryption algorithm.
3143 * If the algorithm does not support a
3144 * salt, pass \c NULL.
3145 * If the algorithm supports an optional
3146 * salt and you do not want to pass a salt,
3147 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003148 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003149 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3150 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003151 * \param salt_length Size of the \p salt buffer in bytes.
3152 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003153 * \param[out] output Buffer where the decrypted message is to
3154 * be written.
3155 * \param output_size Size of the \c output buffer in bytes.
3156 * \param[out] output_length On success, the number of bytes
3157 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003158 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003159 * \retval #PSA_SUCCESS \emptydescription
3160 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3161 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003162 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003163 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003164 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003165 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003166 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003167 * respectively of \p key.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003168 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3169 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3170 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3172 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3173 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3174 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3175 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3176 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003177 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003178 * The library has not been previously initialized by psa_crypto_init().
3179 * It is implementation-dependent whether a failure to initialize
3180 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003181 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003182psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003183 psa_algorithm_t alg,
3184 const uint8_t *input,
3185 size_t input_length,
3186 const uint8_t *salt,
3187 size_t salt_length,
3188 uint8_t *output,
3189 size_t output_size,
3190 size_t *output_length);
3191
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003192/**@}*/
3193
Gilles Peskine35675b62019-05-16 17:26:11 +02003194/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195 * @{
3196 */
3197
Gilles Peskine35675b62019-05-16 17:26:11 +02003198/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003199 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003200 * Before calling any function on a key derivation operation object, the
3201 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202 * - Set the structure to all-bits-zero, for example:
3203 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003204 * psa_key_derivation_operation_t operation;
3205 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206 * \endcode
3207 * - Initialize the structure to logical zero values, for example:
3208 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003209 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003211 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212 * for example:
3213 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003214 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003215 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003216 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003217 * to the structure, for example:
3218 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003219 * psa_key_derivation_operation_t operation;
3220 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003221 * \endcode
3222 *
3223 * This is an implementation-defined \c struct. Applications should not
3224 * make any assumptions about the content of this structure except
3225 * as directed by the documentation of a specific implementation.
3226 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003227typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003228
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003229/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003230 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003231 * This macro returns a suitable initializer for a key derivation operation
3232 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003233 */
3234#ifdef __DOXYGEN_ONLY__
3235/* This is an example definition for documentation purposes.
3236 * Implementations should define a suitable value in `crypto_struct.h`.
3237 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02003239#endif
3240
Gilles Peskine35675b62019-05-16 17:26:11 +02003241/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003242 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003243static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003244
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003245/** Set up a key derivation operation.
3246 *
3247 * A key derivation algorithm takes some inputs and uses them to generate
3248 * a byte stream in a deterministic way.
3249 * This byte stream can be used to produce keys and other
3250 * cryptographic material.
3251 *
3252 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003253 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3254 * -# Call psa_key_derivation_setup() to select the algorithm.
3255 * -# Provide the inputs for the key derivation by calling
3256 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3257 * as appropriate. Which inputs are needed, in what order, and whether
3258 * they may be keys and if so of what type depends on the algorithm.
3259 * -# Optionally set the operation's maximum capacity with
3260 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3261 * of or after providing inputs. For some algorithms, this step is mandatory
3262 * because the output depends on the maximum capacity.
3263 * -# To derive a key, call psa_key_derivation_output_key().
3264 * To derive a byte string for a different purpose, call
3265 * psa_key_derivation_output_bytes().
3266 * Successive calls to these functions use successive output bytes
3267 * calculated by the key derivation algorithm.
3268 * -# Clean up the key derivation operation object with
3269 * psa_key_derivation_abort().
3270 *
3271 * If this function returns an error, the key derivation operation object is
3272 * not changed.
3273 *
3274 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3275 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003276 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003277 * Implementations must reject an attempt to derive a key of size 0.
3278 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003279 * \param[in,out] operation The key derivation operation object
3280 * to set up. It must
3281 * have been initialized but not set up yet.
3282 * \param alg The key derivation algorithm to compute
3283 * (\c PSA_ALG_XXX value such that
3284 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3285 *
3286 * \retval #PSA_SUCCESS
3287 * Success.
3288 * \retval #PSA_ERROR_INVALID_ARGUMENT
3289 * \c alg is not a key derivation algorithm.
3290 * \retval #PSA_ERROR_NOT_SUPPORTED
3291 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003292 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3294 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3295 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3296 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003297 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003298 * The operation state is not valid (it must be inactive), or
3299 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003300 * It is implementation-dependent whether a failure to initialize
3301 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003302 */
3303psa_status_t psa_key_derivation_setup(
3304 psa_key_derivation_operation_t *operation,
3305 psa_algorithm_t alg);
3306
Gilles Peskine35675b62019-05-16 17:26:11 +02003307/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003308 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003309 * The capacity of a key derivation is the maximum number of bytes that it can
3310 * return. When you get *N* bytes of output from a key derivation operation,
3311 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003312 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003313 * \param[in] operation The operation to query.
3314 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003315 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003316 * \retval #PSA_SUCCESS \emptydescription
3317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3318 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3319 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003320 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003321 * The operation state is not valid (it must be active), or
3322 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003323 * It is implementation-dependent whether a failure to initialize
3324 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003325 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003326psa_status_t psa_key_derivation_get_capacity(
3327 const psa_key_derivation_operation_t *operation,
3328 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003329
Gilles Peskine35675b62019-05-16 17:26:11 +02003330/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003331 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003332 * The capacity of a key derivation operation is the maximum number of bytes
3333 * that the key derivation operation can return from this point onwards.
3334 *
3335 * \param[in,out] operation The key derivation operation object to modify.
3336 * \param capacity The new capacity of the operation.
3337 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003338 * current capacity.
3339 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003340 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003341 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003342 * \p capacity is larger than the operation's current capacity.
3343 * In this case, the operation object remains valid and its capacity
3344 * remains unchanged.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3346 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3347 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003348 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003349 * The operation state is not valid (it must be active), or the
3350 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003351 * It is implementation-dependent whether a failure to initialize
3352 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003353 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003354psa_status_t psa_key_derivation_set_capacity(
3355 psa_key_derivation_operation_t *operation,
3356 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003357
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003358/** Use the maximum possible capacity for a key derivation operation.
3359 *
3360 * Use this value as the capacity argument when setting up a key derivation
3361 * to indicate that the operation should have the maximum possible capacity.
3362 * The value of the maximum possible capacity depends on the key derivation
3363 * algorithm.
3364 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003366
3367/** Provide an input for key derivation or key agreement.
3368 *
3369 * Which inputs are required and in what order depends on the algorithm.
3370 * Refer to the documentation of each key derivation or key agreement
3371 * algorithm for information.
3372 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003373 * This function passes direct inputs, which is usually correct for
3374 * non-secret inputs. To pass a secret input, which should be in a key
3375 * object, call psa_key_derivation_input_key() instead of this function.
3376 * Refer to the documentation of individual step types
3377 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3378 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003379 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003380 * If this function returns an error status, the operation enters an error
3381 * state and must be aborted by calling psa_key_derivation_abort().
3382 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003383 * \param[in,out] operation The key derivation operation object to use.
3384 * It must have been set up with
3385 * psa_key_derivation_setup() and must not
3386 * have produced any output yet.
3387 * \param step Which step the input data is for.
3388 * \param[in] data Input data to use.
3389 * \param data_length Size of the \p data buffer in bytes.
3390 *
3391 * \retval #PSA_SUCCESS
3392 * Success.
3393 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003394 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003395 * \c step does not allow direct inputs.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003396 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3397 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3398 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3399 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3400 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003401 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003402 * The operation state is not valid for this input \p step, or
3403 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003404 * It is implementation-dependent whether a failure to initialize
3405 * results in this error code.
3406 */
3407psa_status_t psa_key_derivation_input_bytes(
3408 psa_key_derivation_operation_t *operation,
3409 psa_key_derivation_step_t step,
3410 const uint8_t *data,
3411 size_t data_length);
3412
3413/** Provide an input for key derivation in the form of a key.
3414 *
3415 * Which inputs are required and in what order depends on the algorithm.
3416 * Refer to the documentation of each key derivation or key agreement
3417 * algorithm for information.
3418 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003419 * This function obtains input from a key object, which is usually correct for
3420 * secret inputs or for non-secret personalization strings kept in the key
3421 * store. To pass a non-secret parameter which is not in the key store,
3422 * call psa_key_derivation_input_bytes() instead of this function.
3423 * Refer to the documentation of individual step types
3424 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3425 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003426 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003427 * If this function returns an error status, the operation enters an error
3428 * state and must be aborted by calling psa_key_derivation_abort().
3429 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003430 * \param[in,out] operation The key derivation operation object to use.
3431 * It must have been set up with
3432 * psa_key_derivation_setup() and must not
3433 * have produced any output yet.
3434 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003435 * \param key Identifier of the key. It must have an
3436 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003437 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003438 *
3439 * \retval #PSA_SUCCESS
3440 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003441 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3442 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003443 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003444 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine224b0d62019-09-23 18:13:17 +02003445 * \c step does not allow key inputs of the given type
3446 * or does not allow key inputs at all.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003447 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3448 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3449 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3450 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3451 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003452 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003453 * The operation state is not valid for this input \p step, or
3454 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003455 * It is implementation-dependent whether a failure to initialize
3456 * results in this error code.
3457 */
3458psa_status_t psa_key_derivation_input_key(
3459 psa_key_derivation_operation_t *operation,
3460 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003461 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003462
3463/** Perform a key agreement and use the shared secret as input to a key
3464 * derivation.
3465 *
3466 * A key agreement algorithm takes two inputs: a private key \p private_key
3467 * a public key \p peer_key.
3468 * The result of this function is passed as input to a key derivation.
3469 * The output of this key derivation can be extracted by reading from the
3470 * resulting operation to produce keys and other cryptographic material.
3471 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003472 * If this function returns an error status, the operation enters an error
3473 * state and must be aborted by calling psa_key_derivation_abort().
3474 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003475 * \param[in,out] operation The key derivation operation object to use.
3476 * It must have been set up with
3477 * psa_key_derivation_setup() with a
3478 * key agreement and derivation algorithm
3479 * \c alg (\c PSA_ALG_XXX value such that
3480 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3481 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3482 * is false).
3483 * The operation must be ready for an
3484 * input of the type given by \p step.
3485 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003486 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003487 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003488 * \param[in] peer_key Public key of the peer. The peer key must be in the
3489 * same format that psa_import_key() accepts for the
3490 * public key type corresponding to the type of
3491 * private_key. That is, this function performs the
3492 * equivalent of
3493 * #psa_import_key(...,
3494 * `peer_key`, `peer_key_length`) where
3495 * with key attributes indicating the public key
3496 * type corresponding to the type of `private_key`.
3497 * For example, for EC keys, this means that peer_key
3498 * is interpreted as a point on the curve that the
3499 * private key is on. The standard formats for public
3500 * keys are documented in the documentation of
3501 * psa_export_public_key().
3502 * \param peer_key_length Size of \p peer_key in bytes.
3503 *
3504 * \retval #PSA_SUCCESS
3505 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003506 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3507 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003508 * \retval #PSA_ERROR_INVALID_ARGUMENT
3509 * \c private_key is not compatible with \c alg,
3510 * or \p peer_key is not valid for \c alg or not compatible with
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003511 * \c private_key, or \c step does not allow an input resulting
3512 * from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003513 * \retval #PSA_ERROR_NOT_SUPPORTED
3514 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003515 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3517 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3518 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3519 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003520 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003521 * The operation state is not valid for this key agreement \p step,
3522 * or the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003523 * It is implementation-dependent whether a failure to initialize
3524 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003525 */
3526psa_status_t psa_key_derivation_key_agreement(
3527 psa_key_derivation_operation_t *operation,
3528 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003529 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003530 const uint8_t *peer_key,
3531 size_t peer_key_length);
3532
Gilles Peskine35675b62019-05-16 17:26:11 +02003533/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003534 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003535 * This function calculates output bytes from a key derivation algorithm and
3536 * return those bytes.
3537 * If you view the key derivation's output as a stream of bytes, this
3538 * function destructively reads the requested number of bytes from the
3539 * stream.
3540 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003541 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003542 * If this function returns an error status other than
3543 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003544 * state and must be aborted by calling psa_key_derivation_abort().
3545 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003546 * \param[in,out] operation The key derivation operation object to read from.
3547 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003548 * \param output_length Number of bytes to output.
3549 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003550 * \retval #PSA_SUCCESS \emptydescription
David Saadab4ecc272019-02-14 13:48:10 +02003551 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003552 * The operation's capacity was less than
3553 * \p output_length bytes. Note that in this case,
3554 * no output is written to the output buffer.
3555 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003556 * subsequent calls to this function will not
3557 * succeed, even with a smaller output buffer.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003558 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3559 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3560 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3561 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3562 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003563 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003564 * The operation state is not valid (it must be active and completed
3565 * all required input steps), or the library has not been previously
3566 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003567 * It is implementation-dependent whether a failure to initialize
3568 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003569 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003570psa_status_t psa_key_derivation_output_bytes(
3571 psa_key_derivation_operation_t *operation,
3572 uint8_t *output,
3573 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003574
Gilles Peskine35675b62019-05-16 17:26:11 +02003575/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003576 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003577 * This function calculates output bytes from a key derivation algorithm
3578 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003579 * The key's location, usage policy, type and size are taken from
3580 * \p attributes.
3581 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003582 * If you view the key derivation's output as a stream of bytes, this
3583 * function destructively reads as many bytes as required from the
3584 * stream.
3585 * The operation's capacity decreases by the number of bytes read.
3586 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003587 * If this function returns an error status other than
3588 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003589 * state and must be aborted by calling psa_key_derivation_abort().
3590 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003591 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003592 * the key is derived, depends on the key type and on the key size
3593 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003594 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003595 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003596 * of a given size, this function is functionally equivalent to
3597 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003598 * and passing the resulting output to #psa_import_key.
3599 * However, this function has a security benefit:
3600 * if the implementation provides an isolation boundary then
3601 * the key material is not exposed outside the isolation boundary.
3602 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003603 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003604 * The following key types defined in this specification follow this scheme:
3605 *
3606 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003607 * - #PSA_KEY_TYPE_ARC4;
Gilles Peskine8890f642021-09-21 11:59:39 +02003608 * - #PSA_KEY_TYPE_ARIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003609 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003610 * - #PSA_KEY_TYPE_DERIVE;
3611 * - #PSA_KEY_TYPE_HMAC.
3612 *
3613 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003614 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003615 * Montgomery curve), this function always draws a byte string whose
3616 * length is determined by the curve, and sets the mandatory bits
3617 * accordingly. That is:
3618 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003619 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003620 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003621 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003622 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003623 *
3624 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003625 * \c bits bits with constraints as to which bit sequences are acceptable,
3626 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003627 * up to the nearest whole number of bytes. If the resulting byte string
3628 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3629 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003630 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003631 * for the output produced by psa_export_key().
3632 * The following key types defined in this specification follow this scheme:
3633 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003634 * - #PSA_KEY_TYPE_DES.
3635 * Force-set the parity bits, but discard forbidden weak keys.
3636 * For 2-key and 3-key triple-DES, the three keys are generated
3637 * successively (for example, for 3-key triple-DES,
3638 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3639 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003640 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003641 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003642 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003643 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003644 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003645 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003646 * Weierstrass curve).
3647 * For these key types, interpret the byte string as integer
3648 * in big-endian order. Discard it if it is not in the range
3649 * [0, *N* - 2] where *N* is the boundary of the private key domain
3650 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003651 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003652 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003653 * This method allows compliance to NIST standards, specifically
3654 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003655 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3656 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3657 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3658 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003659 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003660 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003661 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003662 * implementation-defined.
3663 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003664 * In all cases, the data that is read is discarded from the operation.
3665 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003666 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003667 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3668 * the input to that step must be provided with psa_key_derivation_input_key().
3669 * Future versions of this specification may include additional restrictions
3670 * on the derived key based on the attributes and strength of the secret key.
3671 *
Gilles Peskine20628592019-04-19 19:29:50 +02003672 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003673 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003674 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003675 * key. For persistent keys, this is the key
3676 * identifier defined in \p attributes.
3677 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003678 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003679 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003680 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003681 * If the key is persistent, the key material and the key's metadata
3682 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003683 * \retval #PSA_ERROR_ALREADY_EXISTS
3684 * This is an attempt to create a persistent key, and there is
3685 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003686 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003687 * There was not enough data to create the desired key.
3688 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003689 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003690 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003691 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003692 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003693 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003694 * \retval #PSA_ERROR_INVALID_ARGUMENT
3695 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003696 * \retval #PSA_ERROR_NOT_PERMITTED
3697 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3698 * a key.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003699 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3700 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3701 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3702 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3703 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3704 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3705 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3706 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003707 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003708 * The operation state is not valid (it must be active and completed
3709 * all required input steps), or the library has not been previously
3710 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003711 * It is implementation-dependent whether a failure to initialize
3712 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003713 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003714psa_status_t psa_key_derivation_output_key(
3715 const psa_key_attributes_t *attributes,
3716 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003717 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003718
Gilles Peskine35675b62019-05-16 17:26:11 +02003719/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003720 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003721 * Aborting an operation frees all associated resources except for the \c
3722 * operation structure itself. Once aborted, the operation object can be reused
3723 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003724 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003725 * This function may be called at any time after the operation
3726 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003727 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003728 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3729 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003730 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003731 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003732 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003733 * \retval #PSA_SUCCESS \emptydescription
3734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3735 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3736 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003737 * \retval #PSA_ERROR_BAD_STATE
3738 * The library has not been previously initialized by psa_crypto_init().
3739 * It is implementation-dependent whether a failure to initialize
3740 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003741 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003742psa_status_t psa_key_derivation_abort(
3743 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003744
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003745/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003746 *
3747 * \warning The raw result of a key agreement algorithm such as finite-field
3748 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3749 * not be used directly as key material. It should instead be passed as
3750 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003751 * a key derivation, use psa_key_derivation_key_agreement() and other
3752 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003753 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003754 * \param alg The key agreement algorithm to compute
3755 * (\c PSA_ALG_XXX value such that
3756 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3757 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003758 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003759 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003760 * \param[in] peer_key Public key of the peer. It must be
3761 * in the same format that psa_import_key()
3762 * accepts. The standard formats for public
3763 * keys are documented in the documentation
3764 * of psa_export_public_key().
3765 * \param peer_key_length Size of \p peer_key in bytes.
3766 * \param[out] output Buffer where the decrypted message is to
3767 * be written.
3768 * \param output_size Size of the \c output buffer in bytes.
3769 * \param[out] output_length On success, the number of bytes
3770 * that make up the returned output.
3771 *
3772 * \retval #PSA_SUCCESS
3773 * Success.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003774 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3775 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine769c7a62019-01-18 16:42:29 +01003776 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003777 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01003778 * \p private_key is not compatible with \p alg,
3779 * or \p peer_key is not valid for \p alg or not compatible with
3780 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003781 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3782 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003783 * \retval #PSA_ERROR_NOT_SUPPORTED
3784 * \p alg is not a supported key agreement algorithm.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003785 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3786 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3787 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3788 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3789 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003790 * \retval #PSA_ERROR_BAD_STATE
3791 * The library has not been previously initialized by psa_crypto_init().
3792 * It is implementation-dependent whether a failure to initialize
3793 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003794 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003795psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003796 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003797 const uint8_t *peer_key,
3798 size_t peer_key_length,
3799 uint8_t *output,
3800 size_t output_size,
3801 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003802
Gilles Peskineea0fb492018-07-12 17:17:20 +02003803/**@}*/
3804
Gilles Peskineedd76872018-07-20 17:42:05 +02003805/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003806 * @{
3807 */
3808
3809/**
3810 * \brief Generate random bytes.
3811 *
3812 * \warning This function **can** fail! Callers MUST check the return status
3813 * and MUST NOT use the content of the output buffer if the return
3814 * status is not #PSA_SUCCESS.
3815 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003816 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003817 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003818 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003819 * \param output_size Number of bytes to generate and output.
3820 *
Gilles Peskineec1eff32023-02-14 19:21:09 +01003821 * \retval #PSA_SUCCESS \emptydescription
3822 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3823 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3824 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3825 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3826 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3827 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir0adf0fc2018-09-06 16:24:41 +03003828 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003829 * The library has not been previously initialized by psa_crypto_init().
3830 * It is implementation-dependent whether a failure to initialize
3831 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003832 */
3833psa_status_t psa_generate_random(uint8_t *output,
3834 size_t output_size);
3835
3836/**
3837 * \brief Generate a key or key pair.
3838 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003839 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003840 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003841 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003842 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003843 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003844 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003845 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003846 * the public exponent is 65537.
3847 * The modulus is a product of two probabilistic primes
3848 * between 2^{n-1} and 2^n where n is the bit size specified in the
3849 * attributes.
3850 *
Gilles Peskine20628592019-04-19 19:29:50 +02003851 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003852 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003853 * key. For persistent keys, this is the key
3854 * identifier defined in \p attributes.
3855 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003856 *
Gilles Peskine28538492018-07-11 17:34:00 +02003857 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003858 * Success.
3859 * If the key is persistent, the key material and the key's metadata
3860 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003861 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003862 * This is an attempt to create a persistent key, and there is
3863 * already a persistent key with the given identifier.
Gilles Peskineec1eff32023-02-14 19:21:09 +01003864 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3865 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3867 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3869 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3870 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3871 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3872 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3873 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3874 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003875 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003876 * The library has not been previously initialized by psa_crypto_init().
3877 * It is implementation-dependent whether a failure to initialize
3878 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003879 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003880psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003881 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003882
3883/**@}*/
3884
Gilles Peskinee59236f2018-01-27 23:32:46 +01003885#ifdef __cplusplus
3886}
3887#endif
3888
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003889/* The file "crypto_sizes.h" contains definitions for size calculation
3890 * macros whose definitions are implementation-specific. */
3891#include "crypto_sizes.h"
3892
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003893/* The file "crypto_struct.h" contains definitions for
3894 * implementation-specific structs that are declared above. */
3895#include "crypto_struct.h"
3896
3897/* The file "crypto_extra.h" contains vendor-specific definitions. This
3898 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003899#include "crypto_extra.h"
3900
3901#endif /* PSA_CRYPTO_H */