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