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 #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 handle 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 handle 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 handle 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)0x00000000)
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)0x80000000)
287 
288 #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
289 #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
290 #define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
291 #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
292 #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
293 
294 #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
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_t)0x10000000) == \
309  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)0x50000001)
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)0x51000000)
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)0x52000000)
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)0x40000001)
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)0x40000002)
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)0x40000003)
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)0x40000004)
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)0x40000005)
407 
408 /** RSA public key. */
409 #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
410 /** RSA key pair (private and public key). */
411 #define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x70010000)
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)0x60030000)
417 #define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x70030000)
418 #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
419 /** Elliptic curve key pair.
420  *
421  * \param curve A value of type ::psa_ecc_curve_t that identifies the
422  * 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_curve_t that identifies the
429  * 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_GET_CURVE(type) \
449  ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
450  ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
451  0))
452 
453 /* The encoding of curve identifiers is currently aligned with the
454  * TLS Supported Groups Registry (formerly known as the
455  * TLS EC Named Curve Registry)
456  * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
457  * The values are defined by RFC 8422 and RFC 7027. */
458 #define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
459 #define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
460 #define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
461 #define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
462 #define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
463 #define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
464 #define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
465 #define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
466 #define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
467 #define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
468 #define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
469 #define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
470 #define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
471 #define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
472 #define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
473 #define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
474 #define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
475 #define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
476 #define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
477 #define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
478 #define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
479 #define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
480 #define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
481 #define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
482 #define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
483 #define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
484 #define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
485 #define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
486 /** Curve25519.
487  *
488  * This is the curve defined in Bernstein et al.,
489  * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
490  * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
491  */
492 #define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
493 /** Curve448
494  *
495  * This is the curve defined in Hamburg,
496  * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
497  * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
498  */
499 #define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
500 
501 /** Minimum value for a vendor-defined ECC curve identifier
502  *
503  * The range for vendor-defined curve identifiers is a subset of the IANA
504  * registry private use range, `0xfe00` - `0xfeff`.
505  */
506 #define PSA_ECC_CURVE_VENDOR_MIN ((psa_ecc_curve_t) 0xfe00)
507 /** Maximum value for a vendor-defined ECC curve identifier
508  *
509  * The range for vendor-defined curve identifiers is a subset of the IANA
510  * registry private use range, `0xfe00` - `0xfeff`.
511  */
512 #define PSA_ECC_CURVE_VENDOR_MAX ((psa_ecc_curve_t) 0xfe7f)
513 
514 #define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x60040000)
515 #define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x70040000)
516 #define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x0000ffff)
517 /** Diffie-Hellman key pair.
518  *
519  * \param group A value of type ::psa_dh_group_t that identifies the
520  * Diffie-Hellman group to be used.
521  */
522 #define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
523  (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
524 /** Diffie-Hellman public key.
525  *
526  * \param group A value of type ::psa_dh_group_t that identifies the
527  * Diffie-Hellman group to be used.
528  */
529 #define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
530  (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
531 
532 /** Whether a key type is a Diffie-Hellman key (pair or public-only). */
533 #define PSA_KEY_TYPE_IS_DH(type) \
534  ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
535  ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
536 /** Whether a key type is a Diffie-Hellman key pair. */
537 #define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
538  (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
539  PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
540 /** Whether a key type is a Diffie-Hellman public key. */
541 #define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
542  (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
543  PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
544 
545 /** Extract the group from a Diffie-Hellman key type. */
546 #define PSA_KEY_TYPE_GET_GROUP(type) \
547  ((psa_dh_group_t) (PSA_KEY_TYPE_IS_DH(type) ? \
548  ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
549  0))
550 
551 /* The encoding of group identifiers is currently aligned with the
552  * TLS Supported Groups Registry (formerly known as the
553  * TLS EC Named Curve Registry)
554  * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
555  * The values are defined by RFC 7919. */
556 #define PSA_DH_GROUP_FFDHE2048 ((psa_dh_group_t) 0x0100)
557 #define PSA_DH_GROUP_FFDHE3072 ((psa_dh_group_t) 0x0101)
558 #define PSA_DH_GROUP_FFDHE4096 ((psa_dh_group_t) 0x0102)
559 #define PSA_DH_GROUP_FFDHE6144 ((psa_dh_group_t) 0x0103)
560 #define PSA_DH_GROUP_FFDHE8192 ((psa_dh_group_t) 0x0104)
561 
562 /** Minimum value for a vendor-defined Diffie Hellman group identifier
563  *
564  * The range for vendor-defined group identifiers is a subset of the IANA
565  * registry private use range, `0x01fc` - `0x01ff`.
566  */
567 #define PSA_DH_GROUP_VENDOR_MIN ((psa_dh_group_t) 0x01fc)
568 /** Maximum value for a vendor-defined Diffie Hellman group identifier
569  *
570  * The range for vendor-defined group identifiers is a subset of the IANA
571  * registry private use range, `0x01fc` - `0x01ff`.
572  */
573 #define PSA_DH_GROUP_VENDOR_MAX ((psa_dh_group_t) 0x01fd)
574 
575 /** The block size of a block cipher.
576  *
577  * \param type A cipher key type (value of type #psa_key_type_t).
578  *
579  * \return The block size for a block cipher, or 1 for a stream cipher.
580  * The return value is undefined if \p type is not a supported
581  * cipher key type.
582  *
583  * \note It is possible to build stream cipher algorithms on top of a block
584  * cipher, for example CTR mode (#PSA_ALG_CTR).
585  * This macro only takes the key type into account, so it cannot be
586  * used to determine the size of the data that #psa_cipher_update()
587  * might buffer for future processing in general.
588  *
589  * \note This macro returns a compile-time constant if its argument is one.
590  *
591  * \warning This macro may evaluate its argument multiple times.
592  */
593 #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
594  ( \
595  (type) == PSA_KEY_TYPE_AES ? 16 : \
596  (type) == PSA_KEY_TYPE_DES ? 8 : \
597  (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
598  (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
599  (type) == PSA_KEY_TYPE_CHACHA20 ? 1 : \
600  0)
601 
602 /** Vendor-defined algorithm flag.
603  *
604  * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
605  * bit set. Vendors who define additional algorithms must use an encoding with
606  * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
607  * used by standard encodings whenever practical.
608  */
609 #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
610 
611 #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
612 #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
613 #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
614 #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
615 #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
616 #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
617 #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
618 #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x20000000)
619 #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x30000000)
620 
621 /** Whether an algorithm is vendor-defined.
622  *
623  * See also #PSA_ALG_VENDOR_FLAG.
624  */
625 #define PSA_ALG_IS_VENDOR_DEFINED(alg) \
626  (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
627 
628 /** Whether the specified algorithm is a hash algorithm.
629  *
630  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
631  *
632  * \return 1 if \p alg is a hash algorithm, 0 otherwise.
633  * This macro may return either 0 or 1 if \p alg is not a supported
634  * algorithm identifier.
635  */
636 #define PSA_ALG_IS_HASH(alg) \
637  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
638 
639 /** Whether the specified algorithm is a MAC algorithm.
640  *
641  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
642  *
643  * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
644  * This macro may return either 0 or 1 if \p alg is not a supported
645  * algorithm identifier.
646  */
647 #define PSA_ALG_IS_MAC(alg) \
648  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
649 
650 /** Whether the specified algorithm is a symmetric cipher algorithm.
651  *
652  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
653  *
654  * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
655  * This macro may return either 0 or 1 if \p alg is not a supported
656  * algorithm identifier.
657  */
658 #define PSA_ALG_IS_CIPHER(alg) \
659  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
660 
661 /** Whether the specified algorithm is an authenticated encryption
662  * with associated data (AEAD) algorithm.
663  *
664  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
665  *
666  * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
667  * This macro may return either 0 or 1 if \p alg is not a supported
668  * algorithm identifier.
669  */
670 #define PSA_ALG_IS_AEAD(alg) \
671  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
672 
673 /** Whether the specified algorithm is a public-key signature algorithm.
674  *
675  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
676  *
677  * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
678  * This macro may return either 0 or 1 if \p alg is not a supported
679  * algorithm identifier.
680  */
681 #define PSA_ALG_IS_SIGN(alg) \
682  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
683 
684 /** Whether the specified algorithm is a public-key encryption algorithm.
685  *
686  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
687  *
688  * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
689  * This macro may return either 0 or 1 if \p alg is not a supported
690  * algorithm identifier.
691  */
692 #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
693  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
694 
695 /** Whether the specified algorithm is a key agreement algorithm.
696  *
697  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
698  *
699  * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
700  * This macro may return either 0 or 1 if \p alg is not a supported
701  * algorithm identifier.
702  */
703 #define PSA_ALG_IS_KEY_AGREEMENT(alg) \
704  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
705 
706 /** Whether the specified algorithm is a key derivation algorithm.
707  *
708  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
709  *
710  * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
711  * This macro may return either 0 or 1 if \p alg is not a supported
712  * algorithm identifier.
713  */
714 #define PSA_ALG_IS_KEY_DERIVATION(alg) \
715  (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
716 
717 #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
718 /** MD2 */
719 #define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
720 /** MD4 */
721 #define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
722 /** MD5 */
723 #define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
724 /** PSA_ALG_RIPEMD160 */
725 #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
726 /** SHA1 */
727 #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
728 /** SHA2-224 */
729 #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
730 /** SHA2-256 */
731 #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
732 /** SHA2-384 */
733 #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
734 /** SHA2-512 */
735 #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
736 /** SHA2-512/224 */
737 #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
738 /** SHA2-512/256 */
739 #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
740 /** SHA3-224 */
741 #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
742 /** SHA3-256 */
743 #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
744 /** SHA3-384 */
745 #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
746 /** SHA3-512 */
747 #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
748 
749 /** In a hash-and-sign algorithm policy, allow any hash algorithm.
750  *
751  * This value may be used to form the algorithm usage field of a policy
752  * for a signature algorithm that is parametrized by a hash. The key
753  * may then be used to perform operations using the same signature
754  * algorithm parametrized with any supported hash.
755  *
756  * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
757  * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
758  * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
759  * Then you may create and use a key as follows:
760  * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
761  * ```
762  * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
763  * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
764  * ```
765  * - Import or generate key material.
766  * - Call psa_sign_hash() or psa_verify_hash(), passing
767  * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
768  * call to sign or verify a message may use a different hash.
769  * ```
770  * psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
771  * psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
772  * psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
773  * ```
774  *
775  * This value may not be used to build other algorithms that are
776  * parametrized over a hash. For any valid use of this macro to build
777  * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
778  *
779  * This value may not be used to build an algorithm specification to
780  * perform an operation. It is only valid to build policies.
781  */
782 #define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x010000ff)
783 
784 #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
785 #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
786 /** Macro to build an HMAC algorithm.
787  *
788  * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
789  *
790  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
791  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
792  *
793  * \return The corresponding HMAC algorithm.
794  * \return Unspecified if \p hash_alg is not a supported
795  * hash algorithm.
796  */
797 #define PSA_ALG_HMAC(hash_alg) \
798  (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
799 
800 #define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
801  (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
802 
803 /** Whether the specified algorithm is an HMAC algorithm.
804  *
805  * HMAC is a family of MAC algorithms that are based on a hash function.
806  *
807  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
808  *
809  * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
810  * This macro may return either 0 or 1 if \p alg is not a supported
811  * algorithm identifier.
812  */
813 #define PSA_ALG_IS_HMAC(alg) \
814  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
815  PSA_ALG_HMAC_BASE)
816 
817 /* In the encoding of a MAC algorithm, the bits corresponding to
818  * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
819  * truncated. As an exception, the value 0 means the untruncated algorithm,
820  * whatever its length is. The length is encoded in 6 bits, so it can
821  * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
822  * to full length is correctly encoded as 0 and any non-trivial truncation
823  * is correctly encoded as a value between 1 and 63. */
824 #define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
825 #define PSA_MAC_TRUNCATION_OFFSET 8
826 
827 /** Macro to build a truncated MAC algorithm.
828  *
829  * A truncated MAC algorithm is identical to the corresponding MAC
830  * algorithm except that the MAC value for the truncated algorithm
831  * consists of only the first \p mac_length bytes of the MAC value
832  * for the untruncated algorithm.
833  *
834  * \note This macro may allow constructing algorithm identifiers that
835  * are not valid, either because the specified length is larger
836  * than the untruncated MAC or because the specified length is
837  * smaller than permitted by the implementation.
838  *
839  * \note It is implementation-defined whether a truncated MAC that
840  * is truncated to the same length as the MAC of the untruncated
841  * algorithm is considered identical to the untruncated algorithm
842  * for policy comparison purposes.
843  *
844  * \param mac_alg A MAC algorithm identifier (value of type
845  * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
846  * is true). This may be a truncated or untruncated
847  * MAC algorithm.
848  * \param mac_length Desired length of the truncated MAC in bytes.
849  * This must be at most the full length of the MAC
850  * and must be at least an implementation-specified
851  * minimum. The implementation-specified minimum
852  * shall not be zero.
853  *
854  * \return The corresponding MAC algorithm with the specified
855  * length.
856  * \return Unspecified if \p alg is not a supported
857  * MAC algorithm or if \p mac_length is too small or
858  * too large for the specified MAC algorithm.
859  */
860 #define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
861  (((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
862  ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
863 
864 /** Macro to build the base MAC algorithm corresponding to a truncated
865  * MAC algorithm.
866  *
867  * \param mac_alg A MAC algorithm identifier (value of type
868  * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
869  * is true). This may be a truncated or untruncated
870  * MAC algorithm.
871  *
872  * \return The corresponding base MAC algorithm.
873  * \return Unspecified if \p alg is not a supported
874  * MAC algorithm.
875  */
876 #define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
877  ((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
878 
879 /** Length to which a MAC algorithm is truncated.
880  *
881  * \param mac_alg A MAC algorithm identifier (value of type
882  * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
883  * is true).
884  *
885  * \return Length of the truncated MAC in bytes.
886  * \return 0 if \p alg is a non-truncated MAC algorithm.
887  * \return Unspecified if \p alg is not a supported
888  * MAC algorithm.
889  */
890 #define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
891  (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
892 
893 #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
894 /** The CBC-MAC construction over a block cipher
895  *
896  * \warning CBC-MAC is insecure in many cases.
897  * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
898  */
899 #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
900 /** The CMAC construction over a block cipher */
901 #define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
902 
903 /** Whether the specified algorithm is a MAC algorithm based on a block cipher.
904  *
905  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
906  *
907  * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
908  * This macro may return either 0 or 1 if \p alg is not a supported
909  * algorithm identifier.
910  */
911 #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
912  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
913  PSA_ALG_CIPHER_MAC_BASE)
914 
915 #define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
916 #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
917 
918 /** Whether the specified algorithm is a stream cipher.
919  *
920  * A stream cipher is a symmetric cipher that encrypts or decrypts messages
921  * by applying a bitwise-xor with a stream of bytes that is generated
922  * from a key.
923  *
924  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
925  *
926  * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
927  * This macro may return either 0 or 1 if \p alg is not a supported
928  * algorithm identifier or if it is not a symmetric cipher algorithm.
929  */
930 #define PSA_ALG_IS_STREAM_CIPHER(alg) \
931  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
932  (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
933 
934 /** The ARC4 stream cipher algorithm.
935  */
936 #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
937 
938 /** The ChaCha20 stream cipher.
939  *
940  * ChaCha20 is defined in RFC 7539.
941  *
942  * The nonce size for psa_cipher_set_iv() or psa_cipher_generate_iv()
943  * must be 12.
944  *
945  * The initial block counter is always 0.
946  *
947  */
948 #define PSA_ALG_CHACHA20 ((psa_algorithm_t)0x04800005)
949 
950 /** The CTR stream cipher mode.
951  *
952  * CTR is a stream cipher which is built from a block cipher.
953  * The underlying block cipher is determined by the key type.
954  * For example, to use AES-128-CTR, use this algorithm with
955  * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
956  */
957 #define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
958 
959 /** The CFB stream cipher mode.
960  *
961  * The underlying block cipher is determined by the key type.
962  */
963 #define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
964 
965 /** The OFB stream cipher mode.
966  *
967  * The underlying block cipher is determined by the key type.
968  */
969 #define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
970 
971 /** The XTS cipher mode.
972  *
973  * XTS is a cipher mode which is built from a block cipher. It requires at
974  * least one full block of input, but beyond this minimum the input
975  * does not need to be a whole number of blocks.
976  */
977 #define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
978 
979 /** The CBC block cipher chaining mode, with no padding.
980  *
981  * The underlying block cipher is determined by the key type.
982  *
983  * This symmetric cipher mode can only be used with messages whose lengths
984  * are whole number of blocks for the chosen block cipher.
985  */
986 #define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
987 
988 /** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
989  *
990  * \warning ECB mode does not protect the confidentiality of the encrypted data
991  * except in extremely narrow circumstances. It is recommended that applications
992  * only use ECB if they need to construct an operating mode that the
993  * implementation does not provide. Implementations are encouraged to provide
994  * the modes that applications need in preference to supporting direct access
995  * to ECB.
996  *
997  * The underlying block cipher is determined by the key type.
998  *
999  * This symmetric cipher mode can only be used with messages whose lengths are a
1000  * multiple of the block size of the chosen block cipher.
1001  *
1002  * ECB mode does not accept an initialization vector (IV). When using a
1003  * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1004  * and psa_cipher_set_iv() must not be called.
1005  */
1006 #define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
1007 
1008 /** The CBC block cipher chaining mode with PKCS#7 padding.
1009  *
1010  * The underlying block cipher is determined by the key type.
1011  *
1012  * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1013  */
1014 #define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
1015 
1016 #define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1017 
1018 /** Whether the specified algorithm is an AEAD mode on a block cipher.
1019  *
1020  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1021  *
1022  * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1023  * a block cipher, 0 otherwise.
1024  * This macro may return either 0 or 1 if \p alg is not a supported
1025  * algorithm identifier.
1026  */
1027 #define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1028  (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1029  (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1030 
1031 /** The CCM authenticated encryption algorithm.
1032  *
1033  * The underlying block cipher is determined by the key type.
1034  */
1035 #define PSA_ALG_CCM ((psa_algorithm_t)0x06401001)
1036 
1037 /** The GCM authenticated encryption algorithm.
1038  *
1039  * The underlying block cipher is determined by the key type.
1040  */
1041 #define PSA_ALG_GCM ((psa_algorithm_t)0x06401002)
1042 
1043 /** The Chacha20-Poly1305 AEAD algorithm.
1044  *
1045  * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1046  *
1047  * Implementations must support 12-byte nonces, may support 8-byte nonces,
1048  * and should reject other sizes.
1049  *
1050  * Implementations must support 16-byte tags and should reject other sizes.
1051  */
1052 #define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x06001005)
1053 
1054 /* In the encoding of a AEAD algorithm, the bits corresponding to
1055  * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1056  * The constants for default lengths follow this encoding.
1057  */
1058 #define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
1059 #define PSA_AEAD_TAG_LENGTH_OFFSET 8
1060 
1061 /** Macro to build a shortened AEAD algorithm.
1062  *
1063  * A shortened AEAD algorithm is similar to the corresponding AEAD
1064  * algorithm, but has an authentication tag that consists of fewer bytes.
1065  * Depending on the algorithm, the tag length may affect the calculation
1066  * of the ciphertext.
1067  *
1068  * \param aead_alg An AEAD algorithm identifier (value of type
1069  * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
1070  * is true).
1071  * \param tag_length Desired length of the authentication tag in bytes.
1072  *
1073  * \return The corresponding AEAD algorithm with the specified
1074  * length.
1075  * \return Unspecified if \p alg is not a supported
1076  * AEAD algorithm or if \p tag_length is not valid
1077  * for the specified AEAD algorithm.
1078  */
1079 #define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
1080  (((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
1081  ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1082  PSA_ALG_AEAD_TAG_LENGTH_MASK))
1083 
1084 /** Calculate the corresponding AEAD algorithm with the default tag length.
1085  *
1086  * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
1087  * #PSA_ALG_IS_AEAD(\p alg) is true).
1088  *
1089  * \return The corresponding AEAD algorithm with the default
1090  * tag length for that algorithm.
1091  */
1092 #define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
1093  ( \
1094  PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CCM) \
1095  PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_GCM) \
1096  PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
1097  0)
1098 #define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, ref) \
1099  PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
1100  PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
1101  ref :
1102 
1103 #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
1104 /** RSA PKCS#1 v1.5 signature with hashing.
1105  *
1106  * This is the signature scheme defined by RFC 8017
1107  * (PKCS#1: RSA Cryptography Specifications) under the name
1108  * RSASSA-PKCS1-v1_5.
1109  *
1110  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1111  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1112  * This includes #PSA_ALG_ANY_HASH
1113  * when specifying the algorithm in a usage policy.
1114  *
1115  * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
1116  * \return Unspecified if \p hash_alg is not a supported
1117  * hash algorithm.
1118  */
1119 #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1120  (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1121 /** Raw PKCS#1 v1.5 signature.
1122  *
1123  * The input to this algorithm is the DigestInfo structure used by
1124  * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1125  * steps 3&ndash;6.
1126  */
1127 #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1128 #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1129  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1130 
1131 #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
1132 /** RSA PSS signature with hashing.
1133  *
1134  * This is the signature scheme defined by RFC 8017
1135  * (PKCS#1: RSA Cryptography Specifications) under the name
1136  * RSASSA-PSS, with the message generation function MGF1, and with
1137  * a salt length equal to the length of the hash. The specified
1138  * hash algorithm is used to hash the input message, to create the
1139  * salted hash, and for the mask generation.
1140  *
1141  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1142  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1143  * This includes #PSA_ALG_ANY_HASH
1144  * when specifying the algorithm in a usage policy.
1145  *
1146  * \return The corresponding RSA PSS signature algorithm.
1147  * \return Unspecified if \p hash_alg is not a supported
1148  * hash algorithm.
1149  */
1150 #define PSA_ALG_RSA_PSS(hash_alg) \
1151  (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1152 #define PSA_ALG_IS_RSA_PSS(alg) \
1153  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1154 
1155 #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1156 /** ECDSA signature with hashing.
1157  *
1158  * This is the ECDSA signature scheme defined by ANSI X9.62,
1159  * with a random per-message secret number (*k*).
1160  *
1161  * The representation of the signature as a byte string consists of
1162  * the concatentation of the signature values *r* and *s*. Each of
1163  * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1164  * of the base point of the curve in octets. Each value is represented
1165  * in big-endian order (most significant octet first).
1166  *
1167  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1168  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1169  * This includes #PSA_ALG_ANY_HASH
1170  * when specifying the algorithm in a usage policy.
1171  *
1172  * \return The corresponding ECDSA signature algorithm.
1173  * \return Unspecified if \p hash_alg is not a supported
1174  * hash algorithm.
1175  */
1176 #define PSA_ALG_ECDSA(hash_alg) \
1177  (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1178 /** ECDSA signature without hashing.
1179  *
1180  * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1181  * without specifying a hash algorithm. This algorithm may only be
1182  * used to sign or verify a sequence of bytes that should be an
1183  * already-calculated hash. Note that the input is padded with
1184  * zeros on the left or truncated on the left as required to fit
1185  * the curve size.
1186  */
1187 #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1188 #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1189 /** Deterministic ECDSA signature with hashing.
1190  *
1191  * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1192  *
1193  * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1194  *
1195  * Note that when this algorithm is used for verification, signatures
1196  * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1197  * same private key are accepted. In other words,
1198  * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1199  * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1200  *
1201  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1202  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1203  * This includes #PSA_ALG_ANY_HASH
1204  * when specifying the algorithm in a usage policy.
1205  *
1206  * \return The corresponding deterministic ECDSA signature
1207  * algorithm.
1208  * \return Unspecified if \p hash_alg is not a supported
1209  * hash algorithm.
1210  */
1211 #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1212  (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1213 #define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
1214 #define PSA_ALG_IS_ECDSA(alg) \
1215  (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
1216  PSA_ALG_ECDSA_BASE)
1217 #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1218  (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
1219 #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1220  (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1221 #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1222  (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1223 
1224 /** Whether the specified algorithm is a hash-and-sign algorithm.
1225  *
1226  * Hash-and-sign algorithms are public-key signature algorithms structured
1227  * in two parts: first the calculation of a hash in a way that does not
1228  * depend on the key, then the calculation of a signature from the
1229  * hash value and the key.
1230  *
1231  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1232  *
1233  * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1234  * This macro may return either 0 or 1 if \p alg is not a supported
1235  * algorithm identifier.
1236  */
1237 #define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1238  (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1239  PSA_ALG_IS_ECDSA(alg))
1240 
1241 /** Get the hash used by a hash-and-sign signature algorithm.
1242  *
1243  * A hash-and-sign algorithm is a signature algorithm which is
1244  * composed of two phases: first a hashing phase which does not use
1245  * the key and produces a hash of the input message, then a signing
1246  * phase which only uses the hash and the key and not the message
1247  * itself.
1248  *
1249  * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1250  * #PSA_ALG_IS_SIGN(\p alg) is true).
1251  *
1252  * \return The underlying hash algorithm if \p alg is a hash-and-sign
1253  * algorithm.
1254  * \return 0 if \p alg is a signature algorithm that does not
1255  * follow the hash-and-sign structure.
1256  * \return Unspecified if \p alg is not a signature algorithm or
1257  * if it is not supported by the implementation.
1258  */
1259 #define PSA_ALG_SIGN_GET_HASH(alg) \
1260  (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1261  ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1262  ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1263  0)
1264 
1265 /** RSA PKCS#1 v1.5 encryption.
1266  */
1267 #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
1268 
1269 #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
1270 /** RSA OAEP encryption.
1271  *
1272  * This is the encryption scheme defined by RFC 8017
1273  * (PKCS#1: RSA Cryptography Specifications) under the name
1274  * RSAES-OAEP, with the message generation function MGF1.
1275  *
1276  * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1277  * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1278  * for MGF1.
1279  *
1280  * \return The corresponding RSA OAEP signature algorithm.
1281  * \return Unspecified if \p hash_alg is not a supported
1282  * hash algorithm.
1283  */
1284 #define PSA_ALG_RSA_OAEP(hash_alg) \
1285  (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1286 #define PSA_ALG_IS_RSA_OAEP(alg) \
1287  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1288 #define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1289  (PSA_ALG_IS_RSA_OAEP(alg) ? \
1290  ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1291  0)
1292 
1293 #define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x20000100)
1294 /** Macro to build an HKDF algorithm.
1295  *
1296  * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1297  *
1298  * This key derivation algorithm uses the following inputs:
1299  * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1300  * It is optional; if omitted, the derivation uses an empty salt.
1301  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1302  * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1303  * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1304  * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1305  * starting to generate output.
1306  *
1307  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1308  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1309  *
1310  * \return The corresponding HKDF algorithm.
1311  * \return Unspecified if \p hash_alg is not a supported
1312  * hash algorithm.
1313  */
1314 #define PSA_ALG_HKDF(hash_alg) \
1315  (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1316 /** Whether the specified algorithm is an HKDF algorithm.
1317  *
1318  * HKDF is a family of key derivation algorithms that are based on a hash
1319  * function and the HMAC construction.
1320  *
1321  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1322  *
1323  * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1324  * This macro may return either 0 or 1 if \c alg is not a supported
1325  * key derivation algorithm identifier.
1326  */
1327 #define PSA_ALG_IS_HKDF(alg) \
1328  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1329 #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1330  (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1331 
1332 #define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x20000200)
1333 /** Macro to build a TLS-1.2 PRF algorithm.
1334  *
1335  * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1336  * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1337  * used with either SHA-256 or SHA-384.
1338  *
1339  * This key derivation algorithm uses the following inputs, which must be
1340  * passed in the order given here:
1341  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1342  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1343  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1344  *
1345  * For the application to TLS-1.2 key expansion, the seed is the
1346  * concatenation of ServerHello.Random + ClientHello.Random,
1347  * and the label is "key expansion".
1348  *
1349  * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1350  * TLS 1.2 PRF using HMAC-SHA-256.
1351  *
1352  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1353  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1354  *
1355  * \return The corresponding TLS-1.2 PRF algorithm.
1356  * \return Unspecified if \p hash_alg is not a supported
1357  * hash algorithm.
1358  */
1359 #define PSA_ALG_TLS12_PRF(hash_alg) \
1360  (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1361 
1362 /** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1363  *
1364  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1365  *
1366  * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1367  * This macro may return either 0 or 1 if \c alg is not a supported
1368  * key derivation algorithm identifier.
1369  */
1370 #define PSA_ALG_IS_TLS12_PRF(alg) \
1371  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1372 #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1373  (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1374 
1375 #define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x20000300)
1376 /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1377  *
1378  * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1379  * from the PreSharedKey (PSK) through the application of padding
1380  * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1381  * The latter is based on HMAC and can be used with either SHA-256
1382  * or SHA-384.
1383  *
1384  * This key derivation algorithm uses the following inputs, which must be
1385  * passed in the order given here:
1386  * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1387  * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1388  * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1389  *
1390  * For the application to TLS-1.2, the seed (which is
1391  * forwarded to the TLS-1.2 PRF) is the concatenation of the
1392  * ClientHello.Random + ServerHello.Random,
1393  * and the label is "master secret" or "extended master secret".
1394  *
1395  * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1396  * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1397  *
1398  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1399  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1400  *
1401  * \return The corresponding TLS-1.2 PSK to MS algorithm.
1402  * \return Unspecified if \p hash_alg is not a supported
1403  * hash algorithm.
1404  */
1405 #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1406  (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1407 
1408 /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1409  *
1410  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1411  *
1412  * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1413  * This macro may return either 0 or 1 if \c alg is not a supported
1414  * key derivation algorithm identifier.
1415  */
1416 #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1417  (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1418 #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1419  (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1420 
1421 #define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x0803ffff)
1422 #define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0x10fc0000)
1423 
1424 /** Macro to build a combined algorithm that chains a key agreement with
1425  * a key derivation.
1426  *
1427  * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1428  * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1429  * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1430  * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
1431  *
1432  * \return The corresponding key agreement and derivation
1433  * algorithm.
1434  * \return Unspecified if \p ka_alg is not a supported
1435  * key agreement algorithm or \p kdf_alg is not a
1436  * supported key derivation algorithm.
1437  */
1438 #define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1439  ((ka_alg) | (kdf_alg))
1440 
1441 #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1442  (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1443 
1444 #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1445  (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
1446 
1447 /** Whether the specified algorithm is a raw key agreement algorithm.
1448  *
1449  * A raw key agreement algorithm is one that does not specify
1450  * a key derivation function.
1451  * Usually, raw key agreement algorithms are constructed directly with
1452  * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
1453  * constructed with PSA_ALG_KEY_AGREEMENT().
1454  *
1455  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1456  *
1457  * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1458  * This macro may return either 0 or 1 if \p alg is not a supported
1459  * algorithm identifier.
1460  */
1461 #define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1462  (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1463  PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1464 
1465 #define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1466  ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1467 
1468 /** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1469  *
1470  * The shared secret produced by key agreement is
1471  * `g^{ab}` in big-endian format.
1472  * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1473  * in bits.
1474  */
1475 #define PSA_ALG_FFDH ((psa_algorithm_t)0x30100000)
1476 
1477 /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1478  *
1479  * This includes the raw finite field Diffie-Hellman algorithm as well as
1480  * finite-field Diffie-Hellman followed by any supporter key derivation
1481  * algorithm.
1482  *
1483  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1484  *
1485  * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1486  * This macro may return either 0 or 1 if \c alg is not a supported
1487  * key agreement algorithm identifier.
1488  */
1489 #define PSA_ALG_IS_FFDH(alg) \
1490  (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
1491 
1492 /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1493  *
1494  * The shared secret produced by key agreement is the x-coordinate of
1495  * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1496  * `m` is the bit size associated with the curve, i.e. the bit size of the
1497  * order of the curve's coordinate field. When `m` is not a multiple of 8,
1498  * the byte containing the most significant bit of the shared secret
1499  * is padded with zero bits. The byte order is either little-endian
1500  * or big-endian depending on the curve type.
1501  *
1502  * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1503  * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1504  * in little-endian byte order.
1505  * The bit size is 448 for Curve448 and 255 for Curve25519.
1506  * - For Weierstrass curves over prime fields (curve types
1507  * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1508  * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1509  * in big-endian byte order.
1510  * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1511  * - For Weierstrass curves over binary fields (curve types
1512  * `PSA_ECC_CURVE_SECTXXX`),
1513  * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1514  * in big-endian byte order.
1515  * The bit size is `m` for the field `F_{2^m}`.
1516  */
1517 #define PSA_ALG_ECDH ((psa_algorithm_t)0x30200000)
1518 
1519 /** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1520  * algorithm.
1521  *
1522  * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1523  * elliptic curve Diffie-Hellman followed by any supporter key derivation
1524  * algorithm.
1525  *
1526  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1527  *
1528  * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1529  * 0 otherwise.
1530  * This macro may return either 0 or 1 if \c alg is not a supported
1531  * key agreement algorithm identifier.
1532  */
1533 #define PSA_ALG_IS_ECDH(alg) \
1534  (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
1535 
1536 /** Whether the specified algorithm encoding is a wildcard.
1537  *
1538  * Wildcard values may only be used to set the usage algorithm field in
1539  * a policy, not to perform an operation.
1540  *
1541  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1542  *
1543  * \return 1 if \c alg is a wildcard algorithm encoding.
1544  * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1545  * an operation).
1546  * \return This macro may return either 0 or 1 if \c alg is not a supported
1547  * algorithm identifier.
1548  */
1549 #define PSA_ALG_IS_WILDCARD(alg) \
1550  (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1551  PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1552  (alg) == PSA_ALG_ANY_HASH)
1553 
1554 /**@}*/
1555 
1556 /** \defgroup key_lifetimes Key lifetimes
1557  * @{
1558  */
1559 
1560 /** A volatile key only exists as long as the handle to it is not closed.
1561  * The key material is guaranteed to be erased on a power reset.
1562  */
1563 #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1564 
1565 /** The default storage area for persistent keys.
1566  *
1567  * A persistent key remains in storage until it is explicitly destroyed or
1568  * until the corresponding storage area is wiped. This specification does
1569  * not define any mechanism to wipe a storage area, but implementations may
1570  * provide their own mechanism (for example to perform a factory reset,
1571  * to prepare for device refurbishment, or to uninstall an application).
1572  *
1573  * This lifetime value is the default storage area for the calling
1574  * application. Implementations may offer other storage areas designated
1575  * by other lifetime values as implementation-specific extensions.
1576  */
1577 #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1578 
1579 /** The minimum value for a key identifier chosen by the application.
1580  */
1581 #define PSA_KEY_ID_USER_MIN ((psa_app_key_id_t)0x00000001)
1582 /** The maximum value for a key identifier chosen by the application.
1583  */
1584 #define PSA_KEY_ID_USER_MAX ((psa_app_key_id_t)0x3fffffff)
1585 /** The minimum value for a key identifier chosen by the implementation.
1586  */
1587 #define PSA_KEY_ID_VENDOR_MIN ((psa_app_key_id_t)0x40000000)
1588 /** The maximum value for a key identifier chosen by the implementation.
1589  */
1590 #define PSA_KEY_ID_VENDOR_MAX ((psa_app_key_id_t)0x7fffffff)
1591 
1592 /**@}*/
1593 
1594 /** \defgroup policy Key policies
1595  * @{
1596  */
1597 
1598 /** Whether the key may be exported.
1599  *
1600  * A public key or the public part of a key pair may always be exported
1601  * regardless of the value of this permission flag.
1602  *
1603  * If a key does not have export permission, implementations shall not
1604  * allow the key to be exported in plain form from the cryptoprocessor,
1605  * whether through psa_export_key() or through a proprietary interface.
1606  * The key may however be exportable in a wrapped form, i.e. in a form
1607  * where it is encrypted by another key.
1608  */
1609 #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1610 
1611 /** Whether the key may be copied.
1612  *
1613  * This flag allows the use of psa_copy_key() to make a copy of the key
1614  * with the same policy or a more restrictive policy.
1615  *
1616  * For lifetimes for which the key is located in a secure element which
1617  * enforce the non-exportability of keys, copying a key outside the secure
1618  * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
1619  * Copying the key inside the secure element is permitted with just
1620  * #PSA_KEY_USAGE_COPY if the secure element supports it.
1621  * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
1622  * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
1623  * is sufficient to permit the copy.
1624  */
1625 #define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
1626 
1627 /** Whether the key may be used to encrypt a message.
1628  *
1629  * This flag allows the key to be used for a symmetric encryption operation,
1630  * for an AEAD encryption-and-authentication operation,
1631  * or for an asymmetric encryption operation,
1632  * if otherwise permitted by the key's type and policy.
1633  *
1634  * For a key pair, this concerns the public key.
1635  */
1636 #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1637 
1638 /** Whether the key may be used to decrypt a message.
1639  *
1640  * This flag allows the key to be used for a symmetric decryption operation,
1641  * for an AEAD decryption-and-verification operation,
1642  * or for an asymmetric decryption operation,
1643  * if otherwise permitted by the key's type and policy.
1644  *
1645  * For a key pair, this concerns the private key.
1646  */
1647 #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1648 
1649 /** Whether the key may be used to sign a message.
1650  *
1651  * This flag allows the key to be used for a MAC calculation operation
1652  * or for an asymmetric signature operation,
1653  * if otherwise permitted by the key's type and policy.
1654  *
1655  * For a key pair, this concerns the private key.
1656  */
1657 #define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00000400)
1658 
1659 /** Whether the key may be used to verify a message signature.
1660  *
1661  * This flag allows the key to be used for a MAC verification operation
1662  * or for an asymmetric signature verification operation,
1663  * if otherwise permitted by by the key's type and policy.
1664  *
1665  * For a key pair, this concerns the public key.
1666  */
1667 #define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00000800)
1668 
1669 /** Whether the key may be used to derive other keys.
1670  */
1671 #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1672 
1673 /**@}*/
1674 
1675 /** \defgroup derivation Key derivation
1676  * @{
1677  */
1678 
1679 /** A secret input for key derivation.
1680  *
1681  * This should be a key of type #PSA_KEY_TYPE_DERIVE
1682  * (passed to psa_key_derivation_input_key())
1683  * or the shared secret resulting from a key agreement
1684  * (obtained via psa_key_derivation_key_agreement()).
1685  *
1686  * The secret can also be a direct input (passed to
1687  * key_derivation_input_bytes()). In this case, the derivation operation
1688  * may not be used to derive keys: the operation will only allow
1689  * psa_key_derivation_output_bytes(), not psa_key_derivation_output_key().
1690  */
1691 #define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
1692 
1693 /** A label for key derivation.
1694  *
1695  * This should be a direct input.
1696  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1697  */
1698 #define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
1699 
1700 /** A salt for key derivation.
1701  *
1702  * This should be a direct input.
1703  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1704  */
1705 #define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
1706 
1707 /** An information string for key derivation.
1708  *
1709  * This should be a direct input.
1710  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1711  */
1712 #define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
1713 
1714 /** A seed for key derivation.
1715  *
1716  * This should be a direct input.
1717  * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1718  */
1719 #define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
1720 
1721 /** Extract the curve from an elliptic curve key type. */
1722 #define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
1723  ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
1724  ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
1725  0))
1726 
1727 
1728 /** SEC Koblitz curves over prime fields.
1729  *
1730  * This family comprises the following curves:
1731  * secp192k1, secp224k1, secp256k1.
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_SECP_K1 ((psa_ecc_family_t) 0x17)
1737 
1738 /** SEC random curves over prime fields.
1739  *
1740  * This family comprises the following curves:
1741  * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
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_SECP_R1 ((psa_ecc_family_t) 0x12)
1747 /* SECP160R2 (SEC2 v1, obsolete) */
1748 #define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
1749 
1750 /** SEC Koblitz curves over binary fields.
1751  *
1752  * This family comprises the following curves:
1753  * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
1754  * They are defined in _Standards for Efficient Cryptography_,
1755  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1756  * https://www.secg.org/sec2-v2.pdf
1757  */
1758 #define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
1759 
1760 /** SEC random curves over binary fields.
1761  *
1762  * This family comprises the following curves:
1763  * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
1764  * They are defined in _Standards for Efficient Cryptography_,
1765  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1766  * https://www.secg.org/sec2-v2.pdf
1767  */
1768 #define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
1769 
1770 /** SEC additional random curves over binary fields.
1771  *
1772  * This family comprises the following curve:
1773  * sect163r2.
1774  * It is defined in _Standards for Efficient Cryptography_,
1775  * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1776  * https://www.secg.org/sec2-v2.pdf
1777  */
1778 /**@}*/
1779 #define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
1780 
1781 /** Brainpool P random curves.
1782  *
1783  * This family comprises the following curves:
1784  * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
1785  * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
1786  * It is defined in RFC 5639.
1787  */
1788 #define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
1789 
1790 /** Curve25519 and Curve448.
1791  *
1792  * This family comprises the following Montgomery curves:
1793  * - 255-bit: Bernstein et al.,
1794  * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
1795  * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
1796  * - 448-bit: Hamburg,
1797  * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
1798  * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
1799  */
1800 #define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
1801 
1802 #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.