blob: 315cd107d05ec4294c08cc0f0b088d8bf58a9be1 [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
2 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto_values.h
Jamie Fox0e54ebc2019-04-09 14:21:04 +01009 *
10 * \brief PSA cryptography module: macros to build and analyze integer values.
11 *
12 * \note This file may not be included directly. Applications must
Jamie Foxcc31d402019-01-28 17:13:52 +000013 * include psa/crypto.h. Drivers must include the appropriate driver
Jamie Fox0e54ebc2019-04-09 14:21:04 +010014 * 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 slot is occupied when it needs to be free or vice versa,
99 * but shall return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
100 * as applicable. */
101#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
102
103/** The parameters passed to the function are invalid.
104 *
105 * Implementations may return this error any time a parameter or
106 * combination of parameters are recognized as invalid.
107 *
108 * Implementations shall not return this error code to indicate
109 * that a key slot is occupied when it needs to be free or vice versa,
110 * but shall return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
111 * as applicable.
112 *
113 * Implementation shall not return this error code to indicate that a
114 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
115 * instead.
116 */
117#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
118
119/** There is not enough runtime memory.
120 *
121 * If the action is carried out across multiple security realms, this
122 * error can refer to available memory in any of the security realms. */
123#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
124
125/** There is not enough persistent storage.
126 *
127 * Functions that modify the key storage return this error code if
128 * there is insufficient storage space on the host media. In addition,
129 * many functions that do not otherwise access storage may return this
130 * error code if the implementation requires a mandatory log entry for
131 * the requested action and the log storage space is full. */
132#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
133
134/** There was a communication failure inside the implementation.
135 *
136 * This can indicate a communication failure between the application
137 * and an external cryptoprocessor or between the cryptoprocessor and
138 * an external volatile or persistent memory. A communication failure
139 * may be transient or permanent depending on the cause.
140 *
141 * \warning If a function returns this error, it is undetermined
142 * whether the requested action has completed or not. Implementations
143 * should return #PSA_SUCCESS on successful completion whenver
144 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
145 * if the requested action was completed successfully in an external
146 * cryptoprocessor but there was a breakdown of communication before
147 * the cryptoprocessor could report the status to the application.
148 */
149#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
150
151/** There was a storage failure that may have led to data loss.
152 *
153 * This error indicates that some persistent storage is corrupted.
154 * It should not be used for a corruption of volatile memory
155 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
156 * between the cryptoprocessor and its external storage (use
157 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
158 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
159 *
160 * Note that a storage failure does not indicate that any data that was
161 * previously read is invalid. However this previously read data may no
162 * longer be readable from storage.
163 *
164 * When a storage failure occurs, it is no longer possible to ensure
165 * the global integrity of the keystore. Depending on the global
166 * integrity guarantees offered by the implementation, access to other
167 * data may or may not fail even if the data is still readable but
168 * its integrity canont be guaranteed.
169 *
170 * Implementations should only use this error code to report a
171 * permanent storage corruption. However application writers should
172 * keep in mind that transient errors while reading the storage may be
173 * reported using this error code. */
174#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
175
176/** A hardware failure was detected.
177 *
178 * A hardware failure may be transient or permanent depending on the
179 * cause. */
180#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
181
182/** A tampering attempt was detected.
183 *
184 * If an application receives this error code, there is no guarantee
185 * that previously accessed or computed data was correct and remains
186 * confidential. Applications should not perform any security function
187 * and should enter a safe failure state.
188 *
189 * Implementations may return this error code if they detect an invalid
190 * state that cannot happen during normal operation and that indicates
191 * that the implementation's security guarantees no longer hold. Depending
192 * on the implementation architecture and on its security and safety goals,
193 * the implementation may forcibly terminate the application.
194 *
195 * This error code is intended as a last resort when a security breach
196 * is detected and it is unsure whether the keystore data is still
197 * protected. Implementations shall only return this error code
198 * to report an alarm from a tampering detector, to indicate that
199 * the confidentiality of stored data can no longer be guaranteed,
200 * or to indicate that the integrity of previously returned data is now
201 * considered compromised. Implementations shall not use this error code
202 * to indicate a hardware failure that merely makes it impossible to
203 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
204 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
205 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
206 * instead).
207 *
208 * This error indicates an attack against the application. Implementations
209 * shall not return this error code as a consequence of the behavior of
210 * the application itself. */
211#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)-151)
212
213/** There is not enough entropy to generate random data needed
214 * for the requested action.
215 *
216 * This error indicates a failure of a hardware random generator.
217 * Application writers should note that this error can be returned not
218 * only by functions whose purpose is to generate random data, such
219 * as key, IV or nonce generation, but also by functions that execute
220 * an algorithm with a randomized result, as well as functions that
221 * use randomization of intermediate computations as a countermeasure
222 * to certain attacks.
223 *
224 * Implementations should avoid returning this error after psa_crypto_init()
225 * has succeeded. Implementations should generate sufficient
226 * entropy during initialization and subsequently use a cryptographically
227 * secure pseudorandom generator (PRNG). However implementations may return
228 * this error at any time if a policy requires the PRNG to be reseeded
229 * during normal operation. */
230#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
231
232/** The signature, MAC or hash is incorrect.
233 *
234 * Verification functions return this error if the verification
235 * calculations completed successfully, and the value to be verified
236 * was determined to be incorrect.
237 *
238 * If the value to verify has an invalid size, implementations may return
239 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
240#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
241
242/** The decrypted padding is incorrect.
243 *
244 * \warning In some protocols, when decrypting data, it is essential that
245 * the behavior of the application does not depend on whether the padding
246 * is correct, down to precise timing. Applications should prefer
247 * protocols that use authenticated encryption rather than plain
248 * encryption. If the application must perform a decryption of
249 * unauthenticated data, the application writer should take care not
250 * to reveal whether the padding is invalid.
251 *
252 * Implementations should strive to make valid and invalid padding
253 * as close as possible to indistinguishable to an external observer.
254 * In particular, the timing of a decryption operation should not
255 * depend on the validity of the padding. */
256#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
257
258/** Return this error when there's insufficient data when attempting
259 * to read from a resource. */
260#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
261
262/** The key handle is not valid.
263 */
264#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
265
266/**@}*/
267
268/** \defgroup crypto_types Key and algorithm types
269 * @{
270 */
271
272/** An invalid key type value.
273 *
274 * Zero is not the encoding of any key type.
275 */
276#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
277
278/** Vendor-defined flag
279 *
280 * Key types defined by this standard will never have the
281 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
282 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
283 * respect the bitwise structure used by standard encodings whenever practical.
284 */
285#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
286
287#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
288#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
289#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
290#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
291#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
292
293#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
294
295/** Whether a key type is vendor-defined. */
296#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
297 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
298
299/** Whether a key type is an unstructured array of bytes.
300 *
301 * This encompasses both symmetric keys and non-key data.
302 */
303#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
304 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
305 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
306
307/** Whether a key type is asymmetric: either a key pair or a public key. */
308#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
309 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
310 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
311 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
312/** Whether a key type is the public part of a key pair. */
313#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
314 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
315/** Whether a key type is a key pair containing a private part and a public
316 * part. */
317#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
318 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
319/** The key pair type corresponding to a public key type.
320 *
321 * You may also pass a key pair type as \p type, it will be left unchanged.
322 *
323 * \param type A public key type or key pair type.
324 *
325 * \return The corresponding key pair type.
326 * If \p type is not a public key or a key pair,
327 * the return value is undefined.
328 */
329#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
330 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
331/** The public key type corresponding to a key pair type.
332 *
333 * You may also pass a key pair type as \p type, it will be left unchanged.
334 *
335 * \param type A public key type or key pair type.
336 *
337 * \return The corresponding public key type.
338 * If \p type is not a public key or a key pair,
339 * the return value is undefined.
340 */
341#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
342 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
343
344/** Raw data.
345 *
346 * A "key" of this type cannot be used for any cryptographic operation.
347 * Applications may use this type to store arbitrary data in the keystore. */
348#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
349
350/** HMAC key.
351 *
352 * The key policy determines which underlying hash algorithm the key can be
353 * used for.
354 *
355 * HMAC keys should generally have the same size as the underlying hash.
356 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
357 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
358#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
359
360/** A secret for key derivation.
361 *
362 * The key policy determines which key derivation algorithm the key
363 * can be used for.
364 */
365#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
366
367/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
368 *
369 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
370 * 32 bytes (AES-256).
371 */
372#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
373
374/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
375 *
376 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
377 * 24 bytes (3-key 3DES).
378 *
379 * Note that single DES and 2-key 3DES are weak and strongly
380 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
381 * is weak and deprecated and should only be used in legacy protocols.
382 */
383#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
384
385/** Key for an cipher, AEAD or MAC algorithm based on the
386 * Camellia block cipher. */
387#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
388
389/** Key for the RC4 stream cipher.
390 *
391 * Note that RC4 is weak and deprecated and should only be used in
392 * legacy protocols. */
393#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
394
395/** RSA public key. */
396#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
397/** RSA key pair (private and public key). */
398#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
399/** Whether a key type is an RSA key (pair or public-only). */
400#define PSA_KEY_TYPE_IS_RSA(type) \
401 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
402
403/** DSA public key. */
404#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
405/** DSA key pair (private and public key). */
406#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
407/** Whether a key type is an DSA key (pair or public-only). */
408#define PSA_KEY_TYPE_IS_DSA(type) \
409 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
410
411#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
412#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
413#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
414/** Elliptic curve key pair. */
415#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
416 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
417/** Elliptic curve public key. */
418#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
419 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
420
421/** Whether a key type is an elliptic curve key (pair or public-only). */
422#define PSA_KEY_TYPE_IS_ECC(type) \
423 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
424 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
425/** Whether a key type is an elliptic curve key pair. */
426#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
427 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
428 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
429/** Whether a key type is an elliptic curve public key. */
430#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
431 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
432 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
433
434/** Extract the curve from an elliptic curve key type. */
435#define PSA_KEY_TYPE_GET_CURVE(type) \
436 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
437 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
438 0))
439
440/* The encoding of curve identifiers is currently aligned with the
441 * TLS Supported Groups Registry (formerly known as the
442 * TLS EC Named Curve Registry)
443 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
444 * The values are defined by RFC 8422 and RFC 7027. */
445#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
446#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
447#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
448#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
449#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
450#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
451#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
452#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
453#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
454#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
455#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
456#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
457#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
458#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
459#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
460#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
461#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
462#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
463#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
464#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
465#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
466#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
467#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
468#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
469#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
470#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
471#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
472#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
473#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
474#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
475
476/** The block size of a block cipher.
477 *
478 * \param type A cipher key type (value of type #psa_key_type_t).
479 *
480 * \return The block size for a block cipher, or 1 for a stream cipher.
481 * The return value is undefined if \p type is not a supported
482 * cipher key type.
483 *
484 * \note It is possible to build stream cipher algorithms on top of a block
485 * cipher, for example CTR mode (#PSA_ALG_CTR).
486 * This macro only takes the key type into account, so it cannot be
487 * used to determine the size of the data that #psa_cipher_update()
488 * might buffer for future processing in general.
489 *
490 * \note This macro returns a compile-time constant if its argument is one.
491 *
492 * \warning This macro may evaluate its argument multiple times.
493 */
494#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
495 ( \
496 (type) == PSA_KEY_TYPE_AES ? 16 : \
497 (type) == PSA_KEY_TYPE_DES ? 8 : \
498 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
499 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
500 0)
501
502#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
503#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
504#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
505#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
506#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
507#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
508#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
509#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
510#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
511#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
512#define PSA_ALG_CATEGORY_KEY_SELECTION ((psa_algorithm_t)0x31000000)
513
514#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
515 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
516
517/** Whether the specified algorithm is a hash algorithm.
518 *
519 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
520 *
521 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
522 * This macro may return either 0 or 1 if \p alg is not a supported
523 * algorithm identifier.
524 */
525#define PSA_ALG_IS_HASH(alg) \
526 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
527
528/** Whether the specified algorithm is a MAC algorithm.
529 *
530 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
531 *
532 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
533 * This macro may return either 0 or 1 if \p alg is not a supported
534 * algorithm identifier.
535 */
536#define PSA_ALG_IS_MAC(alg) \
537 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
538
539/** Whether the specified algorithm is a symmetric cipher algorithm.
540 *
541 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
542 *
543 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
544 * This macro may return either 0 or 1 if \p alg is not a supported
545 * algorithm identifier.
546 */
547#define PSA_ALG_IS_CIPHER(alg) \
548 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
549
550/** Whether the specified algorithm is an authenticated encryption
551 * with associated data (AEAD) algorithm.
552 *
553 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
554 *
555 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
556 * This macro may return either 0 or 1 if \p alg is not a supported
557 * algorithm identifier.
558 */
559#define PSA_ALG_IS_AEAD(alg) \
560 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
561
562/** Whether the specified algorithm is a public-key signature algorithm.
563 *
564 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
565 *
566 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
567 * This macro may return either 0 or 1 if \p alg is not a supported
568 * algorithm identifier.
569 */
570#define PSA_ALG_IS_SIGN(alg) \
571 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
572
573/** Whether the specified algorithm is a public-key encryption algorithm.
574 *
575 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
576 *
577 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
578 * This macro may return either 0 or 1 if \p alg is not a supported
579 * algorithm identifier.
580 */
581#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
582 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
583
584#define PSA_ALG_KEY_SELECTION_FLAG ((psa_algorithm_t)0x01000000)
585/** Whether the specified algorithm is a key agreement algorithm.
586 *
587 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
588 *
589 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
590 * This macro may return either 0 or 1 if \p alg is not a supported
591 * algorithm identifier.
592 */
593#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
594 (((alg) & PSA_ALG_CATEGORY_MASK & ~PSA_ALG_KEY_SELECTION_FLAG) == \
595 PSA_ALG_CATEGORY_KEY_AGREEMENT)
596
597/** Whether the specified algorithm is a key derivation algorithm.
598 *
599 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
600 *
601 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
602 * This macro may return either 0 or 1 if \p alg is not a supported
603 * algorithm identifier.
604 */
605#define PSA_ALG_IS_KEY_DERIVATION(alg) \
606 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
607
608/** Whether the specified algorithm is a key selection algorithm.
609 *
610 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
611 *
612 * \return 1 if \p alg is a key selection algorithm, 0 otherwise.
613 * This macro may return either 0 or 1 if \p alg is not a supported
614 * algorithm identifier.
615 */
616#define PSA_ALG_IS_KEY_SELECTION(alg) \
617 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_SELECTION)
618
619#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
620
621#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
622#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
623#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
624#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
625#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
626/** SHA2-224 */
627#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
628/** SHA2-256 */
629#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
630/** SHA2-384 */
631#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
632/** SHA2-512 */
633#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
634/** SHA2-512/224 */
635#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
636/** SHA2-512/256 */
637#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
638/** SHA3-224 */
639#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
640/** SHA3-256 */
641#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
642/** SHA3-384 */
643#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
644/** SHA3-512 */
645#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
646
647/** In a hash-and-sign algorithm policy, allow any hash algorithm.
648 *
649 * This value may be used to form the algorithm usage field of a policy
650 * for a signature algorithm that is parametrized by a hash. The key
651 * may then be used to perform operations using the same signature
652 * algorithm parametrized with any supported hash.
653 *
654 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
655 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
656 * - #PSA_ALG_DSA, #PSA_ALG_DETERMINISTIC_DSA,
657 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
658 * Then you may create and use a key as follows:
659 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
660 * ```
661 * psa_key_policy_set_usage(&policy,
662 * PSA_KEY_USAGE_SIGN, //or PSA_KEY_USAGE_VERIFY
663 * PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
664 * psa_set_key_policy(handle, &policy);
665 * ```
666 * - Import or generate key material.
667 * - Call psa_asymmetric_sign() or psa_asymmetric_verify(), passing
668 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
669 * call to sign or verify a message may use a different hash.
670 * ```
671 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
672 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
673 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
674 * ```
675 *
676 * This value may not be used to build other algorithms that are
677 * parametrized over a hash. For any valid use of this macro to build
678 * an algorithm `\p alg`, #PSA_ALG_IS_HASH_AND_SIGN(\p alg) is true.
679 *
680 * This value may not be used to build an algorithm specification to
681 * perform an operation. It is only valid to build policies.
682 */
683#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x010000ff)
684
685#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
686#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
687/** Macro to build an HMAC algorithm.
688 *
689 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
690 *
691 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
692 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
693 *
694 * \return The corresponding HMAC algorithm.
695 * \return Unspecified if \p alg is not a supported
696 * hash algorithm.
697 */
698#define PSA_ALG_HMAC(hash_alg) \
699 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
700
701#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
702 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
703
704/** Whether the specified algorithm is an HMAC algorithm.
705 *
706 * HMAC is a family of MAC algorithms that are based on a hash function.
707 *
708 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
709 *
710 * \return 1 if \p alg is an HMAC 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_HMAC(alg) \
715 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
716 PSA_ALG_HMAC_BASE)
717
718/* In the encoding of a MAC algorithm, the bits corresponding to
719 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
720 * truncated. As an exception, the value 0 means the untruncated algorithm,
721 * whatever its length is. The length is encoded in 6 bits, so it can
722 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
723 * to full length is correctly encoded as 0 and any non-trivial truncation
724 * is correctly encoded as a value between 1 and 63. */
725#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
726#define PSA_MAC_TRUNCATION_OFFSET 8
727
728/** Macro to build a truncated MAC algorithm.
729 *
730 * A truncated MAC algorithm is identical to the corresponding MAC
731 * algorithm except that the MAC value for the truncated algorithm
732 * consists of only the first \p mac_length bytes of the MAC value
733 * for the untruncated algorithm.
734 *
735 * \note This macro may allow constructing algorithm identifiers that
736 * are not valid, either because the specified length is larger
737 * than the untruncated MAC or because the specified length is
738 * smaller than permitted by the implementation.
739 *
740 * \note It is implementation-defined whether a truncated MAC that
741 * is truncated to the same length as the MAC of the untruncated
742 * algorithm is considered identical to the untruncated algorithm
743 * for policy comparison purposes.
744 *
745 * \param mac_alg A MAC algorithm identifier (value of type
746 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
747 * is true). This may be a truncated or untruncated
748 * MAC algorithm.
749 * \param mac_length Desired length of the truncated MAC in bytes.
750 * This must be at most the full length of the MAC
751 * and must be at least an implementation-specified
752 * minimum. The implementation-specified minimum
753 * shall not be zero.
754 *
755 * \return The corresponding MAC algorithm with the specified
756 * length.
757 * \return Unspecified if \p alg is not a supported
758 * MAC algorithm or if \p mac_length is too small or
759 * too large for the specified MAC algorithm.
760 */
761#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
762 (((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
763 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
764
765/** Macro to build the base MAC algorithm corresponding to a truncated
766 * MAC algorithm.
767 *
768 * \param mac_alg A MAC algorithm identifier (value of type
769 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
770 * is true). This may be a truncated or untruncated
771 * MAC algorithm.
772 *
773 * \return The corresponding base MAC algorithm.
774 * \return Unspecified if \p alg is not a supported
775 * MAC algorithm.
776 */
777#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
778 ((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
779
780/** Length to which a MAC algorithm is truncated.
781 *
782 * \param mac_alg A MAC algorithm identifier (value of type
783 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
784 * is true).
785 *
786 * \return Length of the truncated MAC in bytes.
787 * \return 0 if \p alg is a non-truncated MAC algorithm.
788 * \return Unspecified if \p alg is not a supported
789 * MAC algorithm.
790 */
791#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
792 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
793
794#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
795#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
796#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
797#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
798
799/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
800 *
801 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
802 *
803 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
804 * This macro may return either 0 or 1 if \p alg is not a supported
805 * algorithm identifier.
806 */
807#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
808 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
809 PSA_ALG_CIPHER_MAC_BASE)
810
811#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
812#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
813
814/** Whether the specified algorithm is a stream cipher.
815 *
816 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
817 * by applying a bitwise-xor with a stream of bytes that is generated
818 * from a key.
819 *
820 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
821 *
822 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
823 * This macro may return either 0 or 1 if \p alg is not a supported
824 * algorithm identifier or if it is not a symmetric cipher algorithm.
825 */
826#define PSA_ALG_IS_STREAM_CIPHER(alg) \
827 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
828 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
829
830/** The ARC4 stream cipher algorithm.
831 */
832#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
833
834/** The CTR stream cipher mode.
835 *
836 * CTR is a stream cipher which is built from a block cipher.
837 * The underlying block cipher is determined by the key type.
838 * For example, to use AES-128-CTR, use this algorithm with
839 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
840 */
841#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
842
843#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
844
845#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
846
847/** The XTS cipher mode.
848 *
849 * XTS is a cipher mode which is built from a block cipher. It requires at
850 * least one full block of input, but beyond this minimum the input
851 * does not need to be a whole number of blocks.
852 */
853#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
854
855/** The CBC block cipher chaining mode, with no padding.
856 *
857 * The underlying block cipher is determined by the key type.
858 *
859 * This symmetric cipher mode can only be used with messages whose lengths
860 * are whole number of blocks for the chosen block cipher.
861 */
862#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
863
864/** The CBC block cipher chaining mode with PKCS#7 padding.
865 *
866 * The underlying block cipher is determined by the key type.
867 *
868 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
869 */
870#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
871
872#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
873#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
874
875/* In the encoding of a AEAD algorithm, the bits corresponding to
876 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
877 * The constants for default lengths follow this encoding.
878 */
879#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
880#define PSA_AEAD_TAG_LENGTH_OFFSET 8
881
882/** Macro to build a shortened AEAD algorithm.
883 *
884 * A shortened AEAD algorithm is similar to the corresponding AEAD
885 * algorithm, but has an authentication tag that consists of fewer bytes.
886 * Depending on the algorithm, the tag length may affect the calculation
887 * of the ciphertext.
888 *
889 * \param aead_alg An AEAD algorithm identifier (value of type
890 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
891 * is true).
892 * \param tag_length Desired length of the authentication tag in bytes.
893 *
894 * \return The corresponding AEAD algorithm with the specified
895 * length.
896 * \return Unspecified if \p alg is not a supported
897 * AEAD algorithm or if \p tag_length is not valid
898 * for the specified AEAD algorithm.
899 */
900#define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
901 (((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
902 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
903 PSA_ALG_AEAD_TAG_LENGTH_MASK))
904
905/** Calculate the corresponding AEAD algorithm with the default tag length.
906 *
907 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
908 * #PSA_ALG_IS_AEAD(\p alg) is true).
909 *
910 * \return The corresponding AEAD algorithm with the default
911 * tag length for that algorithm.
912 */
913#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
914 ( \
915 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, PSA_ALG_CCM) \
916 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, PSA_ALG_GCM) \
917 0)
918#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, ref) \
919 PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
920 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
921 ref :
922
923#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
924/** RSA PKCS#1 v1.5 signature with hashing.
925 *
926 * This is the signature scheme defined by RFC 8017
927 * (PKCS#1: RSA Cryptography Specifications) under the name
928 * RSASSA-PKCS1-v1_5.
929 *
930 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
931 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
932 * This includes #PSA_ALG_ANY_HASH
933 * when specifying the algorithm in a usage policy.
934 *
935 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
936 * \return Unspecified if \p alg is not a supported
937 * hash algorithm.
938 */
939#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
940 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
941/** Raw PKCS#1 v1.5 signature.
942 *
943 * The input to this algorithm is the DigestInfo structure used by
944 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
945 * steps 3&ndash;6.
946 */
947#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
948#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
949 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
950
951#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
952/** RSA PSS signature with hashing.
953 *
954 * This is the signature scheme defined by RFC 8017
955 * (PKCS#1: RSA Cryptography Specifications) under the name
956 * RSASSA-PSS, with the message generation function MGF1, and with
957 * a salt length equal to the length of the hash. The specified
958 * hash algorithm is used to hash the input message, to create the
959 * salted hash, and for the mask generation.
960 *
961 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
962 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
963 * This includes #PSA_ALG_ANY_HASH
964 * when specifying the algorithm in a usage policy.
965 *
966 * \return The corresponding RSA PSS signature algorithm.
967 * \return Unspecified if \p alg is not a supported
968 * hash algorithm.
969 */
970#define PSA_ALG_RSA_PSS(hash_alg) \
971 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
972#define PSA_ALG_IS_RSA_PSS(alg) \
973 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
974
975#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
976/** DSA signature with hashing.
977 *
978 * This is the signature scheme defined by FIPS 186-4,
979 * with a random per-message secret number (*k*).
980 *
981 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
982 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
983 * This includes #PSA_ALG_ANY_HASH
984 * when specifying the algorithm in a usage policy.
985 *
986 * \return The corresponding DSA signature algorithm.
987 * \return Unspecified if \p alg is not a supported
988 * hash algorithm.
989 */
990#define PSA_ALG_DSA(hash_alg) \
991 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
992#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
993#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
994#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
995 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
996#define PSA_ALG_IS_DSA(alg) \
997 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
998 PSA_ALG_DSA_BASE)
999#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1000 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
1001#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1002 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1003#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1004 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1005
1006#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1007/** ECDSA signature with hashing.
1008 *
1009 * This is the ECDSA signature scheme defined by ANSI X9.62,
1010 * with a random per-message secret number (*k*).
1011 *
1012 * The representation of the signature as a byte string consists of
1013 * the concatentation of the signature values *r* and *s*. Each of
1014 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1015 * of the base point of the curve in octets. Each value is represented
1016 * in big-endian order (most significant octet first).
1017 *
1018 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1019 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1020 * This includes #PSA_ALG_ANY_HASH
1021 * when specifying the algorithm in a usage policy.
1022 *
1023 * \return The corresponding ECDSA signature algorithm.
1024 * \return Unspecified if \p alg is not a supported
1025 * hash algorithm.
1026 */
1027#define PSA_ALG_ECDSA(hash_alg) \
1028 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1029/** ECDSA signature without hashing.
1030 *
1031 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1032 * without specifying a hash algorithm. This algorithm may only be
1033 * used to sign or verify a sequence of bytes that should be an
1034 * already-calculated hash. Note that the input is padded with
1035 * zeros on the left or truncated on the left as required to fit
1036 * the curve size.
1037 */
1038#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1039#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1040/** Deterministic ECDSA signature with hashing.
1041 *
1042 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1043 *
1044 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1045 *
1046 * Note that when this algorithm is used for verification, signatures
1047 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1048 * same private key are accepted. In other words,
1049 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1050 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1051 *
1052 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1053 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1054 * This includes #PSA_ALG_ANY_HASH
1055 * when specifying the algorithm in a usage policy.
1056 *
1057 * \return The corresponding deterministic ECDSA signature
1058 * algorithm.
1059 * \return Unspecified if \p alg is not a supported
1060 * hash algorithm.
1061 */
1062#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1063 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1064#define PSA_ALG_IS_ECDSA(alg) \
1065 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1066 PSA_ALG_ECDSA_BASE)
1067#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1068 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
1069#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1070 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1071#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1072 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1073
1074/** Whether the specified algorithm is a hash-and-sign algorithm.
1075 *
1076 * Hash-and-sign algorithms are public-key signature algorithms structured
1077 * in two parts: first the calculation of a hash in a way that does not
1078 * depend on the key, then the calculation of a signature from the
1079 * hash value and the key.
1080 *
1081 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1082 *
1083 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1084 * This macro may return either 0 or 1 if \p alg is not a supported
1085 * algorithm identifier.
1086 */
1087#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1088 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1089 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
1090
1091/** Get the hash used by a hash-and-sign signature algorithm.
1092 *
1093 * A hash-and-sign algorithm is a signature algorithm which is
1094 * composed of two phases: first a hashing phase which does not use
1095 * the key and produces a hash of the input message, then a signing
1096 * phase which only uses the hash and the key and not the message
1097 * itself.
1098 *
1099 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1100 * #PSA_ALG_IS_SIGN(\p alg) is true).
1101 *
1102 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1103 * algorithm.
1104 * \return 0 if \p alg is a signature algorithm that does not
1105 * follow the hash-and-sign structure.
1106 * \return Unspecified if \p alg is not a signature algorithm or
1107 * if it is not supported by the implementation.
1108 */
1109#define PSA_ALG_SIGN_GET_HASH(alg) \
1110 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1111 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1112 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1113 0)
1114
1115/** RSA PKCS#1 v1.5 encryption.
1116 */
1117#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
1118
1119#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
1120/** RSA OAEP encryption.
1121 *
1122 * This is the encryption scheme defined by RFC 8017
1123 * (PKCS#1: RSA Cryptography Specifications) under the name
1124 * RSAES-OAEP, with the message generation function MGF1.
1125 *
1126 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1127 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1128 * for MGF1.
1129 *
1130 * \return The corresponding RSA OAEP signature algorithm.
1131 * \return Unspecified if \p alg is not a supported
1132 * hash algorithm.
1133 */
1134#define PSA_ALG_RSA_OAEP(hash_alg) \
1135 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1136#define PSA_ALG_IS_RSA_OAEP(alg) \
1137 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1138#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1139 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1140 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1141 0)
1142
1143#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1144/** Macro to build an HKDF algorithm.
1145 *
1146 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1147 *
1148 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1149 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1150 *
1151 * \return The corresponding HKDF algorithm.
1152 * \return Unspecified if \p alg is not a supported
1153 * hash algorithm.
1154 */
1155#define PSA_ALG_HKDF(hash_alg) \
1156 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1157/** Whether the specified algorithm is an HKDF algorithm.
1158 *
1159 * HKDF is a family of key derivation algorithms that are based on a hash
1160 * function and the HMAC construction.
1161 *
1162 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1163 *
1164 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1165 * This macro may return either 0 or 1 if \c alg is not a supported
1166 * key derivation algorithm identifier.
1167 */
1168#define PSA_ALG_IS_HKDF(alg) \
1169 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1170#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1171 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1172
1173#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x30000200)
1174/** Macro to build a TLS-1.2 PRF algorithm.
1175 *
1176 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1177 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1178 * used with either SHA-256 or SHA-384.
1179 *
1180 * For the application to TLS-1.2, the salt and label arguments passed
1181 * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246,
1182 * respectively. For example, for TLS key expansion, the salt is the
1183 * concatenation of ServerHello.Random + ClientHello.Random,
1184 * while the label is "key expansion".
1185 *
1186 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1187 * TLS 1.2 PRF using HMAC-SHA-256.
1188 *
1189 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1190 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1191 *
1192 * \return The corresponding TLS-1.2 PRF algorithm.
1193 * \return Unspecified if \p alg is not a supported
1194 * hash algorithm.
1195 */
1196#define PSA_ALG_TLS12_PRF(hash_alg) \
1197 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1198
1199/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1200 *
1201 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1202 *
1203 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1204 * This macro may return either 0 or 1 if \c alg is not a supported
1205 * key derivation algorithm identifier.
1206 */
1207#define PSA_ALG_IS_TLS12_PRF(alg) \
1208 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1209#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1210 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1211
1212#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x30000300)
1213/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1214 *
1215 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1216 * from the PreSharedKey (PSK) through the application of padding
1217 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1218 * The latter is based on HMAC and can be used with either SHA-256
1219 * or SHA-384.
1220 *
1221 * For the application to TLS-1.2, the salt passed to psa_key_derivation()
1222 * (and forwarded to the TLS-1.2 PRF) is the concatenation of the
1223 * ClientHello.Random + ServerHello.Random, while the label is "master secret"
1224 * or "extended master secret".
1225 *
1226 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1227 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1228 *
1229 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1230 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1231 *
1232 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1233 * \return Unspecified if \p alg is not a supported
1234 * hash algorithm.
1235 */
1236#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1237 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1238
1239/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1240 *
1241 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1242 *
1243 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1244 * This macro may return either 0 or 1 if \c alg is not a supported
1245 * key derivation algorithm identifier.
1246 */
1247#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1248 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1249#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1250 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1251
1252#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x010fffff)
1253
1254/** Use a shared secret as is.
1255 *
1256 * Specify this algorithm as the selection component of a key agreement
1257 * to use the raw result of the key agreement as key material.
1258 *
1259 * \warning The raw result of a key agreement algorithm such as finite-field
1260 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
1261 * not be used directly as key material. It can however be used as the secret
1262 * input in a key derivation algorithm.
1263 */
1264#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
1265
1266#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1267 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1268
1269#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1270 ((alg) & ~PSA_ALG_KEY_DERIVATION_MASK)
1271
1272#define PSA_ALG_FFDH_BASE ((psa_algorithm_t)0x22100000)
1273/** The Diffie-Hellman key agreement algorithm.
1274 *
1275 * This algorithm combines the finite-field Diffie-Hellman (DH) key
1276 * agreement, also known as Diffie-Hellman-Merkle (DHM) key agreement,
1277 * to produce a shared secret from a private key and the peer's
1278 * public key, with a key selection or key derivation algorithm to produce
1279 * one or more shared keys and other shared cryptographic material.
1280 *
1281 * The shared secret produced by key agreement and passed as input to the
1282 * derivation or selection algorithm \p kdf_alg is the shared secret
1283 * `g^{ab}` in big-endian format.
1284 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1285 * in bits.
1286 *
1287 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1288 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1289 * or a key selection algorithm (\c PSA_ALG_XXX value such
1290 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1291 *
1292 * \return The Diffie-Hellman algorithm with the specified
1293 * selection or derivation algorithm.
1294 */
1295#define PSA_ALG_FFDH(kdf_alg) \
1296 (PSA_ALG_FFDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1297/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1298 *
1299 * This includes every supported key selection or key agreement algorithm
1300 * for the output of the Diffie-Hellman calculation.
1301 *
1302 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1303 *
1304 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1305 * This macro may return either 0 or 1 if \c alg is not a supported
1306 * key agreement algorithm identifier.
1307 */
1308#define PSA_ALG_IS_FFDH(alg) \
1309 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH_BASE)
1310
1311#define PSA_ALG_ECDH_BASE ((psa_algorithm_t)0x22200000)
1312/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1313 *
1314 * This algorithm combines the elliptic curve Diffie-Hellman key
1315 * agreement to produce a shared secret from a private key and the peer's
1316 * public key, with a key selection or key derivation algorithm to produce
1317 * one or more shared keys and other shared cryptographic material.
1318 *
1319 * The shared secret produced by key agreement and passed as input to the
1320 * derivation or selection algorithm \p kdf_alg is the x-coordinate of
1321 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1322 * `m` is the bit size associated with the curve, i.e. the bit size of the
1323 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1324 * the byte containing the most significant bit of the shared secret
1325 * is padded with zero bits. The byte order is either little-endian
1326 * or big-endian depending on the curve type.
1327 *
1328 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1329 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1330 * in little-endian byte order.
1331 * The bit size is 448 for Curve448 and 255 for Curve25519.
1332 * - For Weierstrass curves over prime fields (curve types
1333 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1334 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1335 * in big-endian byte order.
1336 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1337 * - For Weierstrass curves over binary fields (curve types
1338 * `PSA_ECC_CURVE_SECTXXX`),
1339 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1340 * in big-endian byte order.
1341 * The bit size is `m` for the field `F_{2^m}`.
1342 *
1343 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1344 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1345 * or a selection algorithm (\c PSA_ALG_XXX value such
1346 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1347 *
1348 * \return The Diffie-Hellman algorithm with the specified
1349 * selection or derivation algorithm.
1350 */
1351#define PSA_ALG_ECDH(kdf_alg) \
1352 (PSA_ALG_ECDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1353/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1354 * algorithm.
1355 *
1356 * This includes every supported key selection or key agreement algorithm
1357 * for the output of the Diffie-Hellman calculation.
1358 *
1359 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1360 *
1361 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1362 * 0 otherwise.
1363 * This macro may return either 0 or 1 if \c alg is not a supported
1364 * key agreement algorithm identifier.
1365 */
1366#define PSA_ALG_IS_ECDH(alg) \
1367 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH_BASE)
1368
1369/** Whether the specified algorithm encoding is a wildcard.
1370 *
1371 * Wildcard values may only be used to set the usage algorithm field in
1372 * a policy, not to perform an operation.
1373 *
1374 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1375 *
1376 * \return 1 if \c alg is a wildcard algorithm encoding.
1377 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1378 * an operation).
1379 * \return This macro may return either 0 or 1 if \c alg is not a supported
1380 * algorithm identifier.
1381 */
1382#define PSA_ALG_IS_WILDCARD(alg) \
1383 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1384 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1385 (alg) == PSA_ALG_ANY_HASH)
1386
1387/**@}*/
1388
1389/** \defgroup key_lifetimes Key lifetimes
1390 * @{
1391 */
1392
1393/** A volatile key only exists as long as the handle to it is not closed.
1394 * The key material is guaranteed to be erased on a power reset.
1395 */
1396#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1397
1398/** The default storage area for persistent keys.
1399 *
1400 * A persistent key remains in storage until it is explicitly destroyed or
1401 * until the corresponding storage area is wiped. This specification does
1402 * not define any mechanism to wipe a storage area, but implementations may
1403 * provide their own mechanism (for example to perform a factory reset,
1404 * to prepare for device refurbishment, or to uninstall an application).
1405 *
1406 * This lifetime value is the default storage area for the calling
1407 * application. Implementations may offer other storage areas designated
1408 * by other lifetime values as implementation-specific extensions.
1409 */
1410#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1411
1412/**@}*/
1413
1414/** \defgroup policy Key policies
1415 * @{
1416 */
1417
1418/** Whether the key may be exported.
1419 *
1420 * A public key or the public part of a key pair may always be exported
1421 * regardless of the value of this permission flag.
1422 *
1423 * If a key does not have export permission, implementations shall not
1424 * allow the key to be exported in plain form from the cryptoprocessor,
1425 * whether through psa_export_key() or through a proprietary interface.
1426 * The key may however be exportable in a wrapped form, i.e. in a form
1427 * where it is encrypted by another key.
1428 */
1429#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1430
1431/** Whether the key may be used to encrypt a message.
1432 *
1433 * This flag allows the key to be used for a symmetric encryption operation,
1434 * for an AEAD encryption-and-authentication operation,
1435 * or for an asymmetric encryption operation,
1436 * if otherwise permitted by the key's type and policy.
1437 *
1438 * For a key pair, this concerns the public key.
1439 */
1440#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1441
1442/** Whether the key may be used to decrypt a message.
1443 *
1444 * This flag allows the key to be used for a symmetric decryption operation,
1445 * for an AEAD decryption-and-verification operation,
1446 * or for an asymmetric decryption operation,
1447 * if otherwise permitted by the key's type and policy.
1448 *
1449 * For a key pair, this concerns the private key.
1450 */
1451#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1452
1453/** Whether the key may be used to sign a message.
1454 *
1455 * This flag allows the key to be used for a MAC calculation operation
1456 * or for an asymmetric signature operation,
1457 * if otherwise permitted by the key's type and policy.
1458 *
1459 * For a key pair, this concerns the private key.
1460 */
1461#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
1462
1463/** Whether the key may be used to verify a message signature.
1464 *
1465 * This flag allows the key to be used for a MAC verification operation
1466 * or for an asymmetric signature verification operation,
1467 * if otherwise permitted by by the key's type and policy.
1468 *
1469 * For a key pair, this concerns the public key.
1470 */
1471#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1472
1473/** Whether the key may be used to derive other keys.
1474 */
1475#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1476
1477/**@}*/
1478
1479#endif /* PSA_CRYPTO_VALUES_H */