blob: 8eba1d4d00205a5ff47f71b8d3ec0c0fedc8e303 [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
Summer Qin614002c2023-01-19 15:22:39 +08002 * Copyright (c) 2018-2023, Arm Limited. All rights reserved.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003 *
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 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010021 * Note that many of the constants defined in this file are embedded in
22 * the persistent key store, as part of key metadata (including usage
23 * policies). As a consequence, they must not be changed (unless the storage
24 * format version changes).
25 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010026 * This header file only defines preprocessor macros.
27 */
28
29#ifndef PSA_CRYPTO_VALUES_H
30#define PSA_CRYPTO_VALUES_H
31
32/** \defgroup error Error codes
33 * @{
34 */
35
36/* PSA error codes */
37
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010038/* Error codes are standardized across PSA domains (framework, crypto, storage,
39 * etc.). Do not change the values in this section or even the expansions
40 * of each macro: it must be possible to `#include` both this header
41 * and some other PSA component's headers in the same C source,
42 * which will lead to duplicate definitions of the `PSA_SUCCESS` and
43 * `PSA_ERROR_xxx` macros, which is ok if and only if the macros expand
44 * to the same sequence of tokens.
45 *
46 * If you must add a new
47 * value, check with the Arm PSA framework group to pick one that other
48 * domains aren't already using. */
49
Antonio de Angelis34a0ffd2023-02-16 11:56:46 +000050/* Tell uncrustify not to touch the constant definitions, otherwise
51 * it might change the spacing to something that is not PSA-compliant
52 * (e.g. adding a space after casts).
53 *
54 * *INDENT-OFF*
55 */
56
Jamie Fox0e54ebc2019-04-09 14:21:04 +010057/** The action was completed successfully. */
58#ifndef PSA_SUCCESS
59#define PSA_SUCCESS ((psa_status_t)0)
60#endif
61
62/** An error occurred that does not correspond to any defined
63 * failure cause.
64 *
65 * Implementations may use this error code if none of the other standard
66 * error codes are applicable. */
67#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
68
69/** The requested operation or a parameter is not supported
70 * by this implementation.
71 *
72 * Implementations should return this error code when an enumeration
73 * parameter such as a key type, algorithm, etc. is not recognized.
74 * If a combination of parameters is recognized and identified as
75 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
76#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
77
78/** The requested action is denied by a policy.
79 *
80 * Implementations should return this error code when the parameters
81 * are recognized as valid and supported, and a policy explicitly
82 * denies the requested operation.
83 *
84 * If a subset of the parameters of a function call identify a
85 * forbidden operation, and another subset of the parameters are
86 * not valid or not supported, it is unspecified whether the function
87 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
88 * #PSA_ERROR_INVALID_ARGUMENT. */
89#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
90
91/** An output buffer is too small.
92 *
93 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
94 * description to determine a sufficient buffer size.
95 *
96 * Implementations should preferably return this error code only
97 * in cases when performing the operation with a larger output
98 * buffer would succeed. However implementations may return this
99 * error if a function has invalid or unsupported parameters in addition
100 * to the parameters that determine the necessary output buffer size. */
101#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
102
103/** Asking for an item that already exists
104 *
105 * Implementations should return this error, when attempting
106 * to write an item (like a key) that already exists. */
107#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
108
109/** Asking for an item that doesn't exist
110 *
111 * Implementations should return this error, if a requested item (like
112 * a key) does not exist. */
113#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
114
115/** The requested action cannot be performed in the current state.
116 *
117 * Multipart operations return this error when one of the
118 * functions is called out of sequence. Refer to the function
119 * descriptions for permitted sequencing of functions.
120 *
121 * Implementations shall not return this error code to indicate
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100122 * that a key either exists or not,
123 * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
124 * as applicable.
125 *
126 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000127 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100128 * instead. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100129#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
130
131/** The parameters passed to the function are invalid.
132 *
133 * Implementations may return this error any time a parameter or
134 * combination of parameters are recognized as invalid.
135 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100136 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000137 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100138 * instead.
139 */
140#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
141
142/** There is not enough runtime memory.
143 *
144 * If the action is carried out across multiple security realms, this
145 * error can refer to available memory in any of the security realms. */
146#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
147
148/** There is not enough persistent storage.
149 *
150 * Functions that modify the key storage return this error code if
151 * there is insufficient storage space on the host media. In addition,
152 * many functions that do not otherwise access storage may return this
153 * error code if the implementation requires a mandatory log entry for
154 * the requested action and the log storage space is full. */
155#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
156
157/** There was a communication failure inside the implementation.
158 *
159 * This can indicate a communication failure between the application
160 * and an external cryptoprocessor or between the cryptoprocessor and
161 * an external volatile or persistent memory. A communication failure
162 * may be transient or permanent depending on the cause.
163 *
164 * \warning If a function returns this error, it is undetermined
165 * whether the requested action has completed or not. Implementations
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100166 * should return #PSA_SUCCESS on successful completion whenever
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100167 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
168 * if the requested action was completed successfully in an external
169 * cryptoprocessor but there was a breakdown of communication before
170 * the cryptoprocessor could report the status to the application.
171 */
172#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
173
174/** There was a storage failure that may have led to data loss.
175 *
176 * This error indicates that some persistent storage is corrupted.
177 * It should not be used for a corruption of volatile memory
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100178 * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100179 * between the cryptoprocessor and its external storage (use
180 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
181 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
182 *
183 * Note that a storage failure does not indicate that any data that was
184 * previously read is invalid. However this previously read data may no
185 * longer be readable from storage.
186 *
187 * When a storage failure occurs, it is no longer possible to ensure
188 * the global integrity of the keystore. Depending on the global
189 * integrity guarantees offered by the implementation, access to other
190 * data may or may not fail even if the data is still readable but
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100191 * its integrity cannot be guaranteed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100192 *
193 * Implementations should only use this error code to report a
194 * permanent storage corruption. However application writers should
195 * keep in mind that transient errors while reading the storage may be
196 * reported using this error code. */
197#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
198
199/** A hardware failure was detected.
200 *
201 * A hardware failure may be transient or permanent depending on the
202 * cause. */
203#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
204
205/** A tampering attempt was detected.
206 *
207 * If an application receives this error code, there is no guarantee
208 * that previously accessed or computed data was correct and remains
209 * confidential. Applications should not perform any security function
210 * and should enter a safe failure state.
211 *
212 * Implementations may return this error code if they detect an invalid
213 * state that cannot happen during normal operation and that indicates
214 * that the implementation's security guarantees no longer hold. Depending
215 * on the implementation architecture and on its security and safety goals,
216 * the implementation may forcibly terminate the application.
217 *
218 * This error code is intended as a last resort when a security breach
219 * is detected and it is unsure whether the keystore data is still
220 * protected. Implementations shall only return this error code
221 * to report an alarm from a tampering detector, to indicate that
222 * the confidentiality of stored data can no longer be guaranteed,
223 * or to indicate that the integrity of previously returned data is now
224 * considered compromised. Implementations shall not use this error code
225 * to indicate a hardware failure that merely makes it impossible to
226 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
227 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
228 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
229 * instead).
230 *
231 * This error indicates an attack against the application. Implementations
232 * shall not return this error code as a consequence of the behavior of
233 * the application itself. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100234#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100235
236/** There is not enough entropy to generate random data needed
237 * for the requested action.
238 *
239 * This error indicates a failure of a hardware random generator.
240 * Application writers should note that this error can be returned not
241 * only by functions whose purpose is to generate random data, such
242 * as key, IV or nonce generation, but also by functions that execute
243 * an algorithm with a randomized result, as well as functions that
244 * use randomization of intermediate computations as a countermeasure
245 * to certain attacks.
246 *
247 * Implementations should avoid returning this error after psa_crypto_init()
248 * has succeeded. Implementations should generate sufficient
249 * entropy during initialization and subsequently use a cryptographically
250 * secure pseudorandom generator (PRNG). However implementations may return
251 * this error at any time if a policy requires the PRNG to be reseeded
252 * during normal operation. */
253#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
254
255/** The signature, MAC or hash is incorrect.
256 *
257 * Verification functions return this error if the verification
258 * calculations completed successfully, and the value to be verified
259 * was determined to be incorrect.
260 *
261 * If the value to verify has an invalid size, implementations may return
262 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
263#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
264
265/** The decrypted padding is incorrect.
266 *
267 * \warning In some protocols, when decrypting data, it is essential that
268 * the behavior of the application does not depend on whether the padding
269 * is correct, down to precise timing. Applications should prefer
270 * protocols that use authenticated encryption rather than plain
271 * encryption. If the application must perform a decryption of
272 * unauthenticated data, the application writer should take care not
273 * to reveal whether the padding is invalid.
274 *
275 * Implementations should strive to make valid and invalid padding
276 * as close as possible to indistinguishable to an external observer.
277 * In particular, the timing of a decryption operation should not
278 * depend on the validity of the padding. */
279#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
280
281/** Return this error when there's insufficient data when attempting
282 * to read from a resource. */
283#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
284
Maulik Patel28659c42021-01-06 14:09:22 +0000285/** The key identifier is not valid. See also :ref:\`key-handles\`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100286 */
287#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
288
Maulik Patel13b27cf2021-05-14 11:44:53 +0100289/** Stored data has been corrupted.
290 *
291 * This error indicates that some persistent storage has suffered corruption.
292 * It does not indicate the following situations, which have specific error
293 * codes:
294 *
295 * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
296 * - A communication error between the cryptoprocessor and its external
297 * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
298 * - When the storage is in a valid state but is full - use
299 * #PSA_ERROR_INSUFFICIENT_STORAGE.
300 * - When the storage fails for other reasons - use
301 * #PSA_ERROR_STORAGE_FAILURE.
302 * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
303 *
304 * \note A storage corruption does not indicate that any data that was
305 * previously read is invalid. However this previously read data might no
306 * longer be readable from storage.
307 *
308 * When a storage failure occurs, it is no longer possible to ensure the
309 * global integrity of the keystore.
310 */
311#define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
312
313/** Data read from storage is not valid for the implementation.
314 *
315 * This error indicates that some data read from storage does not have a valid
316 * format. It does not indicate the following situations, which have specific
317 * error codes:
318 *
319 * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
320 * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
321 * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
322 *
323 * This error is typically a result of either storage corruption on a
324 * cleartext storage backend, or an attempt to read data that was
325 * written by an incompatible version of the library.
326 */
327#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
328
Antonio de Angelis34a0ffd2023-02-16 11:56:46 +0000329/** The function that returns this status is defined as interruptible and
330 * still has work to do, thus the user should call the function again with the
331 * same operation context until it either returns #PSA_SUCCESS or any other
332 * error. This is not an error per se, more a notification of status.
333 */
334#define PSA_OPERATION_INCOMPLETE ((psa_status_t)-248)
335
336/* *INDENT-ON* */
337
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100338/**@}*/
339
340/** \defgroup crypto_types Key and algorithm types
341 * @{
342 */
343
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100344/* Note that key type values, including ECC family and DH group values, are
345 * embedded in the persistent key store, as part of key metadata. As a
346 * consequence, they must not be changed (unless the storage format version
347 * changes).
348 */
349
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100350/** An invalid key type value.
351 *
352 * Zero is not the encoding of any key type.
353 */
Summer Qind635cd02023-03-31 18:07:38 +0800354#define PSA_KEY_TYPE_NONE ((psa_key_type_t) 0x0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100355
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100356/** Vendor-defined key type flag.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100357 *
358 * Key types defined by this standard will never have the
359 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
360 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
361 * respect the bitwise structure used by standard encodings whenever practical.
362 */
Summer Qind635cd02023-03-31 18:07:38 +0800363#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t) 0x8000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100364
Summer Qind635cd02023-03-31 18:07:38 +0800365#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t) 0x7000)
366#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t) 0x1000)
367#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t) 0x2000)
368#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t) 0x4000)
369#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t) 0x7000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100370
Summer Qind635cd02023-03-31 18:07:38 +0800371#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t) 0x3000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100372
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100373/** Whether a key type is vendor-defined.
374 *
375 * See also #PSA_KEY_TYPE_VENDOR_FLAG.
376 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100377#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
378 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
379
380/** Whether a key type is an unstructured array of bytes.
381 *
382 * This encompasses both symmetric keys and non-key data.
383 */
384#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100385 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
386 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100387
388/** Whether a key type is asymmetric: either a key pair or a public key. */
389#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
390 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
391 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
392 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
393/** Whether a key type is the public part of a key pair. */
394#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
395 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
396/** Whether a key type is a key pair containing a private part and a public
397 * part. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100398#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100399 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
400/** The key pair type corresponding to a public key type.
401 *
402 * You may also pass a key pair type as \p type, it will be left unchanged.
403 *
404 * \param type A public key type or key pair type.
405 *
406 * \return The corresponding key pair type.
407 * If \p type is not a public key or a key pair,
408 * the return value is undefined.
409 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100410#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100411 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
412/** The public key type corresponding to a key pair type.
413 *
414 * You may also pass a key pair type as \p type, it will be left unchanged.
415 *
416 * \param type A public key type or key pair type.
417 *
418 * \return The corresponding public key type.
419 * If \p type is not a public key or a key pair,
420 * the return value is undefined.
421 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100422#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100423 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
424
425/** Raw data.
426 *
427 * A "key" of this type cannot be used for any cryptographic operation.
428 * Applications may use this type to store arbitrary data in the keystore. */
Summer Qind635cd02023-03-31 18:07:38 +0800429#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t) 0x1001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100430
431/** HMAC key.
432 *
433 * The key policy determines which underlying hash algorithm the key can be
434 * used for.
435 *
436 * HMAC keys should generally have the same size as the underlying hash.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100437 * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100438 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Summer Qind635cd02023-03-31 18:07:38 +0800439#define PSA_KEY_TYPE_HMAC ((psa_key_type_t) 0x1100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100440
441/** A secret for key derivation.
442 *
Summer Qin359167d2021-07-05 18:11:50 +0800443 * This key type is for high-entropy secrets only. For low-entropy secrets,
444 * #PSA_KEY_TYPE_PASSWORD should be used instead.
445 *
446 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
447 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
448 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100449 * The key policy determines which key derivation algorithm the key
450 * can be used for.
451 */
Summer Qind635cd02023-03-31 18:07:38 +0800452#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t) 0x1200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100453
Summer Qin359167d2021-07-05 18:11:50 +0800454/** A low-entropy secret for password hashing or key derivation.
455 *
456 * This key type is suitable for passwords and passphrases which are typically
457 * intended to be memorizable by humans, and have a low entropy relative to
458 * their size. It can be used for randomly generated or derived keys with
459 * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
460 * for such keys. It is not suitable for passwords with extremely low entropy,
461 * such as numerical PINs.
462 *
463 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
464 * key derivation algorithms. Algorithms that accept such an input were
465 * designed to accept low-entropy secret and are known as password hashing or
466 * key stretching algorithms.
467 *
468 * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
469 * key derivation algorithms, as the algorithms that take such an input expect
470 * it to be high-entropy.
471 *
472 * The key policy determines which key derivation algorithm the key can be
473 * used for, among the permissible subset defined above.
474 */
Summer Qind635cd02023-03-31 18:07:38 +0800475#define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t) 0x1203)
Summer Qin359167d2021-07-05 18:11:50 +0800476
477/** A secret value that can be used to verify a password hash.
478 *
479 * The key policy determines which key derivation algorithm the key
480 * can be used for, among the same permissible subset as for
481 * #PSA_KEY_TYPE_PASSWORD.
482 */
Summer Qind635cd02023-03-31 18:07:38 +0800483#define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t) 0x1205)
Summer Qin359167d2021-07-05 18:11:50 +0800484
485/** A secret value that can be used in when computing a password hash.
486 *
487 * The key policy determines which key derivation algorithm the key
488 * can be used for, among the subset of algorithms that can use pepper.
489 */
Summer Qind635cd02023-03-31 18:07:38 +0800490#define PSA_KEY_TYPE_PEPPER ((psa_key_type_t) 0x1206)
Summer Qin359167d2021-07-05 18:11:50 +0800491
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100492/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100493 *
494 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
495 * 32 bytes (AES-256).
496 */
Summer Qind635cd02023-03-31 18:07:38 +0800497#define PSA_KEY_TYPE_AES ((psa_key_type_t) 0x2400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100498
Summer Qinf07cc312022-01-05 16:52:54 +0800499/** Key for a cipher, AEAD or MAC algorithm based on the
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100500 * ARIA block cipher. */
Summer Qind635cd02023-03-31 18:07:38 +0800501#define PSA_KEY_TYPE_ARIA ((psa_key_type_t) 0x2406)
Summer Qinf07cc312022-01-05 16:52:54 +0800502
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100503/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
504 *
Summer Qin359167d2021-07-05 18:11:50 +0800505 * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
506 * 192 bits (3-key 3DES).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100507 *
508 * Note that single DES and 2-key 3DES are weak and strongly
509 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
510 * is weak and deprecated and should only be used in legacy protocols.
511 */
Summer Qind635cd02023-03-31 18:07:38 +0800512#define PSA_KEY_TYPE_DES ((psa_key_type_t) 0x2301)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100513
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100514/** Key for a cipher, AEAD or MAC algorithm based on the
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100515 * Camellia block cipher. */
Summer Qind635cd02023-03-31 18:07:38 +0800516#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t) 0x2403)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100517
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100518/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
519 *
520 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
521 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100522 * \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports
523 * 12-byte nonces.
524 *
525 * \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt
526 * with the initial counter value 1, you can process and discard a
527 * 64-byte block before the real data.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100528 */
Summer Qind635cd02023-03-31 18:07:38 +0800529#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t) 0x2004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100530
Summer Qin359167d2021-07-05 18:11:50 +0800531/** RSA public key.
532 *
533 * The size of an RSA key is the bit size of the modulus.
534 */
Summer Qind635cd02023-03-31 18:07:38 +0800535#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t) 0x4001)
Summer Qin359167d2021-07-05 18:11:50 +0800536/** RSA key pair (private and public key).
537 *
538 * The size of an RSA key is the bit size of the modulus.
539 */
Summer Qind635cd02023-03-31 18:07:38 +0800540#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t) 0x7001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100541/** Whether a key type is an RSA key (pair or public-only). */
542#define PSA_KEY_TYPE_IS_RSA(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100543 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100544
Summer Qind635cd02023-03-31 18:07:38 +0800545#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4100)
546#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t) 0x7100)
547#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t) 0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100548/** Elliptic curve key pair.
549 *
Summer Qin359167d2021-07-05 18:11:50 +0800550 * The size of an elliptic curve key is the bit size associated with the curve,
551 * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
552 * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
553 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800554 * \param curve A value of type ::psa_ecc_family_t that
555 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100556 */
557#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
558 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
559/** Elliptic curve public key.
560 *
Summer Qin359167d2021-07-05 18:11:50 +0800561 * The size of an elliptic curve public key is the same as the corresponding
562 * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
563 * `PSA_ECC_FAMILY_xxx` curve families).
564 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800565 * \param curve A value of type ::psa_ecc_family_t that
566 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100567 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100568#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
569 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
570
571/** Whether a key type is an elliptic curve key (pair or public-only). */
572#define PSA_KEY_TYPE_IS_ECC(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100573 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100574 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
575/** Whether a key type is an elliptic curve key pair. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100576#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100577 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100578 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100579/** Whether a key type is an elliptic curve public key. */
580#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
581 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
582 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
583
584/** Extract the curve from an elliptic curve key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800585#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
586 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
Summer Qind635cd02023-03-31 18:07:38 +0800587 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
588 0))
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100589
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100590/** Check if the curve of given family is Weierstrass elliptic curve. */
591#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
592
Soby Mathew07ef6e42020-07-20 21:09:23 +0100593/** SEC Koblitz curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100594 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100595 * This family comprises the following curves:
596 * secp192k1, secp224k1, secp256k1.
597 * They are defined in _Standards for Efficient Cryptography_,
598 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
599 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100600 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800601#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100602
Soby Mathew07ef6e42020-07-20 21:09:23 +0100603/** SEC random curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100604 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100605 * This family comprises the following curves:
606 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
607 * They are defined in _Standards for Efficient Cryptography_,
608 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
609 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100610 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800611#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100612/* SECP160R2 (SEC2 v1, obsolete) */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800613#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100614
Soby Mathew07ef6e42020-07-20 21:09:23 +0100615/** SEC Koblitz curves over binary fields.
616 *
617 * This family comprises the following curves:
618 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
619 * They are defined in _Standards for Efficient Cryptography_,
620 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
621 * https://www.secg.org/sec2-v2.pdf
622 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800623#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100624
625/** SEC random curves over binary fields.
626 *
627 * This family comprises the following curves:
628 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
629 * They are defined in _Standards for Efficient Cryptography_,
630 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
631 * https://www.secg.org/sec2-v2.pdf
632 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800633#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100634
635/** SEC additional random curves over binary fields.
636 *
637 * This family comprises the following curve:
638 * sect163r2.
639 * It is defined in _Standards for Efficient Cryptography_,
640 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
641 * https://www.secg.org/sec2-v2.pdf
642 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800643#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100644
645/** Brainpool P random curves.
646 *
647 * This family comprises the following curves:
648 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
649 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
650 * It is defined in RFC 5639.
651 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800652#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100653
654/** Curve25519 and Curve448.
655 *
656 * This family comprises the following Montgomery curves:
657 * - 255-bit: Bernstein et al.,
658 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
659 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
660 * - 448-bit: Hamburg,
661 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
662 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
663 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800664#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100665
Summer Qin359167d2021-07-05 18:11:50 +0800666/** The twisted Edwards curves Ed25519 and Ed448.
667 *
668 * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
669 * #PSA_ALG_ED25519PH for the 255-bit curve,
670 * #PSA_ALG_ED448PH for the 448-bit curve).
671 *
672 * This family comprises the following twisted Edwards curves:
673 * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
674 * to Curve25519.
675 * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
676 * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
677 * to Curve448.
678 * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
679 */
680#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
681
Summer Qind635cd02023-03-31 18:07:38 +0800682#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4200)
683#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t) 0x7200)
684#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t) 0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100685/** Diffie-Hellman key pair.
686 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800687 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100688 * Diffie-Hellman group to be used.
689 */
690#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
691 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
692/** Diffie-Hellman public key.
693 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800694 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100695 * Diffie-Hellman group to be used.
696 */
697#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
698 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
699
700/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
701#define PSA_KEY_TYPE_IS_DH(type) \
702 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
703 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
704/** Whether a key type is a Diffie-Hellman key pair. */
705#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
706 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
707 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
708/** Whether a key type is a Diffie-Hellman public key. */
709#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
710 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
711 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
712
713/** Extract the group from a Diffie-Hellman key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800714#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
715 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
Summer Qind635cd02023-03-31 18:07:38 +0800716 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
717 0))
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100718
Soby Mathew07ef6e42020-07-20 21:09:23 +0100719/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100720 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100721 * This family includes groups with the following key sizes (in bits):
722 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
723 * all of these sizes or only a subset.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100724 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800725#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100726
Soby Mathew07ef6e42020-07-20 21:09:23 +0100727#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
728 (((type) >> 8) & 7)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100729/** The block size of a block cipher.
730 *
731 * \param type A cipher key type (value of type #psa_key_type_t).
732 *
733 * \return The block size for a block cipher, or 1 for a stream cipher.
734 * The return value is undefined if \p type is not a supported
735 * cipher key type.
736 *
737 * \note It is possible to build stream cipher algorithms on top of a block
738 * cipher, for example CTR mode (#PSA_ALG_CTR).
739 * This macro only takes the key type into account, so it cannot be
740 * used to determine the size of the data that #psa_cipher_update()
741 * might buffer for future processing in general.
742 *
743 * \note This macro returns a compile-time constant if its argument is one.
744 *
745 * \warning This macro may evaluate its argument multiple times.
746 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100747#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100748 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100749 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
Summer Qind635cd02023-03-31 18:07:38 +0800750 0u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100751
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100752/* Note that algorithm values are embedded in the persistent key store,
753 * as part of key metadata. As a consequence, they must not be changed
754 * (unless the storage format version changes).
755 */
756
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100757/** Vendor-defined algorithm flag.
758 *
759 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
760 * bit set. Vendors who define additional algorithms must use an encoding with
761 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
762 * used by standard encodings whenever practical.
763 */
Summer Qind635cd02023-03-31 18:07:38 +0800764#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t) 0x80000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100765
Summer Qind635cd02023-03-31 18:07:38 +0800766#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t) 0x7f000000)
767#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t) 0x02000000)
768#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t) 0x03000000)
769#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t) 0x04000000)
770#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t) 0x05000000)
771#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t) 0x06000000)
772#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t) 0x07000000)
773#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t) 0x08000000)
774#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t) 0x09000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100775
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100776/** Whether an algorithm is vendor-defined.
777 *
778 * See also #PSA_ALG_VENDOR_FLAG.
779 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100780#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
781 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
782
783/** Whether the specified algorithm is a hash algorithm.
784 *
785 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
786 *
787 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
788 * This macro may return either 0 or 1 if \p alg is not a supported
789 * algorithm identifier.
790 */
791#define PSA_ALG_IS_HASH(alg) \
792 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
793
794/** Whether the specified algorithm is a MAC algorithm.
795 *
796 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
797 *
798 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
799 * This macro may return either 0 or 1 if \p alg is not a supported
800 * algorithm identifier.
801 */
802#define PSA_ALG_IS_MAC(alg) \
803 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
804
805/** Whether the specified algorithm is a symmetric cipher algorithm.
806 *
807 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
808 *
809 * \return 1 if \p alg is a symmetric cipher 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_CIPHER(alg) \
814 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
815
816/** Whether the specified algorithm is an authenticated encryption
817 * with associated data (AEAD) algorithm.
818 *
819 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
820 *
821 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
822 * This macro may return either 0 or 1 if \p alg is not a supported
823 * algorithm identifier.
824 */
825#define PSA_ALG_IS_AEAD(alg) \
826 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
827
Soby Mathew07ef6e42020-07-20 21:09:23 +0100828/** Whether the specified algorithm is an asymmetric signature algorithm,
829 * also known as public-key signature algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100830 *
831 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
832 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100833 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100834 * This macro may return either 0 or 1 if \p alg is not a supported
835 * algorithm identifier.
836 */
837#define PSA_ALG_IS_SIGN(alg) \
838 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
839
Soby Mathew07ef6e42020-07-20 21:09:23 +0100840/** Whether the specified algorithm is an asymmetric encryption algorithm,
841 * also known as public-key encryption algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100842 *
843 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
844 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100845 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100846 * This macro may return either 0 or 1 if \p alg is not a supported
847 * algorithm identifier.
848 */
849#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
850 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
851
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100852/** Whether the specified algorithm is a key agreement algorithm.
853 *
854 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
855 *
856 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
857 * This macro may return either 0 or 1 if \p alg is not a supported
858 * algorithm identifier.
859 */
860#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100861 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100862
863/** Whether the specified algorithm is a key derivation algorithm.
864 *
865 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
866 *
867 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
868 * This macro may return either 0 or 1 if \p alg is not a supported
869 * algorithm identifier.
870 */
871#define PSA_ALG_IS_KEY_DERIVATION(alg) \
872 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
873
Summer Qin359167d2021-07-05 18:11:50 +0800874/** Whether the specified algorithm is a key stretching / password hashing
875 * algorithm.
876 *
877 * A key stretching / password hashing algorithm is a key derivation algorithm
878 * that is suitable for use with a low-entropy secret such as a password.
879 * Equivalently, it's a key derivation algorithm that uses a
880 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
881 *
882 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
883 *
884 * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
885 * otherwise. This macro may return either 0 or 1 if \p alg is not a
886 * supported algorithm identifier.
887 */
888#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
889 (PSA_ALG_IS_KEY_DERIVATION(alg) && \
890 (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
891
Summer Qinf07cc312022-01-05 16:52:54 +0800892/** An invalid algorithm identifier value. */
Summer Qind635cd02023-03-31 18:07:38 +0800893/* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
Summer Qinf07cc312022-01-05 16:52:54 +0800894#define PSA_ALG_NONE ((psa_algorithm_t)0)
Summer Qind635cd02023-03-31 18:07:38 +0800895/* *INDENT-ON* */
Summer Qinf07cc312022-01-05 16:52:54 +0800896
Summer Qind635cd02023-03-31 18:07:38 +0800897#define PSA_ALG_HASH_MASK ((psa_algorithm_t) 0x000000ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100898/** MD5 */
Summer Qind635cd02023-03-31 18:07:38 +0800899#define PSA_ALG_MD5 ((psa_algorithm_t) 0x02000003)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100900/** PSA_ALG_RIPEMD160 */
Summer Qind635cd02023-03-31 18:07:38 +0800901#define PSA_ALG_RIPEMD160 ((psa_algorithm_t) 0x02000004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100902/** SHA1 */
Summer Qind635cd02023-03-31 18:07:38 +0800903#define PSA_ALG_SHA_1 ((psa_algorithm_t) 0x02000005)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100904/** SHA2-224 */
Summer Qind635cd02023-03-31 18:07:38 +0800905#define PSA_ALG_SHA_224 ((psa_algorithm_t) 0x02000008)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100906/** SHA2-256 */
Summer Qind635cd02023-03-31 18:07:38 +0800907#define PSA_ALG_SHA_256 ((psa_algorithm_t) 0x02000009)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100908/** SHA2-384 */
Summer Qind635cd02023-03-31 18:07:38 +0800909#define PSA_ALG_SHA_384 ((psa_algorithm_t) 0x0200000a)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100910/** SHA2-512 */
Summer Qind635cd02023-03-31 18:07:38 +0800911#define PSA_ALG_SHA_512 ((psa_algorithm_t) 0x0200000b)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100912/** SHA2-512/224 */
Summer Qind635cd02023-03-31 18:07:38 +0800913#define PSA_ALG_SHA_512_224 ((psa_algorithm_t) 0x0200000c)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100914/** SHA2-512/256 */
Summer Qind635cd02023-03-31 18:07:38 +0800915#define PSA_ALG_SHA_512_256 ((psa_algorithm_t) 0x0200000d)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100916/** SHA3-224 */
Summer Qind635cd02023-03-31 18:07:38 +0800917#define PSA_ALG_SHA3_224 ((psa_algorithm_t) 0x02000010)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100918/** SHA3-256 */
Summer Qind635cd02023-03-31 18:07:38 +0800919#define PSA_ALG_SHA3_256 ((psa_algorithm_t) 0x02000011)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100920/** SHA3-384 */
Summer Qind635cd02023-03-31 18:07:38 +0800921#define PSA_ALG_SHA3_384 ((psa_algorithm_t) 0x02000012)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100922/** SHA3-512 */
Summer Qind635cd02023-03-31 18:07:38 +0800923#define PSA_ALG_SHA3_512 ((psa_algorithm_t) 0x02000013)
Summer Qin359167d2021-07-05 18:11:50 +0800924/** The first 512 bits (64 bytes) of the SHAKE256 output.
925 *
926 * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
927 * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
928 * has the same output size and a (theoretically) higher security strength.
929 */
Summer Qind635cd02023-03-31 18:07:38 +0800930#define PSA_ALG_SHAKE256_512 ((psa_algorithm_t) 0x02000015)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100931
932/** In a hash-and-sign algorithm policy, allow any hash algorithm.
933 *
934 * This value may be used to form the algorithm usage field of a policy
935 * for a signature algorithm that is parametrized by a hash. The key
936 * may then be used to perform operations using the same signature
937 * algorithm parametrized with any supported hash.
938 *
939 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
Summer Qinf07cc312022-01-05 16:52:54 +0800940 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100941 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
942 * Then you may create and use a key as follows:
943 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
944 * ```
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100945 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
946 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100947 * ```
948 * - Import or generate key material.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100949 * - Call psa_sign_hash() or psa_verify_hash(), passing
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100950 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
951 * call to sign or verify a message may use a different hash.
952 * ```
Maulik Patel28659c42021-01-06 14:09:22 +0000953 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
954 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
955 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100956 * ```
957 *
958 * This value may not be used to build other algorithms that are
959 * parametrized over a hash. For any valid use of this macro to build
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100960 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100961 *
962 * This value may not be used to build an algorithm specification to
963 * perform an operation. It is only valid to build policies.
964 */
Summer Qind635cd02023-03-31 18:07:38 +0800965#define PSA_ALG_ANY_HASH ((psa_algorithm_t) 0x020000ff)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100966
Summer Qind635cd02023-03-31 18:07:38 +0800967#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t) 0x00c00000)
968#define PSA_ALG_HMAC_BASE ((psa_algorithm_t) 0x03800000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100969/** Macro to build an HMAC algorithm.
970 *
971 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
972 *
973 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
974 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
975 *
976 * \return The corresponding HMAC algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100977 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100978 * hash algorithm.
979 */
980#define PSA_ALG_HMAC(hash_alg) \
981 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
982
983#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
984 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
985
986/** Whether the specified algorithm is an HMAC algorithm.
987 *
988 * HMAC is a family of MAC algorithms that are based on a hash function.
989 *
990 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
991 *
992 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
993 * This macro may return either 0 or 1 if \p alg is not a supported
994 * algorithm identifier.
995 */
996#define PSA_ALG_IS_HMAC(alg) \
997 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
998 PSA_ALG_HMAC_BASE)
999
1000/* In the encoding of a MAC algorithm, the bits corresponding to
1001 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
1002 * truncated. As an exception, the value 0 means the untruncated algorithm,
1003 * whatever its length is. The length is encoded in 6 bits, so it can
1004 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
1005 * to full length is correctly encoded as 0 and any non-trivial truncation
1006 * is correctly encoded as a value between 1 and 63. */
Summer Qind635cd02023-03-31 18:07:38 +08001007#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t) 0x003f0000)
Maulik Patel28659c42021-01-06 14:09:22 +00001008#define PSA_MAC_TRUNCATION_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001009
Maulik Patel13b27cf2021-05-14 11:44:53 +01001010/* In the encoding of a MAC algorithm, the bit corresponding to
1011 * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1012 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1013 * algorithm policy can be used with any algorithm corresponding to the
1014 * same base class and having a (potentially truncated) MAC length greater or
1015 * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
Summer Qind635cd02023-03-31 18:07:38 +08001016#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001017
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001018/** Macro to build a truncated MAC algorithm.
1019 *
1020 * A truncated MAC algorithm is identical to the corresponding MAC
1021 * algorithm except that the MAC value for the truncated algorithm
1022 * consists of only the first \p mac_length bytes of the MAC value
1023 * for the untruncated algorithm.
1024 *
1025 * \note This macro may allow constructing algorithm identifiers that
1026 * are not valid, either because the specified length is larger
1027 * than the untruncated MAC or because the specified length is
1028 * smaller than permitted by the implementation.
1029 *
1030 * \note It is implementation-defined whether a truncated MAC that
1031 * is truncated to the same length as the MAC of the untruncated
1032 * algorithm is considered identical to the untruncated algorithm
1033 * for policy comparison purposes.
1034 *
1035 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001036 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001037 * is true). This may be a truncated or untruncated
1038 * MAC algorithm.
1039 * \param mac_length Desired length of the truncated MAC in bytes.
1040 * This must be at most the full length of the MAC
1041 * and must be at least an implementation-specified
1042 * minimum. The implementation-specified minimum
1043 * shall not be zero.
1044 *
1045 * \return The corresponding MAC algorithm with the specified
1046 * length.
Summer Qin359167d2021-07-05 18:11:50 +08001047 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001048 * MAC algorithm or if \p mac_length is too small or
1049 * too large for the specified MAC algorithm.
1050 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001051#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
1052 (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1053 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001054 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1055
1056/** Macro to build the base MAC algorithm corresponding to a truncated
1057 * MAC algorithm.
1058 *
1059 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001060 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001061 * is true). This may be a truncated or untruncated
1062 * MAC algorithm.
1063 *
1064 * \return The corresponding base MAC algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001065 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001066 * MAC algorithm.
1067 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001068#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
1069 ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1070 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001071
1072/** Length to which a MAC algorithm is truncated.
1073 *
1074 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001075 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001076 * is true).
1077 *
1078 * \return Length of the truncated MAC in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08001079 * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
1080 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001081 * MAC algorithm.
1082 */
1083#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
1084 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1085
Maulik Patel13b27cf2021-05-14 11:44:53 +01001086/** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1087 *
1088 * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1089 * sharing the same base algorithm, and where the (potentially truncated) MAC
1090 * length of the specific algorithm is equal to or larger then the wildcard
1091 * algorithm's minimum MAC length.
1092 *
1093 * \note When setting the minimum required MAC length to less than the
1094 * smallest MAC length allowed by the base algorithm, this effectively
1095 * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1096 *
1097 * \param mac_alg A MAC algorithm identifier (value of type
1098 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1099 * is true).
1100 * \param min_mac_length Desired minimum length of the message authentication
1101 * code in bytes. This must be at most the untruncated
1102 * length of the MAC and must be at least 1.
1103 *
1104 * \return The corresponding MAC wildcard algorithm with the
1105 * specified minimum length.
1106 * \return Unspecified if \p mac_alg is not a supported MAC
1107 * algorithm or if \p min_mac_length is less than 1 or
1108 * too large for the specified MAC algorithm.
1109 */
1110#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
Summer Qind635cd02023-03-31 18:07:38 +08001111 (PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
1112 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001113
Summer Qind635cd02023-03-31 18:07:38 +08001114#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t) 0x03c00000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001115/** The CBC-MAC construction over a block cipher
1116 *
1117 * \warning CBC-MAC is insecure in many cases.
1118 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1119 */
Summer Qind635cd02023-03-31 18:07:38 +08001120#define PSA_ALG_CBC_MAC ((psa_algorithm_t) 0x03c00100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001121/** The CMAC construction over a block cipher */
Summer Qind635cd02023-03-31 18:07:38 +08001122#define PSA_ALG_CMAC ((psa_algorithm_t) 0x03c00200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001123
1124/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1125 *
1126 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1127 *
1128 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1129 * This macro may return either 0 or 1 if \p alg is not a supported
1130 * algorithm identifier.
1131 */
1132#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
1133 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1134 PSA_ALG_CIPHER_MAC_BASE)
1135
Summer Qind635cd02023-03-31 18:07:38 +08001136#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t) 0x00800000)
1137#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001138
1139/** Whether the specified algorithm is a stream cipher.
1140 *
1141 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1142 * by applying a bitwise-xor with a stream of bytes that is generated
1143 * from a key.
1144 *
1145 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1146 *
1147 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1148 * This macro may return either 0 or 1 if \p alg is not a supported
1149 * algorithm identifier or if it is not a symmetric cipher algorithm.
1150 */
1151#define PSA_ALG_IS_STREAM_CIPHER(alg) \
1152 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
Summer Qind635cd02023-03-31 18:07:38 +08001153 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001154
Maulik Patel28659c42021-01-06 14:09:22 +00001155/** The stream cipher mode of a stream cipher algorithm.
1156 *
1157 * The underlying stream cipher is determined by the key type.
1158 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001159 */
Summer Qind635cd02023-03-31 18:07:38 +08001160#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t) 0x04800100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001161
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001162/** The CTR stream cipher mode.
1163 *
1164 * CTR is a stream cipher which is built from a block cipher.
1165 * The underlying block cipher is determined by the key type.
1166 * For example, to use AES-128-CTR, use this algorithm with
1167 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1168 */
Summer Qind635cd02023-03-31 18:07:38 +08001169#define PSA_ALG_CTR ((psa_algorithm_t) 0x04c01000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001170
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001171/** The CFB stream cipher mode.
1172 *
1173 * The underlying block cipher is determined by the key type.
1174 */
Summer Qind635cd02023-03-31 18:07:38 +08001175#define PSA_ALG_CFB ((psa_algorithm_t) 0x04c01100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001176
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001177/** The OFB stream cipher mode.
1178 *
1179 * The underlying block cipher is determined by the key type.
1180 */
Summer Qind635cd02023-03-31 18:07:38 +08001181#define PSA_ALG_OFB ((psa_algorithm_t) 0x04c01200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001182
1183/** The XTS cipher mode.
1184 *
1185 * XTS is a cipher mode which is built from a block cipher. It requires at
1186 * least one full block of input, but beyond this minimum the input
1187 * does not need to be a whole number of blocks.
1188 */
Summer Qind635cd02023-03-31 18:07:38 +08001189#define PSA_ALG_XTS ((psa_algorithm_t) 0x0440ff00)
Maulik Patel28659c42021-01-06 14:09:22 +00001190
1191/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1192 *
1193 * \warning ECB mode does not protect the confidentiality of the encrypted data
1194 * except in extremely narrow circumstances. It is recommended that applications
1195 * only use ECB if they need to construct an operating mode that the
1196 * implementation does not provide. Implementations are encouraged to provide
1197 * the modes that applications need in preference to supporting direct access
1198 * to ECB.
1199 *
1200 * The underlying block cipher is determined by the key type.
1201 *
1202 * This symmetric cipher mode can only be used with messages whose lengths are a
1203 * multiple of the block size of the chosen block cipher.
1204 *
1205 * ECB mode does not accept an initialization vector (IV). When using a
1206 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1207 * and psa_cipher_set_iv() must not be called.
1208 */
Summer Qind635cd02023-03-31 18:07:38 +08001209#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t) 0x04404400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001210
1211/** The CBC block cipher chaining mode, with no padding.
1212 *
1213 * The underlying block cipher is determined by the key type.
1214 *
1215 * This symmetric cipher mode can only be used with messages whose lengths
1216 * are whole number of blocks for the chosen block cipher.
1217 */
Summer Qind635cd02023-03-31 18:07:38 +08001218#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t) 0x04404000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001219
1220/** The CBC block cipher chaining mode with PKCS#7 padding.
1221 *
1222 * The underlying block cipher is determined by the key type.
1223 *
1224 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1225 */
Summer Qind635cd02023-03-31 18:07:38 +08001226#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t) 0x04404100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001227
Summer Qind635cd02023-03-31 18:07:38 +08001228#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001229
1230/** Whether the specified algorithm is an AEAD mode on a block cipher.
1231 *
1232 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1233 *
1234 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1235 * a block cipher, 0 otherwise.
1236 * This macro may return either 0 or 1 if \p alg is not a supported
1237 * algorithm identifier.
1238 */
1239#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1240 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1241 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1242
1243/** The CCM authenticated encryption algorithm.
1244 *
1245 * The underlying block cipher is determined by the key type.
1246 */
Summer Qind635cd02023-03-31 18:07:38 +08001247#define PSA_ALG_CCM ((psa_algorithm_t) 0x05500100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001248
Summer Qinf07cc312022-01-05 16:52:54 +08001249/** The CCM* cipher mode without authentication.
1250 *
1251 * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1252 * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1253 *
1254 * The underlying block cipher is determined by the key type.
1255 *
1256 * Currently only 13-byte long IV's are supported.
1257 */
Summer Qind635cd02023-03-31 18:07:38 +08001258#define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t) 0x04c01300)
Summer Qinf07cc312022-01-05 16:52:54 +08001259
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001260/** The GCM authenticated encryption algorithm.
1261 *
1262 * The underlying block cipher is determined by the key type.
1263 */
Summer Qind635cd02023-03-31 18:07:38 +08001264#define PSA_ALG_GCM ((psa_algorithm_t) 0x05500200)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001265
1266/** The Chacha20-Poly1305 AEAD algorithm.
1267 *
1268 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1269 *
1270 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1271 * and should reject other sizes.
1272 *
1273 * Implementations must support 16-byte tags and should reject other sizes.
1274 */
Summer Qind635cd02023-03-31 18:07:38 +08001275#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t) 0x05100500)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001276
Summer Qin614002c2023-01-19 15:22:39 +08001277/* In the encoding of an AEAD algorithm, the bits corresponding to
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001278 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1279 * The constants for default lengths follow this encoding.
1280 */
Summer Qind635cd02023-03-31 18:07:38 +08001281#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t) 0x003f0000)
Maulik Patel28659c42021-01-06 14:09:22 +00001282#define PSA_AEAD_TAG_LENGTH_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001283
Maulik Patel13b27cf2021-05-14 11:44:53 +01001284/* In the encoding of an AEAD algorithm, the bit corresponding to
1285 * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1286 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1287 * algorithm policy can be used with any algorithm corresponding to the
1288 * same base class and having a tag length greater than or equal to the one
1289 * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
Summer Qind635cd02023-03-31 18:07:38 +08001290#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001291
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001292/** Macro to build a shortened AEAD algorithm.
1293 *
1294 * A shortened AEAD algorithm is similar to the corresponding AEAD
1295 * algorithm, but has an authentication tag that consists of fewer bytes.
1296 * Depending on the algorithm, the tag length may affect the calculation
1297 * of the ciphertext.
1298 *
1299 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001300 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001301 * is true).
1302 * \param tag_length Desired length of the authentication tag in bytes.
1303 *
1304 * \return The corresponding AEAD algorithm with the specified
1305 * length.
Summer Qin359167d2021-07-05 18:11:50 +08001306 * \return Unspecified if \p aead_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001307 * AEAD algorithm or if \p tag_length is not valid
1308 * for the specified AEAD algorithm.
1309 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001310#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
1311 (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1312 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001313 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
Summer Qind635cd02023-03-31 18:07:38 +08001314 PSA_ALG_AEAD_TAG_LENGTH_MASK))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001315
Maulik Patel13b27cf2021-05-14 11:44:53 +01001316/** Retrieve the tag length of a specified AEAD algorithm
1317 *
1318 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001319 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001320 * is true).
1321 *
1322 * \return The tag length specified by the input algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001323 * \return Unspecified if \p aead_alg is not a supported
1324 * AEAD algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +01001325 */
1326#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1327 (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
Summer Qind635cd02023-03-31 18:07:38 +08001328 PSA_AEAD_TAG_LENGTH_OFFSET)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001329
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001330/** Calculate the corresponding AEAD algorithm with the default tag length.
1331 *
1332 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
Summer Qin359167d2021-07-05 18:11:50 +08001333 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001334 *
1335 * \return The corresponding AEAD algorithm with the default
1336 * tag length for that algorithm.
1337 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001338#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001339 ( \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001340 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1341 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1342 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001343 0)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001344#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1345 PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1346 PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001347 ref :
1348
Maulik Patel13b27cf2021-05-14 11:44:53 +01001349/** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1350 *
1351 * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1352 * sharing the same base algorithm, and where the tag length of the specific
1353 * algorithm is equal to or larger then the minimum tag length specified by the
1354 * wildcard algorithm.
1355 *
1356 * \note When setting the minimum required tag length to less than the
1357 * smallest tag length allowed by the base algorithm, this effectively
1358 * becomes an 'any-tag-length-allowed' policy for that base algorithm.
1359 *
1360 * \param aead_alg An AEAD algorithm identifier (value of type
1361 * #psa_algorithm_t such that
1362 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1363 * \param min_tag_length Desired minimum length of the authentication tag in
1364 * bytes. This must be at least 1 and at most the largest
1365 * allowed tag length of the algorithm.
1366 *
1367 * \return The corresponding AEAD wildcard algorithm with the
1368 * specified minimum length.
1369 * \return Unspecified if \p aead_alg is not a supported
1370 * AEAD algorithm or if \p min_tag_length is less than 1
1371 * or too large for the specified AEAD algorithm.
1372 */
1373#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
Summer Qind635cd02023-03-31 18:07:38 +08001374 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1375 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001376
Summer Qind635cd02023-03-31 18:07:38 +08001377#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t) 0x06000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001378/** RSA PKCS#1 v1.5 signature with hashing.
1379 *
1380 * This is the signature scheme defined by RFC 8017
1381 * (PKCS#1: RSA Cryptography Specifications) under the name
1382 * RSASSA-PKCS1-v1_5.
1383 *
1384 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1385 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1386 * This includes #PSA_ALG_ANY_HASH
1387 * when specifying the algorithm in a usage policy.
1388 *
1389 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001390 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001391 * hash algorithm.
1392 */
1393#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1394 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1395/** Raw PKCS#1 v1.5 signature.
1396 *
1397 * The input to this algorithm is the DigestInfo structure used by
1398 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1399 * steps 3&ndash;6.
1400 */
1401#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1402#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1403 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1404
Summer Qind635cd02023-03-31 18:07:38 +08001405#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t) 0x06000300)
1406#define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t) 0x06001300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001407/** RSA PSS signature with hashing.
1408 *
1409 * This is the signature scheme defined by RFC 8017
1410 * (PKCS#1: RSA Cryptography Specifications) under the name
1411 * RSASSA-PSS, with the message generation function MGF1, and with
Summer Qin614002c2023-01-19 15:22:39 +08001412 * a salt length equal to the length of the hash, or the largest
1413 * possible salt length for the algorithm and key size if that is
1414 * smaller than the hash length. The specified hash algorithm is
1415 * used to hash the input message, to create the salted hash, and
1416 * for the mask generation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001417 *
1418 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1419 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1420 * This includes #PSA_ALG_ANY_HASH
1421 * when specifying the algorithm in a usage policy.
1422 *
1423 * \return The corresponding RSA PSS signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001424 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001425 * hash algorithm.
1426 */
1427#define PSA_ALG_RSA_PSS(hash_alg) \
1428 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Summer Qinf07cc312022-01-05 16:52:54 +08001429
1430/** RSA PSS signature with hashing with relaxed verification.
1431 *
1432 * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1433 * but allows an arbitrary salt length (including \c 0) when verifying a
1434 * signature.
1435 *
1436 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1437 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1438 * This includes #PSA_ALG_ANY_HASH
1439 * when specifying the algorithm in a usage policy.
1440 *
1441 * \return The corresponding RSA PSS signature algorithm.
1442 * \return Unspecified if \p hash_alg is not a supported
1443 * hash algorithm.
1444 */
1445#define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \
1446 (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1447
1448/** Whether the specified algorithm is RSA PSS with standard salt.
1449 *
1450 * \param alg An algorithm value or an algorithm policy wildcard.
1451 *
1452 * \return 1 if \p alg is of the form
1453 * #PSA_ALG_RSA_PSS(\c hash_alg),
1454 * where \c hash_alg is a hash algorithm or
1455 * #PSA_ALG_ANY_HASH. 0 otherwise.
1456 * This macro may return either 0 or 1 if \p alg is not
1457 * a supported algorithm identifier or policy.
1458 */
1459#define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001460 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1461
Summer Qinf07cc312022-01-05 16:52:54 +08001462/** Whether the specified algorithm is RSA PSS with any salt.
1463 *
1464 * \param alg An algorithm value or an algorithm policy wildcard.
1465 *
1466 * \return 1 if \p alg is of the form
1467 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1468 * where \c hash_alg is a hash algorithm or
1469 * #PSA_ALG_ANY_HASH. 0 otherwise.
1470 * This macro may return either 0 or 1 if \p alg is not
1471 * a supported algorithm identifier or policy.
1472 */
1473#define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \
1474 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1475
1476/** Whether the specified algorithm is RSA PSS.
1477 *
1478 * This includes any of the RSA PSS algorithm variants, regardless of the
1479 * constraints on salt length.
1480 *
1481 * \param alg An algorithm value or an algorithm policy wildcard.
1482 *
1483 * \return 1 if \p alg is of the form
1484 * #PSA_ALG_RSA_PSS(\c hash_alg) or
1485 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1486 * where \c hash_alg is a hash algorithm or
1487 * #PSA_ALG_ANY_HASH. 0 otherwise.
1488 * This macro may return either 0 or 1 if \p alg is not
1489 * a supported algorithm identifier or policy.
1490 */
1491#define PSA_ALG_IS_RSA_PSS(alg) \
1492 (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \
1493 PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
1494
Summer Qind635cd02023-03-31 18:07:38 +08001495#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t) 0x06000600)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001496/** ECDSA signature with hashing.
1497 *
1498 * This is the ECDSA signature scheme defined by ANSI X9.62,
1499 * with a random per-message secret number (*k*).
1500 *
1501 * The representation of the signature as a byte string consists of
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001502 * the concatenation of the signature values *r* and *s*. Each of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001503 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1504 * of the base point of the curve in octets. Each value is represented
1505 * in big-endian order (most significant octet first).
1506 *
1507 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1508 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1509 * This includes #PSA_ALG_ANY_HASH
1510 * when specifying the algorithm in a usage policy.
1511 *
1512 * \return The corresponding ECDSA signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001513 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001514 * hash algorithm.
1515 */
1516#define PSA_ALG_ECDSA(hash_alg) \
1517 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1518/** ECDSA signature without hashing.
1519 *
1520 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1521 * without specifying a hash algorithm. This algorithm may only be
1522 * used to sign or verify a sequence of bytes that should be an
1523 * already-calculated hash. Note that the input is padded with
1524 * zeros on the left or truncated on the left as required to fit
1525 * the curve size.
1526 */
1527#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
Summer Qind635cd02023-03-31 18:07:38 +08001528#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t) 0x06000700)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001529/** Deterministic ECDSA signature with hashing.
1530 *
1531 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1532 *
1533 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1534 *
1535 * Note that when this algorithm is used for verification, signatures
1536 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1537 * same private key are accepted. In other words,
1538 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1539 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1540 *
1541 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1542 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1543 * This includes #PSA_ALG_ANY_HASH
1544 * when specifying the algorithm in a usage policy.
1545 *
1546 * \return The corresponding deterministic ECDSA signature
1547 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001548 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001549 * hash algorithm.
1550 */
1551#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1552 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Summer Qind635cd02023-03-31 18:07:38 +08001553#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t) 0x00000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001554#define PSA_ALG_IS_ECDSA(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001555 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001556 PSA_ALG_ECDSA_BASE)
1557#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001558 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001559#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1560 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1561#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1562 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1563
Summer Qin359167d2021-07-05 18:11:50 +08001564/** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1565 * using standard parameters.
1566 *
1567 * Contexts are not supported in the current version of this specification
1568 * because there is no suitable signature interface that can take the
1569 * context as a parameter. A future version of this specification may add
1570 * suitable functions and extend this algorithm to support contexts.
1571 *
1572 * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1573 * In this specification, the following curves are supported:
1574 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1575 * in RFC 8032.
1576 * The curve is Edwards25519.
1577 * The hash function used internally is SHA-512.
1578 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1579 * in RFC 8032.
1580 * The curve is Edwards448.
1581 * The hash function used internally is the first 114 bytes of the
1582 * SHAKE256 output.
1583 *
1584 * This algorithm can be used with psa_sign_message() and
1585 * psa_verify_message(). Since there is no prehashing, it cannot be used
1586 * with psa_sign_hash() or psa_verify_hash().
1587 *
1588 * The signature format is the concatenation of R and S as defined by
1589 * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1590 * string for Ed448).
1591 */
Summer Qind635cd02023-03-31 18:07:38 +08001592#define PSA_ALG_PURE_EDDSA ((psa_algorithm_t) 0x06000800)
Summer Qin359167d2021-07-05 18:11:50 +08001593
Summer Qind635cd02023-03-31 18:07:38 +08001594#define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t) 0x06000900)
Summer Qin359167d2021-07-05 18:11:50 +08001595#define PSA_ALG_IS_HASH_EDDSA(alg) \
1596 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1597
1598/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1599 * using SHA-512 and the Edwards25519 curve.
1600 *
1601 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1602 *
1603 * This algorithm is Ed25519 as specified in RFC 8032.
1604 * The curve is Edwards25519.
1605 * The prehash is SHA-512.
1606 * The hash function used internally is SHA-512.
1607 *
1608 * This is a hash-and-sign algorithm: to calculate a signature,
1609 * you can either:
1610 * - call psa_sign_message() on the message;
1611 * - or calculate the SHA-512 hash of the message
1612 * with psa_hash_compute()
1613 * or with a multi-part hash operation started with psa_hash_setup(),
1614 * using the hash algorithm #PSA_ALG_SHA_512,
1615 * then sign the calculated hash with psa_sign_hash().
1616 * Verifying a signature is similar, using psa_verify_message() or
1617 * psa_verify_hash() instead of the signature function.
1618 */
1619#define PSA_ALG_ED25519PH \
1620 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1621
1622/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1623 * using SHAKE256 and the Edwards448 curve.
1624 *
1625 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1626 *
1627 * This algorithm is Ed448 as specified in RFC 8032.
1628 * The curve is Edwards448.
1629 * The prehash is the first 64 bytes of the SHAKE256 output.
1630 * The hash function used internally is the first 114 bytes of the
1631 * SHAKE256 output.
1632 *
1633 * This is a hash-and-sign algorithm: to calculate a signature,
1634 * you can either:
1635 * - call psa_sign_message() on the message;
1636 * - or calculate the first 64 bytes of the SHAKE256 output of the message
1637 * with psa_hash_compute()
1638 * or with a multi-part hash operation started with psa_hash_setup(),
1639 * using the hash algorithm #PSA_ALG_SHAKE256_512,
1640 * then sign the calculated hash with psa_sign_hash().
1641 * Verifying a signature is similar, using psa_verify_message() or
1642 * psa_verify_hash() instead of the signature function.
1643 */
1644#define PSA_ALG_ED448PH \
1645 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1646
1647/* Default definition, to be overridden if the library is extended with
1648 * more hash-and-sign algorithms that we want to keep out of this header
1649 * file. */
1650#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1651
Summer Qinf07cc312022-01-05 16:52:54 +08001652/** Whether the specified algorithm is a signature algorithm that can be used
1653 * with psa_sign_hash() and psa_verify_hash().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001654 *
Summer Qinf07cc312022-01-05 16:52:54 +08001655 * This encompasses all strict hash-and-sign algorithms categorized by
1656 * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1657 * paradigm more loosely:
1658 * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1659 * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001660 *
Summer Qinf07cc312022-01-05 16:52:54 +08001661 * \param alg An algorithm identifier (value of type psa_algorithm_t).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001662 *
Summer Qinf07cc312022-01-05 16:52:54 +08001663 * \return 1 if alg is a signature algorithm that can be used to sign a
1664 * hash. 0 if alg is a signature algorithm that can only be used
1665 * to sign a message. 0 if alg is not a signature algorithm.
1666 * This macro can return either 0 or 1 if alg is not a
1667 * supported algorithm identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001668 */
Summer Qinf07cc312022-01-05 16:52:54 +08001669#define PSA_ALG_IS_SIGN_HASH(alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001670 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
Summer Qin359167d2021-07-05 18:11:50 +08001671 PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
1672 PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1673
1674/** Whether the specified algorithm is a signature algorithm that can be used
1675 * with psa_sign_message() and psa_verify_message().
1676 *
1677 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1678 *
1679 * \return 1 if alg is a signature algorithm that can be used to sign a
1680 * message. 0 if \p alg is a signature algorithm that can only be used
1681 * to sign an already-calculated hash. 0 if \p alg is not a signature
1682 * algorithm. This macro can return either 0 or 1 if \p alg is not a
1683 * supported algorithm identifier.
1684 */
1685#define PSA_ALG_IS_SIGN_MESSAGE(alg) \
Summer Qinf07cc312022-01-05 16:52:54 +08001686 (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
1687
1688/** Whether the specified algorithm is a hash-and-sign algorithm.
1689 *
1690 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1691 * structured in two parts: first the calculation of a hash in a way that
1692 * does not depend on the key, then the calculation of a signature from the
1693 * hash value and the key. Hash-and-sign algorithms encode the hash
1694 * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1695 * to extract this algorithm.
1696 *
1697 * Thus, for a hash-and-sign algorithm,
1698 * `psa_sign_message(key, alg, input, ...)` is equivalent to
1699 * ```
1700 * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1701 * psa_sign_hash(key, alg, hash, ..., signature, ...);
1702 * ```
1703 * Most usefully, separating the hash from the signature allows the hash
1704 * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1705 * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1706 * calculating the hash and then calling psa_verify_hash().
1707 *
1708 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1709 *
1710 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1711 * This macro may return either 0 or 1 if \p alg is not a supported
1712 * algorithm identifier.
1713 */
1714#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1715 (PSA_ALG_IS_SIGN_HASH(alg) && \
1716 ((alg) & PSA_ALG_HASH_MASK) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001717
1718/** Get the hash used by a hash-and-sign signature algorithm.
1719 *
1720 * A hash-and-sign algorithm is a signature algorithm which is
1721 * composed of two phases: first a hashing phase which does not use
1722 * the key and produces a hash of the input message, then a signing
1723 * phase which only uses the hash and the key and not the message
1724 * itself.
1725 *
1726 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1727 * #PSA_ALG_IS_SIGN(\p alg) is true).
1728 *
1729 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1730 * algorithm.
1731 * \return 0 if \p alg is a signature algorithm that does not
1732 * follow the hash-and-sign structure.
1733 * \return Unspecified if \p alg is not a signature algorithm or
1734 * if it is not supported by the implementation.
1735 */
1736#define PSA_ALG_SIGN_GET_HASH(alg) \
1737 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001738 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1739 0)
1740
1741/** RSA PKCS#1 v1.5 encryption.
1742 */
Summer Qind635cd02023-03-31 18:07:38 +08001743#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t) 0x07000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001744
Summer Qind635cd02023-03-31 18:07:38 +08001745#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t) 0x07000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001746/** RSA OAEP encryption.
1747 *
1748 * This is the encryption scheme defined by RFC 8017
1749 * (PKCS#1: RSA Cryptography Specifications) under the name
1750 * RSAES-OAEP, with the message generation function MGF1.
1751 *
1752 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1753 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1754 * for MGF1.
1755 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001756 * \return The corresponding RSA OAEP encryption algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001757 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001758 * hash algorithm.
1759 */
1760#define PSA_ALG_RSA_OAEP(hash_alg) \
1761 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1762#define PSA_ALG_IS_RSA_OAEP(alg) \
1763 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1764#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1765 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1766 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1767 0)
1768
Summer Qind635cd02023-03-31 18:07:38 +08001769#define PSA_ALG_HKDF_BASE ((psa_algorithm_t) 0x08000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001770/** Macro to build an HKDF algorithm.
1771 *
Summer Qind635cd02023-03-31 18:07:38 +08001772 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)` is HKDF using HMAC-SHA-256.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001773 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001774 * This key derivation algorithm uses the following inputs:
1775 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1776 * It is optional; if omitted, the derivation uses an empty salt.
1777 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1778 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1779 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1780 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1781 * starting to generate output.
1782 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001783 * \warning HKDF processes the salt as follows: first hash it with hash_alg
1784 * if the salt is longer than the block size of the hash algorithm; then
1785 * pad with null bytes up to the block size. As a result, it is possible
1786 * for distinct salt inputs to result in the same outputs. To ensure
1787 * unique outputs, it is recommended to use a fixed length for salt values.
1788 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001789 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1790 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1791 *
1792 * \return The corresponding HKDF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001793 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001794 * hash algorithm.
1795 */
1796#define PSA_ALG_HKDF(hash_alg) \
1797 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1798/** Whether the specified algorithm is an HKDF algorithm.
1799 *
1800 * HKDF is a family of key derivation algorithms that are based on a hash
1801 * function and the HMAC construction.
1802 *
1803 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1804 *
1805 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1806 * This macro may return either 0 or 1 if \c alg is not a supported
1807 * key derivation algorithm identifier.
1808 */
1809#define PSA_ALG_IS_HKDF(alg) \
1810 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1811#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1812 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1813
Summer Qind635cd02023-03-31 18:07:38 +08001814#define PSA_ALG_HKDF_EXTRACT_BASE ((psa_algorithm_t) 0x08000400)
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001815/** Macro to build an HKDF-Extract algorithm.
1816 *
Summer Qind635cd02023-03-31 18:07:38 +08001817 * For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA_256)` is
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001818 * HKDF-Extract using HMAC-SHA-256.
1819 *
1820 * This key derivation algorithm uses the following inputs:
1821 * - PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1822 * - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the
1823 * "extract" step.
1824 * The inputs are mandatory and must be passed in the order above.
1825 * Each input may only be passed once.
1826 *
1827 * \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF
1828 * should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided
1829 * as a separate algorithm for the sake of protocols that use it as a
1830 * building block. It may also be a slight performance optimization
1831 * in applications that use HKDF with the same salt and key but many
1832 * different info strings.
1833 *
1834 * \warning HKDF processes the salt as follows: first hash it with hash_alg
1835 * if the salt is longer than the block size of the hash algorithm; then
1836 * pad with null bytes up to the block size. As a result, it is possible
1837 * for distinct salt inputs to result in the same outputs. To ensure
1838 * unique outputs, it is recommended to use a fixed length for salt values.
1839 *
1840 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1841 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1842 *
1843 * \return The corresponding HKDF-Extract algorithm.
1844 * \return Unspecified if \p hash_alg is not a supported
1845 * hash algorithm.
1846 */
1847#define PSA_ALG_HKDF_EXTRACT(hash_alg) \
1848 (PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1849/** Whether the specified algorithm is an HKDF-Extract algorithm.
1850 *
1851 * HKDF-Extract is a family of key derivation algorithms that are based
1852 * on a hash function and the HMAC construction.
1853 *
1854 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1855 *
1856 * \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.
1857 * This macro may return either 0 or 1 if \c alg is not a supported
1858 * key derivation algorithm identifier.
1859 */
1860#define PSA_ALG_IS_HKDF_EXTRACT(alg) \
1861 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)
1862
Summer Qind635cd02023-03-31 18:07:38 +08001863#define PSA_ALG_HKDF_EXPAND_BASE ((psa_algorithm_t) 0x08000500)
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001864/** Macro to build an HKDF-Expand algorithm.
1865 *
Summer Qind635cd02023-03-31 18:07:38 +08001866 * For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA_256)` is
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001867 * HKDF-Expand using HMAC-SHA-256.
1868 *
1869 * This key derivation algorithm uses the following inputs:
1870 * - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).
1871 * - PSA_KEY_DERIVATION_INPUT_INFO is the info string.
1872 *
1873 * The inputs are mandatory and must be passed in the order above.
1874 * Each input may only be passed once.
1875 *
1876 * \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`
1877 * should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as
1878 * a separate algorithm for the sake of protocols that use it as a building
1879 * block. It may also be a slight performance optimization in applications
1880 * that use HKDF with the same salt and key but many different info strings.
1881 *
1882 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1883 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1884 *
1885 * \return The corresponding HKDF-Expand algorithm.
1886 * \return Unspecified if \p hash_alg is not a supported
1887 * hash algorithm.
1888 */
1889#define PSA_ALG_HKDF_EXPAND(hash_alg) \
1890 (PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1891/** Whether the specified algorithm is an HKDF-Expand algorithm.
1892 *
1893 * HKDF-Expand is a family of key derivation algorithms that are based
1894 * on a hash function and the HMAC construction.
1895 *
1896 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1897 *
1898 * \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.
1899 * This macro may return either 0 or 1 if \c alg is not a supported
1900 * key derivation algorithm identifier.
1901 */
1902#define PSA_ALG_IS_HKDF_EXPAND(alg) \
1903 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1904
1905/** Whether the specified algorithm is an HKDF or HKDF-Extract or
1906 * HKDF-Expand algorithm.
1907 *
1908 *
1909 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1910 *
1911 * \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.
1912 * This macro may return either 0 or 1 if \c alg is not a supported
1913 * key derivation algorithm identifier.
1914 */
1915#define PSA_ALG_IS_ANY_HKDF(alg) \
1916 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE || \
1917 ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE || \
1918 ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1919
Summer Qind635cd02023-03-31 18:07:38 +08001920#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t) 0x08000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001921/** Macro to build a TLS-1.2 PRF algorithm.
1922 *
1923 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1924 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1925 * used with either SHA-256 or SHA-384.
1926 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001927 * This key derivation algorithm uses the following inputs, which must be
1928 * passed in the order given here:
1929 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1930 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1931 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1932 *
1933 * For the application to TLS-1.2 key expansion, the seed is the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001934 * concatenation of ServerHello.Random + ClientHello.Random,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001935 * and the label is "key expansion".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001936 *
Summer Qind635cd02023-03-31 18:07:38 +08001937 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256)` represents the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001938 * TLS 1.2 PRF using HMAC-SHA-256.
1939 *
1940 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1941 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1942 *
1943 * \return The corresponding TLS-1.2 PRF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001944 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001945 * hash algorithm.
1946 */
1947#define PSA_ALG_TLS12_PRF(hash_alg) \
1948 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1949
1950/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1951 *
1952 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1953 *
1954 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1955 * This macro may return either 0 or 1 if \c alg is not a supported
1956 * key derivation algorithm identifier.
1957 */
1958#define PSA_ALG_IS_TLS12_PRF(alg) \
1959 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1960#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1961 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1962
Summer Qind635cd02023-03-31 18:07:38 +08001963#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t) 0x08000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001964/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1965 *
1966 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1967 * from the PreSharedKey (PSK) through the application of padding
1968 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1969 * The latter is based on HMAC and can be used with either SHA-256
1970 * or SHA-384.
1971 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001972 * This key derivation algorithm uses the following inputs, which must be
1973 * passed in the order given here:
1974 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001975 * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
1976 * computation of the premaster secret. This input is optional;
1977 * if omitted, it defaults to a string of null bytes with the same length
1978 * as the secret (PSK) input.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001979 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1980 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1981 *
1982 * For the application to TLS-1.2, the seed (which is
1983 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1984 * ClientHello.Random + ServerHello.Random,
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001985 * the label is "master secret" or "extended master secret" and
1986 * the other secret depends on the key exchange specified in the cipher suite:
1987 * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
1988 * PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
1989 * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
1990 * (RFC 5489, Section 2), the other secret should be the output of the
1991 * PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
1992 * The recommended way to pass this input is to use a key derivation
1993 * algorithm constructed as
1994 * PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
1995 * and to call psa_key_derivation_key_agreement(). Alternatively,
1996 * this input may be an output of `psa_raw_key_agreement()` passed with
1997 * psa_key_derivation_input_bytes(), or an equivalent input passed with
1998 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
1999 * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
2000 * should be the 48-byte client challenge (the PreMasterSecret of
2001 * (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
2002 * a 46-byte random string chosen by the client. On the server, this is
2003 * typically an output of psa_asymmetric_decrypt() using
2004 * PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
2005 * with `psa_key_derivation_input_bytes()`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002006 *
Summer Qind635cd02023-03-31 18:07:38 +08002007 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256)` represents the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002008 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
2009 *
2010 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
2011 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
2012 *
2013 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002014 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002015 * hash algorithm.
2016 */
2017#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
2018 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2019
2020/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
2021 *
2022 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2023 *
2024 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
2025 * This macro may return either 0 or 1 if \c alg is not a supported
2026 * key derivation algorithm identifier.
2027 */
2028#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
2029 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
2030#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
2031 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
2032
Summer Qin614002c2023-01-19 15:22:39 +08002033/* The TLS 1.2 ECJPAKE-to-PMS KDF. It takes the shared secret K (an EC point
2034 * in case of EC J-PAKE) and calculates SHA256(K.X) that the rest of TLS 1.2
2035 * will use to derive the session secret, as defined by step 2 of
2036 * https://datatracker.ietf.org/doc/html/draft-cragie-tls-ecjpake-01#section-8.7.
2037 * Uses PSA_ALG_SHA_256.
2038 * This function takes a single input:
2039 * #PSA_KEY_DERIVATION_INPUT_SECRET is the shared secret K from EC J-PAKE.
2040 * The only supported curve is secp256r1 (the 256-bit curve in
2041 * #PSA_ECC_FAMILY_SECP_R1), so the input must be exactly 65 bytes.
2042 * The output has to be read as a single chunk of 32 bytes, defined as
2043 * PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE.
2044 */
Summer Qind635cd02023-03-31 18:07:38 +08002045#define PSA_ALG_TLS12_ECJPAKE_TO_PMS ((psa_algorithm_t) 0x08000609)
Summer Qin614002c2023-01-19 15:22:39 +08002046
Summer Qin359167d2021-07-05 18:11:50 +08002047/* This flag indicates whether the key derivation algorithm is suitable for
2048 * use on low-entropy secrets such as password - these algorithms are also
2049 * known as key stretching or password hashing schemes. These are also the
2050 * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
2051 *
2052 * Those algorithms cannot be combined with a key agreement algorithm.
2053 */
Summer Qind635cd02023-03-31 18:07:38 +08002054#define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t) 0x00800000)
Summer Qin359167d2021-07-05 18:11:50 +08002055
Summer Qind635cd02023-03-31 18:07:38 +08002056#define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t) 0x08800100)
Summer Qin359167d2021-07-05 18:11:50 +08002057/** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
2058 *
2059 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2060 * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
2061 * HMAC with the specified hash.
Summer Qind635cd02023-03-31 18:07:38 +08002062 * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA_256)` specifies PBKDF2
Summer Qin359167d2021-07-05 18:11:50 +08002063 * using the PRF HMAC-SHA-256.
2064 *
2065 * This key derivation algorithm uses the following inputs, which must be
2066 * provided in the following order:
2067 * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
2068 * This input step must be used exactly once.
2069 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
2070 * This input step must be used one or more times; if used several times, the
2071 * inputs will be concatenated. This can be used to build the final salt
2072 * from multiple sources, both public and secret (also known as pepper).
2073 * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
2074 * This input step must be used exactly once.
2075 *
2076 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
2077 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
2078 *
2079 * \return The corresponding PBKDF2-HMAC-XXX algorithm.
2080 * \return Unspecified if \p hash_alg is not a supported
2081 * hash algorithm.
2082 */
2083#define PSA_ALG_PBKDF2_HMAC(hash_alg) \
2084 (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2085
2086/** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
2087 *
2088 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2089 *
2090 * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
2091 * This macro may return either 0 or 1 if \c alg is not a supported
2092 * key derivation algorithm identifier.
2093 */
2094#define PSA_ALG_IS_PBKDF2_HMAC(alg) \
2095 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
2096
2097/** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
2098 *
2099 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2100 * This macro specifies the PBKDF2 algorithm constructed using the
2101 * AES-CMAC-PRF-128 PRF specified by RFC 4615.
2102 *
2103 * This key derivation algorithm uses the same inputs as
2104 * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
2105 */
Summer Qind635cd02023-03-31 18:07:38 +08002106#define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t) 0x08800200)
Summer Qin359167d2021-07-05 18:11:50 +08002107
Summer Qind635cd02023-03-31 18:07:38 +08002108#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t) 0xfe00ffff)
2109#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t) 0xffff0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002110
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002111/** Macro to build a combined algorithm that chains a key agreement with
2112 * a key derivation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002113 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002114 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
2115 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
2116 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
2117 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002118 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002119 * \return The corresponding key agreement and derivation
2120 * algorithm.
2121 * \return Unspecified if \p ka_alg is not a supported
2122 * key agreement algorithm or \p kdf_alg is not a
2123 * supported key derivation algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002124 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002125#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
2126 ((ka_alg) | (kdf_alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002127
2128#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
2129 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
2130
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002131#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
2132 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002133
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002134/** Whether the specified algorithm is a raw key agreement algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002135 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002136 * A raw key agreement algorithm is one that does not specify
2137 * a key derivation function.
2138 * Usually, raw key agreement algorithms are constructed directly with
2139 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
Maulik Patel28659c42021-01-06 14:09:22 +00002140 * constructed with #PSA_ALG_KEY_AGREEMENT().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002141 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002142 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2143 *
2144 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
2145 * This macro may return either 0 or 1 if \p alg is not a supported
2146 * algorithm identifier.
2147 */
2148#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
2149 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
2150 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
2151
2152#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
2153 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
2154
2155/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
2156 *
2157 * The shared secret produced by key agreement is
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002158 * `g^{ab}` in big-endian format.
2159 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
2160 * in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002161 */
Summer Qind635cd02023-03-31 18:07:38 +08002162#define PSA_ALG_FFDH ((psa_algorithm_t) 0x09010000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002163
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002164/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
2165 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002166 * This includes the raw finite field Diffie-Hellman algorithm as well as
2167 * finite-field Diffie-Hellman followed by any supporter key derivation
2168 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002169 *
2170 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2171 *
2172 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
2173 * This macro may return either 0 or 1 if \c alg is not a supported
2174 * key agreement algorithm identifier.
2175 */
2176#define PSA_ALG_IS_FFDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002177 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002178
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002179/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
2180 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002181 * The shared secret produced by key agreement is the x-coordinate of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002182 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
2183 * `m` is the bit size associated with the curve, i.e. the bit size of the
2184 * order of the curve's coordinate field. When `m` is not a multiple of 8,
2185 * the byte containing the most significant bit of the shared secret
2186 * is padded with zero bits. The byte order is either little-endian
2187 * or big-endian depending on the curve type.
2188 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08002189 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002190 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2191 * in little-endian byte order.
2192 * The bit size is 448 for Curve448 and 255 for Curve25519.
2193 * - For Weierstrass curves over prime fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08002194 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002195 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2196 * in big-endian byte order.
2197 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
2198 * - For Weierstrass curves over binary fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08002199 * `PSA_ECC_FAMILY_SECTXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002200 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2201 * in big-endian byte order.
2202 * The bit size is `m` for the field `F_{2^m}`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002203 */
Summer Qind635cd02023-03-31 18:07:38 +08002204#define PSA_ALG_ECDH ((psa_algorithm_t) 0x09020000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002205
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002206/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2207 * algorithm.
2208 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002209 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2210 * elliptic curve Diffie-Hellman followed by any supporter key derivation
2211 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002212 *
2213 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2214 *
2215 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2216 * 0 otherwise.
2217 * This macro may return either 0 or 1 if \c alg is not a supported
2218 * key agreement algorithm identifier.
2219 */
2220#define PSA_ALG_IS_ECDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002221 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002222
2223/** Whether the specified algorithm encoding is a wildcard.
2224 *
2225 * Wildcard values may only be used to set the usage algorithm field in
2226 * a policy, not to perform an operation.
2227 *
2228 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2229 *
2230 * \return 1 if \c alg is a wildcard algorithm encoding.
2231 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2232 * an operation).
2233 * \return This macro may return either 0 or 1 if \c alg is not a supported
2234 * algorithm identifier.
2235 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01002236#define PSA_ALG_IS_WILDCARD(alg) \
2237 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
2238 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
2239 PSA_ALG_IS_MAC(alg) ? \
2240 (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
2241 PSA_ALG_IS_AEAD(alg) ? \
2242 (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002243 (alg) == PSA_ALG_ANY_HASH)
2244
Summer Qin359167d2021-07-05 18:11:50 +08002245/** Get the hash used by a composite algorithm.
2246 *
2247 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2248 *
2249 * \return The underlying hash algorithm if alg is a composite algorithm that
2250 * uses a hash algorithm.
2251 *
2252 * \return \c 0 if alg is not a composite algorithm that uses a hash.
2253 */
2254#define PSA_ALG_GET_HASH(alg) \
Summer Qind635cd02023-03-31 18:07:38 +08002255 (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t) 0) : 0x02000000 | ((alg) & 0x000000ff))
Summer Qin359167d2021-07-05 18:11:50 +08002256
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002257/**@}*/
2258
2259/** \defgroup key_lifetimes Key lifetimes
2260 * @{
2261 */
2262
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002263/* Note that location and persistence level values are embedded in the
2264 * persistent key store, as part of key metadata. As a consequence, they
2265 * must not be changed (unless the storage format version changes).
2266 */
2267
Soby Mathew07ef6e42020-07-20 21:09:23 +01002268/** The default lifetime for volatile keys.
2269 *
Maulik Patel28659c42021-01-06 14:09:22 +00002270 * A volatile key only exists as long as the identifier to it is not destroyed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002271 * The key material is guaranteed to be erased on a power reset.
Soby Mathew07ef6e42020-07-20 21:09:23 +01002272 *
2273 * A key with this lifetime is typically stored in the RAM area of the
2274 * PSA Crypto subsystem. However this is an implementation choice.
2275 * If an implementation stores data about the key in a non-volatile memory,
2276 * it must release all the resources associated with the key and erase the
2277 * key material if the calling application terminates.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002278 */
Summer Qind635cd02023-03-31 18:07:38 +08002279#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t) 0x00000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002280
Soby Mathew07ef6e42020-07-20 21:09:23 +01002281/** The default lifetime for persistent keys.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002282 *
2283 * A persistent key remains in storage until it is explicitly destroyed or
2284 * until the corresponding storage area is wiped. This specification does
Maulik Patel13b27cf2021-05-14 11:44:53 +01002285 * not define any mechanism to wipe a storage area, but integrations may
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002286 * provide their own mechanism (for example to perform a factory reset,
2287 * to prepare for device refurbishment, or to uninstall an application).
2288 *
2289 * This lifetime value is the default storage area for the calling
Maulik Patel13b27cf2021-05-14 11:44:53 +01002290 * application. Integrations of Mbed TLS may support other persistent lifetimes.
Soby Mathew07ef6e42020-07-20 21:09:23 +01002291 * See ::psa_key_lifetime_t for more information.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002292 */
Summer Qind635cd02023-03-31 18:07:38 +08002293#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t) 0x00000001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002294
Soby Mathew07ef6e42020-07-20 21:09:23 +01002295/** The persistence level of volatile keys.
2296 *
2297 * See ::psa_key_persistence_t for more information.
2298 */
Summer Qind635cd02023-03-31 18:07:38 +08002299#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t) 0x00)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002300
2301/** The default persistence level for persistent keys.
2302 *
2303 * See ::psa_key_persistence_t for more information.
2304 */
Summer Qind635cd02023-03-31 18:07:38 +08002305#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t) 0x01)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002306
2307/** A persistence level indicating that a key is never destroyed.
2308 *
2309 * See ::psa_key_persistence_t for more information.
2310 */
Summer Qind635cd02023-03-31 18:07:38 +08002311#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t) 0xff)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002312
2313#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
Summer Qind635cd02023-03-31 18:07:38 +08002314 ((psa_key_persistence_t) ((lifetime) & 0x000000ff))
Soby Mathew07ef6e42020-07-20 21:09:23 +01002315
2316#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
Summer Qind635cd02023-03-31 18:07:38 +08002317 ((psa_key_location_t) ((lifetime) >> 8))
Soby Mathew07ef6e42020-07-20 21:09:23 +01002318
2319/** Whether a key lifetime indicates that the key is volatile.
2320 *
2321 * A volatile key is automatically destroyed by the implementation when
2322 * the application instance terminates. In particular, a volatile key
2323 * is automatically destroyed on a power reset of the device.
2324 *
2325 * A key that is not volatile is persistent. Persistent keys are
2326 * preserved until the application explicitly destroys them or until an
2327 * implementation-specific device management event occurs (for example,
2328 * a factory reset).
2329 *
2330 * \param lifetime The lifetime value to query (value of type
2331 * ::psa_key_lifetime_t).
2332 *
2333 * \return \c 1 if the key is volatile, otherwise \c 0.
2334 */
2335#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
2336 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
Summer Qin0e5b2e02020-10-22 11:23:39 +08002337 PSA_KEY_PERSISTENCE_VOLATILE)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002338
Summer Qin359167d2021-07-05 18:11:50 +08002339/** Whether a key lifetime indicates that the key is read-only.
2340 *
2341 * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2342 * They must be created through platform-specific means that bypass the API.
2343 *
2344 * Some platforms may offer ways to destroy read-only keys. For example,
2345 * consider a platform with multiple levels of privilege, where a
2346 * low-privilege application can use a key but is not allowed to destroy
2347 * it, and the platform exposes the key to the application with a read-only
2348 * lifetime. High-privilege code can destroy the key even though the
2349 * application sees the key as read-only.
2350 *
2351 * \param lifetime The lifetime value to query (value of type
2352 * ::psa_key_lifetime_t).
2353 *
2354 * \return \c 1 if the key is read-only, otherwise \c 0.
2355 */
2356#define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
2357 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2358 PSA_KEY_PERSISTENCE_READ_ONLY)
2359
Soby Mathew07ef6e42020-07-20 21:09:23 +01002360/** Construct a lifetime from a persistence level and a location.
2361 *
2362 * \param persistence The persistence level
2363 * (value of type ::psa_key_persistence_t).
2364 * \param location The location indicator
2365 * (value of type ::psa_key_location_t).
2366 *
2367 * \return The constructed lifetime value.
2368 */
2369#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2370 ((location) << 8 | (persistence))
2371
2372/** The local storage area for persistent keys.
2373 *
2374 * This storage area is available on all systems that can store persistent
2375 * keys without delegating the storage to a third-party cryptoprocessor.
2376 *
2377 * See ::psa_key_location_t for more information.
2378 */
Summer Qind635cd02023-03-31 18:07:38 +08002379#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t) 0x000000)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002380
Summer Qind635cd02023-03-31 18:07:38 +08002381#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t) 0x800000)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002382
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002383/* Note that key identifier values are embedded in the
2384 * persistent key store, as part of key metadata. As a consequence, they
2385 * must not be changed (unless the storage format version changes).
2386 */
2387
Summer Qinf07cc312022-01-05 16:52:54 +08002388/** The null key identifier.
2389 */
Summer Qind635cd02023-03-31 18:07:38 +08002390/* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
Summer Qinf07cc312022-01-05 16:52:54 +08002391#define PSA_KEY_ID_NULL ((psa_key_id_t)0)
Summer Qind635cd02023-03-31 18:07:38 +08002392/* *INDENT-ON* */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002393/** The minimum value for a key identifier chosen by the application.
2394 */
Summer Qind635cd02023-03-31 18:07:38 +08002395#define PSA_KEY_ID_USER_MIN ((psa_key_id_t) 0x00000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002396/** The maximum value for a key identifier chosen by the application.
2397 */
Summer Qind635cd02023-03-31 18:07:38 +08002398#define PSA_KEY_ID_USER_MAX ((psa_key_id_t) 0x3fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002399/** The minimum value for a key identifier chosen by the implementation.
2400 */
Summer Qind635cd02023-03-31 18:07:38 +08002401#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t) 0x40000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002402/** The maximum value for a key identifier chosen by the implementation.
2403 */
Summer Qind635cd02023-03-31 18:07:38 +08002404#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t) 0x7fffffff)
2405
2406
Antonio de Angelis34a0ffd2023-02-16 11:56:46 +00002407#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2408
2409#define MBEDTLS_SVC_KEY_ID_INIT ((psa_key_id_t) 0)
2410#define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) (id)
2411#define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) (0)
2412
2413/** Utility to initialize a key identifier at runtime.
2414 *
2415 * \param unused Unused parameter.
2416 * \param key_id Identifier of the key.
2417 */
2418static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2419 unsigned int unused, psa_key_id_t key_id)
2420{
2421 (void) unused;
2422
2423 return key_id;
2424}
2425
2426/** Compare two key identifiers.
2427 *
2428 * \param id1 First key identifier.
2429 * \param id2 Second key identifier.
2430 *
2431 * \return Non-zero if the two key identifier are equal, zero otherwise.
2432 */
2433static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2434 mbedtls_svc_key_id_t id2)
2435{
2436 return id1 == id2;
2437}
2438
2439/** Check whether a key identifier is null.
2440 *
2441 * \param key Key identifier.
2442 *
2443 * \return Non-zero if the key identifier is null, zero otherwise.
2444 */
2445static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2446{
2447 return key == 0;
2448}
2449
2450#else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2451#include "mbedtls/private_access.h"
2452#define MBEDTLS_SVC_KEY_ID_INIT ((mbedtls_svc_key_id_t){ 0, 0 })
2453#define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) ((id).MBEDTLS_PRIVATE(key_id))
2454#define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) ((id).MBEDTLS_PRIVATE(owner))
2455
2456/** Utility to initialize a key identifier at runtime.
2457 *
2458 * \param owner_id Identifier of the key owner.
2459 * \param key_id Identifier of the key.
2460 */
2461static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2462 mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id)
2463{
2464 return (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,
2465 .MBEDTLS_PRIVATE(owner) = owner_id };
2466}
2467
2468/** Compare two key identifiers.
2469 *
2470 * \param id1 First key identifier.
2471 * \param id2 Second key identifier.
2472 *
2473 * \return Non-zero if the two key identifier are equal, zero otherwise.
2474 */
2475static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2476 mbedtls_svc_key_id_t id2)
2477{
2478 return (id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id)) &&
2479 mbedtls_key_owner_id_equal(id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner));
2480}
2481
2482/** Check whether a key identifier is null.
2483 *
2484 * \param key Key identifier.
2485 *
2486 * \return Non-zero if the key identifier is null, zero otherwise.
2487 */
2488static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2489{
2490 return key.MBEDTLS_PRIVATE(key_id) == 0;
2491}
2492
2493#endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002494
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002495/**@}*/
2496
2497/** \defgroup policy Key policies
2498 * @{
2499 */
2500
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002501/* Note that key usage flags are embedded in the
2502 * persistent key store, as part of key metadata. As a consequence, they
2503 * must not be changed (unless the storage format version changes).
2504 */
2505
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002506/** Whether the key may be exported.
2507 *
2508 * A public key or the public part of a key pair may always be exported
2509 * regardless of the value of this permission flag.
2510 *
2511 * If a key does not have export permission, implementations shall not
2512 * allow the key to be exported in plain form from the cryptoprocessor,
2513 * whether through psa_export_key() or through a proprietary interface.
2514 * The key may however be exportable in a wrapped form, i.e. in a form
2515 * where it is encrypted by another key.
2516 */
Summer Qind635cd02023-03-31 18:07:38 +08002517#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t) 0x00000001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002518
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002519/** Whether the key may be copied.
2520 *
2521 * This flag allows the use of psa_copy_key() to make a copy of the key
2522 * with the same policy or a more restrictive policy.
2523 *
2524 * For lifetimes for which the key is located in a secure element which
2525 * enforce the non-exportability of keys, copying a key outside the secure
2526 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2527 * Copying the key inside the secure element is permitted with just
2528 * #PSA_KEY_USAGE_COPY if the secure element supports it.
2529 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2530 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2531 * is sufficient to permit the copy.
2532 */
Summer Qind635cd02023-03-31 18:07:38 +08002533#define PSA_KEY_USAGE_COPY ((psa_key_usage_t) 0x00000002)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002534
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002535/** Whether the key may be used to encrypt a message.
2536 *
2537 * This flag allows the key to be used for a symmetric encryption operation,
2538 * for an AEAD encryption-and-authentication operation,
2539 * or for an asymmetric encryption operation,
2540 * if otherwise permitted by the key's type and policy.
2541 *
2542 * For a key pair, this concerns the public key.
2543 */
Summer Qind635cd02023-03-31 18:07:38 +08002544#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t) 0x00000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002545
2546/** Whether the key may be used to decrypt a message.
2547 *
2548 * This flag allows the key to be used for a symmetric decryption operation,
2549 * for an AEAD decryption-and-verification operation,
2550 * or for an asymmetric decryption operation,
2551 * if otherwise permitted by the key's type and policy.
2552 *
2553 * For a key pair, this concerns the private key.
2554 */
Summer Qind635cd02023-03-31 18:07:38 +08002555#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t) 0x00000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002556
2557/** Whether the key may be used to sign a message.
2558 *
Summer Qin359167d2021-07-05 18:11:50 +08002559 * This flag allows the key to be used for a MAC calculation operation or for
2560 * an asymmetric message signature operation, if otherwise permitted by the
2561 * key’s type and policy.
2562 *
2563 * For a key pair, this concerns the private key.
2564 */
Summer Qind635cd02023-03-31 18:07:38 +08002565#define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t) 0x00000400)
Summer Qin359167d2021-07-05 18:11:50 +08002566
2567/** Whether the key may be used to verify a message.
2568 *
2569 * This flag allows the key to be used for a MAC verification operation or for
2570 * an asymmetric message signature verification operation, if otherwise
2571 * permitted by the key’s type and policy.
2572 *
2573 * For a key pair, this concerns the public key.
2574 */
Summer Qind635cd02023-03-31 18:07:38 +08002575#define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t) 0x00000800)
Summer Qin359167d2021-07-05 18:11:50 +08002576
2577/** Whether the key may be used to sign a message.
2578 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002579 * This flag allows the key to be used for a MAC calculation operation
2580 * or for an asymmetric signature operation,
2581 * if otherwise permitted by the key's type and policy.
2582 *
2583 * For a key pair, this concerns the private key.
2584 */
Summer Qind635cd02023-03-31 18:07:38 +08002585#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t) 0x00001000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002586
2587/** Whether the key may be used to verify a message signature.
2588 *
2589 * This flag allows the key to be used for a MAC verification operation
2590 * or for an asymmetric signature verification operation,
Summer Qin614002c2023-01-19 15:22:39 +08002591 * if otherwise permitted by the key's type and policy.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002592 *
2593 * For a key pair, this concerns the public key.
2594 */
Summer Qind635cd02023-03-31 18:07:38 +08002595#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t) 0x00002000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002596
Summer Qin359167d2021-07-05 18:11:50 +08002597/** Whether the key may be used to derive other keys or produce a password
2598 * hash.
2599 *
2600 * This flag allows the key to be used for a key derivation operation or for
Summer Qin614002c2023-01-19 15:22:39 +08002601 * a key agreement operation, if otherwise permitted by the key's type and
Summer Qin359167d2021-07-05 18:11:50 +08002602 * policy.
2603 *
2604 * If this flag is present on all keys used in calls to
2605 * psa_key_derivation_input_key() for a key derivation operation, then it
2606 * permits calling psa_key_derivation_output_bytes() or
2607 * psa_key_derivation_output_key() at the end of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002608 */
Summer Qind635cd02023-03-31 18:07:38 +08002609#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t) 0x00004000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002610
Summer Qin359167d2021-07-05 18:11:50 +08002611/** Whether the key may be used to verify the result of a key derivation,
2612 * including password hashing.
2613 *
2614 * This flag allows the key to be used:
2615 *
2616 * This flag allows the key to be used in a key derivation operation, if
Summer Qin614002c2023-01-19 15:22:39 +08002617 * otherwise permitted by the key's type and policy.
Summer Qin359167d2021-07-05 18:11:50 +08002618 *
2619 * If this flag is present on all keys used in calls to
2620 * psa_key_derivation_input_key() for a key derivation operation, then it
2621 * permits calling psa_key_derivation_verify_bytes() or
2622 * psa_key_derivation_verify_key() at the end of the operation.
2623 */
Summer Qind635cd02023-03-31 18:07:38 +08002624#define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t) 0x00008000)
Summer Qin359167d2021-07-05 18:11:50 +08002625
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002626/**@}*/
2627
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002628/** \defgroup derivation Key derivation
2629 * @{
2630 */
2631
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002632/* Key input steps are not embedded in the persistent storage, so you can
2633 * change them if needed: it's only an ABI change. */
2634
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002635/** A secret input for key derivation.
2636 *
2637 * This should be a key of type #PSA_KEY_TYPE_DERIVE
2638 * (passed to psa_key_derivation_input_key())
2639 * or the shared secret resulting from a key agreement
2640 * (obtained via psa_key_derivation_key_agreement()).
2641 *
2642 * The secret can also be a direct input (passed to
2643 * key_derivation_input_bytes()). In this case, the derivation operation
2644 * may not be used to derive keys: the operation will only allow
Summer Qin359167d2021-07-05 18:11:50 +08002645 * psa_key_derivation_output_bytes(),
2646 * psa_key_derivation_verify_bytes(), or
2647 * psa_key_derivation_verify_key(), but not
2648 * psa_key_derivation_output_key().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002649 */
Summer Qind635cd02023-03-31 18:07:38 +08002650#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t) 0x0101)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002651
Summer Qin359167d2021-07-05 18:11:50 +08002652/** A low-entropy secret input for password hashing / key stretching.
2653 *
2654 * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2655 * psa_key_derivation_input_key()) or a direct input (passed to
2656 * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2657 * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2658 * the shared secret resulting from a key agreement.
2659 *
2660 * The secret can also be a direct input (passed to
2661 * key_derivation_input_bytes()). In this case, the derivation operation
2662 * may not be used to derive keys: the operation will only allow
2663 * psa_key_derivation_output_bytes(),
2664 * psa_key_derivation_verify_bytes(), or
2665 * psa_key_derivation_verify_key(), but not
2666 * psa_key_derivation_output_key().
2667 */
Summer Qind635cd02023-03-31 18:07:38 +08002668#define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t) 0x0102)
Summer Qin359167d2021-07-05 18:11:50 +08002669
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002670/** A high-entropy additional secret input for key derivation.
2671 *
2672 * This is typically the shared secret resulting from a key agreement obtained
2673 * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
2674 * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
2675 * a direct input passed to `psa_key_derivation_input_bytes()`.
2676 */
2677#define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
Summer Qind635cd02023-03-31 18:07:38 +08002678 ((psa_key_derivation_step_t) 0x0103)
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002679
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002680/** A label for key derivation.
2681 *
2682 * This should be a direct input.
2683 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2684 */
Summer Qind635cd02023-03-31 18:07:38 +08002685#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t) 0x0201)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002686
2687/** A salt for key derivation.
2688 *
2689 * This should be a direct input.
Summer Qin359167d2021-07-05 18:11:50 +08002690 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2691 * #PSA_KEY_TYPE_PEPPER.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002692 */
Summer Qind635cd02023-03-31 18:07:38 +08002693#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t) 0x0202)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002694
2695/** An information string for key derivation.
2696 *
2697 * This should be a direct input.
2698 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2699 */
Summer Qind635cd02023-03-31 18:07:38 +08002700#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t) 0x0203)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002701
2702/** A seed for key derivation.
2703 *
2704 * This should be a direct input.
2705 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2706 */
Summer Qind635cd02023-03-31 18:07:38 +08002707#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t) 0x0204)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002708
Summer Qin359167d2021-07-05 18:11:50 +08002709/** A cost parameter for password hashing / key stretching.
2710 *
2711 * This must be a direct input, passed to psa_key_derivation_input_integer().
2712 */
Summer Qind635cd02023-03-31 18:07:38 +08002713#define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t) 0x0205)
Summer Qin359167d2021-07-05 18:11:50 +08002714
2715/**@}*/
2716
2717/** \defgroup helper_macros Helper macros
2718 * @{
2719 */
2720
2721/* Helper macros */
2722
2723/** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2724 * regardless of the tag length they encode.
2725 *
2726 * \param aead_alg_1 An AEAD algorithm identifier.
2727 * \param aead_alg_2 An AEAD algorithm identifier.
2728 *
2729 * \return 1 if both identifiers refer to the same AEAD algorithm,
2730 * 0 otherwise.
2731 * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2732 * a supported AEAD algorithm.
2733 */
2734#define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2735 (!(((aead_alg_1) ^ (aead_alg_2)) & \
2736 ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2737
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002738/**@}*/
2739
Summer Qind635cd02023-03-31 18:07:38 +08002740/**@}*/
2741
2742/** \defgroup interruptible Interruptible operations
2743 * @{
2744 */
2745
2746/** Maximum value for use with \c psa_interruptible_set_max_ops() to determine
2747 * the maximum number of ops allowed to be executed by an interruptible
2748 * function in a single call.
2749 */
2750#define PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED UINT32_MAX
2751
2752/**@}*/
2753
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002754#endif /* PSA_CRYPTO_VALUES_H */