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