blob: 9cca6b24cf00bd5e1cc591cac46bec4dd6c0cb3b [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto_values.h
Jamie Fox0e54ebc2019-04-09 14:21:04 +01009 *
10 * \brief PSA cryptography module: macros to build and analyze integer values.
11 *
12 * \note This file may not be included directly. Applications must
Jamie Foxcc31d402019-01-28 17:13:52 +000013 * include psa/crypto.h. Drivers must include the appropriate driver
Jamie Fox0e54ebc2019-04-09 14:21:04 +010014 * header file.
15 *
16 * This file contains portable definitions of macros to build and analyze
17 * values of integral types that encode properties of cryptographic keys,
18 * designations of cryptographic algorithms, and error codes returned by
19 * the library.
20 *
21 * This header file only defines preprocessor macros.
22 */
23
24#ifndef PSA_CRYPTO_VALUES_H
25#define PSA_CRYPTO_VALUES_H
26
27/** \defgroup error Error codes
28 * @{
29 */
30
31/* PSA error codes */
32
33/** The action was completed successfully. */
34#ifndef PSA_SUCCESS
35#define PSA_SUCCESS ((psa_status_t)0)
36#endif
37
38/** An error occurred that does not correspond to any defined
39 * failure cause.
40 *
41 * Implementations may use this error code if none of the other standard
42 * error codes are applicable. */
43#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
44
45/** The requested operation or a parameter is not supported
46 * by this implementation.
47 *
48 * Implementations should return this error code when an enumeration
49 * parameter such as a key type, algorithm, etc. is not recognized.
50 * If a combination of parameters is recognized and identified as
51 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
52#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
53
54/** The requested action is denied by a policy.
55 *
56 * Implementations should return this error code when the parameters
57 * are recognized as valid and supported, and a policy explicitly
58 * denies the requested operation.
59 *
60 * If a subset of the parameters of a function call identify a
61 * forbidden operation, and another subset of the parameters are
62 * not valid or not supported, it is unspecified whether the function
63 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
64 * #PSA_ERROR_INVALID_ARGUMENT. */
65#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
66
67/** An output buffer is too small.
68 *
69 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
70 * description to determine a sufficient buffer size.
71 *
72 * Implementations should preferably return this error code only
73 * in cases when performing the operation with a larger output
74 * buffer would succeed. However implementations may return this
75 * error if a function has invalid or unsupported parameters in addition
76 * to the parameters that determine the necessary output buffer size. */
77#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
78
79/** Asking for an item that already exists
80 *
81 * Implementations should return this error, when attempting
82 * to write an item (like a key) that already exists. */
83#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
84
85/** Asking for an item that doesn't exist
86 *
87 * Implementations should return this error, if a requested item (like
88 * a key) does not exist. */
89#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
90
91/** The requested action cannot be performed in the current state.
92 *
93 * Multipart operations return this error when one of the
94 * functions is called out of sequence. Refer to the function
95 * descriptions for permitted sequencing of functions.
96 *
97 * Implementations shall not return this error code to indicate
Antonio de Angelis04debbd2019-10-14 12:12:52 +010098 * that a key either exists or not,
99 * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
100 * as applicable.
101 *
102 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000103 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100104 * instead. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100105#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
106
107/** The parameters passed to the function are invalid.
108 *
109 * Implementations may return this error any time a parameter or
110 * combination of parameters are recognized as invalid.
111 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100112 * Implementations shall not return this error code to indicate that a
Maulik Patel28659c42021-01-06 14:09:22 +0000113 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100114 * instead.
115 */
116#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
117
118/** There is not enough runtime memory.
119 *
120 * If the action is carried out across multiple security realms, this
121 * error can refer to available memory in any of the security realms. */
122#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
123
124/** There is not enough persistent storage.
125 *
126 * Functions that modify the key storage return this error code if
127 * there is insufficient storage space on the host media. In addition,
128 * many functions that do not otherwise access storage may return this
129 * error code if the implementation requires a mandatory log entry for
130 * the requested action and the log storage space is full. */
131#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
132
133/** There was a communication failure inside the implementation.
134 *
135 * This can indicate a communication failure between the application
136 * and an external cryptoprocessor or between the cryptoprocessor and
137 * an external volatile or persistent memory. A communication failure
138 * may be transient or permanent depending on the cause.
139 *
140 * \warning If a function returns this error, it is undetermined
141 * whether the requested action has completed or not. Implementations
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100142 * should return #PSA_SUCCESS on successful completion whenever
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100143 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
144 * if the requested action was completed successfully in an external
145 * cryptoprocessor but there was a breakdown of communication before
146 * the cryptoprocessor could report the status to the application.
147 */
148#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
149
150/** There was a storage failure that may have led to data loss.
151 *
152 * This error indicates that some persistent storage is corrupted.
153 * It should not be used for a corruption of volatile memory
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100154 * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100155 * between the cryptoprocessor and its external storage (use
156 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
157 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
158 *
159 * Note that a storage failure does not indicate that any data that was
160 * previously read is invalid. However this previously read data may no
161 * longer be readable from storage.
162 *
163 * When a storage failure occurs, it is no longer possible to ensure
164 * the global integrity of the keystore. Depending on the global
165 * integrity guarantees offered by the implementation, access to other
166 * data may or may not fail even if the data is still readable but
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100167 * its integrity cannot be guaranteed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100168 *
169 * Implementations should only use this error code to report a
170 * permanent storage corruption. However application writers should
171 * keep in mind that transient errors while reading the storage may be
172 * reported using this error code. */
173#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
174
175/** A hardware failure was detected.
176 *
177 * A hardware failure may be transient or permanent depending on the
178 * cause. */
179#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
180
181/** A tampering attempt was detected.
182 *
183 * If an application receives this error code, there is no guarantee
184 * that previously accessed or computed data was correct and remains
185 * confidential. Applications should not perform any security function
186 * and should enter a safe failure state.
187 *
188 * Implementations may return this error code if they detect an invalid
189 * state that cannot happen during normal operation and that indicates
190 * that the implementation's security guarantees no longer hold. Depending
191 * on the implementation architecture and on its security and safety goals,
192 * the implementation may forcibly terminate the application.
193 *
194 * This error code is intended as a last resort when a security breach
195 * is detected and it is unsure whether the keystore data is still
196 * protected. Implementations shall only return this error code
197 * to report an alarm from a tampering detector, to indicate that
198 * the confidentiality of stored data can no longer be guaranteed,
199 * or to indicate that the integrity of previously returned data is now
200 * considered compromised. Implementations shall not use this error code
201 * to indicate a hardware failure that merely makes it impossible to
202 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
203 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
204 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
205 * instead).
206 *
207 * This error indicates an attack against the application. Implementations
208 * shall not return this error code as a consequence of the behavior of
209 * the application itself. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100210#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100211
212/** There is not enough entropy to generate random data needed
213 * for the requested action.
214 *
215 * This error indicates a failure of a hardware random generator.
216 * Application writers should note that this error can be returned not
217 * only by functions whose purpose is to generate random data, such
218 * as key, IV or nonce generation, but also by functions that execute
219 * an algorithm with a randomized result, as well as functions that
220 * use randomization of intermediate computations as a countermeasure
221 * to certain attacks.
222 *
223 * Implementations should avoid returning this error after psa_crypto_init()
224 * has succeeded. Implementations should generate sufficient
225 * entropy during initialization and subsequently use a cryptographically
226 * secure pseudorandom generator (PRNG). However implementations may return
227 * this error at any time if a policy requires the PRNG to be reseeded
228 * during normal operation. */
229#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
230
231/** The signature, MAC or hash is incorrect.
232 *
233 * Verification functions return this error if the verification
234 * calculations completed successfully, and the value to be verified
235 * was determined to be incorrect.
236 *
237 * If the value to verify has an invalid size, implementations may return
238 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
239#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
240
241/** The decrypted padding is incorrect.
242 *
243 * \warning In some protocols, when decrypting data, it is essential that
244 * the behavior of the application does not depend on whether the padding
245 * is correct, down to precise timing. Applications should prefer
246 * protocols that use authenticated encryption rather than plain
247 * encryption. If the application must perform a decryption of
248 * unauthenticated data, the application writer should take care not
249 * to reveal whether the padding is invalid.
250 *
251 * Implementations should strive to make valid and invalid padding
252 * as close as possible to indistinguishable to an external observer.
253 * In particular, the timing of a decryption operation should not
254 * depend on the validity of the padding. */
255#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
256
257/** Return this error when there's insufficient data when attempting
258 * to read from a resource. */
259#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
260
Maulik Patel28659c42021-01-06 14:09:22 +0000261/** The key identifier is not valid. See also :ref:\`key-handles\`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100262 */
263#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
264
265/**@}*/
266
267/** \defgroup crypto_types Key and algorithm types
268 * @{
269 */
270
271/** An invalid key type value.
272 *
273 * Zero is not the encoding of any key type.
274 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100275#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100276
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100277/** Vendor-defined key type flag.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100278 *
279 * Key types defined by this standard will never have the
280 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
281 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
282 * respect the bitwise structure used by standard encodings whenever practical.
283 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100284#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100285
Soby Mathew07ef6e42020-07-20 21:09:23 +0100286#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
287#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
288#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
289#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
290#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100291
Soby Mathew07ef6e42020-07-20 21:09:23 +0100292#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100293
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100294/** Whether a key type is vendor-defined.
295 *
296 * See also #PSA_KEY_TYPE_VENDOR_FLAG.
297 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100298#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
299 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
300
301/** Whether a key type is an unstructured array of bytes.
302 *
303 * This encompasses both symmetric keys and non-key data.
304 */
305#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100306 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
307 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100308
309/** Whether a key type is asymmetric: either a key pair or a public key. */
310#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
311 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
312 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
313 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
314/** Whether a key type is the public part of a key pair. */
315#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
316 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
317/** Whether a key type is a key pair containing a private part and a public
318 * part. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100319#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100320 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
321/** The key pair type corresponding to a public key type.
322 *
323 * You may also pass a key pair type as \p type, it will be left unchanged.
324 *
325 * \param type A public key type or key pair type.
326 *
327 * \return The corresponding key pair type.
328 * If \p type is not a public key or a key pair,
329 * the return value is undefined.
330 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100331#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100332 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
333/** The public key type corresponding to a key pair type.
334 *
335 * You may also pass a key pair type as \p type, it will be left unchanged.
336 *
337 * \param type A public key type or key pair type.
338 *
339 * \return The corresponding public key type.
340 * If \p type is not a public key or a key pair,
341 * the return value is undefined.
342 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100343#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100344 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
345
346/** Raw data.
347 *
348 * A "key" of this type cannot be used for any cryptographic operation.
349 * Applications may use this type to store arbitrary data in the keystore. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100350#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100351
352/** HMAC key.
353 *
354 * The key policy determines which underlying hash algorithm the key can be
355 * used for.
356 *
357 * HMAC keys should generally have the same size as the underlying hash.
358 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
359 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100360#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100361
362/** A secret for key derivation.
363 *
364 * The key policy determines which key derivation algorithm the key
365 * can be used for.
366 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100367#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100368
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100369/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100370 *
371 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
372 * 32 bytes (AES-256).
373 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100374#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100375
376/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
377 *
378 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
379 * 24 bytes (3-key 3DES).
380 *
381 * Note that single DES and 2-key 3DES are weak and strongly
382 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
383 * is weak and deprecated and should only be used in legacy protocols.
384 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100385#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100386
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100387/** Key for a cipher, AEAD or MAC algorithm based on the
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100388 * Camellia block cipher. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100389#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100390
391/** Key for the RC4 stream cipher.
392 *
393 * Note that RC4 is weak and deprecated and should only be used in
394 * legacy protocols. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100395#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x2002)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100396
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100397/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
398 *
399 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
400 *
401 * Implementations must support 12-byte nonces, may support 8-byte nonces,
402 * and should reject other sizes.
403 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100404#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100405
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100406/** RSA public key. */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100407#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100408/** RSA key pair (private and public key). */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100409#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100410/** Whether a key type is an RSA key (pair or public-only). */
411#define PSA_KEY_TYPE_IS_RSA(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100412 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100413
Soby Mathew07ef6e42020-07-20 21:09:23 +0100414#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
415#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
416#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100417/** Elliptic curve key pair.
418 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800419 * \param curve A value of type ::psa_ecc_family_t that
420 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100421 */
422#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
423 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
424/** Elliptic curve public key.
425 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800426 * \param curve A value of type ::psa_ecc_family_t that
427 * identifies the ECC curve to be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100428 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100429#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
430 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
431
432/** Whether a key type is an elliptic curve key (pair or public-only). */
433#define PSA_KEY_TYPE_IS_ECC(type) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100434 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100435 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
436/** Whether a key type is an elliptic curve key pair. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100437#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100438 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100439 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100440/** Whether a key type is an elliptic curve public key. */
441#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
442 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
443 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
444
445/** Extract the curve from an elliptic curve key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800446#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
447 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100448 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
449 0))
450
Soby Mathew07ef6e42020-07-20 21:09:23 +0100451/** SEC Koblitz curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100452 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100453 * This family comprises the following curves:
454 * secp192k1, secp224k1, secp256k1.
455 * They are defined in _Standards for Efficient Cryptography_,
456 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
457 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100458 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800459#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100460
Soby Mathew07ef6e42020-07-20 21:09:23 +0100461/** SEC random curves over prime fields.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100462 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100463 * This family comprises the following curves:
464 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
465 * They are defined in _Standards for Efficient Cryptography_,
466 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
467 * https://www.secg.org/sec2-v2.pdf
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100468 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800469#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100470/* SECP160R2 (SEC2 v1, obsolete) */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800471#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100472
Soby Mathew07ef6e42020-07-20 21:09:23 +0100473/** SEC Koblitz curves over binary fields.
474 *
475 * This family comprises the following curves:
476 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
477 * They are defined in _Standards for Efficient Cryptography_,
478 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
479 * https://www.secg.org/sec2-v2.pdf
480 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800481#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100482
483/** SEC random curves over binary fields.
484 *
485 * This family comprises the following curves:
486 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
487 * They are defined in _Standards for Efficient Cryptography_,
488 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
489 * https://www.secg.org/sec2-v2.pdf
490 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800491#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100492
493/** SEC additional random curves over binary fields.
494 *
495 * This family comprises the following curve:
496 * sect163r2.
497 * It is defined in _Standards for Efficient Cryptography_,
498 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
499 * https://www.secg.org/sec2-v2.pdf
500 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800501#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100502
503/** Brainpool P random curves.
504 *
505 * This family comprises the following curves:
506 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
507 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
508 * It is defined in RFC 5639.
509 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800510#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100511
512/** Curve25519 and Curve448.
513 *
514 * This family comprises the following Montgomery curves:
515 * - 255-bit: Bernstein et al.,
516 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
517 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
518 * - 448-bit: Hamburg,
519 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
520 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
521 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800522#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
Soby Mathew07ef6e42020-07-20 21:09:23 +0100523
524#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
525#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
526#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100527/** Diffie-Hellman key pair.
528 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800529 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100530 * Diffie-Hellman group to be used.
531 */
532#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
533 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
534/** Diffie-Hellman public key.
535 *
Summer Qin0e5b2e02020-10-22 11:23:39 +0800536 * \param group A value of type ::psa_dh_family_t that identifies the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100537 * Diffie-Hellman group to be used.
538 */
539#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
540 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
541
542/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
543#define PSA_KEY_TYPE_IS_DH(type) \
544 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
545 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
546/** Whether a key type is a Diffie-Hellman key pair. */
547#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
548 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
549 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
550/** Whether a key type is a Diffie-Hellman public key. */
551#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
552 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
553 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
554
555/** Extract the group from a Diffie-Hellman key type. */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800556#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
557 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100558 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
559 0))
560
Soby Mathew07ef6e42020-07-20 21:09:23 +0100561/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100562 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100563 * This family includes groups with the following key sizes (in bits):
564 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
565 * all of these sizes or only a subset.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100566 */
Summer Qin0e5b2e02020-10-22 11:23:39 +0800567#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100568
Soby Mathew07ef6e42020-07-20 21:09:23 +0100569#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
570 (((type) >> 8) & 7)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100571/** The block size of a block cipher.
572 *
573 * \param type A cipher key type (value of type #psa_key_type_t).
574 *
575 * \return The block size for a block cipher, or 1 for a stream cipher.
576 * The return value is undefined if \p type is not a supported
577 * cipher key type.
578 *
579 * \note It is possible to build stream cipher algorithms on top of a block
580 * cipher, for example CTR mode (#PSA_ALG_CTR).
581 * This macro only takes the key type into account, so it cannot be
582 * used to determine the size of the data that #psa_cipher_update()
583 * might buffer for future processing in general.
584 *
585 * \note This macro returns a compile-time constant if its argument is one.
586 *
587 * \warning This macro may evaluate its argument multiple times.
588 */
589#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Soby Mathew07ef6e42020-07-20 21:09:23 +0100590 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
591 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
592 0u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100593
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100594/** Vendor-defined algorithm flag.
595 *
596 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
597 * bit set. Vendors who define additional algorithms must use an encoding with
598 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
599 * used by standard encodings whenever practical.
600 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100601#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100602
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100603#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000604#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
605#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100606#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
Maulik Patel28659c42021-01-06 14:09:22 +0000607#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
608#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
609#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
610#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
611#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100612
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100613/** Whether an algorithm is vendor-defined.
614 *
615 * See also #PSA_ALG_VENDOR_FLAG.
616 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100617#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
618 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
619
620/** Whether the specified algorithm is a hash algorithm.
621 *
622 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
623 *
624 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
625 * This macro may return either 0 or 1 if \p alg is not a supported
626 * algorithm identifier.
627 */
628#define PSA_ALG_IS_HASH(alg) \
629 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
630
631/** Whether the specified algorithm is a MAC algorithm.
632 *
633 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
634 *
635 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
636 * This macro may return either 0 or 1 if \p alg is not a supported
637 * algorithm identifier.
638 */
639#define PSA_ALG_IS_MAC(alg) \
640 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
641
642/** Whether the specified algorithm is a symmetric cipher algorithm.
643 *
644 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
645 *
646 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
647 * This macro may return either 0 or 1 if \p alg is not a supported
648 * algorithm identifier.
649 */
650#define PSA_ALG_IS_CIPHER(alg) \
651 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
652
653/** Whether the specified algorithm is an authenticated encryption
654 * with associated data (AEAD) algorithm.
655 *
656 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
657 *
658 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
659 * This macro may return either 0 or 1 if \p alg is not a supported
660 * algorithm identifier.
661 */
662#define PSA_ALG_IS_AEAD(alg) \
663 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
664
Soby Mathew07ef6e42020-07-20 21:09:23 +0100665/** Whether the specified algorithm is an asymmetric signature algorithm,
666 * also known as public-key signature algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100667 *
668 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
669 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100670 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100671 * This macro may return either 0 or 1 if \p alg is not a supported
672 * algorithm identifier.
673 */
674#define PSA_ALG_IS_SIGN(alg) \
675 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
676
Soby Mathew07ef6e42020-07-20 21:09:23 +0100677/** Whether the specified algorithm is an asymmetric encryption algorithm,
678 * also known as public-key encryption algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100679 *
680 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
681 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100682 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100683 * This macro may return either 0 or 1 if \p alg is not a supported
684 * algorithm identifier.
685 */
686#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
687 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
688
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100689/** Whether the specified algorithm is a key agreement algorithm.
690 *
691 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
692 *
693 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
694 * This macro may return either 0 or 1 if \p alg is not a supported
695 * algorithm identifier.
696 */
697#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100698 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100699
700/** Whether the specified algorithm is a key derivation algorithm.
701 *
702 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
703 *
704 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
705 * This macro may return either 0 or 1 if \p alg is not a supported
706 * algorithm identifier.
707 */
708#define PSA_ALG_IS_KEY_DERIVATION(alg) \
709 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
710
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100711#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100712/** MD2 */
Maulik Patel28659c42021-01-06 14:09:22 +0000713#define PSA_ALG_MD2 ((psa_algorithm_t)0x02000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100714/** MD4 */
Maulik Patel28659c42021-01-06 14:09:22 +0000715#define PSA_ALG_MD4 ((psa_algorithm_t)0x02000002)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100716/** MD5 */
Maulik Patel28659c42021-01-06 14:09:22 +0000717#define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100718/** PSA_ALG_RIPEMD160 */
Maulik Patel28659c42021-01-06 14:09:22 +0000719#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100720/** SHA1 */
Maulik Patel28659c42021-01-06 14:09:22 +0000721#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100722/** SHA2-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000723#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100724/** SHA2-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000725#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100726/** SHA2-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000727#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100728/** SHA2-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000729#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100730/** SHA2-512/224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000731#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100732/** SHA2-512/256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000733#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100734/** SHA3-224 */
Maulik Patel28659c42021-01-06 14:09:22 +0000735#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100736/** SHA3-256 */
Maulik Patel28659c42021-01-06 14:09:22 +0000737#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100738/** SHA3-384 */
Maulik Patel28659c42021-01-06 14:09:22 +0000739#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100740/** SHA3-512 */
Maulik Patel28659c42021-01-06 14:09:22 +0000741#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100742
743/** In a hash-and-sign algorithm policy, allow any hash algorithm.
744 *
745 * This value may be used to form the algorithm usage field of a policy
746 * for a signature algorithm that is parametrized by a hash. The key
747 * may then be used to perform operations using the same signature
748 * algorithm parametrized with any supported hash.
749 *
750 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
751 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100752 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
753 * Then you may create and use a key as follows:
754 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
755 * ```
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100756 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
757 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100758 * ```
759 * - Import or generate key material.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100760 * - Call psa_sign_hash() or psa_verify_hash(), passing
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100761 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
762 * call to sign or verify a message may use a different hash.
763 * ```
Maulik Patel28659c42021-01-06 14:09:22 +0000764 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
765 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
766 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100767 * ```
768 *
769 * This value may not be used to build other algorithms that are
770 * parametrized over a hash. For any valid use of this macro to build
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100771 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100772 *
773 * This value may not be used to build an algorithm specification to
774 * perform an operation. It is only valid to build policies.
775 */
Maulik Patel28659c42021-01-06 14:09:22 +0000776#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100777
778#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Maulik Patel28659c42021-01-06 14:09:22 +0000779#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100780/** Macro to build an HMAC algorithm.
781 *
782 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
783 *
784 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
785 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
786 *
787 * \return The corresponding HMAC algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100788 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100789 * hash algorithm.
790 */
791#define PSA_ALG_HMAC(hash_alg) \
792 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
793
794#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
795 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
796
797/** Whether the specified algorithm is an HMAC algorithm.
798 *
799 * HMAC is a family of MAC algorithms that are based on a hash function.
800 *
801 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
802 *
803 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
804 * This macro may return either 0 or 1 if \p alg is not a supported
805 * algorithm identifier.
806 */
807#define PSA_ALG_IS_HMAC(alg) \
808 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
809 PSA_ALG_HMAC_BASE)
810
811/* In the encoding of a MAC algorithm, the bits corresponding to
812 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
813 * truncated. As an exception, the value 0 means the untruncated algorithm,
814 * whatever its length is. The length is encoded in 6 bits, so it can
815 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
816 * to full length is correctly encoded as 0 and any non-trivial truncation
817 * is correctly encoded as a value between 1 and 63. */
Maulik Patel28659c42021-01-06 14:09:22 +0000818#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
819#define PSA_MAC_TRUNCATION_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100820
821/** Macro to build a truncated MAC algorithm.
822 *
823 * A truncated MAC algorithm is identical to the corresponding MAC
824 * algorithm except that the MAC value for the truncated algorithm
825 * consists of only the first \p mac_length bytes of the MAC value
826 * for the untruncated algorithm.
827 *
828 * \note This macro may allow constructing algorithm identifiers that
829 * are not valid, either because the specified length is larger
830 * than the untruncated MAC or because the specified length is
831 * smaller than permitted by the implementation.
832 *
833 * \note It is implementation-defined whether a truncated MAC that
834 * is truncated to the same length as the MAC of the untruncated
835 * algorithm is considered identical to the untruncated algorithm
836 * for policy comparison purposes.
837 *
838 * \param mac_alg A MAC algorithm identifier (value of type
839 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
840 * is true). This may be a truncated or untruncated
841 * MAC algorithm.
842 * \param mac_length Desired length of the truncated MAC in bytes.
843 * This must be at most the full length of the MAC
844 * and must be at least an implementation-specified
845 * minimum. The implementation-specified minimum
846 * shall not be zero.
847 *
848 * \return The corresponding MAC algorithm with the specified
849 * length.
850 * \return Unspecified if \p alg is not a supported
851 * MAC algorithm or if \p mac_length is too small or
852 * too large for the specified MAC algorithm.
853 */
854#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
855 (((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
856 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
857
858/** Macro to build the base MAC algorithm corresponding to a truncated
859 * MAC algorithm.
860 *
861 * \param mac_alg A MAC algorithm identifier (value of type
862 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
863 * is true). This may be a truncated or untruncated
864 * MAC algorithm.
865 *
866 * \return The corresponding base MAC algorithm.
867 * \return Unspecified if \p alg is not a supported
868 * MAC algorithm.
869 */
870#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
871 ((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
872
873/** Length to which a MAC algorithm is truncated.
874 *
875 * \param mac_alg A MAC algorithm identifier (value of type
876 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
877 * is true).
878 *
879 * \return Length of the truncated MAC in bytes.
880 * \return 0 if \p alg is a non-truncated MAC algorithm.
881 * \return Unspecified if \p alg is not a supported
882 * MAC algorithm.
883 */
884#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
885 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
886
Maulik Patel28659c42021-01-06 14:09:22 +0000887#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100888/** The CBC-MAC construction over a block cipher
889 *
890 * \warning CBC-MAC is insecure in many cases.
891 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
892 */
Maulik Patel28659c42021-01-06 14:09:22 +0000893#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100894/** The CMAC construction over a block cipher */
Maulik Patel28659c42021-01-06 14:09:22 +0000895#define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100896
897/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
898 *
899 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
900 *
901 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
902 * This macro may return either 0 or 1 if \p alg is not a supported
903 * algorithm identifier.
904 */
905#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
906 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
907 PSA_ALG_CIPHER_MAC_BASE)
908
909#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
910#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
911
912/** Whether the specified algorithm is a stream cipher.
913 *
914 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
915 * by applying a bitwise-xor with a stream of bytes that is generated
916 * from a key.
917 *
918 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
919 *
920 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
921 * This macro may return either 0 or 1 if \p alg is not a supported
922 * algorithm identifier or if it is not a symmetric cipher algorithm.
923 */
924#define PSA_ALG_IS_STREAM_CIPHER(alg) \
925 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
926 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
927
Maulik Patel28659c42021-01-06 14:09:22 +0000928/** The stream cipher mode of a stream cipher algorithm.
929 *
930 * The underlying stream cipher is determined by the key type.
931 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
932 * - To use ARC4, use a key type of #PSA_KEY_TYPE_ARC4.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100933 */
Maulik Patel28659c42021-01-06 14:09:22 +0000934#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100935
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100936/** The CTR stream cipher mode.
937 *
938 * CTR is a stream cipher which is built from a block cipher.
939 * The underlying block cipher is determined by the key type.
940 * For example, to use AES-128-CTR, use this algorithm with
941 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
942 */
Maulik Patel28659c42021-01-06 14:09:22 +0000943#define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100944
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100945/** The CFB stream cipher mode.
946 *
947 * The underlying block cipher is determined by the key type.
948 */
Maulik Patel28659c42021-01-06 14:09:22 +0000949#define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100950
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100951/** The OFB stream cipher mode.
952 *
953 * The underlying block cipher is determined by the key type.
954 */
Maulik Patel28659c42021-01-06 14:09:22 +0000955#define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100956
957/** The XTS cipher mode.
958 *
959 * XTS is a cipher mode which is built from a block cipher. It requires at
960 * least one full block of input, but beyond this minimum the input
961 * does not need to be a whole number of blocks.
962 */
Maulik Patel28659c42021-01-06 14:09:22 +0000963#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
964
965/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
966 *
967 * \warning ECB mode does not protect the confidentiality of the encrypted data
968 * except in extremely narrow circumstances. It is recommended that applications
969 * only use ECB if they need to construct an operating mode that the
970 * implementation does not provide. Implementations are encouraged to provide
971 * the modes that applications need in preference to supporting direct access
972 * to ECB.
973 *
974 * The underlying block cipher is determined by the key type.
975 *
976 * This symmetric cipher mode can only be used with messages whose lengths are a
977 * multiple of the block size of the chosen block cipher.
978 *
979 * ECB mode does not accept an initialization vector (IV). When using a
980 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
981 * and psa_cipher_set_iv() must not be called.
982 */
983#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100984
985/** The CBC block cipher chaining mode, with no padding.
986 *
987 * The underlying block cipher is determined by the key type.
988 *
989 * This symmetric cipher mode can only be used with messages whose lengths
990 * are whole number of blocks for the chosen block cipher.
991 */
Maulik Patel28659c42021-01-06 14:09:22 +0000992#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100993
994/** The CBC block cipher chaining mode with PKCS#7 padding.
995 *
996 * The underlying block cipher is determined by the key type.
997 *
998 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
999 */
Maulik Patel28659c42021-01-06 14:09:22 +00001000#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001001
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001002#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1003
1004/** Whether the specified algorithm is an AEAD mode on a block cipher.
1005 *
1006 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1007 *
1008 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1009 * a block cipher, 0 otherwise.
1010 * This macro may return either 0 or 1 if \p alg is not a supported
1011 * algorithm identifier.
1012 */
1013#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1014 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1015 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1016
1017/** The CCM authenticated encryption algorithm.
1018 *
1019 * The underlying block cipher is determined by the key type.
1020 */
Maulik Patel28659c42021-01-06 14:09:22 +00001021#define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001022
1023/** The GCM authenticated encryption algorithm.
1024 *
1025 * The underlying block cipher is determined by the key type.
1026 */
Maulik Patel28659c42021-01-06 14:09:22 +00001027#define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001028
1029/** The Chacha20-Poly1305 AEAD algorithm.
1030 *
1031 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1032 *
1033 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1034 * and should reject other sizes.
1035 *
1036 * Implementations must support 16-byte tags and should reject other sizes.
1037 */
Maulik Patel28659c42021-01-06 14:09:22 +00001038#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001039
1040/* In the encoding of a AEAD algorithm, the bits corresponding to
1041 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1042 * The constants for default lengths follow this encoding.
1043 */
Maulik Patel28659c42021-01-06 14:09:22 +00001044#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1045#define PSA_AEAD_TAG_LENGTH_OFFSET 16
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001046
1047/** Macro to build a shortened AEAD algorithm.
1048 *
1049 * A shortened AEAD algorithm is similar to the corresponding AEAD
1050 * algorithm, but has an authentication tag that consists of fewer bytes.
1051 * Depending on the algorithm, the tag length may affect the calculation
1052 * of the ciphertext.
1053 *
1054 * \param aead_alg An AEAD algorithm identifier (value of type
1055 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
1056 * is true).
1057 * \param tag_length Desired length of the authentication tag in bytes.
1058 *
1059 * \return The corresponding AEAD algorithm with the specified
1060 * length.
1061 * \return Unspecified if \p alg is not a supported
1062 * AEAD algorithm or if \p tag_length is not valid
1063 * for the specified AEAD algorithm.
1064 */
1065#define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
1066 (((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
1067 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1068 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1069
1070/** Calculate the corresponding AEAD algorithm with the default tag length.
1071 *
1072 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
1073 * #PSA_ALG_IS_AEAD(\p alg) is true).
1074 *
1075 * \return The corresponding AEAD algorithm with the default
1076 * tag length for that algorithm.
1077 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001078#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
1079 ( \
1080 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CCM) \
1081 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_GCM) \
1082 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001083 0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001084#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, ref) \
1085 PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
1086 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001087 ref :
1088
Maulik Patel28659c42021-01-06 14:09:22 +00001089#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001090/** RSA PKCS#1 v1.5 signature with hashing.
1091 *
1092 * This is the signature scheme defined by RFC 8017
1093 * (PKCS#1: RSA Cryptography Specifications) under the name
1094 * RSASSA-PKCS1-v1_5.
1095 *
1096 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1097 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1098 * This includes #PSA_ALG_ANY_HASH
1099 * when specifying the algorithm in a usage policy.
1100 *
1101 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001102 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001103 * hash algorithm.
1104 */
1105#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1106 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1107/** Raw PKCS#1 v1.5 signature.
1108 *
1109 * The input to this algorithm is the DigestInfo structure used by
1110 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1111 * steps 3&ndash;6.
1112 */
1113#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1114#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1115 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1116
Maulik Patel28659c42021-01-06 14:09:22 +00001117#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001118/** RSA PSS signature with hashing.
1119 *
1120 * This is the signature scheme defined by RFC 8017
1121 * (PKCS#1: RSA Cryptography Specifications) under the name
1122 * RSASSA-PSS, with the message generation function MGF1, and with
1123 * a salt length equal to the length of the hash. The specified
1124 * hash algorithm is used to hash the input message, to create the
1125 * salted hash, and for the mask generation.
1126 *
1127 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1128 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1129 * This includes #PSA_ALG_ANY_HASH
1130 * when specifying the algorithm in a usage policy.
1131 *
1132 * \return The corresponding RSA PSS signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001133 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001134 * hash algorithm.
1135 */
1136#define PSA_ALG_RSA_PSS(hash_alg) \
1137 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1138#define PSA_ALG_IS_RSA_PSS(alg) \
1139 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1140
Maulik Patel28659c42021-01-06 14:09:22 +00001141#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001142/** ECDSA signature with hashing.
1143 *
1144 * This is the ECDSA signature scheme defined by ANSI X9.62,
1145 * with a random per-message secret number (*k*).
1146 *
1147 * The representation of the signature as a byte string consists of
1148 * the concatentation of the signature values *r* and *s*. Each of
1149 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1150 * of the base point of the curve in octets. Each value is represented
1151 * in big-endian order (most significant octet first).
1152 *
1153 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1154 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1155 * This includes #PSA_ALG_ANY_HASH
1156 * when specifying the algorithm in a usage policy.
1157 *
1158 * \return The corresponding ECDSA signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001159 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001160 * hash algorithm.
1161 */
1162#define PSA_ALG_ECDSA(hash_alg) \
1163 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1164/** ECDSA signature without hashing.
1165 *
1166 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1167 * without specifying a hash algorithm. This algorithm may only be
1168 * used to sign or verify a sequence of bytes that should be an
1169 * already-calculated hash. Note that the input is padded with
1170 * zeros on the left or truncated on the left as required to fit
1171 * the curve size.
1172 */
1173#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
Maulik Patel28659c42021-01-06 14:09:22 +00001174#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001175/** Deterministic ECDSA signature with hashing.
1176 *
1177 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1178 *
1179 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1180 *
1181 * Note that when this algorithm is used for verification, signatures
1182 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1183 * same private key are accepted. In other words,
1184 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1185 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1186 *
1187 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1188 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1189 * This includes #PSA_ALG_ANY_HASH
1190 * when specifying the algorithm in a usage policy.
1191 *
1192 * \return The corresponding deterministic ECDSA signature
1193 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001194 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001195 * hash algorithm.
1196 */
1197#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1198 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Maulik Patel28659c42021-01-06 14:09:22 +00001199#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001200#define PSA_ALG_IS_ECDSA(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001201 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001202 PSA_ALG_ECDSA_BASE)
1203#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001204 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001205#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1206 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1207#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1208 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1209
1210/** Whether the specified algorithm is a hash-and-sign algorithm.
1211 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001212 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1213 * structured in two parts: first the calculation of a hash in a way that
1214 * does not depend on the key, then the calculation of a signature from the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001215 * hash value and the key.
1216 *
1217 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1218 *
1219 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1220 * This macro may return either 0 or 1 if \p alg is not a supported
1221 * algorithm identifier.
1222 */
1223#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1224 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001225 PSA_ALG_IS_ECDSA(alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001226
1227/** Get the hash used by a hash-and-sign signature algorithm.
1228 *
1229 * A hash-and-sign algorithm is a signature algorithm which is
1230 * composed of two phases: first a hashing phase which does not use
1231 * the key and produces a hash of the input message, then a signing
1232 * phase which only uses the hash and the key and not the message
1233 * itself.
1234 *
1235 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1236 * #PSA_ALG_IS_SIGN(\p alg) is true).
1237 *
1238 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1239 * algorithm.
1240 * \return 0 if \p alg is a signature algorithm that does not
1241 * follow the hash-and-sign structure.
1242 * \return Unspecified if \p alg is not a signature algorithm or
1243 * if it is not supported by the implementation.
1244 */
1245#define PSA_ALG_SIGN_GET_HASH(alg) \
1246 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1247 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1248 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1249 0)
1250
1251/** RSA PKCS#1 v1.5 encryption.
1252 */
Maulik Patel28659c42021-01-06 14:09:22 +00001253#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001254
Maulik Patel28659c42021-01-06 14:09:22 +00001255#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001256/** RSA OAEP encryption.
1257 *
1258 * This is the encryption scheme defined by RFC 8017
1259 * (PKCS#1: RSA Cryptography Specifications) under the name
1260 * RSAES-OAEP, with the message generation function MGF1.
1261 *
1262 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1263 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1264 * for MGF1.
1265 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001266 * \return The corresponding RSA OAEP encryption algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001267 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001268 * hash algorithm.
1269 */
1270#define PSA_ALG_RSA_OAEP(hash_alg) \
1271 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1272#define PSA_ALG_IS_RSA_OAEP(alg) \
1273 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1274#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1275 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1276 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1277 0)
1278
Maulik Patel28659c42021-01-06 14:09:22 +00001279#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001280/** Macro to build an HKDF algorithm.
1281 *
1282 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1283 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001284 * This key derivation algorithm uses the following inputs:
1285 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1286 * It is optional; if omitted, the derivation uses an empty salt.
1287 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1288 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1289 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1290 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1291 * starting to generate output.
1292 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001293 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1294 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1295 *
1296 * \return The corresponding HKDF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001297 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001298 * hash algorithm.
1299 */
1300#define PSA_ALG_HKDF(hash_alg) \
1301 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1302/** Whether the specified algorithm is an HKDF algorithm.
1303 *
1304 * HKDF is a family of key derivation algorithms that are based on a hash
1305 * function and the HMAC construction.
1306 *
1307 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1308 *
1309 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1310 * This macro may return either 0 or 1 if \c alg is not a supported
1311 * key derivation algorithm identifier.
1312 */
1313#define PSA_ALG_IS_HKDF(alg) \
1314 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1315#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1316 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1317
Maulik Patel28659c42021-01-06 14:09:22 +00001318#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001319/** Macro to build a TLS-1.2 PRF algorithm.
1320 *
1321 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1322 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1323 * used with either SHA-256 or SHA-384.
1324 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001325 * This key derivation algorithm uses the following inputs, which must be
1326 * passed in the order given here:
1327 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1328 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1329 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1330 *
1331 * For the application to TLS-1.2 key expansion, the seed is the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001332 * concatenation of ServerHello.Random + ClientHello.Random,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001333 * and the label is "key expansion".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001334 *
1335 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1336 * TLS 1.2 PRF using HMAC-SHA-256.
1337 *
1338 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1339 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1340 *
1341 * \return The corresponding TLS-1.2 PRF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001342 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001343 * hash algorithm.
1344 */
1345#define PSA_ALG_TLS12_PRF(hash_alg) \
1346 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1347
1348/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1349 *
1350 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1351 *
1352 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1353 * This macro may return either 0 or 1 if \c alg is not a supported
1354 * key derivation algorithm identifier.
1355 */
1356#define PSA_ALG_IS_TLS12_PRF(alg) \
1357 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1358#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1359 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1360
Maulik Patel28659c42021-01-06 14:09:22 +00001361#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001362/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1363 *
1364 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1365 * from the PreSharedKey (PSK) through the application of padding
1366 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1367 * The latter is based on HMAC and can be used with either SHA-256
1368 * or SHA-384.
1369 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001370 * This key derivation algorithm uses the following inputs, which must be
1371 * passed in the order given here:
1372 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1373 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1374 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1375 *
1376 * For the application to TLS-1.2, the seed (which is
1377 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1378 * ClientHello.Random + ServerHello.Random,
1379 * and the label is "master secret" or "extended master secret".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001380 *
1381 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1382 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1383 *
1384 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1385 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1386 *
1387 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001388 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001389 * hash algorithm.
1390 */
1391#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1392 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1393
1394/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1395 *
1396 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1397 *
1398 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1399 * This macro may return either 0 or 1 if \c alg is not a supported
1400 * key derivation algorithm identifier.
1401 */
1402#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1403 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1404#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1405 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1406
Maulik Patel28659c42021-01-06 14:09:22 +00001407#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
1408#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001409
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001410/** Macro to build a combined algorithm that chains a key agreement with
1411 * a key derivation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001412 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001413 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1414 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1415 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1416 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001417 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001418 * \return The corresponding key agreement and derivation
1419 * algorithm.
1420 * \return Unspecified if \p ka_alg is not a supported
1421 * key agreement algorithm or \p kdf_alg is not a
1422 * supported key derivation algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001423 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001424#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1425 ((ka_alg) | (kdf_alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001426
1427#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1428 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1429
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001430#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1431 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001432
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001433/** Whether the specified algorithm is a raw key agreement algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001434 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001435 * A raw key agreement algorithm is one that does not specify
1436 * a key derivation function.
1437 * Usually, raw key agreement algorithms are constructed directly with
1438 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
Maulik Patel28659c42021-01-06 14:09:22 +00001439 * constructed with #PSA_ALG_KEY_AGREEMENT().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001440 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001441 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1442 *
1443 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1444 * This macro may return either 0 or 1 if \p alg is not a supported
1445 * algorithm identifier.
1446 */
1447#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1448 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1449 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1450
1451#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1452 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1453
1454/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1455 *
1456 * The shared secret produced by key agreement is
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001457 * `g^{ab}` in big-endian format.
1458 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1459 * in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001460 */
Maulik Patel28659c42021-01-06 14:09:22 +00001461#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001462
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001463/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1464 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001465 * This includes the raw finite field Diffie-Hellman algorithm as well as
1466 * finite-field Diffie-Hellman followed by any supporter key derivation
1467 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001468 *
1469 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1470 *
1471 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1472 * This macro may return either 0 or 1 if \c alg is not a supported
1473 * key agreement algorithm identifier.
1474 */
1475#define PSA_ALG_IS_FFDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001476 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001477
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001478/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1479 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001480 * The shared secret produced by key agreement is the x-coordinate of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001481 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1482 * `m` is the bit size associated with the curve, i.e. the bit size of the
1483 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1484 * the byte containing the most significant bit of the shared secret
1485 * is padded with zero bits. The byte order is either little-endian
1486 * or big-endian depending on the curve type.
1487 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08001488 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001489 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1490 * in little-endian byte order.
1491 * The bit size is 448 for Curve448 and 255 for Curve25519.
1492 * - For Weierstrass curves over prime fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08001493 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001494 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1495 * in big-endian byte order.
1496 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1497 * - For Weierstrass curves over binary fields (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +08001498 * `PSA_ECC_FAMILY_SECTXXX`),
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001499 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1500 * in big-endian byte order.
1501 * The bit size is `m` for the field `F_{2^m}`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001502 */
Maulik Patel28659c42021-01-06 14:09:22 +00001503#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001504
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001505/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1506 * algorithm.
1507 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001508 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1509 * elliptic curve Diffie-Hellman followed by any supporter key derivation
1510 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001511 *
1512 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1513 *
1514 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1515 * 0 otherwise.
1516 * This macro may return either 0 or 1 if \c alg is not a supported
1517 * key agreement algorithm identifier.
1518 */
1519#define PSA_ALG_IS_ECDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001520 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001521
1522/** Whether the specified algorithm encoding is a wildcard.
1523 *
1524 * Wildcard values may only be used to set the usage algorithm field in
1525 * a policy, not to perform an operation.
1526 *
1527 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1528 *
1529 * \return 1 if \c alg is a wildcard algorithm encoding.
1530 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1531 * an operation).
1532 * \return This macro may return either 0 or 1 if \c alg is not a supported
1533 * algorithm identifier.
1534 */
1535#define PSA_ALG_IS_WILDCARD(alg) \
1536 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1537 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1538 (alg) == PSA_ALG_ANY_HASH)
1539
1540/**@}*/
1541
1542/** \defgroup key_lifetimes Key lifetimes
1543 * @{
1544 */
1545
Soby Mathew07ef6e42020-07-20 21:09:23 +01001546/** The default lifetime for volatile keys.
1547 *
Maulik Patel28659c42021-01-06 14:09:22 +00001548 * A volatile key only exists as long as the identifier to it is not destroyed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001549 * The key material is guaranteed to be erased on a power reset.
Soby Mathew07ef6e42020-07-20 21:09:23 +01001550 *
1551 * A key with this lifetime is typically stored in the RAM area of the
1552 * PSA Crypto subsystem. However this is an implementation choice.
1553 * If an implementation stores data about the key in a non-volatile memory,
1554 * it must release all the resources associated with the key and erase the
1555 * key material if the calling application terminates.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001556 */
1557#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1558
Soby Mathew07ef6e42020-07-20 21:09:23 +01001559/** The default lifetime for persistent keys.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001560 *
1561 * A persistent key remains in storage until it is explicitly destroyed or
1562 * until the corresponding storage area is wiped. This specification does
1563 * not define any mechanism to wipe a storage area, but implementations may
1564 * provide their own mechanism (for example to perform a factory reset,
1565 * to prepare for device refurbishment, or to uninstall an application).
1566 *
1567 * This lifetime value is the default storage area for the calling
1568 * application. Implementations may offer other storage areas designated
1569 * by other lifetime values as implementation-specific extensions.
Soby Mathew07ef6e42020-07-20 21:09:23 +01001570 * See ::psa_key_lifetime_t for more information.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001571 */
1572#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1573
Soby Mathew07ef6e42020-07-20 21:09:23 +01001574/** The persistence level of volatile keys.
1575 *
1576 * See ::psa_key_persistence_t for more information.
1577 */
1578#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
1579
1580/** The default persistence level for persistent keys.
1581 *
1582 * See ::psa_key_persistence_t for more information.
1583 */
1584#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
1585
1586/** A persistence level indicating that a key is never destroyed.
1587 *
1588 * See ::psa_key_persistence_t for more information.
1589 */
1590#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
1591
1592#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
1593 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
1594
1595#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
1596 ((psa_key_location_t)((lifetime) >> 8))
1597
1598/** Whether a key lifetime indicates that the key is volatile.
1599 *
1600 * A volatile key is automatically destroyed by the implementation when
1601 * the application instance terminates. In particular, a volatile key
1602 * is automatically destroyed on a power reset of the device.
1603 *
1604 * A key that is not volatile is persistent. Persistent keys are
1605 * preserved until the application explicitly destroys them or until an
1606 * implementation-specific device management event occurs (for example,
1607 * a factory reset).
1608 *
1609 * \param lifetime The lifetime value to query (value of type
1610 * ::psa_key_lifetime_t).
1611 *
1612 * \return \c 1 if the key is volatile, otherwise \c 0.
1613 */
1614#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
1615 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
Summer Qin0e5b2e02020-10-22 11:23:39 +08001616 PSA_KEY_PERSISTENCE_VOLATILE)
Soby Mathew07ef6e42020-07-20 21:09:23 +01001617
1618/** Construct a lifetime from a persistence level and a location.
1619 *
1620 * \param persistence The persistence level
1621 * (value of type ::psa_key_persistence_t).
1622 * \param location The location indicator
1623 * (value of type ::psa_key_location_t).
1624 *
1625 * \return The constructed lifetime value.
1626 */
1627#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
1628 ((location) << 8 | (persistence))
1629
1630/** The local storage area for persistent keys.
1631 *
1632 * This storage area is available on all systems that can store persistent
1633 * keys without delegating the storage to a third-party cryptoprocessor.
1634 *
1635 * See ::psa_key_location_t for more information.
1636 */
1637#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
1638
1639#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
1640
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001641/** The minimum value for a key identifier chosen by the application.
1642 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001643#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001644/** The maximum value for a key identifier chosen by the application.
1645 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001646#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001647/** The minimum value for a key identifier chosen by the implementation.
1648 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001649#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001650/** The maximum value for a key identifier chosen by the implementation.
1651 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001652#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001653
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001654/**@}*/
1655
1656/** \defgroup policy Key policies
1657 * @{
1658 */
1659
1660/** Whether the key may be exported.
1661 *
1662 * A public key or the public part of a key pair may always be exported
1663 * regardless of the value of this permission flag.
1664 *
1665 * If a key does not have export permission, implementations shall not
1666 * allow the key to be exported in plain form from the cryptoprocessor,
1667 * whether through psa_export_key() or through a proprietary interface.
1668 * The key may however be exportable in a wrapped form, i.e. in a form
1669 * where it is encrypted by another key.
1670 */
1671#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1672
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001673/** Whether the key may be copied.
1674 *
1675 * This flag allows the use of psa_copy_key() to make a copy of the key
1676 * with the same policy or a more restrictive policy.
1677 *
1678 * For lifetimes for which the key is located in a secure element which
1679 * enforce the non-exportability of keys, copying a key outside the secure
1680 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
1681 * Copying the key inside the secure element is permitted with just
1682 * #PSA_KEY_USAGE_COPY if the secure element supports it.
1683 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
1684 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
1685 * is sufficient to permit the copy.
1686 */
1687#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
1688
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001689/** Whether the key may be used to encrypt a message.
1690 *
1691 * This flag allows the key to be used for a symmetric encryption operation,
1692 * for an AEAD encryption-and-authentication operation,
1693 * or for an asymmetric encryption operation,
1694 * if otherwise permitted by the key's type and policy.
1695 *
1696 * For a key pair, this concerns the public key.
1697 */
1698#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1699
1700/** Whether the key may be used to decrypt a message.
1701 *
1702 * This flag allows the key to be used for a symmetric decryption operation,
1703 * for an AEAD decryption-and-verification operation,
1704 * or for an asymmetric decryption operation,
1705 * if otherwise permitted by the key's type and policy.
1706 *
1707 * For a key pair, this concerns the private key.
1708 */
1709#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1710
1711/** Whether the key may be used to sign a message.
1712 *
1713 * This flag allows the key to be used for a MAC calculation operation
1714 * or for an asymmetric signature operation,
1715 * if otherwise permitted by the key's type and policy.
1716 *
1717 * For a key pair, this concerns the private key.
1718 */
Maulik Patel28659c42021-01-06 14:09:22 +00001719#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001720
1721/** Whether the key may be used to verify a message signature.
1722 *
1723 * This flag allows the key to be used for a MAC verification operation
1724 * or for an asymmetric signature verification operation,
1725 * if otherwise permitted by by the key's type and policy.
1726 *
1727 * For a key pair, this concerns the public key.
1728 */
Maulik Patel28659c42021-01-06 14:09:22 +00001729#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001730
1731/** Whether the key may be used to derive other keys.
1732 */
Maulik Patel28659c42021-01-06 14:09:22 +00001733#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001734
1735/**@}*/
1736
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001737/** \defgroup derivation Key derivation
1738 * @{
1739 */
1740
1741/** A secret input for key derivation.
1742 *
1743 * This should be a key of type #PSA_KEY_TYPE_DERIVE
1744 * (passed to psa_key_derivation_input_key())
1745 * or the shared secret resulting from a key agreement
1746 * (obtained via psa_key_derivation_key_agreement()).
1747 *
1748 * The secret can also be a direct input (passed to
1749 * key_derivation_input_bytes()). In this case, the derivation operation
1750 * may not be used to derive keys: the operation will only allow
1751 * psa_key_derivation_output_bytes(), not psa_key_derivation_output_key().
1752 */
1753#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
1754
1755/** A label for key derivation.
1756 *
1757 * This should be a direct input.
1758 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1759 */
1760#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
1761
1762/** A salt for key derivation.
1763 *
1764 * This should be a direct input.
1765 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1766 */
1767#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
1768
1769/** An information string for key derivation.
1770 *
1771 * This should be a direct input.
1772 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1773 */
1774#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
1775
1776/** A seed for key derivation.
1777 *
1778 * This should be a direct input.
1779 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1780 */
1781#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
1782
1783/**@}*/
1784
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001785#endif /* PSA_CRYPTO_VALUES_H */