1 /*
2  * Copyright (c) 2018-2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 /**
8  * \file psa/crypto_values.h
9  *
10  * \brief PSA cryptography module: macros to build and analyze integer values.
11  *
12  * \note This file may not be included directly. Applications must
13  * include psa/crypto.h. Drivers must include the appropriate driver
14  * header file.
15  *
16  * This file contains portable definitions of macros to build and analyze
17  * values of integral types that encode properties of cryptographic keys,
18  * designations of cryptographic algorithms, and error codes returned by
19  * the library.
20  *
21  * Note that many of the constants defined in this file are embedded in
22  * the persistent key store, as part of key metadata (including usage
23  * policies). As a consequence, they must not be changed (unless the storage
24  * format version changes).
25  *
26  * This header file only defines preprocessor macros.
27  */
28 
29 #ifndef PSA_CRYPTO_VALUES_H
30 #define PSA_CRYPTO_VALUES_H
31 
32 /** \defgroup error Error codes
33  * @{
34  */
35 
36 /* PSA error codes */
37 
38 /* Error codes are standardized across PSA domains (framework, crypto, storage,
39  * etc.). Do not change the values in this section or even the expansions
40  * of each macro: it must be possible to `#include` both this header
41  * and some other PSA component's headers in the same C source,
42  * which will lead to duplicate definitions of the `PSA_SUCCESS` and
43  * `PSA_ERROR_xxx` macros, which is ok if and only if the macros expand
44  * to the same sequence of tokens.
45  *
46  * If you must add a new
47  * value, check with the Arm PSA framework group to pick one that other
48  * domains aren't already using. */
49 
50 /* Tell uncrustify not to touch the constant definitions, otherwise
51  * it might change the spacing to something that is not PSA-compliant
52  * (e.g. adding a space after casts).
53  *
54  * *INDENT-OFF*
55  */
56 
57 /** The action was completed successfully. */
58 #ifndef PSA_SUCCESS
59 #define PSA_SUCCESS ((psa_status_t)0)
60 #endif
61 
62 /** An error occurred that does not correspond to any defined
63  * failure cause.
64  *
65  * Implementations may use this error code if none of the other standard
66  * error codes are applicable. */
67 #define PSA_ERROR_GENERIC_ERROR         ((psa_status_t)-132)
68 
69 /** The requested operation or a parameter is not supported
70  * by this implementation.
71  *
72  * Implementations should return this error code when an enumeration
73  * parameter such as a key type, algorithm, etc. is not recognized.
74  * If a combination of parameters is recognized and identified as
75  * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
76 #define PSA_ERROR_NOT_SUPPORTED         ((psa_status_t)-134)
77 
78 /** The requested action is denied by a policy.
79  *
80  * Implementations should return this error code when the parameters
81  * are recognized as valid and supported, and a policy explicitly
82  * denies the requested operation.
83  *
84  * If a subset of the parameters of a function call identify a
85  * forbidden operation, and another subset of the parameters are
86  * not valid or not supported, it is unspecified whether the function
87  * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
88  * #PSA_ERROR_INVALID_ARGUMENT. */
89 #define PSA_ERROR_NOT_PERMITTED         ((psa_status_t)-133)
90 
91 /** An output buffer is too small.
92  *
93  * Applications can call the \c PSA_xxx_SIZE macro listed in the function
94  * description to determine a sufficient buffer size.
95  *
96  * Implementations should preferably return this error code only
97  * in cases when performing the operation with a larger output
98  * buffer would succeed. However implementations may return this
99  * error if a function has invalid or unsupported parameters in addition
100  * to the parameters that determine the necessary output buffer size. */
101 #define PSA_ERROR_BUFFER_TOO_SMALL      ((psa_status_t)-138)
102 
103 /** Asking for an item that already exists
104  *
105  * Implementations should return this error, when attempting
106  * to write an item (like a key) that already exists. */
107 #define PSA_ERROR_ALREADY_EXISTS        ((psa_status_t)-139)
108 
109 /** Asking for an item that doesn't exist
110  *
111  * Implementations should return this error, if a requested item (like
112  * a key) does not exist. */
113 #define PSA_ERROR_DOES_NOT_EXIST        ((psa_status_t)-140)
114 
115 /** The requested action cannot be performed in the current state.
116  *
117  * Multipart operations return this error when one of the
118  * functions is called out of sequence. Refer to the function
119  * descriptions for permitted sequencing of functions.
120  *
121  * Implementations shall not return this error code to indicate
122  * that a key either exists or not,
123  * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
124  * as applicable.
125  *
126  * Implementations shall not return this error code to indicate that a
127  * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
128  * instead. */
129 #define PSA_ERROR_BAD_STATE             ((psa_status_t)-137)
130 
131 /** The parameters passed to the function are invalid.
132  *
133  * Implementations may return this error any time a parameter or
134  * combination of parameters are recognized as invalid.
135  *
136  * Implementations shall not return this error code to indicate that a
137  * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
138  * instead.
139  */
140 #define PSA_ERROR_INVALID_ARGUMENT      ((psa_status_t)-135)
141 
142 /** There is not enough runtime memory.
143  *
144  * If the action is carried out across multiple security realms, this
145  * error can refer to available memory in any of the security realms. */
146 #define PSA_ERROR_INSUFFICIENT_MEMORY   ((psa_status_t)-141)
147 
148 /** There is not enough persistent storage.
149  *
150  * Functions that modify the key storage return this error code if
151  * there is insufficient storage space on the host media. In addition,
152  * many functions that do not otherwise access storage may return this
153  * error code if the implementation requires a mandatory log entry for
154  * the requested action and the log storage space is full. */
155 #define PSA_ERROR_INSUFFICIENT_STORAGE  ((psa_status_t)-142)
156 
157 /** There was a communication failure inside the implementation.
158  *
159  * This can indicate a communication failure between the application
160  * and an external cryptoprocessor or between the cryptoprocessor and
161  * an external volatile or persistent memory. A communication failure
162  * may be transient or permanent depending on the cause.
163  *
164  * \warning If a function returns this error, it is undetermined
165  * whether the requested action has completed or not. Implementations
166  * should return #PSA_SUCCESS on successful completion whenever
167  * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
168  * if the requested action was completed successfully in an external
169  * cryptoprocessor but there was a breakdown of communication before
170  * the cryptoprocessor could report the status to the application.
171  */
172 #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
173 
174 /** There was a storage failure that may have led to data loss.
175  *
176  * This error indicates that some persistent storage is corrupted.
177  * It should not be used for a corruption of volatile memory
178  * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
179  * between the cryptoprocessor and its external storage (use
180  * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
181  * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
182  *
183  * Note that a storage failure does not indicate that any data that was
184  * previously read is invalid. However this previously read data may no
185  * longer be readable from storage.
186  *
187  * When a storage failure occurs, it is no longer possible to ensure
188  * the global integrity of the keystore. Depending on the global
189  * integrity guarantees offered by the implementation, access to other
190  * data may or may not fail even if the data is still readable but
191  * its integrity cannot be guaranteed.
192  *
193  * Implementations should only use this error code to report a
194  * permanent storage corruption. However application writers should
195  * keep in mind that transient errors while reading the storage may be
196  * reported using this error code. */
197 #define PSA_ERROR_STORAGE_FAILURE       ((psa_status_t)-146)
198 
199 /** A hardware failure was detected.
200  *
201  * A hardware failure may be transient or permanent depending on the
202  * cause. */
203 #define PSA_ERROR_HARDWARE_FAILURE      ((psa_status_t)-147)
204 
205 /** A tampering attempt was detected.
206  *
207  * If an application receives this error code, there is no guarantee
208  * that previously accessed or computed data was correct and remains
209  * confidential. Applications should not perform any security function
210  * and should enter a safe failure state.
211  *
212  * Implementations may return this error code if they detect an invalid
213  * state that cannot happen during normal operation and that indicates
214  * that the implementation's security guarantees no longer hold. Depending
215  * on the implementation architecture and on its security and safety goals,
216  * the implementation may forcibly terminate the application.
217  *
218  * This error code is intended as a last resort when a security breach
219  * is detected and it is unsure whether the keystore data is still
220  * protected. Implementations shall only return this error code
221  * to report an alarm from a tampering detector, to indicate that
222  * the confidentiality of stored data can no longer be guaranteed,
223  * or to indicate that the integrity of previously returned data is now
224  * considered compromised. Implementations shall not use this error code
225  * to indicate a hardware failure that merely makes it impossible to
226  * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
227  * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
228  * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
229  * instead).
230  *
231  * This error indicates an attack against the application. Implementations
232  * shall not return this error code as a consequence of the behavior of
233  * the application itself. */
234 #define PSA_ERROR_CORRUPTION_DETECTED    ((psa_status_t)-151)
235 
236 /** There is not enough entropy to generate random data needed
237  * for the requested action.
238  *
239  * This error indicates a failure of a hardware random generator.
240  * Application writers should note that this error can be returned not
241  * only by functions whose purpose is to generate random data, such
242  * as key, IV or nonce generation, but also by functions that execute
243  * an algorithm with a randomized result, as well as functions that
244  * use randomization of intermediate computations as a countermeasure
245  * to certain attacks.
246  *
247  * Implementations should avoid returning this error after psa_crypto_init()
248  * has succeeded. Implementations should generate sufficient
249  * entropy during initialization and subsequently use a cryptographically
250  * secure pseudorandom generator (PRNG). However implementations may return
251  * this error at any time if a policy requires the PRNG to be reseeded
252  * during normal operation. */
253 #define PSA_ERROR_INSUFFICIENT_ENTROPY  ((psa_status_t)-148)
254 
255 /** The signature, MAC or hash is incorrect.
256  *
257  * Verification functions return this error if the verification
258  * calculations completed successfully, and the value to be verified
259  * was determined to be incorrect.
260  *
261  * If the value to verify has an invalid size, implementations may return
262  * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
263 #define PSA_ERROR_INVALID_SIGNATURE     ((psa_status_t)-149)
264 
265 /** The decrypted padding is incorrect.
266  *
267  * \warning In some protocols, when decrypting data, it is essential that
268  * the behavior of the application does not depend on whether the padding
269  * is correct, down to precise timing. Applications should prefer
270  * protocols that use authenticated encryption rather than plain
271  * encryption. If the application must perform a decryption of
272  * unauthenticated data, the application writer should take care not
273  * to reveal whether the padding is invalid.
274  *
275  * Implementations should strive to make valid and invalid padding
276  * as close as possible to indistinguishable to an external observer.
277  * In particular, the timing of a decryption operation should not
278  * depend on the validity of the padding. */
279 #define PSA_ERROR_INVALID_PADDING       ((psa_status_t)-150)
280 
281 /** Return this error when there's insufficient data when attempting
282  * to read from a resource. */
283 #define PSA_ERROR_INSUFFICIENT_DATA     ((psa_status_t)-143)
284 
285 /** The key identifier is not valid. See also :ref:\`key-handles\`.
286  */
287 #define PSA_ERROR_INVALID_HANDLE        ((psa_status_t)-136)
288 
289 /** Stored data has been corrupted.
290  *
291  * This error indicates that some persistent storage has suffered corruption.
292  * It does not indicate the following situations, which have specific error
293  * codes:
294  *
295  * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
296  * - A communication error between the cryptoprocessor and its external
297  *   storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
298  * - When the storage is in a valid state but is full - use
299  *   #PSA_ERROR_INSUFFICIENT_STORAGE.
300  * - When the storage fails for other reasons - use
301  *   #PSA_ERROR_STORAGE_FAILURE.
302  * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
303  *
304  * \note A storage corruption does not indicate that any data that was
305  * previously read is invalid. However this previously read data might no
306  * longer be readable from storage.
307  *
308  * When a storage failure occurs, it is no longer possible to ensure the
309  * global integrity of the keystore.
310  */
311 #define PSA_ERROR_DATA_CORRUPT          ((psa_status_t)-152)
312 
313 /** Data read from storage is not valid for the implementation.
314  *
315  * This error indicates that some data read from storage does not have a valid
316  * format. It does not indicate the following situations, which have specific
317  * error codes:
318  *
319  * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
320  * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
321  * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
322  *
323  * This error is typically a result of either storage corruption on a
324  * cleartext storage backend, or an attempt to read data that was
325  * written by an incompatible version of the library.
326  */
327 #define PSA_ERROR_DATA_INVALID          ((psa_status_t)-153)
328 
329 /** The function that returns this status is defined as interruptible and
330  *  still has work to do, thus the user should call the function again with the
331  *  same operation context until it either returns #PSA_SUCCESS or any other
332  *  error. This is not an error per se, more a notification of status.
333  */
334 #define PSA_OPERATION_INCOMPLETE           ((psa_status_t)-248)
335 
336 /* *INDENT-ON* */
337 
338 /**@}*/
339 
340 /** \defgroup crypto_types Key and algorithm types
341  * @{
342  */
343 
344 /* Note that key type values, including ECC family and DH group values, are
345  * embedded in the persistent key store, as part of key metadata. As a
346  * consequence, they must not be changed (unless the storage format version
347  * changes).
348  */
349 
350 /** An invalid key type value.
351  *
352  * Zero is not the encoding of any key type.
353  */
354 #define PSA_KEY_TYPE_NONE                           ((psa_key_type_t)0x0000)
355 
356 /** Vendor-defined key type flag.
357  *
358  * Key types defined by this standard will never have the
359  * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
360  * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
361  * respect the bitwise structure used by standard encodings whenever practical.
362  */
363 #define PSA_KEY_TYPE_VENDOR_FLAG                    ((psa_key_type_t)0x8000)
364 
365 #define PSA_KEY_TYPE_CATEGORY_MASK                  ((psa_key_type_t)0x7000)
366 #define PSA_KEY_TYPE_CATEGORY_RAW                   ((psa_key_type_t)0x1000)
367 #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC             ((psa_key_type_t)0x2000)
368 #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY            ((psa_key_type_t)0x4000)
369 #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR              ((psa_key_type_t)0x7000)
370 
371 #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR             ((psa_key_type_t)0x3000)
372 
373 /** Whether a key type is vendor-defined.
374  *
375  * See also #PSA_KEY_TYPE_VENDOR_FLAG.
376  */
377 #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
378     (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
379 
380 /** Whether a key type is an unstructured array of bytes.
381  *
382  * This encompasses both symmetric keys and non-key data.
383  */
384 #define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
385     (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
386      ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
387 
388 /** Whether a key type is asymmetric: either a key pair or a public key. */
389 #define PSA_KEY_TYPE_IS_ASYMMETRIC(type)                                \
390     (((type) & PSA_KEY_TYPE_CATEGORY_MASK                               \
391       & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) ==                            \
392      PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
393 /** Whether a key type is the public part of a key pair. */
394 #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type)                                \
395     (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
396 /** Whether a key type is a key pair containing a private part and a public
397  * part. */
398 #define PSA_KEY_TYPE_IS_KEY_PAIR(type)                                   \
399     (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
400 /** The key pair type corresponding to a public key type.
401  *
402  * You may also pass a key pair type as \p type, it will be left unchanged.
403  *
404  * \param type      A public key type or key pair type.
405  *
406  * \return          The corresponding key pair type.
407  *                  If \p type is not a public key or a key pair,
408  *                  the return value is undefined.
409  */
410 #define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type)        \
411     ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
412 /** The public key type corresponding to a key pair type.
413  *
414  * You may also pass a key pair type as \p type, it will be left unchanged.
415  *
416  * \param type      A public key type or key pair type.
417  *
418  * \return          The corresponding public key type.
419  *                  If \p type is not a public key or a key pair,
420  *                  the return value is undefined.
421  */
422 #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type)        \
423     ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
424 
425 /** Raw data.
426  *
427  * A "key" of this type cannot be used for any cryptographic operation.
428  * Applications may use this type to store arbitrary data in the keystore. */
429 #define PSA_KEY_TYPE_RAW_DATA                       ((psa_key_type_t)0x1001)
430 
431 /** HMAC key.
432  *
433  * The key policy determines which underlying hash algorithm the key can be
434  * used for.
435  *
436  * HMAC keys should generally have the same size as the underlying hash.
437  * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
438  * \c alg is the HMAC algorithm or the underlying hash algorithm. */
439 #define PSA_KEY_TYPE_HMAC                           ((psa_key_type_t)0x1100)
440 
441 /** A secret for key derivation.
442  *
443  * This key type is for high-entropy secrets only. For low-entropy secrets,
444  * #PSA_KEY_TYPE_PASSWORD should be used instead.
445  *
446  * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
447  * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
448  *
449  * The key policy determines which key derivation algorithm the key
450  * can be used for.
451  */
452 #define PSA_KEY_TYPE_DERIVE                         ((psa_key_type_t)0x1200)
453 
454 /** A low-entropy secret for password hashing or key derivation.
455  *
456  * This key type is suitable for passwords and passphrases which are typically
457  * intended to be memorizable by humans, and have a low entropy relative to
458  * their size. It can be used for randomly generated or derived keys with
459  * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
460  * for such keys. It is not suitable for passwords with extremely low entropy,
461  * such as numerical PINs.
462  *
463  * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
464  * key derivation algorithms. Algorithms that accept such an input were
465  * designed to accept low-entropy secret and are known as password hashing or
466  * key stretching algorithms.
467  *
468  * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
469  * key derivation algorithms, as the algorithms that take such an input expect
470  * it to be high-entropy.
471  *
472  * The key policy determines which key derivation algorithm the key can be
473  * used for, among the permissible subset defined above.
474  */
475 #define PSA_KEY_TYPE_PASSWORD                       ((psa_key_type_t)0x1203)
476 
477 /** A secret value that can be used to verify a password hash.
478  *
479  * The key policy determines which key derivation algorithm the key
480  * can be used for, among the same permissible subset as for
481  * #PSA_KEY_TYPE_PASSWORD.
482  */
483 #define PSA_KEY_TYPE_PASSWORD_HASH                  ((psa_key_type_t)0x1205)
484 
485 /** A secret value that can be used in when computing a password hash.
486  *
487  * The key policy determines which key derivation algorithm the key
488  * can be used for, among the subset of algorithms that can use pepper.
489  */
490 #define PSA_KEY_TYPE_PEPPER                         ((psa_key_type_t)0x1206)
491 
492 /** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
493  *
494  * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
495  * 32 bytes (AES-256).
496  */
497 #define PSA_KEY_TYPE_AES                            ((psa_key_type_t)0x2400)
498 
499 /** Key for a cipher, AEAD or MAC algorithm based on the
500  * ARIA block cipher. */
501 #define PSA_KEY_TYPE_ARIA                           ((psa_key_type_t)0x2406)
502 
503 /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
504  *
505  * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
506  * 192 bits (3-key 3DES).
507  *
508  * Note that single DES and 2-key 3DES are weak and strongly
509  * deprecated and should only be used to decrypt legacy data. 3-key 3DES
510  * is weak and deprecated and should only be used in legacy protocols.
511  */
512 #define PSA_KEY_TYPE_DES                            ((psa_key_type_t)0x2301)
513 
514 /** Key for a cipher, AEAD or MAC algorithm based on the
515  * Camellia block cipher. */
516 #define PSA_KEY_TYPE_CAMELLIA                       ((psa_key_type_t)0x2403)
517 
518 /** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
519  *
520  * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
521  *
522  * \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports
523  *       12-byte nonces.
524  *
525  * \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt
526  *       with the initial counter value 1, you can process and discard a
527  *       64-byte block before the real data.
528  */
529 #define PSA_KEY_TYPE_CHACHA20                       ((psa_key_type_t)0x2004)
530 
531 /** RSA public key.
532  *
533  * The size of an RSA key is the bit size of the modulus.
534  */
535 #define PSA_KEY_TYPE_RSA_PUBLIC_KEY                 ((psa_key_type_t)0x4001)
536 /** RSA key pair (private and public key).
537  *
538  * The size of an RSA key is the bit size of the modulus.
539  */
540 #define PSA_KEY_TYPE_RSA_KEY_PAIR                   ((psa_key_type_t)0x7001)
541 /** Whether a key type is an RSA key (pair or public-only). */
542 #define PSA_KEY_TYPE_IS_RSA(type)                                       \
543     (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
544 
545 #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE            ((psa_key_type_t)0x4100)
546 #define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE              ((psa_key_type_t)0x7100)
547 #define PSA_KEY_TYPE_ECC_CURVE_MASK                 ((psa_key_type_t)0x00ff)
548 /** Elliptic curve key pair.
549  *
550  * The size of an elliptic curve key is the bit size associated with the curve,
551  * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
552  * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
553  *
554  * \param curve     A value of type ::psa_ecc_family_t that
555  *                  identifies the ECC curve to be used.
556  */
557 #define PSA_KEY_TYPE_ECC_KEY_PAIR(curve)         \
558     (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
559 /** Elliptic curve public key.
560  *
561  * The size of an elliptic curve public key is the same as the corresponding
562  * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
563  * `PSA_ECC_FAMILY_xxx` curve families).
564  *
565  * \param curve     A value of type ::psa_ecc_family_t that
566  *                  identifies the ECC curve to be used.
567  */
568 #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve)              \
569     (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
570 
571 /** Whether a key type is an elliptic curve key (pair or public-only). */
572 #define PSA_KEY_TYPE_IS_ECC(type)                                       \
573     ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) &                        \
574       ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
575 /** Whether a key type is an elliptic curve key pair. */
576 #define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)                               \
577     (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) ==                         \
578      PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
579 /** Whether a key type is an elliptic curve public key. */
580 #define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)                            \
581     (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) ==                         \
582      PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
583 
584 /** Extract the curve from an elliptic curve key type. */
585 #define PSA_KEY_TYPE_ECC_GET_FAMILY(type)                        \
586     ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ?             \
587                         ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
588                         0))
589 
590 /** Check if the curve of given family is Weierstrass elliptic curve. */
591 #define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
592 
593 /** SEC Koblitz curves over prime fields.
594  *
595  * This family comprises the following curves:
596  * secp192k1, secp224k1, secp256k1.
597  * They are defined in _Standards for Efficient Cryptography_,
598  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
599  * https://www.secg.org/sec2-v2.pdf
600  */
601 #define PSA_ECC_FAMILY_SECP_K1           ((psa_ecc_family_t) 0x17)
602 
603 /** SEC random curves over prime fields.
604  *
605  * This family comprises the following curves:
606  * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
607  * They are defined in _Standards for Efficient Cryptography_,
608  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
609  * https://www.secg.org/sec2-v2.pdf
610  */
611 #define PSA_ECC_FAMILY_SECP_R1           ((psa_ecc_family_t) 0x12)
612 /* SECP160R2 (SEC2 v1, obsolete) */
613 #define PSA_ECC_FAMILY_SECP_R2           ((psa_ecc_family_t) 0x1b)
614 
615 /** SEC Koblitz curves over binary fields.
616  *
617  * This family comprises the following curves:
618  * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
619  * They are defined in _Standards for Efficient Cryptography_,
620  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
621  * https://www.secg.org/sec2-v2.pdf
622  */
623 #define PSA_ECC_FAMILY_SECT_K1           ((psa_ecc_family_t) 0x27)
624 
625 /** SEC random curves over binary fields.
626  *
627  * This family comprises the following curves:
628  * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
629  * They are defined in _Standards for Efficient Cryptography_,
630  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
631  * https://www.secg.org/sec2-v2.pdf
632  */
633 #define PSA_ECC_FAMILY_SECT_R1           ((psa_ecc_family_t) 0x22)
634 
635 /** SEC additional random curves over binary fields.
636  *
637  * This family comprises the following curve:
638  * sect163r2.
639  * It is defined in _Standards for Efficient Cryptography_,
640  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
641  * https://www.secg.org/sec2-v2.pdf
642  */
643 #define PSA_ECC_FAMILY_SECT_R2           ((psa_ecc_family_t) 0x2b)
644 
645 /** Brainpool P random curves.
646  *
647  * This family comprises the following curves:
648  * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
649  * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
650  * It is defined in RFC 5639.
651  */
652 #define PSA_ECC_FAMILY_BRAINPOOL_P_R1    ((psa_ecc_family_t) 0x30)
653 
654 /** Curve25519 and Curve448.
655  *
656  * This family comprises the following Montgomery curves:
657  * - 255-bit: Bernstein et al.,
658  *   _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
659  *   The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
660  * - 448-bit: Hamburg,
661  *   _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
662  *   The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
663  */
664 #define PSA_ECC_FAMILY_MONTGOMERY        ((psa_ecc_family_t) 0x41)
665 
666 /** The twisted Edwards curves Ed25519 and Ed448.
667  *
668  * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
669  * #PSA_ALG_ED25519PH for the 255-bit curve,
670  * #PSA_ALG_ED448PH for the 448-bit curve).
671  *
672  * This family comprises the following twisted Edwards curves:
673  * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
674  *   to Curve25519.
675  *   Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
676  * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
677  *   to Curve448.
678  *   Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
679  */
680 #define PSA_ECC_FAMILY_TWISTED_EDWARDS   ((psa_ecc_family_t) 0x42)
681 
682 #define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE             ((psa_key_type_t)0x4200)
683 #define PSA_KEY_TYPE_DH_KEY_PAIR_BASE               ((psa_key_type_t)0x7200)
684 #define PSA_KEY_TYPE_DH_GROUP_MASK                  ((psa_key_type_t)0x00ff)
685 /** Diffie-Hellman key pair.
686  *
687  * \param group     A value of type ::psa_dh_family_t that identifies the
688  *                  Diffie-Hellman group to be used.
689  */
690 #define PSA_KEY_TYPE_DH_KEY_PAIR(group)          \
691     (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
692 /** Diffie-Hellman public key.
693  *
694  * \param group     A value of type ::psa_dh_family_t that identifies the
695  *                  Diffie-Hellman group to be used.
696  */
697 #define PSA_KEY_TYPE_DH_PUBLIC_KEY(group)               \
698     (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
699 
700 /** Whether a key type is a Diffie-Hellman key (pair or public-only). */
701 #define PSA_KEY_TYPE_IS_DH(type)                                        \
702     ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) &                        \
703       ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
704 /** Whether a key type is a Diffie-Hellman key pair. */
705 #define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type)                               \
706     (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) ==                         \
707      PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
708 /** Whether a key type is a Diffie-Hellman public key. */
709 #define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type)                            \
710     (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) ==                         \
711      PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
712 
713 /** Extract the group from a Diffie-Hellman key type. */
714 #define PSA_KEY_TYPE_DH_GET_FAMILY(type)                        \
715     ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ?              \
716                        ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) :  \
717                        0))
718 
719 /** Diffie-Hellman groups defined in RFC 7919 Appendix A.
720  *
721  * This family includes groups with the following key sizes (in bits):
722  * 2048, 3072, 4096, 6144, 8192. A given implementation may support
723  * all of these sizes or only a subset.
724  */
725 #define PSA_DH_FAMILY_RFC7919            ((psa_dh_family_t) 0x03)
726 
727 #define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type)      \
728     (((type) >> 8) & 7)
729 /** The block size of a block cipher.
730  *
731  * \param type  A cipher key type (value of type #psa_key_type_t).
732  *
733  * \return      The block size for a block cipher, or 1 for a stream cipher.
734  *              The return value is undefined if \p type is not a supported
735  *              cipher key type.
736  *
737  * \note It is possible to build stream cipher algorithms on top of a block
738  *       cipher, for example CTR mode (#PSA_ALG_CTR).
739  *       This macro only takes the key type into account, so it cannot be
740  *       used to determine the size of the data that #psa_cipher_update()
741  *       might buffer for future processing in general.
742  *
743  * \note This macro returns a compile-time constant if its argument is one.
744  *
745  * \warning This macro may evaluate its argument multiple times.
746  */
747 #define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type)                                     \
748     (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
749      1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) :                         \
750      0u)
751 
752 /* Note that algorithm values are embedded in the persistent key store,
753  * as part of key metadata. As a consequence, they must not be changed
754  * (unless the storage format version changes).
755  */
756 
757 /** Vendor-defined algorithm flag.
758  *
759  * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
760  * bit set. Vendors who define additional algorithms must use an encoding with
761  * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
762  * used by standard encodings whenever practical.
763  */
764 #define PSA_ALG_VENDOR_FLAG                     ((psa_algorithm_t)0x80000000)
765 
766 #define PSA_ALG_CATEGORY_MASK                   ((psa_algorithm_t)0x7f000000)
767 #define PSA_ALG_CATEGORY_HASH                   ((psa_algorithm_t)0x02000000)
768 #define PSA_ALG_CATEGORY_MAC                    ((psa_algorithm_t)0x03000000)
769 #define PSA_ALG_CATEGORY_CIPHER                 ((psa_algorithm_t)0x04000000)
770 #define PSA_ALG_CATEGORY_AEAD                   ((psa_algorithm_t)0x05000000)
771 #define PSA_ALG_CATEGORY_SIGN                   ((psa_algorithm_t)0x06000000)
772 #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION  ((psa_algorithm_t)0x07000000)
773 #define PSA_ALG_CATEGORY_KEY_DERIVATION         ((psa_algorithm_t)0x08000000)
774 #define PSA_ALG_CATEGORY_KEY_AGREEMENT          ((psa_algorithm_t)0x09000000)
775 
776 /** Whether an algorithm is vendor-defined.
777  *
778  * See also #PSA_ALG_VENDOR_FLAG.
779  */
780 #define PSA_ALG_IS_VENDOR_DEFINED(alg)                                  \
781     (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
782 
783 /** Whether the specified algorithm is a hash algorithm.
784  *
785  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
786  *
787  * \return 1 if \p alg is a hash algorithm, 0 otherwise.
788  *         This macro may return either 0 or 1 if \p alg is not a supported
789  *         algorithm identifier.
790  */
791 #define PSA_ALG_IS_HASH(alg)                                            \
792     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
793 
794 /** Whether the specified algorithm is a MAC algorithm.
795  *
796  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
797  *
798  * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
799  *         This macro may return either 0 or 1 if \p alg is not a supported
800  *         algorithm identifier.
801  */
802 #define PSA_ALG_IS_MAC(alg)                                             \
803     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
804 
805 /** Whether the specified algorithm is a symmetric cipher algorithm.
806  *
807  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
808  *
809  * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
810  *         This macro may return either 0 or 1 if \p alg is not a supported
811  *         algorithm identifier.
812  */
813 #define PSA_ALG_IS_CIPHER(alg)                                          \
814     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
815 
816 /** Whether the specified algorithm is an authenticated encryption
817  * with associated data (AEAD) algorithm.
818  *
819  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
820  *
821  * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
822  *         This macro may return either 0 or 1 if \p alg is not a supported
823  *         algorithm identifier.
824  */
825 #define PSA_ALG_IS_AEAD(alg)                                            \
826     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
827 
828 /** Whether the specified algorithm is an asymmetric signature algorithm,
829  * also known as public-key signature algorithm.
830  *
831  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
832  *
833  * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
834  *         This macro may return either 0 or 1 if \p alg is not a supported
835  *         algorithm identifier.
836  */
837 #define PSA_ALG_IS_SIGN(alg)                                            \
838     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
839 
840 /** Whether the specified algorithm is an asymmetric encryption algorithm,
841  * also known as public-key encryption algorithm.
842  *
843  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
844  *
845  * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
846  *         This macro may return either 0 or 1 if \p alg is not a supported
847  *         algorithm identifier.
848  */
849 #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)                           \
850     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
851 
852 /** Whether the specified algorithm is a key agreement algorithm.
853  *
854  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
855  *
856  * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
857  *         This macro may return either 0 or 1 if \p alg is not a supported
858  *         algorithm identifier.
859  */
860 #define PSA_ALG_IS_KEY_AGREEMENT(alg)                                   \
861     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
862 
863 /** Whether the specified algorithm is a key derivation algorithm.
864  *
865  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
866  *
867  * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
868  *         This macro may return either 0 or 1 if \p alg is not a supported
869  *         algorithm identifier.
870  */
871 #define PSA_ALG_IS_KEY_DERIVATION(alg)                                  \
872     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
873 
874 /** Whether the specified algorithm is a key stretching / password hashing
875  * algorithm.
876  *
877  * A key stretching / password hashing algorithm is a key derivation algorithm
878  * that is suitable for use with a low-entropy secret such as a password.
879  * Equivalently, it's a key derivation algorithm that uses a
880  * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
881  *
882  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
883  *
884  * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
885  *         otherwise. This macro may return either 0 or 1 if \p alg is not a
886  *         supported algorithm identifier.
887  */
888 #define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg)                                  \
889     (PSA_ALG_IS_KEY_DERIVATION(alg) &&              \
890      (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
891 
892 /** An invalid algorithm identifier value. */
893 #define PSA_ALG_NONE                            ((psa_algorithm_t)0)
894 
895 #define PSA_ALG_HASH_MASK                       ((psa_algorithm_t)0x000000ff)
896 /** MD5 */
897 #define PSA_ALG_MD5                             ((psa_algorithm_t)0x02000003)
898 /** PSA_ALG_RIPEMD160 */
899 #define PSA_ALG_RIPEMD160                       ((psa_algorithm_t)0x02000004)
900 /** SHA1 */
901 #define PSA_ALG_SHA_1                           ((psa_algorithm_t)0x02000005)
902 /** SHA2-224 */
903 #define PSA_ALG_SHA_224                         ((psa_algorithm_t)0x02000008)
904 /** SHA2-256 */
905 #define PSA_ALG_SHA_256                         ((psa_algorithm_t)0x02000009)
906 /** SHA2-384 */
907 #define PSA_ALG_SHA_384                         ((psa_algorithm_t)0x0200000a)
908 /** SHA2-512 */
909 #define PSA_ALG_SHA_512                         ((psa_algorithm_t)0x0200000b)
910 /** SHA2-512/224 */
911 #define PSA_ALG_SHA_512_224                     ((psa_algorithm_t)0x0200000c)
912 /** SHA2-512/256 */
913 #define PSA_ALG_SHA_512_256                     ((psa_algorithm_t)0x0200000d)
914 /** SHA3-224 */
915 #define PSA_ALG_SHA3_224                        ((psa_algorithm_t)0x02000010)
916 /** SHA3-256 */
917 #define PSA_ALG_SHA3_256                        ((psa_algorithm_t)0x02000011)
918 /** SHA3-384 */
919 #define PSA_ALG_SHA3_384                        ((psa_algorithm_t)0x02000012)
920 /** SHA3-512 */
921 #define PSA_ALG_SHA3_512                        ((psa_algorithm_t)0x02000013)
922 /** The first 512 bits (64 bytes) of the SHAKE256 output.
923  *
924  * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
925  * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
926  * has the same output size and a (theoretically) higher security strength.
927  */
928 #define PSA_ALG_SHAKE256_512                    ((psa_algorithm_t)0x02000015)
929 
930 /** In a hash-and-sign algorithm policy, allow any hash algorithm.
931  *
932  * This value may be used to form the algorithm usage field of a policy
933  * for a signature algorithm that is parametrized by a hash. The key
934  * may then be used to perform operations using the same signature
935  * algorithm parametrized with any supported hash.
936  *
937  * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
938  * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
939  * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
940  * Then you may create and use a key as follows:
941  * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
942  *   ```
943  *   psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
944  *   psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
945  *   ```
946  * - Import or generate key material.
947  * - Call psa_sign_hash() or psa_verify_hash(), passing
948  *   an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
949  *   call to sign or verify a message may use a different hash.
950  *   ```
951  *   psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
952  *   psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
953  *   psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
954  *   ```
955  *
956  * This value may not be used to build other algorithms that are
957  * parametrized over a hash. For any valid use of this macro to build
958  * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
959  *
960  * This value may not be used to build an algorithm specification to
961  * perform an operation. It is only valid to build policies.
962  */
963 #define PSA_ALG_ANY_HASH                        ((psa_algorithm_t)0x020000ff)
964 
965 #define PSA_ALG_MAC_SUBCATEGORY_MASK            ((psa_algorithm_t)0x00c00000)
966 #define PSA_ALG_HMAC_BASE                       ((psa_algorithm_t)0x03800000)
967 /** Macro to build an HMAC algorithm.
968  *
969  * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
970  *
971  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
972  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
973  *
974  * \return              The corresponding HMAC algorithm.
975  * \return              Unspecified if \p hash_alg is not a supported
976  *                      hash algorithm.
977  */
978 #define PSA_ALG_HMAC(hash_alg)                                  \
979     (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
980 
981 #define PSA_ALG_HMAC_GET_HASH(hmac_alg)                             \
982     (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
983 
984 /** Whether the specified algorithm is an HMAC algorithm.
985  *
986  * HMAC is a family of MAC algorithms that are based on a hash function.
987  *
988  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
989  *
990  * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
991  *         This macro may return either 0 or 1 if \p alg is not a supported
992  *         algorithm identifier.
993  */
994 #define PSA_ALG_IS_HMAC(alg)                                            \
995     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
996      PSA_ALG_HMAC_BASE)
997 
998 /* In the encoding of a MAC algorithm, the bits corresponding to
999  * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
1000  * truncated. As an exception, the value 0 means the untruncated algorithm,
1001  * whatever its length is. The length is encoded in 6 bits, so it can
1002  * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
1003  * to full length is correctly encoded as 0 and any non-trivial truncation
1004  * is correctly encoded as a value between 1 and 63. */
1005 #define PSA_ALG_MAC_TRUNCATION_MASK             ((psa_algorithm_t)0x003f0000)
1006 #define PSA_MAC_TRUNCATION_OFFSET 16
1007 
1008 /* In the encoding of a MAC algorithm, the bit corresponding to
1009  * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1010  * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1011  * algorithm policy can be used with any algorithm corresponding to the
1012  * same base class and having a (potentially truncated) MAC length greater or
1013  * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
1014 #define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG   ((psa_algorithm_t)0x00008000)
1015 
1016 /** Macro to build a truncated MAC algorithm.
1017  *
1018  * A truncated MAC algorithm is identical to the corresponding MAC
1019  * algorithm except that the MAC value for the truncated algorithm
1020  * consists of only the first \p mac_length bytes of the MAC value
1021  * for the untruncated algorithm.
1022  *
1023  * \note    This macro may allow constructing algorithm identifiers that
1024  *          are not valid, either because the specified length is larger
1025  *          than the untruncated MAC or because the specified length is
1026  *          smaller than permitted by the implementation.
1027  *
1028  * \note    It is implementation-defined whether a truncated MAC that
1029  *          is truncated to the same length as the MAC of the untruncated
1030  *          algorithm is considered identical to the untruncated algorithm
1031  *          for policy comparison purposes.
1032  *
1033  * \param mac_alg       A MAC algorithm identifier (value of type
1034  *                      #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1035  *                      is true). This may be a truncated or untruncated
1036  *                      MAC algorithm.
1037  * \param mac_length    Desired length of the truncated MAC in bytes.
1038  *                      This must be at most the full length of the MAC
1039  *                      and must be at least an implementation-specified
1040  *                      minimum. The implementation-specified minimum
1041  *                      shall not be zero.
1042  *
1043  * \return              The corresponding MAC algorithm with the specified
1044  *                      length.
1045  * \return              Unspecified if \p mac_alg is not a supported
1046  *                      MAC algorithm or if \p mac_length is too small or
1047  *                      too large for the specified MAC algorithm.
1048  */
1049 #define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length)              \
1050     (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK |               \
1051                     PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) |   \
1052      ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1053 
1054 /** Macro to build the base MAC algorithm corresponding to a truncated
1055  * MAC algorithm.
1056  *
1057  * \param mac_alg       A MAC algorithm identifier (value of type
1058  *                      #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1059  *                      is true). This may be a truncated or untruncated
1060  *                      MAC algorithm.
1061  *
1062  * \return              The corresponding base MAC algorithm.
1063  * \return              Unspecified if \p mac_alg is not a supported
1064  *                      MAC algorithm.
1065  */
1066 #define PSA_ALG_FULL_LENGTH_MAC(mac_alg)                        \
1067     ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK |                \
1068                    PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
1069 
1070 /** Length to which a MAC algorithm is truncated.
1071  *
1072  * \param mac_alg       A MAC algorithm identifier (value of type
1073  *                      #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1074  *                      is true).
1075  *
1076  * \return              Length of the truncated MAC in bytes.
1077  * \return              0 if \p mac_alg is a non-truncated MAC algorithm.
1078  * \return              Unspecified if \p mac_alg is not a supported
1079  *                      MAC algorithm.
1080  */
1081 #define PSA_MAC_TRUNCATED_LENGTH(mac_alg)                               \
1082     (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1083 
1084 /** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1085  *
1086  * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1087  * sharing the same base algorithm, and where the (potentially truncated) MAC
1088  * length of the specific algorithm is equal to or larger then the wildcard
1089  * algorithm's minimum MAC length.
1090  *
1091  * \note    When setting the minimum required MAC length to less than the
1092  *          smallest MAC length allowed by the base algorithm, this effectively
1093  *          becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1094  *
1095  * \param mac_alg         A MAC algorithm identifier (value of type
1096  *                        #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1097  *                        is true).
1098  * \param min_mac_length  Desired minimum length of the message authentication
1099  *                        code in bytes. This must be at most the untruncated
1100  *                        length of the MAC and must be at least 1.
1101  *
1102  * \return                The corresponding MAC wildcard algorithm with the
1103  *                        specified minimum length.
1104  * \return                Unspecified if \p mac_alg is not a supported MAC
1105  *                        algorithm or if \p min_mac_length is less than 1 or
1106  *                        too large for the specified MAC algorithm.
1107  */
1108 #define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length)   \
1109     ( PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) |              \
1110       PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
1111 
1112 #define PSA_ALG_CIPHER_MAC_BASE                 ((psa_algorithm_t)0x03c00000)
1113 /** The CBC-MAC construction over a block cipher
1114  *
1115  * \warning CBC-MAC is insecure in many cases.
1116  * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1117  */
1118 #define PSA_ALG_CBC_MAC                         ((psa_algorithm_t)0x03c00100)
1119 /** The CMAC construction over a block cipher */
1120 #define PSA_ALG_CMAC                            ((psa_algorithm_t)0x03c00200)
1121 
1122 /** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1123  *
1124  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1125  *
1126  * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1127  *         This macro may return either 0 or 1 if \p alg is not a supported
1128  *         algorithm identifier.
1129  */
1130 #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg)                                \
1131     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1132      PSA_ALG_CIPHER_MAC_BASE)
1133 
1134 #define PSA_ALG_CIPHER_STREAM_FLAG              ((psa_algorithm_t)0x00800000)
1135 #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG          ((psa_algorithm_t)0x00400000)
1136 
1137 /** Whether the specified algorithm is a stream cipher.
1138  *
1139  * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1140  * by applying a bitwise-xor with a stream of bytes that is generated
1141  * from a key.
1142  *
1143  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1144  *
1145  * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1146  *         This macro may return either 0 or 1 if \p alg is not a supported
1147  *         algorithm identifier or if it is not a symmetric cipher algorithm.
1148  */
1149 #define PSA_ALG_IS_STREAM_CIPHER(alg)            \
1150     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1151         (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1152 
1153 /** The stream cipher mode of a stream cipher algorithm.
1154  *
1155  * The underlying stream cipher is determined by the key type.
1156  * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
1157  */
1158 #define PSA_ALG_STREAM_CIPHER                   ((psa_algorithm_t)0x04800100)
1159 
1160 /** The CTR stream cipher mode.
1161  *
1162  * CTR is a stream cipher which is built from a block cipher.
1163  * The underlying block cipher is determined by the key type.
1164  * For example, to use AES-128-CTR, use this algorithm with
1165  * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1166  */
1167 #define PSA_ALG_CTR                             ((psa_algorithm_t)0x04c01000)
1168 
1169 /** The CFB stream cipher mode.
1170  *
1171  * The underlying block cipher is determined by the key type.
1172  */
1173 #define PSA_ALG_CFB                             ((psa_algorithm_t)0x04c01100)
1174 
1175 /** The OFB stream cipher mode.
1176  *
1177  * The underlying block cipher is determined by the key type.
1178  */
1179 #define PSA_ALG_OFB                             ((psa_algorithm_t)0x04c01200)
1180 
1181 /** The XTS cipher mode.
1182  *
1183  * XTS is a cipher mode which is built from a block cipher. It requires at
1184  * least one full block of input, but beyond this minimum the input
1185  * does not need to be a whole number of blocks.
1186  */
1187 #define PSA_ALG_XTS                             ((psa_algorithm_t)0x0440ff00)
1188 
1189 /** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1190  *
1191  * \warning ECB mode does not protect the confidentiality of the encrypted data
1192  * except in extremely narrow circumstances. It is recommended that applications
1193  * only use ECB if they need to construct an operating mode that the
1194  * implementation does not provide. Implementations are encouraged to provide
1195  * the modes that applications need in preference to supporting direct access
1196  * to ECB.
1197  *
1198  * The underlying block cipher is determined by the key type.
1199  *
1200  * This symmetric cipher mode can only be used with messages whose lengths are a
1201  * multiple of the block size of the chosen block cipher.
1202  *
1203  * ECB mode does not accept an initialization vector (IV). When using a
1204  * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1205  * and psa_cipher_set_iv() must not be called.
1206  */
1207 #define PSA_ALG_ECB_NO_PADDING                  ((psa_algorithm_t)0x04404400)
1208 
1209 /** The CBC block cipher chaining mode, with no padding.
1210  *
1211  * The underlying block cipher is determined by the key type.
1212  *
1213  * This symmetric cipher mode can only be used with messages whose lengths
1214  * are whole number of blocks for the chosen block cipher.
1215  */
1216 #define PSA_ALG_CBC_NO_PADDING                  ((psa_algorithm_t)0x04404000)
1217 
1218 /** The CBC block cipher chaining mode with PKCS#7 padding.
1219  *
1220  * The underlying block cipher is determined by the key type.
1221  *
1222  * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1223  */
1224 #define PSA_ALG_CBC_PKCS7                       ((psa_algorithm_t)0x04404100)
1225 
1226 #define PSA_ALG_AEAD_FROM_BLOCK_FLAG            ((psa_algorithm_t)0x00400000)
1227 
1228 /** Whether the specified algorithm is an AEAD mode on a block cipher.
1229  *
1230  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1231  *
1232  * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1233  *         a block cipher, 0 otherwise.
1234  *         This macro may return either 0 or 1 if \p alg is not a supported
1235  *         algorithm identifier.
1236  */
1237 #define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg)    \
1238     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1239      (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1240 
1241 /** The CCM authenticated encryption algorithm.
1242  *
1243  * The underlying block cipher is determined by the key type.
1244  */
1245 #define PSA_ALG_CCM                             ((psa_algorithm_t)0x05500100)
1246 
1247 /** The CCM* cipher mode without authentication.
1248  *
1249  * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1250  * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1251  *
1252  * The underlying block cipher is determined by the key type.
1253  *
1254  * Currently only 13-byte long IV's are supported.
1255  */
1256 #define PSA_ALG_CCM_STAR_NO_TAG                 ((psa_algorithm_t)0x04c01300)
1257 
1258 /** The GCM authenticated encryption algorithm.
1259  *
1260  * The underlying block cipher is determined by the key type.
1261  */
1262 #define PSA_ALG_GCM                             ((psa_algorithm_t)0x05500200)
1263 
1264 /** The Chacha20-Poly1305 AEAD algorithm.
1265  *
1266  * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1267  *
1268  * Implementations must support 12-byte nonces, may support 8-byte nonces,
1269  * and should reject other sizes.
1270  *
1271  * Implementations must support 16-byte tags and should reject other sizes.
1272  */
1273 #define PSA_ALG_CHACHA20_POLY1305               ((psa_algorithm_t)0x05100500)
1274 
1275 /* In the encoding of an AEAD algorithm, the bits corresponding to
1276  * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1277  * The constants for default lengths follow this encoding.
1278  */
1279 #define PSA_ALG_AEAD_TAG_LENGTH_MASK            ((psa_algorithm_t)0x003f0000)
1280 #define PSA_AEAD_TAG_LENGTH_OFFSET 16
1281 
1282 /* In the encoding of an AEAD algorithm, the bit corresponding to
1283  * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1284  * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1285  * algorithm policy can be used with any algorithm corresponding to the
1286  * same base class and having a tag length greater than or equal to the one
1287  * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1288 #define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG  ((psa_algorithm_t)0x00008000)
1289 
1290 /** Macro to build a shortened AEAD algorithm.
1291  *
1292  * A shortened AEAD algorithm is similar to the corresponding AEAD
1293  * algorithm, but has an authentication tag that consists of fewer bytes.
1294  * Depending on the algorithm, the tag length may affect the calculation
1295  * of the ciphertext.
1296  *
1297  * \param aead_alg      An AEAD algorithm identifier (value of type
1298  *                      #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
1299  *                      is true).
1300  * \param tag_length    Desired length of the authentication tag in bytes.
1301  *
1302  * \return              The corresponding AEAD algorithm with the specified
1303  *                      length.
1304  * \return              Unspecified if \p aead_alg is not a supported
1305  *                      AEAD algorithm or if \p tag_length is not valid
1306  *                      for the specified AEAD algorithm.
1307  */
1308 #define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length)           \
1309     (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK |                     \
1310                      PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) |         \
1311      ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET &                      \
1312       PSA_ALG_AEAD_TAG_LENGTH_MASK))
1313 
1314 /** Retrieve the tag length of a specified AEAD algorithm
1315  *
1316  * \param aead_alg      An AEAD algorithm identifier (value of type
1317  *                      #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
1318  *                      is true).
1319  *
1320  * \return              The tag length specified by the input algorithm.
1321  * \return              Unspecified if \p aead_alg is not a supported
1322  *                      AEAD algorithm.
1323  */
1324 #define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg)                           \
1325     (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >>                     \
1326       PSA_AEAD_TAG_LENGTH_OFFSET )
1327 
1328 /** Calculate the corresponding AEAD algorithm with the default tag length.
1329  *
1330  * \param aead_alg      An AEAD algorithm (\c PSA_ALG_XXX value such that
1331  *                      #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1332  *
1333  * \return              The corresponding AEAD algorithm with the default
1334  *                      tag length for that algorithm.
1335  */
1336 #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg)                   \
1337     (                                                                    \
1338         PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1339         PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1340         PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
1341         0)
1342 #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref)         \
1343     PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) ==                      \
1344     PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ?                            \
1345     ref :
1346 
1347 /** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1348  *
1349  * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1350  * sharing the same base algorithm, and where the tag length of the specific
1351  * algorithm is equal to or larger then the minimum tag length specified by the
1352  * wildcard algorithm.
1353  *
1354  * \note    When setting the minimum required tag length to less than the
1355  *          smallest tag length allowed by the base algorithm, this effectively
1356  *          becomes an 'any-tag-length-allowed' policy for that base algorithm.
1357  *
1358  * \param aead_alg        An AEAD algorithm identifier (value of type
1359  *                        #psa_algorithm_t such that
1360  *                        #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1361  * \param min_tag_length  Desired minimum length of the authentication tag in
1362  *                        bytes. This must be at least 1 and at most the largest
1363  *                        allowed tag length of the algorithm.
1364  *
1365  * \return                The corresponding AEAD wildcard algorithm with the
1366  *                        specified minimum length.
1367  * \return                Unspecified if \p aead_alg is not a supported
1368  *                        AEAD algorithm or if \p min_tag_length is less than 1
1369  *                        or too large for the specified AEAD algorithm.
1370  */
1371 #define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1372     ( PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) |            \
1373       PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
1374 
1375 #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE          ((psa_algorithm_t)0x06000200)
1376 /** RSA PKCS#1 v1.5 signature with hashing.
1377  *
1378  * This is the signature scheme defined by RFC 8017
1379  * (PKCS#1: RSA Cryptography Specifications) under the name
1380  * RSASSA-PKCS1-v1_5.
1381  *
1382  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1383  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1384  *                      This includes #PSA_ALG_ANY_HASH
1385  *                      when specifying the algorithm in a usage policy.
1386  *
1387  * \return              The corresponding RSA PKCS#1 v1.5 signature algorithm.
1388  * \return              Unspecified if \p hash_alg is not a supported
1389  *                      hash algorithm.
1390  */
1391 #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg)                             \
1392     (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1393 /** Raw PKCS#1 v1.5 signature.
1394  *
1395  * The input to this algorithm is the DigestInfo structure used by
1396  * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1397  * steps 3&ndash;6.
1398  */
1399 #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1400 #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)                               \
1401     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1402 
1403 #define PSA_ALG_RSA_PSS_BASE               ((psa_algorithm_t)0x06000300)
1404 #define PSA_ALG_RSA_PSS_ANY_SALT_BASE      ((psa_algorithm_t)0x06001300)
1405 /** RSA PSS signature with hashing.
1406  *
1407  * This is the signature scheme defined by RFC 8017
1408  * (PKCS#1: RSA Cryptography Specifications) under the name
1409  * RSASSA-PSS, with the message generation function MGF1, and with
1410  * a salt length equal to the length of the hash, or the largest
1411  * possible salt length for the algorithm and key size if that is
1412  * smaller than the hash length. The specified hash algorithm is
1413  * used to hash the input message, to create the salted hash, and
1414  * for the mask generation.
1415  *
1416  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1417  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1418  *                      This includes #PSA_ALG_ANY_HASH
1419  *                      when specifying the algorithm in a usage policy.
1420  *
1421  * \return              The corresponding RSA PSS signature algorithm.
1422  * \return              Unspecified if \p hash_alg is not a supported
1423  *                      hash algorithm.
1424  */
1425 #define PSA_ALG_RSA_PSS(hash_alg)                               \
1426     (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1427 
1428 /** RSA PSS signature with hashing with relaxed verification.
1429  *
1430  * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1431  * but allows an arbitrary salt length (including \c 0) when verifying a
1432  * signature.
1433  *
1434  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1435  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1436  *                      This includes #PSA_ALG_ANY_HASH
1437  *                      when specifying the algorithm in a usage policy.
1438  *
1439  * \return              The corresponding RSA PSS signature algorithm.
1440  * \return              Unspecified if \p hash_alg is not a supported
1441  *                      hash algorithm.
1442  */
1443 #define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg)                      \
1444     (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1445 
1446 /** Whether the specified algorithm is RSA PSS with standard salt.
1447  *
1448  * \param alg           An algorithm value or an algorithm policy wildcard.
1449  *
1450  * \return              1 if \p alg is of the form
1451  *                      #PSA_ALG_RSA_PSS(\c hash_alg),
1452  *                      where \c hash_alg is a hash algorithm or
1453  *                      #PSA_ALG_ANY_HASH. 0 otherwise.
1454  *                      This macro may return either 0 or 1 if \p alg is not
1455  *                      a supported algorithm identifier or policy.
1456  */
1457 #define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg)                   \
1458     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1459 
1460 /** Whether the specified algorithm is RSA PSS with any salt.
1461  *
1462  * \param alg           An algorithm value or an algorithm policy wildcard.
1463  *
1464  * \return              1 if \p alg is of the form
1465  *                      #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1466  *                      where \c hash_alg is a hash algorithm or
1467  *                      #PSA_ALG_ANY_HASH. 0 otherwise.
1468  *                      This macro may return either 0 or 1 if \p alg is not
1469  *                      a supported algorithm identifier or policy.
1470  */
1471 #define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)                                \
1472     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1473 
1474 /** Whether the specified algorithm is RSA PSS.
1475  *
1476  * This includes any of the RSA PSS algorithm variants, regardless of the
1477  * constraints on salt length.
1478  *
1479  * \param alg           An algorithm value or an algorithm policy wildcard.
1480  *
1481  * \return              1 if \p alg is of the form
1482  *                      #PSA_ALG_RSA_PSS(\c hash_alg) or
1483  *                      #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1484  *                      where \c hash_alg is a hash algorithm or
1485  *                      #PSA_ALG_ANY_HASH. 0 otherwise.
1486  *                      This macro may return either 0 or 1 if \p alg is not
1487  *                      a supported algorithm identifier or policy.
1488  */
1489 #define PSA_ALG_IS_RSA_PSS(alg)                                 \
1490     (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) ||                   \
1491      PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
1492 
1493 #define PSA_ALG_ECDSA_BASE                      ((psa_algorithm_t)0x06000600)
1494 /** ECDSA signature with hashing.
1495  *
1496  * This is the ECDSA signature scheme defined by ANSI X9.62,
1497  * with a random per-message secret number (*k*).
1498  *
1499  * The representation of the signature as a byte string consists of
1500  * the concatenation of the signature values *r* and *s*. Each of
1501  * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1502  * of the base point of the curve in octets. Each value is represented
1503  * in big-endian order (most significant octet first).
1504  *
1505  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1506  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1507  *                      This includes #PSA_ALG_ANY_HASH
1508  *                      when specifying the algorithm in a usage policy.
1509  *
1510  * \return              The corresponding ECDSA signature algorithm.
1511  * \return              Unspecified if \p hash_alg is not a supported
1512  *                      hash algorithm.
1513  */
1514 #define PSA_ALG_ECDSA(hash_alg)                                 \
1515     (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1516 /** ECDSA signature without hashing.
1517  *
1518  * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1519  * without specifying a hash algorithm. This algorithm may only be
1520  * used to sign or verify a sequence of bytes that should be an
1521  * already-calculated hash. Note that the input is padded with
1522  * zeros on the left or truncated on the left as required to fit
1523  * the curve size.
1524  */
1525 #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1526 #define PSA_ALG_DETERMINISTIC_ECDSA_BASE        ((psa_algorithm_t)0x06000700)
1527 /** Deterministic ECDSA signature with hashing.
1528  *
1529  * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1530  *
1531  * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1532  *
1533  * Note that when this algorithm is used for verification, signatures
1534  * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1535  * same private key are accepted. In other words,
1536  * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1537  * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1538  *
1539  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1540  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1541  *                      This includes #PSA_ALG_ANY_HASH
1542  *                      when specifying the algorithm in a usage policy.
1543  *
1544  * \return              The corresponding deterministic ECDSA signature
1545  *                      algorithm.
1546  * \return              Unspecified if \p hash_alg is not a supported
1547  *                      hash algorithm.
1548  */
1549 #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg)                           \
1550     (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1551 #define PSA_ALG_ECDSA_DETERMINISTIC_FLAG        ((psa_algorithm_t)0x00000100)
1552 #define PSA_ALG_IS_ECDSA(alg)                                           \
1553     (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) ==  \
1554      PSA_ALG_ECDSA_BASE)
1555 #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)             \
1556     (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
1557 #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)                             \
1558     (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1559 #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg)                                \
1560     (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1561 
1562 /** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1563  * using standard parameters.
1564  *
1565  * Contexts are not supported in the current version of this specification
1566  * because there is no suitable signature interface that can take the
1567  * context as a parameter. A future version of this specification may add
1568  * suitable functions and extend this algorithm to support contexts.
1569  *
1570  * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1571  * In this specification, the following curves are supported:
1572  * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1573  *   in RFC 8032.
1574  *   The curve is Edwards25519.
1575  *   The hash function used internally is SHA-512.
1576  * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1577  *   in RFC 8032.
1578  *   The curve is Edwards448.
1579  *   The hash function used internally is the first 114 bytes of the
1580  *   SHAKE256 output.
1581  *
1582  * This algorithm can be used with psa_sign_message() and
1583  * psa_verify_message(). Since there is no prehashing, it cannot be used
1584  * with psa_sign_hash() or psa_verify_hash().
1585  *
1586  * The signature format is the concatenation of R and S as defined by
1587  * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1588  * string for Ed448).
1589  */
1590 #define PSA_ALG_PURE_EDDSA                      ((psa_algorithm_t)0x06000800)
1591 
1592 #define PSA_ALG_HASH_EDDSA_BASE                 ((psa_algorithm_t)0x06000900)
1593 #define PSA_ALG_IS_HASH_EDDSA(alg)              \
1594     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1595 
1596 /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1597  * using SHA-512 and the Edwards25519 curve.
1598  *
1599  * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1600  *
1601  * This algorithm is Ed25519 as specified in RFC 8032.
1602  * The curve is Edwards25519.
1603  * The prehash is SHA-512.
1604  * The hash function used internally is SHA-512.
1605  *
1606  * This is a hash-and-sign algorithm: to calculate a signature,
1607  * you can either:
1608  * - call psa_sign_message() on the message;
1609  * - or calculate the SHA-512 hash of the message
1610  *   with psa_hash_compute()
1611  *   or with a multi-part hash operation started with psa_hash_setup(),
1612  *   using the hash algorithm #PSA_ALG_SHA_512,
1613  *   then sign the calculated hash with psa_sign_hash().
1614  * Verifying a signature is similar, using psa_verify_message() or
1615  * psa_verify_hash() instead of the signature function.
1616  */
1617 #define PSA_ALG_ED25519PH                               \
1618     (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1619 
1620 /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1621  * using SHAKE256 and the Edwards448 curve.
1622  *
1623  * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1624  *
1625  * This algorithm is Ed448 as specified in RFC 8032.
1626  * The curve is Edwards448.
1627  * The prehash is the first 64 bytes of the SHAKE256 output.
1628  * The hash function used internally is the first 114 bytes of the
1629  * SHAKE256 output.
1630  *
1631  * This is a hash-and-sign algorithm: to calculate a signature,
1632  * you can either:
1633  * - call psa_sign_message() on the message;
1634  * - or calculate the first 64 bytes of the SHAKE256 output of the message
1635  *   with psa_hash_compute()
1636  *   or with a multi-part hash operation started with psa_hash_setup(),
1637  *   using the hash algorithm #PSA_ALG_SHAKE256_512,
1638  *   then sign the calculated hash with psa_sign_hash().
1639  * Verifying a signature is similar, using psa_verify_message() or
1640  * psa_verify_hash() instead of the signature function.
1641  */
1642 #define PSA_ALG_ED448PH                                 \
1643     (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1644 
1645 /* Default definition, to be overridden if the library is extended with
1646  * more hash-and-sign algorithms that we want to keep out of this header
1647  * file. */
1648 #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1649 
1650 /** Whether the specified algorithm is a signature algorithm that can be used
1651  * with psa_sign_hash() and psa_verify_hash().
1652  *
1653  * This encompasses all strict hash-and-sign algorithms categorized by
1654  * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1655  * paradigm more loosely:
1656  * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1657  * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
1658  *
1659  * \param alg An algorithm identifier (value of type psa_algorithm_t).
1660  *
1661  * \return 1 if alg is a signature algorithm that can be used to sign a
1662  *         hash. 0 if alg is a signature algorithm that can only be used
1663  *         to sign a message. 0 if alg is not a signature algorithm.
1664  *         This macro can return either 0 or 1 if alg is not a
1665  *         supported algorithm identifier.
1666  */
1667 #define PSA_ALG_IS_SIGN_HASH(alg)                                       \
1668     (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||    \
1669      PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) ||             \
1670      PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1671 
1672 /** Whether the specified algorithm is a signature algorithm that can be used
1673  * with psa_sign_message() and psa_verify_message().
1674  *
1675  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1676  *
1677  * \return 1 if alg is a signature algorithm that can be used to sign a
1678  *         message. 0 if \p alg is a signature algorithm that can only be used
1679  *         to sign an already-calculated hash. 0 if \p alg is not a signature
1680  *         algorithm. This macro can return either 0 or 1 if \p alg is not a
1681  *         supported algorithm identifier.
1682  */
1683 #define PSA_ALG_IS_SIGN_MESSAGE(alg)                                    \
1684     (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
1685 
1686 /** Whether the specified algorithm is a hash-and-sign algorithm.
1687  *
1688  * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1689  * structured in two parts: first the calculation of a hash in a way that
1690  * does not depend on the key, then the calculation of a signature from the
1691  * hash value and the key. Hash-and-sign algorithms encode the hash
1692  * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1693  * to extract this algorithm.
1694  *
1695  * Thus, for a hash-and-sign algorithm,
1696  * `psa_sign_message(key, alg, input, ...)` is equivalent to
1697  * ```
1698  * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1699  * psa_sign_hash(key, alg, hash, ..., signature, ...);
1700  * ```
1701  * Most usefully, separating the hash from the signature allows the hash
1702  * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1703  * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1704  * calculating the hash and then calling psa_verify_hash().
1705  *
1706  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1707  *
1708  * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1709  *         This macro may return either 0 or 1 if \p alg is not a supported
1710  *         algorithm identifier.
1711  */
1712 #define PSA_ALG_IS_HASH_AND_SIGN(alg)                                   \
1713     (PSA_ALG_IS_SIGN_HASH(alg) &&                                       \
1714      ((alg) & PSA_ALG_HASH_MASK) != 0)
1715 
1716 /** Get the hash used by a hash-and-sign signature algorithm.
1717  *
1718  * A hash-and-sign algorithm is a signature algorithm which is
1719  * composed of two phases: first a hashing phase which does not use
1720  * the key and produces a hash of the input message, then a signing
1721  * phase which only uses the hash and the key and not the message
1722  * itself.
1723  *
1724  * \param alg   A signature algorithm (\c PSA_ALG_XXX value such that
1725  *              #PSA_ALG_IS_SIGN(\p alg) is true).
1726  *
1727  * \return      The underlying hash algorithm if \p alg is a hash-and-sign
1728  *              algorithm.
1729  * \return      0 if \p alg is a signature algorithm that does not
1730  *              follow the hash-and-sign structure.
1731  * \return      Unspecified if \p alg is not a signature algorithm or
1732  *              if it is not supported by the implementation.
1733  */
1734 #define PSA_ALG_SIGN_GET_HASH(alg)                                     \
1735     (PSA_ALG_IS_HASH_AND_SIGN(alg) ?                                   \
1736      ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH :             \
1737      0)
1738 
1739 /** RSA PKCS#1 v1.5 encryption.
1740  */
1741 #define PSA_ALG_RSA_PKCS1V15_CRYPT              ((psa_algorithm_t)0x07000200)
1742 
1743 #define PSA_ALG_RSA_OAEP_BASE                   ((psa_algorithm_t)0x07000300)
1744 /** RSA OAEP encryption.
1745  *
1746  * This is the encryption scheme defined by RFC 8017
1747  * (PKCS#1: RSA Cryptography Specifications) under the name
1748  * RSAES-OAEP, with the message generation function MGF1.
1749  *
1750  * \param hash_alg      The hash algorithm (\c PSA_ALG_XXX value such that
1751  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1752  *                      for MGF1.
1753  *
1754  * \return              The corresponding RSA OAEP encryption algorithm.
1755  * \return              Unspecified if \p hash_alg is not a supported
1756  *                      hash algorithm.
1757  */
1758 #define PSA_ALG_RSA_OAEP(hash_alg)                              \
1759     (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1760 #define PSA_ALG_IS_RSA_OAEP(alg)                                \
1761     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1762 #define PSA_ALG_RSA_OAEP_GET_HASH(alg)                          \
1763     (PSA_ALG_IS_RSA_OAEP(alg) ?                                 \
1764      ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH :      \
1765      0)
1766 
1767 #define PSA_ALG_HKDF_BASE                       ((psa_algorithm_t)0x08000100)
1768 /** Macro to build an HKDF algorithm.
1769  *
1770  * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1771  *
1772  * This key derivation algorithm uses the following inputs:
1773  * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1774  *   It is optional; if omitted, the derivation uses an empty salt.
1775  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1776  * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1777  * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1778  * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1779  * starting to generate output.
1780  *
1781  *  \warning  HKDF processes the salt as follows: first hash it with hash_alg
1782  *  if the salt is longer than the block size of the hash algorithm; then
1783  *  pad with null bytes up to the block size. As a result, it is possible
1784  *  for distinct salt inputs to result in the same outputs. To ensure
1785  *  unique outputs, it is recommended to use a fixed length for salt values.
1786  *
1787  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1788  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1789  *
1790  * \return              The corresponding HKDF algorithm.
1791  * \return              Unspecified if \p hash_alg is not a supported
1792  *                      hash algorithm.
1793  */
1794 #define PSA_ALG_HKDF(hash_alg)                                  \
1795     (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1796 /** Whether the specified algorithm is an HKDF algorithm.
1797  *
1798  * HKDF is a family of key derivation algorithms that are based on a hash
1799  * function and the HMAC construction.
1800  *
1801  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1802  *
1803  * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1804  *         This macro may return either 0 or 1 if \c alg is not a supported
1805  *         key derivation algorithm identifier.
1806  */
1807 #define PSA_ALG_IS_HKDF(alg)                            \
1808     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1809 #define PSA_ALG_HKDF_GET_HASH(hkdf_alg)                         \
1810     (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1811 
1812 #define PSA_ALG_HKDF_EXTRACT_BASE                       ((psa_algorithm_t)0x08000400)
1813 /** Macro to build an HKDF-Extract algorithm.
1814  *
1815  * For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA256)` is
1816  * HKDF-Extract using HMAC-SHA-256.
1817  *
1818  * This key derivation algorithm uses the following inputs:
1819  *  - PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1820  *  - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the
1821  *    "extract" step.
1822  * The inputs are mandatory and must be passed in the order above.
1823  * Each input may only be passed once.
1824  *
1825  *  \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF
1826  *  should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided
1827  *  as a separate algorithm for the sake of protocols that use it as a
1828  *  building block. It may also be a slight performance optimization
1829  *  in applications that use HKDF with the same salt and key but many
1830  *  different info strings.
1831  *
1832  *  \warning  HKDF processes the salt as follows: first hash it with hash_alg
1833  *  if the salt is longer than the block size of the hash algorithm; then
1834  *  pad with null bytes up to the block size. As a result, it is possible
1835  *  for distinct salt inputs to result in the same outputs. To ensure
1836  *  unique outputs, it is recommended to use a fixed length for salt values.
1837  *
1838  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1839  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1840  *
1841  * \return              The corresponding HKDF-Extract algorithm.
1842  * \return              Unspecified if \p hash_alg is not a supported
1843  *                      hash algorithm.
1844  */
1845 #define PSA_ALG_HKDF_EXTRACT(hash_alg)                                  \
1846     (PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1847 /** Whether the specified algorithm is an HKDF-Extract algorithm.
1848  *
1849  * HKDF-Extract is a family of key derivation algorithms that are based
1850  * on a hash function and the HMAC construction.
1851  *
1852  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1853  *
1854  * \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.
1855  *         This macro may return either 0 or 1 if \c alg is not a supported
1856  *         key derivation algorithm identifier.
1857  */
1858 #define PSA_ALG_IS_HKDF_EXTRACT(alg)                            \
1859     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)
1860 
1861 #define PSA_ALG_HKDF_EXPAND_BASE                       ((psa_algorithm_t)0x08000500)
1862 /** Macro to build an HKDF-Expand algorithm.
1863  *
1864  * For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA256)` is
1865  * HKDF-Expand using HMAC-SHA-256.
1866  *
1867  * This key derivation algorithm uses the following inputs:
1868  *  - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).
1869  *  - PSA_KEY_DERIVATION_INPUT_INFO is the info string.
1870  *
1871  *  The inputs are mandatory and must be passed in the order above.
1872  *  Each input may only be passed once.
1873  *
1874  *  \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`
1875  *  should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as
1876  *  a separate algorithm for the sake of protocols that use it as a building
1877  *  block. It may also be a slight performance optimization in applications
1878  *  that use HKDF with the same salt and key but many different info strings.
1879  *
1880  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1881  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1882  *
1883  * \return              The corresponding HKDF-Expand algorithm.
1884  * \return              Unspecified if \p hash_alg is not a supported
1885  *                      hash algorithm.
1886  */
1887 #define PSA_ALG_HKDF_EXPAND(hash_alg)                                  \
1888     (PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1889 /** Whether the specified algorithm is an HKDF-Expand algorithm.
1890  *
1891  * HKDF-Expand is a family of key derivation algorithms that are based
1892  * on a hash function and the HMAC construction.
1893  *
1894  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1895  *
1896  * \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.
1897  *         This macro may return either 0 or 1 if \c alg is not a supported
1898  *         key derivation algorithm identifier.
1899  */
1900 #define PSA_ALG_IS_HKDF_EXPAND(alg)                            \
1901     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1902 
1903 /** Whether the specified algorithm is an HKDF or HKDF-Extract or
1904  *  HKDF-Expand algorithm.
1905  *
1906  *
1907  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1908  *
1909  * \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.
1910  *         This macro may return either 0 or 1 if \c alg is not a supported
1911  *         key derivation algorithm identifier.
1912  */
1913 #define PSA_ALG_IS_ANY_HKDF(alg)                                   \
1914     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE ||          \
1915      ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE ||  \
1916      ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1917 
1918 #define PSA_ALG_TLS12_PRF_BASE                  ((psa_algorithm_t)0x08000200)
1919 /** Macro to build a TLS-1.2 PRF algorithm.
1920  *
1921  * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1922  * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1923  * used with either SHA-256 or SHA-384.
1924  *
1925  * This key derivation algorithm uses the following inputs, which must be
1926  * passed in the order given here:
1927  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1928  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1929  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1930  *
1931  * For the application to TLS-1.2 key expansion, the seed is the
1932  * concatenation of ServerHello.Random + ClientHello.Random,
1933  * and the label is "key expansion".
1934  *
1935  * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1936  * TLS 1.2 PRF using HMAC-SHA-256.
1937  *
1938  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1939  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1940  *
1941  * \return              The corresponding TLS-1.2 PRF algorithm.
1942  * \return              Unspecified if \p hash_alg is not a supported
1943  *                      hash algorithm.
1944  */
1945 #define PSA_ALG_TLS12_PRF(hash_alg)                                  \
1946     (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1947 
1948 /** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1949  *
1950  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1951  *
1952  * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1953  *         This macro may return either 0 or 1 if \c alg is not a supported
1954  *         key derivation algorithm identifier.
1955  */
1956 #define PSA_ALG_IS_TLS12_PRF(alg)                                    \
1957     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1958 #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg)                         \
1959     (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1960 
1961 #define PSA_ALG_TLS12_PSK_TO_MS_BASE            ((psa_algorithm_t)0x08000300)
1962 /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1963  *
1964  * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1965  * from the PreSharedKey (PSK) through the application of padding
1966  * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1967  * The latter is based on HMAC and can be used with either SHA-256
1968  * or SHA-384.
1969  *
1970  * This key derivation algorithm uses the following inputs, which must be
1971  * passed in the order given here:
1972  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1973  * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
1974  *   computation of the premaster secret. This input is optional;
1975  *   if omitted, it defaults to a string of null bytes with the same length
1976  *   as the secret (PSK) input.
1977  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1978  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1979  *
1980  * For the application to TLS-1.2, the seed (which is
1981  * forwarded to the TLS-1.2 PRF) is the concatenation of the
1982  * ClientHello.Random + ServerHello.Random,
1983  * the label is "master secret" or "extended master secret" and
1984  * the other secret depends on the key exchange specified in the cipher suite:
1985  * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
1986  *   PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
1987  * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
1988  *   (RFC 5489, Section 2), the other secret should be the output of the
1989  *   PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
1990  *   The recommended way to pass this input is to use a key derivation
1991  *   algorithm constructed as
1992  *   PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
1993  *   and to call psa_key_derivation_key_agreement(). Alternatively,
1994  *   this input may be an output of `psa_raw_key_agreement()` passed with
1995  *   psa_key_derivation_input_bytes(), or an equivalent input passed with
1996  *   psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
1997  * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
1998  *   should be the 48-byte client challenge (the PreMasterSecret of
1999  *   (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
2000  *   a 46-byte random string chosen by the client. On the server, this is
2001  *   typically an output of psa_asymmetric_decrypt() using
2002  *   PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
2003  *   with `psa_key_derivation_input_bytes()`.
2004  *
2005  * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
2006  * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
2007  *
2008  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
2009  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
2010  *
2011  * \return              The corresponding TLS-1.2 PSK to MS algorithm.
2012  * \return              Unspecified if \p hash_alg is not a supported
2013  *                      hash algorithm.
2014  */
2015 #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg)                                  \
2016     (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2017 
2018 /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
2019  *
2020  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2021  *
2022  * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
2023  *         This macro may return either 0 or 1 if \c alg is not a supported
2024  *         key derivation algorithm identifier.
2025  */
2026 #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg)                                    \
2027     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
2028 #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg)                         \
2029     (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
2030 
2031 /* The TLS 1.2 ECJPAKE-to-PMS KDF. It takes the shared secret K (an EC point
2032  * in case of EC J-PAKE) and calculates SHA256(K.X) that the rest of TLS 1.2
2033  * will use to derive the session secret, as defined by step 2 of
2034  * https://datatracker.ietf.org/doc/html/draft-cragie-tls-ecjpake-01#section-8.7.
2035  * Uses PSA_ALG_SHA_256.
2036  * This function takes a single input:
2037  * #PSA_KEY_DERIVATION_INPUT_SECRET is the shared secret K from EC J-PAKE.
2038  * The only supported curve is secp256r1 (the 256-bit curve in
2039  * #PSA_ECC_FAMILY_SECP_R1), so the input must be exactly 65 bytes.
2040  * The output has to be read as a single chunk of 32 bytes, defined as
2041  * PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE.
2042  */
2043 #define PSA_ALG_TLS12_ECJPAKE_TO_PMS            ((psa_algorithm_t)0x08000609)
2044 
2045 /* This flag indicates whether the key derivation algorithm is suitable for
2046  * use on low-entropy secrets such as password - these algorithms are also
2047  * known as key stretching or password hashing schemes. These are also the
2048  * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
2049  *
2050  * Those algorithms cannot be combined with a key agreement algorithm.
2051  */
2052 #define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG  ((psa_algorithm_t)0x00800000)
2053 
2054 #define PSA_ALG_PBKDF2_HMAC_BASE                ((psa_algorithm_t)0x08800100)
2055 /** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
2056  *
2057  * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2058  * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
2059  * HMAC with the specified hash.
2060  * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA256)` specifies PBKDF2
2061  * using the PRF HMAC-SHA-256.
2062  *
2063  * This key derivation algorithm uses the following inputs, which must be
2064  * provided in the following order:
2065  * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
2066  *   This input step must be used exactly once.
2067  * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
2068  *   This input step must be used one or more times; if used several times, the
2069  *   inputs will be concatenated. This can be used to build the final salt
2070  *   from multiple sources, both public and secret (also known as pepper).
2071  * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
2072  *   This input step must be used exactly once.
2073  *
2074  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
2075  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
2076  *
2077  * \return              The corresponding PBKDF2-HMAC-XXX algorithm.
2078  * \return              Unspecified if \p hash_alg is not a supported
2079  *                      hash algorithm.
2080  */
2081 #define PSA_ALG_PBKDF2_HMAC(hash_alg)                                  \
2082     (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2083 
2084 /** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
2085  *
2086  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2087  *
2088  * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
2089  *         This macro may return either 0 or 1 if \c alg is not a supported
2090  *         key derivation algorithm identifier.
2091  */
2092 #define PSA_ALG_IS_PBKDF2_HMAC(alg)                                    \
2093     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
2094 
2095 /** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
2096  *
2097  * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2098  * This macro specifies the PBKDF2 algorithm constructed using the
2099  * AES-CMAC-PRF-128 PRF specified by RFC 4615.
2100  *
2101  * This key derivation algorithm uses the same inputs as
2102  * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
2103  */
2104 #define PSA_ALG_PBKDF2_AES_CMAC_PRF_128         ((psa_algorithm_t)0x08800200)
2105 
2106 #define PSA_ALG_KEY_DERIVATION_MASK             ((psa_algorithm_t)0xfe00ffff)
2107 #define PSA_ALG_KEY_AGREEMENT_MASK              ((psa_algorithm_t)0xffff0000)
2108 
2109 /** Macro to build a combined algorithm that chains a key agreement with
2110  * a key derivation.
2111  *
2112  * \param ka_alg        A key agreement algorithm (\c PSA_ALG_XXX value such
2113  *                      that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
2114  * \param kdf_alg       A key derivation algorithm (\c PSA_ALG_XXX value such
2115  *                      that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
2116  *
2117  * \return              The corresponding key agreement and derivation
2118  *                      algorithm.
2119  * \return              Unspecified if \p ka_alg is not a supported
2120  *                      key agreement algorithm or \p kdf_alg is not a
2121  *                      supported key derivation algorithm.
2122  */
2123 #define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg)  \
2124     ((ka_alg) | (kdf_alg))
2125 
2126 #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg)                              \
2127     (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
2128 
2129 #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg)                             \
2130     (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
2131 
2132 /** Whether the specified algorithm is a raw key agreement algorithm.
2133  *
2134  * A raw key agreement algorithm is one that does not specify
2135  * a key derivation function.
2136  * Usually, raw key agreement algorithms are constructed directly with
2137  * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
2138  * constructed with #PSA_ALG_KEY_AGREEMENT().
2139  *
2140  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2141  *
2142  * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
2143  *         This macro may return either 0 or 1 if \p alg is not a supported
2144  *         algorithm identifier.
2145  */
2146 #define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)                               \
2147     (PSA_ALG_IS_KEY_AGREEMENT(alg) &&                                   \
2148      PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
2149 
2150 #define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg)     \
2151     ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
2152 
2153 /** The finite-field Diffie-Hellman (DH) key agreement algorithm.
2154  *
2155  * The shared secret produced by key agreement is
2156  * `g^{ab}` in big-endian format.
2157  * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
2158  * in bits.
2159  */
2160 #define PSA_ALG_FFDH                            ((psa_algorithm_t)0x09010000)
2161 
2162 /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
2163  *
2164  * This includes the raw finite field Diffie-Hellman algorithm as well as
2165  * finite-field Diffie-Hellman followed by any supporter key derivation
2166  * algorithm.
2167  *
2168  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2169  *
2170  * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
2171  *         This macro may return either 0 or 1 if \c alg is not a supported
2172  *         key agreement algorithm identifier.
2173  */
2174 #define PSA_ALG_IS_FFDH(alg) \
2175     (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
2176 
2177 /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
2178  *
2179  * The shared secret produced by key agreement is the x-coordinate of
2180  * the shared secret point. It is always `ceiling(m / 8)` bytes long where
2181  * `m` is the bit size associated with the curve, i.e. the bit size of the
2182  * order of the curve's coordinate field. When `m` is not a multiple of 8,
2183  * the byte containing the most significant bit of the shared secret
2184  * is padded with zero bits. The byte order is either little-endian
2185  * or big-endian depending on the curve type.
2186  *
2187  * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
2188  *   the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2189  *   in little-endian byte order.
2190  *   The bit size is 448 for Curve448 and 255 for Curve25519.
2191  * - For Weierstrass curves over prime fields (curve types
2192  *   `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
2193  *   the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2194  *   in big-endian byte order.
2195  *   The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
2196  * - For Weierstrass curves over binary fields (curve types
2197  *   `PSA_ECC_FAMILY_SECTXXX`),
2198  *   the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2199  *   in big-endian byte order.
2200  *   The bit size is `m` for the field `F_{2^m}`.
2201  */
2202 #define PSA_ALG_ECDH                            ((psa_algorithm_t)0x09020000)
2203 
2204 /** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2205  * algorithm.
2206  *
2207  * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2208  * elliptic curve Diffie-Hellman followed by any supporter key derivation
2209  * algorithm.
2210  *
2211  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2212  *
2213  * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2214  *         0 otherwise.
2215  *         This macro may return either 0 or 1 if \c alg is not a supported
2216  *         key agreement algorithm identifier.
2217  */
2218 #define PSA_ALG_IS_ECDH(alg) \
2219     (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
2220 
2221 /** Whether the specified algorithm encoding is a wildcard.
2222  *
2223  * Wildcard values may only be used to set the usage algorithm field in
2224  * a policy, not to perform an operation.
2225  *
2226  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2227  *
2228  * \return 1 if \c alg is a wildcard algorithm encoding.
2229  * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2230  *         an operation).
2231  * \return This macro may return either 0 or 1 if \c alg is not a supported
2232  *         algorithm identifier.
2233  */
2234 #define PSA_ALG_IS_WILDCARD(alg)                            \
2235     (PSA_ALG_IS_HASH_AND_SIGN(alg) ?                        \
2236      PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH :       \
2237      PSA_ALG_IS_MAC(alg) ?                                  \
2238      (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 :   \
2239      PSA_ALG_IS_AEAD(alg) ?                                 \
2240      (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 :  \
2241      (alg) == PSA_ALG_ANY_HASH)
2242 
2243 /** Get the hash used by a composite algorithm.
2244  *
2245  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2246  *
2247  * \return The underlying hash algorithm if alg is a composite algorithm that
2248  * uses a hash algorithm.
2249  *
2250  * \return \c 0 if alg is not a composite algorithm that uses a hash.
2251  */
2252 #define PSA_ALG_GET_HASH(alg) \
2253         (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t)0) : 0x02000000 | ((alg) & 0x000000ff))
2254 
2255 /**@}*/
2256 
2257 /** \defgroup key_lifetimes Key lifetimes
2258  * @{
2259  */
2260 
2261 /* Note that location and persistence level values are embedded in the
2262  * persistent key store, as part of key metadata. As a consequence, they
2263  * must not be changed (unless the storage format version changes).
2264  */
2265 
2266 /** The default lifetime for volatile keys.
2267  *
2268  * A volatile key only exists as long as the identifier to it is not destroyed.
2269  * The key material is guaranteed to be erased on a power reset.
2270  *
2271  * A key with this lifetime is typically stored in the RAM area of the
2272  * PSA Crypto subsystem. However this is an implementation choice.
2273  * If an implementation stores data about the key in a non-volatile memory,
2274  * it must release all the resources associated with the key and erase the
2275  * key material if the calling application terminates.
2276  */
2277 #define PSA_KEY_LIFETIME_VOLATILE               ((psa_key_lifetime_t)0x00000000)
2278 
2279 /** The default lifetime for persistent keys.
2280  *
2281  * A persistent key remains in storage until it is explicitly destroyed or
2282  * until the corresponding storage area is wiped. This specification does
2283  * not define any mechanism to wipe a storage area, but integrations may
2284  * provide their own mechanism (for example to perform a factory reset,
2285  * to prepare for device refurbishment, or to uninstall an application).
2286  *
2287  * This lifetime value is the default storage area for the calling
2288  * application. Integrations of Mbed TLS may support other persistent lifetimes.
2289  * See ::psa_key_lifetime_t for more information.
2290  */
2291 #define PSA_KEY_LIFETIME_PERSISTENT             ((psa_key_lifetime_t)0x00000001)
2292 
2293 /** The persistence level of volatile keys.
2294  *
2295  * See ::psa_key_persistence_t for more information.
2296  */
2297 #define PSA_KEY_PERSISTENCE_VOLATILE            ((psa_key_persistence_t)0x00)
2298 
2299 /** The default persistence level for persistent keys.
2300  *
2301  * See ::psa_key_persistence_t for more information.
2302  */
2303 #define PSA_KEY_PERSISTENCE_DEFAULT             ((psa_key_persistence_t)0x01)
2304 
2305 /** A persistence level indicating that a key is never destroyed.
2306  *
2307  * See ::psa_key_persistence_t for more information.
2308  */
2309 #define PSA_KEY_PERSISTENCE_READ_ONLY           ((psa_key_persistence_t)0xff)
2310 
2311 #define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime)      \
2312     ((psa_key_persistence_t)((lifetime) & 0x000000ff))
2313 
2314 #define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)      \
2315     ((psa_key_location_t)((lifetime) >> 8))
2316 
2317 /** Whether a key lifetime indicates that the key is volatile.
2318  *
2319  * A volatile key is automatically destroyed by the implementation when
2320  * the application instance terminates. In particular, a volatile key
2321  * is automatically destroyed on a power reset of the device.
2322  *
2323  * A key that is not volatile is persistent. Persistent keys are
2324  * preserved until the application explicitly destroys them or until an
2325  * implementation-specific device management event occurs (for example,
2326  * a factory reset).
2327  *
2328  * \param lifetime      The lifetime value to query (value of type
2329  *                      ::psa_key_lifetime_t).
2330  *
2331  * \return \c 1 if the key is volatile, otherwise \c 0.
2332  */
2333 #define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)  \
2334     (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2335      PSA_KEY_PERSISTENCE_VOLATILE)
2336 
2337 /** Whether a key lifetime indicates that the key is read-only.
2338  *
2339  * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2340  * They must be created through platform-specific means that bypass the API.
2341  *
2342  * Some platforms may offer ways to destroy read-only keys. For example,
2343  * consider a platform with multiple levels of privilege, where a
2344  * low-privilege application can use a key but is not allowed to destroy
2345  * it, and the platform exposes the key to the application with a read-only
2346  * lifetime. High-privilege code can destroy the key even though the
2347  * application sees the key as read-only.
2348  *
2349  * \param lifetime      The lifetime value to query (value of type
2350  *                      ::psa_key_lifetime_t).
2351  *
2352  * \return \c 1 if the key is read-only, otherwise \c 0.
2353  */
2354 #define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)  \
2355     (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2356      PSA_KEY_PERSISTENCE_READ_ONLY)
2357 
2358 /** Construct a lifetime from a persistence level and a location.
2359  *
2360  * \param persistence   The persistence level
2361  *                      (value of type ::psa_key_persistence_t).
2362  * \param location      The location indicator
2363  *                      (value of type ::psa_key_location_t).
2364  *
2365  * \return The constructed lifetime value.
2366  */
2367 #define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2368     ((location) << 8 | (persistence))
2369 
2370 /** The local storage area for persistent keys.
2371  *
2372  * This storage area is available on all systems that can store persistent
2373  * keys without delegating the storage to a third-party cryptoprocessor.
2374  *
2375  * See ::psa_key_location_t for more information.
2376  */
2377 #define PSA_KEY_LOCATION_LOCAL_STORAGE          ((psa_key_location_t)0x000000)
2378 
2379 #define PSA_KEY_LOCATION_VENDOR_FLAG            ((psa_key_location_t)0x800000)
2380 
2381 /* Note that key identifier values are embedded in the
2382  * persistent key store, as part of key metadata. As a consequence, they
2383  * must not be changed (unless the storage format version changes).
2384  */
2385 
2386 /** The null key identifier.
2387  */
2388 #define PSA_KEY_ID_NULL                         ((psa_key_id_t)0)
2389 /** The minimum value for a key identifier chosen by the application.
2390  */
2391 #define PSA_KEY_ID_USER_MIN                     ((psa_key_id_t)0x00000001)
2392 /** The maximum value for a key identifier chosen by the application.
2393  */
2394 #define PSA_KEY_ID_USER_MAX                     ((psa_key_id_t)0x3fffffff)
2395 /** The minimum value for a key identifier chosen by the implementation.
2396  */
2397 #define PSA_KEY_ID_VENDOR_MIN                   ((psa_key_id_t)0x40000000)
2398 /** The maximum value for a key identifier chosen by the implementation.
2399  */
2400 #define PSA_KEY_ID_VENDOR_MAX                   ((psa_key_id_t)0x7fffffff)
2401 #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2402 
2403 #define MBEDTLS_SVC_KEY_ID_INIT ((psa_key_id_t) 0)
2404 #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) (id)
2405 #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) (0)
2406 
2407 /** Utility to initialize a key identifier at runtime.
2408  *
2409  * \param unused  Unused parameter.
2410  * \param key_id  Identifier of the key.
2411  */
mbedtls_svc_key_id_make(unsigned int unused,psa_key_id_t key_id)2412 static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2413     unsigned int unused, psa_key_id_t key_id)
2414 {
2415     (void) unused;
2416 
2417     return key_id;
2418 }
2419 
2420 /** Compare two key identifiers.
2421  *
2422  * \param id1 First key identifier.
2423  * \param id2 Second key identifier.
2424  *
2425  * \return Non-zero if the two key identifier are equal, zero otherwise.
2426  */
mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,mbedtls_svc_key_id_t id2)2427 static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2428                                            mbedtls_svc_key_id_t id2)
2429 {
2430     return id1 == id2;
2431 }
2432 
2433 /** Check whether a key identifier is null.
2434  *
2435  * \param key Key identifier.
2436  *
2437  * \return Non-zero if the key identifier is null, zero otherwise.
2438  */
mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2439 static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2440 {
2441     return key == 0;
2442 }
2443 
2444 #else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2445 #include "mbedtls/private_access.h"
2446 #define MBEDTLS_SVC_KEY_ID_INIT ((mbedtls_svc_key_id_t){ 0, 0 })
2447 #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) ((id).MBEDTLS_PRIVATE(key_id))
2448 #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) ((id).MBEDTLS_PRIVATE(owner))
2449 
2450 /** Utility to initialize a key identifier at runtime.
2451  *
2452  * \param owner_id Identifier of the key owner.
2453  * \param key_id   Identifier of the key.
2454  */
mbedtls_svc_key_id_make(mbedtls_key_owner_id_t owner_id,psa_key_id_t key_id)2455 static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2456     mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id)
2457 {
2458     return (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,
2459                                    .MBEDTLS_PRIVATE(owner) = owner_id };
2460 }
2461 
2462 /** Compare two key identifiers.
2463  *
2464  * \param id1 First key identifier.
2465  * \param id2 Second key identifier.
2466  *
2467  * \return Non-zero if the two key identifier are equal, zero otherwise.
2468  */
mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,mbedtls_svc_key_id_t id2)2469 static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2470                                            mbedtls_svc_key_id_t id2)
2471 {
2472     return (id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id)) &&
2473            mbedtls_key_owner_id_equal(id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner));
2474 }
2475 
2476 /** Check whether a key identifier is null.
2477  *
2478  * \param key Key identifier.
2479  *
2480  * \return Non-zero if the key identifier is null, zero otherwise.
2481  */
mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2482 static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2483 {
2484     return key.MBEDTLS_PRIVATE(key_id) == 0;
2485 }
2486 
2487 #endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2488 
2489 /**@}*/
2490 
2491 /** \defgroup policy Key policies
2492  * @{
2493  */
2494 
2495 /* Note that key usage flags are embedded in the
2496  * persistent key store, as part of key metadata. As a consequence, they
2497  * must not be changed (unless the storage format version changes).
2498  */
2499 
2500 /** Whether the key may be exported.
2501  *
2502  * A public key or the public part of a key pair may always be exported
2503  * regardless of the value of this permission flag.
2504  *
2505  * If a key does not have export permission, implementations shall not
2506  * allow the key to be exported in plain form from the cryptoprocessor,
2507  * whether through psa_export_key() or through a proprietary interface.
2508  * The key may however be exportable in a wrapped form, i.e. in a form
2509  * where it is encrypted by another key.
2510  */
2511 #define PSA_KEY_USAGE_EXPORT                    ((psa_key_usage_t)0x00000001)
2512 
2513 /** Whether the key may be copied.
2514  *
2515  * This flag allows the use of psa_copy_key() to make a copy of the key
2516  * with the same policy or a more restrictive policy.
2517  *
2518  * For lifetimes for which the key is located in a secure element which
2519  * enforce the non-exportability of keys, copying a key outside the secure
2520  * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2521  * Copying the key inside the secure element is permitted with just
2522  * #PSA_KEY_USAGE_COPY if the secure element supports it.
2523  * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2524  * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2525  * is sufficient to permit the copy.
2526  */
2527 #define PSA_KEY_USAGE_COPY                      ((psa_key_usage_t)0x00000002)
2528 
2529 /** Whether the key may be used to encrypt a message.
2530  *
2531  * This flag allows the key to be used for a symmetric encryption operation,
2532  * for an AEAD encryption-and-authentication operation,
2533  * or for an asymmetric encryption operation,
2534  * if otherwise permitted by the key's type and policy.
2535  *
2536  * For a key pair, this concerns the public key.
2537  */
2538 #define PSA_KEY_USAGE_ENCRYPT                   ((psa_key_usage_t)0x00000100)
2539 
2540 /** Whether the key may be used to decrypt a message.
2541  *
2542  * This flag allows the key to be used for a symmetric decryption operation,
2543  * for an AEAD decryption-and-verification operation,
2544  * or for an asymmetric decryption operation,
2545  * if otherwise permitted by the key's type and policy.
2546  *
2547  * For a key pair, this concerns the private key.
2548  */
2549 #define PSA_KEY_USAGE_DECRYPT                   ((psa_key_usage_t)0x00000200)
2550 
2551 /** Whether the key may be used to sign a message.
2552  *
2553  * This flag allows the key to be used for a MAC calculation operation or for
2554  * an asymmetric message signature operation, if otherwise permitted by the
2555  * key’s type and policy.
2556  *
2557  * For a key pair, this concerns the private key.
2558  */
2559 #define PSA_KEY_USAGE_SIGN_MESSAGE              ((psa_key_usage_t)0x00000400)
2560 
2561 /** Whether the key may be used to verify a message.
2562  *
2563  * This flag allows the key to be used for a MAC verification operation or for
2564  * an asymmetric message signature verification operation, if otherwise
2565  * permitted by the key’s type and policy.
2566  *
2567  * For a key pair, this concerns the public key.
2568  */
2569 #define PSA_KEY_USAGE_VERIFY_MESSAGE            ((psa_key_usage_t)0x00000800)
2570 
2571 /** Whether the key may be used to sign a message.
2572  *
2573  * This flag allows the key to be used for a MAC calculation operation
2574  * or for an asymmetric signature operation,
2575  * if otherwise permitted by the key's type and policy.
2576  *
2577  * For a key pair, this concerns the private key.
2578  */
2579 #define PSA_KEY_USAGE_SIGN_HASH                 ((psa_key_usage_t)0x00001000)
2580 
2581 /** Whether the key may be used to verify a message signature.
2582  *
2583  * This flag allows the key to be used for a MAC verification operation
2584  * or for an asymmetric signature verification operation,
2585  * if otherwise permitted by the key's type and policy.
2586  *
2587  * For a key pair, this concerns the public key.
2588  */
2589 #define PSA_KEY_USAGE_VERIFY_HASH               ((psa_key_usage_t)0x00002000)
2590 
2591 /** Whether the key may be used to derive other keys or produce a password
2592  * hash.
2593  *
2594  * This flag allows the key to be used for a key derivation operation or for
2595  * a key agreement operation, if otherwise permitted by the key's type and
2596  * policy.
2597  *
2598  * If this flag is present on all keys used in calls to
2599  * psa_key_derivation_input_key() for a key derivation operation, then it
2600  * permits calling psa_key_derivation_output_bytes() or
2601  * psa_key_derivation_output_key() at the end of the operation.
2602  */
2603 #define PSA_KEY_USAGE_DERIVE                    ((psa_key_usage_t)0x00004000)
2604 
2605 /** Whether the key may be used to verify the result of a key derivation,
2606  * including password hashing.
2607  *
2608  * This flag allows the key to be used:
2609  *
2610  * This flag allows the key to be used in a key derivation operation, if
2611  * otherwise permitted by the key's type and policy.
2612  *
2613  * If this flag is present on all keys used in calls to
2614  * psa_key_derivation_input_key() for a key derivation operation, then it
2615  * permits calling psa_key_derivation_verify_bytes() or
2616  * psa_key_derivation_verify_key() at the end of the operation.
2617  */
2618 #define PSA_KEY_USAGE_VERIFY_DERIVATION         ((psa_key_usage_t)0x00008000)
2619 
2620 /**@}*/
2621 
2622 /** \defgroup derivation Key derivation
2623  * @{
2624  */
2625 
2626 /* Key input steps are not embedded in the persistent storage, so you can
2627  * change them if needed: it's only an ABI change. */
2628 
2629 /** A secret input for key derivation.
2630  *
2631  * This should be a key of type #PSA_KEY_TYPE_DERIVE
2632  * (passed to psa_key_derivation_input_key())
2633  * or the shared secret resulting from a key agreement
2634  * (obtained via psa_key_derivation_key_agreement()).
2635  *
2636  * The secret can also be a direct input (passed to
2637  * key_derivation_input_bytes()). In this case, the derivation operation
2638  * may not be used to derive keys: the operation will only allow
2639  * psa_key_derivation_output_bytes(),
2640  * psa_key_derivation_verify_bytes(), or
2641  * psa_key_derivation_verify_key(), but not
2642  * psa_key_derivation_output_key().
2643  */
2644 #define PSA_KEY_DERIVATION_INPUT_SECRET     ((psa_key_derivation_step_t)0x0101)
2645 
2646 /** A low-entropy secret input for password hashing / key stretching.
2647  *
2648  * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2649  * psa_key_derivation_input_key()) or a direct input (passed to
2650  * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2651  * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2652  * the shared secret resulting from a key agreement.
2653  *
2654  * The secret can also be a direct input (passed to
2655  * key_derivation_input_bytes()). In this case, the derivation operation
2656  * may not be used to derive keys: the operation will only allow
2657  * psa_key_derivation_output_bytes(),
2658  * psa_key_derivation_verify_bytes(), or
2659  * psa_key_derivation_verify_key(), but not
2660  * psa_key_derivation_output_key().
2661  */
2662 #define PSA_KEY_DERIVATION_INPUT_PASSWORD   ((psa_key_derivation_step_t)0x0102)
2663 
2664 /** A high-entropy additional secret input for key derivation.
2665  *
2666  * This is typically the shared secret resulting from a key agreement obtained
2667  * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
2668  * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
2669  * a direct input passed to `psa_key_derivation_input_bytes()`.
2670  */
2671 #define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
2672                                             ((psa_key_derivation_step_t)0x0103)
2673 
2674 /** A label for key derivation.
2675  *
2676  * This should be a direct input.
2677  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2678  */
2679 #define PSA_KEY_DERIVATION_INPUT_LABEL      ((psa_key_derivation_step_t)0x0201)
2680 
2681 /** A salt for key derivation.
2682  *
2683  * This should be a direct input.
2684  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2685  * #PSA_KEY_TYPE_PEPPER.
2686  */
2687 #define PSA_KEY_DERIVATION_INPUT_SALT       ((psa_key_derivation_step_t)0x0202)
2688 
2689 /** An information string for key derivation.
2690  *
2691  * This should be a direct input.
2692  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2693  */
2694 #define PSA_KEY_DERIVATION_INPUT_INFO       ((psa_key_derivation_step_t)0x0203)
2695 
2696 /** A seed for key derivation.
2697  *
2698  * This should be a direct input.
2699  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2700  */
2701 #define PSA_KEY_DERIVATION_INPUT_SEED       ((psa_key_derivation_step_t)0x0204)
2702 
2703 /** A cost parameter for password hashing / key stretching.
2704  *
2705  * This must be a direct input, passed to psa_key_derivation_input_integer().
2706  */
2707 #define PSA_KEY_DERIVATION_INPUT_COST       ((psa_key_derivation_step_t)0x0205)
2708 
2709 /**@}*/
2710 
2711 /** \defgroup helper_macros Helper macros
2712  * @{
2713  */
2714 
2715 /* Helper macros */
2716 
2717 /** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2718  *  regardless of the tag length they encode.
2719  *
2720  * \param aead_alg_1 An AEAD algorithm identifier.
2721  * \param aead_alg_2 An AEAD algorithm identifier.
2722  *
2723  * \return           1 if both identifiers refer to the same AEAD algorithm,
2724  *                   0 otherwise.
2725  *                   Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2726  *                   a supported AEAD algorithm.
2727  */
2728 #define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2729     (!(((aead_alg_1) ^ (aead_alg_2)) & \
2730        ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2731 
2732 /**@}*/
2733 
2734 #endif /* PSA_CRYPTO_VALUES_H */
2735