blob: 769bc7d4642e52fc0dd9b9f0b0750f52691dc97d [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
Summer Qinf07cc312022-01-05 16:52:54 +08002 * Copyright (c) 2018-2022, 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 *
21 * This header file only defines preprocessor macros.
22 */
23
24#ifndef PSA_CRYPTO_VALUES_H
25#define PSA_CRYPTO_VALUES_H
26
27/** \defgroup error Error codes
28 * @{
29 */
30
31/* PSA error codes */
32
33/** The action was completed successfully. */
34#ifndef PSA_SUCCESS
35#define PSA_SUCCESS ((psa_status_t)0)
36#endif
37
38/** An error occurred that does not correspond to any defined
39 * failure cause.
40 *
41 * Implementations may use this error code if none of the other standard
42 * error codes are applicable. */
43#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
44
45/** The requested operation or a parameter is not supported
46 * by this implementation.
47 *
48 * Implementations should return this error code when an enumeration
49 * parameter such as a key type, algorithm, etc. is not recognized.
50 * If a combination of parameters is recognized and identified as
51 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
52#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
53
54/** The requested action is denied by a policy.
55 *
56 * Implementations should return this error code when the parameters
57 * are recognized as valid and supported, and a policy explicitly
58 * denies the requested operation.
59 *
60 * If a subset of the parameters of a function call identify a
61 * forbidden operation, and another subset of the parameters are
62 * not valid or not supported, it is unspecified whether the function
63 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
64 * #PSA_ERROR_INVALID_ARGUMENT. */
65#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
66
67/** An output buffer is too small.
68 *
69 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
70 * description to determine a sufficient buffer size.
71 *
72 * Implementations should preferably return this error code only
73 * in cases when performing the operation with a larger output
74 * buffer would succeed. However implementations may return this
75 * error if a function has invalid or unsupported parameters in addition
76 * to the parameters that determine the necessary output buffer size. */
77#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
78
79/** Asking for an item that already exists
80 *
81 * Implementations should return this error, when attempting
82 * to write an item (like a key) that already exists. */
83#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
84
85/** Asking for an item that doesn't exist
86 *
87 * Implementations should return this error, if a requested item (like
88 * a key) does not exist. */
89#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
90
91/** The requested action cannot be performed in the current state.
92 *
93 * Multipart operations return this error when one of the
94 * functions is called out of sequence. Refer to the function
95 * descriptions for permitted sequencing of functions.
96 *
97 * Implementations shall not return this error code to indicate
Antonio de Angelis04debbd2019-10-14 12:12:52 +010098 * that a key either exists or not,
99 * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
100 * as applicable.
101 *
102 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000103 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100104 * instead. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100105#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
106
107/** The parameters passed to the function are invalid.
108 *
109 * Implementations may return this error any time a parameter or
110 * combination of parameters are recognized as invalid.
111 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100112 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000113 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100114 * instead.
115 */
116#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
117
118/** There is not enough runtime memory.
119 *
120 * If the action is carried out across multiple security realms, this
121 * error can refer to available memory in any of the security realms. */
122#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
123
124/** There is not enough persistent storage.
125 *
126 * Functions that modify the key storage return this error code if
127 * there is insufficient storage space on the host media. In addition,
128 * many functions that do not otherwise access storage may return this
129 * error code if the implementation requires a mandatory log entry for
130 * the requested action and the log storage space is full. */
131#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
132
133/** There was a communication failure inside the implementation.
134 *
135 * This can indicate a communication failure between the application
136 * and an external cryptoprocessor or between the cryptoprocessor and
137 * an external volatile or persistent memory. A communication failure
138 * may be transient or permanent depending on the cause.
139 *
140 * \warning If a function returns this error, it is undetermined
141 * whether the requested action has completed or not. Implementations
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100142 * should return #PSA_SUCCESS on successful completion whenever
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100143 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
144 * if the requested action was completed successfully in an external
145 * cryptoprocessor but there was a breakdown of communication before
146 * the cryptoprocessor could report the status to the application.
147 */
148#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
149
150/** There was a storage failure that may have led to data loss.
151 *
152 * This error indicates that some persistent storage is corrupted.
153 * It should not be used for a corruption of volatile memory
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100154 * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100155 * between the cryptoprocessor and its external storage (use
156 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
157 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
158 *
159 * Note that a storage failure does not indicate that any data that was
160 * previously read is invalid. However this previously read data may no
161 * longer be readable from storage.
162 *
163 * When a storage failure occurs, it is no longer possible to ensure
164 * the global integrity of the keystore. Depending on the global
165 * integrity guarantees offered by the implementation, access to other
166 * data may or may not fail even if the data is still readable but
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100167 * its integrity cannot be guaranteed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100168 *
169 * Implementations should only use this error code to report a
170 * permanent storage corruption. However application writers should
171 * keep in mind that transient errors while reading the storage may be
172 * reported using this error code. */
173#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
174
175/** A hardware failure was detected.
176 *
177 * A hardware failure may be transient or permanent depending on the
178 * cause. */
179#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
180
181/** A tampering attempt was detected.
182 *
183 * If an application receives this error code, there is no guarantee
184 * that previously accessed or computed data was correct and remains
185 * confidential. Applications should not perform any security function
186 * and should enter a safe failure state.
187 *
188 * Implementations may return this error code if they detect an invalid
189 * state that cannot happen during normal operation and that indicates
190 * that the implementation's security guarantees no longer hold. Depending
191 * on the implementation architecture and on its security and safety goals,
192 * the implementation may forcibly terminate the application.
193 *
194 * This error code is intended as a last resort when a security breach
195 * is detected and it is unsure whether the keystore data is still
196 * protected. Implementations shall only return this error code
197 * to report an alarm from a tampering detector, to indicate that
198 * the confidentiality of stored data can no longer be guaranteed,
199 * or to indicate that the integrity of previously returned data is now
200 * considered compromised. Implementations shall not use this error code
201 * to indicate a hardware failure that merely makes it impossible to
202 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
203 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
204 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
205 * instead).
206 *
207 * This error indicates an attack against the application. Implementations
208 * shall not return this error code as a consequence of the behavior of
209 * the application itself. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100210#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100211
212/** There is not enough entropy to generate random data needed
213 * for the requested action.
214 *
215 * This error indicates a failure of a hardware random generator.
216 * Application writers should note that this error can be returned not
217 * only by functions whose purpose is to generate random data, such
218 * as key, IV or nonce generation, but also by functions that execute
219 * an algorithm with a randomized result, as well as functions that
220 * use randomization of intermediate computations as a countermeasure
221 * to certain attacks.
222 *
223 * Implementations should avoid returning this error after psa_crypto_init()
224 * has succeeded. Implementations should generate sufficient
225 * entropy during initialization and subsequently use a cryptographically
226 * secure pseudorandom generator (PRNG). However implementations may return
227 * this error at any time if a policy requires the PRNG to be reseeded
228 * during normal operation. */
229#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
230
231/** The signature, MAC or hash is incorrect.
232 *
233 * Verification functions return this error if the verification
234 * calculations completed successfully, and the value to be verified
235 * was determined to be incorrect.
236 *
237 * If the value to verify has an invalid size, implementations may return
238 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
239#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
240
241/** The decrypted padding is incorrect.
242 *
243 * \warning In some protocols, when decrypting data, it is essential that
244 * the behavior of the application does not depend on whether the padding
245 * is correct, down to precise timing. Applications should prefer
246 * protocols that use authenticated encryption rather than plain
247 * encryption. If the application must perform a decryption of
248 * unauthenticated data, the application writer should take care not
249 * to reveal whether the padding is invalid.
250 *
251 * Implementations should strive to make valid and invalid padding
252 * as close as possible to indistinguishable to an external observer.
253 * In particular, the timing of a decryption operation should not
254 * depend on the validity of the padding. */
255#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
256
257/** Return this error when there's insufficient data when attempting
258 * to read from a resource. */
259#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
260
Maulik Patel28659c42021-01-06 14:09:22 +0000261/** The key identifier is not valid. See also :ref:\`key-handles\`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100262 */
263#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
264
Maulik Patel13b27cf2021-05-14 11:44:53 +0100265/** Stored data has been corrupted.
266 *
267 * This error indicates that some persistent storage has suffered corruption.
268 * It does not indicate the following situations, which have specific error
269 * codes:
270 *
271 * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
272 * - A communication error between the cryptoprocessor and its external
273 * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
274 * - When the storage is in a valid state but is full - use
275 * #PSA_ERROR_INSUFFICIENT_STORAGE.
276 * - When the storage fails for other reasons - use
277 * #PSA_ERROR_STORAGE_FAILURE.
278 * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
279 *
280 * \note A storage corruption does not indicate that any data that was
281 * previously read is invalid. However this previously read data might no
282 * longer be readable from storage.
283 *
284 * When a storage failure occurs, it is no longer possible to ensure the
285 * global integrity of the keystore.
286 */
287#define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
288
289/** Data read from storage is not valid for the implementation.
290 *
291 * This error indicates that some data read from storage does not have a valid
292 * format. It does not indicate the following situations, which have specific
293 * error codes:
294 *
295 * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
296 * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
297 * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
298 *
299 * This error is typically a result of either storage corruption on a
300 * cleartext storage backend, or an attempt to read data that was
301 * written by an incompatible version of the library.
302 */
303#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
304
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100305/**@}*/
306
307/** \defgroup crypto_types Key and algorithm types
308 * @{
309 */
310
311/** An invalid key type value.
312 *
313 * Zero is not the encoding of any key type.
314 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100315#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100316
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100317/** Vendor-defined key type flag.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100318 *
319 * Key types defined by this standard will never have the
320 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
321 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
322 * respect the bitwise structure used by standard encodings whenever practical.
323 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100324#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100325
Soby Mathew07ef6e42020-07-20 21:09:23 +0100326#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
327#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
328#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
329#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
330#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100331
Soby Mathew07ef6e42020-07-20 21:09:23 +0100332#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100333
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100334/** Whether a key type is vendor-defined.
335 *
336 * See also #PSA_KEY_TYPE_VENDOR_FLAG.
337 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100338#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
339 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
340
341/** Whether a key type is an unstructured array of bytes.
342 *
343 * This encompasses both symmetric keys and non-key data.
344 */
345#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100346 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
347 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100348
349/** Whether a key type is asymmetric: either a key pair or a public key. */
350#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
351 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
352 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
353 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
354/** Whether a key type is the public part of a key pair. */
355#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
356 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
357/** Whether a key type is a key pair containing a private part and a public
358 * part. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100359#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100360 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
361/** The key pair type corresponding to a public key type.
362 *
363 * You may also pass a key pair type as \p type, it will be left unchanged.
364 *
365 * \param type A public key type or key pair type.
366 *
367 * \return The corresponding key pair type.
368 * If \p type is not a public key or a key pair,
369 * the return value is undefined.
370 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100371#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100372 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
373/** The public key type corresponding to a key pair type.
374 *
375 * You may also pass a key pair type as \p type, it will be left unchanged.
376 *
377 * \param type A public key type or key pair type.
378 *
379 * \return The corresponding public key type.
380 * If \p type is not a public key or a key pair,
381 * the return value is undefined.
382 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100383#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100384 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
385
386/** Raw data.
387 *
388 * A "key" of this type cannot be used for any cryptographic operation.
389 * Applications may use this type to store arbitrary data in the keystore. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100390#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100391
392/** HMAC key.
393 *
394 * The key policy determines which underlying hash algorithm the key can be
395 * used for.
396 *
397 * HMAC keys should generally have the same size as the underlying hash.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100398 * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100399 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100400#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100401
402/** A secret for key derivation.
403 *
Summer Qin359167d2021-07-05 18:11:50 +0800404 * This key type is for high-entropy secrets only. For low-entropy secrets,
405 * #PSA_KEY_TYPE_PASSWORD should be used instead.
406 *
407 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
408 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
409 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100410 * The key policy determines which key derivation algorithm the key
411 * can be used for.
412 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100413#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100414
Summer Qin359167d2021-07-05 18:11:50 +0800415/** A low-entropy secret for password hashing or key derivation.
416 *
417 * This key type is suitable for passwords and passphrases which are typically
418 * intended to be memorizable by humans, and have a low entropy relative to
419 * their size. It can be used for randomly generated or derived keys with
420 * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
421 * for such keys. It is not suitable for passwords with extremely low entropy,
422 * such as numerical PINs.
423 *
424 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
425 * key derivation algorithms. Algorithms that accept such an input were
426 * designed to accept low-entropy secret and are known as password hashing or
427 * key stretching algorithms.
428 *
429 * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
430 * key derivation algorithms, as the algorithms that take such an input expect
431 * it to be high-entropy.
432 *
433 * The key policy determines which key derivation algorithm the key can be
434 * used for, among the permissible subset defined above.
435 */
436#define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t)0x1203)
437
438/** A secret value that can be used to verify a password hash.
439 *
440 * The key policy determines which key derivation algorithm the key
441 * can be used for, among the same permissible subset as for
442 * #PSA_KEY_TYPE_PASSWORD.
443 */
444#define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t)0x1205)
445
446/** A secret value that can be used in when computing a password hash.
447 *
448 * The key policy determines which key derivation algorithm the key
449 * can be used for, among the subset of algorithms that can use pepper.
450 */
451#define PSA_KEY_TYPE_PEPPER ((psa_key_type_t)0x1206)
452
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100453/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100454 *
455 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
456 * 32 bytes (AES-256).
457 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100458#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100459
Summer Qinf07cc312022-01-05 16:52:54 +0800460/** Key for a cipher, AEAD or MAC algorithm based on the
461 * ARIA block cipher.
462 */
463#define PSA_KEY_TYPE_ARIA ((psa_key_type_t)0x2406)
464
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100465/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
466 *
Summer Qin359167d2021-07-05 18:11:50 +0800467 * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
468 * 192 bits (3-key 3DES).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100469 *
470 * Note that single DES and 2-key 3DES are weak and strongly
471 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
472 * is weak and deprecated and should only be used in legacy protocols.
473 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100474#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100475
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100476/** Key for a cipher, AEAD or MAC algorithm based on the
Summer Qinf07cc312022-01-05 16:52:54 +0800477 * Camellia block cipher.
478 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100479#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100480
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100481/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
482 *
483 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
484 *
485 * Implementations must support 12-byte nonces, may support 8-byte nonces,
486 * and should reject other sizes.
487 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100488#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100489
Summer Qin359167d2021-07-05 18:11:50 +0800490/** RSA public key.
491 *
492 * The size of an RSA key is the bit size of the modulus.
493 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100494#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
Summer Qin359167d2021-07-05 18:11:50 +0800495/** RSA key pair (private and public key).
496 *
497 * The size of an RSA key is the bit size of the modulus.
498 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100499#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100500/** Whether a key type is an RSA key (pair or public-only). */
501#define PSA_KEY_TYPE_IS_RSA(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100502 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100503
Soby Mathew07ef6e42020-07-20 21:09:23 +0100504#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
505#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
506#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100507/** Elliptic curve key pair.
508 *
Summer Qin359167d2021-07-05 18:11:50 +0800509 * The size of an elliptic curve key is the bit size associated with the curve,
510 * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
511 * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
512 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800513 * \param curve A value of type ::psa_ecc_family_t that
514 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100515 */
516#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
517 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
518/** Elliptic curve public key.
519 *
Summer Qin359167d2021-07-05 18:11:50 +0800520 * The size of an elliptic curve public key is the same as the corresponding
521 * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
522 * `PSA_ECC_FAMILY_xxx` curve families).
523 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800524 * \param curve A value of type ::psa_ecc_family_t that
525 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100526 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100527#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
528 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
529
530/** Whether a key type is an elliptic curve key (pair or public-only). */
531#define PSA_KEY_TYPE_IS_ECC(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100532 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100533 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
534/** Whether a key type is an elliptic curve key pair. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100535#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100536 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100537 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100538/** Whether a key type is an elliptic curve public key. */
539#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
540 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
541 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
542
543/** Extract the curve from an elliptic curve key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800544#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
545 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100546 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
547 0))
548
Soby Mathew07ef6e42020-07-20 21:09:23 +0100549/** SEC Koblitz curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100550 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100551 * This family comprises the following curves:
552 * secp192k1, secp224k1, secp256k1.
553 * They are defined in _Standards for Efficient Cryptography_,
554 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
555 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100556 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800557#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100558
Soby Mathew07ef6e42020-07-20 21:09:23 +0100559/** SEC random curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100560 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100561 * This family comprises the following curves:
562 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
563 * They are defined in _Standards for Efficient Cryptography_,
564 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
565 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100566 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800567#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100568/* SECP160R2 (SEC2 v1, obsolete) */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800569#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100570
Soby Mathew07ef6e42020-07-20 21:09:23 +0100571/** SEC Koblitz curves over binary fields.
572 *
573 * This family comprises the following curves:
574 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
575 * They are defined in _Standards for Efficient Cryptography_,
576 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
577 * https://www.secg.org/sec2-v2.pdf
578 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800579#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100580
581/** SEC random curves over binary fields.
582 *
583 * This family comprises the following curves:
584 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
585 * They are defined in _Standards for Efficient Cryptography_,
586 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
587 * https://www.secg.org/sec2-v2.pdf
588 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800589#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100590
591/** SEC additional random curves over binary fields.
592 *
593 * This family comprises the following curve:
594 * sect163r2.
595 * It is defined in _Standards for Efficient Cryptography_,
596 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
597 * https://www.secg.org/sec2-v2.pdf
598 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800599#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100600
601/** Brainpool P random curves.
602 *
603 * This family comprises the following curves:
604 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
605 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
606 * It is defined in RFC 5639.
607 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800608#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100609
610/** Curve25519 and Curve448.
611 *
612 * This family comprises the following Montgomery curves:
613 * - 255-bit: Bernstein et al.,
614 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
615 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
616 * - 448-bit: Hamburg,
617 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
618 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
619 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800620#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100621
Summer Qin359167d2021-07-05 18:11:50 +0800622/** The twisted Edwards curves Ed25519 and Ed448.
623 *
624 * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
625 * #PSA_ALG_ED25519PH for the 255-bit curve,
626 * #PSA_ALG_ED448PH for the 448-bit curve).
627 *
628 * This family comprises the following twisted Edwards curves:
629 * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
630 * to Curve25519.
631 * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
632 * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
633 * to Curve448.
634 * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
635 */
636#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
637
Soby Mathew07ef6e42020-07-20 21:09:23 +0100638#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
639#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
640#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100641/** Diffie-Hellman key pair.
642 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800643 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100644 * Diffie-Hellman group to be used.
645 */
646#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
647 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
648/** Diffie-Hellman public key.
649 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800650 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100651 * Diffie-Hellman group to be used.
652 */
653#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
654 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
655
656/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
657#define PSA_KEY_TYPE_IS_DH(type) \
658 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
659 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
660/** Whether a key type is a Diffie-Hellman key pair. */
661#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
662 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
663 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
664/** Whether a key type is a Diffie-Hellman public key. */
665#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
666 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
667 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
668
669/** Extract the group from a Diffie-Hellman key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800670#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
671 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100672 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
673 0))
674
Soby Mathew07ef6e42020-07-20 21:09:23 +0100675/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100676 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100677 * This family includes groups with the following key sizes (in bits):
678 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
679 * all of these sizes or only a subset.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100680 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800681#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100682
Soby Mathew07ef6e42020-07-20 21:09:23 +0100683#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
684 (((type) >> 8) & 7)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100685/** The block size of a block cipher.
686 *
687 * \param type A cipher key type (value of type #psa_key_type_t).
688 *
689 * \return The block size for a block cipher, or 1 for a stream cipher.
690 * The return value is undefined if \p type is not a supported
691 * cipher key type.
692 *
693 * \note It is possible to build stream cipher algorithms on top of a block
694 * cipher, for example CTR mode (#PSA_ALG_CTR).
695 * This macro only takes the key type into account, so it cannot be
696 * used to determine the size of the data that #psa_cipher_update()
697 * might buffer for future processing in general.
698 *
699 * \note This macro returns a compile-time constant if its argument is one.
700 *
701 * \warning This macro may evaluate its argument multiple times.
702 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100703#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100704 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100705 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100706 0u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100707
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100708/** Vendor-defined algorithm flag.
709 *
710 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
711 * bit set. Vendors who define additional algorithms must use an encoding with
712 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
713 * used by standard encodings whenever practical.
714 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100715#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100716
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100717#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000718#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
719#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100720#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000721#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
722#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
723#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
724#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
725#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100726
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100727/** Whether an algorithm is vendor-defined.
728 *
729 * See also #PSA_ALG_VENDOR_FLAG.
730 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100731#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
732 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
733
734/** Whether the specified algorithm is a hash algorithm.
735 *
736 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
737 *
738 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
739 * This macro may return either 0 or 1 if \p alg is not a supported
740 * algorithm identifier.
741 */
742#define PSA_ALG_IS_HASH(alg) \
743 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
744
745/** Whether the specified algorithm is a MAC algorithm.
746 *
747 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
748 *
749 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
750 * This macro may return either 0 or 1 if \p alg is not a supported
751 * algorithm identifier.
752 */
753#define PSA_ALG_IS_MAC(alg) \
754 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
755
756/** Whether the specified algorithm is a symmetric cipher algorithm.
757 *
758 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
759 *
760 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
761 * This macro may return either 0 or 1 if \p alg is not a supported
762 * algorithm identifier.
763 */
764#define PSA_ALG_IS_CIPHER(alg) \
765 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
766
767/** Whether the specified algorithm is an authenticated encryption
768 * with associated data (AEAD) algorithm.
769 *
770 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
771 *
772 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
773 * This macro may return either 0 or 1 if \p alg is not a supported
774 * algorithm identifier.
775 */
776#define PSA_ALG_IS_AEAD(alg) \
777 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
778
Soby Mathew07ef6e42020-07-20 21:09:23 +0100779/** Whether the specified algorithm is an asymmetric signature algorithm,
780 * also known as public-key signature algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100781 *
782 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
783 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100784 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100785 * This macro may return either 0 or 1 if \p alg is not a supported
786 * algorithm identifier.
787 */
788#define PSA_ALG_IS_SIGN(alg) \
789 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
790
Soby Mathew07ef6e42020-07-20 21:09:23 +0100791/** Whether the specified algorithm is an asymmetric encryption algorithm,
792 * also known as public-key encryption algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100793 *
794 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
795 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100796 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100797 * This macro may return either 0 or 1 if \p alg is not a supported
798 * algorithm identifier.
799 */
800#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
801 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
802
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100803/** Whether the specified algorithm is a key agreement algorithm.
804 *
805 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
806 *
807 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
808 * This macro may return either 0 or 1 if \p alg is not a supported
809 * algorithm identifier.
810 */
811#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100812 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100813
814/** Whether the specified algorithm is a key derivation algorithm.
815 *
816 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
817 *
818 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
819 * This macro may return either 0 or 1 if \p alg is not a supported
820 * algorithm identifier.
821 */
822#define PSA_ALG_IS_KEY_DERIVATION(alg) \
823 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
824
Summer Qin359167d2021-07-05 18:11:50 +0800825/** Whether the specified algorithm is a key stretching / password hashing
826 * algorithm.
827 *
828 * A key stretching / password hashing algorithm is a key derivation algorithm
829 * that is suitable for use with a low-entropy secret such as a password.
830 * Equivalently, it's a key derivation algorithm that uses a
831 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
832 *
833 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
834 *
835 * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
836 * otherwise. This macro may return either 0 or 1 if \p alg is not a
837 * supported algorithm identifier.
838 */
839#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
840 (PSA_ALG_IS_KEY_DERIVATION(alg) && \
841 (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
842
Summer Qinf07cc312022-01-05 16:52:54 +0800843/** An invalid algorithm identifier value. */
844#define PSA_ALG_NONE ((psa_algorithm_t)0)
845
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100846#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100847/** MD5 */
Maulik Patel28659c42021-01-06 14:09:22 +0000848#define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100849/** PSA_ALG_RIPEMD160 */
Maulik Patel28659c42021-01-06 14:09:22 +0000850#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100851/** SHA1 */
Maulik Patel28659c42021-01-06 14:09:22 +0000852#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100853/** SHA2-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000854#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100855/** SHA2-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000856#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100857/** SHA2-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000858#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100859/** SHA2-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000860#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100861/** SHA2-512/224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000862#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100863/** SHA2-512/256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000864#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100865/** SHA3-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000866#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100867/** SHA3-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000868#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100869/** SHA3-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000870#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100871/** SHA3-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000872#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
Summer Qin359167d2021-07-05 18:11:50 +0800873/** The first 512 bits (64 bytes) of the SHAKE256 output.
874 *
875 * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
876 * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
877 * has the same output size and a (theoretically) higher security strength.
878 */
879#define PSA_ALG_SHAKE256_512 ((psa_algorithm_t)0x02000015)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100880
881/** In a hash-and-sign algorithm policy, allow any hash algorithm.
882 *
883 * This value may be used to form the algorithm usage field of a policy
884 * for a signature algorithm that is parametrized by a hash. The key
885 * may then be used to perform operations using the same signature
886 * algorithm parametrized with any supported hash.
887 *
888 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
Summer Qinf07cc312022-01-05 16:52:54 +0800889 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100890 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
891 * Then you may create and use a key as follows:
892 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
893 * ```
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100894 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
895 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100896 * ```
897 * - Import or generate key material.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100898 * - Call psa_sign_hash() or psa_verify_hash(), passing
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100899 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
900 * call to sign or verify a message may use a different hash.
901 * ```
Maulik Patel28659c42021-01-06 14:09:22 +0000902 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
903 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
904 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100905 * ```
906 *
907 * This value may not be used to build other algorithms that are
908 * parametrized over a hash. For any valid use of this macro to build
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100909 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100910 *
911 * This value may not be used to build an algorithm specification to
912 * perform an operation. It is only valid to build policies.
913 */
Maulik Patel28659c42021-01-06 14:09:22 +0000914#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100915
916#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Maulik Patel28659c42021-01-06 14:09:22 +0000917#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100918/** Macro to build an HMAC algorithm.
919 *
920 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
921 *
922 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
923 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
924 *
925 * \return The corresponding HMAC algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100926 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100927 * hash algorithm.
928 */
929#define PSA_ALG_HMAC(hash_alg) \
930 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
931
932#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
933 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
934
935/** Whether the specified algorithm is an HMAC algorithm.
936 *
937 * HMAC is a family of MAC algorithms that are based on a hash function.
938 *
939 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
940 *
941 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
942 * This macro may return either 0 or 1 if \p alg is not a supported
943 * algorithm identifier.
944 */
945#define PSA_ALG_IS_HMAC(alg) \
946 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
947 PSA_ALG_HMAC_BASE)
948
949/* In the encoding of a MAC algorithm, the bits corresponding to
950 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
951 * truncated. As an exception, the value 0 means the untruncated algorithm,
952 * whatever its length is. The length is encoded in 6 bits, so it can
953 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
954 * to full length is correctly encoded as 0 and any non-trivial truncation
955 * is correctly encoded as a value between 1 and 63. */
Maulik Patel28659c42021-01-06 14:09:22 +0000956#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
957#define PSA_MAC_TRUNCATION_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100958
Maulik Patel13b27cf2021-05-14 11:44:53 +0100959/* In the encoding of a MAC algorithm, the bit corresponding to
960 * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
961 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
962 * algorithm policy can be used with any algorithm corresponding to the
963 * same base class and having a (potentially truncated) MAC length greater or
964 * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
965#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
966
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100967/** Macro to build a truncated MAC algorithm.
968 *
969 * A truncated MAC algorithm is identical to the corresponding MAC
970 * algorithm except that the MAC value for the truncated algorithm
971 * consists of only the first \p mac_length bytes of the MAC value
972 * for the untruncated algorithm.
973 *
974 * \note This macro may allow constructing algorithm identifiers that
975 * are not valid, either because the specified length is larger
976 * than the untruncated MAC or because the specified length is
977 * smaller than permitted by the implementation.
978 *
979 * \note It is implementation-defined whether a truncated MAC that
980 * is truncated to the same length as the MAC of the untruncated
981 * algorithm is considered identical to the untruncated algorithm
982 * for policy comparison purposes.
983 *
984 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +0800985 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100986 * is true). This may be a truncated or untruncated
987 * MAC algorithm.
988 * \param mac_length Desired length of the truncated MAC in bytes.
989 * This must be at most the full length of the MAC
990 * and must be at least an implementation-specified
991 * minimum. The implementation-specified minimum
992 * shall not be zero.
993 *
994 * \return The corresponding MAC algorithm with the specified
995 * length.
Summer Qin359167d2021-07-05 18:11:50 +0800996 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100997 * MAC algorithm or if \p mac_length is too small or
998 * too large for the specified MAC algorithm.
999 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001000#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
1001 (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1002 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001003 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1004
1005/** Macro to build the base MAC algorithm corresponding to a truncated
1006 * MAC algorithm.
1007 *
1008 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001009 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001010 * is true). This may be a truncated or untruncated
1011 * MAC algorithm.
1012 *
1013 * \return The corresponding base MAC algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001014 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001015 * MAC algorithm.
1016 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001017#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
1018 ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1019 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001020
1021/** Length to which a MAC algorithm is truncated.
1022 *
1023 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001024 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001025 * is true).
1026 *
1027 * \return Length of the truncated MAC in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08001028 * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
1029 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001030 * MAC algorithm.
1031 */
1032#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
1033 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1034
Maulik Patel13b27cf2021-05-14 11:44:53 +01001035/** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1036 *
1037 * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1038 * sharing the same base algorithm, and where the (potentially truncated) MAC
1039 * length of the specific algorithm is equal to or larger then the wildcard
1040 * algorithm's minimum MAC length.
1041 *
1042 * \note When setting the minimum required MAC length to less than the
1043 * smallest MAC length allowed by the base algorithm, this effectively
1044 * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1045 *
1046 * \param mac_alg A MAC algorithm identifier (value of type
1047 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1048 * is true).
1049 * \param min_mac_length Desired minimum length of the message authentication
1050 * code in bytes. This must be at most the untruncated
1051 * length of the MAC and must be at least 1.
1052 *
1053 * \return The corresponding MAC wildcard algorithm with the
1054 * specified minimum length.
1055 * \return Unspecified if \p mac_alg is not a supported MAC
1056 * algorithm or if \p min_mac_length is less than 1 or
1057 * too large for the specified MAC algorithm.
1058 */
1059#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
1060 ( PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
1061 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
1062
Maulik Patel28659c42021-01-06 14:09:22 +00001063#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001064/** The CBC-MAC construction over a block cipher
1065 *
1066 * \warning CBC-MAC is insecure in many cases.
1067 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1068 */
Maulik Patel28659c42021-01-06 14:09:22 +00001069#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001070/** The CMAC construction over a block cipher */
Maulik Patel28659c42021-01-06 14:09:22 +00001071#define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001072
1073/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1074 *
1075 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1076 *
1077 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1078 * This macro may return either 0 or 1 if \p alg is not a supported
1079 * algorithm identifier.
1080 */
1081#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
1082 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1083 PSA_ALG_CIPHER_MAC_BASE)
1084
1085#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
1086#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1087
1088/** Whether the specified algorithm is a stream cipher.
1089 *
1090 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1091 * by applying a bitwise-xor with a stream of bytes that is generated
1092 * from a key.
1093 *
1094 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1095 *
1096 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1097 * This macro may return either 0 or 1 if \p alg is not a supported
1098 * algorithm identifier or if it is not a symmetric cipher algorithm.
1099 */
1100#define PSA_ALG_IS_STREAM_CIPHER(alg) \
1101 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1102 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1103
Maulik Patel28659c42021-01-06 14:09:22 +00001104/** The stream cipher mode of a stream cipher algorithm.
1105 *
1106 * The underlying stream cipher is determined by the key type.
1107 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001108 */
Maulik Patel28659c42021-01-06 14:09:22 +00001109#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001110
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001111/** The CTR stream cipher mode.
1112 *
1113 * CTR is a stream cipher which is built from a block cipher.
1114 * The underlying block cipher is determined by the key type.
1115 * For example, to use AES-128-CTR, use this algorithm with
1116 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1117 */
Maulik Patel28659c42021-01-06 14:09:22 +00001118#define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001119
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001120/** The CFB stream cipher mode.
1121 *
1122 * The underlying block cipher is determined by the key type.
1123 */
Maulik Patel28659c42021-01-06 14:09:22 +00001124#define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001125
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001126/** The OFB stream cipher mode.
1127 *
1128 * The underlying block cipher is determined by the key type.
1129 */
Maulik Patel28659c42021-01-06 14:09:22 +00001130#define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001131
1132/** The XTS cipher mode.
1133 *
1134 * XTS is a cipher mode which is built from a block cipher. It requires at
1135 * least one full block of input, but beyond this minimum the input
1136 * does not need to be a whole number of blocks.
1137 */
Maulik Patel28659c42021-01-06 14:09:22 +00001138#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
1139
1140/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1141 *
1142 * \warning ECB mode does not protect the confidentiality of the encrypted data
1143 * except in extremely narrow circumstances. It is recommended that applications
1144 * only use ECB if they need to construct an operating mode that the
1145 * implementation does not provide. Implementations are encouraged to provide
1146 * the modes that applications need in preference to supporting direct access
1147 * to ECB.
1148 *
1149 * The underlying block cipher is determined by the key type.
1150 *
1151 * This symmetric cipher mode can only be used with messages whose lengths are a
1152 * multiple of the block size of the chosen block cipher.
1153 *
1154 * ECB mode does not accept an initialization vector (IV). When using a
1155 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1156 * and psa_cipher_set_iv() must not be called.
1157 */
1158#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001159
1160/** The CBC block cipher chaining mode, with no padding.
1161 *
1162 * The underlying block cipher is determined by the key type.
1163 *
1164 * This symmetric cipher mode can only be used with messages whose lengths
1165 * are whole number of blocks for the chosen block cipher.
1166 */
Maulik Patel28659c42021-01-06 14:09:22 +00001167#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001168
1169/** The CBC block cipher chaining mode with PKCS#7 padding.
1170 *
1171 * The underlying block cipher is determined by the key type.
1172 *
1173 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1174 */
Maulik Patel28659c42021-01-06 14:09:22 +00001175#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001176
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001177#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1178
1179/** Whether the specified algorithm is an AEAD mode on a block cipher.
1180 *
1181 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1182 *
1183 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1184 * a block cipher, 0 otherwise.
1185 * This macro may return either 0 or 1 if \p alg is not a supported
1186 * algorithm identifier.
1187 */
1188#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1189 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1190 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1191
1192/** The CCM authenticated encryption algorithm.
1193 *
1194 * The underlying block cipher is determined by the key type.
1195 */
Maulik Patel28659c42021-01-06 14:09:22 +00001196#define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001197
Summer Qinf07cc312022-01-05 16:52:54 +08001198/** The CCM* cipher mode without authentication.
1199 *
1200 * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1201 * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1202 *
1203 * The underlying block cipher is determined by the key type.
1204 *
1205 * Currently only 13-byte long IV's are supported.
1206 */
1207#define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t)0x04c01300)
1208
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001209/** The GCM authenticated encryption algorithm.
1210 *
1211 * The underlying block cipher is determined by the key type.
1212 */
Maulik Patel28659c42021-01-06 14:09:22 +00001213#define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001214
1215/** The Chacha20-Poly1305 AEAD algorithm.
1216 *
1217 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1218 *
1219 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1220 * and should reject other sizes.
1221 *
1222 * Implementations must support 16-byte tags and should reject other sizes.
1223 */
Maulik Patel28659c42021-01-06 14:09:22 +00001224#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001225
1226/* In the encoding of a AEAD algorithm, the bits corresponding to
1227 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1228 * The constants for default lengths follow this encoding.
1229 */
Maulik Patel28659c42021-01-06 14:09:22 +00001230#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1231#define PSA_AEAD_TAG_LENGTH_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001232
Maulik Patel13b27cf2021-05-14 11:44:53 +01001233/* In the encoding of an AEAD algorithm, the bit corresponding to
1234 * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1235 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1236 * algorithm policy can be used with any algorithm corresponding to the
1237 * same base class and having a tag length greater than or equal to the one
1238 * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1239#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
1240
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001241/** Macro to build a shortened AEAD algorithm.
1242 *
1243 * A shortened AEAD algorithm is similar to the corresponding AEAD
1244 * algorithm, but has an authentication tag that consists of fewer bytes.
1245 * Depending on the algorithm, the tag length may affect the calculation
1246 * of the ciphertext.
1247 *
1248 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001249 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001250 * is true).
1251 * \param tag_length Desired length of the authentication tag in bytes.
1252 *
1253 * \return The corresponding AEAD algorithm with the specified
1254 * length.
Summer Qin359167d2021-07-05 18:11:50 +08001255 * \return Unspecified if \p aead_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001256 * AEAD algorithm or if \p tag_length is not valid
1257 * for the specified AEAD algorithm.
1258 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001259#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
1260 (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1261 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001262 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1263 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1264
Maulik Patel13b27cf2021-05-14 11:44:53 +01001265/** Retrieve the tag length of a specified AEAD algorithm
1266 *
1267 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001268 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001269 * is true).
1270 *
1271 * \return The tag length specified by the input algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001272 * \return Unspecified if \p aead_alg is not a supported
1273 * AEAD algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +01001274 */
1275#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1276 (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
1277 PSA_AEAD_TAG_LENGTH_OFFSET )
1278
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001279/** Calculate the corresponding AEAD algorithm with the default tag length.
1280 *
1281 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
Summer Qin359167d2021-07-05 18:11:50 +08001282 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001283 *
1284 * \return The corresponding AEAD algorithm with the default
1285 * tag length for that algorithm.
1286 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001287#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001288 ( \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001289 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1290 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1291 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001292 0)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001293#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1294 PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1295 PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001296 ref :
1297
Maulik Patel13b27cf2021-05-14 11:44:53 +01001298/** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1299 *
1300 * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1301 * sharing the same base algorithm, and where the tag length of the specific
1302 * algorithm is equal to or larger then the minimum tag length specified by the
1303 * wildcard algorithm.
1304 *
1305 * \note When setting the minimum required tag length to less than the
1306 * smallest tag length allowed by the base algorithm, this effectively
1307 * becomes an 'any-tag-length-allowed' policy for that base algorithm.
1308 *
1309 * \param aead_alg An AEAD algorithm identifier (value of type
1310 * #psa_algorithm_t such that
1311 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1312 * \param min_tag_length Desired minimum length of the authentication tag in
1313 * bytes. This must be at least 1 and at most the largest
1314 * allowed tag length of the algorithm.
1315 *
1316 * \return The corresponding AEAD wildcard algorithm with the
1317 * specified minimum length.
1318 * \return Unspecified if \p aead_alg is not a supported
1319 * AEAD algorithm or if \p min_tag_length is less than 1
1320 * or too large for the specified AEAD algorithm.
1321 */
1322#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1323 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1324 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
1325
Maulik Patel28659c42021-01-06 14:09:22 +00001326#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001327/** RSA PKCS#1 v1.5 signature with hashing.
1328 *
1329 * This is the signature scheme defined by RFC 8017
1330 * (PKCS#1: RSA Cryptography Specifications) under the name
1331 * RSASSA-PKCS1-v1_5.
1332 *
1333 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1334 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1335 * This includes #PSA_ALG_ANY_HASH
1336 * when specifying the algorithm in a usage policy.
1337 *
1338 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001339 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001340 * hash algorithm.
1341 */
1342#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1343 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1344/** Raw PKCS#1 v1.5 signature.
1345 *
1346 * The input to this algorithm is the DigestInfo structure used by
1347 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1348 * steps 3&ndash;6.
1349 */
1350#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1351#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1352 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1353
Maulik Patel28659c42021-01-06 14:09:22 +00001354#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
Summer Qinf07cc312022-01-05 16:52:54 +08001355#define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t)0x06001300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001356/** RSA PSS signature with hashing.
1357 *
1358 * This is the signature scheme defined by RFC 8017
1359 * (PKCS#1: RSA Cryptography Specifications) under the name
1360 * RSASSA-PSS, with the message generation function MGF1, and with
1361 * a salt length equal to the length of the hash. The specified
1362 * hash algorithm is used to hash the input message, to create the
1363 * salted hash, and for the mask generation.
1364 *
1365 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1366 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1367 * This includes #PSA_ALG_ANY_HASH
1368 * when specifying the algorithm in a usage policy.
1369 *
1370 * \return The corresponding RSA PSS signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001371 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001372 * hash algorithm.
1373 */
1374#define PSA_ALG_RSA_PSS(hash_alg) \
1375 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Summer Qinf07cc312022-01-05 16:52:54 +08001376
1377/** RSA PSS signature with hashing with relaxed verification.
1378 *
1379 * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1380 * but allows an arbitrary salt length (including \c 0) when verifying a
1381 * signature.
1382 *
1383 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1384 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1385 * This includes #PSA_ALG_ANY_HASH
1386 * when specifying the algorithm in a usage policy.
1387 *
1388 * \return The corresponding RSA PSS signature algorithm.
1389 * \return Unspecified if \p hash_alg is not a supported
1390 * hash algorithm.
1391 */
1392#define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \
1393 (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1394
1395/** Whether the specified algorithm is RSA PSS with standard salt.
1396 *
1397 * \param alg An algorithm value or an algorithm policy wildcard.
1398 *
1399 * \return 1 if \p alg is of the form
1400 * #PSA_ALG_RSA_PSS(\c hash_alg),
1401 * where \c hash_alg is a hash algorithm or
1402 * #PSA_ALG_ANY_HASH. 0 otherwise.
1403 * This macro may return either 0 or 1 if \p alg is not
1404 * a supported algorithm identifier or policy.
1405 */
1406#define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001407 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1408
Summer Qinf07cc312022-01-05 16:52:54 +08001409/** Whether the specified algorithm is RSA PSS with any salt.
1410 *
1411 * \param alg An algorithm value or an algorithm policy wildcard.
1412 *
1413 * \return 1 if \p alg is of the form
1414 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1415 * where \c hash_alg is a hash algorithm or
1416 * #PSA_ALG_ANY_HASH. 0 otherwise.
1417 * This macro may return either 0 or 1 if \p alg is not
1418 * a supported algorithm identifier or policy.
1419 */
1420#define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \
1421 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1422
1423/** Whether the specified algorithm is RSA PSS.
1424 *
1425 * This includes any of the RSA PSS algorithm variants, regardless of the
1426 * constraints on salt length.
1427 *
1428 * \param alg An algorithm value or an algorithm policy wildcard.
1429 *
1430 * \return 1 if \p alg is of the form
1431 * #PSA_ALG_RSA_PSS(\c hash_alg) or
1432 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1433 * where \c hash_alg is a hash algorithm or
1434 * #PSA_ALG_ANY_HASH. 0 otherwise.
1435 * This macro may return either 0 or 1 if \p alg is not
1436 * a supported algorithm identifier or policy.
1437 */
1438#define PSA_ALG_IS_RSA_PSS(alg) \
1439 (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \
1440 PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
1441
Maulik Patel28659c42021-01-06 14:09:22 +00001442#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001443/** ECDSA signature with hashing.
1444 *
1445 * This is the ECDSA signature scheme defined by ANSI X9.62,
1446 * with a random per-message secret number (*k*).
1447 *
1448 * The representation of the signature as a byte string consists of
1449 * the concatentation of the signature values *r* and *s*. Each of
1450 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1451 * of the base point of the curve in octets. Each value is represented
1452 * in big-endian order (most significant octet first).
1453 *
1454 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1455 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1456 * This includes #PSA_ALG_ANY_HASH
1457 * when specifying the algorithm in a usage policy.
1458 *
1459 * \return The corresponding ECDSA signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001460 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001461 * hash algorithm.
1462 */
1463#define PSA_ALG_ECDSA(hash_alg) \
1464 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1465/** ECDSA signature without hashing.
1466 *
1467 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1468 * without specifying a hash algorithm. This algorithm may only be
1469 * used to sign or verify a sequence of bytes that should be an
1470 * already-calculated hash. Note that the input is padded with
1471 * zeros on the left or truncated on the left as required to fit
1472 * the curve size.
1473 */
1474#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
Maulik Patel28659c42021-01-06 14:09:22 +00001475#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001476/** Deterministic ECDSA signature with hashing.
1477 *
1478 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1479 *
1480 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1481 *
1482 * Note that when this algorithm is used for verification, signatures
1483 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1484 * same private key are accepted. In other words,
1485 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1486 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1487 *
1488 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1489 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1490 * This includes #PSA_ALG_ANY_HASH
1491 * when specifying the algorithm in a usage policy.
1492 *
1493 * \return The corresponding deterministic ECDSA signature
1494 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001495 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001496 * hash algorithm.
1497 */
1498#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1499 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Maulik Patel28659c42021-01-06 14:09:22 +00001500#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001501#define PSA_ALG_IS_ECDSA(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001502 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001503 PSA_ALG_ECDSA_BASE)
1504#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001505 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001506#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1507 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1508#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1509 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1510
Summer Qin359167d2021-07-05 18:11:50 +08001511/** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1512 * using standard parameters.
1513 *
1514 * Contexts are not supported in the current version of this specification
1515 * because there is no suitable signature interface that can take the
1516 * context as a parameter. A future version of this specification may add
1517 * suitable functions and extend this algorithm to support contexts.
1518 *
1519 * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1520 * In this specification, the following curves are supported:
1521 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1522 * in RFC 8032.
1523 * The curve is Edwards25519.
1524 * The hash function used internally is SHA-512.
1525 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1526 * in RFC 8032.
1527 * The curve is Edwards448.
1528 * The hash function used internally is the first 114 bytes of the
1529 * SHAKE256 output.
1530 *
1531 * This algorithm can be used with psa_sign_message() and
1532 * psa_verify_message(). Since there is no prehashing, it cannot be used
1533 * with psa_sign_hash() or psa_verify_hash().
1534 *
1535 * The signature format is the concatenation of R and S as defined by
1536 * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1537 * string for Ed448).
1538 */
1539#define PSA_ALG_PURE_EDDSA ((psa_algorithm_t)0x06000800)
1540
1541#define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t)0x06000900)
1542#define PSA_ALG_IS_HASH_EDDSA(alg) \
1543 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1544
1545/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1546 * using SHA-512 and the Edwards25519 curve.
1547 *
1548 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1549 *
1550 * This algorithm is Ed25519 as specified in RFC 8032.
1551 * The curve is Edwards25519.
1552 * The prehash is SHA-512.
1553 * The hash function used internally is SHA-512.
1554 *
1555 * This is a hash-and-sign algorithm: to calculate a signature,
1556 * you can either:
1557 * - call psa_sign_message() on the message;
1558 * - or calculate the SHA-512 hash of the message
1559 * with psa_hash_compute()
1560 * or with a multi-part hash operation started with psa_hash_setup(),
1561 * using the hash algorithm #PSA_ALG_SHA_512,
1562 * then sign the calculated hash with psa_sign_hash().
1563 * Verifying a signature is similar, using psa_verify_message() or
1564 * psa_verify_hash() instead of the signature function.
1565 */
1566#define PSA_ALG_ED25519PH \
1567 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1568
1569/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1570 * using SHAKE256 and the Edwards448 curve.
1571 *
1572 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1573 *
1574 * This algorithm is Ed448 as specified in RFC 8032.
1575 * The curve is Edwards448.
1576 * The prehash is the first 64 bytes of the SHAKE256 output.
1577 * The hash function used internally is the first 114 bytes of the
1578 * SHAKE256 output.
1579 *
1580 * This is a hash-and-sign algorithm: to calculate a signature,
1581 * you can either:
1582 * - call psa_sign_message() on the message;
1583 * - or calculate the first 64 bytes of the SHAKE256 output of the message
1584 * with psa_hash_compute()
1585 * or with a multi-part hash operation started with psa_hash_setup(),
1586 * using the hash algorithm #PSA_ALG_SHAKE256_512,
1587 * then sign the calculated hash with psa_sign_hash().
1588 * Verifying a signature is similar, using psa_verify_message() or
1589 * psa_verify_hash() instead of the signature function.
1590 */
1591#define PSA_ALG_ED448PH \
1592 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1593
1594/* Default definition, to be overridden if the library is extended with
1595 * more hash-and-sign algorithms that we want to keep out of this header
1596 * file. */
1597#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1598
Summer Qinf07cc312022-01-05 16:52:54 +08001599/** Whether the specified algorithm is a signature algorithm that can be used
1600 * with psa_sign_hash() and psa_verify_hash().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001601 *
Summer Qinf07cc312022-01-05 16:52:54 +08001602 * This encompasses all strict hash-and-sign algorithms categorized by
1603 * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1604 * paradigm more loosely:
1605 * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1606 * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001607 *
Summer Qinf07cc312022-01-05 16:52:54 +08001608 * \param alg An algorithm identifier (value of type psa_algorithm_t).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001609 *
Summer Qinf07cc312022-01-05 16:52:54 +08001610 * \return 1 if alg is a signature algorithm that can be used to sign a
1611 * hash. 0 if alg is a signature algorithm that can only be used
1612 * to sign a message. 0 if alg is not a signature algorithm.
1613 * This macro can return either 0 or 1 if alg is not a
1614 * supported algorithm identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001615 */
Summer Qinf07cc312022-01-05 16:52:54 +08001616#define PSA_ALG_IS_SIGN_HASH(alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001617 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
Summer Qin359167d2021-07-05 18:11:50 +08001618 PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
1619 PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1620
1621/** Whether the specified algorithm is a signature algorithm that can be used
1622 * with psa_sign_message() and psa_verify_message().
1623 *
1624 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1625 *
1626 * \return 1 if alg is a signature algorithm that can be used to sign a
1627 * message. 0 if \p alg is a signature algorithm that can only be used
1628 * to sign an already-calculated hash. 0 if \p alg is not a signature
1629 * algorithm. This macro can return either 0 or 1 if \p alg is not a
1630 * supported algorithm identifier.
1631 */
1632#define PSA_ALG_IS_SIGN_MESSAGE(alg) \
Summer Qinf07cc312022-01-05 16:52:54 +08001633 (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
1634
1635/** Whether the specified algorithm is a hash-and-sign algorithm.
1636 *
1637 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1638 * structured in two parts: first the calculation of a hash in a way that
1639 * does not depend on the key, then the calculation of a signature from the
1640 * hash value and the key. Hash-and-sign algorithms encode the hash
1641 * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1642 * to extract this algorithm.
1643 *
1644 * Thus, for a hash-and-sign algorithm,
1645 * `psa_sign_message(key, alg, input, ...)` is equivalent to
1646 * ```
1647 * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1648 * psa_sign_hash(key, alg, hash, ..., signature, ...);
1649 * ```
1650 * Most usefully, separating the hash from the signature allows the hash
1651 * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1652 * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1653 * calculating the hash and then calling psa_verify_hash().
1654 *
1655 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1656 *
1657 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1658 * This macro may return either 0 or 1 if \p alg is not a supported
1659 * algorithm identifier.
1660 */
1661#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1662 (PSA_ALG_IS_SIGN_HASH(alg) && \
1663 ((alg) & PSA_ALG_HASH_MASK) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001664
1665/** Get the hash used by a hash-and-sign signature algorithm.
1666 *
1667 * A hash-and-sign algorithm is a signature algorithm which is
1668 * composed of two phases: first a hashing phase which does not use
1669 * the key and produces a hash of the input message, then a signing
1670 * phase which only uses the hash and the key and not the message
1671 * itself.
1672 *
1673 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1674 * #PSA_ALG_IS_SIGN(\p alg) is true).
1675 *
1676 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1677 * algorithm.
1678 * \return 0 if \p alg is a signature algorithm that does not
1679 * follow the hash-and-sign structure.
1680 * \return Unspecified if \p alg is not a signature algorithm or
1681 * if it is not supported by the implementation.
1682 */
1683#define PSA_ALG_SIGN_GET_HASH(alg) \
1684 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001685 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1686 0)
1687
1688/** RSA PKCS#1 v1.5 encryption.
1689 */
Maulik Patel28659c42021-01-06 14:09:22 +00001690#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001691
Maulik Patel28659c42021-01-06 14:09:22 +00001692#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001693/** RSA OAEP encryption.
1694 *
1695 * This is the encryption scheme defined by RFC 8017
1696 * (PKCS#1: RSA Cryptography Specifications) under the name
1697 * RSAES-OAEP, with the message generation function MGF1.
1698 *
1699 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1700 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1701 * for MGF1.
1702 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001703 * \return The corresponding RSA OAEP encryption algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001704 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001705 * hash algorithm.
1706 */
1707#define PSA_ALG_RSA_OAEP(hash_alg) \
1708 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1709#define PSA_ALG_IS_RSA_OAEP(alg) \
1710 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1711#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1712 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1713 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1714 0)
1715
Maulik Patel28659c42021-01-06 14:09:22 +00001716#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001717/** Macro to build an HKDF algorithm.
1718 *
1719 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1720 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001721 * This key derivation algorithm uses the following inputs:
1722 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1723 * It is optional; if omitted, the derivation uses an empty salt.
1724 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1725 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1726 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1727 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1728 * starting to generate output.
1729 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001730 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1731 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1732 *
1733 * \return The corresponding HKDF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001734 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001735 * hash algorithm.
1736 */
1737#define PSA_ALG_HKDF(hash_alg) \
1738 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1739/** Whether the specified algorithm is an HKDF algorithm.
1740 *
1741 * HKDF is a family of key derivation algorithms that are based on a hash
1742 * function and the HMAC construction.
1743 *
1744 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1745 *
1746 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1747 * This macro may return either 0 or 1 if \c alg is not a supported
1748 * key derivation algorithm identifier.
1749 */
1750#define PSA_ALG_IS_HKDF(alg) \
1751 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1752#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1753 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1754
Maulik Patel28659c42021-01-06 14:09:22 +00001755#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001756/** Macro to build a TLS-1.2 PRF algorithm.
1757 *
1758 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1759 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1760 * used with either SHA-256 or SHA-384.
1761 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001762 * This key derivation algorithm uses the following inputs, which must be
1763 * passed in the order given here:
1764 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1765 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1766 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1767 *
1768 * For the application to TLS-1.2 key expansion, the seed is the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001769 * concatenation of ServerHello.Random + ClientHello.Random,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001770 * and the label is "key expansion".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001771 *
1772 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1773 * TLS 1.2 PRF using HMAC-SHA-256.
1774 *
1775 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1776 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1777 *
1778 * \return The corresponding TLS-1.2 PRF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001779 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001780 * hash algorithm.
1781 */
1782#define PSA_ALG_TLS12_PRF(hash_alg) \
1783 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1784
1785/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1786 *
1787 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1788 *
1789 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1790 * This macro may return either 0 or 1 if \c alg is not a supported
1791 * key derivation algorithm identifier.
1792 */
1793#define PSA_ALG_IS_TLS12_PRF(alg) \
1794 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1795#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1796 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1797
Maulik Patel28659c42021-01-06 14:09:22 +00001798#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001799/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1800 *
1801 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1802 * from the PreSharedKey (PSK) through the application of padding
1803 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1804 * The latter is based on HMAC and can be used with either SHA-256
1805 * or SHA-384.
1806 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001807 * This key derivation algorithm uses the following inputs, which must be
1808 * passed in the order given here:
1809 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1810 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1811 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1812 *
1813 * For the application to TLS-1.2, the seed (which is
1814 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1815 * ClientHello.Random + ServerHello.Random,
1816 * and the label is "master secret" or "extended master secret".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001817 *
1818 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1819 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1820 *
1821 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1822 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1823 *
1824 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001825 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001826 * hash algorithm.
1827 */
1828#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1829 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1830
1831/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1832 *
1833 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1834 *
1835 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1836 * This macro may return either 0 or 1 if \c alg is not a supported
1837 * key derivation algorithm identifier.
1838 */
1839#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1840 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1841#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1842 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1843
Summer Qin359167d2021-07-05 18:11:50 +08001844/* This flag indicates whether the key derivation algorithm is suitable for
1845 * use on low-entropy secrets such as password - these algorithms are also
1846 * known as key stretching or password hashing schemes. These are also the
1847 * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
1848 *
1849 * Those algorithms cannot be combined with a key agreement algorithm.
1850 */
1851#define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t)0x00800000)
1852
1853#define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t)0x08800100)
1854/** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
1855 *
1856 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
1857 * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
1858 * HMAC with the specified hash.
1859 * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA256)` specifies PBKDF2
1860 * using the PRF HMAC-SHA-256.
1861 *
1862 * This key derivation algorithm uses the following inputs, which must be
1863 * provided in the following order:
1864 * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
1865 * This input step must be used exactly once.
1866 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1867 * This input step must be used one or more times; if used several times, the
1868 * inputs will be concatenated. This can be used to build the final salt
1869 * from multiple sources, both public and secret (also known as pepper).
1870 * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
1871 * This input step must be used exactly once.
1872 *
1873 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1874 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1875 *
1876 * \return The corresponding PBKDF2-HMAC-XXX algorithm.
1877 * \return Unspecified if \p hash_alg is not a supported
1878 * hash algorithm.
1879 */
1880#define PSA_ALG_PBKDF2_HMAC(hash_alg) \
1881 (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1882
1883/** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
1884 *
1885 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1886 *
1887 * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
1888 * This macro may return either 0 or 1 if \c alg is not a supported
1889 * key derivation algorithm identifier.
1890 */
1891#define PSA_ALG_IS_PBKDF2_HMAC(alg) \
1892 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
1893
1894/** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
1895 *
1896 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
1897 * This macro specifies the PBKDF2 algorithm constructed using the
1898 * AES-CMAC-PRF-128 PRF specified by RFC 4615.
1899 *
1900 * This key derivation algorithm uses the same inputs as
1901 * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
1902 */
1903#define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t)0x08800200)
1904
Maulik Patel28659c42021-01-06 14:09:22 +00001905#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
1906#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001907
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001908/** Macro to build a combined algorithm that chains a key agreement with
1909 * a key derivation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001910 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001911 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1912 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1913 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1914 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001915 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001916 * \return The corresponding key agreement and derivation
1917 * algorithm.
1918 * \return Unspecified if \p ka_alg is not a supported
1919 * key agreement algorithm or \p kdf_alg is not a
1920 * supported key derivation algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001921 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001922#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1923 ((ka_alg) | (kdf_alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001924
1925#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1926 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1927
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001928#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1929 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001930
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001931/** Whether the specified algorithm is a raw key agreement algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001932 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001933 * A raw key agreement algorithm is one that does not specify
1934 * a key derivation function.
1935 * Usually, raw key agreement algorithms are constructed directly with
1936 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
Maulik Patel28659c42021-01-06 14:09:22 +00001937 * constructed with #PSA_ALG_KEY_AGREEMENT().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001938 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001939 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1940 *
1941 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1942 * This macro may return either 0 or 1 if \p alg is not a supported
1943 * algorithm identifier.
1944 */
1945#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1946 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1947 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1948
1949#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1950 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1951
1952/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1953 *
1954 * The shared secret produced by key agreement is
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001955 * `g^{ab}` in big-endian format.
1956 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1957 * in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001958 */
Maulik Patel28659c42021-01-06 14:09:22 +00001959#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001960
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001961/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1962 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001963 * This includes the raw finite field Diffie-Hellman algorithm as well as
1964 * finite-field Diffie-Hellman followed by any supporter key derivation
1965 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001966 *
1967 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1968 *
1969 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1970 * This macro may return either 0 or 1 if \c alg is not a supported
1971 * key agreement algorithm identifier.
1972 */
1973#define PSA_ALG_IS_FFDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001974 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001975
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001976/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1977 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001978 * The shared secret produced by key agreement is the x-coordinate of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001979 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1980 * `m` is the bit size associated with the curve, i.e. the bit size of the
1981 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1982 * the byte containing the most significant bit of the shared secret
1983 * is padded with zero bits. The byte order is either little-endian
1984 * or big-endian depending on the curve type.
1985 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08001986 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001987 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1988 * in little-endian byte order.
1989 * The bit size is 448 for Curve448 and 255 for Curve25519.
1990 * - For Weierstrass curves over prime fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08001991 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001992 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1993 * in big-endian byte order.
1994 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1995 * - For Weierstrass curves over binary fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08001996 * `PSA_ECC_FAMILY_SECTXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001997 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1998 * in big-endian byte order.
1999 * The bit size is `m` for the field `F_{2^m}`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002000 */
Maulik Patel28659c42021-01-06 14:09:22 +00002001#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002002
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002003/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2004 * algorithm.
2005 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002006 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2007 * elliptic curve Diffie-Hellman followed by any supporter key derivation
2008 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002009 *
2010 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2011 *
2012 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2013 * 0 otherwise.
2014 * This macro may return either 0 or 1 if \c alg is not a supported
2015 * key agreement algorithm identifier.
2016 */
2017#define PSA_ALG_IS_ECDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002018 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002019
2020/** Whether the specified algorithm encoding is a wildcard.
2021 *
2022 * Wildcard values may only be used to set the usage algorithm field in
2023 * a policy, not to perform an operation.
2024 *
2025 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2026 *
2027 * \return 1 if \c alg is a wildcard algorithm encoding.
2028 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2029 * an operation).
2030 * \return This macro may return either 0 or 1 if \c alg is not a supported
2031 * algorithm identifier.
2032 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01002033#define PSA_ALG_IS_WILDCARD(alg) \
2034 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
2035 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
2036 PSA_ALG_IS_MAC(alg) ? \
2037 (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
2038 PSA_ALG_IS_AEAD(alg) ? \
2039 (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002040 (alg) == PSA_ALG_ANY_HASH)
2041
Summer Qin359167d2021-07-05 18:11:50 +08002042/** Get the hash used by a composite algorithm.
2043 *
2044 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2045 *
2046 * \return The underlying hash algorithm if alg is a composite algorithm that
2047 * uses a hash algorithm.
2048 *
2049 * \return \c 0 if alg is not a composite algorithm that uses a hash.
2050 */
2051#define PSA_ALG_GET_HASH(alg) \
2052 (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t)0) : 0x02000000 | ((alg) & 0x000000ff))
2053
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002054/**@}*/
2055
2056/** \defgroup key_lifetimes Key lifetimes
2057 * @{
2058 */
2059
Soby Mathew07ef6e42020-07-20 21:09:23 +01002060/** The default lifetime for volatile keys.
2061 *
Maulik Patel28659c42021-01-06 14:09:22 +00002062 * A volatile key only exists as long as the identifier to it is not destroyed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002063 * The key material is guaranteed to be erased on a power reset.
Soby Mathew07ef6e42020-07-20 21:09:23 +01002064 *
2065 * A key with this lifetime is typically stored in the RAM area of the
2066 * PSA Crypto subsystem. However this is an implementation choice.
2067 * If an implementation stores data about the key in a non-volatile memory,
2068 * it must release all the resources associated with the key and erase the
2069 * key material if the calling application terminates.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002070 */
2071#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
2072
Soby Mathew07ef6e42020-07-20 21:09:23 +01002073/** The default lifetime for persistent keys.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002074 *
2075 * A persistent key remains in storage until it is explicitly destroyed or
2076 * until the corresponding storage area is wiped. This specification does
Maulik Patel13b27cf2021-05-14 11:44:53 +01002077 * not define any mechanism to wipe a storage area, but integrations may
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002078 * provide their own mechanism (for example to perform a factory reset,
2079 * to prepare for device refurbishment, or to uninstall an application).
2080 *
2081 * This lifetime value is the default storage area for the calling
Maulik Patel13b27cf2021-05-14 11:44:53 +01002082 * application. Integrations of Mbed TLS may support other persistent lifetimes.
Soby Mathew07ef6e42020-07-20 21:09:23 +01002083 * See ::psa_key_lifetime_t for more information.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002084 */
2085#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
2086
Soby Mathew07ef6e42020-07-20 21:09:23 +01002087/** The persistence level of volatile keys.
2088 *
2089 * See ::psa_key_persistence_t for more information.
2090 */
2091#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
2092
2093/** The default persistence level for persistent keys.
2094 *
2095 * See ::psa_key_persistence_t for more information.
2096 */
2097#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
2098
2099/** A persistence level indicating that a key is never destroyed.
2100 *
2101 * See ::psa_key_persistence_t for more information.
2102 */
2103#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
2104
2105#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
2106 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
2107
2108#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
2109 ((psa_key_location_t)((lifetime) >> 8))
2110
2111/** Whether a key lifetime indicates that the key is volatile.
2112 *
2113 * A volatile key is automatically destroyed by the implementation when
2114 * the application instance terminates. In particular, a volatile key
2115 * is automatically destroyed on a power reset of the device.
2116 *
2117 * A key that is not volatile is persistent. Persistent keys are
2118 * preserved until the application explicitly destroys them or until an
2119 * implementation-specific device management event occurs (for example,
2120 * a factory reset).
2121 *
2122 * \param lifetime The lifetime value to query (value of type
2123 * ::psa_key_lifetime_t).
2124 *
2125 * \return \c 1 if the key is volatile, otherwise \c 0.
2126 */
2127#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
2128 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
Summer Qin0e5b2e02020-10-22 11:23:39 +08002129 PSA_KEY_PERSISTENCE_VOLATILE)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002130
Summer Qin359167d2021-07-05 18:11:50 +08002131/** Whether a key lifetime indicates that the key is read-only.
2132 *
2133 * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2134 * They must be created through platform-specific means that bypass the API.
2135 *
2136 * Some platforms may offer ways to destroy read-only keys. For example,
2137 * consider a platform with multiple levels of privilege, where a
2138 * low-privilege application can use a key but is not allowed to destroy
2139 * it, and the platform exposes the key to the application with a read-only
2140 * lifetime. High-privilege code can destroy the key even though the
2141 * application sees the key as read-only.
2142 *
2143 * \param lifetime The lifetime value to query (value of type
2144 * ::psa_key_lifetime_t).
2145 *
2146 * \return \c 1 if the key is read-only, otherwise \c 0.
2147 */
2148#define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
2149 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2150 PSA_KEY_PERSISTENCE_READ_ONLY)
2151
Soby Mathew07ef6e42020-07-20 21:09:23 +01002152/** Construct a lifetime from a persistence level and a location.
2153 *
2154 * \param persistence The persistence level
2155 * (value of type ::psa_key_persistence_t).
2156 * \param location The location indicator
2157 * (value of type ::psa_key_location_t).
2158 *
2159 * \return The constructed lifetime value.
2160 */
2161#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2162 ((location) << 8 | (persistence))
2163
2164/** The local storage area for persistent keys.
2165 *
2166 * This storage area is available on all systems that can store persistent
2167 * keys without delegating the storage to a third-party cryptoprocessor.
2168 *
2169 * See ::psa_key_location_t for more information.
2170 */
2171#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
2172
2173#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
2174
Summer Qinf07cc312022-01-05 16:52:54 +08002175/** The null key identifier.
2176 */
2177#define PSA_KEY_ID_NULL ((psa_key_id_t)0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002178/** The minimum value for a key identifier chosen by the application.
2179 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002180#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002181/** The maximum value for a key identifier chosen by the application.
2182 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002183#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002184/** The minimum value for a key identifier chosen by the implementation.
2185 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002186#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002187/** The maximum value for a key identifier chosen by the implementation.
2188 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002189#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002190
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002191/**@}*/
2192
2193/** \defgroup policy Key policies
2194 * @{
2195 */
2196
2197/** Whether the key may be exported.
2198 *
2199 * A public key or the public part of a key pair may always be exported
2200 * regardless of the value of this permission flag.
2201 *
2202 * If a key does not have export permission, implementations shall not
2203 * allow the key to be exported in plain form from the cryptoprocessor,
2204 * whether through psa_export_key() or through a proprietary interface.
2205 * The key may however be exportable in a wrapped form, i.e. in a form
2206 * where it is encrypted by another key.
2207 */
2208#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
2209
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002210/** Whether the key may be copied.
2211 *
2212 * This flag allows the use of psa_copy_key() to make a copy of the key
2213 * with the same policy or a more restrictive policy.
2214 *
2215 * For lifetimes for which the key is located in a secure element which
2216 * enforce the non-exportability of keys, copying a key outside the secure
2217 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2218 * Copying the key inside the secure element is permitted with just
2219 * #PSA_KEY_USAGE_COPY if the secure element supports it.
2220 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2221 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2222 * is sufficient to permit the copy.
2223 */
2224#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
2225
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002226/** Whether the key may be used to encrypt a message.
2227 *
2228 * This flag allows the key to be used for a symmetric encryption operation,
2229 * for an AEAD encryption-and-authentication operation,
2230 * or for an asymmetric encryption operation,
2231 * if otherwise permitted by the key's type and policy.
2232 *
2233 * For a key pair, this concerns the public key.
2234 */
2235#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
2236
2237/** Whether the key may be used to decrypt a message.
2238 *
2239 * This flag allows the key to be used for a symmetric decryption operation,
2240 * for an AEAD decryption-and-verification operation,
2241 * or for an asymmetric decryption operation,
2242 * if otherwise permitted by the key's type and policy.
2243 *
2244 * For a key pair, this concerns the private key.
2245 */
2246#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
2247
2248/** Whether the key may be used to sign a message.
2249 *
Summer Qin359167d2021-07-05 18:11:50 +08002250 * This flag allows the key to be used for a MAC calculation operation or for
2251 * an asymmetric message signature operation, if otherwise permitted by the
2252 * key’s type and policy.
2253 *
2254 * For a key pair, this concerns the private key.
2255 */
2256#define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t)0x00000400)
2257
2258/** Whether the key may be used to verify a message.
2259 *
2260 * This flag allows the key to be used for a MAC verification operation or for
2261 * an asymmetric message signature verification operation, if otherwise
2262 * permitted by the key’s type and policy.
2263 *
2264 * For a key pair, this concerns the public key.
2265 */
2266#define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t)0x00000800)
2267
2268/** Whether the key may be used to sign a message.
2269 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002270 * This flag allows the key to be used for a MAC calculation operation
2271 * or for an asymmetric signature operation,
2272 * if otherwise permitted by the key's type and policy.
2273 *
2274 * For a key pair, this concerns the private key.
2275 */
Maulik Patel28659c42021-01-06 14:09:22 +00002276#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002277
2278/** Whether the key may be used to verify a message signature.
2279 *
2280 * This flag allows the key to be used for a MAC verification operation
2281 * or for an asymmetric signature verification operation,
2282 * if otherwise permitted by by the key's type and policy.
2283 *
2284 * For a key pair, this concerns the public key.
2285 */
Maulik Patel28659c42021-01-06 14:09:22 +00002286#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002287
Summer Qin359167d2021-07-05 18:11:50 +08002288/** Whether the key may be used to derive other keys or produce a password
2289 * hash.
2290 *
2291 * This flag allows the key to be used for a key derivation operation or for
2292 * a key agreement operation, if otherwise permitted by by the key's type and
2293 * policy.
2294 *
2295 * If this flag is present on all keys used in calls to
2296 * psa_key_derivation_input_key() for a key derivation operation, then it
2297 * permits calling psa_key_derivation_output_bytes() or
2298 * psa_key_derivation_output_key() at the end of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002299 */
Maulik Patel28659c42021-01-06 14:09:22 +00002300#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002301
Summer Qin359167d2021-07-05 18:11:50 +08002302/** Whether the key may be used to verify the result of a key derivation,
2303 * including password hashing.
2304 *
2305 * This flag allows the key to be used:
2306 *
2307 * This flag allows the key to be used in a key derivation operation, if
2308 * otherwise permitted by by the key's type and policy.
2309 *
2310 * If this flag is present on all keys used in calls to
2311 * psa_key_derivation_input_key() for a key derivation operation, then it
2312 * permits calling psa_key_derivation_verify_bytes() or
2313 * psa_key_derivation_verify_key() at the end of the operation.
2314 */
2315#define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t)0x00008000)
2316
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002317/**@}*/
2318
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002319/** \defgroup derivation Key derivation
2320 * @{
2321 */
2322
2323/** A secret input for key derivation.
2324 *
2325 * This should be a key of type #PSA_KEY_TYPE_DERIVE
2326 * (passed to psa_key_derivation_input_key())
2327 * or the shared secret resulting from a key agreement
2328 * (obtained via psa_key_derivation_key_agreement()).
2329 *
2330 * The secret can also be a direct input (passed to
2331 * key_derivation_input_bytes()). In this case, the derivation operation
2332 * may not be used to derive keys: the operation will only allow
Summer Qin359167d2021-07-05 18:11:50 +08002333 * psa_key_derivation_output_bytes(),
2334 * psa_key_derivation_verify_bytes(), or
2335 * psa_key_derivation_verify_key(), but not
2336 * psa_key_derivation_output_key().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002337 */
2338#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
2339
Summer Qin359167d2021-07-05 18:11:50 +08002340/** A low-entropy secret input for password hashing / key stretching.
2341 *
2342 * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2343 * psa_key_derivation_input_key()) or a direct input (passed to
2344 * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2345 * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2346 * the shared secret resulting from a key agreement.
2347 *
2348 * The secret can also be a direct input (passed to
2349 * key_derivation_input_bytes()). In this case, the derivation operation
2350 * may not be used to derive keys: the operation will only allow
2351 * psa_key_derivation_output_bytes(),
2352 * psa_key_derivation_verify_bytes(), or
2353 * psa_key_derivation_verify_key(), but not
2354 * psa_key_derivation_output_key().
2355 */
2356#define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t)0x0102)
2357
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002358/** A label for key derivation.
2359 *
2360 * This should be a direct input.
2361 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2362 */
2363#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
2364
2365/** A salt for key derivation.
2366 *
2367 * This should be a direct input.
Summer Qin359167d2021-07-05 18:11:50 +08002368 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2369 * #PSA_KEY_TYPE_PEPPER.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002370 */
2371#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
2372
2373/** An information string for key derivation.
2374 *
2375 * This should be a direct input.
2376 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2377 */
2378#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
2379
2380/** A seed for key derivation.
2381 *
2382 * This should be a direct input.
2383 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2384 */
2385#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
2386
Summer Qin359167d2021-07-05 18:11:50 +08002387/** A cost parameter for password hashing / key stretching.
2388 *
2389 * This must be a direct input, passed to psa_key_derivation_input_integer().
2390 */
2391#define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t)0x0205)
2392
2393/**@}*/
2394
2395/** \defgroup helper_macros Helper macros
2396 * @{
2397 */
2398
2399/* Helper macros */
2400
2401/** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2402 * regardless of the tag length they encode.
2403 *
2404 * \param aead_alg_1 An AEAD algorithm identifier.
2405 * \param aead_alg_2 An AEAD algorithm identifier.
2406 *
2407 * \return 1 if both identifiers refer to the same AEAD algorithm,
2408 * 0 otherwise.
2409 * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2410 * a supported AEAD algorithm.
2411 */
2412#define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2413 (!(((aead_alg_1) ^ (aead_alg_2)) & \
2414 ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2415
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002416/**@}*/
2417
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002418#endif /* PSA_CRYPTO_VALUES_H */