Skip to content
0.2.0-dev Download ZIP
5999 lines ·289.7 KB Raw
1/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5/*
6 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8 */
9
10#ifndef PSA_CRYPTO_H
11#define PSA_CRYPTO_H
12
13#if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)
14#include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
15#else
16#include "crypto_platform.h"
17#endif
18
19#include <stddef.h>
20
21#ifdef __DOXYGEN_ONLY__
22/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
23 * must be defined in the crypto_platform.h header. These mock definitions
24 * are present in this file as a convenience to generate pretty-printed
25 * documentation that includes those definitions. */
26
27/** \defgroup platform Implementation-specific definitions
28 * @{
29 */
30
31/**@}*/
32#endif /* __DOXYGEN_ONLY__ */
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/* The file "crypto_types.h" declares types that encode errors,
39 * algorithms, key types, policies, etc. */
40#include "crypto_types.h"
41
42/** \defgroup version API version
43 * @{
44 */
45
46/**
47 * The major version of this implementation of the PSA Crypto API
48 */
49#define PSA_CRYPTO_API_VERSION_MAJOR 1
50
51/**
52 * The minor version of this implementation of the PSA Crypto API
53 */
54#define PSA_CRYPTO_API_VERSION_MINOR 2
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 RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
684 * is the non-encrypted DER encoding of the representation defined by
685 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
686 * ```
687 * RSAPrivateKey ::= SEQUENCE {
688 * version INTEGER, -- must be 0
689 * modulus INTEGER, -- n
690 * publicExponent INTEGER, -- e
691 * privateExponent INTEGER, -- d
692 * prime1 INTEGER, -- p
693 * prime2 INTEGER, -- q
694 * exponent1 INTEGER, -- d mod (p-1)
695 * exponent2 INTEGER, -- d mod (q-1)
696 * coefficient INTEGER, -- (inverse of q) mod p
697 * }
698 * ```
699 * - For elliptic curve key pairs (key types for which
700 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
701 * a representation of the private value as a `ceiling(m/8)`-byte string
702 * where `m` is the bit size associated with the curve, i.e. the bit size
703 * of the order of the curve's coordinate field. This byte string is
704 * in little-endian order for Montgomery curves (curve types
705 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
706 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
707 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
708 * For Weierstrass curves, this is the content of the `privateKey` field of
709 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
710 * the format is defined by RFC 7748, and output is masked according to §5.
711 * For twisted Edwards curves, the private key is as defined by RFC 8032
712 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
713 * - For Diffie-Hellman key exchange key pairs (key types for which
714 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
715 * format is the representation of the private key `x` as a big-endian byte
716 * string. The length of the byte string is the private key size in bytes
717 * (leading zeroes are not stripped).
718 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
719 * true), the format is the same as for psa_export_public_key().
720 *
721 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
722 *
723 * \param key Identifier of the key to export. It must allow the
724 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
725 * key.
726 * \param[out] data Buffer where the key data is to be written.
727 * \param data_size Size of the \p data buffer in bytes.
728 * \param[out] data_length On success, the number of bytes
729 * that make up the key data.
730 *
731 * \retval #PSA_SUCCESS \emptydescription
732 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
733 * \retval #PSA_ERROR_NOT_PERMITTED
734 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
735 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
736 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
737 * The size of the \p data buffer is too small. You can determine a
738 * sufficient buffer size by calling
739 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
740 * where \c type is the key type
741 * and \c bits is the key size in bits.
742 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
743 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
744 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
745 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
746 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
747 * \retval #PSA_ERROR_BAD_STATE
748 * The library has not been previously initialized by psa_crypto_init().
749 * It is implementation-dependent whether a failure to initialize
750 * results in this error code.
751 */
752psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
753 uint8_t *data,
754 size_t data_size,
755 size_t *data_length);
756
757/**
758 * \brief Export a public key or the public part of a key pair in binary format.
759 *
760 * The output of this function can be passed to psa_import_key() to
761 * create an object that is equivalent to the public key.
762 *
763 * This specification supports a single format for each key type.
764 * Implementations may support other formats as long as the standard
765 * format is supported. Implementations that support other formats
766 * should ensure that the formats are clearly unambiguous so as to
767 * minimize the risk that an invalid input is accidentally interpreted
768 * according to a different format.
769 *
770 * For standard key types, the output format is as follows:
771 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
772 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
773 * ```
774 * RSAPublicKey ::= SEQUENCE {
775 * modulus INTEGER, -- n
776 * publicExponent INTEGER } -- e
777 * ```
778 * - For elliptic curve keys on a twisted Edwards curve (key types for which
779 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
780 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
781 * by RFC 8032
782 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
783 * - For other elliptic curve public keys (key types for which
784 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
785 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
786 * Let `m` be the bit size associated with the curve, i.e. the bit size of
787 * `q` for a curve over `F_q`. The representation consists of:
788 * - The byte 0x04;
789 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
790 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
791 * - For Diffie-Hellman key exchange public keys (key types for which
792 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
793 * the format is the representation of the public key `y = g^x mod p` as a
794 * big-endian byte string. The length of the byte string is the length of the
795 * base prime `p` in bytes.
796 *
797 * Exporting a public key object or the public part of a key pair is
798 * always permitted, regardless of the key's usage flags.
799 *
800 * \param key Identifier of the key to export.
801 * \param[out] data Buffer where the key data is to be written.
802 * \param data_size Size of the \p data buffer in bytes.
803 * \param[out] data_length On success, the number of bytes
804 * that make up the key data.
805 *
806 * \retval #PSA_SUCCESS \emptydescription
807 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
808 * \retval #PSA_ERROR_INVALID_ARGUMENT
809 * The key is neither a public key nor a key pair.
810 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
811 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
812 * The size of the \p data buffer is too small. You can determine a
813 * sufficient buffer size by calling
814 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
815 * where \c type is the key type
816 * and \c bits is the key size in bits.
817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
818 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
819 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
820 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
822 * \retval #PSA_ERROR_BAD_STATE
823 * The library has not been previously initialized by psa_crypto_init().
824 * It is implementation-dependent whether a failure to initialize
825 * results in this error code.
826 */
827psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
828 uint8_t *data,
829 size_t data_size,
830 size_t *data_length);
831
832
833
834/**@}*/
835
836/** \defgroup hash Message digests
837 * @{
838 */
839
840/** Calculate the hash (digest) of a message.
841 *
842 * \note To verify the hash of a message against an
843 * expected value, use psa_hash_compare() instead.
844 *
845 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
846 * such that #PSA_ALG_IS_HASH(\p alg) is true).
847 * \param[in] input Buffer containing the message to hash.
848 * \param input_length Size of the \p input buffer in bytes.
849 * \param[out] hash Buffer where the hash is to be written.
850 * \param hash_size Size of the \p hash buffer in bytes.
851 * \param[out] hash_length On success, the number of bytes
852 * that make up the hash value. This is always
853 * #PSA_HASH_LENGTH(\p alg).
854 *
855 * \retval #PSA_SUCCESS
856 * Success.
857 * \retval #PSA_ERROR_NOT_SUPPORTED
858 * \p alg is not supported or is not a hash algorithm.
859 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
860 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
861 * \p hash_size is too small
862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
864 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
865 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
866 * \retval #PSA_ERROR_BAD_STATE
867 * The library has not been previously initialized by psa_crypto_init().
868 * It is implementation-dependent whether a failure to initialize
869 * results in this error code.
870 */
871psa_status_t psa_hash_compute(psa_algorithm_t alg,
872 const uint8_t *input,
873 size_t input_length,
874 uint8_t *hash,
875 size_t hash_size,
876 size_t *hash_length);
877
878/** Calculate the hash (digest) of a message and compare it with a
879 * reference value.
880 *
881 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
882 * such that #PSA_ALG_IS_HASH(\p alg) is true).
883 * \param[in] input Buffer containing the message to hash.
884 * \param input_length Size of the \p input buffer in bytes.
885 * \param[in] hash Buffer containing the expected hash value.
886 * \param hash_length Size of the \p hash buffer in bytes.
887 *
888 * \retval #PSA_SUCCESS
889 * The expected hash is identical to the actual hash of the input.
890 * \retval #PSA_ERROR_INVALID_SIGNATURE
891 * The hash of the message was calculated successfully, but it
892 * differs from the expected hash.
893 * \retval #PSA_ERROR_NOT_SUPPORTED
894 * \p alg is not supported or is not a hash algorithm.
895 * \retval #PSA_ERROR_INVALID_ARGUMENT
896 * \p input_length or \p hash_length do not match the hash size for \p alg
897 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
898 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
899 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
900 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
901 * \retval #PSA_ERROR_BAD_STATE
902 * The library has not been previously initialized by psa_crypto_init().
903 * It is implementation-dependent whether a failure to initialize
904 * results in this error code.
905 */
906psa_status_t psa_hash_compare(psa_algorithm_t alg,
907 const uint8_t *input,
908 size_t input_length,
909 const uint8_t *hash,
910 size_t hash_length);
911
912/** The type of the state data structure for multipart hash operations.
913 *
914 * Before calling any function on a hash operation object, the application must
915 * initialize it by any of the following means:
916 * - Set the structure to all-bits-zero, for example:
917 * \code
918 * psa_hash_operation_t operation;
919 * memset(&operation, 0, sizeof(operation));
920 * \endcode
921 * - Initialize the structure to logical zero values, for example:
922 * \code
923 * psa_hash_operation_t operation = {0};
924 * \endcode
925 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
926 * for example:
927 * \code
928 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
929 * \endcode
930 * - Assign the result of the function psa_hash_operation_init()
931 * to the structure, for example:
932 * \code
933 * psa_hash_operation_t operation;
934 * operation = psa_hash_operation_init();
935 * \endcode
936 *
937 * This is an implementation-defined \c struct. Applications should not
938 * make any assumptions about the content of this structure.
939 * Implementation details can change in future versions without notice. */
940typedef struct psa_hash_operation_s psa_hash_operation_t;
941
942/** \def PSA_HASH_OPERATION_INIT
943 *
944 * This macro returns a suitable initializer for a hash operation object
945 * of type #psa_hash_operation_t.
946 */
947
948/** Return an initial value for a hash operation object.
949 */
950static psa_hash_operation_t psa_hash_operation_init(void);
951
952/** Set up a multipart hash operation.
953 *
954 * The sequence of operations to calculate a hash (message digest)
955 * is as follows:
956 * -# Allocate an operation object which will be passed to all the functions
957 * listed here.
958 * -# Initialize the operation object with one of the methods described in the
959 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
960 * -# Call psa_hash_setup() to specify the algorithm.
961 * -# Call psa_hash_update() zero, one or more times, passing a fragment
962 * of the message each time. The hash that is calculated is the hash
963 * of the concatenation of these messages in order.
964 * -# To calculate the hash, call psa_hash_finish().
965 * To compare the hash with an expected value, call psa_hash_verify().
966 *
967 * If an error occurs at any step after a call to psa_hash_setup(), the
968 * operation will need to be reset by a call to psa_hash_abort(). The
969 * application may call psa_hash_abort() at any time after the operation
970 * has been initialized.
971 *
972 * After a successful call to psa_hash_setup(), the application must
973 * eventually terminate the operation. The following events terminate an
974 * operation:
975 * - A successful call to psa_hash_finish() or psa_hash_verify().
976 * - A call to psa_hash_abort().
977 *
978 * \param[in,out] operation The operation object to set up. It must have
979 * been initialized as per the documentation for
980 * #psa_hash_operation_t and not yet in use.
981 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
982 * such that #PSA_ALG_IS_HASH(\p alg) is true).
983 *
984 * \retval #PSA_SUCCESS
985 * Success.
986 * \retval #PSA_ERROR_NOT_SUPPORTED
987 * \p alg is not a supported hash algorithm.
988 * \retval #PSA_ERROR_INVALID_ARGUMENT
989 * \p alg is not a hash algorithm.
990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
992 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
993 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
994 * \retval #PSA_ERROR_BAD_STATE
995 * The operation state is not valid (it must be inactive), or
996 * the library has not been previously initialized by psa_crypto_init().
997 * It is implementation-dependent whether a failure to initialize
998 * results in this error code.
999 */
1000psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
1001 psa_algorithm_t alg);
1002
1003/** Add a message fragment to a multipart hash operation.
1004 *
1005 * The application must call psa_hash_setup() before calling this function.
1006 *
1007 * If this function returns an error status, the operation enters an error
1008 * state and must be aborted by calling psa_hash_abort().
1009 *
1010 * \param[in,out] operation Active hash operation.
1011 * \param[in] input Buffer containing the message fragment to hash.
1012 * \param input_length Size of the \p input buffer in bytes.
1013 *
1014 * \retval #PSA_SUCCESS
1015 * Success.
1016 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1017 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1018 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1019 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1020 * \retval #PSA_ERROR_BAD_STATE
1021 * The operation state is not valid (it must be active), or
1022 * the library has not been previously initialized by psa_crypto_init().
1023 * It is implementation-dependent whether a failure to initialize
1024 * results in this error code.
1025 */
1026psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1027 const uint8_t *input,
1028 size_t input_length);
1029
1030/** Finish the calculation of the hash of a message.
1031 *
1032 * The application must call psa_hash_setup() before calling this function.
1033 * This function calculates the hash of the message formed by concatenating
1034 * the inputs passed to preceding calls to psa_hash_update().
1035 *
1036 * When this function returns successfully, the operation becomes inactive.
1037 * If this function returns an error status, the operation enters an error
1038 * state and must be aborted by calling psa_hash_abort().
1039 *
1040 * \warning Applications should not call this function if they expect
1041 * a specific value for the hash. Call psa_hash_verify() instead.
1042 * Beware that comparing integrity or authenticity data such as
1043 * hash values with a function such as \c memcmp is risky
1044 * because the time taken by the comparison may leak information
1045 * about the hashed data which could allow an attacker to guess
1046 * a valid hash and thereby bypass security controls.
1047 *
1048 * \param[in,out] operation Active hash operation.
1049 * \param[out] hash Buffer where the hash is to be written.
1050 * \param hash_size Size of the \p hash buffer in bytes.
1051 * \param[out] hash_length On success, the number of bytes
1052 * that make up the hash value. This is always
1053 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
1054 * hash algorithm that is calculated.
1055 *
1056 * \retval #PSA_SUCCESS
1057 * Success.
1058 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1059 * The size of the \p hash buffer is too small. You can determine a
1060 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
1061 * where \c alg is the hash algorithm that is calculated.
1062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1063 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1064 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1065 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1066 * \retval #PSA_ERROR_BAD_STATE
1067 * The operation state is not valid (it must be active), or
1068 * the library has not been previously initialized by psa_crypto_init().
1069 * It is implementation-dependent whether a failure to initialize
1070 * results in this error code.
1071 */
1072psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1073 uint8_t *hash,
1074 size_t hash_size,
1075 size_t *hash_length);
1076
1077/** Finish the calculation of the hash of a message and compare it with
1078 * an expected value.
1079 *
1080 * The application must call psa_hash_setup() before calling this function.
1081 * This function calculates the hash of the message formed by concatenating
1082 * the inputs passed to preceding calls to psa_hash_update(). It then
1083 * compares the calculated hash with the expected hash passed as a
1084 * parameter to this function.
1085 *
1086 * When this function returns successfully, the operation becomes inactive.
1087 * If this function returns an error status, the operation enters an error
1088 * state and must be aborted by calling psa_hash_abort().
1089 *
1090 * \note Implementations shall make the best effort to ensure that the
1091 * comparison between the actual hash and the expected hash is performed
1092 * in constant time.
1093 *
1094 * \param[in,out] operation Active hash operation.
1095 * \param[in] hash Buffer containing the expected hash value.
1096 * \param hash_length Size of the \p hash buffer in bytes.
1097 *
1098 * \retval #PSA_SUCCESS
1099 * The expected hash is identical to the actual hash of the message.
1100 * \retval #PSA_ERROR_INVALID_SIGNATURE
1101 * The hash of the message was calculated successfully, but it
1102 * differs from the expected hash.
1103 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1104 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1105 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1106 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1107 * \retval #PSA_ERROR_BAD_STATE
1108 * The operation state is not valid (it must be active), or
1109 * the library has not been previously initialized by psa_crypto_init().
1110 * It is implementation-dependent whether a failure to initialize
1111 * results in this error code.
1112 */
1113psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1114 const uint8_t *hash,
1115 size_t hash_length);
1116
1117/** Abort a hash operation.
1118 *
1119 * Aborting an operation frees all associated resources except for the
1120 * \p operation structure itself. Once aborted, the operation object
1121 * can be reused for another operation by calling
1122 * psa_hash_setup() again.
1123 *
1124 * You may call this function any time after the operation object has
1125 * been initialized by one of the methods described in #psa_hash_operation_t.
1126 *
1127 * In particular, calling psa_hash_abort() after the operation has been
1128 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1129 * psa_hash_verify() is safe and has no effect.
1130 *
1131 * \param[in,out] operation Initialized hash operation.
1132 *
1133 * \retval #PSA_SUCCESS \emptydescription
1134 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1135 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1136 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1137 * \retval #PSA_ERROR_BAD_STATE
1138 * The library has not been previously initialized by psa_crypto_init().
1139 * It is implementation-dependent whether a failure to initialize
1140 * results in this error code.
1141 */
1142psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1143
1144/** Clone a hash operation.
1145 *
1146 * This function copies the state of an ongoing hash operation to
1147 * a new operation object. In other words, this function is equivalent
1148 * to calling psa_hash_setup() on \p target_operation with the same
1149 * algorithm that \p source_operation was set up for, then
1150 * psa_hash_update() on \p target_operation with the same input that
1151 * that was passed to \p source_operation. After this function returns, the
1152 * two objects are independent, i.e. subsequent calls involving one of
1153 * the objects do not affect the other object.
1154 *
1155 * \param[in] source_operation The active hash operation to clone.
1156 * \param[in,out] target_operation The operation object to set up.
1157 * It must be initialized but not active.
1158 *
1159 * \retval #PSA_SUCCESS \emptydescription
1160 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1161 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1162 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1164 * \retval #PSA_ERROR_BAD_STATE
1165 * The \p source_operation state is not valid (it must be active), or
1166 * the \p target_operation state is not valid (it must be inactive), or
1167 * the library has not been previously initialized by psa_crypto_init().
1168 * It is implementation-dependent whether a failure to initialize
1169 * results in this error code.
1170 */
1171psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1172 psa_hash_operation_t *target_operation);
1173
1174/**@}*/
1175
1176/** \defgroup XOF Extendable-operation functions (XOF)
1177 * @{
1178 */
1179
1180/** The type of the state data structure for multipart XOF operations.
1181 *
1182 * Before calling any function on a XOF operation object, the application must
1183 * initialize it by any of the following means:
1184 * - Set the structure to all-bits-zero, for example:
1185 * \code
1186 * psa_xof_operation_t operation;
1187 * memset(&operation, 0, sizeof(operation));
1188 * \endcode
1189 * - Initialize the structure to logical zero values, for example:
1190 * \code
1191 * psa_xof_operation_t operation = {0};
1192 * \endcode
1193 * - Initialize the structure to the initializer #PSA_XOF_OPERATION_INIT,
1194 * for example:
1195 * \code
1196 * psa_xof_operation_t operation = PSA_XOF_OPERATION_INIT;
1197 * \endcode
1198 * - Assign the result of the function psa_xof_operation_init()
1199 * to the structure, for example:
1200 * \code
1201 * psa_xof_operation_t operation;
1202 * operation = psa_xof_operation_init();
1203 * \endcode
1204 *
1205 * This is an implementation-defined \c struct. Applications should not
1206 * make any assumptions about the content of this structure.
1207 * Implementation details can change in future versions without notice. */
1208typedef struct psa_xof_operation_s psa_xof_operation_t;
1209
1210/** \def PSA_XOF_OPERATION_INIT
1211 *
1212 * This macro returns a suitable initializer for a XOF operation object
1213 * of type #psa_xof_operation_t.
1214 */
1215
1216/** Return an initial value for a XOF operation object.
1217 */
1218static psa_xof_operation_t psa_xof_operation_init(void);
1219
1220/** Set up a multipart XOF (extendable-operation function) operation.
1221 *
1222 * The sequence of operations to calculate a XOF is as follows:
1223 * -# Allocate an operation object which will be passed to all the functions
1224 * listed here.
1225 * -# Initialize the operation object with one of the methods described in the
1226 * documentation for #psa_xof_operation_t, e.g. #PSA_XOF_OPERATION_INIT.
1227 * -# Call psa_xof_setup() to specify the algorithm.
1228 * -# If the XOF uses a context, call psa_xof_set_context().
1229 * -# Call psa_xof_update() zero, one or more times, passing successive
1230 * fragments of the input.
1231 * -# Call psa_xof_output() zero, one or more times to obtain successive
1232 * fragments of the output.
1233 * -# Call psa_xof_abort() to free the resources associated with the
1234 * operation (other than the operation object itself).
1235 *
1236 * If an error occurs at any step after a call to psa_xof_setup(), the
1237 * operation will need to be reset by a call to psa_xof_abort(). The
1238 * application may call psa_xof_abort() at any time after the operation
1239 * has been initialized.
1240 *
1241 * After a successful call to psa_xof_setup(), the application must
1242 * eventually terminate the operation by calling psa_xof_abort().
1243 *
1244 * \param[in,out] operation The operation object to set up. It must have
1245 * been initialized as per the documentation for
1246 * #psa_xof_operation_t and not yet in use.
1247 * \param alg The XOF algorithm to compute (\c PSA_ALG_XXX value
1248 * such that #PSA_ALG_IS_XOF(\p alg) is true).
1249 *
1250 * \retval #PSA_SUCCESS
1251 * Success.
1252 * \retval #PSA_ERROR_NOT_SUPPORTED
1253 * \p alg is not supported.
1254 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1255 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1256 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1257 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1258 * \retval #PSA_ERROR_BAD_STATE
1259 * The operation state is not valid (it must be inactive).
1260 */
1261psa_status_t psa_xof_setup(psa_xof_operation_t *operation,
1262 psa_algorithm_t alg);
1263
1264/** Pass a context to a multipart XOF (extendable-operation function) operation.
1265 *
1266 * \param[in,out] operation The operation object to use. It must have
1267 * been set up with psa_xof_setup(), and must
1268 * not yet have been received a context with
1269 * psa_xof_set_context(), received input with
1270 * psa_xof_update(), switched to output mode with
1271 * psa_xof_output(), or aborted with psa_xof_abort().
1272 * \param[in] context The context to use.
1273 * \param context_length Size of the \p context buffer in bytes.
1274 *
1275 * \retval #PSA_SUCCESS
1276 * Success.
1277 * \retval #PSA_ERROR_INVALID_ARGUMENT
1278 * The algorithm used by \p operation does not allow a context,
1279 * or the context value is invalid for this algorithm.
1280 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1282 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1283 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1284 * \retval #PSA_ERROR_BAD_STATE
1285 * The operation state is not valid (it must be active, it must
1286 * not already have a context set, it must not already have input,
1287 * and it must not have already been switched to output mode).
1288 */
1289psa_status_t psa_xof_set_context(psa_xof_operation_t *operation,
1290 const uint8_t *context, size_t context_length);
1291
1292/** Pass input to a multipart XOF (extendable-operation function) operation.
1293 *
1294 * This function switches the operation to input mode, even when
1295 * \p input_length is 0.
1296 *
1297 * \param[in,out] operation The operation object to use. It must have
1298 * been set up with psa_xof_setup(). It must
1299 * have a context set with psa_xof_set_context()
1300 * if the algorithm requires it. It must not
1301 * yet have been switched to output mode with
1302 * psa_xof_output() or aborted with psa_xof_abort().
1303 * \param[in] input The input fragment.
1304 * \param input_length Size of the \p input buffer in bytes.
1305 *
1306 * \retval #PSA_SUCCESS
1307 * Success.
1308 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1309 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1310 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1311 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1312 * \retval #PSA_ERROR_BAD_STATE
1313 * The operation state is not valid (it must be active, it must
1314 * have a context set if the algorithm requires it, and it must
1315 * not yet have been switched to output mode).
1316 */
1317psa_status_t psa_xof_update(psa_xof_operation_t *operation,
1318 const uint8_t *input, size_t input_length);
1319
1320/** Extract output from a multipart XOF (extendable-operation function) operation.
1321 *
1322 * This function switches the operation to output mode, even when
1323 * \p output_length is 0.
1324 *
1325 * \param[in,out] operation The operation object to use. It must have
1326 * been set up with psa_xof_setup(). It must
1327 * have a context set with psa_xof_set_context()
1328 * if the algorithm requires it. It must not
1329 * yet have been aborted with psa_xof_abort().
1330 * \param[out] output On success, the output fragment.
1331 * \param output_length The number of bytes to write to \p output.
1332 *
1333 * \retval #PSA_SUCCESS
1334 * Success.
1335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1337 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1338 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1339 * \retval #PSA_ERROR_BAD_STATE
1340 * The operation state is not valid (it must be active, and
1341 * it must have a context set if the algorithm requires it).
1342 */
1343psa_status_t psa_xof_output(psa_xof_operation_t *operation,
1344 uint8_t *output, size_t output_length);
1345
1346/** Abort a multipart XOF (extendable-operation function) operation.
1347 *
1348 * \param[in,out] operation The operation object to abort. It must have
1349 * been initialized as per the documentation for
1350 * #psa_xof_operation_t and not yet in use.
1351 *
1352 * \retval #PSA_SUCCESS
1353 * Success.
1354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1356 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1357 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1358 * \retval #PSA_ERROR_BAD_STATE
1359 * The operation state is corrupted.
1360 */
1361psa_status_t psa_xof_abort(psa_xof_operation_t *operation);
1362
1363/**@}*/
1364
1365/** \defgroup MAC Message authentication codes
1366 * @{
1367 */
1368
1369/** Calculate the MAC (message authentication code) of a message.
1370 *
1371 * \note To verify the MAC of a message against an
1372 * expected value, use psa_mac_verify() instead.
1373 * Beware that comparing integrity or authenticity data such as
1374 * MAC values with a function such as \c memcmp is risky
1375 * because the time taken by the comparison may leak information
1376 * about the MAC value which could allow an attacker to guess
1377 * a valid MAC and thereby bypass security controls.
1378 *
1379 * \param key Identifier of the key to use for the operation. It
1380 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
1381 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1382 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1383 * \param[in] input Buffer containing the input message.
1384 * \param input_length Size of the \p input buffer in bytes.
1385 * \param[out] mac Buffer where the MAC value is to be written.
1386 * \param mac_size Size of the \p mac buffer in bytes.
1387 * \param[out] mac_length On success, the number of bytes
1388 * that make up the MAC value.
1389 *
1390 * \retval #PSA_SUCCESS
1391 * Success.
1392 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1393 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1394 * \retval #PSA_ERROR_INVALID_ARGUMENT
1395 * \p key is not compatible with \p alg.
1396 * \retval #PSA_ERROR_NOT_SUPPORTED
1397 * \p alg is not supported or is not a MAC algorithm.
1398 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1399 * \p mac_size is too small
1400 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1401 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1402 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1403 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1404 * \retval #PSA_ERROR_STORAGE_FAILURE
1405 * The key could not be retrieved from storage.
1406 * \retval #PSA_ERROR_BAD_STATE
1407 * The library has not been previously initialized by psa_crypto_init().
1408 * It is implementation-dependent whether a failure to initialize
1409 * results in this error code.
1410 */
1411psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
1412 psa_algorithm_t alg,
1413 const uint8_t *input,
1414 size_t input_length,
1415 uint8_t *mac,
1416 size_t mac_size,
1417 size_t *mac_length);
1418
1419/** Calculate the MAC of a message and compare it with a reference value.
1420 *
1421 * \param key Identifier of the key to use for the operation. It
1422 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
1423 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1424 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1425 * \param[in] input Buffer containing the input message.
1426 * \param input_length Size of the \p input buffer in bytes.
1427 * \param[in] mac Buffer containing the expected MAC value.
1428 * \param mac_length Size of the \p mac buffer in bytes.
1429 *
1430 * \retval #PSA_SUCCESS
1431 * The expected MAC is identical to the actual MAC of the input.
1432 * \retval #PSA_ERROR_INVALID_SIGNATURE
1433 * The MAC of the message was calculated successfully, but it
1434 * differs from the expected value.
1435 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1436 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1437 * \retval #PSA_ERROR_INVALID_ARGUMENT
1438 * \p key is not compatible with \p alg.
1439 * \retval #PSA_ERROR_NOT_SUPPORTED
1440 * \p alg is not supported or is not a MAC algorithm.
1441 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1442 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1443 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1444 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1445 * \retval #PSA_ERROR_STORAGE_FAILURE
1446 * The key could not be retrieved from storage.
1447 * \retval #PSA_ERROR_BAD_STATE
1448 * The library has not been previously initialized by psa_crypto_init().
1449 * It is implementation-dependent whether a failure to initialize
1450 * results in this error code.
1451 */
1452psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
1453 psa_algorithm_t alg,
1454 const uint8_t *input,
1455 size_t input_length,
1456 const uint8_t *mac,
1457 size_t mac_length);
1458
1459/** The type of the state data structure for multipart MAC operations.
1460 *
1461 * Before calling any function on a MAC operation object, the application must
1462 * initialize it by any of the following means:
1463 * - Set the structure to all-bits-zero, for example:
1464 * \code
1465 * psa_mac_operation_t operation;
1466 * memset(&operation, 0, sizeof(operation));
1467 * \endcode
1468 * - Initialize the structure to logical zero values, for example:
1469 * \code
1470 * psa_mac_operation_t operation = {0};
1471 * \endcode
1472 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1473 * for example:
1474 * \code
1475 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1476 * \endcode
1477 * - Assign the result of the function psa_mac_operation_init()
1478 * to the structure, for example:
1479 * \code
1480 * psa_mac_operation_t operation;
1481 * operation = psa_mac_operation_init();
1482 * \endcode
1483 *
1484 *
1485 * This is an implementation-defined \c struct. Applications should not
1486 * make any assumptions about the content of this structure.
1487 * Implementation details can change in future versions without notice. */
1488typedef struct psa_mac_operation_s psa_mac_operation_t;
1489
1490/** \def PSA_MAC_OPERATION_INIT
1491 *
1492 * This macro returns a suitable initializer for a MAC operation object of type
1493 * #psa_mac_operation_t.
1494 */
1495
1496/** Return an initial value for a MAC operation object.
1497 */
1498static psa_mac_operation_t psa_mac_operation_init(void);
1499
1500/** Set up a multipart MAC calculation operation.
1501 *
1502 * This function sets up the calculation of the MAC
1503 * (message authentication code) of a byte string.
1504 * To verify the MAC of a message against an
1505 * expected value, use psa_mac_verify_setup() instead.
1506 *
1507 * The sequence of operations to calculate a MAC is as follows:
1508 * -# Allocate an operation object which will be passed to all the functions
1509 * listed here.
1510 * -# Initialize the operation object with one of the methods described in the
1511 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
1512 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
1513 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1514 * of the message each time. The MAC that is calculated is the MAC
1515 * of the concatenation of these messages in order.
1516 * -# At the end of the message, call psa_mac_sign_finish() to finish
1517 * calculating the MAC value and retrieve it.
1518 *
1519 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1520 * operation will need to be reset by a call to psa_mac_abort(). The
1521 * application may call psa_mac_abort() at any time after the operation
1522 * has been initialized.
1523 *
1524 * After a successful call to psa_mac_sign_setup(), the application must
1525 * eventually terminate the operation through one of the following methods:
1526 * - A successful call to psa_mac_sign_finish().
1527 * - A call to psa_mac_abort().
1528 *
1529 * \param[in,out] operation The operation object to set up. It must have
1530 * been initialized as per the documentation for
1531 * #psa_mac_operation_t and not yet in use.
1532 * \param key Identifier of the key to use for the operation. It
1533 * must remain valid until the operation terminates.
1534 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
1535 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1536 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1537 *
1538 * \retval #PSA_SUCCESS
1539 * Success.
1540 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1541 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1542 * \retval #PSA_ERROR_INVALID_ARGUMENT
1543 * \p key is not compatible with \p alg.
1544 * \retval #PSA_ERROR_NOT_SUPPORTED
1545 * \p alg is not supported or is not a MAC algorithm.
1546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1548 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1549 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1550 * \retval #PSA_ERROR_STORAGE_FAILURE
1551 * The key could not be retrieved from storage.
1552 * \retval #PSA_ERROR_BAD_STATE
1553 * The operation state is not valid (it must be inactive), or
1554 * the library has not been previously initialized by psa_crypto_init().
1555 * It is implementation-dependent whether a failure to initialize
1556 * results in this error code.
1557 */
1558psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1559 mbedtls_svc_key_id_t key,
1560 psa_algorithm_t alg);
1561
1562/** Set up a multipart MAC verification operation.
1563 *
1564 * This function sets up the verification of the MAC
1565 * (message authentication code) of a byte string against an expected value.
1566 *
1567 * The sequence of operations to verify a MAC is as follows:
1568 * -# Allocate an operation object which will be passed to all the functions
1569 * listed here.
1570 * -# Initialize the operation object with one of the methods described in the
1571 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
1572 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1573 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1574 * of the message each time. The MAC that is calculated is the MAC
1575 * of the concatenation of these messages in order.
1576 * -# At the end of the message, call psa_mac_verify_finish() to finish
1577 * calculating the actual MAC of the message and verify it against
1578 * the expected value.
1579 *
1580 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1581 * operation will need to be reset by a call to psa_mac_abort(). The
1582 * application may call psa_mac_abort() at any time after the operation
1583 * has been initialized.
1584 *
1585 * After a successful call to psa_mac_verify_setup(), the application must
1586 * eventually terminate the operation through one of the following methods:
1587 * - A successful call to psa_mac_verify_finish().
1588 * - A call to psa_mac_abort().
1589 *
1590 * \param[in,out] operation The operation object to set up. It must have
1591 * been initialized as per the documentation for
1592 * #psa_mac_operation_t and not yet in use.
1593 * \param key Identifier of the key to use for the operation. It
1594 * must remain valid until the operation terminates.
1595 * It must allow the usage
1596 * PSA_KEY_USAGE_VERIFY_MESSAGE.
1597 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1598 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1599 *
1600 * \retval #PSA_SUCCESS
1601 * Success.
1602 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1603 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1604 * \retval #PSA_ERROR_INVALID_ARGUMENT
1605 * \c key is not compatible with \c alg.
1606 * \retval #PSA_ERROR_NOT_SUPPORTED
1607 * \c alg is not supported or is not a MAC algorithm.
1608 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1610 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1611 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1612 * \retval #PSA_ERROR_STORAGE_FAILURE
1613 * The key could not be retrieved from storage.
1614 * \retval #PSA_ERROR_BAD_STATE
1615 * The operation state is not valid (it must be inactive), or
1616 * the library has not been previously initialized by psa_crypto_init().
1617 * It is implementation-dependent whether a failure to initialize
1618 * results in this error code.
1619 */
1620psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1621 mbedtls_svc_key_id_t key,
1622 psa_algorithm_t alg);
1623
1624/** Add a message fragment to a multipart MAC operation.
1625 *
1626 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1627 * before calling this function.
1628 *
1629 * If this function returns an error status, the operation enters an error
1630 * state and must be aborted by calling psa_mac_abort().
1631 *
1632 * \param[in,out] operation Active MAC operation.
1633 * \param[in] input Buffer containing the message fragment to add to
1634 * the MAC calculation.
1635 * \param input_length Size of the \p input buffer in bytes.
1636 *
1637 * \retval #PSA_SUCCESS
1638 * Success.
1639 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1641 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1642 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1643 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1644 * \retval #PSA_ERROR_BAD_STATE
1645 * The operation state is not valid (it must be active), or
1646 * the library has not been previously initialized by psa_crypto_init().
1647 * It is implementation-dependent whether a failure to initialize
1648 * results in this error code.
1649 */
1650psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1651 const uint8_t *input,
1652 size_t input_length);
1653
1654/** Finish the calculation of the MAC of a message.
1655 *
1656 * The application must call psa_mac_sign_setup() before calling this function.
1657 * This function calculates the MAC of the message formed by concatenating
1658 * the inputs passed to preceding calls to psa_mac_update().
1659 *
1660 * When this function returns successfully, the operation becomes inactive.
1661 * If this function returns an error status, the operation enters an error
1662 * state and must be aborted by calling psa_mac_abort().
1663 *
1664 * \warning Applications should not call this function if they expect
1665 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1666 * Beware that comparing integrity or authenticity data such as
1667 * MAC values with a function such as \c memcmp is risky
1668 * because the time taken by the comparison may leak information
1669 * about the MAC value which could allow an attacker to guess
1670 * a valid MAC and thereby bypass security controls.
1671 *
1672 * \param[in,out] operation Active MAC operation.
1673 * \param[out] mac Buffer where the MAC value is to be written.
1674 * \param mac_size Size of the \p mac buffer in bytes.
1675 * \param[out] mac_length On success, the number of bytes
1676 * that make up the MAC value. This is always
1677 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
1678 * where \c key_type and \c key_bits are the type and
1679 * bit-size respectively of the key and \c alg is the
1680 * MAC algorithm that is calculated.
1681 *
1682 * \retval #PSA_SUCCESS
1683 * Success.
1684 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1685 * The size of the \p mac buffer is too small. You can determine a
1686 * sufficient buffer size by calling PSA_MAC_LENGTH().
1687 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1688 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1689 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1690 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1691 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1692 * \retval #PSA_ERROR_BAD_STATE
1693 * The operation state is not valid (it must be an active mac sign
1694 * operation), or the library has not been previously initialized
1695 * by psa_crypto_init().
1696 * It is implementation-dependent whether a failure to initialize
1697 * results in this error code.
1698 */
1699psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1700 uint8_t *mac,
1701 size_t mac_size,
1702 size_t *mac_length);
1703
1704/** Finish the calculation of the MAC of a message and compare it with
1705 * an expected value.
1706 *
1707 * The application must call psa_mac_verify_setup() before calling this function.
1708 * This function calculates the MAC of the message formed by concatenating
1709 * the inputs passed to preceding calls to psa_mac_update(). It then
1710 * compares the calculated MAC with the expected MAC passed as a
1711 * parameter to this function.
1712 *
1713 * When this function returns successfully, the operation becomes inactive.
1714 * If this function returns an error status, the operation enters an error
1715 * state and must be aborted by calling psa_mac_abort().
1716 *
1717 * \note Implementations shall make the best effort to ensure that the
1718 * comparison between the actual MAC and the expected MAC is performed
1719 * in constant time.
1720 *
1721 * \param[in,out] operation Active MAC operation.
1722 * \param[in] mac Buffer containing the expected MAC value.
1723 * \param mac_length Size of the \p mac buffer in bytes.
1724 *
1725 * \retval #PSA_SUCCESS
1726 * The expected MAC is identical to the actual MAC of the message.
1727 * \retval #PSA_ERROR_INVALID_SIGNATURE
1728 * The MAC of the message was calculated successfully, but it
1729 * differs from the expected MAC.
1730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1732 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1733 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1734 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1735 * \retval #PSA_ERROR_BAD_STATE
1736 * The operation state is not valid (it must be an active mac verify
1737 * operation), or the library has not been previously initialized
1738 * by psa_crypto_init().
1739 * It is implementation-dependent whether a failure to initialize
1740 * results in this error code.
1741 */
1742psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1743 const uint8_t *mac,
1744 size_t mac_length);
1745
1746/** Abort a MAC operation.
1747 *
1748 * Aborting an operation frees all associated resources except for the
1749 * \p operation structure itself. Once aborted, the operation object
1750 * can be reused for another operation by calling
1751 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1752 *
1753 * You may call this function any time after the operation object has
1754 * been initialized by one of the methods described in #psa_mac_operation_t.
1755 *
1756 * In particular, calling psa_mac_abort() after the operation has been
1757 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1758 * psa_mac_verify_finish() is safe and has no effect.
1759 *
1760 * \param[in,out] operation Initialized MAC operation.
1761 *
1762 * \retval #PSA_SUCCESS \emptydescription
1763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1764 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1765 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1766 * \retval #PSA_ERROR_BAD_STATE
1767 * The library has not been previously initialized by psa_crypto_init().
1768 * It is implementation-dependent whether a failure to initialize
1769 * results in this error code.
1770 */
1771psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1772
1773/**@}*/
1774
1775/** \defgroup cipher Symmetric ciphers
1776 * @{
1777 */
1778
1779/** Encrypt a message using a symmetric cipher.
1780 *
1781 * This function encrypts a message with a random IV (initialization
1782 * vector). Use the multipart operation interface with a
1783 * #psa_cipher_operation_t object to provide other forms of IV.
1784 *
1785 * \param key Identifier of the key to use for the operation.
1786 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
1787 * \param alg The cipher algorithm to compute
1788 * (\c PSA_ALG_XXX value such that
1789 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1790 * \param[in] input Buffer containing the message to encrypt.
1791 * \param input_length Size of the \p input buffer in bytes.
1792 * \param[out] output Buffer where the output is to be written.
1793 * The output contains the IV followed by
1794 * the ciphertext proper.
1795 * \param output_size Size of the \p output buffer in bytes.
1796 * \param[out] output_length On success, the number of bytes
1797 * that make up the output.
1798 *
1799 * \retval #PSA_SUCCESS
1800 * Success.
1801 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1802 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1803 * \retval #PSA_ERROR_INVALID_ARGUMENT
1804 * \p key is not compatible with \p alg.
1805 * \retval #PSA_ERROR_NOT_SUPPORTED
1806 * \p alg is not supported or is not a cipher algorithm.
1807 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1810 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1811 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1812 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1813 * \retval #PSA_ERROR_BAD_STATE
1814 * The library has not been previously initialized by psa_crypto_init().
1815 * It is implementation-dependent whether a failure to initialize
1816 * results in this error code.
1817 */
1818psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
1819 psa_algorithm_t alg,
1820 const uint8_t *input,
1821 size_t input_length,
1822 uint8_t *output,
1823 size_t output_size,
1824 size_t *output_length);
1825
1826/** Decrypt a message using a symmetric cipher.
1827 *
1828 * This function decrypts a message encrypted with a symmetric cipher.
1829 *
1830 * \param key Identifier of the key to use for the operation.
1831 * It must remain valid until the operation
1832 * terminates. It must allow the usage
1833 * #PSA_KEY_USAGE_DECRYPT.
1834 * \param alg The cipher algorithm to compute
1835 * (\c PSA_ALG_XXX value such that
1836 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1837 * \param[in] input Buffer containing the message to decrypt.
1838 * This consists of the IV followed by the
1839 * ciphertext proper.
1840 * \param input_length Size of the \p input buffer in bytes.
1841 * \param[out] output Buffer where the plaintext is to be written.
1842 * \param output_size Size of the \p output buffer in bytes.
1843 * \param[out] output_length On success, the number of bytes
1844 * that make up the output.
1845 *
1846 * \retval #PSA_SUCCESS
1847 * Success.
1848 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1849 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1850 * \retval #PSA_ERROR_INVALID_ARGUMENT
1851 * \p key is not compatible with \p alg.
1852 * \retval #PSA_ERROR_NOT_SUPPORTED
1853 * \p alg is not supported or is not a cipher algorithm.
1854 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1856 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1857 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1858 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1859 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1860 * \retval #PSA_ERROR_BAD_STATE
1861 * The library has not been previously initialized by psa_crypto_init().
1862 * It is implementation-dependent whether a failure to initialize
1863 * results in this error code.
1864 */
1865psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
1866 psa_algorithm_t alg,
1867 const uint8_t *input,
1868 size_t input_length,
1869 uint8_t *output,
1870 size_t output_size,
1871 size_t *output_length);
1872
1873/** The type of the state data structure for multipart cipher operations.
1874 *
1875 * Before calling any function on a cipher operation object, the application
1876 * must initialize it by any of the following means:
1877 * - Set the structure to all-bits-zero, for example:
1878 * \code
1879 * psa_cipher_operation_t operation;
1880 * memset(&operation, 0, sizeof(operation));
1881 * \endcode
1882 * - Initialize the structure to logical zero values, for example:
1883 * \code
1884 * psa_cipher_operation_t operation = {0};
1885 * \endcode
1886 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1887 * for example:
1888 * \code
1889 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1890 * \endcode
1891 * - Assign the result of the function psa_cipher_operation_init()
1892 * to the structure, for example:
1893 * \code
1894 * psa_cipher_operation_t operation;
1895 * operation = psa_cipher_operation_init();
1896 * \endcode
1897 *
1898 * This is an implementation-defined \c struct. Applications should not
1899 * make any assumptions about the content of this structure.
1900 * Implementation details can change in future versions without notice. */
1901typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1902
1903/** \def PSA_CIPHER_OPERATION_INIT
1904 *
1905 * This macro returns a suitable initializer for a cipher operation object of
1906 * type #psa_cipher_operation_t.
1907 */
1908
1909/** Return an initial value for a cipher operation object.
1910 */
1911static psa_cipher_operation_t psa_cipher_operation_init(void);
1912
1913/** Set the key for a multipart symmetric encryption operation.
1914 *
1915 * The sequence of operations to encrypt a message with a symmetric cipher
1916 * is as follows:
1917 * -# Allocate an operation object which will be passed to all the functions
1918 * listed here.
1919 * -# Initialize the operation object with one of the methods described in the
1920 * documentation for #psa_cipher_operation_t, e.g.
1921 * #PSA_CIPHER_OPERATION_INIT.
1922 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
1923 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
1924 * generate or set the IV (initialization vector). You should use
1925 * psa_cipher_generate_iv() unless the protocol you are implementing
1926 * requires a specific IV value.
1927 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1928 * of the message each time.
1929 * -# Call psa_cipher_finish().
1930 *
1931 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1932 * the operation will need to be reset by a call to psa_cipher_abort(). The
1933 * application may call psa_cipher_abort() at any time after the operation
1934 * has been initialized.
1935 *
1936 * After a successful call to psa_cipher_encrypt_setup(), the application must
1937 * eventually terminate the operation. The following events terminate an
1938 * operation:
1939 * - A successful call to psa_cipher_finish().
1940 * - A call to psa_cipher_abort().
1941 *
1942 * \param[in,out] operation The operation object to set up. It must have
1943 * been initialized as per the documentation for
1944 * #psa_cipher_operation_t and not yet in use.
1945 * \param key Identifier of the key to use for the operation.
1946 * It must remain valid until the operation
1947 * terminates. It must allow the usage
1948 * #PSA_KEY_USAGE_ENCRYPT.
1949 * \param alg The cipher algorithm to compute
1950 * (\c PSA_ALG_XXX value such that
1951 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1952 *
1953 * \retval #PSA_SUCCESS
1954 * Success.
1955 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1956 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1957 * \retval #PSA_ERROR_INVALID_ARGUMENT
1958 * \p key is not compatible with \p alg.
1959 * \retval #PSA_ERROR_NOT_SUPPORTED
1960 * \p alg is not supported or is not a cipher algorithm.
1961 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1962 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1963 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1964 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1965 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1966 * \retval #PSA_ERROR_BAD_STATE
1967 * The operation state is not valid (it must be inactive), or
1968 * the library has not been previously initialized by psa_crypto_init().
1969 * It is implementation-dependent whether a failure to initialize
1970 * results in this error code.
1971 */
1972psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1973 mbedtls_svc_key_id_t key,
1974 psa_algorithm_t alg);
1975
1976/** Set the key for a multipart symmetric decryption operation.
1977 *
1978 * The sequence of operations to decrypt a message with a symmetric cipher
1979 * is as follows:
1980 * -# Allocate an operation object which will be passed to all the functions
1981 * listed here.
1982 * -# Initialize the operation object with one of the methods described in the
1983 * documentation for #psa_cipher_operation_t, e.g.
1984 * #PSA_CIPHER_OPERATION_INIT.
1985 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
1986 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
1987 * decryption. If the IV is prepended to the ciphertext, you can call
1988 * psa_cipher_update() on a buffer containing the IV followed by the
1989 * beginning of the message.
1990 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1991 * of the message each time.
1992 * -# Call psa_cipher_finish().
1993 *
1994 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1995 * the operation will need to be reset by a call to psa_cipher_abort(). The
1996 * application may call psa_cipher_abort() at any time after the operation
1997 * has been initialized.
1998 *
1999 * After a successful call to psa_cipher_decrypt_setup(), the application must
2000 * eventually terminate the operation. The following events terminate an
2001 * operation:
2002 * - A successful call to psa_cipher_finish().
2003 * - A call to psa_cipher_abort().
2004 *
2005 * \param[in,out] operation The operation object to set up. It must have
2006 * been initialized as per the documentation for
2007 * #psa_cipher_operation_t and not yet in use.
2008 * \param key Identifier of the key to use for the operation.
2009 * It must remain valid until the operation
2010 * terminates. It must allow the usage
2011 * #PSA_KEY_USAGE_DECRYPT.
2012 * \param alg The cipher algorithm to compute
2013 * (\c PSA_ALG_XXX value such that
2014 * #PSA_ALG_IS_CIPHER(\p alg) is true).
2015 *
2016 * \retval #PSA_SUCCESS
2017 * Success.
2018 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2019 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2020 * \retval #PSA_ERROR_INVALID_ARGUMENT
2021 * \p key is not compatible with \p alg.
2022 * \retval #PSA_ERROR_NOT_SUPPORTED
2023 * \p alg is not supported or is not a cipher algorithm.
2024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2026 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2027 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2028 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2029 * \retval #PSA_ERROR_BAD_STATE
2030 * The operation state is not valid (it must be inactive), or
2031 * the library has not been previously initialized by psa_crypto_init().
2032 * It is implementation-dependent whether a failure to initialize
2033 * results in this error code.
2034 */
2035psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2036 mbedtls_svc_key_id_t key,
2037 psa_algorithm_t alg);
2038
2039/** Generate an IV for a symmetric encryption operation.
2040 *
2041 * This function generates a random IV (initialization vector), nonce
2042 * or initial counter value for the encryption operation as appropriate
2043 * for the chosen algorithm, key type and key size.
2044 *
2045 * The application must call psa_cipher_encrypt_setup() before
2046 * calling this function.
2047 *
2048 * If this function returns an error status, the operation enters an error
2049 * state and must be aborted by calling psa_cipher_abort().
2050 *
2051 * \param[in,out] operation Active cipher operation.
2052 * \param[out] iv Buffer where the generated IV is to be written.
2053 * \param iv_size Size of the \p iv buffer in bytes.
2054 * \param[out] iv_length On success, the number of bytes of the
2055 * generated IV.
2056 *
2057 * \retval #PSA_SUCCESS
2058 * Success.
2059 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2060 * The size of the \p iv buffer is too small.
2061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2063 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2064 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2065 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2066 * \retval #PSA_ERROR_BAD_STATE
2067 * The operation state is not valid (it must be active, with no IV set),
2068 * or the library has not been previously initialized
2069 * by psa_crypto_init().
2070 * It is implementation-dependent whether a failure to initialize
2071 * results in this error code.
2072 */
2073psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2074 uint8_t *iv,
2075 size_t iv_size,
2076 size_t *iv_length);
2077
2078/** Set the IV for a symmetric encryption or decryption operation.
2079 *
2080 * This function sets the IV (initialization vector), nonce
2081 * or initial counter value for the encryption or decryption operation.
2082 *
2083 * The application must call psa_cipher_encrypt_setup() before
2084 * calling this function.
2085 *
2086 * If this function returns an error status, the operation enters an error
2087 * state and must be aborted by calling psa_cipher_abort().
2088 *
2089 * \note When encrypting, applications should use psa_cipher_generate_iv()
2090 * instead of this function, unless implementing a protocol that requires
2091 * a non-random IV.
2092 *
2093 * \param[in,out] operation Active cipher operation.
2094 * \param[in] iv Buffer containing the IV to use.
2095 * \param iv_length Size of the IV in bytes.
2096 *
2097 * \retval #PSA_SUCCESS
2098 * Success.
2099 * \retval #PSA_ERROR_INVALID_ARGUMENT
2100 * The size of \p iv is not acceptable for the chosen algorithm,
2101 * or the chosen algorithm does not use an IV.
2102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2104 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2105 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2106 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2107 * \retval #PSA_ERROR_BAD_STATE
2108 * The operation state is not valid (it must be an active cipher
2109 * encrypt operation, with no IV set), or the library has not been
2110 * previously initialized by psa_crypto_init().
2111 * It is implementation-dependent whether a failure to initialize
2112 * results in this error code.
2113 */
2114psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2115 const uint8_t *iv,
2116 size_t iv_length);
2117
2118/** Encrypt or decrypt a message fragment in an active cipher operation.
2119 *
2120 * Before calling this function, you must:
2121 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2122 * The choice of setup function determines whether this function
2123 * encrypts or decrypts its input.
2124 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2125 * (recommended when encrypting) or psa_cipher_set_iv().
2126 *
2127 * If this function returns an error status, the operation enters an error
2128 * state and must be aborted by calling psa_cipher_abort().
2129 *
2130 * \param[in,out] operation Active cipher operation.
2131 * \param[in] input Buffer containing the message fragment to
2132 * encrypt or decrypt.
2133 * \param input_length Size of the \p input buffer in bytes.
2134 * \param[out] output Buffer where the output is to be written.
2135 * \param output_size Size of the \p output buffer in bytes.
2136 * \param[out] output_length On success, the number of bytes
2137 * that make up the returned output.
2138 *
2139 * \retval #PSA_SUCCESS
2140 * Success.
2141 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2142 * The size of the \p output buffer is too small.
2143 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2145 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2146 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2147 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2148 * \retval #PSA_ERROR_BAD_STATE
2149 * The operation state is not valid (it must be active, with an IV set
2150 * if required for the algorithm), or the library has not been
2151 * previously initialized by psa_crypto_init().
2152 * It is implementation-dependent whether a failure to initialize
2153 * results in this error code.
2154 */
2155psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2156 const uint8_t *input,
2157 size_t input_length,
2158 uint8_t *output,
2159 size_t output_size,
2160 size_t *output_length);
2161
2162/** Finish encrypting or decrypting a message in a cipher operation.
2163 *
2164 * The application must call psa_cipher_encrypt_setup() or
2165 * psa_cipher_decrypt_setup() before calling this function. The choice
2166 * of setup function determines whether this function encrypts or
2167 * decrypts its input.
2168 *
2169 * This function finishes the encryption or decryption of the message
2170 * formed by concatenating the inputs passed to preceding calls to
2171 * psa_cipher_update().
2172 *
2173 * When this function returns successfully, the operation becomes inactive.
2174 * If this function returns an error status, the operation enters an error
2175 * state and must be aborted by calling psa_cipher_abort().
2176 *
2177 * \param[in,out] operation Active cipher operation.
2178 * \param[out] output Buffer where the output is to be written.
2179 * \param output_size Size of the \p output buffer in bytes.
2180 * \param[out] output_length On success, the number of bytes
2181 * that make up the returned output.
2182 *
2183 * \retval #PSA_SUCCESS
2184 * Success.
2185 * \retval #PSA_ERROR_INVALID_ARGUMENT
2186 * The total input size passed to this operation is not valid for
2187 * this particular algorithm. For example, the algorithm is a based
2188 * on block cipher and requires a whole number of blocks, but the
2189 * total input size is not a multiple of the block size.
2190 * \retval #PSA_ERROR_INVALID_PADDING
2191 * This is a decryption operation for an algorithm that includes
2192 * padding, and the ciphertext does not contain valid padding.
2193 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2194 * The size of the \p output buffer is too small.
2195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2197 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2198 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2199 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2200 * \retval #PSA_ERROR_BAD_STATE
2201 * The operation state is not valid (it must be active, with an IV set
2202 * if required for the algorithm), or the library has not been
2203 * previously initialized by psa_crypto_init().
2204 * It is implementation-dependent whether a failure to initialize
2205 * results in this error code.
2206 */
2207psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2208 uint8_t *output,
2209 size_t output_size,
2210 size_t *output_length);
2211
2212/** Abort a cipher operation.
2213 *
2214 * Aborting an operation frees all associated resources except for the
2215 * \p operation structure itself. Once aborted, the operation object
2216 * can be reused for another operation by calling
2217 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2218 *
2219 * You may call this function any time after the operation object has
2220 * been initialized as described in #psa_cipher_operation_t.
2221 *
2222 * In particular, calling psa_cipher_abort() after the operation has been
2223 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2224 * is safe and has no effect.
2225 *
2226 * \param[in,out] operation Initialized cipher operation.
2227 *
2228 * \retval #PSA_SUCCESS \emptydescription
2229 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2230 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2231 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2232 * \retval #PSA_ERROR_BAD_STATE
2233 * The library has not been previously initialized by psa_crypto_init().
2234 * It is implementation-dependent whether a failure to initialize
2235 * results in this error code.
2236 */
2237psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2238
2239/**@}*/
2240
2241/** \defgroup aead Authenticated encryption with associated data (AEAD)
2242 * @{
2243 */
2244
2245/** Process an authenticated encryption operation.
2246 *
2247 * \param key Identifier of the key to use for the
2248 * operation. It must allow the usage
2249 * #PSA_KEY_USAGE_ENCRYPT.
2250 * \param alg The AEAD algorithm to compute
2251 * (\c PSA_ALG_XXX value such that
2252 * #PSA_ALG_IS_AEAD(\p alg) is true).
2253 * \param[in] nonce Nonce or IV to use.
2254 * \param nonce_length Size of the \p nonce buffer in bytes.
2255 * \param[in] additional_data Additional data that will be authenticated
2256 * but not encrypted.
2257 * \param additional_data_length Size of \p additional_data in bytes.
2258 * \param[in] plaintext Data that will be authenticated and
2259 * encrypted.
2260 * \param plaintext_length Size of \p plaintext in bytes.
2261 * \param[out] ciphertext Output buffer for the authenticated and
2262 * encrypted data. The additional data is not
2263 * part of this output. For algorithms where the
2264 * encrypted data and the authentication tag
2265 * are defined as separate outputs, the
2266 * authentication tag is appended to the
2267 * encrypted data.
2268 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2269 * This must be appropriate for the selected
2270 * algorithm and key:
2271 * - A sufficient output size is
2272 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2273 * \p alg, \p plaintext_length) where
2274 * \c key_type is the type of \p key.
2275 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2276 * plaintext_length) evaluates to the maximum
2277 * ciphertext size of any supported AEAD
2278 * encryption.
2279 * \param[out] ciphertext_length On success, the size of the output
2280 * in the \p ciphertext buffer.
2281 *
2282 * \retval #PSA_SUCCESS
2283 * Success.
2284 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2285 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2286 * \retval #PSA_ERROR_INVALID_ARGUMENT
2287 * \p key is not compatible with \p alg.
2288 * \retval #PSA_ERROR_NOT_SUPPORTED
2289 * \p alg is not supported or is not an AEAD algorithm.
2290 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2291 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2292 * \p ciphertext_size is too small.
2293 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2294 * \p plaintext_length) or
2295 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2296 * determine the required buffer size.
2297 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2298 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2299 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2300 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2301 * \retval #PSA_ERROR_BAD_STATE
2302 * The library has not been previously initialized by psa_crypto_init().
2303 * It is implementation-dependent whether a failure to initialize
2304 * results in this error code.
2305 */
2306psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
2307 psa_algorithm_t alg,
2308 const uint8_t *nonce,
2309 size_t nonce_length,
2310 const uint8_t *additional_data,
2311 size_t additional_data_length,
2312 const uint8_t *plaintext,
2313 size_t plaintext_length,
2314 uint8_t *ciphertext,
2315 size_t ciphertext_size,
2316 size_t *ciphertext_length);
2317
2318/** Process an authenticated decryption operation.
2319 *
2320 * \param key Identifier of the key to use for the
2321 * operation. It must allow the usage
2322 * #PSA_KEY_USAGE_DECRYPT.
2323 * \param alg The AEAD algorithm to compute
2324 * (\c PSA_ALG_XXX value such that
2325 * #PSA_ALG_IS_AEAD(\p alg) is true).
2326 * \param[in] nonce Nonce or IV to use.
2327 * \param nonce_length Size of the \p nonce buffer in bytes.
2328 * \param[in] additional_data Additional data that has been authenticated
2329 * but not encrypted.
2330 * \param additional_data_length Size of \p additional_data in bytes.
2331 * \param[in] ciphertext Data that has been authenticated and
2332 * encrypted. For algorithms where the
2333 * encrypted data and the authentication tag
2334 * are defined as separate inputs, the buffer
2335 * must contain the encrypted data followed
2336 * by the authentication tag.
2337 * \param ciphertext_length Size of \p ciphertext in bytes.
2338 * \param[out] plaintext Output buffer for the decrypted data.
2339 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2340 * This must be appropriate for the selected
2341 * algorithm and key:
2342 * - A sufficient output size is
2343 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2344 * \p alg, \p ciphertext_length) where
2345 * \c key_type is the type of \p key.
2346 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2347 * ciphertext_length) evaluates to the maximum
2348 * plaintext size of any supported AEAD
2349 * decryption.
2350 * \param[out] plaintext_length On success, the size of the output
2351 * in the \p plaintext buffer.
2352 *
2353 * \retval #PSA_SUCCESS
2354 * Success.
2355 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2356 * \retval #PSA_ERROR_INVALID_SIGNATURE
2357 * The ciphertext is not authentic.
2358 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2359 * \retval #PSA_ERROR_INVALID_ARGUMENT
2360 * \p key is not compatible with \p alg.
2361 * \retval #PSA_ERROR_NOT_SUPPORTED
2362 * \p alg is not supported or is not an AEAD algorithm.
2363 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2364 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2365 * \p plaintext_size is too small.
2366 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2367 * \p ciphertext_length) or
2368 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2369 * to determine the required buffer size.
2370 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2371 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2372 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2373 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2374 * \retval #PSA_ERROR_BAD_STATE
2375 * The library has not been previously initialized by psa_crypto_init().
2376 * It is implementation-dependent whether a failure to initialize
2377 * results in this error code.
2378 */
2379psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
2380 psa_algorithm_t alg,
2381 const uint8_t *nonce,
2382 size_t nonce_length,
2383 const uint8_t *additional_data,
2384 size_t additional_data_length,
2385 const uint8_t *ciphertext,
2386 size_t ciphertext_length,
2387 uint8_t *plaintext,
2388 size_t plaintext_size,
2389 size_t *plaintext_length);
2390
2391/** The type of the state data structure for multipart AEAD operations.
2392 *
2393 * Before calling any function on an AEAD operation object, the application
2394 * must initialize it by any of the following means:
2395 * - Set the structure to all-bits-zero, for example:
2396 * \code
2397 * psa_aead_operation_t operation;
2398 * memset(&operation, 0, sizeof(operation));
2399 * \endcode
2400 * - Initialize the structure to logical zero values, for example:
2401 * \code
2402 * psa_aead_operation_t operation = {0};
2403 * \endcode
2404 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2405 * for example:
2406 * \code
2407 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2408 * \endcode
2409 * - Assign the result of the function psa_aead_operation_init()
2410 * to the structure, for example:
2411 * \code
2412 * psa_aead_operation_t operation;
2413 * operation = psa_aead_operation_init();
2414 * \endcode
2415 *
2416 * This is an implementation-defined \c struct. Applications should not
2417 * make any assumptions about the content of this structure.
2418 * Implementation details can change in future versions without notice. */
2419typedef struct psa_aead_operation_s psa_aead_operation_t;
2420
2421/** \def PSA_AEAD_OPERATION_INIT
2422 *
2423 * This macro returns a suitable initializer for an AEAD operation object of
2424 * type #psa_aead_operation_t.
2425 */
2426
2427/** Return an initial value for an AEAD operation object.
2428 */
2429static psa_aead_operation_t psa_aead_operation_init(void);
2430
2431/** Set the key for a multipart authenticated encryption operation.
2432 *
2433 * The sequence of operations to encrypt a message with authentication
2434 * is as follows:
2435 * -# Allocate an operation object which will be passed to all the functions
2436 * listed here.
2437 * -# Initialize the operation object with one of the methods described in the
2438 * documentation for #psa_aead_operation_t, e.g.
2439 * #PSA_AEAD_OPERATION_INIT.
2440 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2441 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2442 * inputs to the subsequent calls to psa_aead_update_ad() and
2443 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2444 * for details.
2445 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2446 * generate or set the nonce. You should use
2447 * psa_aead_generate_nonce() unless the protocol you are implementing
2448 * requires a specific nonce value.
2449 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2450 * of the non-encrypted additional authenticated data each time.
2451 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2452 * of the message to encrypt each time.
2453 * -# Call psa_aead_finish().
2454 *
2455 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2456 * the operation will need to be reset by a call to psa_aead_abort(). The
2457 * application may call psa_aead_abort() at any time after the operation
2458 * has been initialized.
2459 *
2460 * After a successful call to psa_aead_encrypt_setup(), the application must
2461 * eventually terminate the operation. The following events terminate an
2462 * operation:
2463 * - A successful call to psa_aead_finish().
2464 * - A call to psa_aead_abort().
2465 *
2466 * \param[in,out] operation The operation object to set up. It must have
2467 * been initialized as per the documentation for
2468 * #psa_aead_operation_t and not yet in use.
2469 * \param key Identifier of the key to use for the operation.
2470 * It must remain valid until the operation
2471 * terminates. It must allow the usage
2472 * #PSA_KEY_USAGE_ENCRYPT.
2473 * \param alg The AEAD algorithm to compute
2474 * (\c PSA_ALG_XXX value such that
2475 * #PSA_ALG_IS_AEAD(\p alg) is true).
2476 *
2477 * \retval #PSA_SUCCESS
2478 * Success.
2479 * \retval #PSA_ERROR_BAD_STATE
2480 * The operation state is not valid (it must be inactive), or
2481 * the library has not been previously initialized by psa_crypto_init().
2482 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2483 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2484 * \retval #PSA_ERROR_INVALID_ARGUMENT
2485 * \p key is not compatible with \p alg.
2486 * \retval #PSA_ERROR_NOT_SUPPORTED
2487 * \p alg is not supported or is not an AEAD algorithm.
2488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2489 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2490 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2491 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2492 * \retval #PSA_ERROR_STORAGE_FAILURE
2493 * The library has not been previously initialized by psa_crypto_init().
2494 * It is implementation-dependent whether a failure to initialize
2495 * results in this error code.
2496 */
2497psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2498 mbedtls_svc_key_id_t key,
2499 psa_algorithm_t alg);
2500
2501/** Set the key for a multipart authenticated decryption operation.
2502 *
2503 * The sequence of operations to decrypt a message with authentication
2504 * is as follows:
2505 * -# Allocate an operation object which will be passed to all the functions
2506 * listed here.
2507 * -# Initialize the operation object with one of the methods described in the
2508 * documentation for #psa_aead_operation_t, e.g.
2509 * #PSA_AEAD_OPERATION_INIT.
2510 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2511 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2512 * inputs to the subsequent calls to psa_aead_update_ad() and
2513 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2514 * for details.
2515 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2516 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2517 * of the non-encrypted additional authenticated data each time.
2518 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2519 * of the ciphertext to decrypt each time.
2520 * -# Call psa_aead_verify().
2521 *
2522 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2523 * the operation will need to be reset by a call to psa_aead_abort(). The
2524 * application may call psa_aead_abort() at any time after the operation
2525 * has been initialized.
2526 *
2527 * After a successful call to psa_aead_decrypt_setup(), the application must
2528 * eventually terminate the operation. The following events terminate an
2529 * operation:
2530 * - A successful call to psa_aead_verify().
2531 * - A call to psa_aead_abort().
2532 *
2533 * \param[in,out] operation The operation object to set up. It must have
2534 * been initialized as per the documentation for
2535 * #psa_aead_operation_t and not yet in use.
2536 * \param key Identifier of the key to use for the operation.
2537 * It must remain valid until the operation
2538 * terminates. It must allow the usage
2539 * #PSA_KEY_USAGE_DECRYPT.
2540 * \param alg The AEAD algorithm to compute
2541 * (\c PSA_ALG_XXX value such that
2542 * #PSA_ALG_IS_AEAD(\p alg) is true).
2543 *
2544 * \retval #PSA_SUCCESS
2545 * Success.
2546 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2547 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
2548 * \retval #PSA_ERROR_INVALID_ARGUMENT
2549 * \p key is not compatible with \p alg.
2550 * \retval #PSA_ERROR_NOT_SUPPORTED
2551 * \p alg is not supported or is not an AEAD algorithm.
2552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2554 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2555 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2556 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2557 * \retval #PSA_ERROR_BAD_STATE
2558 * The operation state is not valid (it must be inactive), or the
2559 * library has not been previously initialized by psa_crypto_init().
2560 * It is implementation-dependent whether a failure to initialize
2561 * results in this error code.
2562 */
2563psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2564 mbedtls_svc_key_id_t key,
2565 psa_algorithm_t alg);
2566
2567/** Generate a random nonce for an authenticated encryption operation.
2568 *
2569 * This function generates a random nonce for the authenticated encryption
2570 * operation with an appropriate size for the chosen algorithm, key type
2571 * and key size.
2572 *
2573 * The application must call psa_aead_encrypt_setup() before
2574 * calling this function.
2575 *
2576 * If this function returns an error status, the operation enters an error
2577 * state and must be aborted by calling psa_aead_abort().
2578 *
2579 * \param[in,out] operation Active AEAD operation.
2580 * \param[out] nonce Buffer where the generated nonce is to be
2581 * written.
2582 * \param nonce_size Size of the \p nonce buffer in bytes.
2583 * \param[out] nonce_length On success, the number of bytes of the
2584 * generated nonce.
2585 *
2586 * \retval #PSA_SUCCESS
2587 * Success.
2588 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2589 * The size of the \p nonce buffer is too small.
2590 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2592 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2593 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2594 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2595 * \retval #PSA_ERROR_BAD_STATE
2596 * The operation state is not valid (it must be an active aead encrypt
2597 * operation, with no nonce set), or the library has not been
2598 * previously initialized by psa_crypto_init().
2599 * It is implementation-dependent whether a failure to initialize
2600 * results in this error code.
2601 */
2602psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2603 uint8_t *nonce,
2604 size_t nonce_size,
2605 size_t *nonce_length);
2606
2607/** Set the nonce for an authenticated encryption or decryption operation.
2608 *
2609 * This function sets the nonce for the authenticated
2610 * encryption or decryption operation.
2611 *
2612 * The application must call psa_aead_encrypt_setup() or
2613 * psa_aead_decrypt_setup() before calling this function.
2614 *
2615 * If this function returns an error status, the operation enters an error
2616 * state and must be aborted by calling psa_aead_abort().
2617 *
2618 * \note When encrypting, applications should use psa_aead_generate_nonce()
2619 * instead of this function, unless implementing a protocol that requires
2620 * a non-random IV.
2621 *
2622 * \param[in,out] operation Active AEAD operation.
2623 * \param[in] nonce Buffer containing the nonce to use.
2624 * \param nonce_length Size of the nonce in bytes.
2625 *
2626 * \retval #PSA_SUCCESS
2627 * Success.
2628 * \retval #PSA_ERROR_INVALID_ARGUMENT
2629 * The size of \p nonce is not acceptable for the chosen algorithm.
2630 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2631 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2632 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2633 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2634 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2635 * \retval #PSA_ERROR_BAD_STATE
2636 * The operation state is not valid (it must be active, with no nonce
2637 * set), or the library has not been previously initialized
2638 * by psa_crypto_init().
2639 * It is implementation-dependent whether a failure to initialize
2640 * results in this error code.
2641 */
2642psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2643 const uint8_t *nonce,
2644 size_t nonce_length);
2645
2646/** Declare the lengths of the message and additional data for AEAD.
2647 *
2648 * The application must call this function before calling
2649 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2650 * the operation requires it. If the algorithm does not require it,
2651 * calling this function is optional, but if this function is called
2652 * then the implementation must enforce the lengths.
2653 *
2654 * You may call this function before or after setting the nonce with
2655 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2656 *
2657 * - For #PSA_ALG_CCM, calling this function is required.
2658 * - For the other AEAD algorithms defined in this specification, calling
2659 * this function is not required.
2660 * - For vendor-defined algorithm, refer to the vendor documentation.
2661 *
2662 * If this function returns an error status, the operation enters an error
2663 * state and must be aborted by calling psa_aead_abort().
2664 *
2665 * \param[in,out] operation Active AEAD operation.
2666 * \param ad_length Size of the non-encrypted additional
2667 * authenticated data in bytes.
2668 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2669 *
2670 * \retval #PSA_SUCCESS
2671 * Success.
2672 * \retval #PSA_ERROR_INVALID_ARGUMENT
2673 * At least one of the lengths is not acceptable for the chosen
2674 * algorithm.
2675 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2676 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2677 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2678 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2679 * \retval #PSA_ERROR_BAD_STATE
2680 * The operation state is not valid (it must be active, and
2681 * psa_aead_update_ad() and psa_aead_update() must not have been
2682 * called yet), or the library has not been previously initialized
2683 * by psa_crypto_init().
2684 * It is implementation-dependent whether a failure to initialize
2685 * results in this error code.
2686 */
2687psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2688 size_t ad_length,
2689 size_t plaintext_length);
2690
2691/** Pass additional data to an active AEAD operation.
2692 *
2693 * Additional data is authenticated, but not encrypted.
2694 *
2695 * You may call this function multiple times to pass successive fragments
2696 * of the additional data. You may not call this function after passing
2697 * data to encrypt or decrypt with psa_aead_update().
2698 *
2699 * Before calling this function, you must:
2700 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2701 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2702 *
2703 * If this function returns an error status, the operation enters an error
2704 * state and must be aborted by calling psa_aead_abort().
2705 *
2706 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2707 * there is no guarantee that the input is valid. Therefore, until
2708 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2709 * treat the input as untrusted and prepare to undo any action that
2710 * depends on the input if psa_aead_verify() returns an error status.
2711 *
2712 * \param[in,out] operation Active AEAD operation.
2713 * \param[in] input Buffer containing the fragment of
2714 * additional data.
2715 * \param input_length Size of the \p input buffer in bytes.
2716 *
2717 * \retval #PSA_SUCCESS
2718 * Success.
2719 * \retval #PSA_ERROR_INVALID_ARGUMENT
2720 * The total input length overflows the additional data length that
2721 * was previously specified with psa_aead_set_lengths().
2722 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2723 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2724 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2725 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2726 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2727 * \retval #PSA_ERROR_BAD_STATE
2728 * The operation state is not valid (it must be active, have a nonce
2729 * set, have lengths set if required by the algorithm, and
2730 * psa_aead_update() must not have been called yet), or the library
2731 * has not been previously initialized by psa_crypto_init().
2732 * It is implementation-dependent whether a failure to initialize
2733 * results in this error code.
2734 */
2735psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2736 const uint8_t *input,
2737 size_t input_length);
2738
2739/** Encrypt or decrypt a message fragment in an active AEAD operation.
2740 *
2741 * Before calling this function, you must:
2742 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2743 * The choice of setup function determines whether this function
2744 * encrypts or decrypts its input.
2745 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2746 * 3. Call psa_aead_update_ad() to pass all the additional data.
2747 *
2748 * If this function returns an error status, the operation enters an error
2749 * state and must be aborted by calling psa_aead_abort().
2750 *
2751 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2752 * there is no guarantee that the input is valid. Therefore, until
2753 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2754 * - Do not use the output in any way other than storing it in a
2755 * confidential location. If you take any action that depends
2756 * on the tentative decrypted data, this action will need to be
2757 * undone if the input turns out not to be valid. Furthermore,
2758 * if an adversary can observe that this action took place
2759 * (for example through timing), they may be able to use this
2760 * fact as an oracle to decrypt any message encrypted with the
2761 * same key.
2762 * - In particular, do not copy the output anywhere but to a
2763 * memory or storage space that you have exclusive access to.
2764 *
2765 * This function does not require the input to be aligned to any
2766 * particular block boundary. If the implementation can only process
2767 * a whole block at a time, it must consume all the input provided, but
2768 * it may delay the end of the corresponding output until a subsequent
2769 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2770 * provides sufficient input. The amount of data that can be delayed
2771 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2772 *
2773 * \param[in,out] operation Active AEAD operation.
2774 * \param[in] input Buffer containing the message fragment to
2775 * encrypt or decrypt.
2776 * \param input_length Size of the \p input buffer in bytes.
2777 * \param[out] output Buffer where the output is to be written.
2778 * \param output_size Size of the \p output buffer in bytes.
2779 * This must be appropriate for the selected
2780 * algorithm and key:
2781 * - A sufficient output size is
2782 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2783 * \c alg, \p input_length) where
2784 * \c key_type is the type of key and \c alg is
2785 * the algorithm that were used to set up the
2786 * operation.
2787 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2788 * input_length) evaluates to the maximum
2789 * output size of any supported AEAD
2790 * algorithm.
2791 * \param[out] output_length On success, the number of bytes
2792 * that make up the returned output.
2793 *
2794 * \retval #PSA_SUCCESS
2795 * Success.
2796 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2797 * The size of the \p output buffer is too small.
2798 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2799 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2800 * determine the required buffer size.
2801 * \retval #PSA_ERROR_INVALID_ARGUMENT
2802 * The total length of input to psa_aead_update_ad() so far is
2803 * less than the additional data length that was previously
2804 * specified with psa_aead_set_lengths(), or
2805 * the total input length overflows the plaintext length that
2806 * was previously specified with psa_aead_set_lengths().
2807 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2808 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2809 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2810 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2811 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2812 * \retval #PSA_ERROR_BAD_STATE
2813 * The operation state is not valid (it must be active, have a nonce
2814 * set, and have lengths set if required by the algorithm), or the
2815 * library has not been previously initialized by psa_crypto_init().
2816 * It is implementation-dependent whether a failure to initialize
2817 * results in this error code.
2818 */
2819psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2820 const uint8_t *input,
2821 size_t input_length,
2822 uint8_t *output,
2823 size_t output_size,
2824 size_t *output_length);
2825
2826/** Finish encrypting a message in an AEAD operation.
2827 *
2828 * The operation must have been set up with psa_aead_encrypt_setup().
2829 *
2830 * This function finishes the authentication of the additional data
2831 * formed by concatenating the inputs passed to preceding calls to
2832 * psa_aead_update_ad() with the plaintext formed by concatenating the
2833 * inputs passed to preceding calls to psa_aead_update().
2834 *
2835 * This function has two output buffers:
2836 * - \p ciphertext contains trailing ciphertext that was buffered from
2837 * preceding calls to psa_aead_update().
2838 * - \p tag contains the authentication tag.
2839 *
2840 * When this function returns successfully, the operation becomes inactive.
2841 * If this function returns an error status, the operation enters an error
2842 * state and must be aborted by calling psa_aead_abort().
2843 *
2844 * \param[in,out] operation Active AEAD operation.
2845 * \param[out] ciphertext Buffer where the last part of the ciphertext
2846 * is to be written.
2847 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2848 * This must be appropriate for the selected
2849 * algorithm and key:
2850 * - A sufficient output size is
2851 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2852 * \c alg) where \c key_type is the type of key
2853 * and \c alg is the algorithm that were used to
2854 * set up the operation.
2855 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2856 * the maximum output size of any supported AEAD
2857 * algorithm.
2858 * \param[out] ciphertext_length On success, the number of bytes of
2859 * returned ciphertext.
2860 * \param[out] tag Buffer where the authentication tag is
2861 * to be written.
2862 * \param tag_size Size of the \p tag buffer in bytes.
2863 * This must be appropriate for the selected
2864 * algorithm and key:
2865 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2866 * key_type, \c key_bits, \c alg) where
2867 * \c key_type and \c key_bits are the type and
2868 * bit-size of the key, and \c alg is the
2869 * algorithm that were used in the call to
2870 * psa_aead_encrypt_setup().
2871 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2872 * maximum tag size of any supported AEAD
2873 * algorithm.
2874 * \param[out] tag_length On success, the number of bytes
2875 * that make up the returned tag.
2876 *
2877 * \retval #PSA_SUCCESS
2878 * Success.
2879 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2880 * The size of the \p ciphertext or \p tag buffer is too small.
2881 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2882 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2883 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2884 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2885 * determine the required \p tag buffer size.
2886 * \retval #PSA_ERROR_INVALID_ARGUMENT
2887 * The total length of input to psa_aead_update_ad() so far is
2888 * less than the additional data length that was previously
2889 * specified with psa_aead_set_lengths(), or
2890 * the total length of input to psa_aead_update() so far is
2891 * less than the plaintext length that was previously
2892 * specified with psa_aead_set_lengths().
2893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2895 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2896 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2897 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2898 * \retval #PSA_ERROR_BAD_STATE
2899 * The operation state is not valid (it must be an active encryption
2900 * operation with a nonce set), or the library has not been previously
2901 * initialized by psa_crypto_init().
2902 * It is implementation-dependent whether a failure to initialize
2903 * results in this error code.
2904 */
2905psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2906 uint8_t *ciphertext,
2907 size_t ciphertext_size,
2908 size_t *ciphertext_length,
2909 uint8_t *tag,
2910 size_t tag_size,
2911 size_t *tag_length);
2912
2913/** Finish authenticating and decrypting a message in an AEAD operation.
2914 *
2915 * The operation must have been set up with psa_aead_decrypt_setup().
2916 *
2917 * This function finishes the authenticated decryption of the message
2918 * components:
2919 *
2920 * - The additional data consisting of the concatenation of the inputs
2921 * passed to preceding calls to psa_aead_update_ad().
2922 * - The ciphertext consisting of the concatenation of the inputs passed to
2923 * preceding calls to psa_aead_update().
2924 * - The tag passed to this function call.
2925 *
2926 * If the authentication tag is correct, this function outputs any remaining
2927 * plaintext and reports success. If the authentication tag is not correct,
2928 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2929 *
2930 * When this function returns successfully, the operation becomes inactive.
2931 * If this function returns an error status, the operation enters an error
2932 * state and must be aborted by calling psa_aead_abort().
2933 *
2934 * \note Implementations shall make the best effort to ensure that the
2935 * comparison between the actual tag and the expected tag is performed
2936 * in constant time.
2937 *
2938 * \param[in,out] operation Active AEAD operation.
2939 * \param[out] plaintext Buffer where the last part of the plaintext
2940 * is to be written. This is the remaining data
2941 * from previous calls to psa_aead_update()
2942 * that could not be processed until the end
2943 * of the input.
2944 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2945 * This must be appropriate for the selected algorithm and key:
2946 * - A sufficient output size is
2947 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2948 * \c alg) where \c key_type is the type of key
2949 * and \c alg is the algorithm that were used to
2950 * set up the operation.
2951 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2952 * the maximum output size of any supported AEAD
2953 * algorithm.
2954 * \param[out] plaintext_length On success, the number of bytes of
2955 * returned plaintext.
2956 * \param[in] tag Buffer containing the authentication tag.
2957 * \param tag_length Size of the \p tag buffer in bytes.
2958 *
2959 * \retval #PSA_SUCCESS
2960 * Success.
2961 * \retval #PSA_ERROR_INVALID_SIGNATURE
2962 * The calculations were successful, but the authentication tag is
2963 * not correct.
2964 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2965 * The size of the \p plaintext buffer is too small.
2966 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2967 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2968 * required buffer size.
2969 * \retval #PSA_ERROR_INVALID_ARGUMENT
2970 * The total length of input to psa_aead_update_ad() so far is
2971 * less than the additional data length that was previously
2972 * specified with psa_aead_set_lengths(), or
2973 * the total length of input to psa_aead_update() so far is
2974 * less than the plaintext length that was previously
2975 * specified with psa_aead_set_lengths().
2976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2978 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2979 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2980 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2981 * \retval #PSA_ERROR_BAD_STATE
2982 * The operation state is not valid (it must be an active decryption
2983 * operation with a nonce set), or the library has not been previously
2984 * initialized by psa_crypto_init().
2985 * It is implementation-dependent whether a failure to initialize
2986 * results in this error code.
2987 */
2988psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2989 uint8_t *plaintext,
2990 size_t plaintext_size,
2991 size_t *plaintext_length,
2992 const uint8_t *tag,
2993 size_t tag_length);
2994
2995/** Abort an AEAD operation.
2996 *
2997 * Aborting an operation frees all associated resources except for the
2998 * \p operation structure itself. Once aborted, the operation object
2999 * can be reused for another operation by calling
3000 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
3001 *
3002 * You may call this function any time after the operation object has
3003 * been initialized as described in #psa_aead_operation_t.
3004 *
3005 * In particular, calling psa_aead_abort() after the operation has been
3006 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
3007 * psa_aead_verify() is safe and has no effect.
3008 *
3009 * \param[in,out] operation Initialized AEAD operation.
3010 *
3011 * \retval #PSA_SUCCESS \emptydescription
3012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3013 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3014 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3015 * \retval #PSA_ERROR_BAD_STATE
3016 * The library has not been previously initialized by psa_crypto_init().
3017 * It is implementation-dependent whether a failure to initialize
3018 * results in this error code.
3019 */
3020psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
3021
3022/**@}*/
3023
3024/** \defgroup asymmetric Asymmetric cryptography
3025 * @{
3026 */
3027
3028/**
3029 * \brief Sign a message with a private key. For hash-and-sign algorithms,
3030 * this includes the hashing step.
3031 *
3032 * \note To perform a multi-part hash-and-sign signature algorithm, first use
3033 * a multi-part hash operation and then pass the resulting hash to
3034 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
3035 * hash algorithm to use.
3036 *
3037 * \param[in] key Identifier of the key to use for the operation.
3038 * It must be an asymmetric key pair. The key must
3039 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
3040 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
3041 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
3042 * is true), that is compatible with the type of
3043 * \p key.
3044 * \param[in] input The input message to sign.
3045 * \param[in] input_length Size of the \p input buffer in bytes.
3046 * \param[out] signature Buffer where the signature is to be written.
3047 * \param[in] signature_size Size of the \p signature buffer in bytes. This
3048 * must be appropriate for the selected
3049 * algorithm and key:
3050 * - The required signature size is
3051 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3052 * where \c key_type and \c key_bits are the type and
3053 * bit-size respectively of key.
3054 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
3055 * maximum signature size of any supported
3056 * signature algorithm.
3057 * \param[out] signature_length On success, the number of bytes that make up
3058 * the returned signature value.
3059 *
3060 * \retval #PSA_SUCCESS \emptydescription
3061 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3062 * \retval #PSA_ERROR_NOT_PERMITTED
3063 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
3064 * or it does not permit the requested algorithm.
3065 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3066 * The size of the \p signature buffer is too small. You can
3067 * determine a sufficient buffer size by calling
3068 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3069 * where \c key_type and \c key_bits are the type and bit-size
3070 * respectively of \p key.
3071 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3072 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3073 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3074 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3075 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3076 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3077 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3078 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3079 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3080 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3081 * \retval #PSA_ERROR_BAD_STATE
3082 * The library has not been previously initialized by psa_crypto_init().
3083 * It is implementation-dependent whether a failure to initialize
3084 * results in this error code.
3085 */
3086psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3087 psa_algorithm_t alg,
3088 const uint8_t *input,
3089 size_t input_length,
3090 uint8_t *signature,
3091 size_t signature_size,
3092 size_t *signature_length);
3093
3094/** \brief Verify the signature of a message with a public key, using
3095 * a hash-and-sign verification algorithm.
3096 *
3097 * \note To perform a multi-part hash-and-sign signature verification
3098 * algorithm, first use a multi-part hash operation to hash the message
3099 * and then pass the resulting hash to psa_verify_hash().
3100 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
3101 * to use.
3102 *
3103 * \param[in] key Identifier of the key to use for the operation.
3104 * It must be a public key or an asymmetric key
3105 * pair. The key must allow the usage
3106 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
3107 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
3108 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
3109 * is true), that is compatible with the type of
3110 * \p key.
3111 * \param[in] input The message whose signature is to be verified.
3112 * \param[in] input_length Size of the \p input buffer in bytes.
3113 * \param[in] signature Buffer containing the signature to verify.
3114 * \param[in] signature_length Size of the \p signature buffer in bytes.
3115 *
3116 * \retval #PSA_SUCCESS \emptydescription
3117 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3118 * \retval #PSA_ERROR_NOT_PERMITTED
3119 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
3120 * or it does not permit the requested algorithm.
3121 * \retval #PSA_ERROR_INVALID_SIGNATURE
3122 * The calculation was performed successfully, but the passed signature
3123 * is not a valid signature.
3124 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3125 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3128 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3129 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3130 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3131 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3132 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3133 * \retval #PSA_ERROR_BAD_STATE
3134 * The library has not been previously initialized by psa_crypto_init().
3135 * It is implementation-dependent whether a failure to initialize
3136 * results in this error code.
3137 */
3138psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3139 psa_algorithm_t alg,
3140 const uint8_t *input,
3141 size_t input_length,
3142 const uint8_t *signature,
3143 size_t signature_length);
3144
3145/**
3146 * \brief Sign a hash or short message with a private key.
3147 *
3148 * Note that to perform a hash-and-sign signature algorithm, you must
3149 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
3150 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3151 * Then pass the resulting hash as the \p hash
3152 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3153 * to determine the hash algorithm to use.
3154 *
3155 * \param key Identifier of the key to use for the operation.
3156 * It must be an asymmetric key pair. The key must
3157 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
3158 * \param alg A signature algorithm (PSA_ALG_XXX
3159 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3160 * is true), that is compatible with
3161 * the type of \p key.
3162 * \param[in] hash The hash or message to sign.
3163 * \param hash_length Size of the \p hash buffer in bytes.
3164 * \param[out] signature Buffer where the signature is to be written.
3165 * \param signature_size Size of the \p signature buffer in bytes.
3166 * \param[out] signature_length On success, the number of bytes
3167 * that make up the returned signature value.
3168 *
3169 * \retval #PSA_SUCCESS \emptydescription
3170 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3171 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3172 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3173 * The size of the \p signature buffer is too small. You can
3174 * determine a sufficient buffer size by calling
3175 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3176 * where \c key_type and \c key_bits are the type and bit-size
3177 * respectively of \p key.
3178 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3179 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3180 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3181 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3182 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3183 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3184 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3185 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3186 * \retval #PSA_ERROR_BAD_STATE
3187 * The library has not been previously initialized by psa_crypto_init().
3188 * It is implementation-dependent whether a failure to initialize
3189 * results in this error code.
3190 */
3191psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3192 psa_algorithm_t alg,
3193 const uint8_t *hash,
3194 size_t hash_length,
3195 uint8_t *signature,
3196 size_t signature_size,
3197 size_t *signature_length);
3198
3199/**
3200 * \brief Verify the signature of a hash or short message using a public key.
3201 *
3202 * Note that to perform a hash-and-sign signature algorithm, you must
3203 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
3204 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3205 * Then pass the resulting hash as the \p hash
3206 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3207 * to determine the hash algorithm to use.
3208 *
3209 * \param key Identifier of the key to use for the operation. It
3210 * must be a public key or an asymmetric key pair. The
3211 * key must allow the usage
3212 * #PSA_KEY_USAGE_VERIFY_HASH.
3213 * \param alg A signature algorithm (PSA_ALG_XXX
3214 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3215 * is true), that is compatible with
3216 * the type of \p key.
3217 * \param[in] hash The hash or message whose signature is to be
3218 * verified.
3219 * \param hash_length Size of the \p hash buffer in bytes.
3220 * \param[in] signature Buffer containing the signature to verify.
3221 * \param signature_length Size of the \p signature buffer in bytes.
3222 *
3223 * \retval #PSA_SUCCESS
3224 * The signature is valid.
3225 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3226 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3227 * \retval #PSA_ERROR_INVALID_SIGNATURE
3228 * The calculation was performed successfully, but the passed
3229 * signature is not a valid signature.
3230 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3231 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3232 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3234 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3235 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3236 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3237 * \retval #PSA_ERROR_BAD_STATE
3238 * The library has not been previously initialized by psa_crypto_init().
3239 * It is implementation-dependent whether a failure to initialize
3240 * results in this error code.
3241 */
3242psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3243 psa_algorithm_t alg,
3244 const uint8_t *hash,
3245 size_t hash_length,
3246 const uint8_t *signature,
3247 size_t signature_length);
3248
3249/**
3250 * \brief Encrypt a short message with a public key.
3251 *
3252 * \param key Identifier of the key to use for the operation.
3253 * It must be a public key or an asymmetric key
3254 * pair. It must allow the usage
3255 * #PSA_KEY_USAGE_ENCRYPT.
3256 * \param alg An asymmetric encryption algorithm that is
3257 * compatible with the type of \p key.
3258 * \param[in] input The message to encrypt.
3259 * \param input_length Size of the \p input buffer in bytes.
3260 * \param[in] salt A salt or label, if supported by the
3261 * encryption algorithm.
3262 * If the algorithm does not support a
3263 * salt, pass \c NULL.
3264 * If the algorithm supports an optional
3265 * salt and you do not want to pass a salt,
3266 * pass \c NULL.
3267 *
3268 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3269 * supported.
3270 * \param salt_length Size of the \p salt buffer in bytes.
3271 * If \p salt is \c NULL, pass 0.
3272 * \param[out] output Buffer where the encrypted message is to
3273 * be written.
3274 * \param output_size Size of the \p output buffer in bytes.
3275 * \param[out] output_length On success, the number of bytes
3276 * that make up the returned output.
3277 *
3278 * \retval #PSA_SUCCESS \emptydescription
3279 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3280 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3281 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3282 * The size of the \p output buffer is too small. You can
3283 * determine a sufficient buffer size by calling
3284 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3285 * where \c key_type and \c key_bits are the type and bit-size
3286 * respectively of \p key.
3287 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3288 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3291 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3292 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3293 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3294 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3295 * \retval #PSA_ERROR_BAD_STATE
3296 * The library has not been previously initialized by psa_crypto_init().
3297 * It is implementation-dependent whether a failure to initialize
3298 * results in this error code.
3299 */
3300psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3301 psa_algorithm_t alg,
3302 const uint8_t *input,
3303 size_t input_length,
3304 const uint8_t *salt,
3305 size_t salt_length,
3306 uint8_t *output,
3307 size_t output_size,
3308 size_t *output_length);
3309
3310/**
3311 * \brief Decrypt a short message with a private key.
3312 *
3313 * \param key Identifier of the key to use for the operation.
3314 * It must be an asymmetric key pair. It must
3315 * allow the usage #PSA_KEY_USAGE_DECRYPT.
3316 * \param alg An asymmetric encryption algorithm that is
3317 * compatible with the type of \p key.
3318 * \param[in] input The message to decrypt.
3319 * \param input_length Size of the \p input buffer in bytes.
3320 * \param[in] salt A salt or label, if supported by the
3321 * encryption algorithm.
3322 * If the algorithm does not support a
3323 * salt, pass \c NULL.
3324 * If the algorithm supports an optional
3325 * salt and you do not want to pass a salt,
3326 * pass \c NULL.
3327 *
3328 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3329 * supported.
3330 * \param salt_length Size of the \p salt buffer in bytes.
3331 * If \p salt is \c NULL, pass 0.
3332 * \param[out] output Buffer where the decrypted message is to
3333 * be written.
3334 * \param output_size Size of the \c output buffer in bytes.
3335 * \param[out] output_length On success, the number of bytes
3336 * that make up the returned output.
3337 *
3338 * \retval #PSA_SUCCESS \emptydescription
3339 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3340 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3341 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3342 * The size of the \p output buffer is too small. You can
3343 * determine a sufficient buffer size by calling
3344 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3345 * where \c key_type and \c key_bits are the type and bit-size
3346 * respectively of \p key.
3347 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3348 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3351 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3352 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3353 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3354 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3355 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
3356 * \retval #PSA_ERROR_BAD_STATE
3357 * The library has not been previously initialized by psa_crypto_init().
3358 * It is implementation-dependent whether a failure to initialize
3359 * results in this error code.
3360 */
3361psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3362 psa_algorithm_t alg,
3363 const uint8_t *input,
3364 size_t input_length,
3365 const uint8_t *salt,
3366 size_t salt_length,
3367 uint8_t *output,
3368 size_t output_size,
3369 size_t *output_length);
3370
3371/**@}*/
3372
3373/** \defgroup key_derivation Key derivation and pseudorandom generation
3374 * @{
3375 */
3376
3377/** The type of the state data structure for key derivation operations.
3378 *
3379 * Before calling any function on a key derivation operation object, the
3380 * application must initialize it by any of the following means:
3381 * - Set the structure to all-bits-zero, for example:
3382 * \code
3383 * psa_key_derivation_operation_t operation;
3384 * memset(&operation, 0, sizeof(operation));
3385 * \endcode
3386 * - Initialize the structure to logical zero values, for example:
3387 * \code
3388 * psa_key_derivation_operation_t operation = {0};
3389 * \endcode
3390 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
3391 * for example:
3392 * \code
3393 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3394 * \endcode
3395 * - Assign the result of the function psa_key_derivation_operation_init()
3396 * to the structure, for example:
3397 * \code
3398 * psa_key_derivation_operation_t operation;
3399 * operation = psa_key_derivation_operation_init();
3400 * \endcode
3401 *
3402 * This is an implementation-defined \c struct. Applications should not
3403 * make any assumptions about the content of this structure.
3404 * Implementation details can change in future versions without notice.
3405 */
3406typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
3407
3408/** \def PSA_KEY_DERIVATION_OPERATION_INIT
3409 *
3410 * This macro returns a suitable initializer for a key derivation operation
3411 * object of type #psa_key_derivation_operation_t.
3412 */
3413
3414/** Return an initial value for a key derivation operation object.
3415 */
3416static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
3417
3418/** Set up a key derivation operation.
3419 *
3420 * A key derivation algorithm takes some inputs and uses them to generate
3421 * a byte stream in a deterministic way.
3422 * This byte stream can be used to produce keys and other
3423 * cryptographic material.
3424 *
3425 * To derive a key:
3426 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3427 * -# Call psa_key_derivation_setup() to select the algorithm.
3428 * -# Provide the inputs for the key derivation by calling
3429 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3430 * as appropriate. Which inputs are needed, in what order, and whether
3431 * they may be keys and if so of what type depends on the algorithm.
3432 * -# Optionally set the operation's maximum capacity with
3433 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3434 * of or after providing inputs. For some algorithms, this step is mandatory
3435 * because the output depends on the maximum capacity.
3436 * -# To derive a key, call psa_key_derivation_output_key() or
3437 * psa_key_derivation_output_key_custom().
3438 * To derive a byte string for a different purpose, call
3439 * psa_key_derivation_output_bytes().
3440 * Successive calls to these functions use successive output bytes
3441 * calculated by the key derivation algorithm.
3442 * -# Clean up the key derivation operation object with
3443 * psa_key_derivation_abort().
3444 *
3445 * If this function returns an error, the key derivation operation object is
3446 * not changed.
3447 *
3448 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3449 * the operation will need to be reset by a call to psa_key_derivation_abort().
3450 *
3451 * Implementations must reject an attempt to derive a key of size 0.
3452 *
3453 * \param[in,out] operation The key derivation operation object
3454 * to set up. It must
3455 * have been initialized but not set up yet.
3456 * \param alg The key derivation algorithm to compute
3457 * (\c PSA_ALG_XXX value such that
3458 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3459 *
3460 * \retval #PSA_SUCCESS
3461 * Success.
3462 * \retval #PSA_ERROR_INVALID_ARGUMENT
3463 * \c alg is not a key derivation algorithm.
3464 * \retval #PSA_ERROR_NOT_SUPPORTED
3465 * \c alg is not supported or is not a key derivation algorithm.
3466 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3467 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3468 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3469 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3470 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3471 * \retval #PSA_ERROR_BAD_STATE
3472 * The operation state is not valid (it must be inactive), or
3473 * the library has not been previously initialized by psa_crypto_init().
3474 * It is implementation-dependent whether a failure to initialize
3475 * results in this error code.
3476 */
3477psa_status_t psa_key_derivation_setup(
3478 psa_key_derivation_operation_t *operation,
3479 psa_algorithm_t alg);
3480
3481/** Retrieve the current capacity of a key derivation operation.
3482 *
3483 * The capacity of a key derivation is the maximum number of bytes that it can
3484 * return. When you get *N* bytes of output from a key derivation operation,
3485 * this reduces its capacity by *N*.
3486 *
3487 * \param[in] operation The operation to query.
3488 * \param[out] capacity On success, the capacity of the operation.
3489 *
3490 * \retval #PSA_SUCCESS \emptydescription
3491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3492 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3493 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3494 * \retval #PSA_ERROR_BAD_STATE
3495 * The operation state is not valid (it must be active), or
3496 * the library has not been previously initialized by psa_crypto_init().
3497 * It is implementation-dependent whether a failure to initialize
3498 * results in this error code.
3499 */
3500psa_status_t psa_key_derivation_get_capacity(
3501 const psa_key_derivation_operation_t *operation,
3502 size_t *capacity);
3503
3504/** Set the maximum capacity of a key derivation operation.
3505 *
3506 * The capacity of a key derivation operation is the maximum number of bytes
3507 * that the key derivation operation can return from this point onwards.
3508 *
3509 * \param[in,out] operation The key derivation operation object to modify.
3510 * \param capacity The new capacity of the operation.
3511 * It must be less or equal to the operation's
3512 * current capacity.
3513 *
3514 * \retval #PSA_SUCCESS \emptydescription
3515 * \retval #PSA_ERROR_INVALID_ARGUMENT
3516 * \p capacity is larger than the operation's current capacity.
3517 * In this case, the operation object remains valid and its capacity
3518 * remains unchanged.
3519 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3520 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3521 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3522 * \retval #PSA_ERROR_BAD_STATE
3523 * The operation state is not valid (it must be active), or the
3524 * 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_set_capacity(
3529 psa_key_derivation_operation_t *operation,
3530 size_t capacity);
3531
3532/** Use the maximum possible capacity for a key derivation operation.
3533 *
3534 * Use this value as the capacity argument when setting up a key derivation
3535 * to indicate that the operation should have the maximum possible capacity.
3536 * The value of the maximum possible capacity depends on the key derivation
3537 * algorithm.
3538 */
3539#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
3540
3541/** Provide an input for key derivation or key agreement.
3542 *
3543 * Which inputs are required and in what order depends on the algorithm.
3544 * Refer to the documentation of each key derivation or key agreement
3545 * algorithm for information.
3546 *
3547 * This function passes direct inputs, which is usually correct for
3548 * non-secret inputs. To pass a secret input, which should be in a key
3549 * object, call psa_key_derivation_input_key() instead of this function.
3550 * Refer to the documentation of individual step types
3551 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3552 * for more information.
3553 *
3554 * If this function returns an error status, the operation enters an error
3555 * state and must be aborted by calling psa_key_derivation_abort().
3556 *
3557 * \param[in,out] operation The key derivation operation object to use.
3558 * It must have been set up with
3559 * psa_key_derivation_setup() and must not
3560 * have produced any output yet.
3561 * \param step Which step the input data is for.
3562 * \param[in] data Input data to use.
3563 * \param data_length Size of the \p data buffer in bytes.
3564 *
3565 * \retval #PSA_SUCCESS
3566 * Success.
3567 * \retval #PSA_ERROR_INVALID_ARGUMENT
3568 * \c step is not compatible with the operation's algorithm, or
3569 * \c step does not allow direct inputs.
3570 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3571 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3572 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3573 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3574 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3575 * \retval #PSA_ERROR_BAD_STATE
3576 * The operation state is not valid for this input \p step, or
3577 * the library has not been previously initialized by psa_crypto_init().
3578 * It is implementation-dependent whether a failure to initialize
3579 * results in this error code.
3580 */
3581psa_status_t psa_key_derivation_input_bytes(
3582 psa_key_derivation_operation_t *operation,
3583 psa_key_derivation_step_t step,
3584 const uint8_t *data,
3585 size_t data_length);
3586
3587/** Provide a numeric input for key derivation or key agreement.
3588 *
3589 * Which inputs are required and in what order depends on the algorithm.
3590 * However, when an algorithm requires a particular order, numeric inputs
3591 * usually come first as they tend to be configuration parameters.
3592 * Refer to the documentation of each key derivation or key agreement
3593 * algorithm for information.
3594 *
3595 * This function is used for inputs which are fixed-size non-negative
3596 * integers.
3597 *
3598 * If this function returns an error status, the operation enters an error
3599 * state and must be aborted by calling psa_key_derivation_abort().
3600 *
3601 * \param[in,out] operation The key derivation operation object to use.
3602 * It must have been set up with
3603 * psa_key_derivation_setup() and must not
3604 * have produced any output yet.
3605 * \param step Which step the input data is for.
3606 * \param[in] value The value of the numeric input.
3607 *
3608 * \retval #PSA_SUCCESS
3609 * Success.
3610 * \retval #PSA_ERROR_INVALID_ARGUMENT
3611 * \c step is not compatible with the operation's algorithm, or
3612 * \c step does not allow numeric inputs.
3613 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3615 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3616 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3617 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3618 * \retval #PSA_ERROR_BAD_STATE
3619 * The operation state is not valid for this input \p step, or
3620 * the library has not been previously initialized by psa_crypto_init().
3621 * It is implementation-dependent whether a failure to initialize
3622 * results in this error code.
3623 */
3624psa_status_t psa_key_derivation_input_integer(
3625 psa_key_derivation_operation_t *operation,
3626 psa_key_derivation_step_t step,
3627 uint64_t value);
3628
3629/** Provide an input for key derivation in the form of a key.
3630 *
3631 * Which inputs are required and in what order depends on the algorithm.
3632 * Refer to the documentation of each key derivation or key agreement
3633 * algorithm for information.
3634 *
3635 * This function obtains input from a key object, which is usually correct for
3636 * secret inputs or for non-secret personalization strings kept in the key
3637 * store. To pass a non-secret parameter which is not in the key store,
3638 * call psa_key_derivation_input_bytes() instead of this function.
3639 * Refer to the documentation of individual step types
3640 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3641 * for more information.
3642 *
3643 * If this function returns an error status, the operation enters an error
3644 * state and must be aborted by calling psa_key_derivation_abort().
3645 *
3646 * \param[in,out] operation The key derivation operation object to use.
3647 * It must have been set up with
3648 * psa_key_derivation_setup() and must not
3649 * have produced any output yet.
3650 * \param step Which step the input data is for.
3651 * \param key Identifier of the key. It must have an
3652 * appropriate type for step and must allow the
3653 * usage #PSA_KEY_USAGE_DERIVE or
3654 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3655 * and the algorithm used by the operation.
3656 *
3657 * \note Once all inputs steps are completed, the operations will allow:
3658 * - psa_key_derivation_output_bytes() if each input was either a direct input
3659 * or a key with #PSA_KEY_USAGE_DERIVE set;
3660 * - psa_key_derivation_output_key() or psa_key_derivation_output_key_custom()
3661 * if the input for step
3662 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
3663 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3664 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
3665 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3666 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
3667 * - psa_key_derivation_verify_key() under the same conditions as
3668 * psa_key_derivation_verify_bytes().
3669 *
3670 * \retval #PSA_SUCCESS
3671 * Success.
3672 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3673 * \retval #PSA_ERROR_NOT_PERMITTED
3674 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3675 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3676 * algorithm.
3677 * \retval #PSA_ERROR_INVALID_ARGUMENT
3678 * \c step is not compatible with the operation's algorithm, or
3679 * \c step does not allow key inputs of the given type
3680 * or does not allow key inputs at all.
3681 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3682 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3683 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3684 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3685 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3686 * \retval #PSA_ERROR_BAD_STATE
3687 * The operation state is not valid for this input \p step, or
3688 * the library has not been previously initialized by psa_crypto_init().
3689 * It is implementation-dependent whether a failure to initialize
3690 * results in this error code.
3691 */
3692psa_status_t psa_key_derivation_input_key(
3693 psa_key_derivation_operation_t *operation,
3694 psa_key_derivation_step_t step,
3695 mbedtls_svc_key_id_t key);
3696
3697/** Perform a key agreement and use the shared secret as input to a key
3698 * derivation.
3699 *
3700 * A key agreement algorithm takes two inputs: a private key \p private_key
3701 * a public key \p peer_key.
3702 * The result of this function is passed as input to a key derivation.
3703 * The output of this key derivation can be extracted by reading from the
3704 * resulting operation to produce keys and other cryptographic material.
3705 *
3706 * If this function returns an error status, the operation enters an error
3707 * state and must be aborted by calling psa_key_derivation_abort().
3708 *
3709 * \param[in,out] operation The key derivation operation object to use.
3710 * It must have been set up with
3711 * psa_key_derivation_setup() with a
3712 * key agreement and derivation algorithm
3713 * \c alg (\c PSA_ALG_XXX value such that
3714 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3715 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3716 * is false).
3717 * The operation must be ready for an
3718 * input of the type given by \p step.
3719 * \param step Which step the input data is for.
3720 * \param private_key Identifier of the private key to use. It must
3721 * allow the usage #PSA_KEY_USAGE_DERIVE.
3722 * \param[in] peer_key Public key of the peer. The peer key must be in the
3723 * same format that psa_import_key() accepts for the
3724 * public key type corresponding to the type of
3725 * private_key. That is, this function performs the
3726 * equivalent of
3727 * #psa_import_key(...,
3728 * `peer_key`, `peer_key_length`) where
3729 * with key attributes indicating the public key
3730 * type corresponding to the type of `private_key`.
3731 * For example, for EC keys, this means that peer_key
3732 * is interpreted as a point on the curve that the
3733 * private key is on. The standard formats for public
3734 * keys are documented in the documentation of
3735 * psa_export_public_key().
3736 * \param peer_key_length Size of \p peer_key in bytes.
3737 *
3738 * \retval #PSA_SUCCESS
3739 * Success.
3740 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3741 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
3742 * \retval #PSA_ERROR_INVALID_ARGUMENT
3743 * \c private_key is not compatible with \c alg,
3744 * or \p peer_key is not valid for \c alg or not compatible with
3745 * \c private_key, or \c step does not allow an input resulting
3746 * from a key agreement.
3747 * \retval #PSA_ERROR_NOT_SUPPORTED
3748 * \c alg is not supported or is not a key derivation algorithm.
3749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3751 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3752 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3753 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3754 * \retval #PSA_ERROR_BAD_STATE
3755 * The operation state is not valid for this key agreement \p step,
3756 * or the library has not been previously initialized by psa_crypto_init().
3757 * It is implementation-dependent whether a failure to initialize
3758 * results in this error code.
3759 */
3760psa_status_t psa_key_derivation_key_agreement(
3761 psa_key_derivation_operation_t *operation,
3762 psa_key_derivation_step_t step,
3763 mbedtls_svc_key_id_t private_key,
3764 const uint8_t *peer_key,
3765 size_t peer_key_length);
3766
3767/** Read some data from a key derivation operation.
3768 *
3769 * This function calculates output bytes from a key derivation algorithm and
3770 * return those bytes.
3771 * If you view the key derivation's output as a stream of bytes, this
3772 * function destructively reads the requested number of bytes from the
3773 * stream.
3774 * The operation's capacity decreases by the number of bytes read.
3775 *
3776 * If this function returns an error status other than
3777 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3778 * state and must be aborted by calling psa_key_derivation_abort().
3779 *
3780 * \param[in,out] operation The key derivation operation object to read from.
3781 * \param[out] output Buffer where the output will be written.
3782 * \param output_length Number of bytes to output.
3783 *
3784 * \retval #PSA_SUCCESS \emptydescription
3785 * \retval #PSA_ERROR_NOT_PERMITTED
3786 * One of the inputs was a key whose policy didn't allow
3787 * #PSA_KEY_USAGE_DERIVE.
3788 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3789 * The operation's capacity was less than
3790 * \p output_length bytes. Note that in this case,
3791 * no output is written to the output buffer.
3792 * The operation's capacity is set to 0, thus
3793 * subsequent calls to this function will not
3794 * succeed, even with a smaller output buffer.
3795 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3796 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3797 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3798 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3799 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3800 * \retval #PSA_ERROR_BAD_STATE
3801 * The operation state is not valid (it must be active and completed
3802 * all required input steps), or the library has not been previously
3803 * initialized by psa_crypto_init().
3804 * It is implementation-dependent whether a failure to initialize
3805 * results in this error code.
3806 */
3807psa_status_t psa_key_derivation_output_bytes(
3808 psa_key_derivation_operation_t *operation,
3809 uint8_t *output,
3810 size_t output_length);
3811
3812/** Derive a key from an ongoing key derivation operation.
3813 *
3814 * This function calculates output bytes from a key derivation algorithm
3815 * and uses those bytes to generate a key deterministically.
3816 * The key's location, usage policy, type and size are taken from
3817 * \p attributes.
3818 *
3819 * If you view the key derivation's output as a stream of bytes, this
3820 * function destructively reads as many bytes as required from the
3821 * stream.
3822 * The operation's capacity decreases by the number of bytes read.
3823 *
3824 * If this function returns an error status other than
3825 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3826 * state and must be aborted by calling psa_key_derivation_abort().
3827 *
3828 * How much output is produced and consumed from the operation, and how
3829 * the key is derived, depends on the key type and on the key size
3830 * (denoted \c bits below):
3831 *
3832 * - For key types for which the key is an arbitrary sequence of bytes
3833 * of a given size, this function is functionally equivalent to
3834 * calling #psa_key_derivation_output_bytes
3835 * and passing the resulting output to #psa_import_key.
3836 * However, this function has a security benefit:
3837 * if the implementation provides an isolation boundary then
3838 * the key material is not exposed outside the isolation boundary.
3839 * As a consequence, for these key types, this function always consumes
3840 * exactly (\c bits / 8) bytes from the operation.
3841 * The following key types defined in this specification follow this scheme:
3842 *
3843 * - #PSA_KEY_TYPE_AES;
3844 * - #PSA_KEY_TYPE_ARIA;
3845 * - #PSA_KEY_TYPE_CAMELLIA;
3846 * - #PSA_KEY_TYPE_DERIVE;
3847 * - #PSA_KEY_TYPE_HMAC;
3848 * - #PSA_KEY_TYPE_PASSWORD_HASH.
3849 *
3850 * - For ECC keys on a Montgomery elliptic curve
3851 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3852 * Montgomery curve), this function always draws a byte string whose
3853 * length is determined by the curve, and sets the mandatory bits
3854 * accordingly. That is:
3855 *
3856 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
3857 * string and process it as specified in RFC 7748 &sect;5.
3858 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
3859 * string and process it as specified in RFC 7748 &sect;5.
3860 *
3861 * - For key types for which the key is represented by a single sequence of
3862 * \c bits bits with constraints as to which bit sequences are acceptable,
3863 * this function draws a byte string of length (\c bits / 8) bytes rounded
3864 * up to the nearest whole number of bytes. If the resulting byte string
3865 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3866 * This process is repeated until an acceptable byte string is drawn.
3867 * The byte string drawn from the operation is interpreted as specified
3868 * for the output produced by psa_export_key().
3869 * The following key types defined in this specification follow this scheme:
3870 *
3871 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3872 * where \c group designates any Diffie-Hellman group) and
3873 * ECC keys on a Weierstrass elliptic curve
3874 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3875 * Weierstrass curve).
3876 * For these key types, interpret the byte string as integer
3877 * in big-endian order. Discard it if it is not in the range
3878 * [0, *N* - 2] where *N* is the boundary of the private key domain
3879 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3880 * or the order of the curve's base point for ECC).
3881 * Add 1 to the resulting integer and use this as the private key *x*.
3882 * This method allows compliance to NIST standards, specifically
3883 * the methods titled "key-pair generation by testing candidates"
3884 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3885 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3886 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3887 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3888 *
3889 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3890 * the way in which the operation output is consumed is
3891 * implementation-defined.
3892 *
3893 * In all cases, the data that is read is discarded from the operation.
3894 * The operation's capacity is decreased by the number of bytes read.
3895 *
3896 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3897 * the input to that step must be provided with psa_key_derivation_input_key().
3898 * Future versions of this specification may include additional restrictions
3899 * on the derived key based on the attributes and strength of the secret key.
3900 *
3901 * \note This function is equivalent to calling
3902 * psa_key_derivation_output_key_custom()
3903 * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
3904 * and `custom_data_length == 0` (i.e. `custom_data` is empty).
3905 *
3906 * \param[in] attributes The attributes for the new key.
3907 * If the key type to be created is
3908 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3909 * the policy must be the same as in the current
3910 * operation.
3911 * \param[in,out] operation The key derivation operation object to read from.
3912 * \param[out] key On success, an identifier for the newly created
3913 * key. For persistent keys, this is the key
3914 * identifier defined in \p attributes.
3915 * \c 0 on failure.
3916 *
3917 * \retval #PSA_SUCCESS
3918 * Success.
3919 * If the key is persistent, the key material and the key's metadata
3920 * have been saved to persistent storage.
3921 * \retval #PSA_ERROR_ALREADY_EXISTS
3922 * This is an attempt to create a persistent key, and there is
3923 * already a persistent key with the given identifier.
3924 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3925 * There was not enough data to create the desired key.
3926 * Note that in this case, no output is written to the output buffer.
3927 * The operation's capacity is set to 0, thus subsequent calls to
3928 * this function will not succeed, even with a smaller output buffer.
3929 * \retval #PSA_ERROR_NOT_SUPPORTED
3930 * The key type or key size is not supported, either by the
3931 * implementation in general or in this particular location.
3932 * \retval #PSA_ERROR_INVALID_ARGUMENT
3933 * The provided key attributes are not valid for the operation.
3934 * \retval #PSA_ERROR_NOT_PERMITTED
3935 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3936 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3937 * key; or one of the inputs was a key whose policy didn't allow
3938 * #PSA_KEY_USAGE_DERIVE.
3939 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3940 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3941 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3942 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3943 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3944 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3945 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3946 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3947 * \retval #PSA_ERROR_BAD_STATE
3948 * The operation state is not valid (it must be active and completed
3949 * all required input steps), or the library has not been previously
3950 * initialized by psa_crypto_init().
3951 * It is implementation-dependent whether a failure to initialize
3952 * results in this error code.
3953 */
3954psa_status_t psa_key_derivation_output_key(
3955 const psa_key_attributes_t *attributes,
3956 psa_key_derivation_operation_t *operation,
3957 mbedtls_svc_key_id_t *key);
3958
3959/** Derive a key from an ongoing key derivation operation with custom
3960 * production parameters.
3961 *
3962 * See the description of psa_key_derivation_out_key() for the operation of
3963 * this function with the default production parameters.
3964 * Mbed TLS currently does not currently support any non-default production
3965 * parameters.
3966 *
3967 * \note This function is experimental and may change in future minor
3968 * versions of Mbed TLS.
3969 *
3970 * \param[in] attributes The attributes for the new key.
3971 * If the key type to be created is
3972 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3973 * the policy must be the same as in the current
3974 * operation.
3975 * \param[in,out] operation The key derivation operation object to read from.
3976 * \param[in] custom Customization parameters for the key generation.
3977 * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
3978 * with \p custom_data_length = 0,
3979 * this function is equivalent to
3980 * psa_key_derivation_output_key().
3981 * \param[in] custom_data Variable-length data associated with \c custom.
3982 * \param custom_data_length
3983 * Length of `custom_data` in bytes.
3984 * \param[out] key On success, an identifier for the newly created
3985 * key. For persistent keys, this is the key
3986 * identifier defined in \p attributes.
3987 * \c 0 on failure.
3988 *
3989 * \retval #PSA_SUCCESS
3990 * Success.
3991 * If the key is persistent, the key material and the key's metadata
3992 * have been saved to persistent storage.
3993 * \retval #PSA_ERROR_ALREADY_EXISTS
3994 * This is an attempt to create a persistent key, and there is
3995 * already a persistent key with the given identifier.
3996 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3997 * There was not enough data to create the desired key.
3998 * Note that in this case, no output is written to the output buffer.
3999 * The operation's capacity is set to 0, thus subsequent calls to
4000 * this function will not succeed, even with a smaller output buffer.
4001 * \retval #PSA_ERROR_NOT_SUPPORTED
4002 * The key type or key size is not supported, either by the
4003 * implementation in general or in this particular location.
4004 * \retval #PSA_ERROR_INVALID_ARGUMENT
4005 * The provided key attributes are not valid for the operation.
4006 * \retval #PSA_ERROR_NOT_PERMITTED
4007 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
4008 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
4009 * key; or one of the inputs was a key whose policy didn't allow
4010 * #PSA_KEY_USAGE_DERIVE.
4011 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4012 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4014 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4015 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4016 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4017 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4018 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4019 * \retval #PSA_ERROR_BAD_STATE
4020 * The operation state is not valid (it must be active and completed
4021 * all required input steps), or the library has not been previously
4022 * initialized by psa_crypto_init().
4023 * It is implementation-dependent whether a failure to initialize
4024 * results in this error code.
4025 */
4026psa_status_t psa_key_derivation_output_key_custom(
4027 const psa_key_attributes_t *attributes,
4028 psa_key_derivation_operation_t *operation,
4029 const psa_custom_key_parameters_t *custom,
4030 const uint8_t *custom_data,
4031 size_t custom_data_length,
4032 mbedtls_svc_key_id_t *key);
4033
4034/** Compare output data from a key derivation operation to an expected value.
4035 *
4036 * This function calculates output bytes from a key derivation algorithm and
4037 * compares those bytes to an expected value in constant time.
4038 * If you view the key derivation's output as a stream of bytes, this
4039 * function destructively reads the expected number of bytes from the
4040 * stream before comparing them.
4041 * The operation's capacity decreases by the number of bytes read.
4042 *
4043 * This is functionally equivalent to the following code:
4044 * \code
4045 * psa_key_derivation_output_bytes(operation, tmp, output_length);
4046 * if (memcmp(output, tmp, output_length) != 0)
4047 * return PSA_ERROR_INVALID_SIGNATURE;
4048 * \endcode
4049 * except (1) it works even if the key's policy does not allow outputting the
4050 * bytes, and (2) the comparison will be done in constant time.
4051 *
4052 * If this function returns an error status other than
4053 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
4054 * the operation enters an error state and must be aborted by calling
4055 * psa_key_derivation_abort().
4056 *
4057 * \param[in,out] operation The key derivation operation object to read from.
4058 * \param[in] expected Buffer containing the expected derivation output.
4059 * \param expected_length Length of the expected output; this is also the
4060 * number of bytes that will be read.
4061 *
4062 * \retval #PSA_SUCCESS \emptydescription
4063 * \retval #PSA_ERROR_INVALID_SIGNATURE
4064 * The output was read successfully, but it differs from the expected
4065 * output.
4066 * \retval #PSA_ERROR_NOT_PERMITTED
4067 * One of the inputs was a key whose policy didn't allow
4068 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
4069 * \retval #PSA_ERROR_INSUFFICIENT_DATA
4070 * The operation's capacity was less than
4071 * \p output_length bytes. Note that in this case,
4072 * the operation's capacity is set to 0, thus
4073 * subsequent calls to this function will not
4074 * succeed, even with a smaller expected output.
4075 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4077 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4078 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4079 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4080 * \retval #PSA_ERROR_BAD_STATE
4081 * The operation state is not valid (it must be active and completed
4082 * all required input steps), or the library has not been previously
4083 * initialized by psa_crypto_init().
4084 * It is implementation-dependent whether a failure to initialize
4085 * results in this error code.
4086 */
4087psa_status_t psa_key_derivation_verify_bytes(
4088 psa_key_derivation_operation_t *operation,
4089 const uint8_t *expected,
4090 size_t expected_length);
4091
4092/** Compare output data from a key derivation operation to an expected value
4093 * stored in a key object.
4094 *
4095 * This function calculates output bytes from a key derivation algorithm and
4096 * compares those bytes to an expected value, provided as key of type
4097 * #PSA_KEY_TYPE_PASSWORD_HASH.
4098 * If you view the key derivation's output as a stream of bytes, this
4099 * function destructively reads the number of bytes corresponding to the
4100 * length of the expected value from the stream before comparing them.
4101 * The operation's capacity decreases by the number of bytes read.
4102 *
4103 * This is functionally equivalent to exporting the key and calling
4104 * psa_key_derivation_verify_bytes() on the result, except that it
4105 * works even if the key cannot be exported.
4106 *
4107 * If this function returns an error status other than
4108 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
4109 * the operation enters an error state and must be aborted by calling
4110 * psa_key_derivation_abort().
4111 *
4112 * \param[in,out] operation The key derivation operation object to read from.
4113 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
4114 * containing the expected output. Its policy must
4115 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
4116 * and the permitted algorithm must match the
4117 * operation. The value of this key was likely
4118 * computed by a previous call to
4119 * psa_key_derivation_output_key() or
4120 * psa_key_derivation_output_key_custom().
4121 *
4122 * \retval #PSA_SUCCESS \emptydescription
4123 * \retval #PSA_ERROR_INVALID_SIGNATURE
4124 * The output was read successfully, but if differs from the expected
4125 * output.
4126 * \retval #PSA_ERROR_INVALID_HANDLE
4127 * The key passed as the expected value does not exist.
4128 * \retval #PSA_ERROR_INVALID_ARGUMENT
4129 * The key passed as the expected value has an invalid type.
4130 * \retval #PSA_ERROR_NOT_PERMITTED
4131 * The key passed as the expected value does not allow this usage or
4132 * this algorithm; or one of the inputs was a key whose policy didn't
4133 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
4134 * \retval #PSA_ERROR_INSUFFICIENT_DATA
4135 * The operation's capacity was less than
4136 * the length of the expected value. In this case,
4137 * the operation's capacity is set to 0, thus
4138 * subsequent calls to this function will not
4139 * succeed, even with a smaller expected output.
4140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4141 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4142 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4143 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4144 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4145 * \retval #PSA_ERROR_BAD_STATE
4146 * The operation state is not valid (it must be active and completed
4147 * all required input steps), or the library has not been previously
4148 * initialized by psa_crypto_init().
4149 * It is implementation-dependent whether a failure to initialize
4150 * results in this error code.
4151 */
4152psa_status_t psa_key_derivation_verify_key(
4153 psa_key_derivation_operation_t *operation,
4154 psa_key_id_t expected);
4155
4156/** Abort a key derivation operation.
4157 *
4158 * Aborting an operation frees all associated resources except for the \c
4159 * operation structure itself. Once aborted, the operation object can be reused
4160 * for another operation by calling psa_key_derivation_setup() again.
4161 *
4162 * This function may be called at any time after the operation
4163 * object has been initialized as described in #psa_key_derivation_operation_t.
4164 *
4165 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
4166 * call psa_key_derivation_abort() on an operation that has not been set up.
4167 *
4168 * \param[in,out] operation The operation to abort.
4169 *
4170 * \retval #PSA_SUCCESS \emptydescription
4171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4172 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4173 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4174 * \retval #PSA_ERROR_BAD_STATE
4175 * The library has not been previously initialized by psa_crypto_init().
4176 * It is implementation-dependent whether a failure to initialize
4177 * results in this error code.
4178 */
4179psa_status_t psa_key_derivation_abort(
4180 psa_key_derivation_operation_t *operation);
4181
4182/** Perform a key agreement and return the raw shared secret.
4183 *
4184 * \warning The raw result of a key agreement algorithm such as finite-field
4185 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
4186 * not be used directly as key material. It should instead be passed as
4187 * input to a key derivation algorithm. To chain a key agreement with
4188 * a key derivation, use psa_key_derivation_key_agreement() and other
4189 * functions from the key derivation interface.
4190 *
4191 * \param alg The key agreement algorithm to compute
4192 * (\c PSA_ALG_XXX value such that
4193 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
4194 * is true).
4195 * \param private_key Identifier of the private key to use. It must
4196 * allow the usage #PSA_KEY_USAGE_DERIVE.
4197 * \param[in] peer_key Public key of the peer. It must be
4198 * in the same format that psa_import_key()
4199 * accepts. The standard formats for public
4200 * keys are documented in the documentation
4201 * of psa_export_public_key().
4202 * \param peer_key_length Size of \p peer_key in bytes.
4203 * \param[out] output Buffer where the decrypted message is to
4204 * be written.
4205 * \param output_size Size of the \c output buffer in bytes.
4206 * \param[out] output_length On success, the number of bytes
4207 * that make up the returned output.
4208 *
4209 * \retval #PSA_SUCCESS
4210 * Success.
4211 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4212 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
4213 * \retval #PSA_ERROR_INVALID_ARGUMENT
4214 * \p alg is not a key agreement algorithm, or
4215 * \p private_key is not compatible with \p alg,
4216 * or \p peer_key is not valid for \p alg or not compatible with
4217 * \p private_key.
4218 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4219 * \p output_size is too small
4220 * \retval #PSA_ERROR_NOT_SUPPORTED
4221 * \p alg is not a supported key agreement algorithm.
4222 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4223 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4224 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4225 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4226 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4227 * \retval #PSA_ERROR_BAD_STATE
4228 * The library has not been previously initialized by psa_crypto_init().
4229 * It is implementation-dependent whether a failure to initialize
4230 * results in this error code.
4231 */
4232psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
4233 mbedtls_svc_key_id_t private_key,
4234 const uint8_t *peer_key,
4235 size_t peer_key_length,
4236 uint8_t *output,
4237 size_t output_size,
4238 size_t *output_length);
4239
4240/** Perform a key agreement and return the shared secret as a derivation key.
4241 *
4242 * \param private_key Identifier of the private key to use. It must
4243 * allow the usage #PSA_KEY_USAGE_DERIVE.
4244 * \param[in] peer_key Public key of the peer. It must be
4245 * in the same format that psa_import_key()
4246 * accepts. The standard formats for public
4247 * keys are documented in the documentation
4248 * of psa_export_public_key().
4249 * \param peer_key_length Size of \p peer_key in bytes.
4250 * \param alg The key agreement algorithm to compute
4251 * (\c PSA_ALG_XXX value such that
4252 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
4253 * is true).
4254 * \param[in] attributes The attributes for the new key. This function uses
4255 * the attributes as follows:
4256 * * The key type must be one of #PSA_KEY_TYPE_DERIVE,
4257 * #PSA_KEY_TYPE_RAW_DATA, #PSA_KEY_TYPE_HMAC, or
4258 * #PSA_KEY_TYPE_PASSWORD.
4259 * * The size of the returned key is always the
4260 * bit-size of the shared secret, rounded up
4261 * to a whole number of bytes. The key size in
4262 * attributes can be zero; if it is nonzero, it
4263 * must be equal to the output size of the key
4264 * agreement, in bits.
4265 * The output size, in bits, of the key agreement
4266 * is 8 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(type, bits),
4267 * where type and bits are the type and bit-size of
4268 * private_key.
4269 * * The key permitted-algorithm policy is required for
4270 * keys that will be used for a cryptographic operation.
4271 * * The key usage flags define what operations are
4272 * permitted with the key.
4273 * * The key lifetime and identifier are required
4274 * for a persistent key.
4275 * \param[out] key On success, an identifier for the newly created
4276 * key. #PSA_KEY_ID_NULL on failure.
4277 * \retval #PSA_SUCCESS
4278 * Success.
4279 * \retval #PSA_ERROR_BAD_STATE
4280 * The library has not been previously initialized by psa_crypto_init().
4281 * \retval #PSA_ERROR_INVALID_HANDLE
4282 * \p private_key is not a valid key identifier.
4283 * \retval #PSA_ERROR_NOT_PERMITTED
4284 * \p private_key does not have the PSA_KEY_USAGE_DERIVE flag,
4285 * or it does not permit the requested algorithm.
4286 * \retval #PSA_ERROR_ALREADY_EXISTS
4287 * This is an attempt to create a persistent key, and there is already
4288 * a persistent key with the given identifier.
4289 * \retval #PSA_ERROR_INVALID_ARGUMENT
4290 * * \p alg is not a key agreement algorithm.
4291 * * \p private_key is not compatible with \p alg.
4292 * * \p peer_key is not valid for \p alg or not compatible with
4293 * \p private_key.
4294 * * The output key attributes in \p attributes are not valid:
4295 * * The key type is not valid for key agreement output.
4296 * * The key size is nonzero, and is not the size of the shared secret.
4297 * * The key lifetime is invalid.
4298 * * The key identifier is not valid for the key lifetime.
4299 * * The key usage flags include invalid values.
4300 * * The key’s permitted-usage algorithm is invalid.
4301 * * The key attributes, as a whole, are invalid.
4302 * \retval #PSA_ERROR_NOT_SUPPORTED
4303 * * \p alg is not a supported key agreement algorithm.
4304 * * \p private_key is not supported for use with alg.
4305 * * The output key attributes, as a whole, are not supported,
4306 * either by the implementation in general or in the specified
4307 * storage location.
4308 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4309 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4310 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4311 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4312 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4313 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4314 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4315 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4316 */
4317psa_status_t psa_key_agreement(mbedtls_svc_key_id_t private_key,
4318 const uint8_t *peer_key,
4319 size_t peer_key_length,
4320 psa_algorithm_t alg,
4321 const psa_key_attributes_t *attributes,
4322 mbedtls_svc_key_id_t *key);
4323/**@}*/
4324
4325/** \defgroup random Random generation
4326 * @{
4327 */
4328
4329/**
4330 * \brief Generate random bytes.
4331 *
4332 * \warning This function **can** fail! Callers MUST check the return status
4333 * and MUST NOT use the content of the output buffer if the return
4334 * status is not #PSA_SUCCESS.
4335 *
4336 * \note To generate a key, use psa_generate_key() instead.
4337 *
4338 * \param[out] output Output buffer for the generated data.
4339 * \param output_size Number of bytes to generate and output.
4340 *
4341 * \retval #PSA_SUCCESS \emptydescription
4342 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4343 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4344 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \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_BAD_STATE
4349 * The library has not been previously initialized by psa_crypto_init().
4350 * It is implementation-dependent whether a failure to initialize
4351 * results in this error code.
4352 */
4353psa_status_t psa_generate_random(uint8_t *output,
4354 size_t output_size);
4355
4356/**
4357 * \brief Generate a key or key pair.
4358 *
4359 * The key is generated randomly.
4360 * Its location, usage policy, type and size are taken from \p attributes.
4361 *
4362 * Implementations must reject an attempt to generate a key of size 0.
4363 *
4364 * The following type-specific considerations apply:
4365 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
4366 * the public exponent is 65537.
4367 * The modulus is a product of two probabilistic primes
4368 * between 2^{n-1} and 2^n where n is the bit size specified in the
4369 * attributes.
4370 *
4371 * \note This function is equivalent to calling psa_generate_key_custom()
4372 * with the custom production parameters #PSA_CUSTOM_KEY_PARAMETERS_INIT
4373 * and `custom_data_length == 0` (i.e. `custom_data` is empty).
4374 *
4375 * \param[in] attributes The attributes for the new key.
4376 * \param[out] key On success, an identifier for the newly created
4377 * key. For persistent keys, this is the key
4378 * identifier defined in \p attributes.
4379 * \c 0 on failure.
4380 *
4381 * \retval #PSA_SUCCESS
4382 * Success.
4383 * If the key is persistent, the key material and the key's metadata
4384 * have been saved to persistent storage.
4385 * \retval #PSA_ERROR_ALREADY_EXISTS
4386 * This is an attempt to create a persistent key, and there is
4387 * already a persistent key with the given identifier.
4388 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4389 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4391 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4393 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4394 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4395 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4396 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4397 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4398 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4399 * \retval #PSA_ERROR_BAD_STATE
4400 * The library has not been previously initialized by psa_crypto_init().
4401 * It is implementation-dependent whether a failure to initialize
4402 * results in this error code.
4403 */
4404psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
4405 mbedtls_svc_key_id_t *key);
4406
4407/**
4408 * \brief Generate a key or key pair using custom production parameters.
4409 *
4410 * See the description of psa_generate_key() for the operation of this
4411 * function with the default production parameters. In addition, this function
4412 * supports the following production customizations, described in more detail
4413 * in the documentation of ::psa_custom_key_parameters_t:
4414 *
4415 * - RSA keys: generation with a custom public exponent.
4416 *
4417 * \note This function is experimental and may change in future minor
4418 * versions of Mbed TLS.
4419 *
4420 * \param[in] attributes The attributes for the new key.
4421 * \param[in] custom Customization parameters for the key generation.
4422 * When this is #PSA_CUSTOM_KEY_PARAMETERS_INIT
4423 * with \p custom_data_length = 0,
4424 * this function is equivalent to
4425 * psa_generate_key().
4426 * \param[in] custom_data Variable-length data associated with \c custom.
4427 * \param custom_data_length
4428 * Length of `custom_data` in bytes.
4429 * \param[out] key On success, an identifier for the newly created
4430 * key. For persistent keys, this is the key
4431 * identifier defined in \p attributes.
4432 * \c 0 on failure.
4433 *
4434 * \retval #PSA_SUCCESS
4435 * Success.
4436 * If the key is persistent, the key material and the key's metadata
4437 * have been saved to persistent storage.
4438 * \retval #PSA_ERROR_ALREADY_EXISTS
4439 * This is an attempt to create a persistent key, and there is
4440 * already a persistent key with the given identifier.
4441 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4442 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4444 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4445 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4446 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4447 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4448 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4449 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4450 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4451 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4452 * \retval #PSA_ERROR_BAD_STATE
4453 * The library has not been previously initialized by psa_crypto_init().
4454 * It is implementation-dependent whether a failure to initialize
4455 * results in this error code.
4456 */
4457psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
4458 const psa_custom_key_parameters_t *custom,
4459 const uint8_t *custom_data,
4460 size_t custom_data_length,
4461 mbedtls_svc_key_id_t *key);
4462
4463/**@}*/
4464
4465/** \defgroup interruptible_hash Interruptible sign/verify hash
4466 * @{
4467 */
4468
4469/** The type of the state data structure for interruptible hash
4470 * signing operations.
4471 *
4472 * Before calling any function on a sign hash operation object, the
4473 * application must initialize it by any of the following means:
4474 * - Set the structure to all-bits-zero, for example:
4475 * \code
4476 * psa_sign_hash_interruptible_operation_t operation;
4477 * memset(&operation, 0, sizeof(operation));
4478 * \endcode
4479 * - Initialize the structure to logical zero values, for example:
4480 * \code
4481 * psa_sign_hash_interruptible_operation_t operation = {0};
4482 * \endcode
4483 * - Initialize the structure to the initializer
4484 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4485 * \code
4486 * psa_sign_hash_interruptible_operation_t operation =
4487 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4488 * \endcode
4489 * - Assign the result of the function
4490 * psa_sign_hash_interruptible_operation_init() to the structure, for
4491 * example:
4492 * \code
4493 * psa_sign_hash_interruptible_operation_t operation;
4494 * operation = psa_sign_hash_interruptible_operation_init();
4495 * \endcode
4496 *
4497 * This is an implementation-defined \c struct. Applications should not
4498 * make any assumptions about the content of this structure.
4499 * Implementation details can change in future versions without notice. */
4500typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4501
4502/** The type of the state data structure for interruptible hash
4503 * verification operations.
4504 *
4505 * Before calling any function on a sign hash operation object, the
4506 * application must initialize it by any of the following means:
4507 * - Set the structure to all-bits-zero, for example:
4508 * \code
4509 * psa_verify_hash_interruptible_operation_t operation;
4510 * memset(&operation, 0, sizeof(operation));
4511 * \endcode
4512 * - Initialize the structure to logical zero values, for example:
4513 * \code
4514 * psa_verify_hash_interruptible_operation_t operation = {0};
4515 * \endcode
4516 * - Initialize the structure to the initializer
4517 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4518 * \code
4519 * psa_verify_hash_interruptible_operation_t operation =
4520 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4521 * \endcode
4522 * - Assign the result of the function
4523 * psa_verify_hash_interruptible_operation_init() to the structure, for
4524 * example:
4525 * \code
4526 * psa_verify_hash_interruptible_operation_t operation;
4527 * operation = psa_verify_hash_interruptible_operation_init();
4528 * \endcode
4529 *
4530 * This is an implementation-defined \c struct. Applications should not
4531 * make any assumptions about the content of this structure.
4532 * Implementation details can change in future versions without notice. */
4533typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4534
4535/**
4536 * \brief Set the maximum number of ops allowed to be
4537 * executed by an interruptible function in a
4538 * single call.
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 * \note The time taken to execute a single op is
4545 * implementation specific and depends on
4546 * software, hardware, the algorithm, key type and
4547 * curve chosen. Even within a single operation,
4548 * successive ops can take differing amounts of
4549 * time. The only guarantee is that lower values
4550 * for \p max_ops means functions will block for a
4551 * lesser maximum amount of time. The functions
4552 * \c psa_sign_interruptible_get_num_ops(),
4553 * \c psa_verify_interruptible_get_num_ops() and
4554 * \c psa_generate_key_iop_get_num_ops() are
4555 * provided to help with tuning this value.
4556 *
4557 * \note This value defaults to
4558 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4559 * means the whole operation will be done in one
4560 * go, regardless of the number of ops required.
4561 *
4562 * \note If more ops are needed to complete a
4563 * computation, #PSA_OPERATION_INCOMPLETE will be
4564 * returned by the function performing the
4565 * computation. It is then the caller's
4566 * responsibility to either call again with the
4567 * same operation context until it returns 0 or an
4568 * error code; or to call the relevant abort
4569 * function if the answer is no longer required.
4570 *
4571 * \note The interpretation of \p max_ops is also
4572 * implementation defined. On a hard real time
4573 * system, this can indicate a hard deadline, as a
4574 * real-time system needs a guarantee of not
4575 * spending more than X time, however care must be
4576 * taken in such an implementation to avoid the
4577 * situation whereby calls just return, not being
4578 * able to do any actual work within the allotted
4579 * time. On a non-real-time system, the
4580 * implementation can be more relaxed, but again
4581 * whether this number should be interpreted as as
4582 * hard or soft limit or even whether a less than
4583 * or equals as regards to ops executed in a
4584 * single call is implementation defined.
4585 *
4586 * \note For keys in local storage when no accelerator
4587 * driver applies, please see also the
4588 * documentation for \c psa_interruptible_set_max_ops(),
4589 * which is the internal implementation in these
4590 * cases.
4591 *
4592 * \warning With implementations that interpret this number
4593 * as a hard limit, setting this number too small
4594 * may result in an infinite loop, whereby each
4595 * call results in immediate return with no ops
4596 * done (as there is not enough time to execute
4597 * any), and thus no result will ever be achieved.
4598 *
4599 * \note This only applies to functions whose
4600 * documentation mentions they may return
4601 * #PSA_OPERATION_INCOMPLETE.
4602 *
4603 * \param max_ops The maximum number of ops to be executed in a
4604 * single call. This can be a number from 0 to
4605 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4606 * is the least amount of work done per call.
4607 */
4608void psa_interruptible_set_max_ops(uint32_t max_ops);
4609
4610/**
4611 * \brief Get the maximum number of ops allowed to be
4612 * executed by an interruptible function in a
4613 * single call. This will return the last
4614 * value set by
4615 * \c psa_interruptible_set_max_ops() or
4616 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4617 * that function has never been called.
4618 *
4619 * \warning This is a beta API, and thus subject to change
4620 * at any point. It is not bound by the usual
4621 * interface stability promises.
4622 *
4623 * \return Maximum number of ops allowed to be
4624 * executed by an interruptible function in a
4625 * single call.
4626 */
4627uint32_t psa_interruptible_get_max_ops(void);
4628
4629/**
4630 * \brief Get the number of ops that a hash signing
4631 * operation has taken so far. If the operation
4632 * has completed, then this will represent the
4633 * number of ops required for the entire
4634 * operation. After initialization or calling
4635 * \c psa_sign_hash_interruptible_abort() on
4636 * the operation, a value of 0 will be returned.
4637 *
4638 * \note This interface is guaranteed re-entrant and
4639 * thus may be called from driver code.
4640 *
4641 * \warning This is a beta API, and thus subject to change
4642 * at any point. It is not bound by the usual
4643 * interface stability promises.
4644 *
4645 * This is a helper provided to help you tune the
4646 * value passed to \c
4647 * psa_interruptible_set_max_ops().
4648 *
4649 * \param operation The \c psa_sign_hash_interruptible_operation_t
4650 * to use. This must be initialized first.
4651 *
4652 * \return Number of ops that the operation has taken so
4653 * far.
4654 */
4655uint32_t psa_sign_hash_get_num_ops(
4656 const psa_sign_hash_interruptible_operation_t *operation);
4657
4658/**
4659 * \brief Get the number of ops that a hash verification
4660 * operation has taken so far. If the operation
4661 * has completed, then this will represent the
4662 * number of ops required for the entire
4663 * operation. After initialization or calling \c
4664 * psa_verify_hash_interruptible_abort() on the
4665 * operation, a value of 0 will be returned.
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 * This is a helper provided to help you tune the
4672 * value passed to \c
4673 * psa_interruptible_set_max_ops().
4674 *
4675 * \param operation The \c
4676 * psa_verify_hash_interruptible_operation_t to
4677 * use. This must be initialized first.
4678 *
4679 * \return Number of ops that the operation has taken so
4680 * far.
4681 */
4682uint32_t psa_verify_hash_get_num_ops(
4683 const psa_verify_hash_interruptible_operation_t *operation);
4684
4685/**
4686 * \brief Start signing a hash or short message with a
4687 * private key, in an interruptible manner.
4688 *
4689 * \see \c psa_sign_hash_complete()
4690 *
4691 * \warning This is a beta API, and thus subject to change
4692 * at any point. It is not bound by the usual
4693 * interface stability promises.
4694 *
4695 * \note This function combined with \c
4696 * psa_sign_hash_complete() is equivalent to
4697 * \c psa_sign_hash() but
4698 * \c psa_sign_hash_complete() can return early and
4699 * resume according to the limit set with \c
4700 * psa_interruptible_set_max_ops() to reduce the
4701 * maximum time spent in a function call.
4702 *
4703 * \note Users should call \c psa_sign_hash_complete()
4704 * repeatedly on the same context after a
4705 * successful call to this function until \c
4706 * psa_sign_hash_complete() either returns 0 or an
4707 * error. \c psa_sign_hash_complete() will return
4708 * #PSA_OPERATION_INCOMPLETE if there is more work
4709 * to do. Alternatively users can call
4710 * \c psa_sign_hash_abort() at any point if they no
4711 * longer want the result.
4712 *
4713 * \note If this function returns an error status, the
4714 * operation enters an error state and must be
4715 * aborted by calling \c psa_sign_hash_abort().
4716 *
4717 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4718 * to use. This must be initialized first.
4719 *
4720 * \param key Identifier of the key to use for the operation.
4721 * It must be an asymmetric key pair. The key must
4722 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4723 * \param alg A signature algorithm (\c PSA_ALG_XXX
4724 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4725 * is true), that is compatible with
4726 * the type of \p key.
4727 * \param[in] hash The hash or message to sign.
4728 * \param hash_length Size of the \p hash buffer in bytes.
4729 *
4730 * \retval #PSA_SUCCESS
4731 * The operation started successfully - call \c psa_sign_hash_complete()
4732 * with the same context to complete the operation
4733 *
4734 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
4735 * \retval #PSA_ERROR_NOT_PERMITTED
4736 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4737 * not permit the requested algorithm.
4738 * \retval #PSA_ERROR_BAD_STATE
4739 * An operation has previously been started on this context, and is
4740 * still in progress.
4741 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4742 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4743 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4744 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4745 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4746 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4747 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4748 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4749 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4750 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4751 * \retval #PSA_ERROR_BAD_STATE
4752 * The library has not been previously initialized by psa_crypto_init().
4753 * It is implementation-dependent whether a failure to initialize
4754 * results in this error code.
4755 */
4756psa_status_t psa_sign_hash_start(
4757 psa_sign_hash_interruptible_operation_t *operation,
4758 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4759 const uint8_t *hash, size_t hash_length);
4760
4761/**
4762 * \brief Continue and eventually complete the action of
4763 * signing a hash or short message with a private
4764 * key, in an interruptible manner.
4765 *
4766 * \see \c psa_sign_hash_start()
4767 *
4768 * \warning This is a beta API, and thus subject to change
4769 * at any point. It is not bound by the usual
4770 * interface stability promises.
4771 *
4772 * \note This function combined with \c
4773 * psa_sign_hash_start() is equivalent to
4774 * \c psa_sign_hash() but this function can return
4775 * early and resume according to the limit set with
4776 * \c psa_interruptible_set_max_ops() to reduce the
4777 * maximum time spent in a function call.
4778 *
4779 * \note Users should call this function on the same
4780 * operation object repeatedly until it either
4781 * returns 0 or an error. This function will return
4782 * #PSA_OPERATION_INCOMPLETE if there is more work
4783 * to do. Alternatively users can call
4784 * \c psa_sign_hash_abort() at any point if they no
4785 * longer want the result.
4786 *
4787 * \note When this function returns successfully, the
4788 * operation becomes inactive. If this function
4789 * returns an error status, the operation enters an
4790 * error state and must be aborted by calling
4791 * \c psa_sign_hash_abort().
4792 *
4793 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4794 * to use. This must be initialized first, and have
4795 * had \c psa_sign_hash_start() called with it
4796 * first.
4797 *
4798 * \param[out] signature Buffer where the signature is to be written.
4799 * \param signature_size Size of the \p signature buffer in bytes. This
4800 * must be appropriate for the selected
4801 * algorithm and key:
4802 * - The required signature size is
4803 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4804 * key_bits, \c alg) where \c key_type and \c
4805 * key_bits are the type and bit-size
4806 * respectively of key.
4807 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4808 * maximum signature size of any supported
4809 * signature algorithm.
4810 * \param[out] signature_length On success, the number of bytes that make up
4811 * the returned signature value.
4812 *
4813 * \retval #PSA_SUCCESS
4814 * Operation completed successfully
4815 *
4816 * \retval #PSA_OPERATION_INCOMPLETE
4817 * Operation was interrupted due to the setting of \c
4818 * psa_interruptible_set_max_ops(). There is still work to be done.
4819 * Call this function again with the same operation object.
4820 *
4821 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4822 * The size of the \p signature buffer is too small. You can
4823 * determine a sufficient buffer size by calling
4824 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
4825 * where \c key_type and \c key_bits are the type and bit-size
4826 * respectively of \c key.
4827 *
4828 * \retval #PSA_ERROR_BAD_STATE
4829 * An operation was not previously started on this context via
4830 * \c psa_sign_hash_start().
4831 *
4832 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4833 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4836 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4837 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4838 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4839 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4840 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4841 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4842 * \retval #PSA_ERROR_BAD_STATE
4843 * The library has either not been previously initialized by
4844 * psa_crypto_init() or you did not previously call
4845 * psa_sign_hash_start() with this operation object. It is
4846 * implementation-dependent whether a failure to initialize results in
4847 * this error code.
4848 */
4849psa_status_t psa_sign_hash_complete(
4850 psa_sign_hash_interruptible_operation_t *operation,
4851 uint8_t *signature, size_t signature_size,
4852 size_t *signature_length);
4853
4854/**
4855 * \brief Abort a sign hash operation.
4856 *
4857 * \warning This is a beta API, and thus subject to change
4858 * at any point. It is not bound by the usual
4859 * interface stability promises.
4860 *
4861 * \note This function is the only function that clears
4862 * the number of ops completed as part of the
4863 * operation. Please ensure you copy this value via
4864 * \c psa_sign_hash_get_num_ops() if required
4865 * before calling.
4866 *
4867 * \note Aborting an operation frees all associated
4868 * resources except for the \p operation structure
4869 * itself. Once aborted, the operation object can
4870 * be reused for another operation by calling \c
4871 * psa_sign_hash_start() again.
4872 *
4873 * \note You may call this function any time after the
4874 * operation object has been initialized. In
4875 * particular, calling \c psa_sign_hash_abort()
4876 * after the operation has already been terminated
4877 * by a call to \c psa_sign_hash_abort() or
4878 * psa_sign_hash_complete() is safe.
4879 *
4880 * \param[in,out] operation Initialized sign hash operation.
4881 *
4882 * \retval #PSA_SUCCESS
4883 * The operation was aborted successfully.
4884 *
4885 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4886 * \retval #PSA_ERROR_BAD_STATE
4887 * The library has not been previously initialized by psa_crypto_init().
4888 * It is implementation-dependent whether a failure to initialize
4889 * results in this error code.
4890 */
4891psa_status_t psa_sign_hash_abort(
4892 psa_sign_hash_interruptible_operation_t *operation);
4893
4894/**
4895 * \brief Start reading and verifying a hash or short
4896 * message, in an interruptible manner.
4897 *
4898 * \see \c psa_verify_hash_complete()
4899 *
4900 * \warning This is a beta API, and thus subject to change
4901 * at any point. It is not bound by the usual
4902 * interface stability promises.
4903 *
4904 * \note This function combined with \c
4905 * psa_verify_hash_complete() is equivalent to
4906 * \c psa_verify_hash() but \c
4907 * psa_verify_hash_complete() can return early and
4908 * resume according to the limit set with \c
4909 * psa_interruptible_set_max_ops() to reduce the
4910 * maximum time spent in a function.
4911 *
4912 * \note Users should call \c psa_verify_hash_complete()
4913 * repeatedly on the same operation object after a
4914 * successful call to this function until \c
4915 * psa_verify_hash_complete() either returns 0 or
4916 * an error. \c psa_verify_hash_complete() will
4917 * return #PSA_OPERATION_INCOMPLETE if there is
4918 * more work to do. Alternatively users can call
4919 * \c psa_verify_hash_abort() at any point if they
4920 * no longer want the result.
4921 *
4922 * \note If this function returns an error status, the
4923 * operation enters an error state and must be
4924 * aborted by calling \c psa_verify_hash_abort().
4925 *
4926 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4927 * to use. This must be initialized first.
4928 *
4929 * \param key Identifier of the key to use for the operation.
4930 * The key must allow the usage
4931 * #PSA_KEY_USAGE_VERIFY_HASH.
4932 * \param alg A signature algorithm (\c PSA_ALG_XXX
4933 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4934 * is true), that is compatible with
4935 * the type of \p key.
4936 * \param[in] hash The hash whose signature is to be verified.
4937 * \param hash_length Size of the \p hash buffer in bytes.
4938 * \param[in] signature Buffer containing the signature to verify.
4939 * \param signature_length Size of the \p signature buffer in bytes.
4940 *
4941 * \retval #PSA_SUCCESS
4942 * The operation started successfully - please call \c
4943 * psa_verify_hash_complete() with the same context to complete the
4944 * operation.
4945 *
4946 * \retval #PSA_ERROR_BAD_STATE
4947 * Another operation has already been started on this context, and is
4948 * still in progress.
4949 *
4950 * \retval #PSA_ERROR_NOT_PERMITTED
4951 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4952 * not permit the requested algorithm.
4953 *
4954 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4955 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4956 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4957 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4958 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4959 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4960 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4961 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4962 * \retval PSA_ERROR_DATA_INVALID \emptydescription
4963 * \retval #PSA_ERROR_BAD_STATE
4964 * The library has not been previously initialized by psa_crypto_init().
4965 * It is implementation-dependent whether a failure to initialize
4966 * results in this error code.
4967 */
4968psa_status_t psa_verify_hash_start(
4969 psa_verify_hash_interruptible_operation_t *operation,
4970 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4971 const uint8_t *hash, size_t hash_length,
4972 const uint8_t *signature, size_t signature_length);
4973
4974/**
4975 * \brief Continue and eventually complete the action of
4976 * reading and verifying a hash or short message
4977 * signed with a private key, in an interruptible
4978 * manner.
4979 *
4980 * \see \c psa_verify_hash_start()
4981 *
4982 * \warning This is a beta API, and thus subject to change
4983 * at any point. It is not bound by the usual
4984 * interface stability promises.
4985 *
4986 * \note This function combined with \c
4987 * psa_verify_hash_start() is equivalent to
4988 * \c psa_verify_hash() but this function can
4989 * return early and resume according to the limit
4990 * set with \c psa_interruptible_set_max_ops() to
4991 * reduce the maximum time spent in a function
4992 * call.
4993 *
4994 * \note Users should call this function on the same
4995 * operation object repeatedly until it either
4996 * returns 0 or an error. This function will return
4997 * #PSA_OPERATION_INCOMPLETE if there is more work
4998 * to do. Alternatively users can call
4999 * \c psa_verify_hash_abort() at any point if they
5000 * no longer want the result.
5001 *
5002 * \note When this function returns successfully, the
5003 * operation becomes inactive. If this function
5004 * returns an error status, the operation enters an
5005 * error state and must be aborted by calling
5006 * \c psa_verify_hash_abort().
5007 *
5008 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
5009 * to use. This must be initialized first, and have
5010 * had \c psa_verify_hash_start() called with it
5011 * first.
5012 *
5013 * \retval #PSA_SUCCESS
5014 * Operation completed successfully, and the passed signature is valid.
5015 *
5016 * \retval #PSA_OPERATION_INCOMPLETE
5017 * Operation was interrupted due to the setting of \c
5018 * psa_interruptible_set_max_ops(). There is still work to be done.
5019 * Call this function again with the same operation object.
5020 *
5021 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
5022 * \retval #PSA_ERROR_INVALID_SIGNATURE
5023 * The calculation was performed successfully, but the passed
5024 * signature is not a valid signature.
5025 * \retval #PSA_ERROR_BAD_STATE
5026 * An operation was not previously started on this context via
5027 * \c psa_verify_hash_start().
5028 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
5029 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
5030 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5031 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5032 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
5033 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5034 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5035 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5036 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5037 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
5038 * \retval #PSA_ERROR_BAD_STATE
5039 * The library has either not been previously initialized by
5040 * psa_crypto_init() or you did not previously call
5041 * psa_verify_hash_start() on this object. It is
5042 * implementation-dependent whether a failure to initialize results in
5043 * this error code.
5044 */
5045psa_status_t psa_verify_hash_complete(
5046 psa_verify_hash_interruptible_operation_t *operation);
5047
5048/**
5049 * \brief Abort a verify hash operation.
5050 *
5051 * \warning This is a beta API, and thus subject to change at
5052 * any point. It is not bound by the usual interface
5053 * stability promises.
5054 *
5055 * \note This function is the only function that clears the
5056 * number of ops completed as part of the operation.
5057 * Please ensure you copy this value via
5058 * \c psa_verify_hash_get_num_ops() if required
5059 * before calling.
5060 *
5061 * \note Aborting an operation frees all associated
5062 * resources except for the operation structure
5063 * itself. Once aborted, the operation object can be
5064 * reused for another operation by calling \c
5065 * psa_verify_hash_start() again.
5066 *
5067 * \note You may call this function any time after the
5068 * operation object has been initialized.
5069 * In particular, calling \c psa_verify_hash_abort()
5070 * after the operation has already been terminated by
5071 * a call to \c psa_verify_hash_abort() or
5072 * psa_verify_hash_complete() is safe.
5073 *
5074 * \param[in,out] operation Initialized verify hash operation.
5075 *
5076 * \retval #PSA_SUCCESS
5077 * The operation was aborted successfully.
5078 *
5079 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
5080 * \retval #PSA_ERROR_BAD_STATE
5081 * The library has not been previously initialized by psa_crypto_init().
5082 * It is implementation-dependent whether a failure to initialize
5083 * results in this error code.
5084 */
5085psa_status_t psa_verify_hash_abort(
5086 psa_verify_hash_interruptible_operation_t *operation);
5087
5088
5089/**@}*/
5090
5091/**@}*/
5092
5093/**
5094 * \defgroup interruptible_key_agreement Interruptible Key Agreement
5095 * @{
5096 */
5097
5098/**
5099 * The type of the state data structure for interruptible key agreement
5100 * operations.
5101 *
5102 * Before calling any function on an interruptible key agreement object, the
5103 * application must initialize it by any of the following means:
5104 * - Set the structure to all-bits-zero, for example:
5105 * \code
5106 * psa_key_agreement_iop_t operation;
5107 * memset(&operation, 0, sizeof(operation));
5108 * \endcode
5109 * - Initialize the structure to logical zero values, for example:
5110 * \code
5111 * psa_key_agreement_iop_t operation = {0};
5112 * \endcode
5113 * - Initialize the structure to the initializer #PSA_KEY_AGREEMENT_IOP_INIT,
5114 * for example:
5115 * - \code
5116 * psa_key_agreement_iop_t operation = PSA_KEY_AGREEMENT_IOP_INIT;
5117 * \endcode
5118 * - Assign the result of the function psa_key_agreement_iop_init() to the
5119 * structure, for example:
5120 * \code
5121 * psa_key_agreement_iop_t operation;
5122 * operation = psa_key_agreement_iop_init();
5123 * \endcode
5124 *
5125 * This is an implementation-defined \c struct. Applications should not
5126 * make any assumptions about the content of this structure.
5127 * Implementation details can change in future versions without notice.
5128 */
5129typedef struct psa_key_agreement_iop_s psa_key_agreement_iop_t;
5130
5131/**
5132 * \brief Get the number of ops that a key agreement
5133 * operation has taken so far. If the operation has
5134 * completed, then this will represent the number of
5135 * ops required for the entire operation.
5136 * After initialization or calling \c
5137 * psa_key_agreement_iop_abort() on the operation,
5138 * a value of 0 will be returned.
5139 *
5140 * \warning This is a beta API, and thus subject to change
5141 * at any point. It is not bound by the usual
5142 * interface stability promises.
5143 *
5144 * This is a helper provided to help you tune the
5145 * value passed to \c
5146 * psa_interruptible_set_max_ops().
5147 *
5148 * \param operation The \c psa_key_agreement_iop_t to use. This must
5149 * be initialized first.
5150 *
5151 * \return Number of ops that the operation has taken so
5152 * far.
5153 */
5154uint32_t psa_key_agreement_iop_get_num_ops(psa_key_agreement_iop_t *operation);
5155
5156/**
5157 * \brief Start a key agreement operation, in an
5158 * interruptible manner.
5159 *
5160 * \see \c psa_key_agreement_iop_complete()
5161 *
5162 * \warning This is a beta API, and thus subject to change
5163 * at any point. It is not bound by the usual
5164 * interface stability promises.
5165 *
5166 * \warning The raw result of a key agreement algorithm such
5167 * elliptic curve Diffie-Hellman has biases
5168 * and should not be used directly as key material.
5169 * It should instead be passed as input to a key
5170 * derivation algorithm.
5171 *
5172 * \note This function combined with \c
5173 * psa_key_agreement_iop_complete() is equivalent
5174 * to \c psa_raw_key_agreement() but \c
5175 * psa_key_agreement_iop_complete() can return
5176 * early and resume according to the limit set with
5177 * \c psa_interruptible_set_max_ops() to reduce the
5178 * maximum time spent in a function.
5179 *
5180 * \note Users should call
5181 * \c psa_key_agreement_iop_complete() repeatedly
5182 * on the same operation object after a successful
5183 * call to this function until \c
5184 * psa_key_agreement_iop_complete() either returns
5185 * #PSA_SUCCESS or an error.
5186 * \c psa_key_agreement_iop_complete() will return
5187 * #PSA_OPERATION_INCOMPLETE if there is more work
5188 * to do. Alternatively users can call
5189 * \c psa_key_agreement_iop_abort() at any point
5190 * if they no longer want the result.
5191 *
5192 * \note If this function returns an error status, the
5193 * operation enters an error state and must be
5194 * aborted by calling \c
5195 * psa_key_agreement_iop_abort().
5196 *
5197 * \param[in, out] operation The \c psa_key_agreement_iop_t to use. This must
5198 * be initialized as per the documentation for
5199 * \c psa_key_agreement_iop_t, and be inactive.
5200
5201 * \param private_key Identifier of the private key to use. It must
5202 * allow the usage #PSA_KEY_USAGE_DERIVE.
5203 * \param[in] peer_key Public key of the peer. It must be in the
5204 * same format that psa_import_key() accepts. The
5205 * standard formats for public keys are documented
5206 * in the documentation of psa_export_public_key().
5207 * The peer key data is parsed with the type
5208 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type)
5209 * where \c type is the type of \p private_key,
5210 * and with the same bit-size as \p private_key.
5211 * \param peer_key_length Size of \p peer_key in bytes.
5212 *
5213 * \param alg The key agreement algorithm to compute
5214 * (a \c PSA_ALG_XXX value such that
5215 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
5216 *
5217 * \param[in] attributes The attributes for the new key.
5218 * The following attributes are required for all
5219 * keys:
5220 * * The key type, which must be one of
5221 * #PSA_KEY_TYPE_DERIVE, #PSA_KEY_TYPE_RAW_DATA,
5222 * #PSA_KEY_TYPE_HMAC or #PSA_KEY_TYPE_PASSWORD.
5223 * The following attributes must be set for keys
5224 * used in cryptographic operations:
5225 * * The key permitted-algorithm policy
5226 * * The key usage flags
5227 * The following attributes must be set for keys
5228 * that do not use the default volatile lifetime:
5229 * * The key lifetime
5230 * * The key identifier is required for a key with
5231 * a persistent lifetime
5232 * The following attributes are optional:
5233 * * If the key size is nonzero, it must be equal
5234 * to the output size of the key agreement,
5235 * in bits.
5236 * The output size, in bits, of the key agreement
5237 * is 8 * #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\c
5238 * type, \c bits), where \c type and \c bits are
5239 * the type and bit-size of \p private_key.
5240 *
5241 * \note \p attributes is an input parameter, it is not
5242 * updated with the final key attributes. The final
5243 * attributes of the new key can be queried by
5244 * calling `psa_get_key_attributes()` with
5245 * the key's identifier.
5246 *
5247 * \note This function clears the number of ops completed
5248 * as part of the operation. Please ensure you copy
5249 * this value via
5250 * \c psa_key_agreement_iop_get_num_ops() if
5251 * required before calling.
5252 *
5253 * \retval #PSA_SUCCESS
5254 * The operation started successfully.
5255 * Call \c psa_key_agreement_iop_complete() with the same context to
5256 * complete the operation.
5257 *
5258 * \retval #PSA_ERROR_BAD_STATE
5259 * Another operation has already been started on this context, and is
5260 * still in progress.
5261 *
5262 * \retval #PSA_ERROR_NOT_PERMITTED
5263 * The following conditions can result in this error:
5264 * * Either the \p private_key does not have the #PSA_KEY_USAGE_DERIVE`
5265 * flag, or it does not permit the requested algorithm.
5266 *
5267 * \retval #PSA_ERROR_INVALID_HANDLE
5268 * \p private_key is not a valid key identifier.
5269 *
5270 * \retval #PSA_ERROR_ALREADY_EXISTS
5271 * This is an attempt to create a persistent key, and there is already
5272 * a persistent key with the given identifier.
5273 *
5274 * \retval #PSA_ERROR_INVALID_ARGUMENT
5275 * The following conditions can result in this error:
5276 * * \p alg is not a key agreement algorithm.
5277 * * \p private_key is not compatible with \p alg.
5278 * * \p peer_key is not a valid public key corresponding to
5279 * \p private_key.
5280 * * The output key attributes in \p attributes are not valid:
5281 * - The key type is not valid for key agreement output.
5282 * - The key size is nonzero, and is not the size of the shared
5283 * secret.
5284 * - The key lifetime is invalid.
5285 * - The key identifier is not valid for the key lifetime.
5286 * - The key usage flags include invalid values.
5287 * - The key's permitted-usage algorithm is invalid.
5288 * - The key attributes, as a whole, are invalid.
5289 *
5290 * \retval #PSA_ERROR_NOT_SUPPORTED
5291 * The following conditions can result in this error:
5292 * * \p alg is not supported.
5293 * * \p private_key is not supported for use with \p alg.
5294 * * Only elliptic curve Diffie-Hellman with ECC keys is supported, not
5295 * finite field Diffie-Hellman with DH keys.
5296 *
5297 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
5298 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5299 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5300 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
5301 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5302 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5303 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5304 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5305 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
5306 * \retval #PSA_ERROR_BAD_STATE
5307 * The following conditions can result in this error:
5308 * * The library has not been previously initialized by
5309 * \c psa_crypto_init().
5310 * * The operation state is not valid: it must be inactive.
5311 */
5312
5313psa_status_t psa_key_agreement_iop_setup(
5314 psa_key_agreement_iop_t *operation,
5315 mbedtls_svc_key_id_t private_key,
5316 const uint8_t *peer_key,
5317 size_t peer_key_length,
5318 psa_algorithm_t alg,
5319 const psa_key_attributes_t *attributes);
5320
5321/**
5322 * \brief Continue and eventually complete the action of
5323 * key agreement, in an interruptible
5324 * manner.
5325 *
5326 * \see \c psa_key_agreement_iop_setup()
5327 *
5328 * \warning This is a beta API, and thus subject to change
5329 * at any point. It is not bound by the usual
5330 * interface stability promises.
5331 *
5332 * \note This function combined with \c
5333 * psa_key_agreement_iop_setup() is equivalent to
5334 * \c psa_raw_key_agreement() but this
5335 * function can return early and resume according
5336 * to the limit set with \c
5337 * psa_interruptible_set_max_ops() to reduce the
5338 * maximum time spent in a function call.
5339 *
5340 * \note Users should call this function on the same
5341 * operation object repeatedly while it returns
5342 * #PSA_OPERATION_INCOMPLETE, stopping when it
5343 * returns either #PSA_SUCCESS or an error.
5344 * Alternatively users can call
5345 * \c psa_key_agreement_iop_abort() at any point if
5346 * they no longer want the result.
5347 *
5348 * \note When this function returns successfully, the
5349 * operation becomes inactive. If this function
5350 * returns an error status, the operation enters an
5351 * error state and must be aborted by calling
5352 * \c psa_key_agreement_iop_abort().
5353 *
5354 * \param[in, out] operation The \c psa_key_agreement_iop_t to use. This must
5355 * be initialized first, and have had \c
5356 * psa_key_agreement_iop_start() called with it
5357 * first.
5358 *
5359 * \param[out] key On success, an identifier for the newly created
5360 * key. On failure this will be set to
5361 * #PSA_KEY_ID_NULL.
5362 *
5363 * \retval #PSA_SUCCESS
5364 * The operation is complete and \p key contains the shared secret.
5365 * If the key is persistent, the key material and the key's metadata
5366 * have been saved to persistent storage.
5367 *
5368 * \retval #PSA_OPERATION_INCOMPLETE
5369 * Operation was interrupted due to the setting of \c
5370 * psa_interruptible_set_max_ops(). There is still work to be done.
5371 * Call this function again with the same operation object.
5372 *
5373 * \retval #PSA_ERROR_ALREADY_EXISTS
5374 * This is an attempt to create a persistent key, and there is already a
5375 * persistent key with the given identifier.
5376 * \retval #PSA_ERROR_INVALID_SIGNATURE
5377 * The calculation was performed successfully, but the passed
5378 * signature is not a valid signature.
5379 * \retval #PSA_ERROR_BAD_STATE
5380 * An operation was not previously started on this context via
5381 * \c psa_key_agreement_iop_start().
5382 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
5383 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
5384 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5385 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5386 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
5387 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5388 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5389 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5390 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5391 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
5392 * \retval #PSA_ERROR_BAD_STATE
5393 * The following conditions can result in this error:
5394 * * The library has not been previously initialized by
5395 * \c psa_crypto_init().
5396 * * The operation state is not valid: it must be inactive.
5397 */
5398psa_status_t psa_key_agreement_iop_complete(
5399 psa_key_agreement_iop_t *operation,
5400 mbedtls_svc_key_id_t *key);
5401
5402/**
5403 * \brief Abort a key agreement operation.
5404 *
5405 * \warning This is a beta API, and thus subject to change
5406 * at any point. It is not bound by the usual
5407 * interface stability promises.
5408 *
5409 * \note This function clears the number of ops completed
5410 * as part of the operation. Please ensure you copy
5411 * this value via
5412 * \c psa_key_agreement_iop_get_num_ops() if
5413 * required before calling.
5414 *
5415 * \note Aborting an operation frees all
5416 * associated resources except for the operation
5417 * structure itself. Once aborted, the operation
5418 * object can be reused for another operation by
5419 * calling \c psa_key_agreement_iop_setup() again.
5420 *
5421 * \note You may call this function any time after the
5422 * operation object has been initialized.
5423 * In particular, calling \c
5424 * psa_key_agreement_iop_abort() after the
5425 * operation has already been terminated by a call
5426 * to \c psa_key_agreement_iop_abort() or
5427 * psa_key_agreement_iop_complete() is safe.
5428 *
5429 * \param[in,out] operation The \c psa_key_agreement_iop_t to use
5430 *
5431 * \retval #PSA_SUCCESS
5432 * The operation was aborted successfully.
5433 * \retval #PSA_ERROR_BAD_STATE
5434 * The library has not been previously initialized by
5435 * \c psa_crypto_init().
5436 */
5437psa_status_t psa_key_agreement_iop_abort(
5438 psa_key_agreement_iop_t *operation);
5439
5440/**@}*/
5441
5442/**
5443 * \defgroup interruptible_generate_key Interruptible Key Generation
5444 * @{
5445 */
5446
5447/**
5448 * The type of the state data structure for interruptible key generation
5449 * operations.
5450 *
5451 * Before calling any function on an interruptible key generation object, the
5452 * application must initialize it by any of the following means:
5453 * - Set the structure to all-bits-zero, for example:
5454 * \code
5455 * psa_generate_key_iop_t operation;
5456 * memset(&operation, 0, sizeof(operation));
5457 * \endcode
5458 * - Initialize the structure to logical zero values, for example:
5459 * \code
5460 * psa_generate_key_iop_t operation = {0};
5461 * \endcode
5462 * - Initialize the structure to the initializer #PSA_GENERATE_KEY_IOP_INIT,
5463 * for example:
5464 * \code
5465 * psa_generate_key_iop_t operation = PSA_GENERATE_KEY_IOP_INIT;
5466 * \endcode
5467 * - Assign the result of the function psa_generate_key_iop_init() to the
5468 * structure, for example:
5469 * \code
5470 * psa_generate_key_iop_t operation;
5471 * operation = psa_generate_key_iop_init();
5472 * \endcode
5473 *
5474 * This is an implementation-defined \c struct. Applications should not
5475 * make any assumptions about the content of this structure.
5476 * Implementation details can change in future versions without notice.
5477 */
5478typedef struct psa_generate_key_iop_s psa_generate_key_iop_t;
5479
5480/**
5481 * \brief Get the number of ops that a key generation
5482 * operation has taken so far. If the operation has
5483 * completed, then this will represent the number
5484 * of ops required for the entire operation. After
5485 * initialization or calling \c
5486 * psa_generate_key_iop_abort() on the operation,
5487 * a value of 0 will be returned.
5488 *
5489 * \warning This is a beta API, and thus subject to change
5490 * at any point. It is not bound by the usual
5491 * interface stability promises.
5492 * This is a helper provided to help you tune the
5493 * value passed to \c
5494 * psa_interruptible_set_max_ops().
5495 *
5496 * \param operation The \c psa_generate_key_iop_t to use. This must
5497 * be initialized first.
5498 *
5499 * \return Number of ops that the operation has taken so
5500 * far.
5501 */
5502uint32_t psa_generate_key_iop_get_num_ops(psa_generate_key_iop_t *operation);
5503
5504/**
5505 * \brief Start a key generation operation, in an
5506 * interruptible manner.
5507 *
5508 * \see \c psa_generate_key_iop_complete()
5509 *
5510 * \warning This is a beta API, and thus subject to change
5511 * at any point. It is not bound by the usual
5512 * interface stability promises.
5513 *
5514 * \note This function combined with \c
5515 * psa_generate_key_iop_complete() is equivalent
5516 * to \c psa_generate_key() but \c
5517 * psa_generate_key_iop_complete() can return
5518 * early and resume according to the limit set with
5519 * \c psa_interruptible_set_max_ops() to reduce the
5520 * maximum time spent in a function.
5521 *
5522 * \note Users should call
5523 * \c psa_generate_key_iop_complete() repeatedly
5524 * on the same operation object after a successful
5525 * call to this function until \c
5526 * psa_generate_key_iop_complete() either returns
5527 * #PSA_SUCCESS or an error.
5528 * \c psa_generate_key_iop_complete() will return
5529 * #PSA_OPERATION_INCOMPLETE if there is more work
5530 * to do. Alternatively users can call
5531 * \c psa_generate_key_iop_abort() at any point
5532 * if they no longer want the result.
5533 *
5534 * \note This function clears the number of ops completed
5535 * as part of the operation. Please ensure you copy
5536 * this value via
5537 * \c psa_generate_key_iop_get_num_ops() if
5538 * required before calling.
5539 *
5540 * \note If this function returns an error status, the
5541 * operation enters an error state and must be
5542 * aborted by calling \c
5543 * psa_generate_key_iop_abort().
5544 *
5545 * \note Only asymmetric key pairs are supported. (See \p attributes.)
5546 *
5547 * \param[in, out] operation The \c psa_generate_key_iop_t to use.
5548 * This must be initialized as per the
5549 * documentation for
5550 * \c psa_generate_key_iop_t, and be inactive.
5551 *
5552 * \param[in] attributes The attributes for the new key.
5553 * The following attributes are required for all
5554 * keys:
5555 * * The key type. It must be an asymmetric key-pair.
5556 * * The key size. It must be a valid size for the
5557 * key type.
5558 * The following attributes must be set for keys
5559 * used in cryptographic operations:
5560 * * The key permitted-algorithm policy.
5561 * * The key usage flags.
5562 * The following attributes must be set for keys
5563 * that do not use the default volatile lifetime:
5564 * * The key lifetime.
5565 * * The key identifier is required for a key with
5566 * a persistent lifetime,
5567 *
5568 * \note \p attributes is an input parameter, it is not
5569 * updated with the final key attributes. The final
5570 * attributes of the new key can be queried by
5571 * calling `psa_get_key_attributes()` with
5572 * the key's identifier.
5573 *
5574 * \retval #PSA_SUCCESS
5575 * The operation started successfully.
5576 * Call \c psa_generate_key_iop_complete() with the same context to
5577 * complete the operation.
5578 *
5579 * \retval PSA_ERROR_ALREADY_EXISTS
5580 * This is an attempt to create a persistent key, and there is already
5581 * a persistent key with the given identifier
5582 * \retval PSA_ERROR_NOT_SUPPORTED
5583 * The key attributes, as a whole, are not supported, either in general
5584 * or in the specified storage location.
5585 * \retval PSA_ERROR_INVALID_ARGUMENT
5586 * The following conditions can result in this error:
5587 * * The key type is invalid, or is an asymmetric public key type.
5588 * * The key size is not valid for the key type.
5589 * * The key lifetime is invalid.
5590 * * The key identifier is not valid for the key lifetime.
5591 * * The key usage flags include invalid values.
5592 * * The key's permitted-usage algorithm is invalid.
5593 * * The key attributes, as a whole, are invalid.
5594 * \retval PSA_ERROR_NOT_PERMITTED
5595 * Creating a key with the specified attributes is not permitted.
5596 *
5597 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5598 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5599 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
5600 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5601 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5602 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5603 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5604 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
5605 * \retval #PSA_ERROR_BAD_STATE
5606 * The following conditions can result in this error:
5607 * * The library has not been previously initialized by
5608 * \c psa_crypto_init().
5609 * * The operation state is not valid: it must be inactive.
5610 */
5611
5612psa_status_t psa_generate_key_iop_setup(
5613 psa_generate_key_iop_t *operation,
5614 const psa_key_attributes_t *attributes);
5615
5616/**
5617 * \brief Continue and eventually complete the action of
5618 * key generation, in an interruptible
5619 * manner.
5620 *
5621 * \see \c psa_generate_key_iop_setup()
5622 *
5623 * \warning This is a beta API, and thus subject to change
5624 * at any point. It is not bound by the usual
5625 * interface stability promises.
5626 *
5627 * \note This function combined with \c
5628 * psa_generate_key_iop_setup() is equivalent to
5629 * \c psa_generate_key() but this
5630 * function can return early and resume according
5631 * to the limit set with \c
5632 * psa_interruptible_set_max_ops() to reduce the
5633 * maximum time spent in a function call.
5634 *
5635 * \note Users should call this function on the same
5636 * operation object repeatedly whilst it returns
5637 * #PSA_OPERATION_INCOMPLETE, stopping when it
5638 * returns either #PSA_SUCCESS or an error.
5639 * Alternatively users can call
5640 * \c psa_generate_key_iop_abort() at any
5641 * point if they no longer want the result.
5642 *
5643 * \note When this function returns successfully, the
5644 * operation becomes inactive. If this function
5645 * returns an error status, the operation enters an
5646 * error state and must be aborted by calling
5647 * \c psa_generate_key_iop_abort().
5648 *
5649 * \param[in, out] operation The \c psa_generate_key_iop_t to use.
5650 * This must be initialized first, and have had \c
5651 * psa_generate_key_iop_setup() called
5652 * with it first.
5653 *
5654 * \param[out] key On success, an identifier for the newly created
5655 * key, on failure this will be set to
5656 * #PSA_KEY_ID_NULL.
5657 *
5658 * \retval #PSA_SUCCESS
5659 * The operation is complete and \p key contains the new key.
5660 * If the key is persistent, the key material and the key's metadata
5661 * have been saved to persistent storage.
5662 *
5663 * \retval #PSA_OPERATION_INCOMPLETE
5664 * Operation was interrupted due to the setting of \c
5665 * psa_interruptible_set_max_ops(). There is still work to be done.
5666 * Call this function again with the same operation object.
5667 *
5668 * \retval #PSA_ERROR_ALREADY_EXISTS
5669 * This is an attempt to create a persistent key, and there is already a
5670 * persistent key with the given identifier.
5671 *
5672 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
5673 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
5674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5676 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
5677 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5678 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5679 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5680 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5681 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
5682 * \retval #PSA_ERROR_BAD_STATE
5683 * The following conditions can result in this error:
5684 * * The library has not been previously initialized by
5685 * \c psa_crypto_init().
5686 * * The operation state is not valid: it must be active.
5687 */
5688psa_status_t psa_generate_key_iop_complete(
5689 psa_generate_key_iop_t *operation,
5690 mbedtls_svc_key_id_t *key);
5691
5692/**
5693 * \brief Abort a key generation operation.
5694 *
5695 * \warning This is a beta API, and thus subject to change
5696 * at any point. It is not bound by the usual
5697 * interface stability promises.
5698 *
5699 * \note This function clears the number of ops completed
5700 * as part of the operation. Please ensure you copy
5701 * this value via
5702 * \c psa_generate_key_iop_get_num_ops() if
5703 * required before calling.
5704 *
5705 * \note Aborting an operation frees all
5706 * associated resources except for the operation
5707 * structure itself. Once aborted, the operation
5708 * object can be reused for another operation by
5709 * calling \c psa_generate_key_iop_setup() again.
5710 *
5711 * \note You may call this function any time after the
5712 * operation object has been initialized.
5713 * In particular, calling \c
5714 * psa_generate_key_iop_abort() after the
5715 * operation has already been terminated by a call
5716 * to \c psa_generate_key_iop_abort() or
5717 * \c psa_generate_key_iop_complete() is safe.
5718 *
5719 * \param[in,out] operation The \c psa_key_agreement_iop_t to use
5720 *
5721 * \retval #PSA_SUCCESS
5722 * The operation was aborted successfully.
5723 * \retval #PSA_ERROR_BAD_STATE
5724 * The library has not been previously initialized by
5725 * \c psa_crypto_init().
5726 */
5727psa_status_t psa_generate_key_iop_abort(
5728 psa_generate_key_iop_t *operation);
5729
5730/**@}*/
5731
5732/**
5733 * \defgroup interruptible_export_public_key Interruptible public-key export
5734 * @{
5735 */
5736
5737/**
5738 * The type of the state data structure for interruptible public-key export
5739 * operations.
5740 *
5741 * Before calling any function on an interruptible export public-key object, the
5742 * application must initialize it by any of the following means:
5743 * - Set the structure to all-bits-zero, for example:
5744 * \code
5745 * psa_export_public_key_iop_t operation;
5746 * memset(&operation, 0, sizeof(operation));
5747 * \endcode
5748 * - Initialize the structure to logical zero values, for example:
5749 * \code
5750 * psa_export_public_key_iop_t operation = {0};
5751 * \endcode
5752 * - Initialize the structure to the initializer #PSA_EXPORT_PUBLIC_KEY_IOP_INIT,
5753 * for example:
5754 * \code
5755 * psa_export_public_key_iop_t operation = PSA_EXPORT_PUBLIC_KEY_IOP_INIT;
5756 * \endcode
5757 * - Assign the result of the function psa_export_public_key_iop_init() to the
5758 * structure, for example:
5759 * \code
5760 * psa_export_public_key_iop_t operation;
5761 * operation = psa_export_public_key_iop_init();
5762 * \endcode
5763 *
5764 * This is an implementation-defined \c struct. Applications should not
5765 * make any assumptions about the content of this structure.
5766 * Implementation details can change in future versions without notice.
5767 */
5768typedef struct psa_export_public_key_iop_s psa_export_public_key_iop_t;
5769
5770/**
5771 * \brief Get the number of ops that an export public-key
5772 * operation has taken so far. If the operation has
5773 * completed, then this will represent the number
5774 * of ops required for the entire operation. After
5775 * initialization or calling
5776 * \c psa_export_public_key_iop_abort() on the operation,
5777 * a value of 0 will be returned.
5778 *
5779 * \warning This is a beta API, and thus subject to change
5780 * at any point. It is not bound by the usual
5781 * interface stability promises.
5782 * This is a helper provided to help you tune the
5783 * value passed to
5784 * \c psa_interruptible_set_max_ops().
5785 *
5786 * \param operation The \c psa_export_public_key_iop_t to use. This must
5787 * be initialized first.
5788 *
5789 * \return Number of ops that the operation has taken so
5790 * far.
5791 */
5792uint32_t psa_export_public_key_iop_get_num_ops(psa_export_public_key_iop_t *operation);
5793
5794/**
5795 * \brief Start an interruptible operation to export a
5796 * public key or the public part of a key pair in
5797 * binary format.
5798
5799 *
5800 * \see \c psa_export_public_key_iop_complete()
5801 *
5802 * \warning This is a beta API, and thus subject to change
5803 * at any point. It is not bound by the usual
5804 * interface stability promises.
5805 *
5806 * \note This function combined with
5807 * \c psa_export_public_key_iop_complete() is equivalent
5808 * to \c psa_export_public_key() but
5809 * \c psa_export_public_key_iop_complete() can return
5810 * early and resume according to the limit set with
5811 * \c psa_interruptible_set_max_ops() to reduce the
5812 * maximum time spent in a function.
5813 *
5814 * \note Users should call
5815 * \c psa_export_public_key_iop_complete() repeatedly
5816 * on the same operation object after a successful
5817 * call to this function until
5818 * \c psa_export_public_key_iop_complete() either returns
5819 * #PSA_SUCCESS or an error.
5820 * \c psa_export_public_key_iop_complete() will return
5821 * #PSA_OPERATION_INCOMPLETE if there is more work
5822 * to do. Alternatively users can call
5823 * \c psa_export_public_key_iop_abort() at any point
5824 * if they no longer want the result.
5825 *
5826 * \note This function clears the number of ops completed
5827 * as part of the operation. Please ensure you copy
5828 * this value via
5829 * \c psa_export_public_key_iop_get_num_ops() if
5830 * required before calling.
5831 *
5832 * \note If this function returns an error status, the
5833 * operation enters an error state and must be
5834 * aborted by calling
5835 * \c psa_export_public_key_iop_abort().
5836 *
5837 * \param[in, out] operation The \c psa_export_public_key_iop_t to use.
5838 * This must be initialized as per the
5839 * documentation for
5840 * \c psa_export_public_key_iop_t, and be inactive.
5841 *
5842 * \param[in] key Identifier of the key to export.
5843 *
5844 * \retval #PSA_SUCCESS
5845 * The operation started successfully.
5846 * Call \c psa_export_public_key_iop_complete() with the same context to
5847 * complete the operation.
5848 * \retval #PSA_ERROR_INVALID_HANDLE
5849 * \c key is not a valid key identifier.
5850 * \retval #PSA_ERROR_INVALID_ARGUMENT
5851 * The key is neither a public key nor a key pair.
5852 * \retval #PSA_ERROR_NOT_SUPPORTED
5853 * The following conditions can result in this error:
5854 * * The key's storage location does not support export of the key.
5855 * * The implementation does not support export of keys with this key type.
5856 * \retval #PSA_ERROR_BAD_STATE
5857 * The following conditions can result in this error:
5858 * * The library has not been previously initialized by
5859 * \c psa_crypto_init().
5860 * * The operation state is not valid: it must be inactive.
5861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5862 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5863 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5864 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5865 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5867 */
5868psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operation,
5869 mbedtls_svc_key_id_t key);
5870
5871/**
5872 * \brief Continue and eventually complete the action of
5873 * exporting a public key, in an interruptible
5874 * manner.
5875 * \see \c psa_export_public_key_iop_setup()
5876 *
5877 * \warning This is a beta API, and thus subject to change
5878 * at any point. It is not bound by the usual
5879 * interface stability promises.
5880 *
5881 * \note This function combined with
5882 * \c psa_export_public_key_iop_setup() is equivalent to
5883 * \c psa_export_public_key() but this
5884 * function can return early and resume according
5885 * to the limit set with
5886 * \c psa_interruptible_set_max_ops() to reduce the
5887 * maximum time spent in a function call.
5888 *
5889 * \note Users should call this function on the same
5890 * operation object repeatedly whilst it returns
5891 * #PSA_OPERATION_INCOMPLETE, stopping when it
5892 * returns either #PSA_SUCCESS or an error.
5893 * Alternatively users can call
5894 * \c psa_export_public_key_iop_abort() at any
5895 * point if they no longer want the result.
5896 *
5897 * \note When this function returns successfully, the
5898 * operation becomes inactive. If this function
5899 * returns an error status, the operation enters an
5900 * error state and must be aborted by calling
5901 * \c psa_export_public_key_iop_abort().
5902 *
5903 * \param[in, out] operation The \c psa_export_public_key_iop_t to use.
5904 * This must be initialized first, and have had
5905 * \c psa_export_public_key_iop_setup() called
5906 * with it first.
5907 *
5908 * \param[out] data Buffer where the key data is to be written.
5909 *
5910 * \param[in] data_size Size of the \c data buffer in bytes.
5911 * This must be appropriate for the key:
5912 * * The required output size is
5913 * \c PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits)
5914 * where type is the key type and bits is the key
5915 * size in bits.
5916 * * \c PSA_EXPORT_PUBLIC_KEY_MAX_SIZE evaluates to the maximum
5917 * output size of any supported public key or public part
5918 * of a key pair.
5919 *
5920 * \param[out] data_length On success, the number of bytes that make up the key data.
5921 *
5922 * \retval #PSA_SUCCESS
5923 * Success. The first (*\c data_length) bytes of data contain the exported
5924 public key.
5925 * \retval #PSA_ERROR_BAD_STATE
5926 * The following conditions can result in this error:
5927 * * The library has not been previously initialized by
5928 * \c psa_crypto_init().
5929 * * The operation state is not valid: it must be active.
5930 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
5931 * The size of the data buffer is too small.
5932 * \c PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(),
5933 * \c PSA_EXPORT_PUBLIC_KEY_MAX_SIZE.
5934 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
5935 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
5936 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5937 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
5938 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
5939 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
5940 * \retval #PSA_OPERATION_INCOMPLETE
5941 * Operation was interrupted due to the setting of
5942 * \c psa_interruptible_set_max_ops(). There is still work to be done.
5943 * Call this function again with the same operation object.
5944 */
5945psa_status_t psa_export_public_key_iop_complete(psa_export_public_key_iop_t *operation,
5946 uint8_t *data,
5947 size_t data_size,
5948 size_t *data_length);
5949
5950/**
5951 * \brief Abort an interruptible public-key export operation.
5952 *
5953 * \warning This is a beta API, and thus subject to change
5954 * at any point. It is not bound by the usual
5955 * interface stability promises.
5956 *
5957 * \note This function clears the number of ops completed
5958 * as part of the operation. Please ensure you copy
5959 * this value via
5960 * \c psa_export_public_key_iop_get_num_ops() if
5961 * required before calling.
5962 *
5963 * \note Aborting an operation frees all
5964 * associated resources except for the operation
5965 * structure itself. Once aborted, the operation
5966 * object can be reused for another operation by
5967 * calling \c psa_export_public_key_iop_setup() again.
5968 *
5969 * \note You may call this function any time after the
5970 * operation object has been initialized.
5971 * In particular, calling
5972 * \c psa_export_public_key_iop_abort() after the
5973 * operation has already been terminated by a call
5974 * to \c psa_export_public_key_iop_abort() or
5975 * \c psa_export_public_key_iop_complete() is safe.
5976 *
5977 * \param[in,out] operation The \c psa_export_public_key_iop_t to use
5978 *
5979 * \retval #PSA_SUCCESS
5980 * The operation was aborted successfully.
5981 * \retval #PSA_ERROR_BAD_STATE
5982 * The library has not been previously initialized by
5983 * \c psa_crypto_init().
5984 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
5985 *
5986 */
5987psa_status_t psa_export_public_key_iop_abort(psa_export_public_key_iop_t *operation);
5988
5989/**@}*/
5990
5991#ifdef __cplusplus
5992}
5993#endif
5994
5995/* The file "crypto_extra.h" contains vendor-specific definitions. This
5996 * can include vendor-defined algorithms, extra functions, etc. */
5997#include "crypto_extra.h"
5998
5999#endif /* PSA_CRYPTO_H */