scorpiox-bot-plugin-webhook
←crypto.hc
4998 lines · 240 KBHEADBlameDownload
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 &sect;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 &sect;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 * For #PSA_ALG_CHACHA20_POLY1305, this must
2077 * not exceed `UINT32_MAX * 64` bytes.
2078 * \param[out] ciphertext Output buffer for the authenticated and
2079 * encrypted data. The additional data is not
2080 * part of this output. For algorithms where the
2081 * encrypted data and the authentication tag
2082 * are defined as separate outputs, the
2083 * authentication tag is appended to the
2084 * encrypted data.
2085 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2086 * This must be appropriate for the selected
2087 * algorithm and key:
2088 * - A sufficient output size is
2089 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2090 * \p alg, \p plaintext_length) where
2091 * \c key_type is the type of \p key.
2092 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2093 * plaintext_length) evaluates to the maximum
2094 * ciphertext size of any supported AEAD
2095 * encryption.
2096 * \param[out] ciphertext_length On success, the size of the output
2097 * in the \p ciphertext buffer.
2098 *
2099 * \retval #PSA_SUCCESS
2100 * Success.
2101 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2102 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2103 * \retval #PSA_ERROR_INVALID_ARGUMENT
2104 * \p key is not compatible with \p alg, or \p plaintext_length
2105 * is not acceptable for \p alg.
2106 * \retval #PSA_ERROR_NOT_SUPPORTED
2107 * \p alg is not supported or is not an AEAD algorithm.
2108 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2109 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2110 * \p ciphertext_size is too small.
2111 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2112 * \p plaintext_length) or
2113 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2114 * determine the required buffer size.
2115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2116 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2117 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2118 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2119 * \retval #PSA_ERROR_BAD_STATE
2120 * The library has not been previously initialized by psa_crypto_init().
2121 * It is implementation-dependent whether a failure to initialize
2122 * results in this error code.
2123 */
2124psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
2125 psa_algorithm_t alg,
2126 const uint8_t *nonce,
2127 size_t nonce_length,
2128 const uint8_t *additional_data,
2129 size_t additional_data_length,
2130 const uint8_t *plaintext,
2131 size_t plaintext_length,
2132 uint8_t *ciphertext,
2133 size_t ciphertext_size,
2134 size_t *ciphertext_length);
2135
2136/** Process an authenticated decryption operation.
2137 *
2138 * \param key Identifier of the key to use for the
2139 * operation. It must allow the usage
2140 * #PSA_KEY_USAGE_DECRYPT.
2141 * \param alg The AEAD algorithm to compute
2142 * (\c PSA_ALG_XXX value such that
2143 * #PSA_ALG_IS_AEAD(\p alg) is true).
2144 * \param[in] nonce Nonce or IV to use.
2145 * \param nonce_length Size of the \p nonce buffer in bytes.
2146 * \param[in] additional_data Additional data that has been authenticated
2147 * but not encrypted.
2148 * \param additional_data_length Size of \p additional_data in bytes.
2149 * \param[in] ciphertext Data that has been authenticated and
2150 * encrypted. For algorithms where the
2151 * encrypted data and the authentication tag
2152 * are defined as separate inputs, the buffer
2153 * must contain the encrypted data followed
2154 * by the authentication tag.
2155 * \param ciphertext_length Size of \p ciphertext in bytes.
2156 * For #PSA_ALG_CHACHA20_POLY1305, the
2157 * encrypted data length, excluding the
2158 * authentication tag, must not exceed
2159 * `UINT32_MAX * 64` bytes.
2160 * \param[out] plaintext Output buffer for the decrypted data.
2161 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2162 * This must be appropriate for the selected
2163 * algorithm and key:
2164 * - A sufficient output size is
2165 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2166 * \p alg, \p ciphertext_length) where
2167 * \c key_type is the type of \p key.
2168 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2169 * ciphertext_length) evaluates to the maximum
2170 * plaintext size of any supported AEAD
2171 * decryption.
2172 * \param[out] plaintext_length On success, the size of the output
2173 * in the \p plaintext buffer.
2174 *
2175 * \retval #PSA_SUCCESS
2176 * Success.
2177 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2178 * \retval #PSA_ERROR_INVALID_SIGNATURE
2179 * The ciphertext is not authentic.
2180 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2181 * \retval #PSA_ERROR_INVALID_ARGUMENT
2182 * \p key is not compatible with \p alg, or the encrypted data length
2183 * is not acceptable for \p alg.
2184 * \retval #PSA_ERROR_NOT_SUPPORTED
2185 * \p alg is not supported or is not an AEAD algorithm.
2186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2187 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2188 * \p plaintext_size is too small.
2189 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2190 * \p ciphertext_length) or
2191 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2192 * to determine the required buffer size.
2193 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2194 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2195 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2196 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2197 * \retval #PSA_ERROR_BAD_STATE
2198 * The library has not been previously initialized by psa_crypto_init().
2199 * It is implementation-dependent whether a failure to initialize
2200 * results in this error code.
2201 */
2202psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
2203 psa_algorithm_t alg,
2204 const uint8_t *nonce,
2205 size_t nonce_length,
2206 const uint8_t *additional_data,
2207 size_t additional_data_length,
2208 const uint8_t *ciphertext,
2209 size_t ciphertext_length,
2210 uint8_t *plaintext,
2211 size_t plaintext_size,
2212 size_t *plaintext_length);
2213
2214/** The type of the state data structure for multipart AEAD operations.
2215 *
2216 * Before calling any function on an AEAD operation object, the application
2217 * must initialize it by any of the following means:
2218 * - Set the structure to all-bits-zero, for example:
2219 * \code
2220 * psa_aead_operation_t operation;
2221 * memset(&operation, 0, sizeof(operation));
2222 * \endcode
2223 * - Initialize the structure to logical zero values, for example:
2224 * \code
2225 * psa_aead_operation_t operation = {0};
2226 * \endcode
2227 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2228 * for example:
2229 * \code
2230 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2231 * \endcode
2232 * - Assign the result of the function psa_aead_operation_init()
2233 * to the structure, for example:
2234 * \code
2235 * psa_aead_operation_t operation;
2236 * operation = psa_aead_operation_init();
2237 * \endcode
2238 *
2239 * This is an implementation-defined \c struct. Applications should not
2240 * make any assumptions about the content of this structure.
2241 * Implementation details can change in future versions without notice. */
2242typedef struct psa_aead_operation_s psa_aead_operation_t;
2243
2244/** \def PSA_AEAD_OPERATION_INIT
2245 *
2246 * This macro returns a suitable initializer for an AEAD operation object of
2247 * type #psa_aead_operation_t.
2248 */
2249
2250/** Return an initial value for an AEAD operation object.
2251 */
2252static psa_aead_operation_t psa_aead_operation_init(void);
2253
2254/** Set the key for a multipart authenticated encryption operation.
2255 *
2256 * The sequence of operations to encrypt a message with authentication
2257 * is as follows:
2258 * -# Allocate an operation object which will be passed to all the functions
2259 * listed here.
2260 * -# Initialize the operation object with one of the methods described in the
2261 * documentation for #psa_aead_operation_t, e.g.
2262 * #PSA_AEAD_OPERATION_INIT.
2263 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2264 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2265 * inputs to the subsequent calls to psa_aead_update_ad() and
2266 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2267 * for details.
2268 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2269 * generate or set the nonce. You should use
2270 * psa_aead_generate_nonce() unless the protocol you are implementing
2271 * requires a specific nonce value.
2272 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2273 * of the non-encrypted additional authenticated data each time.
2274 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2275 * of the message to encrypt each time.
2276 * -# Call psa_aead_finish().
2277 *
2278 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2279 * the operation will need to be reset by a call to psa_aead_abort(). The
2280 * application may call psa_aead_abort() at any time after the operation
2281 * has been initialized.
2282 *
2283 * After a successful call to psa_aead_encrypt_setup(), the application must
2284 * eventually terminate the operation. The following events terminate an
2285 * operation:
2286 * - A successful call to psa_aead_finish().
2287 * - A call to psa_aead_abort().
2288 *
2289 * \param[in,out] operation The operation object to set up. It must have
2290 * been initialized as per the documentation for
2291 * #psa_aead_operation_t and not yet in use.
2292 * \param key Identifier of the key to use for the operation.
2293 * It must remain valid until the operation
2294 * terminates. It must allow the usage
2295 * #PSA_KEY_USAGE_ENCRYPT.
2296 * \param alg The AEAD algorithm to compute
2297 * (\c PSA_ALG_XXX value such that
2298 * #PSA_ALG_IS_AEAD(\p alg) is true).
2299 *
2300 * \retval #PSA_SUCCESS
2301 * Success.
2302 * \retval #PSA_ERROR_BAD_STATE
2303 * The operation state is not valid (it must be inactive), or
2304 * the library has not been previously initialized by psa_crypto_init().
2305 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2306 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2307 * \retval #PSA_ERROR_INVALID_ARGUMENT
2308 * \p key is not compatible with \p alg.
2309 * \retval #PSA_ERROR_NOT_SUPPORTED
2310 * \p alg is not supported or is not an AEAD algorithm.
2311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2313 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2314 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2315 * \retval #PSA_ERROR_STORAGE_FAILURE
2316 * The library has not been previously initialized by psa_crypto_init().
2317 * It is implementation-dependent whether a failure to initialize
2318 * results in this error code.
2319 */
2320psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2321 mbedtls_svc_key_id_t key,
2322 psa_algorithm_t alg);
2323
2324/** Set the key for a multipart authenticated decryption operation.
2325 *
2326 * The sequence of operations to decrypt a message with authentication
2327 * is as follows:
2328 * -# Allocate an operation object which will be passed to all the functions
2329 * listed here.
2330 * -# Initialize the operation object with one of the methods described in the
2331 * documentation for #psa_aead_operation_t, e.g.
2332 * #PSA_AEAD_OPERATION_INIT.
2333 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2334 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2335 * inputs to the subsequent calls to psa_aead_update_ad() and
2336 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2337 * for details.
2338 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2339 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2340 * of the non-encrypted additional authenticated data each time.
2341 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2342 * of the ciphertext to decrypt each time.
2343 * -# Call psa_aead_verify().
2344 *
2345 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2346 * the operation will need to be reset by a call to psa_aead_abort(). The
2347 * application may call psa_aead_abort() at any time after the operation
2348 * has been initialized.
2349 *
2350 * After a successful call to psa_aead_decrypt_setup(), the application must
2351 * eventually terminate the operation. The following events terminate an
2352 * operation:
2353 * - A successful call to psa_aead_verify().
2354 * - A call to psa_aead_abort().
2355 *
2356 * \param[in,out] operation The operation object to set up. It must have
2357 * been initialized as per the documentation for
2358 * #psa_aead_operation_t and not yet in use.
2359 * \param key Identifier of the key to use for the operation.
2360 * It must remain valid until the operation
2361 * terminates. It must allow the usage
2362 * #PSA_KEY_USAGE_DECRYPT.
2363 * \param alg The AEAD algorithm to compute
2364 * (\c PSA_ALG_XXX value such that
2365 * #PSA_ALG_IS_AEAD(\p alg) is true).
2366 *
2367 * \retval #PSA_SUCCESS
2368 * Success.
2369 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2370 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2371 * \retval #PSA_ERROR_INVALID_ARGUMENT
2372 * \p key is not compatible with \p alg.
2373 * \retval #PSA_ERROR_NOT_SUPPORTED
2374 * \p alg is not supported or is not an AEAD algorithm.
2375 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2376 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2377 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2378 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2379 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2380 * \retval #PSA_ERROR_BAD_STATE
2381 * The operation state is not valid (it must be inactive), or the
2382 * library has not been previously initialized by psa_crypto_init().
2383 * It is implementation-dependent whether a failure to initialize
2384 * results in this error code.
2385 */
2386psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2387 mbedtls_svc_key_id_t key,
2388 psa_algorithm_t alg);
2389
2390/** Generate a random nonce for an authenticated encryption operation.
2391 *
2392 * This function generates a random nonce for the authenticated encryption
2393 * operation with an appropriate size for the chosen algorithm, key type
2394 * and key size.
2395 *
2396 * The application must call psa_aead_encrypt_setup() before
2397 * calling this function.
2398 *
2399 * If this function returns an error status, the operation enters an error
2400 * state and must be aborted by calling psa_aead_abort().
2401 *
2402 * \param[in,out] operation Active AEAD operation.
2403 * \param[out] nonce Buffer where the generated nonce is to be
2404 * written.
2405 * \param nonce_size Size of the \p nonce buffer in bytes.
2406 * \param[out] nonce_length On success, the number of bytes of the
2407 * generated nonce.
2408 *
2409 * \retval #PSA_SUCCESS
2410 * Success.
2411 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2412 * The size of the \p nonce buffer is too small.
2413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2415 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2416 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2417 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2418 * \retval #PSA_ERROR_BAD_STATE
2419 * The operation state is not valid (it must be an active aead encrypt
2420 * operation, with no nonce set), or the library has not been
2421 * previously initialized by psa_crypto_init().
2422 * It is implementation-dependent whether a failure to initialize
2423 * results in this error code.
2424 */
2425psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2426 uint8_t *nonce,
2427 size_t nonce_size,
2428 size_t *nonce_length);
2429
2430/** Set the nonce for an authenticated encryption or decryption operation.
2431 *
2432 * This function sets the nonce for the authenticated
2433 * encryption or decryption operation.
2434 *
2435 * The application must call psa_aead_encrypt_setup() or
2436 * psa_aead_decrypt_setup() before calling this function.
2437 *
2438 * If this function returns an error status, the operation enters an error
2439 * state and must be aborted by calling psa_aead_abort().
2440 *
2441 * \note When encrypting, applications should use psa_aead_generate_nonce()
2442 * instead of this function, unless implementing a protocol that requires
2443 * a non-random IV.
2444 *
2445 * \param[in,out] operation Active AEAD operation.
2446 * \param[in] nonce Buffer containing the nonce to use.
2447 * \param nonce_length Size of the nonce in bytes.
2448 *
2449 * \retval #PSA_SUCCESS
2450 * Success.
2451 * \retval #PSA_ERROR_INVALID_ARGUMENT
2452 * The size of \p nonce is not acceptable for the chosen algorithm.
2453 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2455 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2456 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2457 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2458 * \retval #PSA_ERROR_BAD_STATE
2459 * The operation state is not valid (it must be active, with no nonce
2460 * set), or the library has not been previously initialized
2461 * by psa_crypto_init().
2462 * It is implementation-dependent whether a failure to initialize
2463 * results in this error code.
2464 */
2465psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2466 const uint8_t *nonce,
2467 size_t nonce_length);
2468
2469/** Declare the lengths of the message and additional data for AEAD.
2470 *
2471 * The application must call this function before calling
2472 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2473 * the operation requires it. If the algorithm does not require it,
2474 * calling this function is optional, but if this function is called
2475 * then the implementation must enforce the lengths.
2476 *
2477 * You may call this function before or after setting the nonce with
2478 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2479 *
2480 * - For #PSA_ALG_CCM, calling this function is required.
2481 * - For #PSA_ALG_CHACHA20_POLY1305, the plaintext length must not
2482 * exceed `UINT32_MAX * 64` bytes.
2483 * - For the other AEAD algorithms defined in this specification, calling
2484 * this function is not required.
2485 * - For vendor-defined algorithm, refer to the vendor documentation.
2486 *
2487 * If this function returns an error status, the operation enters an error
2488 * state and must be aborted by calling psa_aead_abort().
2489 *
2490 * \param[in,out] operation Active AEAD operation.
2491 * \param ad_length Size of the non-encrypted additional
2492 * authenticated data in bytes.
2493 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2494 *
2495 * \retval #PSA_SUCCESS
2496 * Success.
2497 * \retval #PSA_ERROR_INVALID_ARGUMENT
2498 * At least one of the lengths is not acceptable for the chosen
2499 * algorithm.
2500 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2502 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2503 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2504 * \retval #PSA_ERROR_BAD_STATE
2505 * The operation state is not valid (it must be active, and
2506 * psa_aead_update_ad() and psa_aead_update() must not have been
2507 * called yet), or the library has not been previously initialized
2508 * by psa_crypto_init().
2509 * It is implementation-dependent whether a failure to initialize
2510 * results in this error code.
2511 */
2512psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2513 size_t ad_length,
2514 size_t plaintext_length);
2515
2516/** Pass additional data to an active AEAD operation.
2517 *
2518 * Additional data is authenticated, but not encrypted.
2519 *
2520 * You may call this function multiple times to pass successive fragments
2521 * of the additional data. You may not call this function after passing
2522 * data to encrypt or decrypt with psa_aead_update().
2523 *
2524 * Before calling this function, you must:
2525 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2526 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2527 *
2528 * If this function returns an error status, the operation enters an error
2529 * state and must be aborted by calling psa_aead_abort().
2530 *
2531 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2532 * there is no guarantee that the input is valid. Therefore, until
2533 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2534 * treat the input as untrusted and prepare to undo any action that
2535 * depends on the input if psa_aead_verify() returns an error status.
2536 *
2537 * \param[in,out] operation Active AEAD operation.
2538 * \param[in] input Buffer containing the fragment of
2539 * additional data.
2540 * \param input_length Size of the \p input buffer in bytes.
2541 *
2542 * \retval #PSA_SUCCESS
2543 * Success.
2544 * \retval #PSA_ERROR_INVALID_ARGUMENT
2545 * The total input length overflows the additional data length that
2546 * was previously specified with psa_aead_set_lengths().
2547 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2549 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2550 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2551 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2552 * \retval #PSA_ERROR_BAD_STATE
2553 * The operation state is not valid (it must be active, have a nonce
2554 * set, have lengths set if required by the algorithm, and
2555 * psa_aead_update() must not have been called yet), or the library
2556 * has not been previously initialized by psa_crypto_init().
2557 * It is implementation-dependent whether a failure to initialize
2558 * results in this error code.
2559 */
2560psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2561 const uint8_t *input,
2562 size_t input_length);
2563
2564/** Encrypt or decrypt a message fragment in an active AEAD operation.
2565 *
2566 * Before calling this function, you must:
2567 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2568 * The choice of setup function determines whether this function
2569 * encrypts or decrypts its input.
2570 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2571 * 3. Call psa_aead_update_ad() to pass all the additional data.
2572 *
2573 * If this function returns an error status, the operation enters an error
2574 * state and must be aborted by calling psa_aead_abort().
2575 *
2576 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2577 * there is no guarantee that the input is valid. Therefore, until
2578 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2579 * - Do not use the output in any way other than storing it in a
2580 * confidential location. If you take any action that depends
2581 * on the tentative decrypted data, this action will need to be
2582 * undone if the input turns out not to be valid. Furthermore,
2583 * if an adversary can observe that this action took place
2584 * (for example through timing), they may be able to use this
2585 * fact as an oracle to decrypt any message encrypted with the
2586 * same key.
2587 * - In particular, do not copy the output anywhere but to a
2588 * memory or storage space that you have exclusive access to.
2589 *
2590 * This function does not require the input to be aligned to any
2591 * particular block boundary. If the implementation can only process
2592 * a whole block at a time, it must consume all the input provided, but
2593 * it may delay the end of the corresponding output until a subsequent
2594 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2595 * provides sufficient input. The amount of data that can be delayed
2596 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2597 *
2598 * \param[in,out] operation Active AEAD operation.
2599 * \param[in] input Buffer containing the message fragment to
2600 * encrypt or decrypt.
2601 * \param input_length Size of the \p input buffer in bytes.
2602 * \param[out] output Buffer where the output is to be written.
2603 * \param output_size Size of the \p output buffer in bytes.
2604 * This must be appropriate for the selected
2605 * algorithm and key:
2606 * - A sufficient output size is
2607 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2608 * \c alg, \p input_length) where
2609 * \c key_type is the type of key and \c alg is
2610 * the algorithm that were used to set up the
2611 * operation.
2612 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2613 * input_length) evaluates to the maximum
2614 * output size of any supported AEAD
2615 * algorithm.
2616 * \param[out] output_length On success, the number of bytes
2617 * that make up the returned output.
2618 *
2619 * \retval #PSA_SUCCESS
2620 * Success.
2621 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2622 * The size of the \p output buffer is too small.
2623 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2624 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2625 * determine the required buffer size.
2626 * \retval #PSA_ERROR_INVALID_ARGUMENT
2627 * The total length of input to psa_aead_update_ad() so far is
2628 * less than the additional data length that was previously
2629 * specified with psa_aead_set_lengths(), or
2630 * the total input length overflows the plaintext length that
2631 * was previously specified with psa_aead_set_lengths(), or
2632 * the total message length is not acceptable for the algorithm.
2633 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2634 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2635 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2636 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2637 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2638 * \retval #PSA_ERROR_BAD_STATE
2639 * The operation state is not valid (it must be active, have a nonce
2640 * set, and have lengths set if required by the algorithm), or the
2641 * library has not been previously initialized by psa_crypto_init().
2642 * It is implementation-dependent whether a failure to initialize
2643 * results in this error code.
2644 */
2645psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2646 const uint8_t *input,
2647 size_t input_length,
2648 uint8_t *output,
2649 size_t output_size,
2650 size_t *output_length);
2651
2652/** Finish encrypting a message in an AEAD operation.
2653 *
2654 * The operation must have been set up with psa_aead_encrypt_setup().
2655 *
2656 * This function finishes the authentication of the additional data
2657 * formed by concatenating the inputs passed to preceding calls to
2658 * psa_aead_update_ad() with the plaintext formed by concatenating the
2659 * inputs passed to preceding calls to psa_aead_update().
2660 *
2661 * This function has two output buffers:
2662 * - \p ciphertext contains trailing ciphertext that was buffered from
2663 * preceding calls to psa_aead_update().
2664 * - \p tag contains the authentication tag.
2665 *
2666 * When this function returns successfully, the operation becomes inactive.
2667 * If this function returns an error status, the operation enters an error
2668 * state and must be aborted by calling psa_aead_abort().
2669 *
2670 * \param[in,out] operation Active AEAD operation.
2671 * \param[out] ciphertext Buffer where the last part of the ciphertext
2672 * is to be written.
2673 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2674 * This must be appropriate for the selected
2675 * algorithm and key:
2676 * - A sufficient output size is
2677 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2678 * \c alg) where \c key_type is the type of key
2679 * and \c alg is the algorithm that were used to
2680 * set up the operation.
2681 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2682 * the maximum output size of any supported AEAD
2683 * algorithm.
2684 * \param[out] ciphertext_length On success, the number of bytes of
2685 * returned ciphertext.
2686 * \param[out] tag Buffer where the authentication tag is
2687 * to be written.
2688 * \param tag_size Size of the \p tag buffer in bytes.
2689 * This must be appropriate for the selected
2690 * algorithm and key:
2691 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2692 * key_type, \c key_bits, \c alg) where
2693 * \c key_type and \c key_bits are the type and
2694 * bit-size of the key, and \c alg is the
2695 * algorithm that were used in the call to
2696 * psa_aead_encrypt_setup().
2697 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2698 * maximum tag size of any supported AEAD
2699 * algorithm.
2700 * \param[out] tag_length On success, the number of bytes
2701 * that make up the returned tag.
2702 *
2703 * \retval #PSA_SUCCESS
2704 * Success.
2705 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2706 * The size of the \p ciphertext or \p tag buffer is too small.
2707 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2708 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2709 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2710 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2711 * determine the required \p tag buffer size.
2712 * \retval #PSA_ERROR_INVALID_ARGUMENT
2713 * The total length of input to psa_aead_update_ad() so far is
2714 * less than the additional data length that was previously
2715 * specified with psa_aead_set_lengths(), or
2716 * the total length of input to psa_aead_update() so far is
2717 * less than the plaintext length that was previously
2718 * specified with psa_aead_set_lengths().
2719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2721 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2722 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2723 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2724 * \retval #PSA_ERROR_BAD_STATE
2725 * The operation state is not valid (it must be an active encryption
2726 * operation with a nonce set), or the library has not been previously
2727 * initialized by psa_crypto_init().
2728 * It is implementation-dependent whether a failure to initialize
2729 * results in this error code.
2730 */
2731psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2732 uint8_t *ciphertext,
2733 size_t ciphertext_size,
2734 size_t *ciphertext_length,
2735 uint8_t *tag,
2736 size_t tag_size,
2737 size_t *tag_length);
2738
2739/** Finish authenticating and decrypting a message in an AEAD operation.
2740 *
2741 * The operation must have been set up with psa_aead_decrypt_setup().
2742 *
2743 * This function finishes the authenticated decryption of the message
2744 * components:
2745 *
2746 * - The additional data consisting of the concatenation of the inputs
2747 * passed to preceding calls to psa_aead_update_ad().
2748 * - The ciphertext consisting of the concatenation of the inputs passed to
2749 * preceding calls to psa_aead_update().
2750 * - The tag passed to this function call.
2751 *
2752 * If the authentication tag is correct, this function outputs any remaining
2753 * plaintext and reports success. If the authentication tag is not correct,
2754 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2755 *
2756 * When this function returns successfully, the operation becomes inactive.
2757 * If this function returns an error status, the operation enters an error
2758 * state and must be aborted by calling psa_aead_abort().
2759 *
2760 * \note Implementations shall make the best effort to ensure that the
2761 * comparison between the actual tag and the expected tag is performed
2762 * in constant time.
2763 *
2764 * \param[in,out] operation Active AEAD operation.
2765 * \param[out] plaintext Buffer where the last part of the plaintext
2766 * is to be written. This is the remaining data
2767 * from previous calls to psa_aead_update()
2768 * that could not be processed until the end
2769 * of the input.
2770 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2771 * This must be appropriate for the selected algorithm and key:
2772 * - A sufficient output size is
2773 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2774 * \c alg) where \c key_type is the type of key
2775 * and \c alg is the algorithm that were used to
2776 * set up the operation.
2777 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2778 * the maximum output size of any supported AEAD
2779 * algorithm.
2780 * \param[out] plaintext_length On success, the number of bytes of
2781 * returned plaintext.
2782 * \param[in] tag Buffer containing the authentication tag.
2783 * \param tag_length Size of the \p tag buffer in bytes.
2784 *
2785 * \retval #PSA_SUCCESS
2786 * Success.
2787 * \retval #PSA_ERROR_INVALID_SIGNATURE
2788 * The calculations were successful, but the authentication tag is
2789 * not correct.
2790 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2791 * The size of the \p plaintext buffer is too small.
2792 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2793 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2794 * required buffer size.
2795 * \retval #PSA_ERROR_INVALID_ARGUMENT
2796 * The total length of input to psa_aead_update_ad() so far is
2797 * less than the additional data length that was previously
2798 * specified with psa_aead_set_lengths(), or
2799 * the total length of input to psa_aead_update() so far is
2800 * less than the plaintext length that was previously
2801 * specified with psa_aead_set_lengths().
2802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2804 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2805 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2806 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2807 * \retval #PSA_ERROR_BAD_STATE
2808 * The operation state is not valid (it must be an active decryption
2809 * operation with a nonce set), or the library has not been previously
2810 * initialized by psa_crypto_init().
2811 * It is implementation-dependent whether a failure to initialize
2812 * results in this error code.
2813 */
2814psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2815 uint8_t *plaintext,
2816 size_t plaintext_size,
2817 size_t *plaintext_length,
2818 const uint8_t *tag,
2819 size_t tag_length);
2820
2821/** Abort an AEAD operation.
2822 *
2823 * Aborting an operation frees all associated resources except for the
2824 * \p operation structure itself. Once aborted, the operation object
2825 * can be reused for another operation by calling
2826 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2827 *
2828 * You may call this function any time after the operation object has
2829 * been initialized as described in #psa_aead_operation_t.
2830 *
2831 * In particular, calling psa_aead_abort() after the operation has been
2832 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2833 * psa_aead_verify() is safe and has no effect.
2834 *
2835 * \param[in,out] operation Initialized AEAD operation.
2836 *
2837 * \retval #PSA_SUCCESS \emptydescription
2838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2839 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2840 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2841 * \retval #PSA_ERROR_BAD_STATE
2842 * The library has not been previously initialized by psa_crypto_init().
2843 * It is implementation-dependent whether a failure to initialize
2844 * results in this error code.
2845 */
2846psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2847
2848/**@}*/
2849
2850/** \defgroup asymmetric Asymmetric cryptography
2851 * @{
2852 */
2853
2854/**
2855 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2856 * this includes the hashing step.
2857 *
2858 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2859 * a multi-part hash operation and then pass the resulting hash to
2860 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2861 * hash algorithm to use.
2862 *
2863 * \param[in] key Identifier of the key to use for the operation.
2864 * It must be an asymmetric key pair. The key must
2865 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
2866 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2867 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2868 * is true), that is compatible with the type of
2869 * \p key.
2870 * \param[in] input The input message to sign.
2871 * \param[in] input_length Size of the \p input buffer in bytes.
2872 * \param[out] signature Buffer where the signature is to be written.
2873 * \param[in] signature_size Size of the \p signature buffer in bytes. This
2874 * must be appropriate for the selected
2875 * algorithm and key:
2876 * - The required signature size is
2877 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2878 * where \c key_type and \c key_bits are the type and
2879 * bit-size respectively of key.
2880 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2881 * maximum signature size of any supported
2882 * signature algorithm.
2883 * \param[out] signature_length On success, the number of bytes that make up
2884 * the returned signature value.
2885 *
2886 * \retval #PSA_SUCCESS \emptydescription
2887 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2888 * \retval #PSA_ERROR_NOT_PERMITTED
2889 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2890 * or it does not permit the requested algorithm.
2891 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2892 * The size of the \p signature buffer is too small. You can
2893 * determine a sufficient buffer size by calling
2894 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2895 * where \c key_type and \c key_bits are the type and bit-size
2896 * respectively of \p key.
2897 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2898 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2900 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2901 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2902 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2903 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2904 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2905 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2906 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
2907 * \retval #PSA_ERROR_BAD_STATE
2908 * The library has not been previously initialized by psa_crypto_init().
2909 * It is implementation-dependent whether a failure to initialize
2910 * results in this error code.
2911 */
2912psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2913 psa_algorithm_t alg,
2914 const uint8_t *input,
2915 size_t input_length,
2916 uint8_t *signature,
2917 size_t signature_size,
2918 size_t *signature_length);
2919
2920/** \brief Verify the signature of a message with a public key, using
2921 * a hash-and-sign verification algorithm.
2922 *
2923 * \note To perform a multi-part hash-and-sign signature verification
2924 * algorithm, first use a multi-part hash operation to hash the message
2925 * and then pass the resulting hash to psa_verify_hash().
2926 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2927 * to use.
2928 *
2929 * \param[in] key Identifier of the key to use for the operation.
2930 * It must be a public key or an asymmetric key
2931 * pair. The key must allow the usage
2932 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
2933 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2934 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2935 * is true), that is compatible with the type of
2936 * \p key.
2937 * \param[in] input The message whose signature is to be verified.
2938 * \param[in] input_length Size of the \p input buffer in bytes.
2939 * \param[in] signature Buffer containing the signature to verify.
2940 * \param[in] signature_length Size of the \p signature buffer in bytes.
2941 *
2942 * \retval #PSA_SUCCESS \emptydescription
2943 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2944 * \retval #PSA_ERROR_NOT_PERMITTED
2945 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2946 * or it does not permit the requested algorithm.
2947 * \retval #PSA_ERROR_INVALID_SIGNATURE
2948 * The calculation was performed successfully, but the passed signature
2949 * is not a valid signature.
2950 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2951 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2954 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2955 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2956 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2957 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2958 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2959 * \retval #PSA_ERROR_BAD_STATE
2960 * The library has not been previously initialized by psa_crypto_init().
2961 * It is implementation-dependent whether a failure to initialize
2962 * results in this error code.
2963 */
2964psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2965 psa_algorithm_t alg,
2966 const uint8_t *input,
2967 size_t input_length,
2968 const uint8_t *signature,
2969 size_t signature_length);
2970
2971/**
2972 * \brief Sign a hash or short message with a private key.
2973 *
2974 * Note that to perform a hash-and-sign signature algorithm, you must
2975 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2976 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2977 * Then pass the resulting hash as the \p hash
2978 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2979 * to determine the hash algorithm to use.
2980 *
2981 * \param key Identifier of the key to use for the operation.
2982 * It must be an asymmetric key pair. The key must
2983 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
2984 * \param alg A signature algorithm (PSA_ALG_XXX
2985 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2986 * is true), that is compatible with
2987 * the type of \p key.
2988 * \param[in] hash The hash or message to sign.
2989 * \param hash_length Size of the \p hash buffer in bytes.
2990 * \param[out] signature Buffer where the signature is to be written.
2991 * \param signature_size Size of the \p signature buffer in bytes.
2992 * \param[out] signature_length On success, the number of bytes
2993 * that make up the returned signature value.
2994 *
2995 * \retval #PSA_SUCCESS \emptydescription
2996 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2997 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2998 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2999 * The size of the \p signature buffer is too small. You can
3000 * determine a sufficient buffer size by calling
3001 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3002 * where \c key_type and \c key_bits are the type and bit-size
3003 * respectively of \p key.
3004 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3005 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3008 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3009 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3010 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3011 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3012 * \retval #PSA_ERROR_BAD_STATE
3013 * The library has not been previously initialized by psa_crypto_init().
3014 * It is implementation-dependent whether a failure to initialize
3015 * results in this error code.
3016 */
3017psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3018 psa_algorithm_t alg,
3019 const uint8_t *hash,
3020 size_t hash_length,
3021 uint8_t *signature,
3022 size_t signature_size,
3023 size_t *signature_length);
3024
3025/**
3026 * \brief Verify the signature of a hash or short message using a public key.
3027 *
3028 * Note that to perform a hash-and-sign signature algorithm, you must
3029 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
3030 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3031 * Then pass the resulting hash as the \p hash
3032 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3033 * to determine the hash algorithm to use.
3034 *
3035 * \param key Identifier of the key to use for the operation. It
3036 * must be a public key or an asymmetric key pair. The
3037 * key must allow the usage
3038 * #PSA_KEY_USAGE_VERIFY_HASH.
3039 * \param alg A signature algorithm (PSA_ALG_XXX
3040 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3041 * is true), that is compatible with
3042 * the type of \p key.
3043 * \param[in] hash The hash or message whose signature is to be
3044 * verified.
3045 * \param hash_length Size of the \p hash buffer in bytes.
3046 * \param[in] signature Buffer containing the signature to verify.
3047 * \param signature_length Size of the \p signature buffer in bytes.
3048 *
3049 * \retval #PSA_SUCCESS
3050 * The signature is valid.
3051 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3052 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3053 * \retval #PSA_ERROR_INVALID_SIGNATURE
3054 * The calculation was performed successfully, but the passed
3055 * signature is not a valid signature.
3056 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3057 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3060 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3061 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3062 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3063 * \retval #PSA_ERROR_BAD_STATE
3064 * The library has not been previously initialized by psa_crypto_init().
3065 * It is implementation-dependent whether a failure to initialize
3066 * results in this error code.
3067 */
3068psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3069 psa_algorithm_t alg,
3070 const uint8_t *hash,
3071 size_t hash_length,
3072 const uint8_t *signature,
3073 size_t signature_length);
3074
3075/**
3076 * \brief Encrypt a short message with a public key.
3077 *
3078 * \param key Identifier of the key to use for the operation.
3079 * It must be a public key or an asymmetric key
3080 * pair. It must allow the usage
3081 * #PSA_KEY_USAGE_ENCRYPT.
3082 * \param alg An asymmetric encryption algorithm that is
3083 * compatible with the type of \p key.
3084 * \param[in] input The message to encrypt.
3085 * \param input_length Size of the \p input buffer in bytes.
3086 * \param[in] salt A salt or label, if supported by the
3087 * encryption algorithm.
3088 * If the algorithm does not support a
3089 * salt, pass \c NULL.
3090 * If the algorithm supports an optional
3091 * salt and you do not want to pass a salt,
3092 * pass \c NULL.
3093 *
3094 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3095 * supported.
3096 * \param salt_length Size of the \p salt buffer in bytes.
3097 * If \p salt is \c NULL, pass 0.
3098 * \param[out] output Buffer where the encrypted message is to
3099 * be written.
3100 * \param output_size Size of the \p output buffer in bytes.
3101 * \param[out] output_length On success, the number of bytes
3102 * that make up the returned output.
3103 *
3104 * \retval #PSA_SUCCESS \emptydescription
3105 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3106 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3107 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3108 * The size of the \p output buffer is too small. You can
3109 * determine a sufficient buffer size by calling
3110 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3111 * where \c key_type and \c key_bits are the type and bit-size
3112 * respectively of \p key.
3113 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3114 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3116 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3117 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3118 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3119 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3120 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3121 * \retval #PSA_ERROR_BAD_STATE
3122 * The library has not been previously initialized by psa_crypto_init().
3123 * It is implementation-dependent whether a failure to initialize
3124 * results in this error code.
3125 */
3126psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3127 psa_algorithm_t alg,
3128 const uint8_t *input,
3129 size_t input_length,
3130 const uint8_t *salt,
3131 size_t salt_length,
3132 uint8_t *output,
3133 size_t output_size,
3134 size_t *output_length);
3135
3136/**
3137 * \brief Decrypt a short message with a private key.
3138 *
3139 * \warning When \p alg is #PSA_ALG_RSA_PKCS1V15_CRYPT, this is an
3140 * inherently dangerous function (CWE-242): unless it is used
3141 * in a side channel free and safe way, the calling code
3142 * is vulnerable.
3143 * Specifically, callers need to ensure an adversary cannot
3144 * distinguish between success, #PSA_ERROR_INVALID_PADDING and
3145 * #PSA_ERROR_BUFFER_TOO_SMALL. Also, in the latter two cases,
3146 * the values of the output bytes must be ignored, again
3147 * without revealing whether that's the case.
3148 *
3149 * \param key Identifier of the key to use for the operation.
3150 * It must be an asymmetric key pair. It must
3151 * allow the usage #PSA_KEY_USAGE_DECRYPT.
3152 * \param alg An asymmetric encryption algorithm that is
3153 * compatible with the type of \p key.
3154 * \param[in] input The message to decrypt.
3155 * \param input_length Size of the \p input buffer in bytes.
3156 * \param[in] salt A salt or label, if supported by the
3157 * encryption algorithm.
3158 * If the algorithm does not support a
3159 * salt, pass \c NULL.
3160 * If the algorithm supports an optional
3161 * salt and you do not want to pass a salt,
3162 * pass \c NULL.
3163 *
3164 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3165 * supported.
3166 * \param salt_length Size of the \p salt buffer in bytes.
3167 * If \p salt is \c NULL, pass 0.
3168 * \param[out] output Buffer where the decrypted message is to
3169 * be written.
3170 * \param output_size Size of the \c output buffer in bytes.
3171 * \param[out] output_length On success, the number of bytes
3172 * that make up the returned output.
3173 *
3174 * \retval #PSA_SUCCESS \emptydescription
3175 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3176 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3177 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3178 * The size of the \p output buffer is too small. You can
3179 * determine a sufficient buffer size by calling
3180 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3181 * where \c key_type and \c key_bits are the type and bit-size
3182 * respectively of \p key.
3183 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3184 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3185 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3186 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3187 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3188 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3189 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3190 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3191 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
3192 * \retval #PSA_ERROR_BAD_STATE
3193 * The library has not been previously initialized by psa_crypto_init().
3194 * It is implementation-dependent whether a failure to initialize
3195 * results in this error code.
3196 */
3197psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3198 psa_algorithm_t alg,
3199 const uint8_t *input,
3200 size_t input_length,
3201 const uint8_t *salt,
3202 size_t salt_length,
3203 uint8_t *output,
3204 size_t output_size,
3205 size_t *output_length);
3206
3207/**@}*/
3208
3209/** \defgroup key_derivation Key derivation and pseudorandom generation
3210 * @{
3211 */
3212
3213/** The type of the state data structure for key derivation operations.
3214 *
3215 * Before calling any function on a key derivation operation object, the
3216 * application must initialize it by any of the following means:
3217 * - Set the structure to all-bits-zero, for example:
3218 * \code
3219 * psa_key_derivation_operation_t operation;
3220 * memset(&operation, 0, sizeof(operation));
3221 * \endcode
3222 * - Initialize the structure to logical zero values, for example:
3223 * \code
3224 * psa_key_derivation_operation_t operation = {0};
3225 * \endcode
3226 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
3227 * for example:
3228 * \code
3229 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3230 * \endcode
3231 * - Assign the result of the function psa_key_derivation_operation_init()
3232 * to the structure, for example:
3233 * \code
3234 * psa_key_derivation_operation_t operation;
3235 * operation = psa_key_derivation_operation_init();
3236 * \endcode
3237 *
3238 * This is an implementation-defined \c struct. Applications should not
3239 * make any assumptions about the content of this structure.
3240 * Implementation details can change in future versions without notice.
3241 */
3242typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
3243
3244/** \def PSA_KEY_DERIVATION_OPERATION_INIT
3245 *
3246 * This macro returns a suitable initializer for a key derivation operation
3247 * object of type #psa_key_derivation_operation_t.
3248 */
3249
3250/** Return an initial value for a key derivation operation object.
3251 */
3252static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
3253
3254/** Set up a key derivation operation.
3255 *
3256 * A key derivation algorithm takes some inputs and uses them to generate
3257 * a byte stream in a deterministic way.
3258 * This byte stream can be used to produce keys and other
3259 * cryptographic material.
3260 *
3261 * To derive a key:
3262 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3263 * -# Call psa_key_derivation_setup() to select the algorithm.
3264 * -# Provide the inputs for the key derivation by calling
3265 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3266 * as appropriate. Which inputs are needed, in what order, and whether
3267 * they may be keys and if so of what type depends on the algorithm.
3268 * -# Optionally set the operation's maximum capacity with
3269 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3270 * of or after providing inputs. For some algorithms, this step is mandatory
3271 * because the output depends on the maximum capacity.
3272 * -# To derive a key, call psa_key_derivation_output_key() or
3273 * psa_key_derivation_output_key_custom().
3274 * To derive a byte string for a different purpose, call
3275 * psa_key_derivation_output_bytes().
3276 * Successive calls to these functions use successive output bytes
3277 * calculated by the key derivation algorithm.
3278 * -# Clean up the key derivation operation object with
3279 * psa_key_derivation_abort().
3280 *
3281 * If this function returns an error, the key derivation operation object is
3282 * not changed.
3283 *
3284 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3285 * the operation will need to be reset by a call to psa_key_derivation_abort().
3286 *
3287 * Implementations must reject an attempt to derive a key of size 0.
3288 *
3289 * \param[in,out] operation The key derivation operation object
3290 * to set up. It must
3291 * have been initialized but not set up yet.
3292 * \param alg The key derivation algorithm to compute
3293 * (\c PSA_ALG_XXX value such that
3294 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3295 *
3296 * \retval #PSA_SUCCESS
3297 * Success.
3298 * \retval #PSA_ERROR_INVALID_ARGUMENT
3299 * \c alg is not a key derivation algorithm.
3300 * \retval #PSA_ERROR_NOT_SUPPORTED
3301 * \c alg is not supported or is not a key derivation algorithm.
3302 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3303 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3304 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3305 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3306 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3307 * \retval #PSA_ERROR_BAD_STATE
3308 * The operation state is not valid (it must be inactive), or
3309 * the library has not been previously initialized by psa_crypto_init().
3310 * It is implementation-dependent whether a failure to initialize
3311 * results in this error code.
3312 */
3313psa_status_t psa_key_derivation_setup(
3314 psa_key_derivation_operation_t *operation,
3315 psa_algorithm_t alg);
3316
3317/** Retrieve the current capacity of a key derivation operation.
3318 *
3319 * The capacity of a key derivation is the maximum number of bytes that it can
3320 * return. When you get *N* bytes of output from a key derivation operation,
3321 * this reduces its capacity by *N*.
3322 *
3323 * \param[in] operation The operation to query.
3324 * \param[out] capacity On success, the capacity of the operation.
3325 *
3326 * \retval #PSA_SUCCESS \emptydescription
3327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3328 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3329 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3330 * \retval #PSA_ERROR_BAD_STATE
3331 * The operation state is not valid (it must be active), or
3332 * the library has not been previously initialized by psa_crypto_init().
3333 * It is implementation-dependent whether a failure to initialize
3334 * results in this error code.
3335 */
3336psa_status_t psa_key_derivation_get_capacity(
3337 const psa_key_derivation_operation_t *operation,
3338 size_t *capacity);
3339
3340/** Set the maximum capacity of a key derivation operation.
3341 *
3342 * The capacity of a key derivation operation is the maximum number of bytes
3343 * that the key derivation operation can return from this point onwards.
3344 *
3345 * \param[in,out] operation The key derivation operation object to modify.
3346 * \param capacity The new capacity of the operation.
3347 * It must be less or equal to the operation's
3348 * current capacity.
3349 *
3350 * \retval #PSA_SUCCESS \emptydescription
3351 * \retval #PSA_ERROR_INVALID_ARGUMENT
3352 * \p capacity is larger than the operation's current capacity.
3353 * In this case, the operation object remains valid and its capacity
3354 * remains unchanged.
3355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3356 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3357 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3358 * \retval #PSA_ERROR_BAD_STATE
3359 * The operation state is not valid (it must be active), or the
3360 * library has not been previously initialized by psa_crypto_init().
3361 * It is implementation-dependent whether a failure to initialize
3362 * results in this error code.
3363 */
3364psa_status_t psa_key_derivation_set_capacity(
3365 psa_key_derivation_operation_t *operation,
3366 size_t capacity);
3367
3368/** Use the maximum possible capacity for a key derivation operation.
3369 *
3370 * Use this value as the capacity argument when setting up a key derivation
3371 * to indicate that the operation should have the maximum possible capacity.
3372 * The value of the maximum possible capacity depends on the key derivation
3373 * algorithm.
3374 */
3375#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
3376
3377/** Provide an input for key derivation or key agreement.
3378 *
3379 * Which inputs are required and in what order depends on the algorithm.
3380 * Refer to the documentation of each key derivation or key agreement
3381 * algorithm for information.
3382 *
3383 * This function passes direct inputs, which is usually correct for
3384 * non-secret inputs. To pass a secret input, which should be in a key
3385 * object, call psa_key_derivation_input_key() instead of this function.
3386 * Refer to the documentation of individual step types
3387 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3388 * for more information.
3389 *
3390 * If this function returns an error status, the operation enters an error
3391 * state and must be aborted by calling psa_key_derivation_abort().
3392 *
3393 * \param[in,out] operation The key derivation operation object to use.
3394 * It must have been set up with
3395 * psa_key_derivation_setup() and must not
3396 * have produced any output yet.
3397 * \param step Which step the input data is for.
3398 * \param[in] data Input data to use.
3399 * \param data_length Size of the \p data buffer in bytes.
3400 *
3401 * \retval #PSA_SUCCESS
3402 * Success.
3403 * \retval #PSA_ERROR_INVALID_ARGUMENT
3404 * \c step is not compatible with the operation's algorithm, or
3405 * \c step does not allow direct inputs.
3406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3408 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3409 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3410 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3411 * \retval #PSA_ERROR_BAD_STATE
3412 * The operation state is not valid for this input \p step, or
3413 * the library has not been previously initialized by psa_crypto_init().
3414 * It is implementation-dependent whether a failure to initialize
3415 * results in this error code.
3416 */
3417psa_status_t psa_key_derivation_input_bytes(
3418 psa_key_derivation_operation_t *operation,
3419 psa_key_derivation_step_t step,
3420 const uint8_t *data,
3421 size_t data_length);
3422
3423/** Provide a numeric input for key derivation or key agreement.
3424 *
3425 * Which inputs are required and in what order depends on the algorithm.
3426 * However, when an algorithm requires a particular order, numeric inputs
3427 * usually come first as they tend to be configuration parameters.
3428 * Refer to the documentation of each key derivation or key agreement
3429 * algorithm for information.
3430 *
3431 * This function is used for inputs which are fixed-size non-negative
3432 * integers.
3433 *
3434 * If this function returns an error status, the operation enters an error
3435 * state and must be aborted by calling psa_key_derivation_abort().
3436 *
3437 * \param[in,out] operation The key derivation operation object to use.
3438 * It must have been set up with
3439 * psa_key_derivation_setup() and must not
3440 * have produced any output yet.
3441 * \param step Which step the input data is for.
3442 * \param[in] value The value of the numeric input.
3443 *
3444 * \retval #PSA_SUCCESS
3445 * Success.
3446 * \retval #PSA_ERROR_INVALID_ARGUMENT
3447 * \c step is not compatible with the operation's algorithm, or
3448 * \c step does not allow numeric inputs.
3449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3451 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3452 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3453 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3454 * \retval #PSA_ERROR_BAD_STATE
3455 * The operation state is not valid for this input \p step, or
3456 * the library has not been previously initialized by psa_crypto_init().
3457 * It is implementation-dependent whether a failure to initialize
3458 * results in this error code.
3459 */
3460psa_status_t psa_key_derivation_input_integer(
3461 psa_key_derivation_operation_t *operation,
3462 psa_key_derivation_step_t step,
3463 uint64_t value);
3464
3465/** Provide an input for key derivation in the form of a key.
3466 *
3467 * Which inputs are required and in what order depends on the algorithm.
3468 * Refer to the documentation of each key derivation or key agreement
3469 * algorithm for information.
3470 *
3471 * This function obtains input from a key object, which is usually correct for
3472 * secret inputs or for non-secret personalization strings kept in the key
3473 * store. To pass a non-secret parameter which is not in the key store,
3474 * call psa_key_derivation_input_bytes() instead of this function.
3475 * Refer to the documentation of individual step types
3476 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3477 * for more information.
3478 *
3479 * If this function returns an error status, the operation enters an error
3480 * state and must be aborted by calling psa_key_derivation_abort().
3481 *
3482 * \param[in,out] operation The key derivation operation object to use.
3483 * It must have been set up with
3484 * psa_key_derivation_setup() and must not
3485 * have produced any output yet.
3486 * \param step Which step the input data is for.
3487 * \param key Identifier of the key. It must have an
3488 * appropriate type for step and must allow the
3489 * usage #PSA_KEY_USAGE_DERIVE or
3490 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3491 * and the algorithm used by the operation.
3492 *
3493 * \note Once all inputs steps are completed, the operations will allow:
3494 * - psa_key_derivation_output_bytes() if each input was either a direct input
3495 * or a key with #PSA_KEY_USAGE_DERIVE set;
3496 * - psa_key_derivation_output_key() or psa_key_derivation_output_key_custom()
3497 * if the input for step
3498 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
3499 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3500 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
3501 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3502 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
3503 * - psa_key_derivation_verify_key() under the same conditions as
3504 * psa_key_derivation_verify_bytes().
3505 *
3506 * \retval #PSA_SUCCESS
3507 * Success.
3508 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3509 * \retval #PSA_ERROR_NOT_PERMITTED
3510 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3511 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3512 * algorithm.
3513 * \retval #PSA_ERROR_INVALID_ARGUMENT
3514 * \c step is not compatible with the operation's algorithm, or
3515 * \c step does not allow key inputs of the given type
3516 * or does not allow key inputs at all.
3517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3519 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3520 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3521 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3522 * \retval #PSA_ERROR_BAD_STATE
3523 * The operation state is not valid for this input \p step, or
3524 * the library has not been previously initialized by psa_crypto_init().
3525 * It is implementation-dependent whether a failure to initialize
3526 * results in this error code.
3527 */
3528psa_status_t psa_key_derivation_input_key(
3529 psa_key_derivation_operation_t *operation,
3530 psa_key_derivation_step_t step,
3531 mbedtls_svc_key_id_t key);
3532
3533/** Perform a key agreement and use the shared secret as input to a key
3534 * derivation.
3535 *
3536 * A key agreement algorithm takes two inputs: a private key \p private_key
3537 * a public key \p peer_key.
3538 * The result of this function is passed as input to a key derivation.
3539 * The output of this key derivation can be extracted by reading from the
3540 * resulting operation to produce keys and other cryptographic material.
3541 *
3542 * If this function returns an error status, the operation enters an error
3543 * state and must be aborted by calling psa_key_derivation_abort().
3544 *
3545 * \param[in,out] operation The key derivation operation object to use.
3546 * It must have been set up with
3547 * psa_key_derivation_setup() with a
3548 * key agreement and derivation algorithm
3549 * \c alg (\c PSA_ALG_XXX value such that
3550 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3551 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3552 * is false).
3553 * The operation must be ready for an
3554 * input of the type given by \p step.
3555 * \param step Which step the input data is for.
3556 * \param private_key Identifier of the private key to use. It must
3557 * allow the usage #PSA_KEY_USAGE_DERIVE.
3558 * \param[in] peer_key Public key of the peer. The peer key must be in the
3559 * same format that psa_import_key() accepts for the
3560 * public key type corresponding to the type of
3561 * private_key. That is, this function performs the
3562 * equivalent of
3563 * #psa_import_key(...,
3564 * `peer_key`, `peer_key_length`) where
3565 * with key attributes indicating the public key
3566 * type corresponding to the type of `private_key`.
3567 * For example, for EC keys, this means that peer_key
3568 * is interpreted as a point on the curve that the
3569 * private key is on. The standard formats for public
3570 * keys are documented in the documentation of
3571 * psa_export_public_key().
3572 * \param peer_key_length Size of \p peer_key in bytes.
3573 *
3574 * \retval #PSA_SUCCESS
3575 * Success.
3576 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3577 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3578 * \retval #PSA_ERROR_INVALID_ARGUMENT
3579 * \c private_key is not compatible with \c alg,
3580 * or \p peer_key is not valid for \c alg or not compatible with
3581 * \c private_key, or \c step does not allow an input resulting
3582 * from a key agreement.
3583 * \retval #PSA_ERROR_NOT_SUPPORTED
3584 * \c alg is not supported or is not a key derivation algorithm.
3585 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3586 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3587 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3588 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3589 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3590 * \retval #PSA_ERROR_BAD_STATE
3591 * The operation state is not valid for this key agreement \p step,
3592 * or the library has not been previously initialized by psa_crypto_init().
3593 * It is implementation-dependent whether a failure to initialize
3594 * results in this error code.
3595 */
3596psa_status_t psa_key_derivation_key_agreement(
3597 psa_key_derivation_operation_t *operation,
3598 psa_key_derivation_step_t step,
3599 mbedtls_svc_key_id_t private_key,
3600 const uint8_t *peer_key,
3601 size_t peer_key_length);
3602
3603/** Read some data from a key derivation operation.
3604 *
3605 * This function calculates output bytes from a key derivation algorithm and
3606 * return those bytes.
3607 * If you view the key derivation's output as a stream of bytes, this
3608 * function destructively reads the requested number of bytes from the
3609 * stream.
3610 * The operation's capacity decreases by the number of bytes read.
3611 *
3612 * If this function returns an error status other than
3613 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3614 * state and must be aborted by calling psa_key_derivation_abort().
3615 *
3616 * \param[in,out] operation The key derivation operation object to read from.
3617 * \param[out] output Buffer where the output will be written.
3618 * \param output_length Number of bytes to output.
3619 *
3620 * \retval #PSA_SUCCESS \emptydescription
3621 * \retval #PSA_ERROR_NOT_PERMITTED
3622 * One of the inputs was a key whose policy didn't allow
3623 * #PSA_KEY_USAGE_DERIVE.
3624 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3625 * The operation's capacity was less than
3626 * \p output_length bytes. Note that in this case,
3627 * no output is written to the output buffer.
3628 * The operation's capacity is set to 0, thus
3629 * subsequent calls to this function will not
3630 * succeed, even with a smaller output buffer.
3631 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3633 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3634 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3635 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3636 * \retval #PSA_ERROR_BAD_STATE
3637 * The operation state is not valid (it must be active and completed
3638 * all required input steps), or the library has not been previously
3639 * initialized by psa_crypto_init().
3640 * It is implementation-dependent whether a failure to initialize
3641 * results in this error code.
3642 */
3643psa_status_t psa_key_derivation_output_bytes(
3644 psa_key_derivation_operation_t *operation,
3645 uint8_t *output,
3646 size_t output_length);
3647
3648/** Derive a key from an ongoing key derivation operation.
3649 *
3650 * This function calculates output bytes from a key derivation algorithm
3651 * and uses those bytes to generate a key deterministically.
3652 * The key's location, usage policy, type and size are taken from
3653 * \p attributes.
3654 *
3655 * If you view the key derivation's output as a stream of bytes, this
3656 * function destructively reads as many bytes as required from the
3657 * stream.
3658 * The operation's capacity decreases by the number of bytes read.
3659 *
3660 * If this function returns an error status other than
3661 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3662 * state and must be aborted by calling psa_key_derivation_abort().
3663 *
3664 * How much output is produced and consumed from the operation, and how
3665 * the key is derived, depends on the key type and on the key size
3666 * (denoted \c bits below):
3667 *
3668 * - For key types for which the key is an arbitrary sequence of bytes
3669 * of a given size, this function is functionally equivalent to
3670 * calling #psa_key_derivation_output_bytes
3671 * and passing the resulting output to #psa_import_key.
3672 * However, this function has a security benefit:
3673 * if the implementation provides an isolation boundary then
3674 * the key material is not exposed outside the isolation boundary.
3675 * As a consequence, for these key types, this function always consumes
3676 * exactly (\c bits / 8) bytes from the operation.
3677 * The following key types defined in this specification follow this scheme:
3678 *
3679 * - #PSA_KEY_TYPE_AES;
3680 * - #PSA_KEY_TYPE_ARIA;
3681 * - #PSA_KEY_TYPE_CAMELLIA;
3682 * - #PSA_KEY_TYPE_DERIVE;
3683 * - #PSA_KEY_TYPE_HMAC;
3684 * - #PSA_KEY_TYPE_PASSWORD_HASH.
3685 *
3686 * - For ECC keys on a Montgomery elliptic curve
3687 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3688 * Montgomery curve), this function always draws a byte string whose
3689 * length is determined by the curve, and sets the mandatory bits
3690 * accordingly. That is:
3691 *
3692 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
3693 * string and process it as specified in RFC 7748 &sect;5.
3694 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
3695 * string and process it as specified in RFC 7748 &sect;5.
3696 *
3697 * - For key types for which the key is represented by a single sequence of
3698 * \c bits bits with constraints as to which bit sequences are acceptable,
3699 * this function draws a byte string of length (\c bits / 8) bytes rounded
3700 * up to the nearest whole number of bytes. If the resulting byte string
3701 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3702 * This process is repeated until an acceptable byte string is drawn.
3703 * The byte string drawn from the operation is interpreted as specified
3704 * for the output produced by psa_export_key().
3705 * The following key types defined in this specification follow this scheme:
3706 *
3707 * - #PSA_KEY_TYPE_DES.
3708 * Force-set the parity bits, but discard forbidden weak keys.
3709 * For 2-key and 3-key triple-DES, the three keys are generated
3710 * successively (for example, for 3-key triple-DES,
3711 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3712 * discard the first 8 bytes, use the next 8 bytes as the first key,
3713 * and continue reading output from the operation to derive the other
3714 * two keys).
3715 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3716 * where \c group designates any Diffie-Hellman group) and
3717 * ECC keys on a Weierstrass elliptic curve
3718 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3719 * Weierstrass curve).
3720 * For these key types, interpret the byte string as integer
3721 * in big-endian order. Discard it if it is not in the range
3722 * [0, *N* - 2] where *N* is the boundary of the private key domain
3723 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3724 * or the order of the curve's base point for ECC).
3725 * Add 1 to the resulting integer and use this as the private key *x*.
3726 * This method allows compliance to NIST standards, specifically
3727 * the methods titled "key-pair generation by testing candidates"
3728 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3729 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3730 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3731 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3732 *
3733 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3734 * the way in which the operation output is consumed is
3735 * implementation-defined.
3736 *
3737 * In all cases, the data that is read is discarded from the operation.
3738 * The operation's capacity is decreased by the number of bytes read.
3739 *
3740 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3741 * the input to that step must be provided with psa_key_derivation_input_key().
3742 * Future versions of this specification may include additional restrictions
3743 * on the derived key based on the attributes and strength of the secret key.
3744 *
3745 * \note This function is equivalent to calling
3746 * psa_key_derivation_output_key_custom()
3747 * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
3748 * and `custom_data_length == 0` (i.e. `custom_data` is empty).
3749 *
3750 * \param[in] attributes The attributes for the new key.
3751 * If the key type to be created is
3752 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3753 * the policy must be the same as in the current
3754 * operation.
3755 * \param[in,out] operation The key derivation operation object to read from.
3756 * \param[out] key On success, an identifier for the newly created
3757 * key. For persistent keys, this is the key
3758 * identifier defined in \p attributes.
3759 * \c 0 on failure.
3760 *
3761 * \retval #PSA_SUCCESS
3762 * Success.
3763 * If the key is persistent, the key material and the key's metadata
3764 * have been saved to persistent storage.
3765 * \retval #PSA_ERROR_ALREADY_EXISTS
3766 * This is an attempt to create a persistent key, and there is
3767 * already a persistent key with the given identifier.
3768 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3769 * There was not enough data to create the desired key.
3770 * Note that in this case, no output is written to the output buffer.
3771 * The operation's capacity is set to 0, thus subsequent calls to
3772 * this function will not succeed, even with a smaller output buffer.
3773 * \retval #PSA_ERROR_NOT_SUPPORTED
3774 * The key type or key size is not supported, either by the
3775 * implementation in general or in this particular location.
3776 * \retval #PSA_ERROR_INVALID_ARGUMENT
3777 * The provided key attributes are not valid for the operation.
3778 * \retval #PSA_ERROR_NOT_PERMITTED
3779 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3780 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3781 * key; or one of the inputs was a key whose policy didn't allow
3782 * #PSA_KEY_USAGE_DERIVE.
3783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3784 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3786 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3787 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3788 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3789 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3790 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3791 * \retval #PSA_ERROR_BAD_STATE
3792 * The operation state is not valid (it must be active and completed
3793 * all required input steps), or the library has not been previously
3794 * initialized by psa_crypto_init().
3795 * It is implementation-dependent whether a failure to initialize
3796 * results in this error code.
3797 */
3798psa_status_t psa_key_derivation_output_key(
3799 const psa_key_attributes_t *attributes,
3800 psa_key_derivation_operation_t *operation,
3801 mbedtls_svc_key_id_t *key);
3802
3803/** Derive a key from an ongoing key derivation operation with custom
3804 * production parameters.
3805 *
3806 * See the description of psa_key_derivation_out_key() for the operation of
3807 * this function with the default production parameters.
3808 * Mbed TLS currently does not currently support any non-default production
3809 * parameters.
3810 *
3811 * \note This function is experimental and may change in future minor
3812 * versions of Mbed TLS.
3813 *
3814 * \param[in] attributes The attributes for the new key.
3815 * If the key type to be created is
3816 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3817 * the policy must be the same as in the current
3818 * operation.
3819 * \param[in,out] operation The key derivation operation object to read from.
3820 * \param[in] custom Customization parameters for the key generation.
3821 * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
3822 * with \p custom_data_length = 0,
3823 * this function is equivalent to
3824 * psa_key_derivation_output_key().
3825 * \param[in] custom_data Variable-length data associated with \c custom.
3826 * \param custom_data_length
3827 * Length of `custom_data` in bytes.
3828 * \param[out] key On success, an identifier for the newly created
3829 * key. For persistent keys, this is the key
3830 * identifier defined in \p attributes.
3831 * \c 0 on failure.
3832 *
3833 * \retval #PSA_SUCCESS
3834 * Success.
3835 * If the key is persistent, the key material and the key's metadata
3836 * have been saved to persistent storage.
3837 * \retval #PSA_ERROR_ALREADY_EXISTS
3838 * This is an attempt to create a persistent key, and there is
3839 * already a persistent key with the given identifier.
3840 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3841 * There was not enough data to create the desired key.
3842 * Note that in this case, no output is written to the output buffer.
3843 * The operation's capacity is set to 0, thus subsequent calls to
3844 * this function will not succeed, even with a smaller output buffer.
3845 * \retval #PSA_ERROR_NOT_SUPPORTED
3846 * The key type or key size is not supported, either by the
3847 * implementation in general or in this particular location.
3848 * \retval #PSA_ERROR_INVALID_ARGUMENT
3849 * The provided key attributes are not valid for the operation.
3850 * \retval #PSA_ERROR_NOT_PERMITTED
3851 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3852 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3853 * key; or one of the inputs was a key whose policy didn't allow
3854 * #PSA_KEY_USAGE_DERIVE.
3855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3856 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3858 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3859 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3860 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3861 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3862 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3863 * \retval #PSA_ERROR_BAD_STATE
3864 * The operation state is not valid (it must be active and completed
3865 * all required input steps), or the library has not been previously
3866 * initialized by psa_crypto_init().
3867 * It is implementation-dependent whether a failure to initialize
3868 * results in this error code.
3869 */
3870psa_status_t psa_key_derivation_output_key_custom(
3871 const psa_key_attributes_t *attributes,
3872 psa_key_derivation_operation_t *operation,
3873 const psa_custom_key_parameters_t *custom,
3874 const uint8_t *custom_data,
3875 size_t custom_data_length,
3876 mbedtls_svc_key_id_t *key);
3877
3878#ifndef __cplusplus
3879/* Omitted when compiling in C++, because one of the parameters is a
3880 * pointer to a struct with a flexible array member, and that is not
3881 * standard C++.
3882 * https://github.com/Mbed-TLS/mbedtls/issues/9020
3883 */
3884/** Derive a key from an ongoing key derivation operation with custom
3885 * production parameters.
3886 *
3887 * \note
3888 * This is a deprecated variant of psa_key_derivation_output_key_custom().
3889 * It is equivalent except that the associated variable-length data
3890 * is passed in `params->data` instead of a separate parameter.
3891 * This function will be removed in a future version of Mbed TLS.
3892 *
3893 * \param[in] attributes The attributes for the new key.
3894 * If the key type to be created is
3895 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3896 * the policy must be the same as in the current
3897 * operation.
3898 * \param[in,out] operation The key derivation operation object to read from.
3899 * \param[in] params Customization parameters for the key derivation.
3900 * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
3901 * with \p params_data_length = 0,
3902 * this function is equivalent to
3903 * psa_key_derivation_output_key().
3904 * Mbed TLS currently only supports the default
3905 * production parameters, i.e.
3906 * #PSA_KEY_PRODUCTION_PARAMETERS_INIT,
3907 * for all key types.
3908 * \param params_data_length
3909 * Length of `params->data` in bytes.
3910 * \param[out] key On success, an identifier for the newly created
3911 * key. For persistent keys, this is the key
3912 * identifier defined in \p attributes.
3913 * \c 0 on failure.
3914 *
3915 * \retval #PSA_SUCCESS
3916 * Success.
3917 * If the key is persistent, the key material and the key's metadata
3918 * have been saved to persistent storage.
3919 * \retval #PSA_ERROR_ALREADY_EXISTS
3920 * This is an attempt to create a persistent key, and there is
3921 * already a persistent key with the given identifier.
3922 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3923 * There was not enough data to create the desired key.
3924 * Note that in this case, no output is written to the output buffer.
3925 * The operation's capacity is set to 0, thus subsequent calls to
3926 * this function will not succeed, even with a smaller output buffer.
3927 * \retval #PSA_ERROR_NOT_SUPPORTED
3928 * The key type or key size is not supported, either by the
3929 * implementation in general or in this particular location.
3930 * \retval #PSA_ERROR_INVALID_ARGUMENT
3931 * The provided key attributes are not valid for the operation.
3932 * \retval #PSA_ERROR_NOT_PERMITTED
3933 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3934 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3935 * key; or one of the inputs was a key whose policy didn't allow
3936 * #PSA_KEY_USAGE_DERIVE.
3937 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3938 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3939 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3940 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3941 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3942 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3943 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3944 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3945 * \retval #PSA_ERROR_BAD_STATE
3946 * The operation state is not valid (it must be active and completed
3947 * all required input steps), or the library has not been previously
3948 * initialized by psa_crypto_init().
3949 * It is implementation-dependent whether a failure to initialize
3950 * results in this error code.
3951 */
3952psa_status_t psa_key_derivation_output_key_ext(
3953 const psa_key_attributes_t *attributes,
3954 psa_key_derivation_operation_t *operation,
3955 const psa_key_production_parameters_t *params,
3956 size_t params_data_length,
3957 mbedtls_svc_key_id_t *key);
3958#endif /* !__cplusplus */
3959
3960/** Compare output data from a key derivation operation to an expected value.
3961 *
3962 * This function calculates output bytes from a key derivation algorithm and
3963 * compares those bytes to an expected value in constant time.
3964 * If you view the key derivation's output as a stream of bytes, this
3965 * function destructively reads the expected number of bytes from the
3966 * stream before comparing them.
3967 * The operation's capacity decreases by the number of bytes read.
3968 *
3969 * This is functionally equivalent to the following code:
3970 * \code
3971 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3972 * if (memcmp(output, tmp, output_length) != 0)
3973 * return PSA_ERROR_INVALID_SIGNATURE;
3974 * \endcode
3975 * except (1) it works even if the key's policy does not allow outputting the
3976 * bytes, and (2) the comparison will be done in constant time.
3977 *
3978 * If this function returns an error status other than
3979 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3980 * the operation enters an error state and must be aborted by calling
3981 * psa_key_derivation_abort().
3982 *
3983 * \param[in,out] operation The key derivation operation object to read from.
3984 * \param[in] expected Buffer containing the expected derivation output.
3985 * \param expected_length Length of the expected output; this is also the
3986 * number of bytes that will be read.
3987 *
3988 * \retval #PSA_SUCCESS \emptydescription
3989 * \retval #PSA_ERROR_INVALID_SIGNATURE
3990 * The output was read successfully, but it differs from the expected
3991 * output.
3992 * \retval #PSA_ERROR_NOT_PERMITTED
3993 * One of the inputs was a key whose policy didn't allow
3994 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
3995 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3996 * The operation's capacity was less than
3997 * \p output_length bytes. Note that in this case,
3998 * the operation's capacity is set to 0, thus
3999 * subsequent calls to this function will not
4000 * succeed, even with a smaller expected output.
4001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4003 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4004 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4005 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4006 * \retval #PSA_ERROR_BAD_STATE
4007 * The operation state is not valid (it must be active and completed
4008 * all required input steps), or the library has not been previously
4009 * initialized by psa_crypto_init().
4010 * It is implementation-dependent whether a failure to initialize
4011 * results in this error code.
4012 */
4013psa_status_t psa_key_derivation_verify_bytes(
4014 psa_key_derivation_operation_t *operation,
4015 const uint8_t *expected,
4016 size_t expected_length);
4017
4018/** Compare output data from a key derivation operation to an expected value
4019 * stored in a key object.
4020 *
4021 * This function calculates output bytes from a key derivation algorithm and
4022 * compares those bytes to an expected value, provided as key of type
4023 * #PSA_KEY_TYPE_PASSWORD_HASH.
4024 * If you view the key derivation's output as a stream of bytes, this
4025 * function destructively reads the number of bytes corresponding to the
4026 * length of the expected value from the stream before comparing them.
4027 * The operation's capacity decreases by the number of bytes read.
4028 *
4029 * This is functionally equivalent to exporting the key and calling
4030 * psa_key_derivation_verify_bytes() on the result, except that it
4031 * works even if the key cannot be exported.
4032 *
4033 * If this function returns an error status other than
4034 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
4035 * the operation enters an error state and must be aborted by calling
4036 * psa_key_derivation_abort().
4037 *
4038 * \param[in,out] operation The key derivation operation object to read from.
4039 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
4040 * containing the expected output. Its policy must
4041 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
4042 * and the permitted algorithm must match the
4043 * operation. The value of this key was likely
4044 * computed by a previous call to
4045 * psa_key_derivation_output_key() or
4046 * psa_key_derivation_output_key_custom().
4047 *
4048 * \retval #PSA_SUCCESS \emptydescription
4049 * \retval #PSA_ERROR_INVALID_SIGNATURE
4050 * The output was read successfully, but if differs from the expected
4051 * output.
4052 * \retval #PSA_ERROR_INVALID_HANDLE
4053 * The key passed as the expected value does not exist.
4054 * \retval #PSA_ERROR_INVALID_ARGUMENT
4055 * The key passed as the expected value has an invalid type.
4056 * \retval #PSA_ERROR_NOT_PERMITTED
4057 * The key passed as the expected value does not allow this usage or
4058 * this algorithm; or one of the inputs was a key whose policy didn't
4059 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
4060 * \retval #PSA_ERROR_INSUFFICIENT_DATA
4061 * The operation's capacity was less than
4062 * the length of the expected value. In this case,
4063 * the operation's capacity is set to 0, thus
4064 * subsequent calls to this function will not
4065 * succeed, even with a smaller expected output.
4066 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4067 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4068 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4069 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4070 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4071 * \retval #PSA_ERROR_BAD_STATE
4072 * The operation state is not valid (it must be active and completed
4073 * all required input steps), or the library has not been previously
4074 * initialized by psa_crypto_init().
4075 * It is implementation-dependent whether a failure to initialize
4076 * results in this error code.
4077 */
4078psa_status_t psa_key_derivation_verify_key(
4079 psa_key_derivation_operation_t *operation,
4080 psa_key_id_t expected);
4081
4082/** Abort a key derivation operation.
4083 *
4084 * Aborting an operation frees all associated resources except for the \c
4085 * operation structure itself. Once aborted, the operation object can be reused
4086 * for another operation by calling psa_key_derivation_setup() again.
4087 *
4088 * This function may be called at any time after the operation
4089 * object has been initialized as described in #psa_key_derivation_operation_t.
4090 *
4091 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
4092 * call psa_key_derivation_abort() on an operation that has not been set up.
4093 *
4094 * \param[in,out] operation The operation to abort.
4095 *
4096 * \retval #PSA_SUCCESS \emptydescription
4097 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4098 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4099 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4100 * \retval #PSA_ERROR_BAD_STATE
4101 * The library has not been previously initialized by psa_crypto_init().
4102 * It is implementation-dependent whether a failure to initialize
4103 * results in this error code.
4104 */
4105psa_status_t psa_key_derivation_abort(
4106 psa_key_derivation_operation_t *operation);
4107
4108/** Perform a key agreement and return the raw shared secret.
4109 *
4110 * \warning The raw result of a key agreement algorithm such as finite-field
4111 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
4112 * not be used directly as key material. It should instead be passed as
4113 * input to a key derivation algorithm. To chain a key agreement with
4114 * a key derivation, use psa_key_derivation_key_agreement() and other
4115 * functions from the key derivation interface.
4116 *
4117 * \param alg The key agreement algorithm to compute
4118 * (\c PSA_ALG_XXX value such that
4119 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
4120 * is true).
4121 * \param private_key Identifier of the private key to use. It must
4122 * allow the usage #PSA_KEY_USAGE_DERIVE.
4123 * \param[in] peer_key Public key of the peer. It must be
4124 * in the same format that psa_import_key()
4125 * accepts. The standard formats for public
4126 * keys are documented in the documentation
4127 * of psa_export_public_key().
4128 * \param peer_key_length Size of \p peer_key in bytes.
4129 * \param[out] output Buffer where the decrypted message is to
4130 * be written.
4131 * \param output_size Size of the \c output buffer in bytes.
4132 * \param[out] output_length On success, the number of bytes
4133 * that make up the returned output.
4134 *
4135 * \retval #PSA_SUCCESS
4136 * Success.
4137 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4138 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
4139 * \retval #PSA_ERROR_INVALID_ARGUMENT
4140 * \p alg is not a key agreement algorithm, or
4141 * \p private_key is not compatible with \p alg,
4142 * or \p peer_key is not valid for \p alg or not compatible with
4143 * \p private_key.
4144 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4145 * \p output_size is too small
4146 * \retval #PSA_ERROR_NOT_SUPPORTED
4147 * \p alg is not a supported key agreement algorithm.
4148 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4149 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4150 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4151 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4152 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4153 * \retval #PSA_ERROR_BAD_STATE
4154 * The library has not been previously initialized by psa_crypto_init().
4155 * It is implementation-dependent whether a failure to initialize
4156 * results in this error code.
4157 */
4158psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
4159 mbedtls_svc_key_id_t private_key,
4160 const uint8_t *peer_key,
4161 size_t peer_key_length,
4162 uint8_t *output,
4163 size_t output_size,
4164 size_t *output_length);
4165
4166/**@}*/
4167
4168/** \defgroup random Random generation
4169 * @{
4170 */
4171
4172/**
4173 * \brief Generate random bytes.
4174 *
4175 * \warning This function **can** fail! Callers MUST check the return status
4176 * and MUST NOT use the content of the output buffer if the return
4177 * status is not #PSA_SUCCESS.
4178 *
4179 * \note To generate a key, use psa_generate_key() instead.
4180 *
4181 * \param[out] output Output buffer for the generated data.
4182 * \param output_size Number of bytes to generate and output.
4183 *
4184 * \retval #PSA_SUCCESS \emptydescription
4185 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4186 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4189 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4190 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4191 * \retval #PSA_ERROR_BAD_STATE
4192 * The library has not been previously initialized by psa_crypto_init().
4193 * It is implementation-dependent whether a failure to initialize
4194 * results in this error code.
4195 */
4196psa_status_t psa_generate_random(uint8_t *output,
4197 size_t output_size);
4198
4199/**
4200 * \brief Generate a key or key pair.
4201 *
4202 * The key is generated randomly.
4203 * Its location, usage policy, type and size are taken from \p attributes.
4204 *
4205 * Implementations must reject an attempt to generate a key of size 0.
4206 *
4207 * The following type-specific considerations apply:
4208 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
4209 * the public exponent is 65537.
4210 * The modulus is a product of two probabilistic primes
4211 * between 2^{n-1} and 2^n where n is the bit size specified in the
4212 * attributes.
4213 *
4214 * \note This function is equivalent to calling psa_generate_key_custom()
4215 * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
4216 * and `custom_data_length == 0` (i.e. `custom_data` is empty).
4217 *
4218 * \param[in] attributes The attributes for the new key.
4219 * \param[out] key On success, an identifier for the newly created
4220 * key. For persistent keys, this is the key
4221 * identifier defined in \p attributes.
4222 * \c 0 on failure.
4223 *
4224 * \retval #PSA_SUCCESS
4225 * Success.
4226 * If the key is persistent, the key material and the key's metadata
4227 * have been saved to persistent storage.
4228 * \retval #PSA_ERROR_ALREADY_EXISTS
4229 * This is an attempt to create a persistent key, and there is
4230 * already a persistent key with the given identifier.
4231 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4232 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4233 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4234 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4236 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4237 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4238 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4239 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4240 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4241 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4242 * \retval #PSA_ERROR_BAD_STATE
4243 * The library has not been previously initialized by psa_crypto_init().
4244 * It is implementation-dependent whether a failure to initialize
4245 * results in this error code.
4246 */
4247psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
4248 mbedtls_svc_key_id_t *key);
4249
4250/**
4251 * \brief Generate a key or key pair using custom production parameters.
4252 *
4253 * See the description of psa_generate_key() for the operation of this
4254 * function with the default production parameters. In addition, this function
4255 * supports the following production customizations, described in more detail
4256 * in the documentation of ::psa_custom_key_parameters_t:
4257 *
4258 * - RSA keys: generation with a custom public exponent.
4259 *
4260 * \note This function is experimental and may change in future minor
4261 * versions of Mbed TLS.
4262 *
4263 * \param[in] attributes The attributes for the new key.
4264 * \param[in] custom Customization parameters for the key generation.
4265 * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
4266 * with \p custom_data_length = 0,
4267 * this function is equivalent to
4268 * psa_generate_key().
4269 * \param[in] custom_data Variable-length data associated with \c custom.
4270 * \param custom_data_length
4271 * Length of `custom_data` in bytes.
4272 * \param[out] key On success, an identifier for the newly created
4273 * key. For persistent keys, this is the key
4274 * identifier defined in \p attributes.
4275 * \c 0 on failure.
4276 *
4277 * \retval #PSA_SUCCESS
4278 * Success.
4279 * If the key is persistent, the key material and the key's metadata
4280 * have been saved to persistent storage.
4281 * \retval #PSA_ERROR_ALREADY_EXISTS
4282 * This is an attempt to create a persistent key, and there is
4283 * already a persistent key with the given identifier.
4284 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4285 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4287 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4288 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4289 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4290 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4291 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4292 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4293 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4294 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4295 * \retval #PSA_ERROR_BAD_STATE
4296 * The library has not been previously initialized by psa_crypto_init().
4297 * It is implementation-dependent whether a failure to initialize
4298 * results in this error code.
4299 */
4300psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
4301 const psa_custom_key_parameters_t *custom,
4302 const uint8_t *custom_data,
4303 size_t custom_data_length,
4304 mbedtls_svc_key_id_t *key);
4305
4306#ifndef __cplusplus
4307/* Omitted when compiling in C++, because one of the parameters is a
4308 * pointer to a struct with a flexible array member, and that is not
4309 * standard C++.
4310 * https://github.com/Mbed-TLS/mbedtls/issues/9020
4311 */
4312/**
4313 * \brief Generate a key or key pair using custom production parameters.
4314 *
4315 * \note
4316 * This is a deprecated variant of psa_key_derivation_output_key_custom().
4317 * It is equivalent except that the associated variable-length data
4318 * is passed in `params->data` instead of a separate parameter.
4319 * This function will be removed in a future version of Mbed TLS.
4320 *
4321 * \param[in] attributes The attributes for the new key.
4322 * \param[in] params Customization parameters for the key generation.
4323 * When this is #PSA_KEY_PRODUCTION_PARAMETERS_INIT
4324 * with \p params_data_length = 0,
4325 * this function is equivalent to
4326 * psa_generate_key().
4327 * \param params_data_length
4328 * Length of `params->data` in bytes.
4329 * \param[out] key On success, an identifier for the newly created
4330 * key. For persistent keys, this is the key
4331 * identifier defined in \p attributes.
4332 * \c 0 on failure.
4333 *
4334 * \retval #PSA_SUCCESS
4335 * Success.
4336 * If the key is persistent, the key material and the key's metadata
4337 * have been saved to persistent storage.
4338 * \retval #PSA_ERROR_ALREADY_EXISTS
4339 * This is an attempt to create a persistent key, and there is
4340 * already a persistent key with the given identifier.
4341 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4342 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4343 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4344 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4346 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4347 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4348 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4349 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4350 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4351 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4352 * \retval #PSA_ERROR_BAD_STATE
4353 * The library has not been previously initialized by psa_crypto_init().
4354 * It is implementation-dependent whether a failure to initialize
4355 * results in this error code.
4356 */
4357psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
4358 const psa_key_production_parameters_t *params,
4359 size_t params_data_length,
4360 mbedtls_svc_key_id_t *key);
4361#endif /* !__cplusplus */
4362
4363/**@}*/
4364
4365/** \defgroup interruptible_hash Interruptible sign/verify hash
4366 * @{
4367 */
4368
4369/** The type of the state data structure for interruptible hash
4370 * signing operations.
4371 *
4372 * Before calling any function on a sign hash operation object, the
4373 * application must initialize it by any of the following means:
4374 * - Set the structure to all-bits-zero, for example:
4375 * \code
4376 * psa_sign_hash_interruptible_operation_t operation;
4377 * memset(&operation, 0, sizeof(operation));
4378 * \endcode
4379 * - Initialize the structure to logical zero values, for example:
4380 * \code
4381 * psa_sign_hash_interruptible_operation_t operation = {0};
4382 * \endcode
4383 * - Initialize the structure to the initializer
4384 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4385 * \code
4386 * psa_sign_hash_interruptible_operation_t operation =
4387 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4388 * \endcode
4389 * - Assign the result of the function
4390 * psa_sign_hash_interruptible_operation_init() to the structure, for
4391 * example:
4392 * \code
4393 * psa_sign_hash_interruptible_operation_t operation;
4394 * operation = psa_sign_hash_interruptible_operation_init();
4395 * \endcode
4396 *
4397 * This is an implementation-defined \c struct. Applications should not
4398 * make any assumptions about the content of this structure.
4399 * Implementation details can change in future versions without notice. */
4400typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4401
4402/** The type of the state data structure for interruptible hash
4403 * verification operations.
4404 *
4405 * Before calling any function on a sign hash operation object, the
4406 * application must initialize it by any of the following means:
4407 * - Set the structure to all-bits-zero, for example:
4408 * \code
4409 * psa_verify_hash_interruptible_operation_t operation;
4410 * memset(&operation, 0, sizeof(operation));
4411 * \endcode
4412 * - Initialize the structure to logical zero values, for example:
4413 * \code
4414 * psa_verify_hash_interruptible_operation_t operation = {0};
4415 * \endcode
4416 * - Initialize the structure to the initializer
4417 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4418 * \code
4419 * psa_verify_hash_interruptible_operation_t operation =
4420 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4421 * \endcode
4422 * - Assign the result of the function
4423 * psa_verify_hash_interruptible_operation_init() to the structure, for
4424 * example:
4425 * \code
4426 * psa_verify_hash_interruptible_operation_t operation;
4427 * operation = psa_verify_hash_interruptible_operation_init();
4428 * \endcode
4429 *
4430 * This is an implementation-defined \c struct. Applications should not
4431 * make any assumptions about the content of this structure.
4432 * Implementation details can change in future versions without notice. */
4433typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4434
4435/**
4436 * \brief Set the maximum number of ops allowed to be
4437 * executed by an interruptible function in a
4438 * single call.
4439 *
4440 * \warning This is a beta API, and thus subject to change
4441 * at any point. It is not bound by the usual
4442 * interface stability promises.
4443 *
4444 * \note The time taken to execute a single op is
4445 * implementation specific and depends on
4446 * software, hardware, the algorithm, key type and
4447 * curve chosen. Even within a single operation,
4448 * successive ops can take differing amounts of
4449 * time. The only guarantee is that lower values
4450 * for \p max_ops means functions will block for a
4451 * lesser maximum amount of time. The functions
4452 * \c psa_sign_interruptible_get_num_ops() and
4453 * \c psa_verify_interruptible_get_num_ops() are
4454 * provided to help with tuning this value.
4455 *
4456 * \note This value defaults to
4457 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4458 * means the whole operation will be done in one
4459 * go, regardless of the number of ops required.
4460 *
4461 * \note If more ops are needed to complete a
4462 * computation, #PSA_OPERATION_INCOMPLETE will be
4463 * returned by the function performing the
4464 * computation. It is then the caller's
4465 * responsibility to either call again with the
4466 * same operation context until it returns 0 or an
4467 * error code; or to call the relevant abort
4468 * function if the answer is no longer required.
4469 *
4470 * \note The interpretation of \p max_ops is also
4471 * implementation defined. On a hard real time
4472 * system, this can indicate a hard deadline, as a
4473 * real-time system needs a guarantee of not
4474 * spending more than X time, however care must be
4475 * taken in such an implementation to avoid the
4476 * situation whereby calls just return, not being
4477 * able to do any actual work within the allotted
4478 * time. On a non-real-time system, the
4479 * implementation can be more relaxed, but again
4480 * whether this number should be interpreted as as
4481 * hard or soft limit or even whether a less than
4482 * or equals as regards to ops executed in a
4483 * single call is implementation defined.
4484 *
4485 * \note For keys in local storage when no accelerator
4486 * driver applies, please see also the
4487 * documentation for \c mbedtls_ecp_set_max_ops(),
4488 * which is the internal implementation in these
4489 * cases.
4490 *
4491 * \warning With implementations that interpret this number
4492 * as a hard limit, setting this number too small
4493 * may result in an infinite loop, whereby each
4494 * call results in immediate return with no ops
4495 * done (as there is not enough time to execute
4496 * any), and thus no result will ever be achieved.
4497 *
4498 * \note This only applies to functions whose
4499 * documentation mentions they may return
4500 * #PSA_OPERATION_INCOMPLETE.
4501 *
4502 * \param max_ops The maximum number of ops to be executed in a
4503 * single call. This can be a number from 0 to
4504 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4505 * is the least amount of work done per call.
4506 */
4507void psa_interruptible_set_max_ops(uint32_t max_ops);
4508
4509/**
4510 * \brief Get the maximum number of ops allowed to be
4511 * executed by an interruptible function in a
4512 * single call. This will return the last
4513 * value set by
4514 * \c psa_interruptible_set_max_ops() or
4515 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4516 * that function has never been called.
4517 *
4518 * \warning This is a beta API, and thus subject to change
4519 * at any point. It is not bound by the usual
4520 * interface stability promises.
4521 *
4522 * \return Maximum number of ops allowed to be
4523 * executed by an interruptible function in a
4524 * single call.
4525 */
4526uint32_t psa_interruptible_get_max_ops(void);
4527
4528/**
4529 * \brief Get the number of ops that a hash signing
4530 * operation has taken so far. If the operation
4531 * has completed, then this will represent the
4532 * number of ops required for the entire
4533 * operation. After initialization or calling
4534 * \c psa_sign_hash_interruptible_abort() on
4535 * the operation, a value of 0 will be returned.
4536 *
4537 * \note This interface is guaranteed re-entrant and
4538 * thus may be called from driver code.
4539 *
4540 * \warning This is a beta API, and thus subject to change
4541 * at any point. It is not bound by the usual
4542 * interface stability promises.
4543 *
4544 * This is a helper provided to help you tune the
4545 * value passed to \c
4546 * psa_interruptible_set_max_ops().
4547 *
4548 * \param operation The \c psa_sign_hash_interruptible_operation_t
4549 * to use. This must be initialized first.
4550 *
4551 * \return Number of ops that the operation has taken so
4552 * far.
4553 */
4554uint32_t psa_sign_hash_get_num_ops(
4555 const psa_sign_hash_interruptible_operation_t *operation);
4556
4557/**
4558 * \brief Get the number of ops that a hash verification
4559 * operation has taken so far. If the operation
4560 * has completed, then this will represent the
4561 * number of ops required for the entire
4562 * operation. After initialization or calling \c
4563 * psa_verify_hash_interruptible_abort() on the
4564 * operation, a value of 0 will be returned.
4565 *
4566 * \warning This is a beta API, and thus subject to change
4567 * at any point. It is not bound by the usual
4568 * interface stability promises.
4569 *
4570 * This is a helper provided to help you tune the
4571 * value passed to \c
4572 * psa_interruptible_set_max_ops().
4573 *
4574 * \param operation The \c
4575 * psa_verify_hash_interruptible_operation_t to
4576 * use. This must be initialized first.
4577 *
4578 * \return Number of ops that the operation has taken so
4579 * far.
4580 */
4581uint32_t psa_verify_hash_get_num_ops(
4582 const psa_verify_hash_interruptible_operation_t *operation);
4583
4584/**
4585 * \brief Start signing a hash or short message with a
4586 * private key, in an interruptible manner.
4587 *
4588 * \see \c psa_sign_hash_complete()
4589 *
4590 * \warning This is a beta API, and thus subject to change
4591 * at any point. It is not bound by the usual
4592 * interface stability promises.
4593 *
4594 * \note This function combined with \c
4595 * psa_sign_hash_complete() is equivalent to
4596 * \c psa_sign_hash() but
4597 * \c psa_sign_hash_complete() can return early and
4598 * resume according to the limit set with \c
4599 * psa_interruptible_set_max_ops() to reduce the
4600 * maximum time spent in a function call.
4601 *
4602 * \note Users should call \c psa_sign_hash_complete()
4603 * repeatedly on the same context after a
4604 * successful call to this function until \c
4605 * psa_sign_hash_complete() either returns 0 or an
4606 * error. \c psa_sign_hash_complete() will return
4607 * #PSA_OPERATION_INCOMPLETE if there is more work
4608 * to do. Alternatively users can call
4609 * \c psa_sign_hash_abort() at any point if they no
4610 * longer want the result.
4611 *
4612 * \note If this function returns an error status, the
4613 * operation enters an error state and must be
4614 * aborted by calling \c psa_sign_hash_abort().
4615 *
4616 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4617 * to use. This must be initialized first.
4618 *
4619 * \param key Identifier of the key to use for the operation.
4620 * It must be an asymmetric key pair. The key must
4621 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4622 * \param alg A signature algorithm (\c PSA_ALG_XXX
4623 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4624 * is true), that is compatible with
4625 * the type of \p key.
4626 * \param[in] hash The hash or message to sign.
4627 * \param hash_length Size of the \p hash buffer in bytes.
4628 *
4629 * \retval #PSA_SUCCESS
4630 * The operation started successfully - call \c psa_sign_hash_complete()
4631 * with the same context to complete the operation
4632 *
4633 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4634 * \retval #PSA_ERROR_NOT_PERMITTED
4635 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4636 * not permit the requested algorithm.
4637 * \retval #PSA_ERROR_BAD_STATE
4638 * An operation has previously been started on this context, and is
4639 * still in progress.
4640 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4641 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4642 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4643 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4644 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4645 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4646 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4647 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4648 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4649 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4650 * \retval #PSA_ERROR_BAD_STATE
4651 * The library has not been previously initialized by psa_crypto_init().
4652 * It is implementation-dependent whether a failure to initialize
4653 * results in this error code.
4654 */
4655psa_status_t psa_sign_hash_start(
4656 psa_sign_hash_interruptible_operation_t *operation,
4657 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4658 const uint8_t *hash, size_t hash_length);
4659
4660/**
4661 * \brief Continue and eventually complete the action of
4662 * signing a hash or short message with a private
4663 * key, in an interruptible manner.
4664 *
4665 * \see \c psa_sign_hash_start()
4666 *
4667 * \warning This is a beta API, and thus subject to change
4668 * at any point. It is not bound by the usual
4669 * interface stability promises.
4670 *
4671 * \note This function combined with \c
4672 * psa_sign_hash_start() is equivalent to
4673 * \c psa_sign_hash() but this function can return
4674 * early and resume according to the limit set with
4675 * \c psa_interruptible_set_max_ops() to reduce the
4676 * maximum time spent in a function call.
4677 *
4678 * \note Users should call this function on the same
4679 * operation object repeatedly until it either
4680 * returns 0 or an error. This function will return
4681 * #PSA_OPERATION_INCOMPLETE if there is more work
4682 * to do. Alternatively users can call
4683 * \c psa_sign_hash_abort() at any point if they no
4684 * longer want the result.
4685 *
4686 * \note When this function returns successfully, the
4687 * operation becomes inactive. If this function
4688 * returns an error status, the operation enters an
4689 * error state and must be aborted by calling
4690 * \c psa_sign_hash_abort().
4691 *
4692 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4693 * to use. This must be initialized first, and have
4694 * had \c psa_sign_hash_start() called with it
4695 * first.
4696 *
4697 * \param[out] signature Buffer where the signature is to be written.
4698 * \param signature_size Size of the \p signature buffer in bytes. This
4699 * must be appropriate for the selected
4700 * algorithm and key:
4701 * - The required signature size is
4702 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4703 * key_bits, \c alg) where \c key_type and \c
4704 * key_bits are the type and bit-size
4705 * respectively of key.
4706 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4707 * maximum signature size of any supported
4708 * signature algorithm.
4709 * \param[out] signature_length On success, the number of bytes that make up
4710 * the returned signature value.
4711 *
4712 * \retval #PSA_SUCCESS
4713 * Operation completed successfully
4714 *
4715 * \retval #PSA_OPERATION_INCOMPLETE
4716 * Operation was interrupted due to the setting of \c
4717 * psa_interruptible_set_max_ops(). There is still work to be done.
4718 * Call this function again with the same operation object.
4719 *
4720 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4721 * The size of the \p signature buffer is too small. You can
4722 * determine a sufficient buffer size by calling
4723 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
4724 * where \c key_type and \c key_bits are the type and bit-size
4725 * respectively of \c key.
4726 *
4727 * \retval #PSA_ERROR_BAD_STATE
4728 * An operation was not previously started on this context via
4729 * \c psa_sign_hash_start().
4730 *
4731 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4732 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4735 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4736 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4737 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4738 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4739 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4740 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4741 * \retval #PSA_ERROR_BAD_STATE
4742 * The library has either not been previously initialized by
4743 * psa_crypto_init() or you did not previously call
4744 * psa_sign_hash_start() with this operation object. It is
4745 * implementation-dependent whether a failure to initialize results in
4746 * this error code.
4747 */
4748psa_status_t psa_sign_hash_complete(
4749 psa_sign_hash_interruptible_operation_t *operation,
4750 uint8_t *signature, size_t signature_size,
4751 size_t *signature_length);
4752
4753/**
4754 * \brief Abort a sign hash operation.
4755 *
4756 * \warning This is a beta API, and thus subject to change
4757 * at any point. It is not bound by the usual
4758 * interface stability promises.
4759 *
4760 * \note This function is the only function that clears
4761 * the number of ops completed as part of the
4762 * operation. Please ensure you copy this value via
4763 * \c psa_sign_hash_get_num_ops() if required
4764 * before calling.
4765 *
4766 * \note Aborting an operation frees all associated
4767 * resources except for the \p operation structure
4768 * itself. Once aborted, the operation object can
4769 * be reused for another operation by calling \c
4770 * psa_sign_hash_start() again.
4771 *
4772 * \note You may call this function any time after the
4773 * operation object has been initialized. In
4774 * particular, calling \c psa_sign_hash_abort()
4775 * after the operation has already been terminated
4776 * by a call to \c psa_sign_hash_abort() or
4777 * psa_sign_hash_complete() is safe.
4778 *
4779 * \param[in,out] operation Initialized sign hash operation.
4780 *
4781 * \retval #PSA_SUCCESS
4782 * The operation was aborted successfully.
4783 *
4784 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4785 * \retval #PSA_ERROR_BAD_STATE
4786 * The library has not been previously initialized by psa_crypto_init().
4787 * It is implementation-dependent whether a failure to initialize
4788 * results in this error code.
4789 */
4790psa_status_t psa_sign_hash_abort(
4791 psa_sign_hash_interruptible_operation_t *operation);
4792
4793/**
4794 * \brief Start reading and verifying a hash or short
4795 * message, in an interruptible manner.
4796 *
4797 * \see \c psa_verify_hash_complete()
4798 *
4799 * \warning This is a beta API, and thus subject to change
4800 * at any point. It is not bound by the usual
4801 * interface stability promises.
4802 *
4803 * \note This function combined with \c
4804 * psa_verify_hash_complete() is equivalent to
4805 * \c psa_verify_hash() but \c
4806 * psa_verify_hash_complete() can return early and
4807 * resume according to the limit set with \c
4808 * psa_interruptible_set_max_ops() to reduce the
4809 * maximum time spent in a function.
4810 *
4811 * \note Users should call \c psa_verify_hash_complete()
4812 * repeatedly on the same operation object after a
4813 * successful call to this function until \c
4814 * psa_verify_hash_complete() either returns 0 or
4815 * an error. \c psa_verify_hash_complete() will
4816 * return #PSA_OPERATION_INCOMPLETE if there is
4817 * more work to do. Alternatively users can call
4818 * \c psa_verify_hash_abort() at any point if they
4819 * no longer want the result.
4820 *
4821 * \note If this function returns an error status, the
4822 * operation enters an error state and must be
4823 * aborted by calling \c psa_verify_hash_abort().
4824 *
4825 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4826 * to use. This must be initialized first.
4827 *
4828 * \param key Identifier of the key to use for the operation.
4829 * The key must allow the usage
4830 * #PSA_KEY_USAGE_VERIFY_HASH.
4831 * \param alg A signature algorithm (\c PSA_ALG_XXX
4832 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4833 * is true), that is compatible with
4834 * the type of \p key.
4835 * \param[in] hash The hash whose signature is to be verified.
4836 * \param hash_length Size of the \p hash buffer in bytes.
4837 * \param[in] signature Buffer containing the signature to verify.
4838 * \param signature_length Size of the \p signature buffer in bytes.
4839 *
4840 * \retval #PSA_SUCCESS
4841 * The operation started successfully - please call \c
4842 * psa_verify_hash_complete() with the same context to complete the
4843 * operation.
4844 *
4845 * \retval #PSA_ERROR_BAD_STATE
4846 * Another operation has already been started on this context, and is
4847 * still in progress.
4848 *
4849 * \retval #PSA_ERROR_NOT_PERMITTED
4850 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4851 * not permit the requested algorithm.
4852 *
4853 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4854 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4856 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4857 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4858 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4859 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4860 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4861 * \retval PSA_ERROR_DATA_INVALID \emptydescription
4862 * \retval #PSA_ERROR_BAD_STATE
4863 * The library has not been previously initialized by psa_crypto_init().
4864 * It is implementation-dependent whether a failure to initialize
4865 * results in this error code.
4866 */
4867psa_status_t psa_verify_hash_start(
4868 psa_verify_hash_interruptible_operation_t *operation,
4869 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4870 const uint8_t *hash, size_t hash_length,
4871 const uint8_t *signature, size_t signature_length);
4872
4873/**
4874 * \brief Continue and eventually complete the action of
4875 * reading and verifying a hash or short message
4876 * signed with a private key, in an interruptible
4877 * manner.
4878 *
4879 * \see \c psa_verify_hash_start()
4880 *
4881 * \warning This is a beta API, and thus subject to change
4882 * at any point. It is not bound by the usual
4883 * interface stability promises.
4884 *
4885 * \note This function combined with \c
4886 * psa_verify_hash_start() is equivalent to
4887 * \c psa_verify_hash() but this function can
4888 * return early and resume according to the limit
4889 * set with \c psa_interruptible_set_max_ops() to
4890 * reduce the maximum time spent in a function
4891 * call.
4892 *
4893 * \note Users should call this function on the same
4894 * operation object repeatedly until it either
4895 * returns 0 or an error. This function will return
4896 * #PSA_OPERATION_INCOMPLETE if there is more work
4897 * to do. Alternatively users can call
4898 * \c psa_verify_hash_abort() at any point if they
4899 * no longer want the result.
4900 *
4901 * \note When this function returns successfully, the
4902 * operation becomes inactive. If this function
4903 * returns an error status, the operation enters an
4904 * error state and must be aborted by calling
4905 * \c psa_verify_hash_abort().
4906 *
4907 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4908 * to use. This must be initialized first, and have
4909 * had \c psa_verify_hash_start() called with it
4910 * first.
4911 *
4912 * \retval #PSA_SUCCESS
4913 * Operation completed successfully, and the passed signature is valid.
4914 *
4915 * \retval #PSA_OPERATION_INCOMPLETE
4916 * Operation was interrupted due to the setting of \c
4917 * psa_interruptible_set_max_ops(). There is still work to be done.
4918 * Call this function again with the same operation object.
4919 *
4920 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4921 * \retval #PSA_ERROR_INVALID_SIGNATURE
4922 * The calculation was performed successfully, but the passed
4923 * signature is not a valid signature.
4924 * \retval #PSA_ERROR_BAD_STATE
4925 * An operation was not previously started on this context via
4926 * \c psa_verify_hash_start().
4927 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4928 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4929 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4930 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4931 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4932 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4933 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4934 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4935 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4936 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4937 * \retval #PSA_ERROR_BAD_STATE
4938 * The library has either not been previously initialized by
4939 * psa_crypto_init() or you did not previously call
4940 * psa_verify_hash_start() on this object. It is
4941 * implementation-dependent whether a failure to initialize results in
4942 * this error code.
4943 */
4944psa_status_t psa_verify_hash_complete(
4945 psa_verify_hash_interruptible_operation_t *operation);
4946
4947/**
4948 * \brief Abort a verify hash operation.
4949 *
4950 * \warning This is a beta API, and thus subject to change at
4951 * any point. It is not bound by the usual interface
4952 * stability promises.
4953 *
4954 * \note This function is the only function that clears the
4955 * number of ops completed as part of the operation.
4956 * Please ensure you copy this value via
4957 * \c psa_verify_hash_get_num_ops() if required
4958 * before calling.
4959 *
4960 * \note Aborting an operation frees all associated
4961 * resources except for the operation structure
4962 * itself. Once aborted, the operation object can be
4963 * reused for another operation by calling \c
4964 * psa_verify_hash_start() again.
4965 *
4966 * \note You may call this function any time after the
4967 * operation object has been initialized.
4968 * In particular, calling \c psa_verify_hash_abort()
4969 * after the operation has already been terminated by
4970 * a call to \c psa_verify_hash_abort() or
4971 * psa_verify_hash_complete() is safe.
4972 *
4973 * \param[in,out] operation Initialized verify hash operation.
4974 *
4975 * \retval #PSA_SUCCESS
4976 * The operation was aborted successfully.
4977 *
4978 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4979 * \retval #PSA_ERROR_BAD_STATE
4980 * The library has not been previously initialized by psa_crypto_init().
4981 * It is implementation-dependent whether a failure to initialize
4982 * results in this error code.
4983 */
4984psa_status_t psa_verify_hash_abort(
4985 psa_verify_hash_interruptible_operation_t *operation);
4986
4987
4988/**@}*/
4989
4990#ifdef __cplusplus
4991}
4992#endif
4993
4994/* The file "crypto_extra.h" contains vendor-specific definitions. This
4995 * can include vendor-defined algorithms, extra functions, etc. */
4996#include "crypto_extra.h"
4997
4998#endif /* PSA_CRYPTO_H */