blob: 8a70616aeebad608754f1b837f5bec7bf9bac145 [file] [log] [blame]
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, 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
103 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
104 * 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
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100113 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
114 * 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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100261/** The key handle 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 *
419 * \param curve A value of type ::psa_ecc_curve_t that identifies the
420 * ECC curve to be used.
421 */
422#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
423 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
424/** Elliptic curve public key.
425 *
426 * \param curve A value of type ::psa_ecc_curve_t that identifies the
427 * ECC curve to be used.
428 */
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. */
446#define PSA_KEY_TYPE_GET_CURVE(type) \
447 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
448 ((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 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100459#define PSA_ECC_CURVE_SECP_K1 ((psa_ecc_curve_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 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100469#define PSA_ECC_CURVE_SECP_R1 ((psa_ecc_curve_t) 0x12)
470/* SECP160R2 (SEC2 v1, obsolete) */
471#define PSA_ECC_CURVE_SECP_R2 ((psa_ecc_curve_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 */
481#define PSA_ECC_CURVE_SECT_K1 ((psa_ecc_curve_t) 0x27)
482
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 */
491#define PSA_ECC_CURVE_SECT_R1 ((psa_ecc_curve_t) 0x22)
492
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 */
501#define PSA_ECC_CURVE_SECT_R2 ((psa_ecc_curve_t) 0x2b)
502
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 */
510#define PSA_ECC_CURVE_BRAINPOOL_P_R1 ((psa_ecc_curve_t) 0x30)
511
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 */
522#define PSA_ECC_CURVE_MONTGOMERY ((psa_ecc_curve_t) 0x41)
523
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 *
529 * \param group A value of type ::psa_dh_group_t that identifies the
530 * 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 *
536 * \param group A value of type ::psa_dh_group_t that identifies the
537 * 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. */
556#define PSA_KEY_TYPE_GET_GROUP(type) \
557 ((psa_dh_group_t) (PSA_KEY_TYPE_IS_DH(type) ? \
558 ((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 */
Soby Mathew07ef6e42020-07-20 21:09:23 +0100567#define PSA_DH_GROUP_RFC7919 ((psa_dh_group_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)
604#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
605#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
606#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
607#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
608#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
609#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100610#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x20000000)
611#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x30000000)
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 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100713#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100714/** MD4 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100715#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100716/** MD5 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100717#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100718/** PSA_ALG_RIPEMD160 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100719#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100720/** SHA1 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100721#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
722/** SHA2-224 */
723#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
724/** SHA2-256 */
725#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
726/** SHA2-384 */
727#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
728/** SHA2-512 */
729#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
730/** SHA2-512/224 */
731#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
732/** SHA2-512/256 */
733#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
734/** SHA3-224 */
735#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
736/** SHA3-256 */
737#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
738/** SHA3-384 */
739#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
740/** SHA3-512 */
741#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
742
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 * ```
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100764 * psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
765 * psa_sign_hash(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
766 * psa_sign_hash(handle, 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 */
776#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x010000ff)
777
778#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
779#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
780/** 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. */
818#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
819#define PSA_MAC_TRUNCATION_OFFSET 8
820
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
887#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
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 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100893#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100894/** The CMAC construction over a block cipher */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100895#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
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
928/** The ARC4 stream cipher algorithm.
929 */
930#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
931
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100932/** The ChaCha20 stream cipher.
933 *
934 * ChaCha20 is defined in RFC 7539.
935 *
936 * The nonce size for psa_cipher_set_iv() or psa_cipher_generate_iv()
937 * must be 12.
938 *
939 * The initial block counter is always 0.
940 *
941 */
942#define PSA_ALG_CHACHA20 ((psa_algorithm_t)0x04800005)
943
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100944/** The CTR stream cipher mode.
945 *
946 * CTR is a stream cipher which is built from a block cipher.
947 * The underlying block cipher is determined by the key type.
948 * For example, to use AES-128-CTR, use this algorithm with
949 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
950 */
951#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
952
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100953/** The CFB stream cipher mode.
954 *
955 * The underlying block cipher is determined by the key type.
956 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100957#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
958
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100959/** The OFB stream cipher mode.
960 *
961 * The underlying block cipher is determined by the key type.
962 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100963#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
964
965/** The XTS cipher mode.
966 *
967 * XTS is a cipher mode which is built from a block cipher. It requires at
968 * least one full block of input, but beyond this minimum the input
969 * does not need to be a whole number of blocks.
970 */
971#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
972
973/** The CBC block cipher chaining mode, with no padding.
974 *
975 * The underlying block cipher is determined by the key type.
976 *
977 * This symmetric cipher mode can only be used with messages whose lengths
978 * are whole number of blocks for the chosen block cipher.
979 */
980#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
981
982/** The CBC block cipher chaining mode with PKCS#7 padding.
983 *
984 * The underlying block cipher is determined by the key type.
985 *
986 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
987 */
988#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
989
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100990#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
991
992/** Whether the specified algorithm is an AEAD mode on a block cipher.
993 *
994 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
995 *
996 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
997 * a block cipher, 0 otherwise.
998 * This macro may return either 0 or 1 if \p alg is not a supported
999 * algorithm identifier.
1000 */
1001#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1002 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1003 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1004
1005/** The CCM authenticated encryption algorithm.
1006 *
1007 * The underlying block cipher is determined by the key type.
1008 */
1009#define PSA_ALG_CCM ((psa_algorithm_t)0x06401001)
1010
1011/** The GCM authenticated encryption algorithm.
1012 *
1013 * The underlying block cipher is determined by the key type.
1014 */
1015#define PSA_ALG_GCM ((psa_algorithm_t)0x06401002)
1016
1017/** The Chacha20-Poly1305 AEAD algorithm.
1018 *
1019 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1020 *
1021 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1022 * and should reject other sizes.
1023 *
1024 * Implementations must support 16-byte tags and should reject other sizes.
1025 */
1026#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x06001005)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001027
1028/* In the encoding of a AEAD algorithm, the bits corresponding to
1029 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1030 * The constants for default lengths follow this encoding.
1031 */
1032#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
1033#define PSA_AEAD_TAG_LENGTH_OFFSET 8
1034
1035/** Macro to build a shortened AEAD algorithm.
1036 *
1037 * A shortened AEAD algorithm is similar to the corresponding AEAD
1038 * algorithm, but has an authentication tag that consists of fewer bytes.
1039 * Depending on the algorithm, the tag length may affect the calculation
1040 * of the ciphertext.
1041 *
1042 * \param aead_alg An AEAD algorithm identifier (value of type
1043 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
1044 * is true).
1045 * \param tag_length Desired length of the authentication tag in bytes.
1046 *
1047 * \return The corresponding AEAD algorithm with the specified
1048 * length.
1049 * \return Unspecified if \p alg is not a supported
1050 * AEAD algorithm or if \p tag_length is not valid
1051 * for the specified AEAD algorithm.
1052 */
1053#define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
1054 (((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
1055 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1056 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1057
1058/** Calculate the corresponding AEAD algorithm with the default tag length.
1059 *
1060 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
1061 * #PSA_ALG_IS_AEAD(\p alg) is true).
1062 *
1063 * \return The corresponding AEAD algorithm with the default
1064 * tag length for that algorithm.
1065 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001066#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
1067 ( \
1068 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CCM) \
1069 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_GCM) \
1070 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001071 0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001072#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE(aead_alg, ref) \
1073 PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
1074 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001075 ref :
1076
1077#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
1078/** RSA PKCS#1 v1.5 signature with hashing.
1079 *
1080 * This is the signature scheme defined by RFC 8017
1081 * (PKCS#1: RSA Cryptography Specifications) under the name
1082 * RSASSA-PKCS1-v1_5.
1083 *
1084 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1085 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1086 * This includes #PSA_ALG_ANY_HASH
1087 * when specifying the algorithm in a usage policy.
1088 *
1089 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001090 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001091 * hash algorithm.
1092 */
1093#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1094 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1095/** Raw PKCS#1 v1.5 signature.
1096 *
1097 * The input to this algorithm is the DigestInfo structure used by
1098 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1099 * steps 3&ndash;6.
1100 */
1101#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1102#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1103 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1104
1105#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
1106/** RSA PSS signature with hashing.
1107 *
1108 * This is the signature scheme defined by RFC 8017
1109 * (PKCS#1: RSA Cryptography Specifications) under the name
1110 * RSASSA-PSS, with the message generation function MGF1, and with
1111 * a salt length equal to the length of the hash. The specified
1112 * hash algorithm is used to hash the input message, to create the
1113 * salted hash, and for the mask generation.
1114 *
1115 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1116 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1117 * This includes #PSA_ALG_ANY_HASH
1118 * when specifying the algorithm in a usage policy.
1119 *
1120 * \return The corresponding RSA PSS signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001121 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001122 * hash algorithm.
1123 */
1124#define PSA_ALG_RSA_PSS(hash_alg) \
1125 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1126#define PSA_ALG_IS_RSA_PSS(alg) \
1127 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1128
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001129#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1130/** ECDSA signature with hashing.
1131 *
1132 * This is the ECDSA signature scheme defined by ANSI X9.62,
1133 * with a random per-message secret number (*k*).
1134 *
1135 * The representation of the signature as a byte string consists of
1136 * the concatentation of the signature values *r* and *s*. Each of
1137 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1138 * of the base point of the curve in octets. Each value is represented
1139 * in big-endian order (most significant octet first).
1140 *
1141 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1142 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1143 * This includes #PSA_ALG_ANY_HASH
1144 * when specifying the algorithm in a usage policy.
1145 *
1146 * \return The corresponding ECDSA signature algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001147 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001148 * hash algorithm.
1149 */
1150#define PSA_ALG_ECDSA(hash_alg) \
1151 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1152/** ECDSA signature without hashing.
1153 *
1154 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1155 * without specifying a hash algorithm. This algorithm may only be
1156 * used to sign or verify a sequence of bytes that should be an
1157 * already-calculated hash. Note that the input is padded with
1158 * zeros on the left or truncated on the left as required to fit
1159 * the curve size.
1160 */
1161#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1162#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1163/** Deterministic ECDSA signature with hashing.
1164 *
1165 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1166 *
1167 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1168 *
1169 * Note that when this algorithm is used for verification, signatures
1170 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1171 * same private key are accepted. In other words,
1172 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1173 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1174 *
1175 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1176 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1177 * This includes #PSA_ALG_ANY_HASH
1178 * when specifying the algorithm in a usage policy.
1179 *
1180 * \return The corresponding deterministic ECDSA signature
1181 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001182 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001183 * hash algorithm.
1184 */
1185#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1186 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001187#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001188#define PSA_ALG_IS_ECDSA(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001189 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001190 PSA_ALG_ECDSA_BASE)
1191#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001192 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001193#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1194 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1195#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1196 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1197
1198/** Whether the specified algorithm is a hash-and-sign algorithm.
1199 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001200 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1201 * structured in two parts: first the calculation of a hash in a way that
1202 * does not depend on the key, then the calculation of a signature from the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001203 * hash value and the key.
1204 *
1205 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1206 *
1207 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1208 * This macro may return either 0 or 1 if \p alg is not a supported
1209 * algorithm identifier.
1210 */
1211#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1212 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001213 PSA_ALG_IS_ECDSA(alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001214
1215/** Get the hash used by a hash-and-sign signature algorithm.
1216 *
1217 * A hash-and-sign algorithm is a signature algorithm which is
1218 * composed of two phases: first a hashing phase which does not use
1219 * the key and produces a hash of the input message, then a signing
1220 * phase which only uses the hash and the key and not the message
1221 * itself.
1222 *
1223 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1224 * #PSA_ALG_IS_SIGN(\p alg) is true).
1225 *
1226 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1227 * algorithm.
1228 * \return 0 if \p alg is a signature algorithm that does not
1229 * follow the hash-and-sign structure.
1230 * \return Unspecified if \p alg is not a signature algorithm or
1231 * if it is not supported by the implementation.
1232 */
1233#define PSA_ALG_SIGN_GET_HASH(alg) \
1234 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1235 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1236 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1237 0)
1238
1239/** RSA PKCS#1 v1.5 encryption.
1240 */
1241#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
1242
1243#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
1244/** RSA OAEP encryption.
1245 *
1246 * This is the encryption scheme defined by RFC 8017
1247 * (PKCS#1: RSA Cryptography Specifications) under the name
1248 * RSAES-OAEP, with the message generation function MGF1.
1249 *
1250 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1251 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1252 * for MGF1.
1253 *
Soby Mathew07ef6e42020-07-20 21:09:23 +01001254 * \return The corresponding RSA OAEP encryption algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001255 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001256 * hash algorithm.
1257 */
1258#define PSA_ALG_RSA_OAEP(hash_alg) \
1259 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1260#define PSA_ALG_IS_RSA_OAEP(alg) \
1261 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1262#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1263 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1264 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1265 0)
1266
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001267#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x20000100)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001268/** Macro to build an HKDF algorithm.
1269 *
1270 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1271 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001272 * This key derivation algorithm uses the following inputs:
1273 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1274 * It is optional; if omitted, the derivation uses an empty salt.
1275 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1276 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1277 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1278 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1279 * starting to generate output.
1280 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001281 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1282 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1283 *
1284 * \return The corresponding HKDF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001285 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001286 * hash algorithm.
1287 */
1288#define PSA_ALG_HKDF(hash_alg) \
1289 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1290/** Whether the specified algorithm is an HKDF algorithm.
1291 *
1292 * HKDF is a family of key derivation algorithms that are based on a hash
1293 * function and the HMAC construction.
1294 *
1295 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1296 *
1297 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1298 * This macro may return either 0 or 1 if \c alg is not a supported
1299 * key derivation algorithm identifier.
1300 */
1301#define PSA_ALG_IS_HKDF(alg) \
1302 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1303#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1304 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1305
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001306#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x20000200)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001307/** Macro to build a TLS-1.2 PRF algorithm.
1308 *
1309 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1310 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1311 * used with either SHA-256 or SHA-384.
1312 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001313 * This key derivation algorithm uses the following inputs, which must be
1314 * passed in the order given here:
1315 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1316 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1317 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1318 *
1319 * For the application to TLS-1.2 key expansion, the seed is the
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001320 * concatenation of ServerHello.Random + ClientHello.Random,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001321 * and the label is "key expansion".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001322 *
1323 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1324 * TLS 1.2 PRF using HMAC-SHA-256.
1325 *
1326 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1327 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1328 *
1329 * \return The corresponding TLS-1.2 PRF algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001330 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001331 * hash algorithm.
1332 */
1333#define PSA_ALG_TLS12_PRF(hash_alg) \
1334 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1335
1336/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1337 *
1338 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1339 *
1340 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1341 * This macro may return either 0 or 1 if \c alg is not a supported
1342 * key derivation algorithm identifier.
1343 */
1344#define PSA_ALG_IS_TLS12_PRF(alg) \
1345 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1346#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1347 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1348
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001349#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x20000300)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001350/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1351 *
1352 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1353 * from the PreSharedKey (PSK) through the application of padding
1354 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1355 * The latter is based on HMAC and can be used with either SHA-256
1356 * or SHA-384.
1357 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001358 * This key derivation algorithm uses the following inputs, which must be
1359 * passed in the order given here:
1360 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1361 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1362 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1363 *
1364 * For the application to TLS-1.2, the seed (which is
1365 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1366 * ClientHello.Random + ServerHello.Random,
1367 * and the label is "master secret" or "extended master secret".
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001368 *
1369 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1370 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1371 *
1372 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1373 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1374 *
1375 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001376 * \return Unspecified if \p hash_alg is not a supported
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001377 * hash algorithm.
1378 */
1379#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1380 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1381
1382/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1383 *
1384 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1385 *
1386 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1387 * This macro may return either 0 or 1 if \c alg is not a supported
1388 * key derivation algorithm identifier.
1389 */
1390#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1391 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1392#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1393 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1394
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001395#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x0803ffff)
1396#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0x10fc0000)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001397
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001398/** Macro to build a combined algorithm that chains a key agreement with
1399 * a key derivation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001400 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001401 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1402 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1403 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1404 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001405 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001406 * \return The corresponding key agreement and derivation
1407 * algorithm.
1408 * \return Unspecified if \p ka_alg is not a supported
1409 * key agreement algorithm or \p kdf_alg is not a
1410 * supported key derivation algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001411 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001412#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1413 ((ka_alg) | (kdf_alg))
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001414
1415#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1416 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1417
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001418#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1419 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001420
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001421/** Whether the specified algorithm is a raw key agreement algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001422 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001423 * A raw key agreement algorithm is one that does not specify
1424 * a key derivation function.
1425 * Usually, raw key agreement algorithms are constructed directly with
1426 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
1427 * constructed with PSA_ALG_KEY_AGREEMENT().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001428 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001429 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1430 *
1431 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1432 * This macro may return either 0 or 1 if \p alg is not a supported
1433 * algorithm identifier.
1434 */
1435#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1436 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1437 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1438
1439#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1440 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1441
1442/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1443 *
1444 * The shared secret produced by key agreement is
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001445 * `g^{ab}` in big-endian format.
1446 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1447 * in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001448 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001449#define PSA_ALG_FFDH ((psa_algorithm_t)0x30100000)
1450
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001451/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1452 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001453 * This includes the raw finite field Diffie-Hellman algorithm as well as
1454 * finite-field Diffie-Hellman followed by any supporter key derivation
1455 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001456 *
1457 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1458 *
1459 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1460 * This macro may return either 0 or 1 if \c alg is not a supported
1461 * key agreement algorithm identifier.
1462 */
1463#define PSA_ALG_IS_FFDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001464 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001465
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001466/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1467 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001468 * The shared secret produced by key agreement is the x-coordinate of
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001469 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1470 * `m` is the bit size associated with the curve, i.e. the bit size of the
1471 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1472 * the byte containing the most significant bit of the shared secret
1473 * is padded with zero bits. The byte order is either little-endian
1474 * or big-endian depending on the curve type.
1475 *
1476 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1477 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1478 * in little-endian byte order.
1479 * The bit size is 448 for Curve448 and 255 for Curve25519.
1480 * - For Weierstrass curves over prime fields (curve types
1481 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1482 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1483 * in big-endian byte order.
1484 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1485 * - For Weierstrass curves over binary fields (curve types
1486 * `PSA_ECC_CURVE_SECTXXX`),
1487 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1488 * in big-endian byte order.
1489 * The bit size is `m` for the field `F_{2^m}`.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001490 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001491#define PSA_ALG_ECDH ((psa_algorithm_t)0x30200000)
1492
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001493/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1494 * algorithm.
1495 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001496 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1497 * elliptic curve Diffie-Hellman followed by any supporter key derivation
1498 * algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001499 *
1500 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1501 *
1502 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1503 * 0 otherwise.
1504 * This macro may return either 0 or 1 if \c alg is not a supported
1505 * key agreement algorithm identifier.
1506 */
1507#define PSA_ALG_IS_ECDH(alg) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001508 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001509
1510/** Whether the specified algorithm encoding is a wildcard.
1511 *
1512 * Wildcard values may only be used to set the usage algorithm field in
1513 * a policy, not to perform an operation.
1514 *
1515 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1516 *
1517 * \return 1 if \c alg is a wildcard algorithm encoding.
1518 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1519 * an operation).
1520 * \return This macro may return either 0 or 1 if \c alg is not a supported
1521 * algorithm identifier.
1522 */
1523#define PSA_ALG_IS_WILDCARD(alg) \
1524 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1525 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1526 (alg) == PSA_ALG_ANY_HASH)
1527
1528/**@}*/
1529
1530/** \defgroup key_lifetimes Key lifetimes
1531 * @{
1532 */
1533
Soby Mathew07ef6e42020-07-20 21:09:23 +01001534/** The default lifetime for volatile keys.
1535 *
1536 * A volatile key only exists as long as the handle to it is not closed.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001537 * The key material is guaranteed to be erased on a power reset.
Soby Mathew07ef6e42020-07-20 21:09:23 +01001538 *
1539 * A key with this lifetime is typically stored in the RAM area of the
1540 * PSA Crypto subsystem. However this is an implementation choice.
1541 * If an implementation stores data about the key in a non-volatile memory,
1542 * it must release all the resources associated with the key and erase the
1543 * key material if the calling application terminates.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001544 */
1545#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1546
Soby Mathew07ef6e42020-07-20 21:09:23 +01001547/** The default lifetime for persistent keys.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001548 *
1549 * A persistent key remains in storage until it is explicitly destroyed or
1550 * until the corresponding storage area is wiped. This specification does
1551 * not define any mechanism to wipe a storage area, but implementations may
1552 * provide their own mechanism (for example to perform a factory reset,
1553 * to prepare for device refurbishment, or to uninstall an application).
1554 *
1555 * This lifetime value is the default storage area for the calling
1556 * application. Implementations may offer other storage areas designated
1557 * by other lifetime values as implementation-specific extensions.
Soby Mathew07ef6e42020-07-20 21:09:23 +01001558 * See ::psa_key_lifetime_t for more information.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001559 */
1560#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1561
Soby Mathew07ef6e42020-07-20 21:09:23 +01001562/** The persistence level of volatile keys.
1563 *
1564 * See ::psa_key_persistence_t for more information.
1565 */
1566#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
1567
1568/** The default persistence level for persistent keys.
1569 *
1570 * See ::psa_key_persistence_t for more information.
1571 */
1572#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
1573
1574/** A persistence level indicating that a key is never destroyed.
1575 *
1576 * See ::psa_key_persistence_t for more information.
1577 */
1578#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
1579
1580#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
1581 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
1582
1583#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
1584 ((psa_key_location_t)((lifetime) >> 8))
1585
1586/** Whether a key lifetime indicates that the key is volatile.
1587 *
1588 * A volatile key is automatically destroyed by the implementation when
1589 * the application instance terminates. In particular, a volatile key
1590 * is automatically destroyed on a power reset of the device.
1591 *
1592 * A key that is not volatile is persistent. Persistent keys are
1593 * preserved until the application explicitly destroys them or until an
1594 * implementation-specific device management event occurs (for example,
1595 * a factory reset).
1596 *
1597 * \param lifetime The lifetime value to query (value of type
1598 * ::psa_key_lifetime_t).
1599 *
1600 * \return \c 1 if the key is volatile, otherwise \c 0.
1601 */
1602#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
1603 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
1604 PSA_KEY_LIFETIME_PERSISTENCE_VOLATILE)
1605
1606/** Construct a lifetime from a persistence level and a location.
1607 *
1608 * \param persistence The persistence level
1609 * (value of type ::psa_key_persistence_t).
1610 * \param location The location indicator
1611 * (value of type ::psa_key_location_t).
1612 *
1613 * \return The constructed lifetime value.
1614 */
1615#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
1616 ((location) << 8 | (persistence))
1617
1618/** The local storage area for persistent keys.
1619 *
1620 * This storage area is available on all systems that can store persistent
1621 * keys without delegating the storage to a third-party cryptoprocessor.
1622 *
1623 * See ::psa_key_location_t for more information.
1624 */
1625#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
1626
1627#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
1628
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001629/** The minimum value for a key identifier chosen by the application.
1630 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001631#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001632/** The maximum value for a key identifier chosen by the application.
1633 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001634#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001635/** The minimum value for a key identifier chosen by the implementation.
1636 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001637#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001638/** The maximum value for a key identifier chosen by the implementation.
1639 */
Soby Mathewd7b79f22020-05-21 15:06:54 +01001640#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001641
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001642/**@}*/
1643
1644/** \defgroup policy Key policies
1645 * @{
1646 */
1647
1648/** Whether the key may be exported.
1649 *
1650 * A public key or the public part of a key pair may always be exported
1651 * regardless of the value of this permission flag.
1652 *
1653 * If a key does not have export permission, implementations shall not
1654 * allow the key to be exported in plain form from the cryptoprocessor,
1655 * whether through psa_export_key() or through a proprietary interface.
1656 * The key may however be exportable in a wrapped form, i.e. in a form
1657 * where it is encrypted by another key.
1658 */
1659#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1660
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001661/** Whether the key may be copied.
1662 *
1663 * This flag allows the use of psa_copy_key() to make a copy of the key
1664 * with the same policy or a more restrictive policy.
1665 *
1666 * For lifetimes for which the key is located in a secure element which
1667 * enforce the non-exportability of keys, copying a key outside the secure
1668 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
1669 * Copying the key inside the secure element is permitted with just
1670 * #PSA_KEY_USAGE_COPY if the secure element supports it.
1671 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
1672 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
1673 * is sufficient to permit the copy.
1674 */
1675#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
1676
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001677/** Whether the key may be used to encrypt a message.
1678 *
1679 * This flag allows the key to be used for a symmetric encryption operation,
1680 * for an AEAD encryption-and-authentication operation,
1681 * or for an asymmetric encryption operation,
1682 * if otherwise permitted by the key's type and policy.
1683 *
1684 * For a key pair, this concerns the public key.
1685 */
1686#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1687
1688/** Whether the key may be used to decrypt a message.
1689 *
1690 * This flag allows the key to be used for a symmetric decryption operation,
1691 * for an AEAD decryption-and-verification operation,
1692 * or for an asymmetric decryption operation,
1693 * if otherwise permitted by the key's type and policy.
1694 *
1695 * For a key pair, this concerns the private key.
1696 */
1697#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1698
1699/** Whether the key may be used to sign a message.
1700 *
1701 * This flag allows the key to be used for a MAC calculation operation
1702 * or for an asymmetric signature operation,
1703 * if otherwise permitted by the key's type and policy.
1704 *
1705 * For a key pair, this concerns the private key.
1706 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001707#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00000400)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001708
1709/** Whether the key may be used to verify a message signature.
1710 *
1711 * This flag allows the key to be used for a MAC verification operation
1712 * or for an asymmetric signature verification operation,
1713 * if otherwise permitted by by the key's type and policy.
1714 *
1715 * For a key pair, this concerns the public key.
1716 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001717#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00000800)
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001718
1719/** Whether the key may be used to derive other keys.
1720 */
1721#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1722
1723/**@}*/
1724
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001725/** \defgroup derivation Key derivation
1726 * @{
1727 */
1728
1729/** A secret input for key derivation.
1730 *
1731 * This should be a key of type #PSA_KEY_TYPE_DERIVE
1732 * (passed to psa_key_derivation_input_key())
1733 * or the shared secret resulting from a key agreement
1734 * (obtained via psa_key_derivation_key_agreement()).
1735 *
1736 * The secret can also be a direct input (passed to
1737 * key_derivation_input_bytes()). In this case, the derivation operation
1738 * may not be used to derive keys: the operation will only allow
1739 * psa_key_derivation_output_bytes(), not psa_key_derivation_output_key().
1740 */
1741#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
1742
1743/** A label for key derivation.
1744 *
1745 * This should be a direct input.
1746 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1747 */
1748#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
1749
1750/** A salt for key derivation.
1751 *
1752 * This should be a direct input.
1753 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1754 */
1755#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
1756
1757/** An information string for key derivation.
1758 *
1759 * This should be a direct input.
1760 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1761 */
1762#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
1763
1764/** A seed for key derivation.
1765 *
1766 * This should be a direct input.
1767 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1768 */
1769#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
1770
1771/**@}*/
1772
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001773#endif /* PSA_CRYPTO_VALUES_H */