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