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