blob: dc8d81a869cee987a9cd73bea08fe1e3767b2be8 [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, 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
460/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
461 *
Summer Qin359167d2021-07-05 18:11:50 +0800462 * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
463 * 192 bits (3-key 3DES).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100464 *
465 * Note that single DES and 2-key 3DES are weak and strongly
466 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
467 * is weak and deprecated and should only be used in legacy protocols.
468 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100469#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100470
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100471/** Key for a cipher, AEAD or MAC algorithm based on the
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100472 * Camellia block cipher. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100473#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100474
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100475/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
476 *
477 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
478 *
479 * Implementations must support 12-byte nonces, may support 8-byte nonces,
480 * and should reject other sizes.
481 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100482#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100483
Summer Qin359167d2021-07-05 18:11:50 +0800484/** RSA public key.
485 *
486 * The size of an RSA key is the bit size of the modulus.
487 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100488#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
Summer Qin359167d2021-07-05 18:11:50 +0800489/** RSA key pair (private and public key).
490 *
491 * The size of an RSA key is the bit size of the modulus.
492 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100493#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100494/** Whether a key type is an RSA key (pair or public-only). */
495#define PSA_KEY_TYPE_IS_RSA(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100496 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100497
Soby Mathew07ef6e42020-07-20 21:09:23 +0100498#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
499#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
500#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100501/** Elliptic curve key pair.
502 *
Summer Qin359167d2021-07-05 18:11:50 +0800503 * The size of an elliptic curve key is the bit size associated with the curve,
504 * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
505 * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
506 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800507 * \param curve A value of type ::psa_ecc_family_t that
508 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100509 */
510#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
511 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
512/** Elliptic curve public key.
513 *
Summer Qin359167d2021-07-05 18:11:50 +0800514 * The size of an elliptic curve public key is the same as the corresponding
515 * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
516 * `PSA_ECC_FAMILY_xxx` curve families).
517 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800518 * \param curve A value of type ::psa_ecc_family_t that
519 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100520 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100521#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
522 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
523
524/** Whether a key type is an elliptic curve key (pair or public-only). */
525#define PSA_KEY_TYPE_IS_ECC(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100526 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100527 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
528/** Whether a key type is an elliptic curve key pair. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100529#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100530 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100531 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100532/** Whether a key type is an elliptic curve public key. */
533#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
534 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
535 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
536
537/** Extract the curve from an elliptic curve key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800538#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
539 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100540 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
541 0))
542
Soby Mathew07ef6e42020-07-20 21:09:23 +0100543/** SEC Koblitz curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100544 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100545 * This family comprises the following curves:
546 * secp192k1, secp224k1, secp256k1.
547 * They are defined in _Standards for Efficient Cryptography_,
548 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
549 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100550 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800551#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100552
Soby Mathew07ef6e42020-07-20 21:09:23 +0100553/** SEC random curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100554 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100555 * This family comprises the following curves:
556 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
557 * They are defined in _Standards for Efficient Cryptography_,
558 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
559 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100560 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800561#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100562/* SECP160R2 (SEC2 v1, obsolete) */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800563#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100564
Soby Mathew07ef6e42020-07-20 21:09:23 +0100565/** SEC Koblitz curves over binary fields.
566 *
567 * This family comprises the following curves:
568 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
569 * They are defined in _Standards for Efficient Cryptography_,
570 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
571 * https://www.secg.org/sec2-v2.pdf
572 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800573#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100574
575/** SEC random curves over binary fields.
576 *
577 * This family comprises the following curves:
578 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
579 * They are defined in _Standards for Efficient Cryptography_,
580 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
581 * https://www.secg.org/sec2-v2.pdf
582 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800583#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100584
585/** SEC additional random curves over binary fields.
586 *
587 * This family comprises the following curve:
588 * sect163r2.
589 * It is defined in _Standards for Efficient Cryptography_,
590 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
591 * https://www.secg.org/sec2-v2.pdf
592 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800593#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100594
595/** Brainpool P random curves.
596 *
597 * This family comprises the following curves:
598 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
599 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
600 * It is defined in RFC 5639.
601 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800602#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100603
604/** Curve25519 and Curve448.
605 *
606 * This family comprises the following Montgomery curves:
607 * - 255-bit: Bernstein et al.,
608 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
609 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
610 * - 448-bit: Hamburg,
611 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
612 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
613 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800614#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100615
Summer Qin359167d2021-07-05 18:11:50 +0800616/** The twisted Edwards curves Ed25519 and Ed448.
617 *
618 * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
619 * #PSA_ALG_ED25519PH for the 255-bit curve,
620 * #PSA_ALG_ED448PH for the 448-bit curve).
621 *
622 * This family comprises the following twisted Edwards curves:
623 * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
624 * to Curve25519.
625 * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
626 * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
627 * to Curve448.
628 * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
629 */
630#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
631
Soby Mathew07ef6e42020-07-20 21:09:23 +0100632#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
633#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
634#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100635/** Diffie-Hellman key pair.
636 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800637 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100638 * Diffie-Hellman group to be used.
639 */
640#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
641 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
642/** Diffie-Hellman public key.
643 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800644 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100645 * Diffie-Hellman group to be used.
646 */
647#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
648 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
649
650/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
651#define PSA_KEY_TYPE_IS_DH(type) \
652 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
653 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
654/** Whether a key type is a Diffie-Hellman key pair. */
655#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
656 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
657 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
658/** Whether a key type is a Diffie-Hellman public key. */
659#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
660 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
661 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
662
663/** Extract the group from a Diffie-Hellman key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800664#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
665 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100666 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
667 0))
668
Soby Mathew07ef6e42020-07-20 21:09:23 +0100669/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100670 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100671 * This family includes groups with the following key sizes (in bits):
672 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
673 * all of these sizes or only a subset.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100674 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800675#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100676
Soby Mathew07ef6e42020-07-20 21:09:23 +0100677#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
678 (((type) >> 8) & 7)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100679/** The block size of a block cipher.
680 *
681 * \param type A cipher key type (value of type #psa_key_type_t).
682 *
683 * \return The block size for a block cipher, or 1 for a stream cipher.
684 * The return value is undefined if \p type is not a supported
685 * cipher key type.
686 *
687 * \note It is possible to build stream cipher algorithms on top of a block
688 * cipher, for example CTR mode (#PSA_ALG_CTR).
689 * This macro only takes the key type into account, so it cannot be
690 * used to determine the size of the data that #psa_cipher_update()
691 * might buffer for future processing in general.
692 *
693 * \note This macro returns a compile-time constant if its argument is one.
694 *
695 * \warning This macro may evaluate its argument multiple times.
696 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100697#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100698 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100699 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100700 0u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100701
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100702/** Vendor-defined algorithm flag.
703 *
704 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
705 * bit set. Vendors who define additional algorithms must use an encoding with
706 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
707 * used by standard encodings whenever practical.
708 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100709#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100710
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100711#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000712#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
713#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100714#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000715#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
716#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
717#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
718#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
719#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100720
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100721/** Whether an algorithm is vendor-defined.
722 *
723 * See also #PSA_ALG_VENDOR_FLAG.
724 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100725#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
726 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
727
728/** Whether the specified algorithm is a hash algorithm.
729 *
730 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
731 *
732 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
733 * This macro may return either 0 or 1 if \p alg is not a supported
734 * algorithm identifier.
735 */
736#define PSA_ALG_IS_HASH(alg) \
737 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
738
739/** Whether the specified algorithm is a MAC algorithm.
740 *
741 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
742 *
743 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
744 * This macro may return either 0 or 1 if \p alg is not a supported
745 * algorithm identifier.
746 */
747#define PSA_ALG_IS_MAC(alg) \
748 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
749
750/** Whether the specified algorithm is a symmetric cipher algorithm.
751 *
752 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
753 *
754 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
755 * This macro may return either 0 or 1 if \p alg is not a supported
756 * algorithm identifier.
757 */
758#define PSA_ALG_IS_CIPHER(alg) \
759 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
760
761/** Whether the specified algorithm is an authenticated encryption
762 * with associated data (AEAD) algorithm.
763 *
764 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
765 *
766 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
767 * This macro may return either 0 or 1 if \p alg is not a supported
768 * algorithm identifier.
769 */
770#define PSA_ALG_IS_AEAD(alg) \
771 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
772
Soby Mathew07ef6e42020-07-20 21:09:23 +0100773/** Whether the specified algorithm is an asymmetric signature algorithm,
774 * also known as public-key signature algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100775 *
776 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
777 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100778 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100779 * This macro may return either 0 or 1 if \p alg is not a supported
780 * algorithm identifier.
781 */
782#define PSA_ALG_IS_SIGN(alg) \
783 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
784
Soby Mathew07ef6e42020-07-20 21:09:23 +0100785/** Whether the specified algorithm is an asymmetric encryption algorithm,
786 * also known as public-key encryption algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100787 *
788 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
789 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100790 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100791 * This macro may return either 0 or 1 if \p alg is not a supported
792 * algorithm identifier.
793 */
794#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
795 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
796
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100797/** Whether the specified algorithm is a key agreement algorithm.
798 *
799 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
800 *
801 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
802 * This macro may return either 0 or 1 if \p alg is not a supported
803 * algorithm identifier.
804 */
805#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100806 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100807
808/** Whether the specified algorithm is a key derivation algorithm.
809 *
810 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
811 *
812 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
813 * This macro may return either 0 or 1 if \p alg is not a supported
814 * algorithm identifier.
815 */
816#define PSA_ALG_IS_KEY_DERIVATION(alg) \
817 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
818
Summer Qin359167d2021-07-05 18:11:50 +0800819/** Whether the specified algorithm is a key stretching / password hashing
820 * algorithm.
821 *
822 * A key stretching / password hashing algorithm is a key derivation algorithm
823 * that is suitable for use with a low-entropy secret such as a password.
824 * Equivalently, it's a key derivation algorithm that uses a
825 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
826 *
827 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
828 *
829 * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
830 * otherwise. This macro may return either 0 or 1 if \p alg is not a
831 * supported algorithm identifier.
832 */
833#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
834 (PSA_ALG_IS_KEY_DERIVATION(alg) && \
835 (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
836
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100837#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100838/** MD5 */
Maulik Patel28659c42021-01-06 14:09:22 +0000839#define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100840/** PSA_ALG_RIPEMD160 */
Maulik Patel28659c42021-01-06 14:09:22 +0000841#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100842/** SHA1 */
Maulik Patel28659c42021-01-06 14:09:22 +0000843#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100844/** SHA2-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000845#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100846/** SHA2-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000847#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100848/** SHA2-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000849#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100850/** SHA2-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000851#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100852/** SHA2-512/224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000853#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100854/** SHA2-512/256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000855#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100856/** SHA3-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000857#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100858/** SHA3-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000859#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100860/** SHA3-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000861#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100862/** SHA3-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000863#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
Summer Qin359167d2021-07-05 18:11:50 +0800864/** The first 512 bits (64 bytes) of the SHAKE256 output.
865 *
866 * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
867 * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
868 * has the same output size and a (theoretically) higher security strength.
869 */
870#define PSA_ALG_SHAKE256_512 ((psa_algorithm_t)0x02000015)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100871
872/** In a hash-and-sign algorithm policy, allow any hash algorithm.
873 *
874 * This value may be used to form the algorithm usage field of a policy
875 * for a signature algorithm that is parametrized by a hash. The key
876 * may then be used to perform operations using the same signature
877 * algorithm parametrized with any supported hash.
878 *
879 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
880 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100881 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
882 * Then you may create and use a key as follows:
883 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
884 * ```
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100885 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
886 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100887 * ```
888 * - Import or generate key material.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100889 * - Call psa_sign_hash() or psa_verify_hash(), passing
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100890 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
891 * call to sign or verify a message may use a different hash.
892 * ```
Maulik Patel28659c42021-01-06 14:09:22 +0000893 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
894 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
895 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100896 * ```
897 *
898 * This value may not be used to build other algorithms that are
899 * parametrized over a hash. For any valid use of this macro to build
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100900 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100901 *
902 * This value may not be used to build an algorithm specification to
903 * perform an operation. It is only valid to build policies.
904 */
Maulik Patel28659c42021-01-06 14:09:22 +0000905#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100906
907#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Maulik Patel28659c42021-01-06 14:09:22 +0000908#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100909/** Macro to build an HMAC algorithm.
910 *
911 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
912 *
913 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
914 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
915 *
916 * \return The corresponding HMAC algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100917 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100918 * hash algorithm.
919 */
920#define PSA_ALG_HMAC(hash_alg) \
921 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
922
923#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
924 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
925
926/** Whether the specified algorithm is an HMAC algorithm.
927 *
928 * HMAC is a family of MAC algorithms that are based on a hash function.
929 *
930 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
931 *
932 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
933 * This macro may return either 0 or 1 if \p alg is not a supported
934 * algorithm identifier.
935 */
936#define PSA_ALG_IS_HMAC(alg) \
937 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
938 PSA_ALG_HMAC_BASE)
939
940/* In the encoding of a MAC algorithm, the bits corresponding to
941 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
942 * truncated. As an exception, the value 0 means the untruncated algorithm,
943 * whatever its length is. The length is encoded in 6 bits, so it can
944 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
945 * to full length is correctly encoded as 0 and any non-trivial truncation
946 * is correctly encoded as a value between 1 and 63. */
Maulik Patel28659c42021-01-06 14:09:22 +0000947#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
948#define PSA_MAC_TRUNCATION_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100949
Maulik Patel13b27cf2021-05-14 11:44:53 +0100950/* In the encoding of a MAC algorithm, the bit corresponding to
951 * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
952 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
953 * algorithm policy can be used with any algorithm corresponding to the
954 * same base class and having a (potentially truncated) MAC length greater or
955 * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
956#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
957
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100958/** Macro to build a truncated MAC algorithm.
959 *
960 * A truncated MAC algorithm is identical to the corresponding MAC
961 * algorithm except that the MAC value for the truncated algorithm
962 * consists of only the first \p mac_length bytes of the MAC value
963 * for the untruncated algorithm.
964 *
965 * \note This macro may allow constructing algorithm identifiers that
966 * are not valid, either because the specified length is larger
967 * than the untruncated MAC or because the specified length is
968 * smaller than permitted by the implementation.
969 *
970 * \note It is implementation-defined whether a truncated MAC that
971 * is truncated to the same length as the MAC of the untruncated
972 * algorithm is considered identical to the untruncated algorithm
973 * for policy comparison purposes.
974 *
975 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +0800976 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100977 * is true). This may be a truncated or untruncated
978 * MAC algorithm.
979 * \param mac_length Desired length of the truncated MAC in bytes.
980 * This must be at most the full length of the MAC
981 * and must be at least an implementation-specified
982 * minimum. The implementation-specified minimum
983 * shall not be zero.
984 *
985 * \return The corresponding MAC algorithm with the specified
986 * length.
Summer Qin359167d2021-07-05 18:11:50 +0800987 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100988 * MAC algorithm or if \p mac_length is too small or
989 * too large for the specified MAC algorithm.
990 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100991#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
992 (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
993 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100994 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
995
996/** Macro to build the base MAC algorithm corresponding to a truncated
997 * MAC algorithm.
998 *
999 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001000 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001001 * is true). This may be a truncated or untruncated
1002 * MAC algorithm.
1003 *
1004 * \return The corresponding base MAC algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001005 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001006 * MAC algorithm.
1007 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001008#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
1009 ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1010 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001011
1012/** Length to which a MAC algorithm is truncated.
1013 *
1014 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001015 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001016 * is true).
1017 *
1018 * \return Length of the truncated MAC in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08001019 * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
1020 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001021 * MAC algorithm.
1022 */
1023#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
1024 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1025
Maulik Patel13b27cf2021-05-14 11:44:53 +01001026/** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1027 *
1028 * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1029 * sharing the same base algorithm, and where the (potentially truncated) MAC
1030 * length of the specific algorithm is equal to or larger then the wildcard
1031 * algorithm's minimum MAC length.
1032 *
1033 * \note When setting the minimum required MAC length to less than the
1034 * smallest MAC length allowed by the base algorithm, this effectively
1035 * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1036 *
1037 * \param mac_alg A MAC algorithm identifier (value of type
1038 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1039 * is true).
1040 * \param min_mac_length Desired minimum length of the message authentication
1041 * code in bytes. This must be at most the untruncated
1042 * length of the MAC and must be at least 1.
1043 *
1044 * \return The corresponding MAC wildcard algorithm with the
1045 * specified minimum length.
1046 * \return Unspecified if \p mac_alg is not a supported MAC
1047 * algorithm or if \p min_mac_length is less than 1 or
1048 * too large for the specified MAC algorithm.
1049 */
1050#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
1051 ( PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
1052 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
1053
Maulik Patel28659c42021-01-06 14:09:22 +00001054#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001055/** The CBC-MAC construction over a block cipher
1056 *
1057 * \warning CBC-MAC is insecure in many cases.
1058 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1059 */
Maulik Patel28659c42021-01-06 14:09:22 +00001060#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001061/** The CMAC construction over a block cipher */
Maulik Patel28659c42021-01-06 14:09:22 +00001062#define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001063
1064/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1065 *
1066 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1067 *
1068 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1069 * This macro may return either 0 or 1 if \p alg is not a supported
1070 * algorithm identifier.
1071 */
1072#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
1073 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1074 PSA_ALG_CIPHER_MAC_BASE)
1075
1076#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
1077#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1078
1079/** Whether the specified algorithm is a stream cipher.
1080 *
1081 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1082 * by applying a bitwise-xor with a stream of bytes that is generated
1083 * from a key.
1084 *
1085 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1086 *
1087 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1088 * This macro may return either 0 or 1 if \p alg is not a supported
1089 * algorithm identifier or if it is not a symmetric cipher algorithm.
1090 */
1091#define PSA_ALG_IS_STREAM_CIPHER(alg) \
1092 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1093 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1094
Maulik Patel28659c42021-01-06 14:09:22 +00001095/** The stream cipher mode of a stream cipher algorithm.
1096 *
1097 * The underlying stream cipher is determined by the key type.
1098 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001099 */
Maulik Patel28659c42021-01-06 14:09:22 +00001100#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001101
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001102/** The CTR stream cipher mode.
1103 *
1104 * CTR is a stream cipher which is built from a block cipher.
1105 * The underlying block cipher is determined by the key type.
1106 * For example, to use AES-128-CTR, use this algorithm with
1107 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1108 */
Maulik Patel28659c42021-01-06 14:09:22 +00001109#define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001110
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001111/** The CFB stream cipher mode.
1112 *
1113 * The underlying block cipher is determined by the key type.
1114 */
Maulik Patel28659c42021-01-06 14:09:22 +00001115#define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001116
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001117/** The OFB stream cipher mode.
1118 *
1119 * The underlying block cipher is determined by the key type.
1120 */
Maulik Patel28659c42021-01-06 14:09:22 +00001121#define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001122
1123/** The XTS cipher mode.
1124 *
1125 * XTS is a cipher mode which is built from a block cipher. It requires at
1126 * least one full block of input, but beyond this minimum the input
1127 * does not need to be a whole number of blocks.
1128 */
Maulik Patel28659c42021-01-06 14:09:22 +00001129#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
1130
1131/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1132 *
1133 * \warning ECB mode does not protect the confidentiality of the encrypted data
1134 * except in extremely narrow circumstances. It is recommended that applications
1135 * only use ECB if they need to construct an operating mode that the
1136 * implementation does not provide. Implementations are encouraged to provide
1137 * the modes that applications need in preference to supporting direct access
1138 * to ECB.
1139 *
1140 * The underlying block cipher is determined by the key type.
1141 *
1142 * This symmetric cipher mode can only be used with messages whose lengths are a
1143 * multiple of the block size of the chosen block cipher.
1144 *
1145 * ECB mode does not accept an initialization vector (IV). When using a
1146 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1147 * and psa_cipher_set_iv() must not be called.
1148 */
1149#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001150
1151/** The CBC block cipher chaining mode, with no padding.
1152 *
1153 * The underlying block cipher is determined by the key type.
1154 *
1155 * This symmetric cipher mode can only be used with messages whose lengths
1156 * are whole number of blocks for the chosen block cipher.
1157 */
Maulik Patel28659c42021-01-06 14:09:22 +00001158#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001159
1160/** The CBC block cipher chaining mode with PKCS#7 padding.
1161 *
1162 * The underlying block cipher is determined by the key type.
1163 *
1164 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1165 */
Maulik Patel28659c42021-01-06 14:09:22 +00001166#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001167
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001168#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1169
1170/** Whether the specified algorithm is an AEAD mode on a block cipher.
1171 *
1172 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1173 *
1174 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1175 * a block cipher, 0 otherwise.
1176 * This macro may return either 0 or 1 if \p alg is not a supported
1177 * algorithm identifier.
1178 */
1179#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1180 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1181 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1182
1183/** The CCM authenticated encryption algorithm.
1184 *
1185 * The underlying block cipher is determined by the key type.
1186 */
Maulik Patel28659c42021-01-06 14:09:22 +00001187#define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001188
1189/** The GCM authenticated encryption algorithm.
1190 *
1191 * The underlying block cipher is determined by the key type.
1192 */
Maulik Patel28659c42021-01-06 14:09:22 +00001193#define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001194
1195/** The Chacha20-Poly1305 AEAD algorithm.
1196 *
1197 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1198 *
1199 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1200 * and should reject other sizes.
1201 *
1202 * Implementations must support 16-byte tags and should reject other sizes.
1203 */
Maulik Patel28659c42021-01-06 14:09:22 +00001204#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001205
1206/* In the encoding of a AEAD algorithm, the bits corresponding to
1207 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1208 * The constants for default lengths follow this encoding.
1209 */
Maulik Patel28659c42021-01-06 14:09:22 +00001210#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1211#define PSA_AEAD_TAG_LENGTH_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001212
Maulik Patel13b27cf2021-05-14 11:44:53 +01001213/* In the encoding of an AEAD algorithm, the bit corresponding to
1214 * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1215 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1216 * algorithm policy can be used with any algorithm corresponding to the
1217 * same base class and having a tag length greater than or equal to the one
1218 * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1219#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
1220
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001221/** Macro to build a shortened AEAD algorithm.
1222 *
1223 * A shortened AEAD algorithm is similar to the corresponding AEAD
1224 * algorithm, but has an authentication tag that consists of fewer bytes.
1225 * Depending on the algorithm, the tag length may affect the calculation
1226 * of the ciphertext.
1227 *
1228 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001229 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001230 * is true).
1231 * \param tag_length Desired length of the authentication tag in bytes.
1232 *
1233 * \return The corresponding AEAD algorithm with the specified
1234 * length.
Summer Qin359167d2021-07-05 18:11:50 +08001235 * \return Unspecified if \p aead_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001236 * AEAD algorithm or if \p tag_length is not valid
1237 * for the specified AEAD algorithm.
1238 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001239#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
1240 (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1241 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001242 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1243 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1244
Maulik Patel13b27cf2021-05-14 11:44:53 +01001245/** Retrieve the tag length of a specified AEAD algorithm
1246 *
1247 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001248 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001249 * is true).
1250 *
1251 * \return The tag length specified by the input algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001252 * \return Unspecified if \p aead_alg is not a supported
1253 * AEAD algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +01001254 */
1255#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1256 (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
1257 PSA_AEAD_TAG_LENGTH_OFFSET )
1258
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001259/** Calculate the corresponding AEAD algorithm with the default tag length.
1260 *
1261 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
Summer Qin359167d2021-07-05 18:11:50 +08001262 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001263 *
1264 * \return The corresponding AEAD algorithm with the default
1265 * tag length for that algorithm.
1266 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001267#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001268 ( \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001269 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1270 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1271 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001272 0)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001273#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1274 PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1275 PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001276 ref :
1277
Maulik Patel13b27cf2021-05-14 11:44:53 +01001278/** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1279 *
1280 * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1281 * sharing the same base algorithm, and where the tag length of the specific
1282 * algorithm is equal to or larger then the minimum tag length specified by the
1283 * wildcard algorithm.
1284 *
1285 * \note When setting the minimum required tag length to less than the
1286 * smallest tag length allowed by the base algorithm, this effectively
1287 * becomes an 'any-tag-length-allowed' policy for that base algorithm.
1288 *
1289 * \param aead_alg An AEAD algorithm identifier (value of type
1290 * #psa_algorithm_t such that
1291 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1292 * \param min_tag_length Desired minimum length of the authentication tag in
1293 * bytes. This must be at least 1 and at most the largest
1294 * allowed tag length of the algorithm.
1295 *
1296 * \return The corresponding AEAD wildcard algorithm with the
1297 * specified minimum length.
1298 * \return Unspecified if \p aead_alg is not a supported
1299 * AEAD algorithm or if \p min_tag_length is less than 1
1300 * or too large for the specified AEAD algorithm.
1301 */
1302#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1303 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1304 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
1305
Maulik Patel28659c42021-01-06 14:09:22 +00001306#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001307/** RSA PKCS#1 v1.5 signature with hashing.
1308 *
1309 * This is the signature scheme defined by RFC 8017
1310 * (PKCS#1: RSA Cryptography Specifications) under the name
1311 * RSASSA-PKCS1-v1_5.
1312 *
1313 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1314 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1315 * This includes #PSA_ALG_ANY_HASH
1316 * when specifying the algorithm in a usage policy.
1317 *
1318 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001319 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001320 * hash algorithm.
1321 */
1322#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1323 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1324/** Raw PKCS#1 v1.5 signature.
1325 *
1326 * The input to this algorithm is the DigestInfo structure used by
1327 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1328 * steps 3&ndash;6.
1329 */
1330#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1331#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1332 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1333
Maulik Patel28659c42021-01-06 14:09:22 +00001334#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001335/** RSA PSS signature with hashing.
1336 *
1337 * This is the signature scheme defined by RFC 8017
1338 * (PKCS#1: RSA Cryptography Specifications) under the name
1339 * RSASSA-PSS, with the message generation function MGF1, and with
1340 * a salt length equal to the length of the hash. The specified
1341 * hash algorithm is used to hash the input message, to create the
1342 * salted hash, and for the mask generation.
1343 *
1344 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1345 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1346 * This includes #PSA_ALG_ANY_HASH
1347 * when specifying the algorithm in a usage policy.
1348 *
1349 * \return The corresponding RSA PSS signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001350 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001351 * hash algorithm.
1352 */
1353#define PSA_ALG_RSA_PSS(hash_alg) \
1354 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1355#define PSA_ALG_IS_RSA_PSS(alg) \
1356 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1357
Maulik Patel28659c42021-01-06 14:09:22 +00001358#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001359/** ECDSA signature with hashing.
1360 *
1361 * This is the ECDSA signature scheme defined by ANSI X9.62,
1362 * with a random per-message secret number (*k*).
1363 *
1364 * The representation of the signature as a byte string consists of
1365 * the concatentation of the signature values *r* and *s*. Each of
1366 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1367 * of the base point of the curve in octets. Each value is represented
1368 * in big-endian order (most significant octet first).
1369 *
1370 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1371 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1372 * This includes #PSA_ALG_ANY_HASH
1373 * when specifying the algorithm in a usage policy.
1374 *
1375 * \return The corresponding ECDSA signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001376 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001377 * hash algorithm.
1378 */
1379#define PSA_ALG_ECDSA(hash_alg) \
1380 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1381/** ECDSA signature without hashing.
1382 *
1383 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1384 * without specifying a hash algorithm. This algorithm may only be
1385 * used to sign or verify a sequence of bytes that should be an
1386 * already-calculated hash. Note that the input is padded with
1387 * zeros on the left or truncated on the left as required to fit
1388 * the curve size.
1389 */
1390#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
Maulik Patel28659c42021-01-06 14:09:22 +00001391#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001392/** Deterministic ECDSA signature with hashing.
1393 *
1394 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1395 *
1396 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1397 *
1398 * Note that when this algorithm is used for verification, signatures
1399 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1400 * same private key are accepted. In other words,
1401 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1402 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1403 *
1404 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1405 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1406 * This includes #PSA_ALG_ANY_HASH
1407 * when specifying the algorithm in a usage policy.
1408 *
1409 * \return The corresponding deterministic ECDSA signature
1410 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001411 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001412 * hash algorithm.
1413 */
1414#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1415 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Maulik Patel28659c42021-01-06 14:09:22 +00001416#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001417#define PSA_ALG_IS_ECDSA(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001418 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001419 PSA_ALG_ECDSA_BASE)
1420#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001421 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001422#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1423 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1424#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1425 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1426
Summer Qin359167d2021-07-05 18:11:50 +08001427/** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1428 * using standard parameters.
1429 *
1430 * Contexts are not supported in the current version of this specification
1431 * because there is no suitable signature interface that can take the
1432 * context as a parameter. A future version of this specification may add
1433 * suitable functions and extend this algorithm to support contexts.
1434 *
1435 * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1436 * In this specification, the following curves are supported:
1437 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1438 * in RFC 8032.
1439 * The curve is Edwards25519.
1440 * The hash function used internally is SHA-512.
1441 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1442 * in RFC 8032.
1443 * The curve is Edwards448.
1444 * The hash function used internally is the first 114 bytes of the
1445 * SHAKE256 output.
1446 *
1447 * This algorithm can be used with psa_sign_message() and
1448 * psa_verify_message(). Since there is no prehashing, it cannot be used
1449 * with psa_sign_hash() or psa_verify_hash().
1450 *
1451 * The signature format is the concatenation of R and S as defined by
1452 * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1453 * string for Ed448).
1454 */
1455#define PSA_ALG_PURE_EDDSA ((psa_algorithm_t)0x06000800)
1456
1457#define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t)0x06000900)
1458#define PSA_ALG_IS_HASH_EDDSA(alg) \
1459 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1460
1461/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1462 * using SHA-512 and the Edwards25519 curve.
1463 *
1464 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1465 *
1466 * This algorithm is Ed25519 as specified in RFC 8032.
1467 * The curve is Edwards25519.
1468 * The prehash is SHA-512.
1469 * The hash function used internally is SHA-512.
1470 *
1471 * This is a hash-and-sign algorithm: to calculate a signature,
1472 * you can either:
1473 * - call psa_sign_message() on the message;
1474 * - or calculate the SHA-512 hash of the message
1475 * with psa_hash_compute()
1476 * or with a multi-part hash operation started with psa_hash_setup(),
1477 * using the hash algorithm #PSA_ALG_SHA_512,
1478 * then sign the calculated hash with psa_sign_hash().
1479 * Verifying a signature is similar, using psa_verify_message() or
1480 * psa_verify_hash() instead of the signature function.
1481 */
1482#define PSA_ALG_ED25519PH \
1483 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1484
1485/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1486 * using SHAKE256 and the Edwards448 curve.
1487 *
1488 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1489 *
1490 * This algorithm is Ed448 as specified in RFC 8032.
1491 * The curve is Edwards448.
1492 * The prehash is the first 64 bytes of the SHAKE256 output.
1493 * The hash function used internally is the first 114 bytes of the
1494 * SHAKE256 output.
1495 *
1496 * This is a hash-and-sign algorithm: to calculate a signature,
1497 * you can either:
1498 * - call psa_sign_message() on the message;
1499 * - or calculate the first 64 bytes of the SHAKE256 output of the message
1500 * with psa_hash_compute()
1501 * or with a multi-part hash operation started with psa_hash_setup(),
1502 * using the hash algorithm #PSA_ALG_SHAKE256_512,
1503 * then sign the calculated hash with psa_sign_hash().
1504 * Verifying a signature is similar, using psa_verify_message() or
1505 * psa_verify_hash() instead of the signature function.
1506 */
1507#define PSA_ALG_ED448PH \
1508 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1509
1510/* Default definition, to be overridden if the library is extended with
1511 * more hash-and-sign algorithms that we want to keep out of this header
1512 * file. */
1513#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1514
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001515/** Whether the specified algorithm is a hash-and-sign algorithm.
1516 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001517 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1518 * structured in two parts: first the calculation of a hash in a way that
1519 * does not depend on the key, then the calculation of a signature from the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001520 * hash value and the key.
1521 *
1522 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1523 *
1524 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1525 * This macro may return either 0 or 1 if \p alg is not a supported
1526 * algorithm identifier.
1527 */
1528#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1529 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
Summer Qin359167d2021-07-05 18:11:50 +08001530 PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
1531 PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1532
1533/** Whether the specified algorithm is a signature algorithm that can be used
1534 * with psa_sign_message() and psa_verify_message().
1535 *
1536 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1537 *
1538 * \return 1 if alg is a signature algorithm that can be used to sign a
1539 * message. 0 if \p alg is a signature algorithm that can only be used
1540 * to sign an already-calculated hash. 0 if \p alg is not a signature
1541 * algorithm. This macro can return either 0 or 1 if \p alg is not a
1542 * supported algorithm identifier.
1543 */
1544#define PSA_ALG_IS_SIGN_MESSAGE(alg) \
1545 (PSA_ALG_IS_HASH_AND_SIGN(alg) || (alg) == PSA_ALG_PURE_EDDSA )
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001546
1547/** Get the hash used by a hash-and-sign signature algorithm.
1548 *
1549 * A hash-and-sign algorithm is a signature algorithm which is
1550 * composed of two phases: first a hashing phase which does not use
1551 * the key and produces a hash of the input message, then a signing
1552 * phase which only uses the hash and the key and not the message
1553 * itself.
1554 *
1555 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1556 * #PSA_ALG_IS_SIGN(\p alg) is true).
1557 *
1558 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1559 * algorithm.
1560 * \return 0 if \p alg is a signature algorithm that does not
1561 * follow the hash-and-sign structure.
1562 * \return Unspecified if \p alg is not a signature algorithm or
1563 * if it is not supported by the implementation.
1564 */
1565#define PSA_ALG_SIGN_GET_HASH(alg) \
1566 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1567 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1568 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1569 0)
1570
1571/** RSA PKCS#1 v1.5 encryption.
1572 */
Maulik Patel28659c42021-01-06 14:09:22 +00001573#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001574
Maulik Patel28659c42021-01-06 14:09:22 +00001575#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001576/** RSA OAEP encryption.
1577 *
1578 * This is the encryption scheme defined by RFC 8017
1579 * (PKCS#1: RSA Cryptography Specifications) under the name
1580 * RSAES-OAEP, with the message generation function MGF1.
1581 *
1582 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1583 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1584 * for MGF1.
1585 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001586 * \return The corresponding RSA OAEP encryption algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001587 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001588 * hash algorithm.
1589 */
1590#define PSA_ALG_RSA_OAEP(hash_alg) \
1591 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1592#define PSA_ALG_IS_RSA_OAEP(alg) \
1593 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1594#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1595 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1596 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1597 0)
1598
Maulik Patel28659c42021-01-06 14:09:22 +00001599#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001600/** Macro to build an HKDF algorithm.
1601 *
1602 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1603 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001604 * This key derivation algorithm uses the following inputs:
1605 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1606 * It is optional; if omitted, the derivation uses an empty salt.
1607 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1608 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1609 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1610 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1611 * starting to generate output.
1612 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001613 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1614 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1615 *
1616 * \return The corresponding HKDF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001617 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001618 * hash algorithm.
1619 */
1620#define PSA_ALG_HKDF(hash_alg) \
1621 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1622/** Whether the specified algorithm is an HKDF algorithm.
1623 *
1624 * HKDF is a family of key derivation algorithms that are based on a hash
1625 * function and the HMAC construction.
1626 *
1627 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1628 *
1629 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1630 * This macro may return either 0 or 1 if \c alg is not a supported
1631 * key derivation algorithm identifier.
1632 */
1633#define PSA_ALG_IS_HKDF(alg) \
1634 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1635#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1636 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1637
Maulik Patel28659c42021-01-06 14:09:22 +00001638#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001639/** Macro to build a TLS-1.2 PRF algorithm.
1640 *
1641 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1642 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1643 * used with either SHA-256 or SHA-384.
1644 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001645 * This key derivation algorithm uses the following inputs, which must be
1646 * passed in the order given here:
1647 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1648 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1649 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1650 *
1651 * For the application to TLS-1.2 key expansion, the seed is the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001652 * concatenation of ServerHello.Random + ClientHello.Random,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001653 * and the label is "key expansion".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001654 *
1655 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1656 * TLS 1.2 PRF using HMAC-SHA-256.
1657 *
1658 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1659 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1660 *
1661 * \return The corresponding TLS-1.2 PRF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001662 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001663 * hash algorithm.
1664 */
1665#define PSA_ALG_TLS12_PRF(hash_alg) \
1666 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1667
1668/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1669 *
1670 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1671 *
1672 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1673 * This macro may return either 0 or 1 if \c alg is not a supported
1674 * key derivation algorithm identifier.
1675 */
1676#define PSA_ALG_IS_TLS12_PRF(alg) \
1677 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1678#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1679 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1680
Maulik Patel28659c42021-01-06 14:09:22 +00001681#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001682/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1683 *
1684 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1685 * from the PreSharedKey (PSK) through the application of padding
1686 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1687 * The latter is based on HMAC and can be used with either SHA-256
1688 * or SHA-384.
1689 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001690 * This key derivation algorithm uses the following inputs, which must be
1691 * passed in the order given here:
1692 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1693 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1694 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1695 *
1696 * For the application to TLS-1.2, the seed (which is
1697 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1698 * ClientHello.Random + ServerHello.Random,
1699 * and the label is "master secret" or "extended master secret".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001700 *
1701 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1702 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1703 *
1704 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1705 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1706 *
1707 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001708 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001709 * hash algorithm.
1710 */
1711#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1712 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1713
1714/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1715 *
1716 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1717 *
1718 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1719 * This macro may return either 0 or 1 if \c alg is not a supported
1720 * key derivation algorithm identifier.
1721 */
1722#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1723 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1724#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1725 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1726
Summer Qin359167d2021-07-05 18:11:50 +08001727/* This flag indicates whether the key derivation algorithm is suitable for
1728 * use on low-entropy secrets such as password - these algorithms are also
1729 * known as key stretching or password hashing schemes. These are also the
1730 * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
1731 *
1732 * Those algorithms cannot be combined with a key agreement algorithm.
1733 */
1734#define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t)0x00800000)
1735
1736#define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t)0x08800100)
1737/** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
1738 *
1739 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
1740 * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
1741 * HMAC with the specified hash.
1742 * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA256)` specifies PBKDF2
1743 * using the PRF HMAC-SHA-256.
1744 *
1745 * This key derivation algorithm uses the following inputs, which must be
1746 * provided in the following order:
1747 * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
1748 * This input step must be used exactly once.
1749 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1750 * This input step must be used one or more times; if used several times, the
1751 * inputs will be concatenated. This can be used to build the final salt
1752 * from multiple sources, both public and secret (also known as pepper).
1753 * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
1754 * This input step must be used exactly once.
1755 *
1756 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1757 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1758 *
1759 * \return The corresponding PBKDF2-HMAC-XXX algorithm.
1760 * \return Unspecified if \p hash_alg is not a supported
1761 * hash algorithm.
1762 */
1763#define PSA_ALG_PBKDF2_HMAC(hash_alg) \
1764 (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1765
1766/** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
1767 *
1768 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1769 *
1770 * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
1771 * This macro may return either 0 or 1 if \c alg is not a supported
1772 * key derivation algorithm identifier.
1773 */
1774#define PSA_ALG_IS_PBKDF2_HMAC(alg) \
1775 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
1776
1777/** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
1778 *
1779 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
1780 * This macro specifies the PBKDF2 algorithm constructed using the
1781 * AES-CMAC-PRF-128 PRF specified by RFC 4615.
1782 *
1783 * This key derivation algorithm uses the same inputs as
1784 * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
1785 */
1786#define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t)0x08800200)
1787
Maulik Patel28659c42021-01-06 14:09:22 +00001788#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
1789#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001790
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001791/** Macro to build a combined algorithm that chains a key agreement with
1792 * a key derivation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001793 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001794 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1795 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1796 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1797 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001798 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001799 * \return The corresponding key agreement and derivation
1800 * algorithm.
1801 * \return Unspecified if \p ka_alg is not a supported
1802 * key agreement algorithm or \p kdf_alg is not a
1803 * supported key derivation algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001804 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001805#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1806 ((ka_alg) | (kdf_alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001807
1808#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1809 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1810
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001811#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1812 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001813
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001814/** Whether the specified algorithm is a raw key agreement algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001815 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001816 * A raw key agreement algorithm is one that does not specify
1817 * a key derivation function.
1818 * Usually, raw key agreement algorithms are constructed directly with
1819 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
Maulik Patel28659c42021-01-06 14:09:22 +00001820 * constructed with #PSA_ALG_KEY_AGREEMENT().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001821 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001822 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1823 *
1824 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1825 * This macro may return either 0 or 1 if \p alg is not a supported
1826 * algorithm identifier.
1827 */
1828#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1829 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1830 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1831
1832#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1833 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1834
1835/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1836 *
1837 * The shared secret produced by key agreement is
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001838 * `g^{ab}` in big-endian format.
1839 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1840 * in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001841 */
Maulik Patel28659c42021-01-06 14:09:22 +00001842#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001843
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001844/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1845 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001846 * This includes the raw finite field Diffie-Hellman algorithm as well as
1847 * finite-field Diffie-Hellman followed by any supporter key derivation
1848 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001849 *
1850 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1851 *
1852 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1853 * This macro may return either 0 or 1 if \c alg is not a supported
1854 * key agreement algorithm identifier.
1855 */
1856#define PSA_ALG_IS_FFDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001857 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001858
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001859/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1860 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001861 * The shared secret produced by key agreement is the x-coordinate of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001862 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1863 * `m` is the bit size associated with the curve, i.e. the bit size of the
1864 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1865 * the byte containing the most significant bit of the shared secret
1866 * is padded with zero bits. The byte order is either little-endian
1867 * or big-endian depending on the curve type.
1868 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08001869 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001870 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1871 * in little-endian byte order.
1872 * The bit size is 448 for Curve448 and 255 for Curve25519.
1873 * - For Weierstrass curves over prime fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08001874 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001875 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1876 * in big-endian byte order.
1877 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1878 * - For Weierstrass curves over binary fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08001879 * `PSA_ECC_FAMILY_SECTXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001880 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1881 * in big-endian byte order.
1882 * The bit size is `m` for the field `F_{2^m}`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001883 */
Maulik Patel28659c42021-01-06 14:09:22 +00001884#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001885
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001886/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1887 * algorithm.
1888 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001889 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1890 * elliptic curve Diffie-Hellman followed by any supporter key derivation
1891 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001892 *
1893 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1894 *
1895 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1896 * 0 otherwise.
1897 * This macro may return either 0 or 1 if \c alg is not a supported
1898 * key agreement algorithm identifier.
1899 */
1900#define PSA_ALG_IS_ECDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001901 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001902
1903/** Whether the specified algorithm encoding is a wildcard.
1904 *
1905 * Wildcard values may only be used to set the usage algorithm field in
1906 * a policy, not to perform an operation.
1907 *
1908 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1909 *
1910 * \return 1 if \c alg is a wildcard algorithm encoding.
1911 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1912 * an operation).
1913 * \return This macro may return either 0 or 1 if \c alg is not a supported
1914 * algorithm identifier.
1915 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001916#define PSA_ALG_IS_WILDCARD(alg) \
1917 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1918 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1919 PSA_ALG_IS_MAC(alg) ? \
1920 (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
1921 PSA_ALG_IS_AEAD(alg) ? \
1922 (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001923 (alg) == PSA_ALG_ANY_HASH)
1924
Summer Qin359167d2021-07-05 18:11:50 +08001925/** Get the hash used by a composite algorithm.
1926 *
1927 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1928 *
1929 * \return The underlying hash algorithm if alg is a composite algorithm that
1930 * uses a hash algorithm.
1931 *
1932 * \return \c 0 if alg is not a composite algorithm that uses a hash.
1933 */
1934#define PSA_ALG_GET_HASH(alg) \
1935 (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t)0) : 0x02000000 | ((alg) & 0x000000ff))
1936
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001937/**@}*/
1938
1939/** \defgroup key_lifetimes Key lifetimes
1940 * @{
1941 */
1942
Soby Mathew07ef6e42020-07-20 21:09:23 +01001943/** The default lifetime for volatile keys.
1944 *
Maulik Patel28659c42021-01-06 14:09:22 +00001945 * A volatile key only exists as long as the identifier to it is not destroyed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001946 * The key material is guaranteed to be erased on a power reset.
Soby Mathew07ef6e42020-07-20 21:09:23 +01001947 *
1948 * A key with this lifetime is typically stored in the RAM area of the
1949 * PSA Crypto subsystem. However this is an implementation choice.
1950 * If an implementation stores data about the key in a non-volatile memory,
1951 * it must release all the resources associated with the key and erase the
1952 * key material if the calling application terminates.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001953 */
1954#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1955
Soby Mathew07ef6e42020-07-20 21:09:23 +01001956/** The default lifetime for persistent keys.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001957 *
1958 * A persistent key remains in storage until it is explicitly destroyed or
1959 * until the corresponding storage area is wiped. This specification does
Maulik Patel13b27cf2021-05-14 11:44:53 +01001960 * not define any mechanism to wipe a storage area, but integrations may
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001961 * provide their own mechanism (for example to perform a factory reset,
1962 * to prepare for device refurbishment, or to uninstall an application).
1963 *
1964 * This lifetime value is the default storage area for the calling
Maulik Patel13b27cf2021-05-14 11:44:53 +01001965 * application. Integrations of Mbed TLS may support other persistent lifetimes.
Soby Mathew07ef6e42020-07-20 21:09:23 +01001966 * See ::psa_key_lifetime_t for more information.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001967 */
1968#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1969
Soby Mathew07ef6e42020-07-20 21:09:23 +01001970/** The persistence level of volatile keys.
1971 *
1972 * See ::psa_key_persistence_t for more information.
1973 */
1974#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
1975
1976/** The default persistence level for persistent keys.
1977 *
1978 * See ::psa_key_persistence_t for more information.
1979 */
1980#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
1981
1982/** A persistence level indicating that a key is never destroyed.
1983 *
1984 * See ::psa_key_persistence_t for more information.
1985 */
1986#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
1987
1988#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
1989 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
1990
1991#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
1992 ((psa_key_location_t)((lifetime) >> 8))
1993
1994/** Whether a key lifetime indicates that the key is volatile.
1995 *
1996 * A volatile key is automatically destroyed by the implementation when
1997 * the application instance terminates. In particular, a volatile key
1998 * is automatically destroyed on a power reset of the device.
1999 *
2000 * A key that is not volatile is persistent. Persistent keys are
2001 * preserved until the application explicitly destroys them or until an
2002 * implementation-specific device management event occurs (for example,
2003 * a factory reset).
2004 *
2005 * \param lifetime The lifetime value to query (value of type
2006 * ::psa_key_lifetime_t).
2007 *
2008 * \return \c 1 if the key is volatile, otherwise \c 0.
2009 */
2010#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
2011 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
Summer Qin0e5b2e02020-10-22 11:23:39 +08002012 PSA_KEY_PERSISTENCE_VOLATILE)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002013
Summer Qin359167d2021-07-05 18:11:50 +08002014/** Whether a key lifetime indicates that the key is read-only.
2015 *
2016 * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2017 * They must be created through platform-specific means that bypass the API.
2018 *
2019 * Some platforms may offer ways to destroy read-only keys. For example,
2020 * consider a platform with multiple levels of privilege, where a
2021 * low-privilege application can use a key but is not allowed to destroy
2022 * it, and the platform exposes the key to the application with a read-only
2023 * lifetime. High-privilege code can destroy the key even though the
2024 * application sees the key as read-only.
2025 *
2026 * \param lifetime The lifetime value to query (value of type
2027 * ::psa_key_lifetime_t).
2028 *
2029 * \return \c 1 if the key is read-only, otherwise \c 0.
2030 */
2031#define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
2032 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2033 PSA_KEY_PERSISTENCE_READ_ONLY)
2034
Soby Mathew07ef6e42020-07-20 21:09:23 +01002035/** Construct a lifetime from a persistence level and a location.
2036 *
2037 * \param persistence The persistence level
2038 * (value of type ::psa_key_persistence_t).
2039 * \param location The location indicator
2040 * (value of type ::psa_key_location_t).
2041 *
2042 * \return The constructed lifetime value.
2043 */
2044#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2045 ((location) << 8 | (persistence))
2046
2047/** The local storage area for persistent keys.
2048 *
2049 * This storage area is available on all systems that can store persistent
2050 * keys without delegating the storage to a third-party cryptoprocessor.
2051 *
2052 * See ::psa_key_location_t for more information.
2053 */
2054#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
2055
2056#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
2057
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002058/** The minimum value for a key identifier chosen by the application.
2059 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002060#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002061/** The maximum value for a key identifier chosen by the application.
2062 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002063#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002064/** The minimum value for a key identifier chosen by the implementation.
2065 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002066#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002067/** The maximum value for a key identifier chosen by the implementation.
2068 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002069#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002070
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002071/**@}*/
2072
2073/** \defgroup policy Key policies
2074 * @{
2075 */
2076
2077/** Whether the key may be exported.
2078 *
2079 * A public key or the public part of a key pair may always be exported
2080 * regardless of the value of this permission flag.
2081 *
2082 * If a key does not have export permission, implementations shall not
2083 * allow the key to be exported in plain form from the cryptoprocessor,
2084 * whether through psa_export_key() or through a proprietary interface.
2085 * The key may however be exportable in a wrapped form, i.e. in a form
2086 * where it is encrypted by another key.
2087 */
2088#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
2089
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002090/** Whether the key may be copied.
2091 *
2092 * This flag allows the use of psa_copy_key() to make a copy of the key
2093 * with the same policy or a more restrictive policy.
2094 *
2095 * For lifetimes for which the key is located in a secure element which
2096 * enforce the non-exportability of keys, copying a key outside the secure
2097 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2098 * Copying the key inside the secure element is permitted with just
2099 * #PSA_KEY_USAGE_COPY if the secure element supports it.
2100 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2101 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2102 * is sufficient to permit the copy.
2103 */
2104#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
2105
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002106/** Whether the key may be used to encrypt a message.
2107 *
2108 * This flag allows the key to be used for a symmetric encryption operation,
2109 * for an AEAD encryption-and-authentication operation,
2110 * or for an asymmetric encryption operation,
2111 * if otherwise permitted by the key's type and policy.
2112 *
2113 * For a key pair, this concerns the public key.
2114 */
2115#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
2116
2117/** Whether the key may be used to decrypt a message.
2118 *
2119 * This flag allows the key to be used for a symmetric decryption operation,
2120 * for an AEAD decryption-and-verification operation,
2121 * or for an asymmetric decryption operation,
2122 * if otherwise permitted by the key's type and policy.
2123 *
2124 * For a key pair, this concerns the private key.
2125 */
2126#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
2127
2128/** Whether the key may be used to sign a message.
2129 *
Summer Qin359167d2021-07-05 18:11:50 +08002130 * This flag allows the key to be used for a MAC calculation operation or for
2131 * an asymmetric message signature operation, if otherwise permitted by the
2132 * key’s type and policy.
2133 *
2134 * For a key pair, this concerns the private key.
2135 */
2136#define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t)0x00000400)
2137
2138/** Whether the key may be used to verify a message.
2139 *
2140 * This flag allows the key to be used for a MAC verification operation or for
2141 * an asymmetric message signature verification operation, if otherwise
2142 * permitted by the key’s type and policy.
2143 *
2144 * For a key pair, this concerns the public key.
2145 */
2146#define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t)0x00000800)
2147
2148/** Whether the key may be used to sign a message.
2149 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002150 * This flag allows the key to be used for a MAC calculation operation
2151 * or for an asymmetric signature operation,
2152 * if otherwise permitted by the key's type and policy.
2153 *
2154 * For a key pair, this concerns the private key.
2155 */
Maulik Patel28659c42021-01-06 14:09:22 +00002156#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002157
2158/** Whether the key may be used to verify a message signature.
2159 *
2160 * This flag allows the key to be used for a MAC verification operation
2161 * or for an asymmetric signature verification operation,
2162 * if otherwise permitted by by the key's type and policy.
2163 *
2164 * For a key pair, this concerns the public key.
2165 */
Maulik Patel28659c42021-01-06 14:09:22 +00002166#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002167
Summer Qin359167d2021-07-05 18:11:50 +08002168/** Whether the key may be used to derive other keys or produce a password
2169 * hash.
2170 *
2171 * This flag allows the key to be used for a key derivation operation or for
2172 * a key agreement operation, if otherwise permitted by by the key's type and
2173 * policy.
2174 *
2175 * If this flag is present on all keys used in calls to
2176 * psa_key_derivation_input_key() for a key derivation operation, then it
2177 * permits calling psa_key_derivation_output_bytes() or
2178 * psa_key_derivation_output_key() at the end of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002179 */
Maulik Patel28659c42021-01-06 14:09:22 +00002180#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002181
Summer Qin359167d2021-07-05 18:11:50 +08002182/** Whether the key may be used to verify the result of a key derivation,
2183 * including password hashing.
2184 *
2185 * This flag allows the key to be used:
2186 *
2187 * This flag allows the key to be used in a key derivation operation, if
2188 * otherwise permitted by by the key's type and policy.
2189 *
2190 * If this flag is present on all keys used in calls to
2191 * psa_key_derivation_input_key() for a key derivation operation, then it
2192 * permits calling psa_key_derivation_verify_bytes() or
2193 * psa_key_derivation_verify_key() at the end of the operation.
2194 */
2195#define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t)0x00008000)
2196
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002197/**@}*/
2198
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002199/** \defgroup derivation Key derivation
2200 * @{
2201 */
2202
2203/** A secret input for key derivation.
2204 *
2205 * This should be a key of type #PSA_KEY_TYPE_DERIVE
2206 * (passed to psa_key_derivation_input_key())
2207 * or the shared secret resulting from a key agreement
2208 * (obtained via psa_key_derivation_key_agreement()).
2209 *
2210 * The secret can also be a direct input (passed to
2211 * key_derivation_input_bytes()). In this case, the derivation operation
2212 * may not be used to derive keys: the operation will only allow
Summer Qin359167d2021-07-05 18:11:50 +08002213 * psa_key_derivation_output_bytes(),
2214 * psa_key_derivation_verify_bytes(), or
2215 * psa_key_derivation_verify_key(), but not
2216 * psa_key_derivation_output_key().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002217 */
2218#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
2219
Summer Qin359167d2021-07-05 18:11:50 +08002220/** A low-entropy secret input for password hashing / key stretching.
2221 *
2222 * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2223 * psa_key_derivation_input_key()) or a direct input (passed to
2224 * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2225 * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2226 * the shared secret resulting from a key agreement.
2227 *
2228 * The secret can also be a direct input (passed to
2229 * key_derivation_input_bytes()). In this case, the derivation operation
2230 * may not be used to derive keys: the operation will only allow
2231 * psa_key_derivation_output_bytes(),
2232 * psa_key_derivation_verify_bytes(), or
2233 * psa_key_derivation_verify_key(), but not
2234 * psa_key_derivation_output_key().
2235 */
2236#define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t)0x0102)
2237
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002238/** A label for key derivation.
2239 *
2240 * This should be a direct input.
2241 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2242 */
2243#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
2244
2245/** A salt for key derivation.
2246 *
2247 * This should be a direct input.
Summer Qin359167d2021-07-05 18:11:50 +08002248 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2249 * #PSA_KEY_TYPE_PEPPER.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002250 */
2251#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
2252
2253/** An information string for key derivation.
2254 *
2255 * This should be a direct input.
2256 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2257 */
2258#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
2259
2260/** A seed for key derivation.
2261 *
2262 * This should be a direct input.
2263 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2264 */
2265#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
2266
Summer Qin359167d2021-07-05 18:11:50 +08002267/** A cost parameter for password hashing / key stretching.
2268 *
2269 * This must be a direct input, passed to psa_key_derivation_input_integer().
2270 */
2271#define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t)0x0205)
2272
2273/**@}*/
2274
2275/** \defgroup helper_macros Helper macros
2276 * @{
2277 */
2278
2279/* Helper macros */
2280
2281/** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2282 * regardless of the tag length they encode.
2283 *
2284 * \param aead_alg_1 An AEAD algorithm identifier.
2285 * \param aead_alg_2 An AEAD algorithm identifier.
2286 *
2287 * \return 1 if both identifiers refer to the same AEAD algorithm,
2288 * 0 otherwise.
2289 * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2290 * a supported AEAD algorithm.
2291 */
2292#define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2293 (!(((aead_alg_1) ^ (aead_alg_2)) & \
2294 ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2295
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002296/**@}*/
2297
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002298#endif /* PSA_CRYPTO_VALUES_H */