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 /* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
894 #define PSA_ALG_NONE                            ((psa_algorithm_t)0)
895 /* *INDENT-ON* */
896 
897 #define PSA_ALG_HASH_MASK                       ((psa_algorithm_t) 0x000000ff)
898 /** MD5 */
899 #define PSA_ALG_MD5                             ((psa_algorithm_t) 0x02000003)
900 /** PSA_ALG_RIPEMD160 */
901 #define PSA_ALG_RIPEMD160                       ((psa_algorithm_t) 0x02000004)
902 /** SHA1 */
903 #define PSA_ALG_SHA_1                           ((psa_algorithm_t) 0x02000005)
904 /** SHA2-224 */
905 #define PSA_ALG_SHA_224                         ((psa_algorithm_t) 0x02000008)
906 /** SHA2-256 */
907 #define PSA_ALG_SHA_256                         ((psa_algorithm_t) 0x02000009)
908 /** SHA2-384 */
909 #define PSA_ALG_SHA_384                         ((psa_algorithm_t) 0x0200000a)
910 /** SHA2-512 */
911 #define PSA_ALG_SHA_512                         ((psa_algorithm_t) 0x0200000b)
912 /** SHA2-512/224 */
913 #define PSA_ALG_SHA_512_224                     ((psa_algorithm_t) 0x0200000c)
914 /** SHA2-512/256 */
915 #define PSA_ALG_SHA_512_256                     ((psa_algorithm_t) 0x0200000d)
916 /** SHA3-224 */
917 #define PSA_ALG_SHA3_224                        ((psa_algorithm_t) 0x02000010)
918 /** SHA3-256 */
919 #define PSA_ALG_SHA3_256                        ((psa_algorithm_t) 0x02000011)
920 /** SHA3-384 */
921 #define PSA_ALG_SHA3_384                        ((psa_algorithm_t) 0x02000012)
922 /** SHA3-512 */
923 #define PSA_ALG_SHA3_512                        ((psa_algorithm_t) 0x02000013)
924 /** The first 512 bits (64 bytes) of the SHAKE256 output.
925  *
926  * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
927  * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
928  * has the same output size and a (theoretically) higher security strength.
929  */
930 #define PSA_ALG_SHAKE256_512                    ((psa_algorithm_t) 0x02000015)
931 
932 /** In a hash-and-sign algorithm policy, allow any hash algorithm.
933  *
934  * This value may be used to form the algorithm usage field of a policy
935  * for a signature algorithm that is parametrized by a hash. The key
936  * may then be used to perform operations using the same signature
937  * algorithm parametrized with any supported hash.
938  *
939  * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
940  * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
941  * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
942  * Then you may create and use a key as follows:
943  * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
944  *   ```
945  *   psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
946  *   psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
947  *   ```
948  * - Import or generate key material.
949  * - Call psa_sign_hash() or psa_verify_hash(), passing
950  *   an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
951  *   call to sign or verify a message may use a different hash.
952  *   ```
953  *   psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
954  *   psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
955  *   psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
956  *   ```
957  *
958  * This value may not be used to build other algorithms that are
959  * parametrized over a hash. For any valid use of this macro to build
960  * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
961  *
962  * This value may not be used to build an algorithm specification to
963  * perform an operation. It is only valid to build policies.
964  */
965 #define PSA_ALG_ANY_HASH                        ((psa_algorithm_t) 0x020000ff)
966 
967 #define PSA_ALG_MAC_SUBCATEGORY_MASK            ((psa_algorithm_t) 0x00c00000)
968 #define PSA_ALG_HMAC_BASE                       ((psa_algorithm_t) 0x03800000)
969 /** Macro to build an HMAC algorithm.
970  *
971  * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
972  *
973  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
974  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
975  *
976  * \return              The corresponding HMAC algorithm.
977  * \return              Unspecified if \p hash_alg is not a supported
978  *                      hash algorithm.
979  */
980 #define PSA_ALG_HMAC(hash_alg)                                  \
981     (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
982 
983 #define PSA_ALG_HMAC_GET_HASH(hmac_alg)                             \
984     (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
985 
986 /** Whether the specified algorithm is an HMAC algorithm.
987  *
988  * HMAC is a family of MAC algorithms that are based on a hash function.
989  *
990  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
991  *
992  * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
993  *         This macro may return either 0 or 1 if \p alg is not a supported
994  *         algorithm identifier.
995  */
996 #define PSA_ALG_IS_HMAC(alg)                                            \
997     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
998      PSA_ALG_HMAC_BASE)
999 
1000 /* In the encoding of a MAC algorithm, the bits corresponding to
1001  * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
1002  * truncated. As an exception, the value 0 means the untruncated algorithm,
1003  * whatever its length is. The length is encoded in 6 bits, so it can
1004  * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
1005  * to full length is correctly encoded as 0 and any non-trivial truncation
1006  * is correctly encoded as a value between 1 and 63. */
1007 #define PSA_ALG_MAC_TRUNCATION_MASK             ((psa_algorithm_t) 0x003f0000)
1008 #define PSA_MAC_TRUNCATION_OFFSET 16
1009 
1010 /* In the encoding of a MAC algorithm, the bit corresponding to
1011  * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1012  * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1013  * algorithm policy can be used with any algorithm corresponding to the
1014  * same base class and having a (potentially truncated) MAC length greater or
1015  * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
1016 #define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG   ((psa_algorithm_t) 0x00008000)
1017 
1018 /** Macro to build a truncated MAC algorithm.
1019  *
1020  * A truncated MAC algorithm is identical to the corresponding MAC
1021  * algorithm except that the MAC value for the truncated algorithm
1022  * consists of only the first \p mac_length bytes of the MAC value
1023  * for the untruncated algorithm.
1024  *
1025  * \note    This macro may allow constructing algorithm identifiers that
1026  *          are not valid, either because the specified length is larger
1027  *          than the untruncated MAC or because the specified length is
1028  *          smaller than permitted by the implementation.
1029  *
1030  * \note    It is implementation-defined whether a truncated MAC that
1031  *          is truncated to the same length as the MAC of the untruncated
1032  *          algorithm is considered identical to the untruncated algorithm
1033  *          for policy comparison purposes.
1034  *
1035  * \param mac_alg       A MAC algorithm identifier (value of type
1036  *                      #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1037  *                      is true). This may be a truncated or untruncated
1038  *                      MAC algorithm.
1039  * \param mac_length    Desired length of the truncated MAC in bytes.
1040  *                      This must be at most the full length of the MAC
1041  *                      and must be at least an implementation-specified
1042  *                      minimum. The implementation-specified minimum
1043  *                      shall not be zero.
1044  *
1045  * \return              The corresponding MAC algorithm with the specified
1046  *                      length.
1047  * \return              Unspecified if \p mac_alg is not a supported
1048  *                      MAC algorithm or if \p mac_length is too small or
1049  *                      too large for the specified MAC algorithm.
1050  */
1051 #define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length)              \
1052     (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK |               \
1053                     PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) |   \
1054      ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1055 
1056 /** Macro to build the base MAC algorithm corresponding to a truncated
1057  * MAC algorithm.
1058  *
1059  * \param mac_alg       A MAC algorithm identifier (value of type
1060  *                      #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1061  *                      is true). This may be a truncated or untruncated
1062  *                      MAC algorithm.
1063  *
1064  * \return              The corresponding base MAC algorithm.
1065  * \return              Unspecified if \p mac_alg is not a supported
1066  *                      MAC algorithm.
1067  */
1068 #define PSA_ALG_FULL_LENGTH_MAC(mac_alg)                        \
1069     ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK |                \
1070                    PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
1071 
1072 /** Length to which a MAC algorithm is truncated.
1073  *
1074  * \param mac_alg       A MAC algorithm identifier (value of type
1075  *                      #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1076  *                      is true).
1077  *
1078  * \return              Length of the truncated MAC in bytes.
1079  * \return              0 if \p mac_alg is a non-truncated MAC algorithm.
1080  * \return              Unspecified if \p mac_alg is not a supported
1081  *                      MAC algorithm.
1082  */
1083 #define PSA_MAC_TRUNCATED_LENGTH(mac_alg)                               \
1084     (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1085 
1086 /** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1087  *
1088  * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1089  * sharing the same base algorithm, and where the (potentially truncated) MAC
1090  * length of the specific algorithm is equal to or larger then the wildcard
1091  * algorithm's minimum MAC length.
1092  *
1093  * \note    When setting the minimum required MAC length to less than the
1094  *          smallest MAC length allowed by the base algorithm, this effectively
1095  *          becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1096  *
1097  * \param mac_alg         A MAC algorithm identifier (value of type
1098  *                        #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1099  *                        is true).
1100  * \param min_mac_length  Desired minimum length of the message authentication
1101  *                        code in bytes. This must be at most the untruncated
1102  *                        length of the MAC and must be at least 1.
1103  *
1104  * \return                The corresponding MAC wildcard algorithm with the
1105  *                        specified minimum length.
1106  * \return                Unspecified if \p mac_alg is not a supported MAC
1107  *                        algorithm or if \p min_mac_length is less than 1 or
1108  *                        too large for the specified MAC algorithm.
1109  */
1110 #define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length)   \
1111     (PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) |              \
1112      PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)
1113 
1114 #define PSA_ALG_CIPHER_MAC_BASE                 ((psa_algorithm_t) 0x03c00000)
1115 /** The CBC-MAC construction over a block cipher
1116  *
1117  * \warning CBC-MAC is insecure in many cases.
1118  * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1119  */
1120 #define PSA_ALG_CBC_MAC                         ((psa_algorithm_t) 0x03c00100)
1121 /** The CMAC construction over a block cipher */
1122 #define PSA_ALG_CMAC                            ((psa_algorithm_t) 0x03c00200)
1123 
1124 /** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1125  *
1126  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1127  *
1128  * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1129  *         This macro may return either 0 or 1 if \p alg is not a supported
1130  *         algorithm identifier.
1131  */
1132 #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg)                                \
1133     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1134      PSA_ALG_CIPHER_MAC_BASE)
1135 
1136 #define PSA_ALG_CIPHER_STREAM_FLAG              ((psa_algorithm_t) 0x00800000)
1137 #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG          ((psa_algorithm_t) 0x00400000)
1138 
1139 /** Whether the specified algorithm is a stream cipher.
1140  *
1141  * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1142  * by applying a bitwise-xor with a stream of bytes that is generated
1143  * from a key.
1144  *
1145  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1146  *
1147  * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1148  *         This macro may return either 0 or 1 if \p alg is not a supported
1149  *         algorithm identifier or if it is not a symmetric cipher algorithm.
1150  */
1151 #define PSA_ALG_IS_STREAM_CIPHER(alg)            \
1152     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1153      (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1154 
1155 /** The stream cipher mode of a stream cipher algorithm.
1156  *
1157  * The underlying stream cipher is determined by the key type.
1158  * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
1159  */
1160 #define PSA_ALG_STREAM_CIPHER                   ((psa_algorithm_t) 0x04800100)
1161 
1162 /** The CTR stream cipher mode.
1163  *
1164  * CTR is a stream cipher which is built from a block cipher.
1165  * The underlying block cipher is determined by the key type.
1166  * For example, to use AES-128-CTR, use this algorithm with
1167  * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1168  */
1169 #define PSA_ALG_CTR                             ((psa_algorithm_t) 0x04c01000)
1170 
1171 /** The CFB stream cipher mode.
1172  *
1173  * The underlying block cipher is determined by the key type.
1174  */
1175 #define PSA_ALG_CFB                             ((psa_algorithm_t) 0x04c01100)
1176 
1177 /** The OFB stream cipher mode.
1178  *
1179  * The underlying block cipher is determined by the key type.
1180  */
1181 #define PSA_ALG_OFB                             ((psa_algorithm_t) 0x04c01200)
1182 
1183 /** The XTS cipher mode.
1184  *
1185  * XTS is a cipher mode which is built from a block cipher. It requires at
1186  * least one full block of input, but beyond this minimum the input
1187  * does not need to be a whole number of blocks.
1188  */
1189 #define PSA_ALG_XTS                             ((psa_algorithm_t) 0x0440ff00)
1190 
1191 /** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1192  *
1193  * \warning ECB mode does not protect the confidentiality of the encrypted data
1194  * except in extremely narrow circumstances. It is recommended that applications
1195  * only use ECB if they need to construct an operating mode that the
1196  * implementation does not provide. Implementations are encouraged to provide
1197  * the modes that applications need in preference to supporting direct access
1198  * to ECB.
1199  *
1200  * The underlying block cipher is determined by the key type.
1201  *
1202  * This symmetric cipher mode can only be used with messages whose lengths are a
1203  * multiple of the block size of the chosen block cipher.
1204  *
1205  * ECB mode does not accept an initialization vector (IV). When using a
1206  * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1207  * and psa_cipher_set_iv() must not be called.
1208  */
1209 #define PSA_ALG_ECB_NO_PADDING                  ((psa_algorithm_t) 0x04404400)
1210 
1211 /** The CBC block cipher chaining mode, with no padding.
1212  *
1213  * The underlying block cipher is determined by the key type.
1214  *
1215  * This symmetric cipher mode can only be used with messages whose lengths
1216  * are whole number of blocks for the chosen block cipher.
1217  */
1218 #define PSA_ALG_CBC_NO_PADDING                  ((psa_algorithm_t) 0x04404000)
1219 
1220 /** The CBC block cipher chaining mode with PKCS#7 padding.
1221  *
1222  * The underlying block cipher is determined by the key type.
1223  *
1224  * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1225  */
1226 #define PSA_ALG_CBC_PKCS7                       ((psa_algorithm_t) 0x04404100)
1227 
1228 #define PSA_ALG_AEAD_FROM_BLOCK_FLAG            ((psa_algorithm_t) 0x00400000)
1229 
1230 /** Whether the specified algorithm is an AEAD mode on a block cipher.
1231  *
1232  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1233  *
1234  * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1235  *         a block cipher, 0 otherwise.
1236  *         This macro may return either 0 or 1 if \p alg is not a supported
1237  *         algorithm identifier.
1238  */
1239 #define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg)    \
1240     (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1241      (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1242 
1243 /** The CCM authenticated encryption algorithm.
1244  *
1245  * The underlying block cipher is determined by the key type.
1246  */
1247 #define PSA_ALG_CCM                             ((psa_algorithm_t) 0x05500100)
1248 
1249 /** The CCM* cipher mode without authentication.
1250  *
1251  * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1252  * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1253  *
1254  * The underlying block cipher is determined by the key type.
1255  *
1256  * Currently only 13-byte long IV's are supported.
1257  */
1258 #define PSA_ALG_CCM_STAR_NO_TAG                 ((psa_algorithm_t) 0x04c01300)
1259 
1260 /** The GCM authenticated encryption algorithm.
1261  *
1262  * The underlying block cipher is determined by the key type.
1263  */
1264 #define PSA_ALG_GCM                             ((psa_algorithm_t) 0x05500200)
1265 
1266 /** The Chacha20-Poly1305 AEAD algorithm.
1267  *
1268  * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1269  *
1270  * Implementations must support 12-byte nonces, may support 8-byte nonces,
1271  * and should reject other sizes.
1272  *
1273  * Implementations must support 16-byte tags and should reject other sizes.
1274  */
1275 #define PSA_ALG_CHACHA20_POLY1305               ((psa_algorithm_t) 0x05100500)
1276 
1277 /* In the encoding of an AEAD algorithm, the bits corresponding to
1278  * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1279  * The constants for default lengths follow this encoding.
1280  */
1281 #define PSA_ALG_AEAD_TAG_LENGTH_MASK            ((psa_algorithm_t) 0x003f0000)
1282 #define PSA_AEAD_TAG_LENGTH_OFFSET 16
1283 
1284 /* In the encoding of an AEAD algorithm, the bit corresponding to
1285  * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1286  * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1287  * algorithm policy can be used with any algorithm corresponding to the
1288  * same base class and having a tag length greater than or equal to the one
1289  * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1290 #define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG  ((psa_algorithm_t) 0x00008000)
1291 
1292 /** Macro to build a shortened AEAD algorithm.
1293  *
1294  * A shortened AEAD algorithm is similar to the corresponding AEAD
1295  * algorithm, but has an authentication tag that consists of fewer bytes.
1296  * Depending on the algorithm, the tag length may affect the calculation
1297  * of the ciphertext.
1298  *
1299  * \param aead_alg      An AEAD algorithm identifier (value of type
1300  *                      #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
1301  *                      is true).
1302  * \param tag_length    Desired length of the authentication tag in bytes.
1303  *
1304  * \return              The corresponding AEAD algorithm with the specified
1305  *                      length.
1306  * \return              Unspecified if \p aead_alg is not a supported
1307  *                      AEAD algorithm or if \p tag_length is not valid
1308  *                      for the specified AEAD algorithm.
1309  */
1310 #define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length)           \
1311     (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK |                     \
1312                      PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) |         \
1313      ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET &                      \
1314         PSA_ALG_AEAD_TAG_LENGTH_MASK))
1315 
1316 /** Retrieve the tag length of a specified AEAD algorithm
1317  *
1318  * \param aead_alg      An AEAD algorithm identifier (value of type
1319  *                      #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
1320  *                      is true).
1321  *
1322  * \return              The tag length specified by the input algorithm.
1323  * \return              Unspecified if \p aead_alg is not a supported
1324  *                      AEAD algorithm.
1325  */
1326 #define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg)                           \
1327     (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >>                     \
1328      PSA_AEAD_TAG_LENGTH_OFFSET)
1329 
1330 /** Calculate the corresponding AEAD algorithm with the default tag length.
1331  *
1332  * \param aead_alg      An AEAD algorithm (\c PSA_ALG_XXX value such that
1333  *                      #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1334  *
1335  * \return              The corresponding AEAD algorithm with the default
1336  *                      tag length for that algorithm.
1337  */
1338 #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg)                   \
1339     (                                                                    \
1340         PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1341         PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1342         PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
1343         0)
1344 #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref)         \
1345     PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) ==                      \
1346     PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ?                            \
1347     ref :
1348 
1349 /** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1350  *
1351  * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1352  * sharing the same base algorithm, and where the tag length of the specific
1353  * algorithm is equal to or larger then the minimum tag length specified by the
1354  * wildcard algorithm.
1355  *
1356  * \note    When setting the minimum required tag length to less than the
1357  *          smallest tag length allowed by the base algorithm, this effectively
1358  *          becomes an 'any-tag-length-allowed' policy for that base algorithm.
1359  *
1360  * \param aead_alg        An AEAD algorithm identifier (value of type
1361  *                        #psa_algorithm_t such that
1362  *                        #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1363  * \param min_tag_length  Desired minimum length of the authentication tag in
1364  *                        bytes. This must be at least 1 and at most the largest
1365  *                        allowed tag length of the algorithm.
1366  *
1367  * \return                The corresponding AEAD wildcard algorithm with the
1368  *                        specified minimum length.
1369  * \return                Unspecified if \p aead_alg is not a supported
1370  *                        AEAD algorithm or if \p min_tag_length is less than 1
1371  *                        or too large for the specified AEAD algorithm.
1372  */
1373 #define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1374     (PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) |            \
1375      PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)
1376 
1377 #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE          ((psa_algorithm_t) 0x06000200)
1378 /** RSA PKCS#1 v1.5 signature with hashing.
1379  *
1380  * This is the signature scheme defined by RFC 8017
1381  * (PKCS#1: RSA Cryptography Specifications) under the name
1382  * RSASSA-PKCS1-v1_5.
1383  *
1384  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1385  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1386  *                      This includes #PSA_ALG_ANY_HASH
1387  *                      when specifying the algorithm in a usage policy.
1388  *
1389  * \return              The corresponding RSA PKCS#1 v1.5 signature algorithm.
1390  * \return              Unspecified if \p hash_alg is not a supported
1391  *                      hash algorithm.
1392  */
1393 #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg)                             \
1394     (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1395 /** Raw PKCS#1 v1.5 signature.
1396  *
1397  * The input to this algorithm is the DigestInfo structure used by
1398  * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1399  * steps 3&ndash;6.
1400  */
1401 #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1402 #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)                               \
1403     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1404 
1405 #define PSA_ALG_RSA_PSS_BASE               ((psa_algorithm_t) 0x06000300)
1406 #define PSA_ALG_RSA_PSS_ANY_SALT_BASE      ((psa_algorithm_t) 0x06001300)
1407 /** RSA PSS signature with hashing.
1408  *
1409  * This is the signature scheme defined by RFC 8017
1410  * (PKCS#1: RSA Cryptography Specifications) under the name
1411  * RSASSA-PSS, with the message generation function MGF1, and with
1412  * a salt length equal to the length of the hash, or the largest
1413  * possible salt length for the algorithm and key size if that is
1414  * smaller than the hash length. The specified hash algorithm is
1415  * used to hash the input message, to create the salted hash, and
1416  * for the mask generation.
1417  *
1418  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1419  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1420  *                      This includes #PSA_ALG_ANY_HASH
1421  *                      when specifying the algorithm in a usage policy.
1422  *
1423  * \return              The corresponding RSA PSS signature algorithm.
1424  * \return              Unspecified if \p hash_alg is not a supported
1425  *                      hash algorithm.
1426  */
1427 #define PSA_ALG_RSA_PSS(hash_alg)                               \
1428     (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1429 
1430 /** RSA PSS signature with hashing with relaxed verification.
1431  *
1432  * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1433  * but allows an arbitrary salt length (including \c 0) when verifying a
1434  * signature.
1435  *
1436  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1437  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1438  *                      This includes #PSA_ALG_ANY_HASH
1439  *                      when specifying the algorithm in a usage policy.
1440  *
1441  * \return              The corresponding RSA PSS signature algorithm.
1442  * \return              Unspecified if \p hash_alg is not a supported
1443  *                      hash algorithm.
1444  */
1445 #define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg)                      \
1446     (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1447 
1448 /** Whether the specified algorithm is RSA PSS with standard salt.
1449  *
1450  * \param alg           An algorithm value or an algorithm policy wildcard.
1451  *
1452  * \return              1 if \p alg is of the form
1453  *                      #PSA_ALG_RSA_PSS(\c hash_alg),
1454  *                      where \c hash_alg is a hash algorithm or
1455  *                      #PSA_ALG_ANY_HASH. 0 otherwise.
1456  *                      This macro may return either 0 or 1 if \p alg is not
1457  *                      a supported algorithm identifier or policy.
1458  */
1459 #define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg)                   \
1460     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1461 
1462 /** Whether the specified algorithm is RSA PSS with any salt.
1463  *
1464  * \param alg           An algorithm value or an algorithm policy wildcard.
1465  *
1466  * \return              1 if \p alg is of the form
1467  *                      #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1468  *                      where \c hash_alg is a hash algorithm or
1469  *                      #PSA_ALG_ANY_HASH. 0 otherwise.
1470  *                      This macro may return either 0 or 1 if \p alg is not
1471  *                      a supported algorithm identifier or policy.
1472  */
1473 #define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)                                \
1474     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1475 
1476 /** Whether the specified algorithm is RSA PSS.
1477  *
1478  * This includes any of the RSA PSS algorithm variants, regardless of the
1479  * constraints on salt length.
1480  *
1481  * \param alg           An algorithm value or an algorithm policy wildcard.
1482  *
1483  * \return              1 if \p alg is of the form
1484  *                      #PSA_ALG_RSA_PSS(\c hash_alg) or
1485  *                      #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1486  *                      where \c hash_alg is a hash algorithm or
1487  *                      #PSA_ALG_ANY_HASH. 0 otherwise.
1488  *                      This macro may return either 0 or 1 if \p alg is not
1489  *                      a supported algorithm identifier or policy.
1490  */
1491 #define PSA_ALG_IS_RSA_PSS(alg)                                 \
1492     (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) ||                   \
1493      PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
1494 
1495 #define PSA_ALG_ECDSA_BASE                      ((psa_algorithm_t) 0x06000600)
1496 /** ECDSA signature with hashing.
1497  *
1498  * This is the ECDSA signature scheme defined by ANSI X9.62,
1499  * with a random per-message secret number (*k*).
1500  *
1501  * The representation of the signature as a byte string consists of
1502  * the concatenation of the signature values *r* and *s*. Each of
1503  * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1504  * of the base point of the curve in octets. Each value is represented
1505  * in big-endian order (most significant octet first).
1506  *
1507  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1508  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1509  *                      This includes #PSA_ALG_ANY_HASH
1510  *                      when specifying the algorithm in a usage policy.
1511  *
1512  * \return              The corresponding ECDSA signature algorithm.
1513  * \return              Unspecified if \p hash_alg is not a supported
1514  *                      hash algorithm.
1515  */
1516 #define PSA_ALG_ECDSA(hash_alg)                                 \
1517     (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1518 /** ECDSA signature without hashing.
1519  *
1520  * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1521  * without specifying a hash algorithm. This algorithm may only be
1522  * used to sign or verify a sequence of bytes that should be an
1523  * already-calculated hash. Note that the input is padded with
1524  * zeros on the left or truncated on the left as required to fit
1525  * the curve size.
1526  */
1527 #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1528 #define PSA_ALG_DETERMINISTIC_ECDSA_BASE        ((psa_algorithm_t) 0x06000700)
1529 /** Deterministic ECDSA signature with hashing.
1530  *
1531  * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1532  *
1533  * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1534  *
1535  * Note that when this algorithm is used for verification, signatures
1536  * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1537  * same private key are accepted. In other words,
1538  * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1539  * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1540  *
1541  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1542  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1543  *                      This includes #PSA_ALG_ANY_HASH
1544  *                      when specifying the algorithm in a usage policy.
1545  *
1546  * \return              The corresponding deterministic ECDSA signature
1547  *                      algorithm.
1548  * \return              Unspecified if \p hash_alg is not a supported
1549  *                      hash algorithm.
1550  */
1551 #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg)                           \
1552     (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1553 #define PSA_ALG_ECDSA_DETERMINISTIC_FLAG        ((psa_algorithm_t) 0x00000100)
1554 #define PSA_ALG_IS_ECDSA(alg)                                           \
1555     (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) ==  \
1556      PSA_ALG_ECDSA_BASE)
1557 #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)             \
1558     (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
1559 #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)                             \
1560     (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1561 #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg)                                \
1562     (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1563 
1564 /** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1565  * using standard parameters.
1566  *
1567  * Contexts are not supported in the current version of this specification
1568  * because there is no suitable signature interface that can take the
1569  * context as a parameter. A future version of this specification may add
1570  * suitable functions and extend this algorithm to support contexts.
1571  *
1572  * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1573  * In this specification, the following curves are supported:
1574  * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1575  *   in RFC 8032.
1576  *   The curve is Edwards25519.
1577  *   The hash function used internally is SHA-512.
1578  * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1579  *   in RFC 8032.
1580  *   The curve is Edwards448.
1581  *   The hash function used internally is the first 114 bytes of the
1582  *   SHAKE256 output.
1583  *
1584  * This algorithm can be used with psa_sign_message() and
1585  * psa_verify_message(). Since there is no prehashing, it cannot be used
1586  * with psa_sign_hash() or psa_verify_hash().
1587  *
1588  * The signature format is the concatenation of R and S as defined by
1589  * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1590  * string for Ed448).
1591  */
1592 #define PSA_ALG_PURE_EDDSA                      ((psa_algorithm_t) 0x06000800)
1593 
1594 #define PSA_ALG_HASH_EDDSA_BASE                 ((psa_algorithm_t) 0x06000900)
1595 #define PSA_ALG_IS_HASH_EDDSA(alg)              \
1596     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1597 
1598 /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1599  * using SHA-512 and the Edwards25519 curve.
1600  *
1601  * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1602  *
1603  * This algorithm is Ed25519 as specified in RFC 8032.
1604  * The curve is Edwards25519.
1605  * The prehash is SHA-512.
1606  * The hash function used internally is SHA-512.
1607  *
1608  * This is a hash-and-sign algorithm: to calculate a signature,
1609  * you can either:
1610  * - call psa_sign_message() on the message;
1611  * - or calculate the SHA-512 hash of the message
1612  *   with psa_hash_compute()
1613  *   or with a multi-part hash operation started with psa_hash_setup(),
1614  *   using the hash algorithm #PSA_ALG_SHA_512,
1615  *   then sign the calculated hash with psa_sign_hash().
1616  * Verifying a signature is similar, using psa_verify_message() or
1617  * psa_verify_hash() instead of the signature function.
1618  */
1619 #define PSA_ALG_ED25519PH                               \
1620     (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1621 
1622 /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1623  * using SHAKE256 and the Edwards448 curve.
1624  *
1625  * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1626  *
1627  * This algorithm is Ed448 as specified in RFC 8032.
1628  * The curve is Edwards448.
1629  * The prehash is the first 64 bytes of the SHAKE256 output.
1630  * The hash function used internally is the first 114 bytes of the
1631  * SHAKE256 output.
1632  *
1633  * This is a hash-and-sign algorithm: to calculate a signature,
1634  * you can either:
1635  * - call psa_sign_message() on the message;
1636  * - or calculate the first 64 bytes of the SHAKE256 output of the message
1637  *   with psa_hash_compute()
1638  *   or with a multi-part hash operation started with psa_hash_setup(),
1639  *   using the hash algorithm #PSA_ALG_SHAKE256_512,
1640  *   then sign the calculated hash with psa_sign_hash().
1641  * Verifying a signature is similar, using psa_verify_message() or
1642  * psa_verify_hash() instead of the signature function.
1643  */
1644 #define PSA_ALG_ED448PH                                 \
1645     (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1646 
1647 /* Default definition, to be overridden if the library is extended with
1648  * more hash-and-sign algorithms that we want to keep out of this header
1649  * file. */
1650 #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1651 
1652 /** Whether the specified algorithm is a signature algorithm that can be used
1653  * with psa_sign_hash() and psa_verify_hash().
1654  *
1655  * This encompasses all strict hash-and-sign algorithms categorized by
1656  * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1657  * paradigm more loosely:
1658  * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1659  * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
1660  *
1661  * \param alg An algorithm identifier (value of type psa_algorithm_t).
1662  *
1663  * \return 1 if alg is a signature algorithm that can be used to sign a
1664  *         hash. 0 if alg is a signature algorithm that can only be used
1665  *         to sign a message. 0 if alg is not a signature algorithm.
1666  *         This macro can return either 0 or 1 if alg is not a
1667  *         supported algorithm identifier.
1668  */
1669 #define PSA_ALG_IS_SIGN_HASH(alg)                                       \
1670     (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||    \
1671      PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) ||             \
1672      PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1673 
1674 /** Whether the specified algorithm is a signature algorithm that can be used
1675  * with psa_sign_message() and psa_verify_message().
1676  *
1677  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1678  *
1679  * \return 1 if alg is a signature algorithm that can be used to sign a
1680  *         message. 0 if \p alg is a signature algorithm that can only be used
1681  *         to sign an already-calculated hash. 0 if \p alg is not a signature
1682  *         algorithm. This macro can return either 0 or 1 if \p alg is not a
1683  *         supported algorithm identifier.
1684  */
1685 #define PSA_ALG_IS_SIGN_MESSAGE(alg)                                    \
1686     (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
1687 
1688 /** Whether the specified algorithm is a hash-and-sign algorithm.
1689  *
1690  * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1691  * structured in two parts: first the calculation of a hash in a way that
1692  * does not depend on the key, then the calculation of a signature from the
1693  * hash value and the key. Hash-and-sign algorithms encode the hash
1694  * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1695  * to extract this algorithm.
1696  *
1697  * Thus, for a hash-and-sign algorithm,
1698  * `psa_sign_message(key, alg, input, ...)` is equivalent to
1699  * ```
1700  * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1701  * psa_sign_hash(key, alg, hash, ..., signature, ...);
1702  * ```
1703  * Most usefully, separating the hash from the signature allows the hash
1704  * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1705  * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1706  * calculating the hash and then calling psa_verify_hash().
1707  *
1708  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1709  *
1710  * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1711  *         This macro may return either 0 or 1 if \p alg is not a supported
1712  *         algorithm identifier.
1713  */
1714 #define PSA_ALG_IS_HASH_AND_SIGN(alg)                                   \
1715     (PSA_ALG_IS_SIGN_HASH(alg) &&                                       \
1716      ((alg) & PSA_ALG_HASH_MASK) != 0)
1717 
1718 /** Get the hash used by a hash-and-sign signature algorithm.
1719  *
1720  * A hash-and-sign algorithm is a signature algorithm which is
1721  * composed of two phases: first a hashing phase which does not use
1722  * the key and produces a hash of the input message, then a signing
1723  * phase which only uses the hash and the key and not the message
1724  * itself.
1725  *
1726  * \param alg   A signature algorithm (\c PSA_ALG_XXX value such that
1727  *              #PSA_ALG_IS_SIGN(\p alg) is true).
1728  *
1729  * \return      The underlying hash algorithm if \p alg is a hash-and-sign
1730  *              algorithm.
1731  * \return      0 if \p alg is a signature algorithm that does not
1732  *              follow the hash-and-sign structure.
1733  * \return      Unspecified if \p alg is not a signature algorithm or
1734  *              if it is not supported by the implementation.
1735  */
1736 #define PSA_ALG_SIGN_GET_HASH(alg)                                     \
1737     (PSA_ALG_IS_HASH_AND_SIGN(alg) ?                                   \
1738      ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH :             \
1739      0)
1740 
1741 /** RSA PKCS#1 v1.5 encryption.
1742  */
1743 #define PSA_ALG_RSA_PKCS1V15_CRYPT              ((psa_algorithm_t) 0x07000200)
1744 
1745 #define PSA_ALG_RSA_OAEP_BASE                   ((psa_algorithm_t) 0x07000300)
1746 /** RSA OAEP encryption.
1747  *
1748  * This is the encryption scheme defined by RFC 8017
1749  * (PKCS#1: RSA Cryptography Specifications) under the name
1750  * RSAES-OAEP, with the message generation function MGF1.
1751  *
1752  * \param hash_alg      The hash algorithm (\c PSA_ALG_XXX value such that
1753  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1754  *                      for MGF1.
1755  *
1756  * \return              The corresponding RSA OAEP encryption algorithm.
1757  * \return              Unspecified if \p hash_alg is not a supported
1758  *                      hash algorithm.
1759  */
1760 #define PSA_ALG_RSA_OAEP(hash_alg)                              \
1761     (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1762 #define PSA_ALG_IS_RSA_OAEP(alg)                                \
1763     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1764 #define PSA_ALG_RSA_OAEP_GET_HASH(alg)                          \
1765     (PSA_ALG_IS_RSA_OAEP(alg) ?                                 \
1766      ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH :      \
1767      0)
1768 
1769 #define PSA_ALG_HKDF_BASE                       ((psa_algorithm_t) 0x08000100)
1770 /** Macro to build an HKDF algorithm.
1771  *
1772  * For example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)` is HKDF using HMAC-SHA-256.
1773  *
1774  * This key derivation algorithm uses the following inputs:
1775  * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1776  *   It is optional; if omitted, the derivation uses an empty salt.
1777  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1778  * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1779  * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1780  * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1781  * starting to generate output.
1782  *
1783  *  \warning  HKDF processes the salt as follows: first hash it with hash_alg
1784  *  if the salt is longer than the block size of the hash algorithm; then
1785  *  pad with null bytes up to the block size. As a result, it is possible
1786  *  for distinct salt inputs to result in the same outputs. To ensure
1787  *  unique outputs, it is recommended to use a fixed length for salt values.
1788  *
1789  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1790  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1791  *
1792  * \return              The corresponding HKDF algorithm.
1793  * \return              Unspecified if \p hash_alg is not a supported
1794  *                      hash algorithm.
1795  */
1796 #define PSA_ALG_HKDF(hash_alg)                                  \
1797     (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1798 /** Whether the specified algorithm is an HKDF algorithm.
1799  *
1800  * HKDF is a family of key derivation algorithms that are based on a hash
1801  * function and the HMAC construction.
1802  *
1803  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1804  *
1805  * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1806  *         This macro may return either 0 or 1 if \c alg is not a supported
1807  *         key derivation algorithm identifier.
1808  */
1809 #define PSA_ALG_IS_HKDF(alg)                            \
1810     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1811 #define PSA_ALG_HKDF_GET_HASH(hkdf_alg)                         \
1812     (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1813 
1814 #define PSA_ALG_HKDF_EXTRACT_BASE                       ((psa_algorithm_t) 0x08000400)
1815 /** Macro to build an HKDF-Extract algorithm.
1816  *
1817  * For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA_256)` is
1818  * HKDF-Extract using HMAC-SHA-256.
1819  *
1820  * This key derivation algorithm uses the following inputs:
1821  *  - PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1822  *  - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the
1823  *    "extract" step.
1824  * The inputs are mandatory and must be passed in the order above.
1825  * Each input may only be passed once.
1826  *
1827  *  \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF
1828  *  should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided
1829  *  as a separate algorithm for the sake of protocols that use it as a
1830  *  building block. It may also be a slight performance optimization
1831  *  in applications that use HKDF with the same salt and key but many
1832  *  different info strings.
1833  *
1834  *  \warning  HKDF processes the salt as follows: first hash it with hash_alg
1835  *  if the salt is longer than the block size of the hash algorithm; then
1836  *  pad with null bytes up to the block size. As a result, it is possible
1837  *  for distinct salt inputs to result in the same outputs. To ensure
1838  *  unique outputs, it is recommended to use a fixed length for salt values.
1839  *
1840  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1841  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1842  *
1843  * \return              The corresponding HKDF-Extract algorithm.
1844  * \return              Unspecified if \p hash_alg is not a supported
1845  *                      hash algorithm.
1846  */
1847 #define PSA_ALG_HKDF_EXTRACT(hash_alg)                                  \
1848     (PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1849 /** Whether the specified algorithm is an HKDF-Extract algorithm.
1850  *
1851  * HKDF-Extract is a family of key derivation algorithms that are based
1852  * on a hash function and the HMAC construction.
1853  *
1854  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1855  *
1856  * \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.
1857  *         This macro may return either 0 or 1 if \c alg is not a supported
1858  *         key derivation algorithm identifier.
1859  */
1860 #define PSA_ALG_IS_HKDF_EXTRACT(alg)                            \
1861     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)
1862 
1863 #define PSA_ALG_HKDF_EXPAND_BASE                       ((psa_algorithm_t) 0x08000500)
1864 /** Macro to build an HKDF-Expand algorithm.
1865  *
1866  * For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA_256)` is
1867  * HKDF-Expand using HMAC-SHA-256.
1868  *
1869  * This key derivation algorithm uses the following inputs:
1870  *  - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).
1871  *  - PSA_KEY_DERIVATION_INPUT_INFO is the info string.
1872  *
1873  *  The inputs are mandatory and must be passed in the order above.
1874  *  Each input may only be passed once.
1875  *
1876  *  \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`
1877  *  should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as
1878  *  a separate algorithm for the sake of protocols that use it as a building
1879  *  block. It may also be a slight performance optimization in applications
1880  *  that use HKDF with the same salt and key but many different info strings.
1881  *
1882  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1883  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1884  *
1885  * \return              The corresponding HKDF-Expand algorithm.
1886  * \return              Unspecified if \p hash_alg is not a supported
1887  *                      hash algorithm.
1888  */
1889 #define PSA_ALG_HKDF_EXPAND(hash_alg)                                  \
1890     (PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1891 /** Whether the specified algorithm is an HKDF-Expand algorithm.
1892  *
1893  * HKDF-Expand is a family of key derivation algorithms that are based
1894  * on a hash function and the HMAC construction.
1895  *
1896  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1897  *
1898  * \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.
1899  *         This macro may return either 0 or 1 if \c alg is not a supported
1900  *         key derivation algorithm identifier.
1901  */
1902 #define PSA_ALG_IS_HKDF_EXPAND(alg)                            \
1903     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1904 
1905 /** Whether the specified algorithm is an HKDF or HKDF-Extract or
1906  *  HKDF-Expand algorithm.
1907  *
1908  *
1909  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1910  *
1911  * \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.
1912  *         This macro may return either 0 or 1 if \c alg is not a supported
1913  *         key derivation algorithm identifier.
1914  */
1915 #define PSA_ALG_IS_ANY_HKDF(alg)                                   \
1916     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE ||          \
1917      ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE ||  \
1918      ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1919 
1920 #define PSA_ALG_TLS12_PRF_BASE                  ((psa_algorithm_t) 0x08000200)
1921 /** Macro to build a TLS-1.2 PRF algorithm.
1922  *
1923  * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1924  * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1925  * used with either SHA-256 or SHA-384.
1926  *
1927  * This key derivation algorithm uses the following inputs, which must be
1928  * passed in the order given here:
1929  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1930  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1931  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1932  *
1933  * For the application to TLS-1.2 key expansion, the seed is the
1934  * concatenation of ServerHello.Random + ClientHello.Random,
1935  * and the label is "key expansion".
1936  *
1937  * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256)` represents the
1938  * TLS 1.2 PRF using HMAC-SHA-256.
1939  *
1940  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
1941  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
1942  *
1943  * \return              The corresponding TLS-1.2 PRF algorithm.
1944  * \return              Unspecified if \p hash_alg is not a supported
1945  *                      hash algorithm.
1946  */
1947 #define PSA_ALG_TLS12_PRF(hash_alg)                                  \
1948     (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1949 
1950 /** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1951  *
1952  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1953  *
1954  * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1955  *         This macro may return either 0 or 1 if \c alg is not a supported
1956  *         key derivation algorithm identifier.
1957  */
1958 #define PSA_ALG_IS_TLS12_PRF(alg)                                    \
1959     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1960 #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg)                         \
1961     (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1962 
1963 #define PSA_ALG_TLS12_PSK_TO_MS_BASE            ((psa_algorithm_t) 0x08000300)
1964 /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1965  *
1966  * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1967  * from the PreSharedKey (PSK) through the application of padding
1968  * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1969  * The latter is based on HMAC and can be used with either SHA-256
1970  * or SHA-384.
1971  *
1972  * This key derivation algorithm uses the following inputs, which must be
1973  * passed in the order given here:
1974  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1975  * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
1976  *   computation of the premaster secret. This input is optional;
1977  *   if omitted, it defaults to a string of null bytes with the same length
1978  *   as the secret (PSK) input.
1979  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1980  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1981  *
1982  * For the application to TLS-1.2, the seed (which is
1983  * forwarded to the TLS-1.2 PRF) is the concatenation of the
1984  * ClientHello.Random + ServerHello.Random,
1985  * the label is "master secret" or "extended master secret" and
1986  * the other secret depends on the key exchange specified in the cipher suite:
1987  * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
1988  *   PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
1989  * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
1990  *   (RFC 5489, Section 2), the other secret should be the output of the
1991  *   PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
1992  *   The recommended way to pass this input is to use a key derivation
1993  *   algorithm constructed as
1994  *   PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
1995  *   and to call psa_key_derivation_key_agreement(). Alternatively,
1996  *   this input may be an output of `psa_raw_key_agreement()` passed with
1997  *   psa_key_derivation_input_bytes(), or an equivalent input passed with
1998  *   psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
1999  * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
2000  *   should be the 48-byte client challenge (the PreMasterSecret of
2001  *   (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
2002  *   a 46-byte random string chosen by the client. On the server, this is
2003  *   typically an output of psa_asymmetric_decrypt() using
2004  *   PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
2005  *   with `psa_key_derivation_input_bytes()`.
2006  *
2007  * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256)` represents the
2008  * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
2009  *
2010  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
2011  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
2012  *
2013  * \return              The corresponding TLS-1.2 PSK to MS algorithm.
2014  * \return              Unspecified if \p hash_alg is not a supported
2015  *                      hash algorithm.
2016  */
2017 #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg)                                  \
2018     (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2019 
2020 /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
2021  *
2022  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2023  *
2024  * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
2025  *         This macro may return either 0 or 1 if \c alg is not a supported
2026  *         key derivation algorithm identifier.
2027  */
2028 #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg)                                    \
2029     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
2030 #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg)                         \
2031     (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
2032 
2033 /* The TLS 1.2 ECJPAKE-to-PMS KDF. It takes the shared secret K (an EC point
2034  * in case of EC J-PAKE) and calculates SHA256(K.X) that the rest of TLS 1.2
2035  * will use to derive the session secret, as defined by step 2 of
2036  * https://datatracker.ietf.org/doc/html/draft-cragie-tls-ecjpake-01#section-8.7.
2037  * Uses PSA_ALG_SHA_256.
2038  * This function takes a single input:
2039  * #PSA_KEY_DERIVATION_INPUT_SECRET is the shared secret K from EC J-PAKE.
2040  * The only supported curve is secp256r1 (the 256-bit curve in
2041  * #PSA_ECC_FAMILY_SECP_R1), so the input must be exactly 65 bytes.
2042  * The output has to be read as a single chunk of 32 bytes, defined as
2043  * PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE.
2044  */
2045 #define PSA_ALG_TLS12_ECJPAKE_TO_PMS            ((psa_algorithm_t) 0x08000609)
2046 
2047 /* This flag indicates whether the key derivation algorithm is suitable for
2048  * use on low-entropy secrets such as password - these algorithms are also
2049  * known as key stretching or password hashing schemes. These are also the
2050  * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
2051  *
2052  * Those algorithms cannot be combined with a key agreement algorithm.
2053  */
2054 #define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG  ((psa_algorithm_t) 0x00800000)
2055 
2056 #define PSA_ALG_PBKDF2_HMAC_BASE                ((psa_algorithm_t) 0x08800100)
2057 /** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
2058  *
2059  * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2060  * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
2061  * HMAC with the specified hash.
2062  * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA_256)` specifies PBKDF2
2063  * using the PRF HMAC-SHA-256.
2064  *
2065  * This key derivation algorithm uses the following inputs, which must be
2066  * provided in the following order:
2067  * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
2068  *   This input step must be used exactly once.
2069  * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
2070  *   This input step must be used one or more times; if used several times, the
2071  *   inputs will be concatenated. This can be used to build the final salt
2072  *   from multiple sources, both public and secret (also known as pepper).
2073  * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
2074  *   This input step must be used exactly once.
2075  *
2076  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
2077  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
2078  *
2079  * \return              The corresponding PBKDF2-HMAC-XXX algorithm.
2080  * \return              Unspecified if \p hash_alg is not a supported
2081  *                      hash algorithm.
2082  */
2083 #define PSA_ALG_PBKDF2_HMAC(hash_alg)                                  \
2084     (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2085 
2086 /** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
2087  *
2088  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2089  *
2090  * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
2091  *         This macro may return either 0 or 1 if \c alg is not a supported
2092  *         key derivation algorithm identifier.
2093  */
2094 #define PSA_ALG_IS_PBKDF2_HMAC(alg)                                    \
2095     (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
2096 
2097 /** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
2098  *
2099  * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2100  * This macro specifies the PBKDF2 algorithm constructed using the
2101  * AES-CMAC-PRF-128 PRF specified by RFC 4615.
2102  *
2103  * This key derivation algorithm uses the same inputs as
2104  * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
2105  */
2106 #define PSA_ALG_PBKDF2_AES_CMAC_PRF_128         ((psa_algorithm_t) 0x08800200)
2107 
2108 #define PSA_ALG_KEY_DERIVATION_MASK             ((psa_algorithm_t) 0xfe00ffff)
2109 #define PSA_ALG_KEY_AGREEMENT_MASK              ((psa_algorithm_t) 0xffff0000)
2110 
2111 /** Macro to build a combined algorithm that chains a key agreement with
2112  * a key derivation.
2113  *
2114  * \param ka_alg        A key agreement algorithm (\c PSA_ALG_XXX value such
2115  *                      that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
2116  * \param kdf_alg       A key derivation algorithm (\c PSA_ALG_XXX value such
2117  *                      that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
2118  *
2119  * \return              The corresponding key agreement and derivation
2120  *                      algorithm.
2121  * \return              Unspecified if \p ka_alg is not a supported
2122  *                      key agreement algorithm or \p kdf_alg is not a
2123  *                      supported key derivation algorithm.
2124  */
2125 #define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg)  \
2126     ((ka_alg) | (kdf_alg))
2127 
2128 #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg)                              \
2129     (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
2130 
2131 #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg)                             \
2132     (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
2133 
2134 /** Whether the specified algorithm is a raw key agreement algorithm.
2135  *
2136  * A raw key agreement algorithm is one that does not specify
2137  * a key derivation function.
2138  * Usually, raw key agreement algorithms are constructed directly with
2139  * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
2140  * constructed with #PSA_ALG_KEY_AGREEMENT().
2141  *
2142  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2143  *
2144  * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
2145  *         This macro may return either 0 or 1 if \p alg is not a supported
2146  *         algorithm identifier.
2147  */
2148 #define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)                               \
2149     (PSA_ALG_IS_KEY_AGREEMENT(alg) &&                                   \
2150      PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
2151 
2152 #define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg)     \
2153     ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
2154 
2155 /** The finite-field Diffie-Hellman (DH) key agreement algorithm.
2156  *
2157  * The shared secret produced by key agreement is
2158  * `g^{ab}` in big-endian format.
2159  * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
2160  * in bits.
2161  */
2162 #define PSA_ALG_FFDH                            ((psa_algorithm_t) 0x09010000)
2163 
2164 /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
2165  *
2166  * This includes the raw finite field Diffie-Hellman algorithm as well as
2167  * finite-field Diffie-Hellman followed by any supporter key derivation
2168  * algorithm.
2169  *
2170  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2171  *
2172  * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
2173  *         This macro may return either 0 or 1 if \c alg is not a supported
2174  *         key agreement algorithm identifier.
2175  */
2176 #define PSA_ALG_IS_FFDH(alg) \
2177     (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
2178 
2179 /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
2180  *
2181  * The shared secret produced by key agreement is the x-coordinate of
2182  * the shared secret point. It is always `ceiling(m / 8)` bytes long where
2183  * `m` is the bit size associated with the curve, i.e. the bit size of the
2184  * order of the curve's coordinate field. When `m` is not a multiple of 8,
2185  * the byte containing the most significant bit of the shared secret
2186  * is padded with zero bits. The byte order is either little-endian
2187  * or big-endian depending on the curve type.
2188  *
2189  * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
2190  *   the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2191  *   in little-endian byte order.
2192  *   The bit size is 448 for Curve448 and 255 for Curve25519.
2193  * - For Weierstrass curves over prime fields (curve types
2194  *   `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
2195  *   the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2196  *   in big-endian byte order.
2197  *   The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
2198  * - For Weierstrass curves over binary fields (curve types
2199  *   `PSA_ECC_FAMILY_SECTXXX`),
2200  *   the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2201  *   in big-endian byte order.
2202  *   The bit size is `m` for the field `F_{2^m}`.
2203  */
2204 #define PSA_ALG_ECDH                            ((psa_algorithm_t) 0x09020000)
2205 
2206 /** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2207  * algorithm.
2208  *
2209  * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2210  * elliptic curve Diffie-Hellman followed by any supporter key derivation
2211  * algorithm.
2212  *
2213  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2214  *
2215  * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2216  *         0 otherwise.
2217  *         This macro may return either 0 or 1 if \c alg is not a supported
2218  *         key agreement algorithm identifier.
2219  */
2220 #define PSA_ALG_IS_ECDH(alg) \
2221     (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
2222 
2223 /** Whether the specified algorithm encoding is a wildcard.
2224  *
2225  * Wildcard values may only be used to set the usage algorithm field in
2226  * a policy, not to perform an operation.
2227  *
2228  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2229  *
2230  * \return 1 if \c alg is a wildcard algorithm encoding.
2231  * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2232  *         an operation).
2233  * \return This macro may return either 0 or 1 if \c alg is not a supported
2234  *         algorithm identifier.
2235  */
2236 #define PSA_ALG_IS_WILDCARD(alg)                            \
2237     (PSA_ALG_IS_HASH_AND_SIGN(alg) ?                        \
2238      PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH :       \
2239      PSA_ALG_IS_MAC(alg) ?                                  \
2240      (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 :   \
2241      PSA_ALG_IS_AEAD(alg) ?                                 \
2242      (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 :  \
2243      (alg) == PSA_ALG_ANY_HASH)
2244 
2245 /** Get the hash used by a composite algorithm.
2246  *
2247  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2248  *
2249  * \return The underlying hash algorithm if alg is a composite algorithm that
2250  * uses a hash algorithm.
2251  *
2252  * \return \c 0 if alg is not a composite algorithm that uses a hash.
2253  */
2254 #define PSA_ALG_GET_HASH(alg) \
2255     (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t) 0) : 0x02000000 | ((alg) & 0x000000ff))
2256 
2257 /**@}*/
2258 
2259 /** \defgroup key_lifetimes Key lifetimes
2260  * @{
2261  */
2262 
2263 /* Note that location and persistence level values are embedded in the
2264  * persistent key store, as part of key metadata. As a consequence, they
2265  * must not be changed (unless the storage format version changes).
2266  */
2267 
2268 /** The default lifetime for volatile keys.
2269  *
2270  * A volatile key only exists as long as the identifier to it is not destroyed.
2271  * The key material is guaranteed to be erased on a power reset.
2272  *
2273  * A key with this lifetime is typically stored in the RAM area of the
2274  * PSA Crypto subsystem. However this is an implementation choice.
2275  * If an implementation stores data about the key in a non-volatile memory,
2276  * it must release all the resources associated with the key and erase the
2277  * key material if the calling application terminates.
2278  */
2279 #define PSA_KEY_LIFETIME_VOLATILE               ((psa_key_lifetime_t) 0x00000000)
2280 
2281 /** The default lifetime for persistent keys.
2282  *
2283  * A persistent key remains in storage until it is explicitly destroyed or
2284  * until the corresponding storage area is wiped. This specification does
2285  * not define any mechanism to wipe a storage area, but integrations may
2286  * provide their own mechanism (for example to perform a factory reset,
2287  * to prepare for device refurbishment, or to uninstall an application).
2288  *
2289  * This lifetime value is the default storage area for the calling
2290  * application. Integrations of Mbed TLS may support other persistent lifetimes.
2291  * See ::psa_key_lifetime_t for more information.
2292  */
2293 #define PSA_KEY_LIFETIME_PERSISTENT             ((psa_key_lifetime_t) 0x00000001)
2294 
2295 /** The persistence level of volatile keys.
2296  *
2297  * See ::psa_key_persistence_t for more information.
2298  */
2299 #define PSA_KEY_PERSISTENCE_VOLATILE            ((psa_key_persistence_t) 0x00)
2300 
2301 /** The default persistence level for persistent keys.
2302  *
2303  * See ::psa_key_persistence_t for more information.
2304  */
2305 #define PSA_KEY_PERSISTENCE_DEFAULT             ((psa_key_persistence_t) 0x01)
2306 
2307 /** A persistence level indicating that a key is never destroyed.
2308  *
2309  * See ::psa_key_persistence_t for more information.
2310  */
2311 #define PSA_KEY_PERSISTENCE_READ_ONLY           ((psa_key_persistence_t) 0xff)
2312 
2313 #define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime)      \
2314     ((psa_key_persistence_t) ((lifetime) & 0x000000ff))
2315 
2316 #define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)      \
2317     ((psa_key_location_t) ((lifetime) >> 8))
2318 
2319 /** Whether a key lifetime indicates that the key is volatile.
2320  *
2321  * A volatile key is automatically destroyed by the implementation when
2322  * the application instance terminates. In particular, a volatile key
2323  * is automatically destroyed on a power reset of the device.
2324  *
2325  * A key that is not volatile is persistent. Persistent keys are
2326  * preserved until the application explicitly destroys them or until an
2327  * implementation-specific device management event occurs (for example,
2328  * a factory reset).
2329  *
2330  * \param lifetime      The lifetime value to query (value of type
2331  *                      ::psa_key_lifetime_t).
2332  *
2333  * \return \c 1 if the key is volatile, otherwise \c 0.
2334  */
2335 #define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)  \
2336     (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2337      PSA_KEY_PERSISTENCE_VOLATILE)
2338 
2339 /** Whether a key lifetime indicates that the key is read-only.
2340  *
2341  * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2342  * They must be created through platform-specific means that bypass the API.
2343  *
2344  * Some platforms may offer ways to destroy read-only keys. For example,
2345  * consider a platform with multiple levels of privilege, where a
2346  * low-privilege application can use a key but is not allowed to destroy
2347  * it, and the platform exposes the key to the application with a read-only
2348  * lifetime. High-privilege code can destroy the key even though the
2349  * application sees the key as read-only.
2350  *
2351  * \param lifetime      The lifetime value to query (value of type
2352  *                      ::psa_key_lifetime_t).
2353  *
2354  * \return \c 1 if the key is read-only, otherwise \c 0.
2355  */
2356 #define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)  \
2357     (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2358      PSA_KEY_PERSISTENCE_READ_ONLY)
2359 
2360 /** Construct a lifetime from a persistence level and a location.
2361  *
2362  * \param persistence   The persistence level
2363  *                      (value of type ::psa_key_persistence_t).
2364  * \param location      The location indicator
2365  *                      (value of type ::psa_key_location_t).
2366  *
2367  * \return The constructed lifetime value.
2368  */
2369 #define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2370     ((location) << 8 | (persistence))
2371 
2372 /** The local storage area for persistent keys.
2373  *
2374  * This storage area is available on all systems that can store persistent
2375  * keys without delegating the storage to a third-party cryptoprocessor.
2376  *
2377  * See ::psa_key_location_t for more information.
2378  */
2379 #define PSA_KEY_LOCATION_LOCAL_STORAGE          ((psa_key_location_t) 0x000000)
2380 
2381 #define PSA_KEY_LOCATION_VENDOR_FLAG            ((psa_key_location_t) 0x800000)
2382 
2383 /* Note that key identifier values are embedded in the
2384  * persistent key store, as part of key metadata. As a consequence, they
2385  * must not be changed (unless the storage format version changes).
2386  */
2387 
2388 /** The null key identifier.
2389  */
2390 /* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
2391 #define PSA_KEY_ID_NULL                         ((psa_key_id_t)0)
2392 /* *INDENT-ON* */
2393 /** The minimum value for a key identifier chosen by the application.
2394  */
2395 #define PSA_KEY_ID_USER_MIN                     ((psa_key_id_t) 0x00000001)
2396 /** The maximum value for a key identifier chosen by the application.
2397  */
2398 #define PSA_KEY_ID_USER_MAX                     ((psa_key_id_t) 0x3fffffff)
2399 /** The minimum value for a key identifier chosen by the implementation.
2400  */
2401 #define PSA_KEY_ID_VENDOR_MIN                   ((psa_key_id_t) 0x40000000)
2402 /** The maximum value for a key identifier chosen by the implementation.
2403  */
2404 #define PSA_KEY_ID_VENDOR_MAX                   ((psa_key_id_t) 0x7fffffff)
2405 
2406 
2407 #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2408 
2409 #define MBEDTLS_SVC_KEY_ID_INIT ((psa_key_id_t) 0)
2410 #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) (id)
2411 #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) (0)
2412 
2413 /** Utility to initialize a key identifier at runtime.
2414  *
2415  * \param unused  Unused parameter.
2416  * \param key_id  Identifier of the key.
2417  */
mbedtls_svc_key_id_make(unsigned int unused,psa_key_id_t key_id)2418 static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2419     unsigned int unused, psa_key_id_t key_id)
2420 {
2421     (void) unused;
2422 
2423     return key_id;
2424 }
2425 
2426 /** Compare two key identifiers.
2427  *
2428  * \param id1 First key identifier.
2429  * \param id2 Second key identifier.
2430  *
2431  * \return Non-zero if the two key identifier are equal, zero otherwise.
2432  */
mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,mbedtls_svc_key_id_t id2)2433 static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2434                                            mbedtls_svc_key_id_t id2)
2435 {
2436     return id1 == id2;
2437 }
2438 
2439 /** Check whether a key identifier is null.
2440  *
2441  * \param key Key identifier.
2442  *
2443  * \return Non-zero if the key identifier is null, zero otherwise.
2444  */
mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2445 static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2446 {
2447     return key == 0;
2448 }
2449 
2450 #else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2451 #include "mbedtls/private_access.h"
2452 #define MBEDTLS_SVC_KEY_ID_INIT ((mbedtls_svc_key_id_t){ 0, 0 })
2453 #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) ((id).MBEDTLS_PRIVATE(key_id))
2454 #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) ((id).MBEDTLS_PRIVATE(owner))
2455 
2456 /** Utility to initialize a key identifier at runtime.
2457  *
2458  * \param owner_id Identifier of the key owner.
2459  * \param key_id   Identifier of the key.
2460  */
mbedtls_svc_key_id_make(mbedtls_key_owner_id_t owner_id,psa_key_id_t key_id)2461 static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2462     mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id)
2463 {
2464     return (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,
2465                                    .MBEDTLS_PRIVATE(owner) = owner_id };
2466 }
2467 
2468 /** Compare two key identifiers.
2469  *
2470  * \param id1 First key identifier.
2471  * \param id2 Second key identifier.
2472  *
2473  * \return Non-zero if the two key identifier are equal, zero otherwise.
2474  */
mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,mbedtls_svc_key_id_t id2)2475 static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2476                                            mbedtls_svc_key_id_t id2)
2477 {
2478     return (id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id)) &&
2479            mbedtls_key_owner_id_equal(id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner));
2480 }
2481 
2482 /** Check whether a key identifier is null.
2483  *
2484  * \param key Key identifier.
2485  *
2486  * \return Non-zero if the key identifier is null, zero otherwise.
2487  */
mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2488 static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2489 {
2490     return key.MBEDTLS_PRIVATE(key_id) == 0;
2491 }
2492 
2493 #endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2494 
2495 /**@}*/
2496 
2497 /** \defgroup policy Key policies
2498  * @{
2499  */
2500 
2501 /* Note that key usage flags are embedded in the
2502  * persistent key store, as part of key metadata. As a consequence, they
2503  * must not be changed (unless the storage format version changes).
2504  */
2505 
2506 /** Whether the key may be exported.
2507  *
2508  * A public key or the public part of a key pair may always be exported
2509  * regardless of the value of this permission flag.
2510  *
2511  * If a key does not have export permission, implementations shall not
2512  * allow the key to be exported in plain form from the cryptoprocessor,
2513  * whether through psa_export_key() or through a proprietary interface.
2514  * The key may however be exportable in a wrapped form, i.e. in a form
2515  * where it is encrypted by another key.
2516  */
2517 #define PSA_KEY_USAGE_EXPORT                    ((psa_key_usage_t) 0x00000001)
2518 
2519 /** Whether the key may be copied.
2520  *
2521  * This flag allows the use of psa_copy_key() to make a copy of the key
2522  * with the same policy or a more restrictive policy.
2523  *
2524  * For lifetimes for which the key is located in a secure element which
2525  * enforce the non-exportability of keys, copying a key outside the secure
2526  * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2527  * Copying the key inside the secure element is permitted with just
2528  * #PSA_KEY_USAGE_COPY if the secure element supports it.
2529  * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2530  * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2531  * is sufficient to permit the copy.
2532  */
2533 #define PSA_KEY_USAGE_COPY                      ((psa_key_usage_t) 0x00000002)
2534 
2535 /** Whether the key may be used to encrypt a message.
2536  *
2537  * This flag allows the key to be used for a symmetric encryption operation,
2538  * for an AEAD encryption-and-authentication operation,
2539  * or for an asymmetric encryption operation,
2540  * if otherwise permitted by the key's type and policy.
2541  *
2542  * For a key pair, this concerns the public key.
2543  */
2544 #define PSA_KEY_USAGE_ENCRYPT                   ((psa_key_usage_t) 0x00000100)
2545 
2546 /** Whether the key may be used to decrypt a message.
2547  *
2548  * This flag allows the key to be used for a symmetric decryption operation,
2549  * for an AEAD decryption-and-verification operation,
2550  * or for an asymmetric decryption operation,
2551  * if otherwise permitted by the key's type and policy.
2552  *
2553  * For a key pair, this concerns the private key.
2554  */
2555 #define PSA_KEY_USAGE_DECRYPT                   ((psa_key_usage_t) 0x00000200)
2556 
2557 /** Whether the key may be used to sign a message.
2558  *
2559  * This flag allows the key to be used for a MAC calculation operation or for
2560  * an asymmetric message signature operation, if otherwise permitted by the
2561  * key’s type and policy.
2562  *
2563  * For a key pair, this concerns the private key.
2564  */
2565 #define PSA_KEY_USAGE_SIGN_MESSAGE              ((psa_key_usage_t) 0x00000400)
2566 
2567 /** Whether the key may be used to verify a message.
2568  *
2569  * This flag allows the key to be used for a MAC verification operation or for
2570  * an asymmetric message signature verification operation, if otherwise
2571  * permitted by the key’s type and policy.
2572  *
2573  * For a key pair, this concerns the public key.
2574  */
2575 #define PSA_KEY_USAGE_VERIFY_MESSAGE            ((psa_key_usage_t) 0x00000800)
2576 
2577 /** Whether the key may be used to sign a message.
2578  *
2579  * This flag allows the key to be used for a MAC calculation operation
2580  * or for an asymmetric signature operation,
2581  * if otherwise permitted by the key's type and policy.
2582  *
2583  * For a key pair, this concerns the private key.
2584  */
2585 #define PSA_KEY_USAGE_SIGN_HASH                 ((psa_key_usage_t) 0x00001000)
2586 
2587 /** Whether the key may be used to verify a message signature.
2588  *
2589  * This flag allows the key to be used for a MAC verification operation
2590  * or for an asymmetric signature verification operation,
2591  * if otherwise permitted by the key's type and policy.
2592  *
2593  * For a key pair, this concerns the public key.
2594  */
2595 #define PSA_KEY_USAGE_VERIFY_HASH               ((psa_key_usage_t) 0x00002000)
2596 
2597 /** Whether the key may be used to derive other keys or produce a password
2598  * hash.
2599  *
2600  * This flag allows the key to be used for a key derivation operation or for
2601  * a key agreement operation, if otherwise permitted by the key's type and
2602  * policy.
2603  *
2604  * If this flag is present on all keys used in calls to
2605  * psa_key_derivation_input_key() for a key derivation operation, then it
2606  * permits calling psa_key_derivation_output_bytes() or
2607  * psa_key_derivation_output_key() at the end of the operation.
2608  */
2609 #define PSA_KEY_USAGE_DERIVE                    ((psa_key_usage_t) 0x00004000)
2610 
2611 /** Whether the key may be used to verify the result of a key derivation,
2612  * including password hashing.
2613  *
2614  * This flag allows the key to be used:
2615  *
2616  * This flag allows the key to be used in a key derivation operation, if
2617  * otherwise permitted by the key's type and policy.
2618  *
2619  * If this flag is present on all keys used in calls to
2620  * psa_key_derivation_input_key() for a key derivation operation, then it
2621  * permits calling psa_key_derivation_verify_bytes() or
2622  * psa_key_derivation_verify_key() at the end of the operation.
2623  */
2624 #define PSA_KEY_USAGE_VERIFY_DERIVATION         ((psa_key_usage_t) 0x00008000)
2625 
2626 /**@}*/
2627 
2628 /** \defgroup derivation Key derivation
2629  * @{
2630  */
2631 
2632 /* Key input steps are not embedded in the persistent storage, so you can
2633  * change them if needed: it's only an ABI change. */
2634 
2635 /** A secret input for key derivation.
2636  *
2637  * This should be a key of type #PSA_KEY_TYPE_DERIVE
2638  * (passed to psa_key_derivation_input_key())
2639  * or the shared secret resulting from a key agreement
2640  * (obtained via psa_key_derivation_key_agreement()).
2641  *
2642  * The secret can also be a direct input (passed to
2643  * key_derivation_input_bytes()). In this case, the derivation operation
2644  * may not be used to derive keys: the operation will only allow
2645  * psa_key_derivation_output_bytes(),
2646  * psa_key_derivation_verify_bytes(), or
2647  * psa_key_derivation_verify_key(), but not
2648  * psa_key_derivation_output_key().
2649  */
2650 #define PSA_KEY_DERIVATION_INPUT_SECRET     ((psa_key_derivation_step_t) 0x0101)
2651 
2652 /** A low-entropy secret input for password hashing / key stretching.
2653  *
2654  * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2655  * psa_key_derivation_input_key()) or a direct input (passed to
2656  * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2657  * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2658  * the shared secret resulting from a key agreement.
2659  *
2660  * The secret can also be a direct input (passed to
2661  * key_derivation_input_bytes()). In this case, the derivation operation
2662  * may not be used to derive keys: the operation will only allow
2663  * psa_key_derivation_output_bytes(),
2664  * psa_key_derivation_verify_bytes(), or
2665  * psa_key_derivation_verify_key(), but not
2666  * psa_key_derivation_output_key().
2667  */
2668 #define PSA_KEY_DERIVATION_INPUT_PASSWORD   ((psa_key_derivation_step_t) 0x0102)
2669 
2670 /** A high-entropy additional secret input for key derivation.
2671  *
2672  * This is typically the shared secret resulting from a key agreement obtained
2673  * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
2674  * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
2675  * a direct input passed to `psa_key_derivation_input_bytes()`.
2676  */
2677 #define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
2678     ((psa_key_derivation_step_t) 0x0103)
2679 
2680 /** A label for key derivation.
2681  *
2682  * This should be a direct input.
2683  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2684  */
2685 #define PSA_KEY_DERIVATION_INPUT_LABEL      ((psa_key_derivation_step_t) 0x0201)
2686 
2687 /** A salt for key derivation.
2688  *
2689  * This should be a direct input.
2690  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2691  * #PSA_KEY_TYPE_PEPPER.
2692  */
2693 #define PSA_KEY_DERIVATION_INPUT_SALT       ((psa_key_derivation_step_t) 0x0202)
2694 
2695 /** An information string for key derivation.
2696  *
2697  * This should be a direct input.
2698  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2699  */
2700 #define PSA_KEY_DERIVATION_INPUT_INFO       ((psa_key_derivation_step_t) 0x0203)
2701 
2702 /** A seed for key derivation.
2703  *
2704  * This should be a direct input.
2705  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2706  */
2707 #define PSA_KEY_DERIVATION_INPUT_SEED       ((psa_key_derivation_step_t) 0x0204)
2708 
2709 /** A cost parameter for password hashing / key stretching.
2710  *
2711  * This must be a direct input, passed to psa_key_derivation_input_integer().
2712  */
2713 #define PSA_KEY_DERIVATION_INPUT_COST       ((psa_key_derivation_step_t) 0x0205)
2714 
2715 /**@}*/
2716 
2717 /** \defgroup helper_macros Helper macros
2718  * @{
2719  */
2720 
2721 /* Helper macros */
2722 
2723 /** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2724  *  regardless of the tag length they encode.
2725  *
2726  * \param aead_alg_1 An AEAD algorithm identifier.
2727  * \param aead_alg_2 An AEAD algorithm identifier.
2728  *
2729  * \return           1 if both identifiers refer to the same AEAD algorithm,
2730  *                   0 otherwise.
2731  *                   Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2732  *                   a supported AEAD algorithm.
2733  */
2734 #define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2735     (!(((aead_alg_1) ^ (aead_alg_2)) & \
2736        ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2737 
2738 /**@}*/
2739 
2740 /**@}*/
2741 
2742 /** \defgroup interruptible Interruptible operations
2743  * @{
2744  */
2745 
2746 /** Maximum value for use with \c psa_interruptible_set_max_ops() to determine
2747  *  the maximum number of ops allowed to be executed by an interruptible
2748  *  function in a single call.
2749  */
2750 #define PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED UINT32_MAX
2751 
2752 /**@}*/
2753 
2754 #endif /* PSA_CRYPTO_VALUES_H */
2755