Mistake on this page?
Report an issue in GitHub or email us
TARGET_TFM/TARGET_TFM_LATEST/include/psa/crypto_values.h
1 /*
2  * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 /**
8  * \file psa/crypto_values.h
9  *
10  * \brief PSA cryptography module: macros to build and analyze integer values.
11  *
12  * \note This file may not be included directly. Applications must
13  * include psa/crypto.h. Drivers must include the appropriate driver
14  * header file.
15  *
16  * This file contains portable definitions of macros to build and analyze
17  * values of integral types that encode properties of cryptographic keys,
18  * designations of cryptographic algorithms, and error codes returned by
19  * the library.
20  *
21  * This header file only defines preprocessor macros.
22  */
23 
24 #ifndef PSA_CRYPTO_VALUES_H
25 #define PSA_CRYPTO_VALUES_H
26 
27 #include "mbedtls_svc_key_id.h"
28 
29 /** \defgroup error Error codes
30  * @{
31  */
32 
33 /* PSA error codes */
34 
35 /** The action was completed successfully. */
36 #ifndef PSA_SUCCESS
37 #define PSA_SUCCESS ((psa_status_t)0)
38 #endif
39 
40 /** An error occurred that does not correspond to any defined
41  * failure cause.
42  *
43  * Implementations may use this error code if none of the other standard
44  * error codes are applicable. */
45 #define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
46 
47 /** The requested operation or a parameter is not supported
48  * by this implementation.
49  *
50  * Implementations should return this error code when an enumeration
51  * parameter such as a key type, algorithm, etc. is not recognized.
52  * If a combination of parameters is recognized and identified as
53  * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
54 #define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
55 
56 /** The requested action is denied by a policy.
57  *
58  * Implementations should return this error code when the parameters
59  * are recognized as valid and supported, and a policy explicitly
60  * denies the requested operation.
61  *
62  * If a subset of the parameters of a function call identify a
63  * forbidden operation, and another subset of the parameters are
64  * not valid or not supported, it is unspecified whether the function
65  * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
66  * #PSA_ERROR_INVALID_ARGUMENT. */
67 #define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
68 
69 /** An output buffer is too small.
70  *
71  * Applications can call the \c PSA_xxx_SIZE macro listed in the function
72  * description to determine a sufficient buffer size.
73  *
74  * Implementations should preferably return this error code only
75  * in cases when performing the operation with a larger output
76  * buffer would succeed. However implementations may return this
77  * error if a function has invalid or unsupported parameters in addition
78  * to the parameters that determine the necessary output buffer size. */
79 #define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
80 
81 /** Asking for an item that already exists
82  *
83  * Implementations should return this error, when attempting
84  * to write an item (like a key) that already exists. */
85 #define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
86 
87 /** Asking for an item that doesn't exist
88  *
89  * Implementations should return this error, if a requested item (like
90  * a key) does not exist. */
91 #define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
92 
93 /** The requested action cannot be performed in the current state.
94  *
95  * Multipart operations return this error when one of the
96  * functions is called out of sequence. Refer to the function
97  * descriptions for permitted sequencing of functions.
98  *
99  * Implementations shall not return this error code to indicate
100  * that a key either exists or not,
101  * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
102  * as applicable.
103  *
104  * Implementations shall not return this error code to indicate that a
105  * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
106  * instead. */
107 #define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
108 
109 /** The parameters passed to the function are invalid.
110  *
111  * Implementations may return this error any time a parameter or
112  * combination of parameters are recognized as invalid.
113  *
114  * Implementations shall not return this error code to indicate that a
115  * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
116  * instead.
117  */
118 #define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
119 
120 /** There is not enough runtime memory.
121  *
122  * If the action is carried out across multiple security realms, this
123  * error can refer to available memory in any of the security realms. */
124 #define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
125 
126 /** There is not enough persistent storage.
127  *
128  * Functions that modify the key storage return this error code if
129  * there is insufficient storage space on the host media. In addition,
130  * many functions that do not otherwise access storage may return this
131  * error code if the implementation requires a mandatory log entry for
132  * the requested action and the log storage space is full. */
133 #define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
134 
135 /** There was a communication failure inside the implementation.
136  *
137  * This can indicate a communication failure between the application
138  * and an external cryptoprocessor or between the cryptoprocessor and
139  * an external volatile or persistent memory. A communication failure
140  * may be transient or permanent depending on the cause.
141  *
142  * \warning If a function returns this error, it is undetermined
143  * whether the requested action has completed or not. Implementations
144  * should return #PSA_SUCCESS on successful completion whenever
145  * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
146  * if the requested action was completed successfully in an external
147  * cryptoprocessor but there was a breakdown of communication before
148  * the cryptoprocessor could report the status to the application.
149  */
150 #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
151 
152 /** There was a storage failure that may have led to data loss.
153  *
154  * This error indicates that some persistent storage is corrupted.
155  * It should not be used for a corruption of volatile memory
156  * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
157  * between the cryptoprocessor and its external storage (use
158  * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
159  * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
160  *
161  * Note that a storage failure does not indicate that any data that was
162  * previously read is invalid. However this previously read data may no
163  * longer be readable from storage.
164  *
165  * When a storage failure occurs, it is no longer possible to ensure
166  * the global integrity of the keystore. Depending on the global
167  * integrity guarantees offered by the implementation, access to other
168  * data may or may not fail even if the data is still readable but
169  * its integrity cannot be guaranteed.
170  *
171  * Implementations should only use this error code to report a
172  * permanent storage corruption. However application writers should
173  * keep in mind that transient errors while reading the storage may be
174  * reported using this error code. */
175 #define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
176 
177 /** A hardware failure was detected.
178  *
179  * A hardware failure may be transient or permanent depending on the
180  * cause. */
181 #define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
182 
183 /** A tampering attempt was detected.
184  *
185  * If an application receives this error code, there is no guarantee
186  * that previously accessed or computed data was correct and remains
187  * confidential. Applications should not perform any security function
188  * and should enter a safe failure state.
189  *
190  * Implementations may return this error code if they detect an invalid
191  * state that cannot happen during normal operation and that indicates
192  * that the implementation's security guarantees no longer hold. Depending
193  * on the implementation architecture and on its security and safety goals,
194  * the implementation may forcibly terminate the application.
195  *
196  * This error code is intended as a last resort when a security breach
197  * is detected and it is unsure whether the keystore data is still
198  * protected. Implementations shall only return this error code
199  * to report an alarm from a tampering detector, to indicate that
200  * the confidentiality of stored data can no longer be guaranteed,
201  * or to indicate that the integrity of previously returned data is now
202  * considered compromised. Implementations shall not use this error code
203  * to indicate a hardware failure that merely makes it impossible to
204  * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
205  * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
206  * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
207  * instead).
208  *
209  * This error indicates an attack against the application. Implementations
210  * shall not return this error code as a consequence of the behavior of
211  * the application itself. */
212 #define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
213 
214 /** There is not enough entropy to generate random data needed
215  * for the requested action.
216  *
217  * This error indicates a failure of a hardware random generator.
218  * Application writers should note that this error can be returned not
219  * only by functions whose purpose is to generate random data, such
220  * as key, IV or nonce generation, but also by functions that execute
221  * an algorithm with a randomized result, as well as functions that
222  * use randomization of intermediate computations as a countermeasure
223  * to certain attacks.
224  *
225  * Implementations should avoid returning this error after psa_crypto_init()
226  * has succeeded. Implementations should generate sufficient
227  * entropy during initialization and subsequently use a cryptographically
228  * secure pseudorandom generator (PRNG). However implementations may return
229  * this error at any time if a policy requires the PRNG to be reseeded
230  * during normal operation. */
231 #define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
232 
233 /** The signature, MAC or hash is incorrect.
234  *
235  * Verification functions return this error if the verification
236  * calculations completed successfully, and the value to be verified
237  * was determined to be incorrect.
238  *
239  * If the value to verify has an invalid size, implementations may return
240  * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
241 #define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
242 
243 /** The decrypted padding is incorrect.
244  *
245  * \warning In some protocols, when decrypting data, it is essential that
246  * the behavior of the application does not depend on whether the padding
247  * is correct, down to precise timing. Applications should prefer
248  * protocols that use authenticated encryption rather than plain
249  * encryption. If the application must perform a decryption of
250  * unauthenticated data, the application writer should take care not
251  * to reveal whether the padding is invalid.
252  *
253  * Implementations should strive to make valid and invalid padding
254  * as close as possible to indistinguishable to an external observer.
255  * In particular, the timing of a decryption operation should not
256  * depend on the validity of the padding. */
257 #define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
258 
259 /** Return this error when there's insufficient data when attempting
260  * to read from a resource. */
261 #define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
262 
263 /** The key identifier is not valid. See also :ref:\`key-handles\`.
264  */
265 #define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
266 
267 /**@}*/
268 
269 /** \defgroup crypto_types Key and algorithm types
270  * @{
271  */
272 
273 /** An invalid key type value.
274  *
275  * Zero is not the encoding of any key type.
276  */
277 #define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x0000)
278 
279 /** Vendor-defined key type flag.
280  *
281  * Key types defined by this standard will never have the
282  * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
283  * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
284  * respect the bitwise structure used by standard encodings whenever practical.
285  */
286 #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
287 
288 #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
289 #define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
290 #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
291 #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
292 #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
293 
294 #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
295 
296 /** Whether a key type is vendor-defined.
297  *
298  * See also #PSA_KEY_TYPE_VENDOR_FLAG.
299  */
300 #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
301  (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
302 
303 /** Whether a key type is an unstructured array of bytes.
304  *
305  * This encompasses both symmetric keys and non-key data.
306  */
307 #define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
308  (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
309  ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
310 
311 /** Whether a key type is asymmetric: either a key pair or a public key. */
312 #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
313  (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
314  & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
315  PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
316 /** Whether a key type is the public part of a key pair. */
317 #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
318  (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
319 /** Whether a key type is a key pair containing a private part and a public
320  * part. */
321 #define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
322  (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
323 /** The key pair type corresponding to a public key type.
324  *
325  * You may also pass a key pair type as \p type, it will be left unchanged.
326  *
327  * \param type A public key type or key pair type.
328  *
329  * \return The corresponding key pair type.
330  * If \p type is not a public key or a key pair,
331  * the return value is undefined.
332  */
333 #define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
334  ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
335 /** The public key type corresponding to a key pair type.
336  *
337  * You may also pass a key pair type as \p type, it will be left unchanged.
338  *
339  * \param type A public key type or key pair type.
340  *
341  * \return The corresponding public key type.
342  * If \p type is not a public key or a key pair,
343  * the return value is undefined.
344  */
345 #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
346  ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
347 
348 /** Raw data.
349  *
350  * A "key" of this type cannot be used for any cryptographic operation.
351  * Applications may use this type to store arbitrary data in the keystore. */
352 #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
353 
354 /** HMAC key.
355  *
356  * The key policy determines which underlying hash algorithm the key can be
357  * used for.
358  *
359  * HMAC keys should generally have the same size as the underlying hash.
360  * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
361  * \c alg is the HMAC algorithm or the underlying hash algorithm. */
362 #define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
363 
364 /** A secret for key derivation.
365  *
366  * The key policy determines which key derivation algorithm the key
367  * can be used for.
368  */
369 #define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
370 
371 /** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
372  *
373  * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
374  * 32 bytes (AES-256).
375  */
376 #define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2400)
377 
378 /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
379  *
380  * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
381  * 24 bytes (3-key 3DES).
382  *
383  * Note that single DES and 2-key 3DES are weak and strongly
384  * deprecated and should only be used to decrypt legacy data. 3-key 3DES
385  * is weak and deprecated and should only be used in legacy protocols.
386  */
387 #define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
388 
389 /** Key for a cipher, AEAD or MAC algorithm based on the
390  * Camellia block cipher. */
391 #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
392 
393 /** Key for the RC4 stream cipher.
394  *
395  * Note that RC4 is weak and deprecated and should only be used in
396  * legacy protocols. */
397 #define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x2002)
398 
399 /** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
400  *
401  * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
402  *
403  * Implementations must support 12-byte nonces, may support 8-byte nonces,
404  * and should reject other sizes.
405  */
406 #define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
407 
408 /** RSA public key. */
409 #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
410 /** RSA key pair (private and public key). */
411 #define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
412 /** Whether a key type is an RSA key (pair or public-only). */
413 #define PSA_KEY_TYPE_IS_RSA(type) \
414  (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
415 
416 #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
417 #define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
418 #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
419 /** Elliptic curve key pair.
420  *
421  * \param curve A value of type ::psa_ecc_family_t that
422  * identifies the ECC curve to be used.
423  */
424 #define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
425  (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
426 /** Elliptic curve public key.
427  *
428  * \param curve A value of type ::psa_ecc_family_t that
429  * identifies the ECC curve to be used.
430  */
431 #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
432  (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
433 
434 /** Whether a key type is an elliptic curve key (pair or public-only). */
435 #define PSA_KEY_TYPE_IS_ECC(type) \
436  ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
437  ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
438 /** Whether a key type is an elliptic curve key pair. */
439 #define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
440  (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
441  PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
442 /** Whether a key type is an elliptic curve public key. */
443 #define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
444  (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
445  PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
446 
447 /** Extract the curve from an elliptic curve key type. */
448 #define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
449  ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
450  ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
451  0))
452 
453 /** SEC Koblitz curves over prime fields.
454  *
455  * This family comprises the following curves:
456  * secp192k1, secp224k1, secp256k1.
457  * They are defined in _Standards for Efficient Cryptography_,
458  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
459  * https://www.secg.org/sec2-v2.pdf
460  */
461 #define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
462 
463 /** SEC random curves over prime fields.
464  *
465  * This family comprises the following curves:
466  * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
467  * They are defined in _Standards for Efficient Cryptography_,
468  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
469  * https://www.secg.org/sec2-v2.pdf
470  */
471 #define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
472 /* SECP160R2 (SEC2 v1, obsolete) */
473 #define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
474 
475 /** SEC Koblitz curves over binary fields.
476  *
477  * This family comprises the following curves:
478  * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
479  * They are defined in _Standards for Efficient Cryptography_,
480  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
481  * https://www.secg.org/sec2-v2.pdf
482  */
483 #define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
484 
485 /** SEC random curves over binary fields.
486  *
487  * This family comprises the following curves:
488  * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
489  * They are defined in _Standards for Efficient Cryptography_,
490  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
491  * https://www.secg.org/sec2-v2.pdf
492  */
493 #define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
494 
495 /** SEC additional random curves over binary fields.
496  *
497  * This family comprises the following curve:
498  * sect163r2.
499  * It is defined in _Standards for Efficient Cryptography_,
500  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
501  * https://www.secg.org/sec2-v2.pdf
502  */
503 #define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
504 
505 /** Brainpool P random curves.
506  *
507  * This family comprises the following curves:
508  * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
509  * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
510  * It is defined in RFC 5639.
511  */
512 #define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
513 
514 /** Curve25519 and Curve448.
515  *
516  * This family comprises the following Montgomery curves:
517  * - 255-bit: Bernstein et al.,
518  * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
519  * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
520  * - 448-bit: Hamburg,
521  * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
522  * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
523  */
524 #define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
525 
526 #define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
527 #define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
528 #define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
529 /** Diffie-Hellman key pair.
530  *
531  * \param group A value of type ::psa_dh_family_t that identifies the
532  * Diffie-Hellman group to be used.
533  */
534 #define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
535  (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
536 /** Diffie-Hellman public key.
537  *
538  * \param group A value of type ::psa_dh_family_t that identifies the
539  * Diffie-Hellman group to be used.
540  */
541 #define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
542  (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
543 
544 /** Whether a key type is a Diffie-Hellman key (pair or public-only). */
545 #define PSA_KEY_TYPE_IS_DH(type) \
546  ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
547  ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
548 /** Whether a key type is a Diffie-Hellman key pair. */
549 #define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
550  (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
551  PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
552 /** Whether a key type is a Diffie-Hellman public key. */
553 #define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
554  (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
555  PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
556 
557 /** Extract the group from a Diffie-Hellman key type. */
558 #define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
559  ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
560  ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
561  0))
562 
563 /** Diffie-Hellman groups defined in RFC 7919 Appendix A.
564  *
565  * This family includes groups with the following key sizes (in bits):
566  * 2048, 3072, 4096, 6144, 8192. A given implementation may support
567  * all of these sizes or only a subset.
568  */
569 #define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
570 
571 #define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
572  (((type) >> 8) & 7)
573 /** The block size of a block cipher.
574  *
575  * \param type A cipher key type (value of type #psa_key_type_t).
576  *
577  * \return The block size for a block cipher, or 1 for a stream cipher.
578  * The return value is undefined if \p type is not a supported
579  * cipher key type.
580  *
581  * \note It is possible to build stream cipher algorithms on top of a block
582  * cipher, for example CTR mode (#PSA_ALG_CTR).
583  * This macro only takes the key type into account, so it cannot be
584  * used to determine the size of the data that #psa_cipher_update()
585  * might buffer for future processing in general.
586  *
587  * \note This macro returns a compile-time constant if its argument is one.
588  *
589  * \warning This macro may evaluate its argument multiple times.
590  */
591 #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
592  (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
593  1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
594  0u)
595 
596 /** Vendor-defined algorithm flag.
597  *
598  * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
599  * bit set. Vendors who define additional algorithms must use an encoding with
600  * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
601  * used by standard encodings whenever practical.
602  */
603 #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
604 
605 #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
606 #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
607 #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
608 #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
609 #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
610 #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
611 #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
612 #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
613 #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
614 
615 /** Whether an algorithm is vendor-defined.
616  *
617  * See also #PSA_ALG_VENDOR_FLAG.
618  */
619 #define PSA_ALG_IS_VENDOR_DEFINED(alg) \
620  (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
621 
622 /** Whether the specified algorithm is a hash algorithm.
623  *
624  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
625  *
626  * \return 1 if \p alg is a hash algorithm, 0 otherwise.
627  * This macro may return either 0 or 1 if \p alg is not a supported
628  * algorithm identifier.
629  */
630 #define PSA_ALG_IS_HASH(alg) \
631  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
632 
633 /** Whether the specified algorithm is a MAC algorithm.
634  *
635  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
636  *
637  * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
638  * This macro may return either 0 or 1 if \p alg is not a supported
639  * algorithm identifier.
640  */
641 #define PSA_ALG_IS_MAC(alg) \
642  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
643 
644 /** Whether the specified algorithm is a symmetric cipher algorithm.
645  *
646  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
647  *
648  * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
649  * This macro may return either 0 or 1 if \p alg is not a supported
650  * algorithm identifier.
651  */
652 #define PSA_ALG_IS_CIPHER(alg) \
653  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
654 
655 /** Whether the specified algorithm is an authenticated encryption
656  * with associated data (AEAD) algorithm.
657  *
658  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
659  *
660  * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
661  * This macro may return either 0 or 1 if \p alg is not a supported
662  * algorithm identifier.
663  */
664 #define PSA_ALG_IS_AEAD(alg) \
665  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
666 
667 /** Whether the specified algorithm is an asymmetric signature algorithm,
668  * also known as public-key signature algorithm.
669  *
670  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
671  *
672  * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
673  * This macro may return either 0 or 1 if \p alg is not a supported
674  * algorithm identifier.
675  */
676 #define PSA_ALG_IS_SIGN(alg) \
677  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
678 
679 /** Whether the specified algorithm is an asymmetric encryption algorithm,
680  * also known as public-key encryption algorithm.
681  *
682  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
683  *
684  * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
685  * This macro may return either 0 or 1 if \p alg is not a supported
686  * algorithm identifier.
687  */
688 #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
689  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
690 
691 /** Whether the specified algorithm is a key agreement algorithm.
692  *
693  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
694  *
695  * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
696  * This macro may return either 0 or 1 if \p alg is not a supported
697  * algorithm identifier.
698  */
699 #define PSA_ALG_IS_KEY_AGREEMENT(alg) \
700  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
701 
702 /** Whether the specified algorithm is a key derivation algorithm.
703  *
704  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
705  *
706  * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
707  * This macro may return either 0 or 1 if \p alg is not a supported
708  * algorithm identifier.
709  */
710 #define PSA_ALG_IS_KEY_DERIVATION(alg) \
711  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
712 
713 #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
714 /** MD2 */
715 #define PSA_ALG_MD2 ((psa_algorithm_t)0x02000001)
716 /** MD4 */
717 #define PSA_ALG_MD4 ((psa_algorithm_t)0x02000002)
718 /** MD5 */
719 #define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
720 /** PSA_ALG_RIPEMD160 */
721 #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
722 /** SHA1 */
723 #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
724 /** SHA2-224 */
725 #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
726 /** SHA2-256 */
727 #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
728 /** SHA2-384 */
729 #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
730 /** SHA2-512 */
731 #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
732 /** SHA2-512/224 */
733 #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
734 /** SHA2-512/256 */
735 #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
736 /** SHA3-224 */
737 #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
738 /** SHA3-256 */
739 #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
740 /** SHA3-384 */
741 #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
742 /** SHA3-512 */
743 #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
744 
745 /** In a hash-and-sign algorithm policy, allow any hash algorithm.
746  *
747  * This value may be used to form the algorithm usage field of a policy
748  * for a signature algorithm that is parametrized by a hash. The key
749  * may then be used to perform operations using the same signature
750  * algorithm parametrized with any supported hash.
751  *
752  * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
753  * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
754  * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
755  * Then you may create and use a key as follows:
756  * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
757  * ```
758  * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
759  * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
760  * ```
761  * - Import or generate key material.
762  * - Call psa_sign_hash() or psa_verify_hash(), passing
763  * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
764  * call to sign or verify a message may use a different hash.
765  * ```
766  * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
767  * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
768  * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
769  * ```
770  *
771  * This value may not be used to build other algorithms that are
772  * parametrized over a hash. For any valid use of this macro to build
773  * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
774  *
775  * This value may not be used to build an algorithm specification to
776  * perform an operation. It is only valid to build policies.
777  */
778 #define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
779 
780 #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
781 #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
782 /** Macro to build an HMAC algorithm.
783  *
784  * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
785  *
786  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
787  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
788  *
789  * \return The corresponding HMAC algorithm.
790  * \return Unspecified if \p hash_alg is not a supported
791  * hash algorithm.
792  */
793 #define PSA_ALG_HMAC(hash_alg) \
794  (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
795 
796 #define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
797  (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
798 
799 /** Whether the specified algorithm is an HMAC algorithm.
800  *
801  * HMAC is a family of MAC algorithms that are based on a hash function.
802  *
803  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
804  *
805  * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
806  * This macro may return either 0 or 1 if \p alg is not a supported
807  * algorithm identifier.
808  */
809 #define PSA_ALG_IS_HMAC(alg) \
810  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
811  PSA_ALG_HMAC_BASE)
812 
813 /* In the encoding of a MAC algorithm, the bits corresponding to
814  * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
815  * truncated. As an exception, the value 0 means the untruncated algorithm,
816  * whatever its length is. The length is encoded in 6 bits, so it can
817  * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
818  * to full length is correctly encoded as 0 and any non-trivial truncation
819  * is correctly encoded as a value between 1 and 63. */
820 #define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
821 #define PSA_MAC_TRUNCATION_OFFSET 16
822 
823 /** Macro to build a truncated MAC algorithm.
824  *
825  * A truncated MAC algorithm is identical to the corresponding MAC
826  * algorithm except that the MAC value for the truncated algorithm
827  * consists of only the first \p mac_length bytes of the MAC value
828  * for the untruncated algorithm.
829  *
830  * \note This macro may allow constructing algorithm identifiers that
831  * are not valid, either because the specified length is larger
832  * than the untruncated MAC or because the specified length is
833  * smaller than permitted by the implementation.
834  *
835  * \note It is implementation-defined whether a truncated MAC that
836  * is truncated to the same length as the MAC of the untruncated
837  * algorithm is considered identical to the untruncated algorithm
838  * for policy comparison purposes.
839  *
840  * \param mac_alg A MAC algorithm identifier (value of type
841  * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
842  * is true). This may be a truncated or untruncated
843  * MAC algorithm.
844  * \param mac_length Desired length of the truncated MAC in bytes.
845  * This must be at most the full length of the MAC
846  * and must be at least an implementation-specified
847  * minimum. The implementation-specified minimum
848  * shall not be zero.
849  *
850  * \return The corresponding MAC algorithm with the specified
851  * length.
852  * \return Unspecified if \p alg is not a supported
853  * MAC algorithm or if \p mac_length is too small or
854  * too large for the specified MAC algorithm.
855  */
856 #define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
857  (((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
858  ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
859 
860 /** Macro to build the base MAC algorithm corresponding to a truncated
861  * MAC algorithm.
862  *
863  * \param mac_alg A MAC algorithm identifier (value of type
864  * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
865  * is true). This may be a truncated or untruncated
866  * MAC algorithm.
867  *
868  * \return The corresponding base MAC algorithm.
869  * \return Unspecified if \p alg is not a supported
870  * MAC algorithm.
871  */
872 #define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
873  ((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
874 
875 /** Length to which a MAC algorithm is truncated.
876  *
877  * \param mac_alg A MAC algorithm identifier (value of type
878  * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
879  * is true).
880  *
881  * \return Length of the truncated MAC in bytes.
882  * \return 0 if \p alg is a non-truncated MAC algorithm.
883  * \return Unspecified if \p alg is not a supported
884  * MAC algorithm.
885  */
886 #define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
887  (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
888 
889 #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
890 /** The CBC-MAC construction over a block cipher
891  *
892  * \warning CBC-MAC is insecure in many cases.
893  * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
894  */
895 #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
896 /** The CMAC construction over a block cipher */
897 #define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
898 
899 /** Whether the specified algorithm is a MAC algorithm based on a block cipher.
900  *
901  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
902  *
903  * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
904  * This macro may return either 0 or 1 if \p alg is not a supported
905  * algorithm identifier.
906  */
907 #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
908  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
909  PSA_ALG_CIPHER_MAC_BASE)
910 
911 #define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
912 #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
913 
914 /** Whether the specified algorithm is a stream cipher.
915  *
916  * A stream cipher is a symmetric cipher that encrypts or decrypts messages
917  * by applying a bitwise-xor with a stream of bytes that is generated
918  * from a key.
919  *
920  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
921  *
922  * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
923  * This macro may return either 0 or 1 if \p alg is not a supported
924  * algorithm identifier or if it is not a symmetric cipher algorithm.
925  */
926 #define PSA_ALG_IS_STREAM_CIPHER(alg) \
927  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
928  (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
929 
930 /** The stream cipher mode of a stream cipher algorithm.
931  *
932  * The underlying stream cipher is determined by the key type.
933  * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
934  * - To use ARC4, use a key type of #PSA_KEY_TYPE_ARC4.
935  */
936 #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
937 
938 /** The CTR stream cipher mode.
939  *
940  * CTR is a stream cipher which is built from a block cipher.
941  * The underlying block cipher is determined by the key type.
942  * For example, to use AES-128-CTR, use this algorithm with
943  * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
944  */
945 #define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
946 
947 /** The CFB stream cipher mode.
948  *
949  * The underlying block cipher is determined by the key type.
950  */
951 #define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
952 
953 /** The OFB stream cipher mode.
954  *
955  * The underlying block cipher is determined by the key type.
956  */
957 #define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
958 
959 /** The XTS cipher mode.
960  *
961  * XTS is a cipher mode which is built from a block cipher. It requires at
962  * least one full block of input, but beyond this minimum the input
963  * does not need to be a whole number of blocks.
964  */
965 #define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
966 
967 /** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
968  *
969  * \warning ECB mode does not protect the confidentiality of the encrypted data
970  * except in extremely narrow circumstances. It is recommended that applications
971  * only use ECB if they need to construct an operating mode that the
972  * implementation does not provide. Implementations are encouraged to provide
973  * the modes that applications need in preference to supporting direct access
974  * to ECB.
975  *
976  * The underlying block cipher is determined by the key type.
977  *
978  * This symmetric cipher mode can only be used with messages whose lengths are a
979  * multiple of the block size of the chosen block cipher.
980  *
981  * ECB mode does not accept an initialization vector (IV). When using a
982  * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
983  * and psa_cipher_set_iv() must not be called.
984  */
985 #define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
986 
987 /** The CBC block cipher chaining mode, with no padding.
988  *
989  * The underlying block cipher is determined by the key type.
990  *
991  * This symmetric cipher mode can only be used with messages whose lengths
992  * are whole number of blocks for the chosen block cipher.
993  */
994 #define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
995 
996 /** The CBC block cipher chaining mode with PKCS#7 padding.
997  *
998  * The underlying block cipher is determined by the key type.
999  *
1000  * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1001  */
1002 #define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
1003 
1004 #define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1005 
1006 /** Whether the specified algorithm is an AEAD mode on a block cipher.
1007  *
1008  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1009  *
1010  * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1011  * a block cipher, 0 otherwise.
1012  * This macro may return either 0 or 1 if \p alg is not a supported
1013  * algorithm identifier.
1014  */
1015 #define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1016  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1017  (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1018 
1019 /** The CCM authenticated encryption algorithm.
1020  *
1021  * The underlying block cipher is determined by the key type.
1022  */
1023 #define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
1024 
1025 /** The GCM authenticated encryption algorithm.
1026  *
1027  * The underlying block cipher is determined by the key type.
1028  */
1029 #define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
1030 
1031 /** The Chacha20-Poly1305 AEAD algorithm.
1032  *
1033  * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1034  *
1035  * Implementations must support 12-byte nonces, may support 8-byte nonces,
1036  * and should reject other sizes.
1037  *
1038  * Implementations must support 16-byte tags and should reject other sizes.
1039  */
1040 #define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
1041 
1042 /* In the encoding of a AEAD algorithm, the bits corresponding to
1043  * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1044  * The constants for default lengths follow this encoding.
1045  */
1046 #define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1047 #define PSA_AEAD_TAG_LENGTH_OFFSET 16
1048 
1049 /** Macro to build a shortened AEAD algorithm.
1050  *
1051  * A shortened AEAD algorithm is similar to the corresponding AEAD
1052  * algorithm, but has an authentication tag that consists of fewer bytes.
1053  * Depending on the algorithm, the tag length may affect the calculation
1054  * of the ciphertext.
1055  *
1056  * \param aead_alg An AEAD algorithm identifier (value of type
1057  * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
1058  * is true).
1059  * \param tag_length Desired length of the authentication tag in bytes.
1060  *
1061  * \return The corresponding AEAD algorithm with the specified
1062  * length.
1063  * \return Unspecified if \p alg is not a supported
1064  * AEAD algorithm or if \p tag_length is not valid
1065  * for the specified AEAD algorithm.
1066  */
1067 #define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
1068  (((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
1069  ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1070  PSA_ALG_AEAD_TAG_LENGTH_MASK))
1071 
1072 /** Calculate the corresponding AEAD algorithm with the default tag length.
1073  *
1074  * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
1075  * #PSA_ALG_IS_AEAD(\p alg) is true).
1076  *
1077  * \return The corresponding AEAD algorithm with the default
1078  * tag length for that algorithm.
1079  */
1080 #define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
1081  ( \
1082  PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CCM) \
1083  PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_GCM) \
1084  PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
1085  0)
1086 #define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, ref) \
1087  PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
1088  PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
1089  ref :
1090 
1091 #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
1092 /** RSA PKCS#1 v1.5 signature with hashing.
1093  *
1094  * This is the signature scheme defined by RFC 8017
1095  * (PKCS#1: RSA Cryptography Specifications) under the name
1096  * RSASSA-PKCS1-v1_5.
1097  *
1098  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1099  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1100  * This includes #PSA_ALG_ANY_HASH
1101  * when specifying the algorithm in a usage policy.
1102  *
1103  * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
1104  * \return Unspecified if \p hash_alg is not a supported
1105  * hash algorithm.
1106  */
1107 #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1108  (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1109 /** Raw PKCS#1 v1.5 signature.
1110  *
1111  * The input to this algorithm is the DigestInfo structure used by
1112  * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1113  * steps 3&ndash;6.
1114  */
1115 #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1116 #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1117  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1118 
1119 #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
1120 /** RSA PSS signature with hashing.
1121  *
1122  * This is the signature scheme defined by RFC 8017
1123  * (PKCS#1: RSA Cryptography Specifications) under the name
1124  * RSASSA-PSS, with the message generation function MGF1, and with
1125  * a salt length equal to the length of the hash. The specified
1126  * hash algorithm is used to hash the input message, to create the
1127  * salted hash, and for the mask generation.
1128  *
1129  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1130  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1131  * This includes #PSA_ALG_ANY_HASH
1132  * when specifying the algorithm in a usage policy.
1133  *
1134  * \return The corresponding RSA PSS signature algorithm.
1135  * \return Unspecified if \p hash_alg is not a supported
1136  * hash algorithm.
1137  */
1138 #define PSA_ALG_RSA_PSS(hash_alg) \
1139  (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1140 #define PSA_ALG_IS_RSA_PSS(alg) \
1141  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1142 
1143 #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
1144 /** ECDSA signature with hashing.
1145  *
1146  * This is the ECDSA signature scheme defined by ANSI X9.62,
1147  * with a random per-message secret number (*k*).
1148  *
1149  * The representation of the signature as a byte string consists of
1150  * the concatentation of the signature values *r* and *s*. Each of
1151  * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1152  * of the base point of the curve in octets. Each value is represented
1153  * in big-endian order (most significant octet first).
1154  *
1155  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1156  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1157  * This includes #PSA_ALG_ANY_HASH
1158  * when specifying the algorithm in a usage policy.
1159  *
1160  * \return The corresponding ECDSA signature algorithm.
1161  * \return Unspecified if \p hash_alg is not a supported
1162  * hash algorithm.
1163  */
1164 #define PSA_ALG_ECDSA(hash_alg) \
1165  (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1166 /** ECDSA signature without hashing.
1167  *
1168  * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1169  * without specifying a hash algorithm. This algorithm may only be
1170  * used to sign or verify a sequence of bytes that should be an
1171  * already-calculated hash. Note that the input is padded with
1172  * zeros on the left or truncated on the left as required to fit
1173  * the curve size.
1174  */
1175 #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1176 #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
1177 /** Deterministic ECDSA signature with hashing.
1178  *
1179  * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1180  *
1181  * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1182  *
1183  * Note that when this algorithm is used for verification, signatures
1184  * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1185  * same private key are accepted. In other words,
1186  * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1187  * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1188  *
1189  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1190  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1191  * This includes #PSA_ALG_ANY_HASH
1192  * when specifying the algorithm in a usage policy.
1193  *
1194  * \return The corresponding deterministic ECDSA signature
1195  * algorithm.
1196  * \return Unspecified if \p hash_alg is not a supported
1197  * hash algorithm.
1198  */
1199 #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1200  (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1201 #define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
1202 #define PSA_ALG_IS_ECDSA(alg) \
1203  (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
1204  PSA_ALG_ECDSA_BASE)
1205 #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1206  (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
1207 #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1208  (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1209 #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1210  (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1211 
1212 /** Whether the specified algorithm is a hash-and-sign algorithm.
1213  *
1214  * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1215  * structured in two parts: first the calculation of a hash in a way that
1216  * does not depend on the key, then the calculation of a signature from the
1217  * hash value and the key.
1218  *
1219  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1220  *
1221  * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1222  * This macro may return either 0 or 1 if \p alg is not a supported
1223  * algorithm identifier.
1224  */
1225 #define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1226  (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1227  PSA_ALG_IS_ECDSA(alg))
1228 
1229 /** Get the hash used by a hash-and-sign signature algorithm.
1230  *
1231  * A hash-and-sign algorithm is a signature algorithm which is
1232  * composed of two phases: first a hashing phase which does not use
1233  * the key and produces a hash of the input message, then a signing
1234  * phase which only uses the hash and the key and not the message
1235  * itself.
1236  *
1237  * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1238  * #PSA_ALG_IS_SIGN(\p alg) is true).
1239  *
1240  * \return The underlying hash algorithm if \p alg is a hash-and-sign
1241  * algorithm.
1242  * \return 0 if \p alg is a signature algorithm that does not
1243  * follow the hash-and-sign structure.
1244  * \return Unspecified if \p alg is not a signature algorithm or
1245  * if it is not supported by the implementation.
1246  */
1247 #define PSA_ALG_SIGN_GET_HASH(alg) \
1248  (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1249  ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1250  ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1251  0)
1252 
1253 /** RSA PKCS#1 v1.5 encryption.
1254  */
1255 #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
1256 
1257 #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
1258 /** RSA OAEP encryption.
1259  *
1260  * This is the encryption scheme defined by RFC 8017
1261  * (PKCS#1: RSA Cryptography Specifications) under the name
1262  * RSAES-OAEP, with the message generation function MGF1.
1263  *
1264  * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1265  * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1266  * for MGF1.
1267  *
1268  * \return The corresponding RSA OAEP encryption algorithm.
1269  * \return Unspecified if \p hash_alg is not a supported
1270  * hash algorithm.
1271  */
1272 #define PSA_ALG_RSA_OAEP(hash_alg) \
1273  (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1274 #define PSA_ALG_IS_RSA_OAEP(alg) \
1275  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1276 #define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1277  (PSA_ALG_IS_RSA_OAEP(alg) ? \
1278  ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1279  0)
1280 
1281 #define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
1282 /** Macro to build an HKDF algorithm.
1283  *
1284  * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1285  *
1286  * This key derivation algorithm uses the following inputs:
1287  * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1288  * It is optional; if omitted, the derivation uses an empty salt.
1289  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1290  * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1291  * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1292  * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1293  * starting to generate output.
1294  *
1295  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1296  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1297  *
1298  * \return The corresponding HKDF algorithm.
1299  * \return Unspecified if \p hash_alg is not a supported
1300  * hash algorithm.
1301  */
1302 #define PSA_ALG_HKDF(hash_alg) \
1303  (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1304 /** Whether the specified algorithm is an HKDF algorithm.
1305  *
1306  * HKDF is a family of key derivation algorithms that are based on a hash
1307  * function and the HMAC construction.
1308  *
1309  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1310  *
1311  * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1312  * This macro may return either 0 or 1 if \c alg is not a supported
1313  * key derivation algorithm identifier.
1314  */
1315 #define PSA_ALG_IS_HKDF(alg) \
1316  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1317 #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1318  (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1319 
1320 #define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
1321 /** Macro to build a TLS-1.2 PRF algorithm.
1322  *
1323  * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1324  * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1325  * used with either SHA-256 or SHA-384.
1326  *
1327  * This key derivation algorithm uses the following inputs, which must be
1328  * passed in the order given here:
1329  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1330  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1331  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1332  *
1333  * For the application to TLS-1.2 key expansion, the seed is the
1334  * concatenation of ServerHello.Random + ClientHello.Random,
1335  * and the label is "key expansion".
1336  *
1337  * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1338  * TLS 1.2 PRF using HMAC-SHA-256.
1339  *
1340  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1341  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1342  *
1343  * \return The corresponding TLS-1.2 PRF algorithm.
1344  * \return Unspecified if \p hash_alg is not a supported
1345  * hash algorithm.
1346  */
1347 #define PSA_ALG_TLS12_PRF(hash_alg) \
1348  (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1349 
1350 /** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1351  *
1352  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1353  *
1354  * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1355  * This macro may return either 0 or 1 if \c alg is not a supported
1356  * key derivation algorithm identifier.
1357  */
1358 #define PSA_ALG_IS_TLS12_PRF(alg) \
1359  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1360 #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1361  (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1362 
1363 #define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
1364 /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1365  *
1366  * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1367  * from the PreSharedKey (PSK) through the application of padding
1368  * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1369  * The latter is based on HMAC and can be used with either SHA-256
1370  * or SHA-384.
1371  *
1372  * This key derivation algorithm uses the following inputs, which must be
1373  * passed in the order given here:
1374  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1375  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1376  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1377  *
1378  * For the application to TLS-1.2, the seed (which is
1379  * forwarded to the TLS-1.2 PRF) is the concatenation of the
1380  * ClientHello.Random + ServerHello.Random,
1381  * and the label is "master secret" or "extended master secret".
1382  *
1383  * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1384  * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1385  *
1386  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1387  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1388  *
1389  * \return The corresponding TLS-1.2 PSK to MS algorithm.
1390  * \return Unspecified if \p hash_alg is not a supported
1391  * hash algorithm.
1392  */
1393 #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1394  (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1395 
1396 /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1397  *
1398  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1399  *
1400  * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1401  * This macro may return either 0 or 1 if \c alg is not a supported
1402  * key derivation algorithm identifier.
1403  */
1404 #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1405  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1406 #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1407  (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1408 
1409 #define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
1410 #define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
1411 
1412 /** Macro to build a combined algorithm that chains a key agreement with
1413  * a key derivation.
1414  *
1415  * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1416  * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1417  * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1418  * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
1419  *
1420  * \return The corresponding key agreement and derivation
1421  * algorithm.
1422  * \return Unspecified if \p ka_alg is not a supported
1423  * key agreement algorithm or \p kdf_alg is not a
1424  * supported key derivation algorithm.
1425  */
1426 #define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1427  ((ka_alg) | (kdf_alg))
1428 
1429 #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1430  (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1431 
1432 #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1433  (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
1434 
1435 /** Whether the specified algorithm is a raw key agreement algorithm.
1436  *
1437  * A raw key agreement algorithm is one that does not specify
1438  * a key derivation function.
1439  * Usually, raw key agreement algorithms are constructed directly with
1440  * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
1441  * constructed with #PSA_ALG_KEY_AGREEMENT().
1442  *
1443  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1444  *
1445  * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1446  * This macro may return either 0 or 1 if \p alg is not a supported
1447  * algorithm identifier.
1448  */
1449 #define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1450  (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1451  PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1452 
1453 #define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1454  ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1455 
1456 /** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1457  *
1458  * The shared secret produced by key agreement is
1459  * `g^{ab}` in big-endian format.
1460  * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1461  * in bits.
1462  */
1463 #define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
1464 
1465 /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1466  *
1467  * This includes the raw finite field Diffie-Hellman algorithm as well as
1468  * finite-field Diffie-Hellman followed by any supporter key derivation
1469  * algorithm.
1470  *
1471  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1472  *
1473  * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1474  * This macro may return either 0 or 1 if \c alg is not a supported
1475  * key agreement algorithm identifier.
1476  */
1477 #define PSA_ALG_IS_FFDH(alg) \
1478  (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
1479 
1480 /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1481  *
1482  * The shared secret produced by key agreement is the x-coordinate of
1483  * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1484  * `m` is the bit size associated with the curve, i.e. the bit size of the
1485  * order of the curve's coordinate field. When `m` is not a multiple of 8,
1486  * the byte containing the most significant bit of the shared secret
1487  * is padded with zero bits. The byte order is either little-endian
1488  * or big-endian depending on the curve type.
1489  *
1490  * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
1491  * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1492  * in little-endian byte order.
1493  * The bit size is 448 for Curve448 and 255 for Curve25519.
1494  * - For Weierstrass curves over prime fields (curve types
1495  * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
1496  * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1497  * in big-endian byte order.
1498  * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1499  * - For Weierstrass curves over binary fields (curve types
1500  * `PSA_ECC_FAMILY_SECTXXX`),
1501  * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1502  * in big-endian byte order.
1503  * The bit size is `m` for the field `F_{2^m}`.
1504  */
1505 #define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
1506 
1507 /** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1508  * algorithm.
1509  *
1510  * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1511  * elliptic curve Diffie-Hellman followed by any supporter key derivation
1512  * algorithm.
1513  *
1514  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1515  *
1516  * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1517  * 0 otherwise.
1518  * This macro may return either 0 or 1 if \c alg is not a supported
1519  * key agreement algorithm identifier.
1520  */
1521 #define PSA_ALG_IS_ECDH(alg) \
1522  (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
1523 
1524 /** Whether the specified algorithm encoding is a wildcard.
1525  *
1526  * Wildcard values may only be used to set the usage algorithm field in
1527  * a policy, not to perform an operation.
1528  *
1529  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1530  *
1531  * \return 1 if \c alg is a wildcard algorithm encoding.
1532  * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1533  * an operation).
1534  * \return This macro may return either 0 or 1 if \c alg is not a supported
1535  * algorithm identifier.
1536  */
1537 #define PSA_ALG_IS_WILDCARD(alg) \
1538  (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1539  PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1540  (alg) == PSA_ALG_ANY_HASH)
1541 
1542 /**@}*/
1543 
1544 /** \defgroup key_lifetimes Key lifetimes
1545  * @{
1546  */
1547 
1548 /** The default lifetime for volatile keys.
1549  *
1550  * A volatile key only exists as long as the identifier to it is not destroyed.
1551  * The key material is guaranteed to be erased on a power reset.
1552  *
1553  * A key with this lifetime is typically stored in the RAM area of the
1554  * PSA Crypto subsystem. However this is an implementation choice.
1555  * If an implementation stores data about the key in a non-volatile memory,
1556  * it must release all the resources associated with the key and erase the
1557  * key material if the calling application terminates.
1558  */
1559 #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1560 
1561 /** The default lifetime for persistent keys.
1562  *
1563  * A persistent key remains in storage until it is explicitly destroyed or
1564  * until the corresponding storage area is wiped. This specification does
1565  * not define any mechanism to wipe a storage area, but implementations may
1566  * provide their own mechanism (for example to perform a factory reset,
1567  * to prepare for device refurbishment, or to uninstall an application).
1568  *
1569  * This lifetime value is the default storage area for the calling
1570  * application. Implementations may offer other storage areas designated
1571  * by other lifetime values as implementation-specific extensions.
1572  * See ::psa_key_lifetime_t for more information.
1573  */
1574 #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1575 
1576 /** The persistence level of volatile keys.
1577  *
1578  * See ::psa_key_persistence_t for more information.
1579  */
1580 #define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
1581 
1582 /** The default persistence level for persistent keys.
1583  *
1584  * See ::psa_key_persistence_t for more information.
1585  */
1586 #define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
1587 
1588 /** A persistence level indicating that a key is never destroyed.
1589  *
1590  * See ::psa_key_persistence_t for more information.
1591  */
1592 #define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
1593 
1594 #define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
1595  ((psa_key_persistence_t)((lifetime) & 0x000000ff))
1596 
1597 #define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
1598  ((psa_key_location_t)((lifetime) >> 8))
1599 
1600 /** Whether a key lifetime indicates that the key is volatile.
1601  *
1602  * A volatile key is automatically destroyed by the implementation when
1603  * the application instance terminates. In particular, a volatile key
1604  * is automatically destroyed on a power reset of the device.
1605  *
1606  * A key that is not volatile is persistent. Persistent keys are
1607  * preserved until the application explicitly destroys them or until an
1608  * implementation-specific device management event occurs (for example,
1609  * a factory reset).
1610  *
1611  * \param lifetime The lifetime value to query (value of type
1612  * ::psa_key_lifetime_t).
1613  *
1614  * \return \c 1 if the key is volatile, otherwise \c 0.
1615  */
1616 #define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
1617  (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
1618  PSA_KEY_PERSISTENCE_VOLATILE)
1619 
1620 /** Construct a lifetime from a persistence level and a location.
1621  *
1622  * \param persistence The persistence level
1623  * (value of type ::psa_key_persistence_t).
1624  * \param location The location indicator
1625  * (value of type ::psa_key_location_t).
1626  *
1627  * \return The constructed lifetime value.
1628  */
1629 #define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
1630  ((location) << 8 | (persistence))
1631 
1632 /** The local storage area for persistent keys.
1633  *
1634  * This storage area is available on all systems that can store persistent
1635  * keys without delegating the storage to a third-party cryptoprocessor.
1636  *
1637  * See ::psa_key_location_t for more information.
1638  */
1639 #define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
1640 
1641 #define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
1642 
1643 /** The minimum value for a key identifier chosen by the application.
1644  */
1645 #define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
1646 /** The maximum value for a key identifier chosen by the application.
1647  */
1648 #define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
1649 /** The minimum value for a key identifier chosen by the implementation.
1650  */
1651 #define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
1652 /** The maximum value for a key identifier chosen by the implementation.
1653  */
1654 #define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
1655 
1656 /**@}*/
1657 
1658 /** \defgroup policy Key policies
1659  * @{
1660  */
1661 
1662 /** Whether the key may be exported.
1663  *
1664  * A public key or the public part of a key pair may always be exported
1665  * regardless of the value of this permission flag.
1666  *
1667  * If a key does not have export permission, implementations shall not
1668  * allow the key to be exported in plain form from the cryptoprocessor,
1669  * whether through psa_export_key() or through a proprietary interface.
1670  * The key may however be exportable in a wrapped form, i.e. in a form
1671  * where it is encrypted by another key.
1672  */
1673 #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1674 
1675 /** Whether the key may be copied.
1676  *
1677  * This flag allows the use of psa_copy_key() to make a copy of the key
1678  * with the same policy or a more restrictive policy.
1679  *
1680  * For lifetimes for which the key is located in a secure element which
1681  * enforce the non-exportability of keys, copying a key outside the secure
1682  * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
1683  * Copying the key inside the secure element is permitted with just
1684  * #PSA_KEY_USAGE_COPY if the secure element supports it.
1685  * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
1686  * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
1687  * is sufficient to permit the copy.
1688  */
1689 #define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
1690 
1691 /** Whether the key may be used to encrypt a message.
1692  *
1693  * This flag allows the key to be used for a symmetric encryption operation,
1694  * for an AEAD encryption-and-authentication operation,
1695  * or for an asymmetric encryption operation,
1696  * if otherwise permitted by the key's type and policy.
1697  *
1698  * For a key pair, this concerns the public key.
1699  */
1700 #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1701 
1702 /** Whether the key may be used to decrypt a message.
1703  *
1704  * This flag allows the key to be used for a symmetric decryption operation,
1705  * for an AEAD decryption-and-verification operation,
1706  * or for an asymmetric decryption operation,
1707  * if otherwise permitted by the key's type and policy.
1708  *
1709  * For a key pair, this concerns the private key.
1710  */
1711 #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1712 
1713 /** Whether the key may be used to sign a message.
1714  *
1715  * This flag allows the key to be used for a MAC calculation operation
1716  * or for an asymmetric signature operation,
1717  * if otherwise permitted by the key's type and policy.
1718  *
1719  * For a key pair, this concerns the private key.
1720  */
1721 #define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
1722 
1723 /** Whether the key may be used to verify a message signature.
1724  *
1725  * This flag allows the key to be used for a MAC verification operation
1726  * or for an asymmetric signature verification operation,
1727  * if otherwise permitted by by the key's type and policy.
1728  *
1729  * For a key pair, this concerns the public key.
1730  */
1731 #define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
1732 
1733 /** Whether the key may be used to derive other keys.
1734  */
1735 #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
1736 
1737 /**@}*/
1738 
1739 /** \defgroup derivation Key derivation
1740  * @{
1741  */
1742 
1743 /** A secret input for key derivation.
1744  *
1745  * This should be a key of type #PSA_KEY_TYPE_DERIVE
1746  * (passed to psa_key_derivation_input_key())
1747  * or the shared secret resulting from a key agreement
1748  * (obtained via psa_key_derivation_key_agreement()).
1749  *
1750  * The secret can also be a direct input (passed to
1751  * key_derivation_input_bytes()). In this case, the derivation operation
1752  * may not be used to derive keys: the operation will only allow
1753  * psa_key_derivation_output_bytes(), not psa_key_derivation_output_key().
1754  */
1755 #define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
1756 
1757 /** A label for key derivation.
1758  *
1759  * This should be a direct input.
1760  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1761  */
1762 #define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
1763 
1764 /** A salt for key derivation.
1765  *
1766  * This should be a direct input.
1767  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1768  */
1769 #define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
1770 
1771 /** An information string for key derivation.
1772  *
1773  * This should be a direct input.
1774  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1775  */
1776 #define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
1777 
1778 /** A seed for key derivation.
1779  *
1780  * This should be a direct input.
1781  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1782  */
1783 #define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
1784 
1785 /**@}*/
1786 
1787 #endif /* PSA_CRYPTO_VALUES_H */
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.