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