blob: 797c8c56385c9f9ca68da748a7770f49d5842d6c [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
Summer Qinf07cc312022-01-05 16:52:54 +08002 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto_values.h
Jamie Fox0e54ebc2019-04-09 14:21:04 +01009 *
10 * \brief PSA cryptography module: macros to build and analyze integer values.
11 *
12 * \note This file may not be included directly. Applications must
Jamie Foxcc31d402019-01-28 17:13:52 +000013 * include psa/crypto.h. Drivers must include the appropriate driver
Jamie Fox0e54ebc2019-04-09 14:21:04 +010014 * header file.
15 *
16 * This file contains portable definitions of macros to build and analyze
17 * values of integral types that encode properties of cryptographic keys,
18 * designations of cryptographic algorithms, and error codes returned by
19 * the library.
20 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010021 * Note that many of the constants defined in this file are embedded in
22 * the persistent key store, as part of key metadata (including usage
23 * policies). As a consequence, they must not be changed (unless the storage
24 * format version changes).
25 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010026 * This header file only defines preprocessor macros.
27 */
28
29#ifndef PSA_CRYPTO_VALUES_H
30#define PSA_CRYPTO_VALUES_H
31
32/** \defgroup error Error codes
33 * @{
34 */
35
36/* PSA error codes */
37
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010038/* Error codes are standardized across PSA domains (framework, crypto, storage,
39 * etc.). Do not change the values in this section or even the expansions
40 * of each macro: it must be possible to `#include` both this header
41 * and some other PSA component's headers in the same C source,
42 * which will lead to duplicate definitions of the `PSA_SUCCESS` and
43 * `PSA_ERROR_xxx` macros, which is ok if and only if the macros expand
44 * to the same sequence of tokens.
45 *
46 * If you must add a new
47 * value, check with the Arm PSA framework group to pick one that other
48 * domains aren't already using. */
49
Jamie Fox0e54ebc2019-04-09 14:21:04 +010050/** The action was completed successfully. */
51#ifndef PSA_SUCCESS
52#define PSA_SUCCESS ((psa_status_t)0)
53#endif
54
55/** An error occurred that does not correspond to any defined
56 * failure cause.
57 *
58 * Implementations may use this error code if none of the other standard
59 * error codes are applicable. */
60#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
61
62/** The requested operation or a parameter is not supported
63 * by this implementation.
64 *
65 * Implementations should return this error code when an enumeration
66 * parameter such as a key type, algorithm, etc. is not recognized.
67 * If a combination of parameters is recognized and identified as
68 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
69#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
70
71/** The requested action is denied by a policy.
72 *
73 * Implementations should return this error code when the parameters
74 * are recognized as valid and supported, and a policy explicitly
75 * denies the requested operation.
76 *
77 * If a subset of the parameters of a function call identify a
78 * forbidden operation, and another subset of the parameters are
79 * not valid or not supported, it is unspecified whether the function
80 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
81 * #PSA_ERROR_INVALID_ARGUMENT. */
82#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
83
84/** An output buffer is too small.
85 *
86 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
87 * description to determine a sufficient buffer size.
88 *
89 * Implementations should preferably return this error code only
90 * in cases when performing the operation with a larger output
91 * buffer would succeed. However implementations may return this
92 * error if a function has invalid or unsupported parameters in addition
93 * to the parameters that determine the necessary output buffer size. */
94#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
95
96/** Asking for an item that already exists
97 *
98 * Implementations should return this error, when attempting
99 * to write an item (like a key) that already exists. */
100#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
101
102/** Asking for an item that doesn't exist
103 *
104 * Implementations should return this error, if a requested item (like
105 * a key) does not exist. */
106#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
107
108/** The requested action cannot be performed in the current state.
109 *
110 * Multipart operations return this error when one of the
111 * functions is called out of sequence. Refer to the function
112 * descriptions for permitted sequencing of functions.
113 *
114 * Implementations shall not return this error code to indicate
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100115 * that a key either exists or not,
116 * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
117 * as applicable.
118 *
119 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000120 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100121 * instead. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100122#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
123
124/** The parameters passed to the function are invalid.
125 *
126 * Implementations may return this error any time a parameter or
127 * combination of parameters are recognized as invalid.
128 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100129 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000130 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100131 * instead.
132 */
133#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
134
135/** There is not enough runtime memory.
136 *
137 * If the action is carried out across multiple security realms, this
138 * error can refer to available memory in any of the security realms. */
139#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
140
141/** There is not enough persistent storage.
142 *
143 * Functions that modify the key storage return this error code if
144 * there is insufficient storage space on the host media. In addition,
145 * many functions that do not otherwise access storage may return this
146 * error code if the implementation requires a mandatory log entry for
147 * the requested action and the log storage space is full. */
148#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
149
150/** There was a communication failure inside the implementation.
151 *
152 * This can indicate a communication failure between the application
153 * and an external cryptoprocessor or between the cryptoprocessor and
154 * an external volatile or persistent memory. A communication failure
155 * may be transient or permanent depending on the cause.
156 *
157 * \warning If a function returns this error, it is undetermined
158 * whether the requested action has completed or not. Implementations
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100159 * should return #PSA_SUCCESS on successful completion whenever
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100160 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
161 * if the requested action was completed successfully in an external
162 * cryptoprocessor but there was a breakdown of communication before
163 * the cryptoprocessor could report the status to the application.
164 */
165#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
166
167/** There was a storage failure that may have led to data loss.
168 *
169 * This error indicates that some persistent storage is corrupted.
170 * It should not be used for a corruption of volatile memory
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100171 * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100172 * between the cryptoprocessor and its external storage (use
173 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
174 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
175 *
176 * Note that a storage failure does not indicate that any data that was
177 * previously read is invalid. However this previously read data may no
178 * longer be readable from storage.
179 *
180 * When a storage failure occurs, it is no longer possible to ensure
181 * the global integrity of the keystore. Depending on the global
182 * integrity guarantees offered by the implementation, access to other
183 * data may or may not fail even if the data is still readable but
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100184 * its integrity cannot be guaranteed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100185 *
186 * Implementations should only use this error code to report a
187 * permanent storage corruption. However application writers should
188 * keep in mind that transient errors while reading the storage may be
189 * reported using this error code. */
190#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
191
192/** A hardware failure was detected.
193 *
194 * A hardware failure may be transient or permanent depending on the
195 * cause. */
196#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
197
198/** A tampering attempt was detected.
199 *
200 * If an application receives this error code, there is no guarantee
201 * that previously accessed or computed data was correct and remains
202 * confidential. Applications should not perform any security function
203 * and should enter a safe failure state.
204 *
205 * Implementations may return this error code if they detect an invalid
206 * state that cannot happen during normal operation and that indicates
207 * that the implementation's security guarantees no longer hold. Depending
208 * on the implementation architecture and on its security and safety goals,
209 * the implementation may forcibly terminate the application.
210 *
211 * This error code is intended as a last resort when a security breach
212 * is detected and it is unsure whether the keystore data is still
213 * protected. Implementations shall only return this error code
214 * to report an alarm from a tampering detector, to indicate that
215 * the confidentiality of stored data can no longer be guaranteed,
216 * or to indicate that the integrity of previously returned data is now
217 * considered compromised. Implementations shall not use this error code
218 * to indicate a hardware failure that merely makes it impossible to
219 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
220 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
221 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
222 * instead).
223 *
224 * This error indicates an attack against the application. Implementations
225 * shall not return this error code as a consequence of the behavior of
226 * the application itself. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100227#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100228
229/** There is not enough entropy to generate random data needed
230 * for the requested action.
231 *
232 * This error indicates a failure of a hardware random generator.
233 * Application writers should note that this error can be returned not
234 * only by functions whose purpose is to generate random data, such
235 * as key, IV or nonce generation, but also by functions that execute
236 * an algorithm with a randomized result, as well as functions that
237 * use randomization of intermediate computations as a countermeasure
238 * to certain attacks.
239 *
240 * Implementations should avoid returning this error after psa_crypto_init()
241 * has succeeded. Implementations should generate sufficient
242 * entropy during initialization and subsequently use a cryptographically
243 * secure pseudorandom generator (PRNG). However implementations may return
244 * this error at any time if a policy requires the PRNG to be reseeded
245 * during normal operation. */
246#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
247
248/** The signature, MAC or hash is incorrect.
249 *
250 * Verification functions return this error if the verification
251 * calculations completed successfully, and the value to be verified
252 * was determined to be incorrect.
253 *
254 * If the value to verify has an invalid size, implementations may return
255 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
256#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
257
258/** The decrypted padding is incorrect.
259 *
260 * \warning In some protocols, when decrypting data, it is essential that
261 * the behavior of the application does not depend on whether the padding
262 * is correct, down to precise timing. Applications should prefer
263 * protocols that use authenticated encryption rather than plain
264 * encryption. If the application must perform a decryption of
265 * unauthenticated data, the application writer should take care not
266 * to reveal whether the padding is invalid.
267 *
268 * Implementations should strive to make valid and invalid padding
269 * as close as possible to indistinguishable to an external observer.
270 * In particular, the timing of a decryption operation should not
271 * depend on the validity of the padding. */
272#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
273
274/** Return this error when there's insufficient data when attempting
275 * to read from a resource. */
276#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
277
Maulik Patel28659c42021-01-06 14:09:22 +0000278/** The key identifier is not valid. See also :ref:\`key-handles\`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100279 */
280#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
281
Maulik Patel13b27cf2021-05-14 11:44:53 +0100282/** Stored data has been corrupted.
283 *
284 * This error indicates that some persistent storage has suffered corruption.
285 * It does not indicate the following situations, which have specific error
286 * codes:
287 *
288 * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
289 * - A communication error between the cryptoprocessor and its external
290 * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
291 * - When the storage is in a valid state but is full - use
292 * #PSA_ERROR_INSUFFICIENT_STORAGE.
293 * - When the storage fails for other reasons - use
294 * #PSA_ERROR_STORAGE_FAILURE.
295 * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
296 *
297 * \note A storage corruption does not indicate that any data that was
298 * previously read is invalid. However this previously read data might no
299 * longer be readable from storage.
300 *
301 * When a storage failure occurs, it is no longer possible to ensure the
302 * global integrity of the keystore.
303 */
304#define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
305
306/** Data read from storage is not valid for the implementation.
307 *
308 * This error indicates that some data read from storage does not have a valid
309 * format. It does not indicate the following situations, which have specific
310 * error codes:
311 *
312 * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
313 * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
314 * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
315 *
316 * This error is typically a result of either storage corruption on a
317 * cleartext storage backend, or an attempt to read data that was
318 * written by an incompatible version of the library.
319 */
320#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
321
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100322/**@}*/
323
324/** \defgroup crypto_types Key and algorithm types
325 * @{
326 */
327
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100328/* Note that key type values, including ECC family and DH group values, are
329 * embedded in the persistent key store, as part of key metadata. As a
330 * consequence, they must not be changed (unless the storage format version
331 * changes).
332 */
333
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100334/** An invalid key type value.
335 *
336 * Zero is not the encoding of any key type.
337 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100338#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100339
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100340/** Vendor-defined key type flag.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100341 *
342 * Key types defined by this standard will never have the
343 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
344 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
345 * respect the bitwise structure used by standard encodings whenever practical.
346 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100347#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100348
Soby Mathew07ef6e42020-07-20 21:09:23 +0100349#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
350#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
351#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
352#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
353#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100354
Soby Mathew07ef6e42020-07-20 21:09:23 +0100355#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100356
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100357/** Whether a key type is vendor-defined.
358 *
359 * See also #PSA_KEY_TYPE_VENDOR_FLAG.
360 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100361#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
362 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
363
364/** Whether a key type is an unstructured array of bytes.
365 *
366 * This encompasses both symmetric keys and non-key data.
367 */
368#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100369 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
370 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100371
372/** Whether a key type is asymmetric: either a key pair or a public key. */
373#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
374 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
375 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
376 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
377/** Whether a key type is the public part of a key pair. */
378#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
379 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
380/** Whether a key type is a key pair containing a private part and a public
381 * part. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100382#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100383 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
384/** The key pair type corresponding to a public key type.
385 *
386 * You may also pass a key pair type as \p type, it will be left unchanged.
387 *
388 * \param type A public key type or key pair type.
389 *
390 * \return The corresponding key pair type.
391 * If \p type is not a public key or a key pair,
392 * the return value is undefined.
393 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100394#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100395 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
396/** The public key type corresponding to a key pair type.
397 *
398 * You may also pass a key pair type as \p type, it will be left unchanged.
399 *
400 * \param type A public key type or key pair type.
401 *
402 * \return The corresponding public key type.
403 * If \p type is not a public key or a key pair,
404 * the return value is undefined.
405 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100406#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100407 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
408
409/** Raw data.
410 *
411 * A "key" of this type cannot be used for any cryptographic operation.
412 * Applications may use this type to store arbitrary data in the keystore. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100413#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100414
415/** HMAC key.
416 *
417 * The key policy determines which underlying hash algorithm the key can be
418 * used for.
419 *
420 * HMAC keys should generally have the same size as the underlying hash.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100421 * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100422 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100423#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100424
425/** A secret for key derivation.
426 *
Summer Qin359167d2021-07-05 18:11:50 +0800427 * This key type is for high-entropy secrets only. For low-entropy secrets,
428 * #PSA_KEY_TYPE_PASSWORD should be used instead.
429 *
430 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
431 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
432 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100433 * The key policy determines which key derivation algorithm the key
434 * can be used for.
435 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100436#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100437
Summer Qin359167d2021-07-05 18:11:50 +0800438/** A low-entropy secret for password hashing or key derivation.
439 *
440 * This key type is suitable for passwords and passphrases which are typically
441 * intended to be memorizable by humans, and have a low entropy relative to
442 * their size. It can be used for randomly generated or derived keys with
443 * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
444 * for such keys. It is not suitable for passwords with extremely low entropy,
445 * such as numerical PINs.
446 *
447 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
448 * key derivation algorithms. Algorithms that accept such an input were
449 * designed to accept low-entropy secret and are known as password hashing or
450 * key stretching algorithms.
451 *
452 * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
453 * key derivation algorithms, as the algorithms that take such an input expect
454 * it to be high-entropy.
455 *
456 * The key policy determines which key derivation algorithm the key can be
457 * used for, among the permissible subset defined above.
458 */
459#define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t)0x1203)
460
461/** A secret value that can be used to verify a password hash.
462 *
463 * The key policy determines which key derivation algorithm the key
464 * can be used for, among the same permissible subset as for
465 * #PSA_KEY_TYPE_PASSWORD.
466 */
467#define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t)0x1205)
468
469/** A secret value that can be used in when computing a password hash.
470 *
471 * The key policy determines which key derivation algorithm the key
472 * can be used for, among the subset of algorithms that can use pepper.
473 */
474#define PSA_KEY_TYPE_PEPPER ((psa_key_type_t)0x1206)
475
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100476/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100477 *
478 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
479 * 32 bytes (AES-256).
480 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100481#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100482
Summer Qinf07cc312022-01-05 16:52:54 +0800483/** Key for a cipher, AEAD or MAC algorithm based on the
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100484 * ARIA block cipher. */
Summer Qinf07cc312022-01-05 16:52:54 +0800485#define PSA_KEY_TYPE_ARIA ((psa_key_type_t)0x2406)
486
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100487/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
488 *
Summer Qin359167d2021-07-05 18:11:50 +0800489 * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
490 * 192 bits (3-key 3DES).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100491 *
492 * Note that single DES and 2-key 3DES are weak and strongly
493 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
494 * is weak and deprecated and should only be used in legacy protocols.
495 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100496#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100497
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100498/** Key for a cipher, AEAD or MAC algorithm based on the
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100499 * Camellia block cipher. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100500#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100501
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100502/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
503 *
504 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
505 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100506 * \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports
507 * 12-byte nonces.
508 *
509 * \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt
510 * with the initial counter value 1, you can process and discard a
511 * 64-byte block before the real data.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100512 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100513#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100514
Summer Qin359167d2021-07-05 18:11:50 +0800515/** RSA public key.
516 *
517 * The size of an RSA key is the bit size of the modulus.
518 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100519#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
Summer Qin359167d2021-07-05 18:11:50 +0800520/** RSA key pair (private and public key).
521 *
522 * The size of an RSA key is the bit size of the modulus.
523 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100524#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100525/** Whether a key type is an RSA key (pair or public-only). */
526#define PSA_KEY_TYPE_IS_RSA(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100527 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100528
Soby Mathew07ef6e42020-07-20 21:09:23 +0100529#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
530#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
531#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100532/** Elliptic curve key pair.
533 *
Summer Qin359167d2021-07-05 18:11:50 +0800534 * The size of an elliptic curve key is the bit size associated with the curve,
535 * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
536 * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
537 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800538 * \param curve A value of type ::psa_ecc_family_t that
539 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100540 */
541#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
542 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
543/** Elliptic curve public key.
544 *
Summer Qin359167d2021-07-05 18:11:50 +0800545 * The size of an elliptic curve public key is the same as the corresponding
546 * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
547 * `PSA_ECC_FAMILY_xxx` curve families).
548 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800549 * \param curve A value of type ::psa_ecc_family_t that
550 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100551 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100552#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
553 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
554
555/** Whether a key type is an elliptic curve key (pair or public-only). */
556#define PSA_KEY_TYPE_IS_ECC(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100557 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100558 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
559/** Whether a key type is an elliptic curve key pair. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100560#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100561 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100562 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100563/** Whether a key type is an elliptic curve public key. */
564#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
565 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
566 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
567
568/** Extract the curve from an elliptic curve key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800569#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
570 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100571 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
572 0))
573
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100574/** Check if the curve of given family is Weierstrass elliptic curve. */
575#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
576
Soby Mathew07ef6e42020-07-20 21:09:23 +0100577/** SEC Koblitz curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100578 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100579 * This family comprises the following curves:
580 * secp192k1, secp224k1, secp256k1.
581 * They are defined in _Standards for Efficient Cryptography_,
582 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
583 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100584 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800585#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100586
Soby Mathew07ef6e42020-07-20 21:09:23 +0100587/** SEC random curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100588 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100589 * This family comprises the following curves:
590 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
591 * They are defined in _Standards for Efficient Cryptography_,
592 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
593 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100594 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800595#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100596/* SECP160R2 (SEC2 v1, obsolete) */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800597#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100598
Soby Mathew07ef6e42020-07-20 21:09:23 +0100599/** SEC Koblitz curves over binary fields.
600 *
601 * This family comprises the following curves:
602 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
603 * They are defined in _Standards for Efficient Cryptography_,
604 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
605 * https://www.secg.org/sec2-v2.pdf
606 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800607#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100608
609/** SEC random curves over binary fields.
610 *
611 * This family comprises the following curves:
612 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
613 * They are defined in _Standards for Efficient Cryptography_,
614 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
615 * https://www.secg.org/sec2-v2.pdf
616 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800617#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100618
619/** SEC additional random curves over binary fields.
620 *
621 * This family comprises the following curve:
622 * sect163r2.
623 * It is defined in _Standards for Efficient Cryptography_,
624 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
625 * https://www.secg.org/sec2-v2.pdf
626 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800627#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100628
629/** Brainpool P random curves.
630 *
631 * This family comprises the following curves:
632 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
633 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
634 * It is defined in RFC 5639.
635 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800636#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100637
638/** Curve25519 and Curve448.
639 *
640 * This family comprises the following Montgomery curves:
641 * - 255-bit: Bernstein et al.,
642 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
643 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
644 * - 448-bit: Hamburg,
645 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
646 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
647 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800648#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100649
Summer Qin359167d2021-07-05 18:11:50 +0800650/** The twisted Edwards curves Ed25519 and Ed448.
651 *
652 * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
653 * #PSA_ALG_ED25519PH for the 255-bit curve,
654 * #PSA_ALG_ED448PH for the 448-bit curve).
655 *
656 * This family comprises the following twisted Edwards curves:
657 * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
658 * to Curve25519.
659 * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
660 * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
661 * to Curve448.
662 * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
663 */
664#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
665
Soby Mathew07ef6e42020-07-20 21:09:23 +0100666#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
667#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
668#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100669/** Diffie-Hellman key pair.
670 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800671 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100672 * Diffie-Hellman group to be used.
673 */
674#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
675 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
676/** Diffie-Hellman public key.
677 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800678 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100679 * Diffie-Hellman group to be used.
680 */
681#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
682 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
683
684/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
685#define PSA_KEY_TYPE_IS_DH(type) \
686 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
687 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
688/** Whether a key type is a Diffie-Hellman key pair. */
689#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
690 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
691 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
692/** Whether a key type is a Diffie-Hellman public key. */
693#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
694 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
695 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
696
697/** Extract the group from a Diffie-Hellman key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800698#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
699 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100700 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
701 0))
702
Soby Mathew07ef6e42020-07-20 21:09:23 +0100703/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100704 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100705 * This family includes groups with the following key sizes (in bits):
706 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
707 * all of these sizes or only a subset.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100708 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800709#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100710
Soby Mathew07ef6e42020-07-20 21:09:23 +0100711#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
712 (((type) >> 8) & 7)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100713/** The block size of a block cipher.
714 *
715 * \param type A cipher key type (value of type #psa_key_type_t).
716 *
717 * \return The block size for a block cipher, or 1 for a stream cipher.
718 * The return value is undefined if \p type is not a supported
719 * cipher key type.
720 *
721 * \note It is possible to build stream cipher algorithms on top of a block
722 * cipher, for example CTR mode (#PSA_ALG_CTR).
723 * This macro only takes the key type into account, so it cannot be
724 * used to determine the size of the data that #psa_cipher_update()
725 * might buffer for future processing in general.
726 *
727 * \note This macro returns a compile-time constant if its argument is one.
728 *
729 * \warning This macro may evaluate its argument multiple times.
730 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100731#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100732 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100733 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100734 0u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100735
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100736/* Note that algorithm values are embedded in the persistent key store,
737 * as part of key metadata. As a consequence, they must not be changed
738 * (unless the storage format version changes).
739 */
740
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100741/** Vendor-defined algorithm flag.
742 *
743 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
744 * bit set. Vendors who define additional algorithms must use an encoding with
745 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
746 * used by standard encodings whenever practical.
747 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100748#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100749
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100750#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000751#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
752#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100753#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000754#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
755#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
756#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
757#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
758#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100759
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100760/** Whether an algorithm is vendor-defined.
761 *
762 * See also #PSA_ALG_VENDOR_FLAG.
763 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100764#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
765 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
766
767/** Whether the specified algorithm is a hash algorithm.
768 *
769 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
770 *
771 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
772 * This macro may return either 0 or 1 if \p alg is not a supported
773 * algorithm identifier.
774 */
775#define PSA_ALG_IS_HASH(alg) \
776 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
777
778/** Whether the specified algorithm is a MAC algorithm.
779 *
780 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
781 *
782 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
783 * This macro may return either 0 or 1 if \p alg is not a supported
784 * algorithm identifier.
785 */
786#define PSA_ALG_IS_MAC(alg) \
787 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
788
789/** Whether the specified algorithm is a symmetric cipher algorithm.
790 *
791 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
792 *
793 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
794 * This macro may return either 0 or 1 if \p alg is not a supported
795 * algorithm identifier.
796 */
797#define PSA_ALG_IS_CIPHER(alg) \
798 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
799
800/** Whether the specified algorithm is an authenticated encryption
801 * with associated data (AEAD) algorithm.
802 *
803 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
804 *
805 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
806 * This macro may return either 0 or 1 if \p alg is not a supported
807 * algorithm identifier.
808 */
809#define PSA_ALG_IS_AEAD(alg) \
810 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
811
Soby Mathew07ef6e42020-07-20 21:09:23 +0100812/** Whether the specified algorithm is an asymmetric signature algorithm,
813 * also known as public-key signature algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100814 *
815 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
816 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100817 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100818 * This macro may return either 0 or 1 if \p alg is not a supported
819 * algorithm identifier.
820 */
821#define PSA_ALG_IS_SIGN(alg) \
822 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
823
Soby Mathew07ef6e42020-07-20 21:09:23 +0100824/** Whether the specified algorithm is an asymmetric encryption algorithm,
825 * also known as public-key encryption algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100826 *
827 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
828 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100829 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100830 * This macro may return either 0 or 1 if \p alg is not a supported
831 * algorithm identifier.
832 */
833#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
834 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
835
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100836/** Whether the specified algorithm is a key agreement algorithm.
837 *
838 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
839 *
840 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
841 * This macro may return either 0 or 1 if \p alg is not a supported
842 * algorithm identifier.
843 */
844#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100845 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100846
847/** Whether the specified algorithm is a key derivation algorithm.
848 *
849 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
850 *
851 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
852 * This macro may return either 0 or 1 if \p alg is not a supported
853 * algorithm identifier.
854 */
855#define PSA_ALG_IS_KEY_DERIVATION(alg) \
856 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
857
Summer Qin359167d2021-07-05 18:11:50 +0800858/** Whether the specified algorithm is a key stretching / password hashing
859 * algorithm.
860 *
861 * A key stretching / password hashing algorithm is a key derivation algorithm
862 * that is suitable for use with a low-entropy secret such as a password.
863 * Equivalently, it's a key derivation algorithm that uses a
864 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
865 *
866 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
867 *
868 * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
869 * otherwise. This macro may return either 0 or 1 if \p alg is not a
870 * supported algorithm identifier.
871 */
872#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
873 (PSA_ALG_IS_KEY_DERIVATION(alg) && \
874 (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
875
Summer Qinf07cc312022-01-05 16:52:54 +0800876/** An invalid algorithm identifier value. */
877#define PSA_ALG_NONE ((psa_algorithm_t)0)
878
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100879#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100880/** MD5 */
Maulik Patel28659c42021-01-06 14:09:22 +0000881#define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100882/** PSA_ALG_RIPEMD160 */
Maulik Patel28659c42021-01-06 14:09:22 +0000883#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100884/** SHA1 */
Maulik Patel28659c42021-01-06 14:09:22 +0000885#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100886/** SHA2-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000887#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100888/** SHA2-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000889#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100890/** SHA2-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000891#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100892/** SHA2-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000893#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100894/** SHA2-512/224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000895#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100896/** SHA2-512/256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000897#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100898/** SHA3-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000899#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100900/** SHA3-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000901#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100902/** SHA3-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000903#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100904/** SHA3-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000905#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
Summer Qin359167d2021-07-05 18:11:50 +0800906/** The first 512 bits (64 bytes) of the SHAKE256 output.
907 *
908 * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
909 * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
910 * has the same output size and a (theoretically) higher security strength.
911 */
912#define PSA_ALG_SHAKE256_512 ((psa_algorithm_t)0x02000015)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100913
914/** In a hash-and-sign algorithm policy, allow any hash algorithm.
915 *
916 * This value may be used to form the algorithm usage field of a policy
917 * for a signature algorithm that is parametrized by a hash. The key
918 * may then be used to perform operations using the same signature
919 * algorithm parametrized with any supported hash.
920 *
921 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
Summer Qinf07cc312022-01-05 16:52:54 +0800922 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100923 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
924 * Then you may create and use a key as follows:
925 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
926 * ```
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100927 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
928 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100929 * ```
930 * - Import or generate key material.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100931 * - Call psa_sign_hash() or psa_verify_hash(), passing
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100932 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
933 * call to sign or verify a message may use a different hash.
934 * ```
Maulik Patel28659c42021-01-06 14:09:22 +0000935 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
936 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
937 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100938 * ```
939 *
940 * This value may not be used to build other algorithms that are
941 * parametrized over a hash. For any valid use of this macro to build
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100942 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100943 *
944 * This value may not be used to build an algorithm specification to
945 * perform an operation. It is only valid to build policies.
946 */
Maulik Patel28659c42021-01-06 14:09:22 +0000947#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100948
949#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Maulik Patel28659c42021-01-06 14:09:22 +0000950#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100951/** Macro to build an HMAC algorithm.
952 *
953 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
954 *
955 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
956 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
957 *
958 * \return The corresponding HMAC algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100959 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100960 * hash algorithm.
961 */
962#define PSA_ALG_HMAC(hash_alg) \
963 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
964
965#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
966 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
967
968/** Whether the specified algorithm is an HMAC algorithm.
969 *
970 * HMAC is a family of MAC algorithms that are based on a hash function.
971 *
972 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
973 *
974 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
975 * This macro may return either 0 or 1 if \p alg is not a supported
976 * algorithm identifier.
977 */
978#define PSA_ALG_IS_HMAC(alg) \
979 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
980 PSA_ALG_HMAC_BASE)
981
982/* In the encoding of a MAC algorithm, the bits corresponding to
983 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
984 * truncated. As an exception, the value 0 means the untruncated algorithm,
985 * whatever its length is. The length is encoded in 6 bits, so it can
986 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
987 * to full length is correctly encoded as 0 and any non-trivial truncation
988 * is correctly encoded as a value between 1 and 63. */
Maulik Patel28659c42021-01-06 14:09:22 +0000989#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
990#define PSA_MAC_TRUNCATION_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100991
Maulik Patel13b27cf2021-05-14 11:44:53 +0100992/* In the encoding of a MAC algorithm, the bit corresponding to
993 * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
994 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
995 * algorithm policy can be used with any algorithm corresponding to the
996 * same base class and having a (potentially truncated) MAC length greater or
997 * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
998#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
999
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001000/** Macro to build a truncated MAC algorithm.
1001 *
1002 * A truncated MAC algorithm is identical to the corresponding MAC
1003 * algorithm except that the MAC value for the truncated algorithm
1004 * consists of only the first \p mac_length bytes of the MAC value
1005 * for the untruncated algorithm.
1006 *
1007 * \note This macro may allow constructing algorithm identifiers that
1008 * are not valid, either because the specified length is larger
1009 * than the untruncated MAC or because the specified length is
1010 * smaller than permitted by the implementation.
1011 *
1012 * \note It is implementation-defined whether a truncated MAC that
1013 * is truncated to the same length as the MAC of the untruncated
1014 * algorithm is considered identical to the untruncated algorithm
1015 * for policy comparison purposes.
1016 *
1017 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001018 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001019 * is true). This may be a truncated or untruncated
1020 * MAC algorithm.
1021 * \param mac_length Desired length of the truncated MAC in bytes.
1022 * This must be at most the full length of the MAC
1023 * and must be at least an implementation-specified
1024 * minimum. The implementation-specified minimum
1025 * shall not be zero.
1026 *
1027 * \return The corresponding MAC algorithm with the specified
1028 * length.
Summer Qin359167d2021-07-05 18:11:50 +08001029 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001030 * MAC algorithm or if \p mac_length is too small or
1031 * too large for the specified MAC algorithm.
1032 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001033#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
1034 (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1035 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001036 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1037
1038/** Macro to build the base MAC algorithm corresponding to a truncated
1039 * MAC algorithm.
1040 *
1041 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001042 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001043 * is true). This may be a truncated or untruncated
1044 * MAC algorithm.
1045 *
1046 * \return The corresponding base MAC algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001047 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001048 * MAC algorithm.
1049 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001050#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
1051 ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1052 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001053
1054/** Length to which a MAC algorithm is truncated.
1055 *
1056 * \param mac_alg A MAC algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001057 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001058 * is true).
1059 *
1060 * \return Length of the truncated MAC in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08001061 * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
1062 * \return Unspecified if \p mac_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001063 * MAC algorithm.
1064 */
1065#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
1066 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1067
Maulik Patel13b27cf2021-05-14 11:44:53 +01001068/** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1069 *
1070 * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1071 * sharing the same base algorithm, and where the (potentially truncated) MAC
1072 * length of the specific algorithm is equal to or larger then the wildcard
1073 * algorithm's minimum MAC length.
1074 *
1075 * \note When setting the minimum required MAC length to less than the
1076 * smallest MAC length allowed by the base algorithm, this effectively
1077 * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1078 *
1079 * \param mac_alg A MAC algorithm identifier (value of type
1080 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1081 * is true).
1082 * \param min_mac_length Desired minimum length of the message authentication
1083 * code in bytes. This must be at most the untruncated
1084 * length of the MAC and must be at least 1.
1085 *
1086 * \return The corresponding MAC wildcard algorithm with the
1087 * specified minimum length.
1088 * \return Unspecified if \p mac_alg is not a supported MAC
1089 * algorithm or if \p min_mac_length is less than 1 or
1090 * too large for the specified MAC algorithm.
1091 */
1092#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
1093 ( PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
1094 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
1095
Maulik Patel28659c42021-01-06 14:09:22 +00001096#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001097/** The CBC-MAC construction over a block cipher
1098 *
1099 * \warning CBC-MAC is insecure in many cases.
1100 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1101 */
Maulik Patel28659c42021-01-06 14:09:22 +00001102#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001103/** The CMAC construction over a block cipher */
Maulik Patel28659c42021-01-06 14:09:22 +00001104#define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001105
1106/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1107 *
1108 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1109 *
1110 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1111 * This macro may return either 0 or 1 if \p alg is not a supported
1112 * algorithm identifier.
1113 */
1114#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
1115 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1116 PSA_ALG_CIPHER_MAC_BASE)
1117
1118#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
1119#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1120
1121/** Whether the specified algorithm is a stream cipher.
1122 *
1123 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1124 * by applying a bitwise-xor with a stream of bytes that is generated
1125 * from a key.
1126 *
1127 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1128 *
1129 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1130 * This macro may return either 0 or 1 if \p alg is not a supported
1131 * algorithm identifier or if it is not a symmetric cipher algorithm.
1132 */
1133#define PSA_ALG_IS_STREAM_CIPHER(alg) \
1134 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1135 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1136
Maulik Patel28659c42021-01-06 14:09:22 +00001137/** The stream cipher mode of a stream cipher algorithm.
1138 *
1139 * The underlying stream cipher is determined by the key type.
1140 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001141 */
Maulik Patel28659c42021-01-06 14:09:22 +00001142#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001143
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001144/** The CTR stream cipher mode.
1145 *
1146 * CTR is a stream cipher which is built from a block cipher.
1147 * The underlying block cipher is determined by the key type.
1148 * For example, to use AES-128-CTR, use this algorithm with
1149 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1150 */
Maulik Patel28659c42021-01-06 14:09:22 +00001151#define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001152
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001153/** The CFB stream cipher mode.
1154 *
1155 * The underlying block cipher is determined by the key type.
1156 */
Maulik Patel28659c42021-01-06 14:09:22 +00001157#define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001158
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001159/** The OFB stream cipher mode.
1160 *
1161 * The underlying block cipher is determined by the key type.
1162 */
Maulik Patel28659c42021-01-06 14:09:22 +00001163#define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001164
1165/** The XTS cipher mode.
1166 *
1167 * XTS is a cipher mode which is built from a block cipher. It requires at
1168 * least one full block of input, but beyond this minimum the input
1169 * does not need to be a whole number of blocks.
1170 */
Maulik Patel28659c42021-01-06 14:09:22 +00001171#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
1172
1173/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1174 *
1175 * \warning ECB mode does not protect the confidentiality of the encrypted data
1176 * except in extremely narrow circumstances. It is recommended that applications
1177 * only use ECB if they need to construct an operating mode that the
1178 * implementation does not provide. Implementations are encouraged to provide
1179 * the modes that applications need in preference to supporting direct access
1180 * to ECB.
1181 *
1182 * The underlying block cipher is determined by the key type.
1183 *
1184 * This symmetric cipher mode can only be used with messages whose lengths are a
1185 * multiple of the block size of the chosen block cipher.
1186 *
1187 * ECB mode does not accept an initialization vector (IV). When using a
1188 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1189 * and psa_cipher_set_iv() must not be called.
1190 */
1191#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001192
1193/** The CBC block cipher chaining mode, with no padding.
1194 *
1195 * The underlying block cipher is determined by the key type.
1196 *
1197 * This symmetric cipher mode can only be used with messages whose lengths
1198 * are whole number of blocks for the chosen block cipher.
1199 */
Maulik Patel28659c42021-01-06 14:09:22 +00001200#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001201
1202/** The CBC block cipher chaining mode with PKCS#7 padding.
1203 *
1204 * The underlying block cipher is determined by the key type.
1205 *
1206 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1207 */
Maulik Patel28659c42021-01-06 14:09:22 +00001208#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001209
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001210#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1211
1212/** Whether the specified algorithm is an AEAD mode on a block cipher.
1213 *
1214 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1215 *
1216 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1217 * a block cipher, 0 otherwise.
1218 * This macro may return either 0 or 1 if \p alg is not a supported
1219 * algorithm identifier.
1220 */
1221#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1222 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1223 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1224
1225/** The CCM authenticated encryption algorithm.
1226 *
1227 * The underlying block cipher is determined by the key type.
1228 */
Maulik Patel28659c42021-01-06 14:09:22 +00001229#define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001230
Summer Qinf07cc312022-01-05 16:52:54 +08001231/** The CCM* cipher mode without authentication.
1232 *
1233 * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1234 * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1235 *
1236 * The underlying block cipher is determined by the key type.
1237 *
1238 * Currently only 13-byte long IV's are supported.
1239 */
1240#define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t)0x04c01300)
1241
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001242/** The GCM authenticated encryption algorithm.
1243 *
1244 * The underlying block cipher is determined by the key type.
1245 */
Maulik Patel28659c42021-01-06 14:09:22 +00001246#define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001247
1248/** The Chacha20-Poly1305 AEAD algorithm.
1249 *
1250 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1251 *
1252 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1253 * and should reject other sizes.
1254 *
1255 * Implementations must support 16-byte tags and should reject other sizes.
1256 */
Maulik Patel28659c42021-01-06 14:09:22 +00001257#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001258
1259/* In the encoding of a AEAD algorithm, the bits corresponding to
1260 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1261 * The constants for default lengths follow this encoding.
1262 */
Maulik Patel28659c42021-01-06 14:09:22 +00001263#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1264#define PSA_AEAD_TAG_LENGTH_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001265
Maulik Patel13b27cf2021-05-14 11:44:53 +01001266/* In the encoding of an AEAD algorithm, the bit corresponding to
1267 * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1268 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1269 * algorithm policy can be used with any algorithm corresponding to the
1270 * same base class and having a tag length greater than or equal to the one
1271 * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1272#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
1273
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001274/** Macro to build a shortened AEAD algorithm.
1275 *
1276 * A shortened AEAD algorithm is similar to the corresponding AEAD
1277 * algorithm, but has an authentication tag that consists of fewer bytes.
1278 * Depending on the algorithm, the tag length may affect the calculation
1279 * of the ciphertext.
1280 *
1281 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001282 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001283 * is true).
1284 * \param tag_length Desired length of the authentication tag in bytes.
1285 *
1286 * \return The corresponding AEAD algorithm with the specified
1287 * length.
Summer Qin359167d2021-07-05 18:11:50 +08001288 * \return Unspecified if \p aead_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001289 * AEAD algorithm or if \p tag_length is not valid
1290 * for the specified AEAD algorithm.
1291 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001292#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
1293 (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1294 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001295 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1296 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1297
Maulik Patel13b27cf2021-05-14 11:44:53 +01001298/** Retrieve the tag length of a specified AEAD algorithm
1299 *
1300 * \param aead_alg An AEAD algorithm identifier (value of type
Summer Qin359167d2021-07-05 18:11:50 +08001301 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001302 * is true).
1303 *
1304 * \return The tag length specified by the input algorithm.
Summer Qin359167d2021-07-05 18:11:50 +08001305 * \return Unspecified if \p aead_alg is not a supported
1306 * AEAD algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +01001307 */
1308#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1309 (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
1310 PSA_AEAD_TAG_LENGTH_OFFSET )
1311
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001312/** Calculate the corresponding AEAD algorithm with the default tag length.
1313 *
1314 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
Summer Qin359167d2021-07-05 18:11:50 +08001315 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001316 *
1317 * \return The corresponding AEAD algorithm with the default
1318 * tag length for that algorithm.
1319 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001320#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001321 ( \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001322 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1323 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1324 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001325 0)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001326#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1327 PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1328 PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001329 ref :
1330
Maulik Patel13b27cf2021-05-14 11:44:53 +01001331/** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1332 *
1333 * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1334 * sharing the same base algorithm, and where the tag length of the specific
1335 * algorithm is equal to or larger then the minimum tag length specified by the
1336 * wildcard algorithm.
1337 *
1338 * \note When setting the minimum required tag length to less than the
1339 * smallest tag length allowed by the base algorithm, this effectively
1340 * becomes an 'any-tag-length-allowed' policy for that base algorithm.
1341 *
1342 * \param aead_alg An AEAD algorithm identifier (value of type
1343 * #psa_algorithm_t such that
1344 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1345 * \param min_tag_length Desired minimum length of the authentication tag in
1346 * bytes. This must be at least 1 and at most the largest
1347 * allowed tag length of the algorithm.
1348 *
1349 * \return The corresponding AEAD wildcard algorithm with the
1350 * specified minimum length.
1351 * \return Unspecified if \p aead_alg is not a supported
1352 * AEAD algorithm or if \p min_tag_length is less than 1
1353 * or too large for the specified AEAD algorithm.
1354 */
1355#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1356 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1357 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
1358
Maulik Patel28659c42021-01-06 14:09:22 +00001359#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001360/** RSA PKCS#1 v1.5 signature with hashing.
1361 *
1362 * This is the signature scheme defined by RFC 8017
1363 * (PKCS#1: RSA Cryptography Specifications) under the name
1364 * RSASSA-PKCS1-v1_5.
1365 *
1366 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1367 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1368 * This includes #PSA_ALG_ANY_HASH
1369 * when specifying the algorithm in a usage policy.
1370 *
1371 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001372 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001373 * hash algorithm.
1374 */
1375#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1376 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1377/** Raw PKCS#1 v1.5 signature.
1378 *
1379 * The input to this algorithm is the DigestInfo structure used by
1380 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1381 * steps 3&ndash;6.
1382 */
1383#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1384#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1385 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1386
Maulik Patel28659c42021-01-06 14:09:22 +00001387#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
Summer Qinf07cc312022-01-05 16:52:54 +08001388#define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t)0x06001300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001389/** RSA PSS signature with hashing.
1390 *
1391 * This is the signature scheme defined by RFC 8017
1392 * (PKCS#1: RSA Cryptography Specifications) under the name
1393 * RSASSA-PSS, with the message generation function MGF1, and with
1394 * a salt length equal to the length of the hash. The specified
1395 * hash algorithm is used to hash the input message, to create the
1396 * salted hash, and for the mask generation.
1397 *
1398 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1399 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1400 * This includes #PSA_ALG_ANY_HASH
1401 * when specifying the algorithm in a usage policy.
1402 *
1403 * \return The corresponding RSA PSS signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001404 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001405 * hash algorithm.
1406 */
1407#define PSA_ALG_RSA_PSS(hash_alg) \
1408 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Summer Qinf07cc312022-01-05 16:52:54 +08001409
1410/** RSA PSS signature with hashing with relaxed verification.
1411 *
1412 * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1413 * but allows an arbitrary salt length (including \c 0) when verifying a
1414 * signature.
1415 *
1416 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1417 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1418 * This includes #PSA_ALG_ANY_HASH
1419 * when specifying the algorithm in a usage policy.
1420 *
1421 * \return The corresponding RSA PSS signature algorithm.
1422 * \return Unspecified if \p hash_alg is not a supported
1423 * hash algorithm.
1424 */
1425#define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \
1426 (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1427
1428/** Whether the specified algorithm is RSA PSS with standard salt.
1429 *
1430 * \param alg An algorithm value or an algorithm policy wildcard.
1431 *
1432 * \return 1 if \p alg is of the form
1433 * #PSA_ALG_RSA_PSS(\c hash_alg),
1434 * where \c hash_alg is a hash algorithm or
1435 * #PSA_ALG_ANY_HASH. 0 otherwise.
1436 * This macro may return either 0 or 1 if \p alg is not
1437 * a supported algorithm identifier or policy.
1438 */
1439#define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001440 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1441
Summer Qinf07cc312022-01-05 16:52:54 +08001442/** Whether the specified algorithm is RSA PSS with any salt.
1443 *
1444 * \param alg An algorithm value or an algorithm policy wildcard.
1445 *
1446 * \return 1 if \p alg is of the form
1447 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1448 * where \c hash_alg is a hash algorithm or
1449 * #PSA_ALG_ANY_HASH. 0 otherwise.
1450 * This macro may return either 0 or 1 if \p alg is not
1451 * a supported algorithm identifier or policy.
1452 */
1453#define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \
1454 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1455
1456/** Whether the specified algorithm is RSA PSS.
1457 *
1458 * This includes any of the RSA PSS algorithm variants, regardless of the
1459 * constraints on salt length.
1460 *
1461 * \param alg An algorithm value or an algorithm policy wildcard.
1462 *
1463 * \return 1 if \p alg is of the form
1464 * #PSA_ALG_RSA_PSS(\c hash_alg) or
1465 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1466 * where \c hash_alg is a hash algorithm or
1467 * #PSA_ALG_ANY_HASH. 0 otherwise.
1468 * This macro may return either 0 or 1 if \p alg is not
1469 * a supported algorithm identifier or policy.
1470 */
1471#define PSA_ALG_IS_RSA_PSS(alg) \
1472 (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \
1473 PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
1474
Maulik Patel28659c42021-01-06 14:09:22 +00001475#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001476/** ECDSA signature with hashing.
1477 *
1478 * This is the ECDSA signature scheme defined by ANSI X9.62,
1479 * with a random per-message secret number (*k*).
1480 *
1481 * The representation of the signature as a byte string consists of
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001482 * the concatenation of the signature values *r* and *s*. Each of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001483 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1484 * of the base point of the curve in octets. Each value is represented
1485 * in big-endian order (most significant octet first).
1486 *
1487 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1488 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1489 * This includes #PSA_ALG_ANY_HASH
1490 * when specifying the algorithm in a usage policy.
1491 *
1492 * \return The corresponding ECDSA signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001493 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001494 * hash algorithm.
1495 */
1496#define PSA_ALG_ECDSA(hash_alg) \
1497 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1498/** ECDSA signature without hashing.
1499 *
1500 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1501 * without specifying a hash algorithm. This algorithm may only be
1502 * used to sign or verify a sequence of bytes that should be an
1503 * already-calculated hash. Note that the input is padded with
1504 * zeros on the left or truncated on the left as required to fit
1505 * the curve size.
1506 */
1507#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
Maulik Patel28659c42021-01-06 14:09:22 +00001508#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001509/** Deterministic ECDSA signature with hashing.
1510 *
1511 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1512 *
1513 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1514 *
1515 * Note that when this algorithm is used for verification, signatures
1516 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1517 * same private key are accepted. In other words,
1518 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1519 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1520 *
1521 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1522 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1523 * This includes #PSA_ALG_ANY_HASH
1524 * when specifying the algorithm in a usage policy.
1525 *
1526 * \return The corresponding deterministic ECDSA signature
1527 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001528 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001529 * hash algorithm.
1530 */
1531#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1532 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Maulik Patel28659c42021-01-06 14:09:22 +00001533#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001534#define PSA_ALG_IS_ECDSA(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001535 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001536 PSA_ALG_ECDSA_BASE)
1537#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001538 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001539#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1540 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1541#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1542 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1543
Summer Qin359167d2021-07-05 18:11:50 +08001544/** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1545 * using standard parameters.
1546 *
1547 * Contexts are not supported in the current version of this specification
1548 * because there is no suitable signature interface that can take the
1549 * context as a parameter. A future version of this specification may add
1550 * suitable functions and extend this algorithm to support contexts.
1551 *
1552 * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1553 * In this specification, the following curves are supported:
1554 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1555 * in RFC 8032.
1556 * The curve is Edwards25519.
1557 * The hash function used internally is SHA-512.
1558 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1559 * in RFC 8032.
1560 * The curve is Edwards448.
1561 * The hash function used internally is the first 114 bytes of the
1562 * SHAKE256 output.
1563 *
1564 * This algorithm can be used with psa_sign_message() and
1565 * psa_verify_message(). Since there is no prehashing, it cannot be used
1566 * with psa_sign_hash() or psa_verify_hash().
1567 *
1568 * The signature format is the concatenation of R and S as defined by
1569 * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1570 * string for Ed448).
1571 */
1572#define PSA_ALG_PURE_EDDSA ((psa_algorithm_t)0x06000800)
1573
1574#define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t)0x06000900)
1575#define PSA_ALG_IS_HASH_EDDSA(alg) \
1576 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1577
1578/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1579 * using SHA-512 and the Edwards25519 curve.
1580 *
1581 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1582 *
1583 * This algorithm is Ed25519 as specified in RFC 8032.
1584 * The curve is Edwards25519.
1585 * The prehash is SHA-512.
1586 * The hash function used internally is SHA-512.
1587 *
1588 * This is a hash-and-sign algorithm: to calculate a signature,
1589 * you can either:
1590 * - call psa_sign_message() on the message;
1591 * - or calculate the SHA-512 hash of the message
1592 * with psa_hash_compute()
1593 * or with a multi-part hash operation started with psa_hash_setup(),
1594 * using the hash algorithm #PSA_ALG_SHA_512,
1595 * then sign the calculated hash with psa_sign_hash().
1596 * Verifying a signature is similar, using psa_verify_message() or
1597 * psa_verify_hash() instead of the signature function.
1598 */
1599#define PSA_ALG_ED25519PH \
1600 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1601
1602/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1603 * using SHAKE256 and the Edwards448 curve.
1604 *
1605 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1606 *
1607 * This algorithm is Ed448 as specified in RFC 8032.
1608 * The curve is Edwards448.
1609 * The prehash is the first 64 bytes of the SHAKE256 output.
1610 * The hash function used internally is the first 114 bytes of the
1611 * SHAKE256 output.
1612 *
1613 * This is a hash-and-sign algorithm: to calculate a signature,
1614 * you can either:
1615 * - call psa_sign_message() on the message;
1616 * - or calculate the first 64 bytes of the SHAKE256 output of the message
1617 * with psa_hash_compute()
1618 * or with a multi-part hash operation started with psa_hash_setup(),
1619 * using the hash algorithm #PSA_ALG_SHAKE256_512,
1620 * then sign the calculated hash with psa_sign_hash().
1621 * Verifying a signature is similar, using psa_verify_message() or
1622 * psa_verify_hash() instead of the signature function.
1623 */
1624#define PSA_ALG_ED448PH \
1625 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1626
1627/* Default definition, to be overridden if the library is extended with
1628 * more hash-and-sign algorithms that we want to keep out of this header
1629 * file. */
1630#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1631
Summer Qinf07cc312022-01-05 16:52:54 +08001632/** Whether the specified algorithm is a signature algorithm that can be used
1633 * with psa_sign_hash() and psa_verify_hash().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001634 *
Summer Qinf07cc312022-01-05 16:52:54 +08001635 * This encompasses all strict hash-and-sign algorithms categorized by
1636 * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1637 * paradigm more loosely:
1638 * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1639 * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001640 *
Summer Qinf07cc312022-01-05 16:52:54 +08001641 * \param alg An algorithm identifier (value of type psa_algorithm_t).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001642 *
Summer Qinf07cc312022-01-05 16:52:54 +08001643 * \return 1 if alg is a signature algorithm that can be used to sign a
1644 * hash. 0 if alg is a signature algorithm that can only be used
1645 * to sign a message. 0 if alg is not a signature algorithm.
1646 * This macro can return either 0 or 1 if alg is not a
1647 * supported algorithm identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001648 */
Summer Qinf07cc312022-01-05 16:52:54 +08001649#define PSA_ALG_IS_SIGN_HASH(alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001650 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
Summer Qin359167d2021-07-05 18:11:50 +08001651 PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
1652 PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1653
1654/** Whether the specified algorithm is a signature algorithm that can be used
1655 * with psa_sign_message() and psa_verify_message().
1656 *
1657 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1658 *
1659 * \return 1 if alg is a signature algorithm that can be used to sign a
1660 * message. 0 if \p alg is a signature algorithm that can only be used
1661 * to sign an already-calculated hash. 0 if \p alg is not a signature
1662 * algorithm. This macro can return either 0 or 1 if \p alg is not a
1663 * supported algorithm identifier.
1664 */
1665#define PSA_ALG_IS_SIGN_MESSAGE(alg) \
Summer Qinf07cc312022-01-05 16:52:54 +08001666 (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
1667
1668/** Whether the specified algorithm is a hash-and-sign algorithm.
1669 *
1670 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1671 * structured in two parts: first the calculation of a hash in a way that
1672 * does not depend on the key, then the calculation of a signature from the
1673 * hash value and the key. Hash-and-sign algorithms encode the hash
1674 * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1675 * to extract this algorithm.
1676 *
1677 * Thus, for a hash-and-sign algorithm,
1678 * `psa_sign_message(key, alg, input, ...)` is equivalent to
1679 * ```
1680 * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1681 * psa_sign_hash(key, alg, hash, ..., signature, ...);
1682 * ```
1683 * Most usefully, separating the hash from the signature allows the hash
1684 * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1685 * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1686 * calculating the hash and then calling psa_verify_hash().
1687 *
1688 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1689 *
1690 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1691 * This macro may return either 0 or 1 if \p alg is not a supported
1692 * algorithm identifier.
1693 */
1694#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1695 (PSA_ALG_IS_SIGN_HASH(alg) && \
1696 ((alg) & PSA_ALG_HASH_MASK) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001697
1698/** Get the hash used by a hash-and-sign signature algorithm.
1699 *
1700 * A hash-and-sign algorithm is a signature algorithm which is
1701 * composed of two phases: first a hashing phase which does not use
1702 * the key and produces a hash of the input message, then a signing
1703 * phase which only uses the hash and the key and not the message
1704 * itself.
1705 *
1706 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1707 * #PSA_ALG_IS_SIGN(\p alg) is true).
1708 *
1709 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1710 * algorithm.
1711 * \return 0 if \p alg is a signature algorithm that does not
1712 * follow the hash-and-sign structure.
1713 * \return Unspecified if \p alg is not a signature algorithm or
1714 * if it is not supported by the implementation.
1715 */
1716#define PSA_ALG_SIGN_GET_HASH(alg) \
1717 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001718 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1719 0)
1720
1721/** RSA PKCS#1 v1.5 encryption.
1722 */
Maulik Patel28659c42021-01-06 14:09:22 +00001723#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001724
Maulik Patel28659c42021-01-06 14:09:22 +00001725#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001726/** RSA OAEP encryption.
1727 *
1728 * This is the encryption scheme defined by RFC 8017
1729 * (PKCS#1: RSA Cryptography Specifications) under the name
1730 * RSAES-OAEP, with the message generation function MGF1.
1731 *
1732 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1733 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1734 * for MGF1.
1735 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001736 * \return The corresponding RSA OAEP encryption algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001737 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001738 * hash algorithm.
1739 */
1740#define PSA_ALG_RSA_OAEP(hash_alg) \
1741 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1742#define PSA_ALG_IS_RSA_OAEP(alg) \
1743 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1744#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1745 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1746 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1747 0)
1748
Maulik Patel28659c42021-01-06 14:09:22 +00001749#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001750/** Macro to build an HKDF algorithm.
1751 *
1752 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1753 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001754 * This key derivation algorithm uses the following inputs:
1755 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1756 * It is optional; if omitted, the derivation uses an empty salt.
1757 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1758 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1759 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1760 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1761 * starting to generate output.
1762 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001763 * \warning HKDF processes the salt as follows: first hash it with hash_alg
1764 * if the salt is longer than the block size of the hash algorithm; then
1765 * pad with null bytes up to the block size. As a result, it is possible
1766 * for distinct salt inputs to result in the same outputs. To ensure
1767 * unique outputs, it is recommended to use a fixed length for salt values.
1768 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001769 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1770 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1771 *
1772 * \return The corresponding HKDF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001773 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001774 * hash algorithm.
1775 */
1776#define PSA_ALG_HKDF(hash_alg) \
1777 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1778/** Whether the specified algorithm is an HKDF algorithm.
1779 *
1780 * HKDF is a family of key derivation algorithms that are based on a hash
1781 * function and the HMAC construction.
1782 *
1783 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1784 *
1785 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1786 * This macro may return either 0 or 1 if \c alg is not a supported
1787 * key derivation algorithm identifier.
1788 */
1789#define PSA_ALG_IS_HKDF(alg) \
1790 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1791#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1792 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1793
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001794#define PSA_ALG_HKDF_EXTRACT_BASE ((psa_algorithm_t)0x08000400)
1795/** Macro to build an HKDF-Extract algorithm.
1796 *
1797 * For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA256)` is
1798 * HKDF-Extract using HMAC-SHA-256.
1799 *
1800 * This key derivation algorithm uses the following inputs:
1801 * - PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1802 * - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the
1803 * "extract" step.
1804 * The inputs are mandatory and must be passed in the order above.
1805 * Each input may only be passed once.
1806 *
1807 * \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF
1808 * should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided
1809 * as a separate algorithm for the sake of protocols that use it as a
1810 * building block. It may also be a slight performance optimization
1811 * in applications that use HKDF with the same salt and key but many
1812 * different info strings.
1813 *
1814 * \warning HKDF processes the salt as follows: first hash it with hash_alg
1815 * if the salt is longer than the block size of the hash algorithm; then
1816 * pad with null bytes up to the block size. As a result, it is possible
1817 * for distinct salt inputs to result in the same outputs. To ensure
1818 * unique outputs, it is recommended to use a fixed length for salt values.
1819 *
1820 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1821 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1822 *
1823 * \return The corresponding HKDF-Extract algorithm.
1824 * \return Unspecified if \p hash_alg is not a supported
1825 * hash algorithm.
1826 */
1827#define PSA_ALG_HKDF_EXTRACT(hash_alg) \
1828 (PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1829/** Whether the specified algorithm is an HKDF-Extract algorithm.
1830 *
1831 * HKDF-Extract is a family of key derivation algorithms that are based
1832 * on a hash function and the HMAC construction.
1833 *
1834 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1835 *
1836 * \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.
1837 * This macro may return either 0 or 1 if \c alg is not a supported
1838 * key derivation algorithm identifier.
1839 */
1840#define PSA_ALG_IS_HKDF_EXTRACT(alg) \
1841 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)
1842
1843#define PSA_ALG_HKDF_EXPAND_BASE ((psa_algorithm_t)0x08000500)
1844/** Macro to build an HKDF-Expand algorithm.
1845 *
1846 * For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA256)` is
1847 * HKDF-Expand using HMAC-SHA-256.
1848 *
1849 * This key derivation algorithm uses the following inputs:
1850 * - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).
1851 * - PSA_KEY_DERIVATION_INPUT_INFO is the info string.
1852 *
1853 * The inputs are mandatory and must be passed in the order above.
1854 * Each input may only be passed once.
1855 *
1856 * \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`
1857 * should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as
1858 * a separate algorithm for the sake of protocols that use it as a building
1859 * block. It may also be a slight performance optimization in applications
1860 * that use HKDF with the same salt and key but many different info strings.
1861 *
1862 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1863 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1864 *
1865 * \return The corresponding HKDF-Expand algorithm.
1866 * \return Unspecified if \p hash_alg is not a supported
1867 * hash algorithm.
1868 */
1869#define PSA_ALG_HKDF_EXPAND(hash_alg) \
1870 (PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1871/** Whether the specified algorithm is an HKDF-Expand algorithm.
1872 *
1873 * HKDF-Expand is a family of key derivation algorithms that are based
1874 * on a hash function and the HMAC construction.
1875 *
1876 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1877 *
1878 * \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.
1879 * This macro may return either 0 or 1 if \c alg is not a supported
1880 * key derivation algorithm identifier.
1881 */
1882#define PSA_ALG_IS_HKDF_EXPAND(alg) \
1883 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1884
1885/** Whether the specified algorithm is an HKDF or HKDF-Extract or
1886 * HKDF-Expand algorithm.
1887 *
1888 *
1889 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1890 *
1891 * \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.
1892 * This macro may return either 0 or 1 if \c alg is not a supported
1893 * key derivation algorithm identifier.
1894 */
1895#define PSA_ALG_IS_ANY_HKDF(alg) \
1896 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE || \
1897 ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE || \
1898 ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1899
Maulik Patel28659c42021-01-06 14:09:22 +00001900#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001901/** Macro to build a TLS-1.2 PRF algorithm.
1902 *
1903 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1904 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1905 * used with either SHA-256 or SHA-384.
1906 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001907 * This key derivation algorithm uses the following inputs, which must be
1908 * passed in the order given here:
1909 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1910 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1911 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1912 *
1913 * For the application to TLS-1.2 key expansion, the seed is the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001914 * concatenation of ServerHello.Random + ClientHello.Random,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001915 * and the label is "key expansion".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001916 *
1917 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1918 * TLS 1.2 PRF using HMAC-SHA-256.
1919 *
1920 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1921 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1922 *
1923 * \return The corresponding TLS-1.2 PRF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001924 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001925 * hash algorithm.
1926 */
1927#define PSA_ALG_TLS12_PRF(hash_alg) \
1928 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1929
1930/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1931 *
1932 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1933 *
1934 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1935 * This macro may return either 0 or 1 if \c alg is not a supported
1936 * key derivation algorithm identifier.
1937 */
1938#define PSA_ALG_IS_TLS12_PRF(alg) \
1939 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1940#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1941 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1942
Maulik Patel28659c42021-01-06 14:09:22 +00001943#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001944/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1945 *
1946 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1947 * from the PreSharedKey (PSK) through the application of padding
1948 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1949 * The latter is based on HMAC and can be used with either SHA-256
1950 * or SHA-384.
1951 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001952 * This key derivation algorithm uses the following inputs, which must be
1953 * passed in the order given here:
1954 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001955 * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
1956 * computation of the premaster secret. This input is optional;
1957 * if omitted, it defaults to a string of null bytes with the same length
1958 * as the secret (PSK) input.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001959 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1960 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1961 *
1962 * For the application to TLS-1.2, the seed (which is
1963 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1964 * ClientHello.Random + ServerHello.Random,
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001965 * the label is "master secret" or "extended master secret" and
1966 * the other secret depends on the key exchange specified in the cipher suite:
1967 * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
1968 * PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
1969 * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
1970 * (RFC 5489, Section 2), the other secret should be the output of the
1971 * PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
1972 * The recommended way to pass this input is to use a key derivation
1973 * algorithm constructed as
1974 * PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
1975 * and to call psa_key_derivation_key_agreement(). Alternatively,
1976 * this input may be an output of `psa_raw_key_agreement()` passed with
1977 * psa_key_derivation_input_bytes(), or an equivalent input passed with
1978 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
1979 * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
1980 * should be the 48-byte client challenge (the PreMasterSecret of
1981 * (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
1982 * a 46-byte random string chosen by the client. On the server, this is
1983 * typically an output of psa_asymmetric_decrypt() using
1984 * PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
1985 * with `psa_key_derivation_input_bytes()`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001986 *
1987 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1988 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1989 *
1990 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1991 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1992 *
1993 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001994 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001995 * hash algorithm.
1996 */
1997#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1998 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1999
2000/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
2001 *
2002 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2003 *
2004 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
2005 * This macro may return either 0 or 1 if \c alg is not a supported
2006 * key derivation algorithm identifier.
2007 */
2008#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
2009 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
2010#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
2011 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
2012
Summer Qin359167d2021-07-05 18:11:50 +08002013/* This flag indicates whether the key derivation algorithm is suitable for
2014 * use on low-entropy secrets such as password - these algorithms are also
2015 * known as key stretching or password hashing schemes. These are also the
2016 * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
2017 *
2018 * Those algorithms cannot be combined with a key agreement algorithm.
2019 */
2020#define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t)0x00800000)
2021
2022#define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t)0x08800100)
2023/** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
2024 *
2025 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2026 * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
2027 * HMAC with the specified hash.
2028 * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA256)` specifies PBKDF2
2029 * using the PRF HMAC-SHA-256.
2030 *
2031 * This key derivation algorithm uses the following inputs, which must be
2032 * provided in the following order:
2033 * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
2034 * This input step must be used exactly once.
2035 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
2036 * This input step must be used one or more times; if used several times, the
2037 * inputs will be concatenated. This can be used to build the final salt
2038 * from multiple sources, both public and secret (also known as pepper).
2039 * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
2040 * This input step must be used exactly once.
2041 *
2042 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
2043 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
2044 *
2045 * \return The corresponding PBKDF2-HMAC-XXX algorithm.
2046 * \return Unspecified if \p hash_alg is not a supported
2047 * hash algorithm.
2048 */
2049#define PSA_ALG_PBKDF2_HMAC(hash_alg) \
2050 (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2051
2052/** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
2053 *
2054 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2055 *
2056 * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
2057 * This macro may return either 0 or 1 if \c alg is not a supported
2058 * key derivation algorithm identifier.
2059 */
2060#define PSA_ALG_IS_PBKDF2_HMAC(alg) \
2061 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
2062
2063/** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
2064 *
2065 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2066 * This macro specifies the PBKDF2 algorithm constructed using the
2067 * AES-CMAC-PRF-128 PRF specified by RFC 4615.
2068 *
2069 * This key derivation algorithm uses the same inputs as
2070 * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
2071 */
2072#define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t)0x08800200)
2073
Maulik Patel28659c42021-01-06 14:09:22 +00002074#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
2075#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002076
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002077/** Macro to build a combined algorithm that chains a key agreement with
2078 * a key derivation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002079 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002080 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
2081 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
2082 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
2083 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002084 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002085 * \return The corresponding key agreement and derivation
2086 * algorithm.
2087 * \return Unspecified if \p ka_alg is not a supported
2088 * key agreement algorithm or \p kdf_alg is not a
2089 * supported key derivation algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002090 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002091#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
2092 ((ka_alg) | (kdf_alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002093
2094#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
2095 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
2096
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002097#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
2098 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002099
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002100/** Whether the specified algorithm is a raw key agreement algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002101 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002102 * A raw key agreement algorithm is one that does not specify
2103 * a key derivation function.
2104 * Usually, raw key agreement algorithms are constructed directly with
2105 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
Maulik Patel28659c42021-01-06 14:09:22 +00002106 * constructed with #PSA_ALG_KEY_AGREEMENT().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002107 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002108 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2109 *
2110 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
2111 * This macro may return either 0 or 1 if \p alg is not a supported
2112 * algorithm identifier.
2113 */
2114#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
2115 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
2116 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
2117
2118#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
2119 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
2120
2121/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
2122 *
2123 * The shared secret produced by key agreement is
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002124 * `g^{ab}` in big-endian format.
2125 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
2126 * in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002127 */
Maulik Patel28659c42021-01-06 14:09:22 +00002128#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002129
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002130/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
2131 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002132 * This includes the raw finite field Diffie-Hellman algorithm as well as
2133 * finite-field Diffie-Hellman followed by any supporter key derivation
2134 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002135 *
2136 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2137 *
2138 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
2139 * This macro may return either 0 or 1 if \c alg is not a supported
2140 * key agreement algorithm identifier.
2141 */
2142#define PSA_ALG_IS_FFDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002143 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002144
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002145/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
2146 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002147 * The shared secret produced by key agreement is the x-coordinate of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002148 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
2149 * `m` is the bit size associated with the curve, i.e. the bit size of the
2150 * order of the curve's coordinate field. When `m` is not a multiple of 8,
2151 * the byte containing the most significant bit of the shared secret
2152 * is padded with zero bits. The byte order is either little-endian
2153 * or big-endian depending on the curve type.
2154 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08002155 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002156 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2157 * in little-endian byte order.
2158 * The bit size is 448 for Curve448 and 255 for Curve25519.
2159 * - For Weierstrass curves over prime fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08002160 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002161 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2162 * in big-endian byte order.
2163 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
2164 * - For Weierstrass curves over binary fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08002165 * `PSA_ECC_FAMILY_SECTXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002166 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2167 * in big-endian byte order.
2168 * The bit size is `m` for the field `F_{2^m}`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002169 */
Maulik Patel28659c42021-01-06 14:09:22 +00002170#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002171
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002172/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2173 * algorithm.
2174 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002175 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2176 * elliptic curve Diffie-Hellman followed by any supporter key derivation
2177 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002178 *
2179 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2180 *
2181 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2182 * 0 otherwise.
2183 * This macro may return either 0 or 1 if \c alg is not a supported
2184 * key agreement algorithm identifier.
2185 */
2186#define PSA_ALG_IS_ECDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002187 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002188
2189/** Whether the specified algorithm encoding is a wildcard.
2190 *
2191 * Wildcard values may only be used to set the usage algorithm field in
2192 * a policy, not to perform an operation.
2193 *
2194 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2195 *
2196 * \return 1 if \c alg is a wildcard algorithm encoding.
2197 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2198 * an operation).
2199 * \return This macro may return either 0 or 1 if \c alg is not a supported
2200 * algorithm identifier.
2201 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01002202#define PSA_ALG_IS_WILDCARD(alg) \
2203 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
2204 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
2205 PSA_ALG_IS_MAC(alg) ? \
2206 (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
2207 PSA_ALG_IS_AEAD(alg) ? \
2208 (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002209 (alg) == PSA_ALG_ANY_HASH)
2210
Summer Qin359167d2021-07-05 18:11:50 +08002211/** Get the hash used by a composite algorithm.
2212 *
2213 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2214 *
2215 * \return The underlying hash algorithm if alg is a composite algorithm that
2216 * uses a hash algorithm.
2217 *
2218 * \return \c 0 if alg is not a composite algorithm that uses a hash.
2219 */
2220#define PSA_ALG_GET_HASH(alg) \
2221 (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t)0) : 0x02000000 | ((alg) & 0x000000ff))
2222
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002223/**@}*/
2224
2225/** \defgroup key_lifetimes Key lifetimes
2226 * @{
2227 */
2228
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002229/* Note that location and persistence level values are embedded in the
2230 * persistent key store, as part of key metadata. As a consequence, they
2231 * must not be changed (unless the storage format version changes).
2232 */
2233
Soby Mathew07ef6e42020-07-20 21:09:23 +01002234/** The default lifetime for volatile keys.
2235 *
Maulik Patel28659c42021-01-06 14:09:22 +00002236 * A volatile key only exists as long as the identifier to it is not destroyed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002237 * The key material is guaranteed to be erased on a power reset.
Soby Mathew07ef6e42020-07-20 21:09:23 +01002238 *
2239 * A key with this lifetime is typically stored in the RAM area of the
2240 * PSA Crypto subsystem. However this is an implementation choice.
2241 * If an implementation stores data about the key in a non-volatile memory,
2242 * it must release all the resources associated with the key and erase the
2243 * key material if the calling application terminates.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002244 */
2245#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
2246
Soby Mathew07ef6e42020-07-20 21:09:23 +01002247/** The default lifetime for persistent keys.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002248 *
2249 * A persistent key remains in storage until it is explicitly destroyed or
2250 * until the corresponding storage area is wiped. This specification does
Maulik Patel13b27cf2021-05-14 11:44:53 +01002251 * not define any mechanism to wipe a storage area, but integrations may
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002252 * provide their own mechanism (for example to perform a factory reset,
2253 * to prepare for device refurbishment, or to uninstall an application).
2254 *
2255 * This lifetime value is the default storage area for the calling
Maulik Patel13b27cf2021-05-14 11:44:53 +01002256 * application. Integrations of Mbed TLS may support other persistent lifetimes.
Soby Mathew07ef6e42020-07-20 21:09:23 +01002257 * See ::psa_key_lifetime_t for more information.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002258 */
2259#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
2260
Soby Mathew07ef6e42020-07-20 21:09:23 +01002261/** The persistence level of volatile keys.
2262 *
2263 * See ::psa_key_persistence_t for more information.
2264 */
2265#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
2266
2267/** The default persistence level for persistent keys.
2268 *
2269 * See ::psa_key_persistence_t for more information.
2270 */
2271#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
2272
2273/** A persistence level indicating that a key is never destroyed.
2274 *
2275 * See ::psa_key_persistence_t for more information.
2276 */
2277#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
2278
2279#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
2280 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
2281
2282#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
2283 ((psa_key_location_t)((lifetime) >> 8))
2284
2285/** Whether a key lifetime indicates that the key is volatile.
2286 *
2287 * A volatile key is automatically destroyed by the implementation when
2288 * the application instance terminates. In particular, a volatile key
2289 * is automatically destroyed on a power reset of the device.
2290 *
2291 * A key that is not volatile is persistent. Persistent keys are
2292 * preserved until the application explicitly destroys them or until an
2293 * implementation-specific device management event occurs (for example,
2294 * a factory reset).
2295 *
2296 * \param lifetime The lifetime value to query (value of type
2297 * ::psa_key_lifetime_t).
2298 *
2299 * \return \c 1 if the key is volatile, otherwise \c 0.
2300 */
2301#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
2302 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
Summer Qin0e5b2e02020-10-22 11:23:39 +08002303 PSA_KEY_PERSISTENCE_VOLATILE)
Soby Mathew07ef6e42020-07-20 21:09:23 +01002304
Summer Qin359167d2021-07-05 18:11:50 +08002305/** Whether a key lifetime indicates that the key is read-only.
2306 *
2307 * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2308 * They must be created through platform-specific means that bypass the API.
2309 *
2310 * Some platforms may offer ways to destroy read-only keys. For example,
2311 * consider a platform with multiple levels of privilege, where a
2312 * low-privilege application can use a key but is not allowed to destroy
2313 * it, and the platform exposes the key to the application with a read-only
2314 * lifetime. High-privilege code can destroy the key even though the
2315 * application sees the key as read-only.
2316 *
2317 * \param lifetime The lifetime value to query (value of type
2318 * ::psa_key_lifetime_t).
2319 *
2320 * \return \c 1 if the key is read-only, otherwise \c 0.
2321 */
2322#define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
2323 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2324 PSA_KEY_PERSISTENCE_READ_ONLY)
2325
Soby Mathew07ef6e42020-07-20 21:09:23 +01002326/** Construct a lifetime from a persistence level and a location.
2327 *
2328 * \param persistence The persistence level
2329 * (value of type ::psa_key_persistence_t).
2330 * \param location The location indicator
2331 * (value of type ::psa_key_location_t).
2332 *
2333 * \return The constructed lifetime value.
2334 */
2335#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2336 ((location) << 8 | (persistence))
2337
2338/** The local storage area for persistent keys.
2339 *
2340 * This storage area is available on all systems that can store persistent
2341 * keys without delegating the storage to a third-party cryptoprocessor.
2342 *
2343 * See ::psa_key_location_t for more information.
2344 */
2345#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
2346
2347#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
2348
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002349/* Note that key identifier values are embedded in the
2350 * persistent key store, as part of key metadata. As a consequence, they
2351 * must not be changed (unless the storage format version changes).
2352 */
2353
Summer Qinf07cc312022-01-05 16:52:54 +08002354/** The null key identifier.
2355 */
2356#define PSA_KEY_ID_NULL ((psa_key_id_t)0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002357/** The minimum value for a key identifier chosen by the application.
2358 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002359#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002360/** The maximum value for a key identifier chosen by the application.
2361 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002362#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002363/** The minimum value for a key identifier chosen by the implementation.
2364 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002365#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002366/** The maximum value for a key identifier chosen by the implementation.
2367 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01002368#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002369
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002370/**@}*/
2371
2372/** \defgroup policy Key policies
2373 * @{
2374 */
2375
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002376/* Note that key usage flags are embedded in the
2377 * persistent key store, as part of key metadata. As a consequence, they
2378 * must not be changed (unless the storage format version changes).
2379 */
2380
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002381/** Whether the key may be exported.
2382 *
2383 * A public key or the public part of a key pair may always be exported
2384 * regardless of the value of this permission flag.
2385 *
2386 * If a key does not have export permission, implementations shall not
2387 * allow the key to be exported in plain form from the cryptoprocessor,
2388 * whether through psa_export_key() or through a proprietary interface.
2389 * The key may however be exportable in a wrapped form, i.e. in a form
2390 * where it is encrypted by another key.
2391 */
2392#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
2393
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002394/** Whether the key may be copied.
2395 *
2396 * This flag allows the use of psa_copy_key() to make a copy of the key
2397 * with the same policy or a more restrictive policy.
2398 *
2399 * For lifetimes for which the key is located in a secure element which
2400 * enforce the non-exportability of keys, copying a key outside the secure
2401 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2402 * Copying the key inside the secure element is permitted with just
2403 * #PSA_KEY_USAGE_COPY if the secure element supports it.
2404 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2405 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2406 * is sufficient to permit the copy.
2407 */
2408#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
2409
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002410/** Whether the key may be used to encrypt a message.
2411 *
2412 * This flag allows the key to be used for a symmetric encryption operation,
2413 * for an AEAD encryption-and-authentication operation,
2414 * or for an asymmetric encryption operation,
2415 * if otherwise permitted by the key's type and policy.
2416 *
2417 * For a key pair, this concerns the public key.
2418 */
2419#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
2420
2421/** Whether the key may be used to decrypt a message.
2422 *
2423 * This flag allows the key to be used for a symmetric decryption operation,
2424 * for an AEAD decryption-and-verification operation,
2425 * or for an asymmetric decryption operation,
2426 * if otherwise permitted by the key's type and policy.
2427 *
2428 * For a key pair, this concerns the private key.
2429 */
2430#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
2431
2432/** Whether the key may be used to sign a message.
2433 *
Summer Qin359167d2021-07-05 18:11:50 +08002434 * This flag allows the key to be used for a MAC calculation operation or for
2435 * an asymmetric message signature operation, if otherwise permitted by the
2436 * key’s type and policy.
2437 *
2438 * For a key pair, this concerns the private key.
2439 */
2440#define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t)0x00000400)
2441
2442/** Whether the key may be used to verify a message.
2443 *
2444 * This flag allows the key to be used for a MAC verification operation or for
2445 * an asymmetric message signature verification operation, if otherwise
2446 * permitted by the key’s type and policy.
2447 *
2448 * For a key pair, this concerns the public key.
2449 */
2450#define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t)0x00000800)
2451
2452/** Whether the key may be used to sign a message.
2453 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002454 * This flag allows the key to be used for a MAC calculation operation
2455 * or for an asymmetric signature operation,
2456 * if otherwise permitted by the key's type and policy.
2457 *
2458 * For a key pair, this concerns the private key.
2459 */
Maulik Patel28659c42021-01-06 14:09:22 +00002460#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002461
2462/** Whether the key may be used to verify a message signature.
2463 *
2464 * This flag allows the key to be used for a MAC verification operation
2465 * or for an asymmetric signature verification operation,
2466 * if otherwise permitted by by the key's type and policy.
2467 *
2468 * For a key pair, this concerns the public key.
2469 */
Maulik Patel28659c42021-01-06 14:09:22 +00002470#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002471
Summer Qin359167d2021-07-05 18:11:50 +08002472/** Whether the key may be used to derive other keys or produce a password
2473 * hash.
2474 *
2475 * This flag allows the key to be used for a key derivation operation or for
2476 * a key agreement operation, if otherwise permitted by by the key's type and
2477 * policy.
2478 *
2479 * If this flag is present on all keys used in calls to
2480 * psa_key_derivation_input_key() for a key derivation operation, then it
2481 * permits calling psa_key_derivation_output_bytes() or
2482 * psa_key_derivation_output_key() at the end of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002483 */
Maulik Patel28659c42021-01-06 14:09:22 +00002484#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002485
Summer Qin359167d2021-07-05 18:11:50 +08002486/** Whether the key may be used to verify the result of a key derivation,
2487 * including password hashing.
2488 *
2489 * This flag allows the key to be used:
2490 *
2491 * This flag allows the key to be used in a key derivation operation, if
2492 * otherwise permitted by by the key's type and policy.
2493 *
2494 * If this flag is present on all keys used in calls to
2495 * psa_key_derivation_input_key() for a key derivation operation, then it
2496 * permits calling psa_key_derivation_verify_bytes() or
2497 * psa_key_derivation_verify_key() at the end of the operation.
2498 */
2499#define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t)0x00008000)
2500
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002501/**@}*/
2502
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002503/** \defgroup derivation Key derivation
2504 * @{
2505 */
2506
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002507/* Key input steps are not embedded in the persistent storage, so you can
2508 * change them if needed: it's only an ABI change. */
2509
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002510/** A secret input for key derivation.
2511 *
2512 * This should be a key of type #PSA_KEY_TYPE_DERIVE
2513 * (passed to psa_key_derivation_input_key())
2514 * or the shared secret resulting from a key agreement
2515 * (obtained via psa_key_derivation_key_agreement()).
2516 *
2517 * The secret can also be a direct input (passed to
2518 * key_derivation_input_bytes()). In this case, the derivation operation
2519 * may not be used to derive keys: the operation will only allow
Summer Qin359167d2021-07-05 18:11:50 +08002520 * psa_key_derivation_output_bytes(),
2521 * psa_key_derivation_verify_bytes(), or
2522 * psa_key_derivation_verify_key(), but not
2523 * psa_key_derivation_output_key().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002524 */
2525#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
2526
Summer Qin359167d2021-07-05 18:11:50 +08002527/** A low-entropy secret input for password hashing / key stretching.
2528 *
2529 * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2530 * psa_key_derivation_input_key()) or a direct input (passed to
2531 * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2532 * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2533 * the shared secret resulting from a key agreement.
2534 *
2535 * The secret can also be a direct input (passed to
2536 * key_derivation_input_bytes()). In this case, the derivation operation
2537 * may not be used to derive keys: the operation will only allow
2538 * psa_key_derivation_output_bytes(),
2539 * psa_key_derivation_verify_bytes(), or
2540 * psa_key_derivation_verify_key(), but not
2541 * psa_key_derivation_output_key().
2542 */
2543#define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t)0x0102)
2544
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002545/** A high-entropy additional secret input for key derivation.
2546 *
2547 * This is typically the shared secret resulting from a key agreement obtained
2548 * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
2549 * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
2550 * a direct input passed to `psa_key_derivation_input_bytes()`.
2551 */
2552#define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
2553 ((psa_key_derivation_step_t)0x0103)
2554
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002555/** A label for key derivation.
2556 *
2557 * This should be a direct input.
2558 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2559 */
2560#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
2561
2562/** A salt for key derivation.
2563 *
2564 * This should be a direct input.
Summer Qin359167d2021-07-05 18:11:50 +08002565 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2566 * #PSA_KEY_TYPE_PEPPER.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002567 */
2568#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
2569
2570/** An information string for key derivation.
2571 *
2572 * This should be a direct input.
2573 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2574 */
2575#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
2576
2577/** A seed for key derivation.
2578 *
2579 * This should be a direct input.
2580 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2581 */
2582#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
2583
Summer Qin359167d2021-07-05 18:11:50 +08002584/** A cost parameter for password hashing / key stretching.
2585 *
2586 * This must be a direct input, passed to psa_key_derivation_input_integer().
2587 */
2588#define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t)0x0205)
2589
2590/**@}*/
2591
2592/** \defgroup helper_macros Helper macros
2593 * @{
2594 */
2595
2596/* Helper macros */
2597
2598/** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2599 * regardless of the tag length they encode.
2600 *
2601 * \param aead_alg_1 An AEAD algorithm identifier.
2602 * \param aead_alg_2 An AEAD algorithm identifier.
2603 *
2604 * \return 1 if both identifiers refer to the same AEAD algorithm,
2605 * 0 otherwise.
2606 * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2607 * a supported AEAD algorithm.
2608 */
2609#define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2610 (!(((aead_alg_1) ^ (aead_alg_2)) & \
2611 ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2612
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002613/**@}*/
2614
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002615#endif /* PSA_CRYPTO_VALUES_H */