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