blob: 43b54b16f0d96d4a44ac969a9799deec894d48dd [file] [log] [blame]
Julian Halla7e76c82021-04-14 11:12:11 +01001/*
2 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
8 * \file psa/crypto_values.h
9 *
10 * \brief PSA cryptography module: macros to build and analyze integer values.
11 *
12 * \note This file may not be included directly. Applications must
13 * include psa/crypto.h. Drivers must include the appropriate driver
14 * 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#define PSA_SUCCESS ((psa_status_t)0)
35
36/** An error occurred that does not correspond to any defined
37 * failure cause.
38 *
39 * Implementations may use this error code if none of the other standard
40 * error codes are applicable. */
41#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
42
43/** The requested operation or a parameter is not supported
44 * by this implementation.
45 *
46 * Implementations should return this error code when an enumeration
47 * parameter such as a key type, algorithm, etc. is not recognized.
48 * If a combination of parameters is recognized and identified as
49 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
50#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
51
52/** The requested action is denied by a policy.
53 *
54 * Implementations should return this error code when the parameters
55 * are recognized as valid and supported, and a policy explicitly
56 * denies the requested operation.
57 *
58 * If a subset of the parameters of a function call identify a
59 * forbidden operation, and another subset of the parameters are
60 * not valid or not supported, it is unspecified whether the function
61 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
62 * #PSA_ERROR_INVALID_ARGUMENT. */
63#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
64
65/** An output buffer is too small.
66 *
67 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
68 * description to determine a sufficient buffer size.
69 *
70 * Implementations should preferably return this error code only
71 * in cases when performing the operation with a larger output
72 * buffer would succeed. However implementations may return this
73 * error if a function has invalid or unsupported parameters in addition
74 * to the parameters that determine the necessary output buffer size. */
75#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
76
77/** Asking for an item that already exists
78 *
79 * Implementations should return this error, when attempting
80 * to write an item (like a key) that already exists. */
81#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
82
83/** Asking for an item that doesn't exist
84 *
85 * Implementations should return this error, if a requested item (like
86 * a key) does not exist. */
87#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
88
89/** The requested action cannot be performed in the current state.
90 *
91 * Multipart operations return this error when one of the
92 * functions is called out of sequence. Refer to the function
93 * descriptions for permitted sequencing of functions.
94 *
95 * Implementations shall not return this error code to indicate
96 * that a key either exists or not,
97 * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
98 * as applicable.
99 *
100 * Implementations shall not return this error code to indicate that a
101 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
102 * instead. */
103#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
104
105/** The parameters passed to the function are invalid.
106 *
107 * Implementations may return this error any time a parameter or
108 * combination of parameters are recognized as invalid.
109 *
110 * Implementations shall not return this error code to indicate that a
111 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
112 * instead.
113 */
114#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
115
116/** There is not enough runtime memory.
117 *
118 * If the action is carried out across multiple security realms, this
119 * error can refer to available memory in any of the security realms. */
120#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
121
122/** There is not enough persistent storage.
123 *
124 * Functions that modify the key storage return this error code if
125 * there is insufficient storage space on the host media. In addition,
126 * many functions that do not otherwise access storage may return this
127 * error code if the implementation requires a mandatory log entry for
128 * the requested action and the log storage space is full. */
129#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
130
131/** There was a communication failure inside the implementation.
132 *
133 * This can indicate a communication failure between the application
134 * and an external cryptoprocessor or between the cryptoprocessor and
135 * an external volatile or persistent memory. A communication failure
136 * may be transient or permanent depending on the cause.
137 *
138 * \warning If a function returns this error, it is undetermined
139 * whether the requested action has completed or not. Implementations
140 * should return #PSA_SUCCESS on successful completion whenever
141 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
142 * if the requested action was completed successfully in an external
143 * cryptoprocessor but there was a breakdown of communication before
144 * the cryptoprocessor could report the status to the application.
145 */
146#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
147
148/** There was a storage failure that may have led to data loss.
149 *
150 * This error indicates that some persistent storage is corrupted.
151 * It should not be used for a corruption of volatile memory
152 * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
153 * between the cryptoprocessor and its external storage (use
154 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
155 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
156 *
157 * Note that a storage failure does not indicate that any data that was
158 * previously read is invalid. However this previously read data may no
159 * longer be readable from storage.
160 *
161 * When a storage failure occurs, it is no longer possible to ensure
162 * the global integrity of the keystore. Depending on the global
163 * integrity guarantees offered by the implementation, access to other
164 * data may or may not fail even if the data is still readable but
165 * its integrity cannot be guaranteed.
166 *
167 * Implementations should only use this error code to report a
168 * permanent storage corruption. However application writers should
169 * keep in mind that transient errors while reading the storage may be
170 * reported using this error code. */
171#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
172
173/** A hardware failure was detected.
174 *
175 * A hardware failure may be transient or permanent depending on the
176 * cause. */
177#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
178
179/** A tampering attempt was detected.
180 *
181 * If an application receives this error code, there is no guarantee
182 * that previously accessed or computed data was correct and remains
183 * confidential. Applications should not perform any security function
184 * and should enter a safe failure state.
185 *
186 * Implementations may return this error code if they detect an invalid
187 * state that cannot happen during normal operation and that indicates
188 * that the implementation's security guarantees no longer hold. Depending
189 * on the implementation architecture and on its security and safety goals,
190 * the implementation may forcibly terminate the application.
191 *
192 * This error code is intended as a last resort when a security breach
193 * is detected and it is unsure whether the keystore data is still
194 * protected. Implementations shall only return this error code
195 * to report an alarm from a tampering detector, to indicate that
196 * the confidentiality of stored data can no longer be guaranteed,
197 * or to indicate that the integrity of previously returned data is now
198 * considered compromised. Implementations shall not use this error code
199 * to indicate a hardware failure that merely makes it impossible to
200 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
201 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
202 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
203 * instead).
204 *
205 * This error indicates an attack against the application. Implementations
206 * shall not return this error code as a consequence of the behavior of
207 * the application itself. */
208#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
209
210/** There is not enough entropy to generate random data needed
211 * for the requested action.
212 *
213 * This error indicates a failure of a hardware random generator.
214 * Application writers should note that this error can be returned not
215 * only by functions whose purpose is to generate random data, such
216 * as key, IV or nonce generation, but also by functions that execute
217 * an algorithm with a randomized result, as well as functions that
218 * use randomization of intermediate computations as a countermeasure
219 * to certain attacks.
220 *
221 * Implementations should avoid returning this error after psa_crypto_init()
222 * has succeeded. Implementations should generate sufficient
223 * entropy during initialization and subsequently use a cryptographically
224 * secure pseudorandom generator (PRNG). However implementations may return
225 * this error at any time if a policy requires the PRNG to be reseeded
226 * during normal operation. */
227#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
228
229/** The signature, MAC or hash is incorrect.
230 *
231 * Verification functions return this error if the verification
232 * calculations completed successfully, and the value to be verified
233 * was determined to be incorrect.
234 *
235 * If the value to verify has an invalid size, implementations may return
236 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
237#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
238
239/** The decrypted padding is incorrect.
240 *
241 * \warning In some protocols, when decrypting data, it is essential that
242 * the behavior of the application does not depend on whether the padding
243 * is correct, down to precise timing. Applications should prefer
244 * protocols that use authenticated encryption rather than plain
245 * encryption. If the application must perform a decryption of
246 * unauthenticated data, the application writer should take care not
247 * to reveal whether the padding is invalid.
248 *
249 * Implementations should strive to make valid and invalid padding
250 * as close as possible to indistinguishable to an external observer.
251 * In particular, the timing of a decryption operation should not
252 * depend on the validity of the padding. */
253#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
254
255/** Return this error when there's insufficient data when attempting
256 * to read from a resource. */
257#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
258
259/** The key identifier is not valid. See also :ref:\`key-handles\`.
260 */
261#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
262
263/** Stored data has been corrupted.
264 *
265 * This error indicates that some persistent storage has suffered corruption.
266 * It does not indicate the following situations, which have specific error
267 * codes:
268 *
269 * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
270 * - A communication error between the cryptoprocessor and its external
271 * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
272 * - When the storage is in a valid state but is full - use
273 * #PSA_ERROR_INSUFFICIENT_STORAGE.
274 * - When the storage fails for other reasons - use
275 * #PSA_ERROR_STORAGE_FAILURE.
276 * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
277 *
278 * \note A storage corruption does not indicate that any data that was
279 * previously read is invalid. However this previously read data might no
280 * longer be readable from storage.
281 *
282 * When a storage failure occurs, it is no longer possible to ensure the
283 * global integrity of the keystore.
284 */
285#define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
286
287/** Data read from storage is not valid for the implementation.
288 *
289 * This error indicates that some data read from storage does not have a valid
290 * format. It does not indicate the following situations, which have specific
291 * error codes:
292 *
293 * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
294 * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
295 * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
296 *
297 * This error is typically a result of either storage corruption on a
298 * cleartext storage backend, or an attempt to read data that was
299 * written by an incompatible version of the library.
300 */
301#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
302
303/**@}*/
304
305/** \defgroup crypto_types Key and algorithm types
306 * @{
307 */
308
309/** An invalid key type value.
310 *
311 * Zero is not the encoding of any key type.
312 */
313#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x0000)
314
315/** Vendor-defined key type flag.
316 *
317 * Key types defined by this standard will never have the
318 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
319 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
320 * respect the bitwise structure used by standard encodings whenever practical.
321 */
322#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
323
324#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
325#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
326#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
327#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
328#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
329
330#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
331
332/** Whether a key type is vendor-defined.
333 *
334 * See also #PSA_KEY_TYPE_VENDOR_FLAG.
335 */
336#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
337 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
338
339/** Whether a key type is an unstructured array of bytes.
340 *
341 * This encompasses both symmetric keys and non-key data.
342 */
343#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
344 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
345 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
346
347/** Whether a key type is asymmetric: either a key pair or a public key. */
348#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
349 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
350 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
351 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
352/** Whether a key type is the public part of a key pair. */
353#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
354 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
355/** Whether a key type is a key pair containing a private part and a public
356 * part. */
357#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
358 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
359/** The key pair type corresponding to a public key type.
360 *
361 * You may also pass a key pair type as \p type, it will be left unchanged.
362 *
363 * \param type A public key type or key pair type.
364 *
365 * \return The corresponding key pair type.
366 * If \p type is not a public key or a key pair,
367 * the return value is undefined.
368 */
369#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
370 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
371/** The public key type corresponding to a key pair type.
372 *
373 * You may also pass a key pair type as \p type, it will be left unchanged.
374 *
375 * \param type A public key type or key pair type.
376 *
377 * \return The corresponding public key type.
378 * If \p type is not a public key or a key pair,
379 * the return value is undefined.
380 */
381#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
382 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
383
384/** Raw data.
385 *
386 * A "key" of this type cannot be used for any cryptographic operation.
387 * Applications may use this type to store arbitrary data in the keystore. */
388#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
389
390/** HMAC key.
391 *
392 * The key policy determines which underlying hash algorithm the key can be
393 * used for.
394 *
395 * HMAC keys should generally have the same size as the underlying hash.
396 * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
397 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
398#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
399
400/** A secret for key derivation.
401 *
402 * The key policy determines which key derivation algorithm the key
403 * can be used for.
404 */
405#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
406
407/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
408 *
409 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
410 * 32 bytes (AES-256).
411 */
412#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2400)
413
414/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
415 *
416 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
417 * 24 bytes (3-key 3DES).
418 *
419 * Note that single DES and 2-key 3DES are weak and strongly
420 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
421 * is weak and deprecated and should only be used in legacy protocols.
422 */
423#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
424
425/** Key for a cipher, AEAD or MAC algorithm based on the
426 * Camellia block cipher. */
427#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
428
429/** Key for the RC4 stream cipher.
430 *
431 * Note that RC4 is weak and deprecated and should only be used in
432 * legacy protocols. */
433#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x2002)
434
435/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
436 *
437 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
438 *
439 * Implementations must support 12-byte nonces, may support 8-byte nonces,
440 * and should reject other sizes.
441 */
442#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
443
444/** RSA public key. */
445#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
446/** RSA key pair (private and public key). */
447#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
448/** Whether a key type is an RSA key (pair or public-only). */
449#define PSA_KEY_TYPE_IS_RSA(type) \
450 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
451
452#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
453#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
454#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
455/** Elliptic curve key pair.
456 *
457 * \param curve A value of type ::psa_ecc_family_t that
458 * identifies the ECC curve to be used.
459 */
460#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
461 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
462/** Elliptic curve public key.
463 *
464 * \param curve A value of type ::psa_ecc_family_t that
465 * identifies the ECC curve to be used.
466 */
467#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
468 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
469
470/** Whether a key type is an elliptic curve key (pair or public-only). */
471#define PSA_KEY_TYPE_IS_ECC(type) \
472 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
473 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
474/** Whether a key type is an elliptic curve key pair. */
475#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
476 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
477 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
478/** Whether a key type is an elliptic curve public key. */
479#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
480 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
481 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
482
483/** Extract the curve from an elliptic curve key type. */
484#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
485 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
486 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
487 0))
488
489/** SEC Koblitz curves over prime fields.
490 *
491 * This family comprises the following curves:
492 * secp192k1, secp224k1, secp256k1.
493 * They are defined in _Standards for Efficient Cryptography_,
494 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
495 * https://www.secg.org/sec2-v2.pdf
496 */
497#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
498
499/** SEC random curves over prime fields.
500 *
501 * This family comprises the following curves:
502 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
503 * They are defined in _Standards for Efficient Cryptography_,
504 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
505 * https://www.secg.org/sec2-v2.pdf
506 */
507#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
508/* SECP160R2 (SEC2 v1, obsolete) */
509#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
510
511/** SEC Koblitz curves over binary fields.
512 *
513 * This family comprises the following curves:
514 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
515 * They are defined in _Standards for Efficient Cryptography_,
516 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
517 * https://www.secg.org/sec2-v2.pdf
518 */
519#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
520
521/** SEC random curves over binary fields.
522 *
523 * This family comprises the following curves:
524 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
525 * They are defined in _Standards for Efficient Cryptography_,
526 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
527 * https://www.secg.org/sec2-v2.pdf
528 */
529#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
530
531/** SEC additional random curves over binary fields.
532 *
533 * This family comprises the following curve:
534 * sect163r2.
535 * It is defined in _Standards for Efficient Cryptography_,
536 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
537 * https://www.secg.org/sec2-v2.pdf
538 */
539#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
540
541/** Brainpool P random curves.
542 *
543 * This family comprises the following curves:
544 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
545 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
546 * It is defined in RFC 5639.
547 */
548#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
549
550/** Curve25519 and Curve448.
551 *
552 * This family comprises the following Montgomery curves:
553 * - 255-bit: Bernstein et al.,
554 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
555 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
556 * - 448-bit: Hamburg,
557 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
558 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
559 */
560#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
561
562#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
563#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
564#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
565/** Diffie-Hellman key pair.
566 *
567 * \param group A value of type ::psa_dh_family_t that identifies the
568 * Diffie-Hellman group to be used.
569 */
570#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
571 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
572/** Diffie-Hellman public key.
573 *
574 * \param group A value of type ::psa_dh_family_t that identifies the
575 * Diffie-Hellman group to be used.
576 */
577#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
578 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
579
580/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
581#define PSA_KEY_TYPE_IS_DH(type) \
582 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
583 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
584/** Whether a key type is a Diffie-Hellman key pair. */
585#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
586 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
587 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
588/** Whether a key type is a Diffie-Hellman public key. */
589#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
590 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
591 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
592
593/** Extract the group from a Diffie-Hellman key type. */
594#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
595 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
596 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
597 0))
598
599/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
600 *
601 * This family includes groups with the following key sizes (in bits):
602 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
603 * all of these sizes or only a subset.
604 */
605#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
606
607#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
608 (((type) >> 8) & 7)
609/** The block size of a block cipher.
610 *
611 * \param type A cipher key type (value of type #psa_key_type_t).
612 *
613 * \return The block size for a block cipher, or 1 for a stream cipher.
614 * The return value is undefined if \p type is not a supported
615 * cipher key type.
616 *
617 * \note It is possible to build stream cipher algorithms on top of a block
618 * cipher, for example CTR mode (#PSA_ALG_CTR).
619 * This macro only takes the key type into account, so it cannot be
620 * used to determine the size of the data that #psa_cipher_update()
621 * might buffer for future processing in general.
622 *
623 * \note This macro returns a compile-time constant if its argument is one.
624 *
625 * \warning This macro may evaluate its argument multiple times.
626 */
627#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
628 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
629 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
630 0u)
631
632/** Vendor-defined algorithm flag.
633 *
634 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
635 * bit set. Vendors who define additional algorithms must use an encoding with
636 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
637 * used by standard encodings whenever practical.
638 */
639#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
640
641#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
642#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
643#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
644#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
645#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
646#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
647#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
648#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
649#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
650
651/** Whether an algorithm is vendor-defined.
652 *
653 * See also #PSA_ALG_VENDOR_FLAG.
654 */
655#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
656 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
657
658/** Whether the specified algorithm is a hash algorithm.
659 *
660 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
661 *
662 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
663 * This macro may return either 0 or 1 if \p alg is not a supported
664 * algorithm identifier.
665 */
666#define PSA_ALG_IS_HASH(alg) \
667 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
668
669/** Whether the specified algorithm is a MAC algorithm.
670 *
671 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
672 *
673 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
674 * This macro may return either 0 or 1 if \p alg is not a supported
675 * algorithm identifier.
676 */
677#define PSA_ALG_IS_MAC(alg) \
678 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
679
680/** Whether the specified algorithm is a symmetric cipher algorithm.
681 *
682 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
683 *
684 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
685 * This macro may return either 0 or 1 if \p alg is not a supported
686 * algorithm identifier.
687 */
688#define PSA_ALG_IS_CIPHER(alg) \
689 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
690
691/** Whether the specified algorithm is an authenticated encryption
692 * with associated data (AEAD) algorithm.
693 *
694 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
695 *
696 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
697 * This macro may return either 0 or 1 if \p alg is not a supported
698 * algorithm identifier.
699 */
700#define PSA_ALG_IS_AEAD(alg) \
701 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
702
703/** Whether the specified algorithm is an asymmetric signature algorithm,
704 * also known as public-key signature algorithm.
705 *
706 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
707 *
708 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
709 * This macro may return either 0 or 1 if \p alg is not a supported
710 * algorithm identifier.
711 */
712#define PSA_ALG_IS_SIGN(alg) \
713 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
714
715/** Whether the specified algorithm is an asymmetric encryption algorithm,
716 * also known as public-key encryption algorithm.
717 *
718 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
719 *
720 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
721 * This macro may return either 0 or 1 if \p alg is not a supported
722 * algorithm identifier.
723 */
724#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
725 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
726
727/** Whether the specified algorithm is a key agreement algorithm.
728 *
729 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
730 *
731 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
732 * This macro may return either 0 or 1 if \p alg is not a supported
733 * algorithm identifier.
734 */
735#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
736 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
737
738/** Whether the specified algorithm is a key derivation algorithm.
739 *
740 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
741 *
742 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
743 * This macro may return either 0 or 1 if \p alg is not a supported
744 * algorithm identifier.
745 */
746#define PSA_ALG_IS_KEY_DERIVATION(alg) \
747 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
748
749#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
750/** MD2 */
751#define PSA_ALG_MD2 ((psa_algorithm_t)0x02000001)
752/** MD4 */
753#define PSA_ALG_MD4 ((psa_algorithm_t)0x02000002)
754/** MD5 */
755#define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
756/** PSA_ALG_RIPEMD160 */
757#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
758/** SHA1 */
759#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
760/** SHA2-224 */
761#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
762/** SHA2-256 */
763#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
764/** SHA2-384 */
765#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
766/** SHA2-512 */
767#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
768/** SHA2-512/224 */
769#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
770/** SHA2-512/256 */
771#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
772/** SHA3-224 */
773#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
774/** SHA3-256 */
775#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
776/** SHA3-384 */
777#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
778/** SHA3-512 */
779#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
780
781/** In a hash-and-sign algorithm policy, allow any hash algorithm.
782 *
783 * This value may be used to form the algorithm usage field of a policy
784 * for a signature algorithm that is parametrized by a hash. The key
785 * may then be used to perform operations using the same signature
786 * algorithm parametrized with any supported hash.
787 *
788 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
789 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
790 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
791 * Then you may create and use a key as follows:
792 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
793 * ```
794 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
795 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
796 * ```
797 * - Import or generate key material.
798 * - Call psa_sign_hash() or psa_verify_hash(), passing
799 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
800 * call to sign or verify a message may use a different hash.
801 * ```
802 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
803 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
804 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
805 * ```
806 *
807 * This value may not be used to build other algorithms that are
808 * parametrized over a hash. For any valid use of this macro to build
809 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
810 *
811 * This value may not be used to build an algorithm specification to
812 * perform an operation. It is only valid to build policies.
813 */
814#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
815
816#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
817#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
818/** Macro to build an HMAC algorithm.
819 *
820 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
821 *
822 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
823 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
824 *
825 * \return The corresponding HMAC algorithm.
826 * \return Unspecified if \p hash_alg is not a supported
827 * hash algorithm.
828 */
829#define PSA_ALG_HMAC(hash_alg) \
830 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
831
832#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
833 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
834
835/** Whether the specified algorithm is an HMAC algorithm.
836 *
837 * HMAC is a family of MAC algorithms that are based on a hash function.
838 *
839 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
840 *
841 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
842 * This macro may return either 0 or 1 if \p alg is not a supported
843 * algorithm identifier.
844 */
845#define PSA_ALG_IS_HMAC(alg) \
846 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
847 PSA_ALG_HMAC_BASE)
848
849/* In the encoding of a MAC algorithm, the bits corresponding to
850 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
851 * truncated. As an exception, the value 0 means the untruncated algorithm,
852 * whatever its length is. The length is encoded in 6 bits, so it can
853 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
854 * to full length is correctly encoded as 0 and any non-trivial truncation
855 * is correctly encoded as a value between 1 and 63. */
856#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
857#define PSA_MAC_TRUNCATION_OFFSET 16
858
859/* In the encoding of a MAC algorithm, the bit corresponding to
860 * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
861 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
862 * algorithm policy can be used with any algorithm corresponding to the
863 * same base class and having a (potentially truncated) MAC length greater or
864 * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
865#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
866
867/** Macro to build a truncated MAC algorithm.
868 *
869 * A truncated MAC algorithm is identical to the corresponding MAC
870 * algorithm except that the MAC value for the truncated algorithm
871 * consists of only the first \p mac_length bytes of the MAC value
872 * for the untruncated algorithm.
873 *
874 * \note This macro may allow constructing algorithm identifiers that
875 * are not valid, either because the specified length is larger
876 * than the untruncated MAC or because the specified length is
877 * smaller than permitted by the implementation.
878 *
879 * \note It is implementation-defined whether a truncated MAC that
880 * is truncated to the same length as the MAC of the untruncated
881 * algorithm is considered identical to the untruncated algorithm
882 * for policy comparison purposes.
883 *
884 * \param mac_alg A MAC algorithm identifier (value of type
885 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
886 * is true). This may be a truncated or untruncated
887 * MAC algorithm.
888 * \param mac_length Desired length of the truncated MAC in bytes.
889 * This must be at most the full length of the MAC
890 * and must be at least an implementation-specified
891 * minimum. The implementation-specified minimum
892 * shall not be zero.
893 *
894 * \return The corresponding MAC algorithm with the specified
895 * length.
896 * \return Unspecified if \p alg is not a supported
897 * MAC algorithm or if \p mac_length is too small or
898 * too large for the specified MAC algorithm.
899 */
900#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
901 (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
902 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
903 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
904
905/** Macro to build the base MAC algorithm corresponding to a truncated
906 * MAC algorithm.
907 *
908 * \param mac_alg A MAC algorithm identifier (value of type
909 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
910 * is true). This may be a truncated or untruncated
911 * MAC algorithm.
912 *
913 * \return The corresponding base MAC algorithm.
914 * \return Unspecified if \p alg is not a supported
915 * MAC algorithm.
916 */
917#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
918 ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
919 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
920
921/** Length to which a MAC algorithm is truncated.
922 *
923 * \param mac_alg A MAC algorithm identifier (value of type
924 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
925 * is true).
926 *
927 * \return Length of the truncated MAC in bytes.
928 * \return 0 if \p alg is a non-truncated MAC algorithm.
929 * \return Unspecified if \p alg is not a supported
930 * MAC algorithm.
931 */
932#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
933 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
934
935/** Macro to build a MAC minimum-MAC-length wildcard algorithm.
936 *
937 * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
938 * sharing the same base algorithm, and where the (potentially truncated) MAC
939 * length of the specific algorithm is equal to or larger then the wildcard
940 * algorithm's minimum MAC length.
941 *
942 * \note When setting the minimum required MAC length to less than the
943 * smallest MAC length allowed by the base algorithm, this effectively
944 * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
945 *
946 * \param mac_alg A MAC algorithm identifier (value of type
947 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
948 * is true).
949 * \param min_mac_length Desired minimum length of the message authentication
950 * code in bytes. This must be at most the untruncated
951 * length of the MAC and must be at least 1.
952 *
953 * \return The corresponding MAC wildcard algorithm with the
954 * specified minimum length.
955 * \return Unspecified if \p mac_alg is not a supported MAC
956 * algorithm or if \p min_mac_length is less than 1 or
957 * too large for the specified MAC algorithm.
958 */
959#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
960 ( PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
961 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
962
963#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
964/** The CBC-MAC construction over a block cipher
965 *
966 * \warning CBC-MAC is insecure in many cases.
967 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
968 */
969#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
970/** The CMAC construction over a block cipher */
971#define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
972
973/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
974 *
975 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
976 *
977 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
978 * This macro may return either 0 or 1 if \p alg is not a supported
979 * algorithm identifier.
980 */
981#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
982 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
983 PSA_ALG_CIPHER_MAC_BASE)
984
985#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
986#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
987
988/** Whether the specified algorithm is a stream cipher.
989 *
990 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
991 * by applying a bitwise-xor with a stream of bytes that is generated
992 * from a key.
993 *
994 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
995 *
996 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
997 * This macro may return either 0 or 1 if \p alg is not a supported
998 * algorithm identifier or if it is not a symmetric cipher algorithm.
999 */
1000#define PSA_ALG_IS_STREAM_CIPHER(alg) \
1001 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1002 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1003
1004/** The stream cipher mode of a stream cipher algorithm.
1005 *
1006 * The underlying stream cipher is determined by the key type.
1007 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
1008 * - To use ARC4, use a key type of #PSA_KEY_TYPE_ARC4.
1009 */
1010#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
1011
1012/** The CTR stream cipher mode.
1013 *
1014 * CTR is a stream cipher which is built from a block cipher.
1015 * The underlying block cipher is determined by the key type.
1016 * For example, to use AES-128-CTR, use this algorithm with
1017 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1018 */
1019#define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
1020
1021/** The CFB stream cipher mode.
1022 *
1023 * The underlying block cipher is determined by the key type.
1024 */
1025#define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
1026
1027/** The OFB stream cipher mode.
1028 *
1029 * The underlying block cipher is determined by the key type.
1030 */
1031#define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
1032
1033/** The XTS cipher mode.
1034 *
1035 * XTS is a cipher mode which is built from a block cipher. It requires at
1036 * least one full block of input, but beyond this minimum the input
1037 * does not need to be a whole number of blocks.
1038 */
1039#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
1040
1041/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1042 *
1043 * \warning ECB mode does not protect the confidentiality of the encrypted data
1044 * except in extremely narrow circumstances. It is recommended that applications
1045 * only use ECB if they need to construct an operating mode that the
1046 * implementation does not provide. Implementations are encouraged to provide
1047 * the modes that applications need in preference to supporting direct access
1048 * to ECB.
1049 *
1050 * The underlying block cipher is determined by the key type.
1051 *
1052 * This symmetric cipher mode can only be used with messages whose lengths are a
1053 * multiple of the block size of the chosen block cipher.
1054 *
1055 * ECB mode does not accept an initialization vector (IV). When using a
1056 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1057 * and psa_cipher_set_iv() must not be called.
1058 */
1059#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
1060
1061/** The CBC block cipher chaining mode, with no padding.
1062 *
1063 * The underlying block cipher is determined by the key type.
1064 *
1065 * This symmetric cipher mode can only be used with messages whose lengths
1066 * are whole number of blocks for the chosen block cipher.
1067 */
1068#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
1069
1070/** The CBC block cipher chaining mode with PKCS#7 padding.
1071 *
1072 * The underlying block cipher is determined by the key type.
1073 *
1074 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1075 */
1076#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
1077
1078#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1079
1080/** Whether the specified algorithm is an AEAD mode on a block cipher.
1081 *
1082 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1083 *
1084 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1085 * a block cipher, 0 otherwise.
1086 * This macro may return either 0 or 1 if \p alg is not a supported
1087 * algorithm identifier.
1088 */
1089#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1090 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1091 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1092
1093/** The CCM authenticated encryption algorithm.
1094 *
1095 * The underlying block cipher is determined by the key type.
1096 */
1097#define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
1098
1099/** The GCM authenticated encryption algorithm.
1100 *
1101 * The underlying block cipher is determined by the key type.
1102 */
1103#define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
1104
1105/** The Chacha20-Poly1305 AEAD algorithm.
1106 *
1107 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1108 *
1109 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1110 * and should reject other sizes.
1111 *
1112 * Implementations must support 16-byte tags and should reject other sizes.
1113 */
1114#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
1115
1116/* In the encoding of a AEAD algorithm, the bits corresponding to
1117 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1118 * The constants for default lengths follow this encoding.
1119 */
1120#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1121#define PSA_AEAD_TAG_LENGTH_OFFSET 16
1122
1123/* In the encoding of an AEAD algorithm, the bit corresponding to
1124 * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1125 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1126 * algorithm policy can be used with any algorithm corresponding to the
1127 * same base class and having a tag length greater than or equal to the one
1128 * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1129#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
1130
1131/** Macro to build a shortened AEAD algorithm.
1132 *
1133 * A shortened AEAD algorithm is similar to the corresponding AEAD
1134 * algorithm, but has an authentication tag that consists of fewer bytes.
1135 * Depending on the algorithm, the tag length may affect the calculation
1136 * of the ciphertext.
1137 *
1138 * \param aead_alg An AEAD algorithm identifier (value of type
1139 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
1140 * is true).
1141 * \param tag_length Desired length of the authentication tag in bytes.
1142 *
1143 * \return The corresponding AEAD algorithm with the specified
1144 * length.
1145 * \return Unspecified if \p alg is not a supported
1146 * AEAD algorithm or if \p tag_length is not valid
1147 * for the specified AEAD algorithm.
1148 */
1149#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
1150 (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1151 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
1152 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1153 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1154
1155/** Retrieve the tag length of a specified AEAD algorithm
1156 *
1157 * \param aead_alg An AEAD algorithm identifier (value of type
1158 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
1159 * is true).
1160 *
1161 * \return The tag length specified by the input algorithm.
1162 * \return Unspecified if \p alg is not a supported
1163 * AEAD algorithm or if \p tag_length is not valid
1164 * for the specified AEAD algorithm.
1165 */
1166#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1167 (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
1168 PSA_AEAD_TAG_LENGTH_OFFSET )
1169
1170/** Calculate the corresponding AEAD algorithm with the default tag length.
1171 *
1172 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
1173 * #PSA_ALG_IS_AEAD(\p alg) is true).
1174 *
1175 * \return The corresponding AEAD algorithm with the default
1176 * tag length for that algorithm.
1177 */
1178#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
1179 ( \
1180 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1181 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1182 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
1183 0)
1184#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1185 PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1186 PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
1187 ref :
1188
1189/** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1190 *
1191 * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1192 * sharing the same base algorithm, and where the tag length of the specific
1193 * algorithm is equal to or larger then the minimum tag length specified by the
1194 * wildcard algorithm.
1195 *
1196 * \note When setting the minimum required tag length to less than the
1197 * smallest tag length allowed by the base algorithm, this effectively
1198 * becomes an 'any-tag-length-allowed' policy for that base algorithm.
1199 *
1200 * \param aead_alg An AEAD algorithm identifier (value of type
1201 * #psa_algorithm_t such that
1202 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1203 * \param min_tag_length Desired minimum length of the authentication tag in
1204 * bytes. This must be at least 1 and at most the largest
1205 * allowed tag length of the algorithm.
1206 *
1207 * \return The corresponding AEAD wildcard algorithm with the
1208 * specified minimum length.
1209 * \return Unspecified if \p aead_alg is not a supported
1210 * AEAD algorithm or if \p min_tag_length is less than 1
1211 * or too large for the specified AEAD algorithm.
1212 */
1213#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1214 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1215 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
1216
1217#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
1218/** RSA PKCS#1 v1.5 signature with hashing.
1219 *
1220 * This is the signature scheme defined by RFC 8017
1221 * (PKCS#1: RSA Cryptography Specifications) under the name
1222 * RSASSA-PKCS1-v1_5.
1223 *
1224 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1225 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1226 * This includes #PSA_ALG_ANY_HASH
1227 * when specifying the algorithm in a usage policy.
1228 *
1229 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
1230 * \return Unspecified if \p hash_alg is not a supported
1231 * hash algorithm.
1232 */
1233#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1234 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1235/** Raw PKCS#1 v1.5 signature.
1236 *
1237 * The input to this algorithm is the DigestInfo structure used by
1238 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1239 * steps 3&ndash;6.
1240 */
1241#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1242#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1243 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1244
1245#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
1246/** RSA PSS signature with hashing.
1247 *
1248 * This is the signature scheme defined by RFC 8017
1249 * (PKCS#1: RSA Cryptography Specifications) under the name
1250 * RSASSA-PSS, with the message generation function MGF1, and with
1251 * a salt length equal to the length of the hash. The specified
1252 * hash algorithm is used to hash the input message, to create the
1253 * salted hash, and for the mask generation.
1254 *
1255 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1256 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1257 * This includes #PSA_ALG_ANY_HASH
1258 * when specifying the algorithm in a usage policy.
1259 *
1260 * \return The corresponding RSA PSS signature algorithm.
1261 * \return Unspecified if \p hash_alg is not a supported
1262 * hash algorithm.
1263 */
1264#define PSA_ALG_RSA_PSS(hash_alg) \
1265 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1266#define PSA_ALG_IS_RSA_PSS(alg) \
1267 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1268
1269#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
1270/** ECDSA signature with hashing.
1271 *
1272 * This is the ECDSA signature scheme defined by ANSI X9.62,
1273 * with a random per-message secret number (*k*).
1274 *
1275 * The representation of the signature as a byte string consists of
1276 * the concatentation of the signature values *r* and *s*. Each of
1277 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1278 * of the base point of the curve in octets. Each value is represented
1279 * in big-endian order (most significant octet first).
1280 *
1281 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1282 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1283 * This includes #PSA_ALG_ANY_HASH
1284 * when specifying the algorithm in a usage policy.
1285 *
1286 * \return The corresponding ECDSA signature algorithm.
1287 * \return Unspecified if \p hash_alg is not a supported
1288 * hash algorithm.
1289 */
1290#define PSA_ALG_ECDSA(hash_alg) \
1291 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1292/** ECDSA signature without hashing.
1293 *
1294 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1295 * without specifying a hash algorithm. This algorithm may only be
1296 * used to sign or verify a sequence of bytes that should be an
1297 * already-calculated hash. Note that the input is padded with
1298 * zeros on the left or truncated on the left as required to fit
1299 * the curve size.
1300 */
1301#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1302#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
1303/** Deterministic ECDSA signature with hashing.
1304 *
1305 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1306 *
1307 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1308 *
1309 * Note that when this algorithm is used for verification, signatures
1310 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1311 * same private key are accepted. In other words,
1312 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1313 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1314 *
1315 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1316 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1317 * This includes #PSA_ALG_ANY_HASH
1318 * when specifying the algorithm in a usage policy.
1319 *
1320 * \return The corresponding deterministic ECDSA signature
1321 * algorithm.
1322 * \return Unspecified if \p hash_alg is not a supported
1323 * hash algorithm.
1324 */
1325#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1326 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1327#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
1328#define PSA_ALG_IS_ECDSA(alg) \
1329 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
1330 PSA_ALG_ECDSA_BASE)
1331#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1332 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
1333#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1334 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1335#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1336 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1337
1338/** Whether the specified algorithm is a hash-and-sign algorithm.
1339 *
1340 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1341 * structured in two parts: first the calculation of a hash in a way that
1342 * does not depend on the key, then the calculation of a signature from the
1343 * hash value and the key.
1344 *
1345 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1346 *
1347 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1348 * This macro may return either 0 or 1 if \p alg is not a supported
1349 * algorithm identifier.
1350 */
1351#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1352 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1353 PSA_ALG_IS_ECDSA(alg))
1354
1355/** Get the hash used by a hash-and-sign signature algorithm.
1356 *
1357 * A hash-and-sign algorithm is a signature algorithm which is
1358 * composed of two phases: first a hashing phase which does not use
1359 * the key and produces a hash of the input message, then a signing
1360 * phase which only uses the hash and the key and not the message
1361 * itself.
1362 *
1363 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1364 * #PSA_ALG_IS_SIGN(\p alg) is true).
1365 *
1366 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1367 * algorithm.
1368 * \return 0 if \p alg is a signature algorithm that does not
1369 * follow the hash-and-sign structure.
1370 * \return Unspecified if \p alg is not a signature algorithm or
1371 * if it is not supported by the implementation.
1372 */
1373#define PSA_ALG_SIGN_GET_HASH(alg) \
1374 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1375 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1376 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1377 0)
1378
1379/** RSA PKCS#1 v1.5 encryption.
1380 */
1381#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
1382
1383#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
1384/** RSA OAEP encryption.
1385 *
1386 * This is the encryption scheme defined by RFC 8017
1387 * (PKCS#1: RSA Cryptography Specifications) under the name
1388 * RSAES-OAEP, with the message generation function MGF1.
1389 *
1390 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1391 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1392 * for MGF1.
1393 *
1394 * \return The corresponding RSA OAEP encryption algorithm.
1395 * \return Unspecified if \p hash_alg is not a supported
1396 * hash algorithm.
1397 */
1398#define PSA_ALG_RSA_OAEP(hash_alg) \
1399 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1400#define PSA_ALG_IS_RSA_OAEP(alg) \
1401 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1402#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1403 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1404 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1405 0)
1406
1407#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
1408/** Macro to build an HKDF algorithm.
1409 *
1410 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1411 *
1412 * This key derivation algorithm uses the following inputs:
1413 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1414 * It is optional; if omitted, the derivation uses an empty salt.
1415 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1416 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1417 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1418 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1419 * starting to generate output.
1420 *
1421 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1422 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1423 *
1424 * \return The corresponding HKDF algorithm.
1425 * \return Unspecified if \p hash_alg is not a supported
1426 * hash algorithm.
1427 */
1428#define PSA_ALG_HKDF(hash_alg) \
1429 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1430/** Whether the specified algorithm is an HKDF algorithm.
1431 *
1432 * HKDF is a family of key derivation algorithms that are based on a hash
1433 * function and the HMAC construction.
1434 *
1435 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1436 *
1437 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1438 * This macro may return either 0 or 1 if \c alg is not a supported
1439 * key derivation algorithm identifier.
1440 */
1441#define PSA_ALG_IS_HKDF(alg) \
1442 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1443#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1444 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1445
1446#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
1447/** Macro to build a TLS-1.2 PRF algorithm.
1448 *
1449 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1450 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1451 * used with either SHA-256 or SHA-384.
1452 *
1453 * This key derivation algorithm uses the following inputs, which must be
1454 * passed in the order given here:
1455 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1456 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1457 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1458 *
1459 * For the application to TLS-1.2 key expansion, the seed is the
1460 * concatenation of ServerHello.Random + ClientHello.Random,
1461 * and the label is "key expansion".
1462 *
1463 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1464 * TLS 1.2 PRF using HMAC-SHA-256.
1465 *
1466 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1467 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1468 *
1469 * \return The corresponding TLS-1.2 PRF algorithm.
1470 * \return Unspecified if \p hash_alg is not a supported
1471 * hash algorithm.
1472 */
1473#define PSA_ALG_TLS12_PRF(hash_alg) \
1474 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1475
1476/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1477 *
1478 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1479 *
1480 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1481 * This macro may return either 0 or 1 if \c alg is not a supported
1482 * key derivation algorithm identifier.
1483 */
1484#define PSA_ALG_IS_TLS12_PRF(alg) \
1485 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1486#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1487 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1488
1489#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
1490/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1491 *
1492 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1493 * from the PreSharedKey (PSK) through the application of padding
1494 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1495 * The latter is based on HMAC and can be used with either SHA-256
1496 * or SHA-384.
1497 *
1498 * This key derivation algorithm uses the following inputs, which must be
1499 * passed in the order given here:
1500 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1501 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1502 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1503 *
1504 * For the application to TLS-1.2, the seed (which is
1505 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1506 * ClientHello.Random + ServerHello.Random,
1507 * and the label is "master secret" or "extended master secret".
1508 *
1509 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1510 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1511 *
1512 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1513 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1514 *
1515 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1516 * \return Unspecified if \p hash_alg is not a supported
1517 * hash algorithm.
1518 */
1519#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1520 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1521
1522/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1523 *
1524 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1525 *
1526 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1527 * This macro may return either 0 or 1 if \c alg is not a supported
1528 * key derivation algorithm identifier.
1529 */
1530#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1531 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1532#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1533 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1534
1535#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
1536#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
1537
1538/** Macro to build a combined algorithm that chains a key agreement with
1539 * a key derivation.
1540 *
1541 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1542 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1543 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1544 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
1545 *
1546 * \return The corresponding key agreement and derivation
1547 * algorithm.
1548 * \return Unspecified if \p ka_alg is not a supported
1549 * key agreement algorithm or \p kdf_alg is not a
1550 * supported key derivation algorithm.
1551 */
1552#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1553 ((ka_alg) | (kdf_alg))
1554
1555#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1556 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1557
1558#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1559 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
1560
1561/** Whether the specified algorithm is a raw key agreement algorithm.
1562 *
1563 * A raw key agreement algorithm is one that does not specify
1564 * a key derivation function.
1565 * Usually, raw key agreement algorithms are constructed directly with
1566 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
1567 * constructed with #PSA_ALG_KEY_AGREEMENT().
1568 *
1569 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1570 *
1571 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1572 * This macro may return either 0 or 1 if \p alg is not a supported
1573 * algorithm identifier.
1574 */
1575#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
1576 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1577 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
1578
1579#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1580 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1581
1582/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
1583 *
1584 * The shared secret produced by key agreement is
1585 * `g^{ab}` in big-endian format.
1586 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1587 * in bits.
1588 */
1589#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
1590
1591/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1592 *
1593 * This includes the raw finite field Diffie-Hellman algorithm as well as
1594 * finite-field Diffie-Hellman followed by any supporter key derivation
1595 * algorithm.
1596 *
1597 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1598 *
1599 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1600 * This macro may return either 0 or 1 if \c alg is not a supported
1601 * key agreement algorithm identifier.
1602 */
1603#define PSA_ALG_IS_FFDH(alg) \
1604 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
1605
1606/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1607 *
1608 * The shared secret produced by key agreement is the x-coordinate of
1609 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1610 * `m` is the bit size associated with the curve, i.e. the bit size of the
1611 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1612 * the byte containing the most significant bit of the shared secret
1613 * is padded with zero bits. The byte order is either little-endian
1614 * or big-endian depending on the curve type.
1615 *
1616 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
1617 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1618 * in little-endian byte order.
1619 * The bit size is 448 for Curve448 and 255 for Curve25519.
1620 * - For Weierstrass curves over prime fields (curve types
1621 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
1622 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1623 * in big-endian byte order.
1624 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1625 * - For Weierstrass curves over binary fields (curve types
1626 * `PSA_ECC_FAMILY_SECTXXX`),
1627 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1628 * in big-endian byte order.
1629 * The bit size is `m` for the field `F_{2^m}`.
1630 */
1631#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
1632
1633/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1634 * algorithm.
1635 *
1636 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1637 * elliptic curve Diffie-Hellman followed by any supporter key derivation
1638 * algorithm.
1639 *
1640 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1641 *
1642 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1643 * 0 otherwise.
1644 * This macro may return either 0 or 1 if \c alg is not a supported
1645 * key agreement algorithm identifier.
1646 */
1647#define PSA_ALG_IS_ECDH(alg) \
1648 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
1649
1650/** Whether the specified algorithm encoding is a wildcard.
1651 *
1652 * Wildcard values may only be used to set the usage algorithm field in
1653 * a policy, not to perform an operation.
1654 *
1655 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1656 *
1657 * \return 1 if \c alg is a wildcard algorithm encoding.
1658 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1659 * an operation).
1660 * \return This macro may return either 0 or 1 if \c alg is not a supported
1661 * algorithm identifier.
1662 */
1663#define PSA_ALG_IS_WILDCARD(alg) \
1664 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1665 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1666 PSA_ALG_IS_MAC(alg) ? \
1667 (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
1668 PSA_ALG_IS_AEAD(alg) ? \
1669 (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
1670 (alg) == PSA_ALG_ANY_HASH)
1671
1672/**@}*/
1673
1674/** \defgroup key_lifetimes Key lifetimes
1675 * @{
1676 */
1677
1678/** The default lifetime for volatile keys.
1679 *
1680 * A volatile key only exists as long as the identifier to it is not destroyed.
1681 * The key material is guaranteed to be erased on a power reset.
1682 *
1683 * A key with this lifetime is typically stored in the RAM area of the
1684 * PSA Crypto subsystem. However this is an implementation choice.
1685 * If an implementation stores data about the key in a non-volatile memory,
1686 * it must release all the resources associated with the key and erase the
1687 * key material if the calling application terminates.
1688 */
1689#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1690
1691/** The default lifetime for persistent keys.
1692 *
1693 * A persistent key remains in storage until it is explicitly destroyed or
1694 * until the corresponding storage area is wiped. This specification does
1695 * not define any mechanism to wipe a storage area, but integrations may
1696 * provide their own mechanism (for example to perform a factory reset,
1697 * to prepare for device refurbishment, or to uninstall an application).
1698 *
1699 * This lifetime value is the default storage area for the calling
1700 * application. Integrations of Mbed TLS may support other persistent lifetimes.
1701 * See ::psa_key_lifetime_t for more information.
1702 */
1703#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1704
1705/** The persistence level of volatile keys.
1706 *
1707 * See ::psa_key_persistence_t for more information.
1708 */
1709#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
1710
1711/** The default persistence level for persistent keys.
1712 *
1713 * See ::psa_key_persistence_t for more information.
1714 */
1715#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
1716
1717/** A persistence level indicating that a key is never destroyed.
1718 *
1719 * See ::psa_key_persistence_t for more information.
1720 */
1721#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
1722
1723#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
1724 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
1725
1726#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
1727 ((psa_key_location_t)((lifetime) >> 8))
1728
1729/** Whether a key lifetime indicates that the key is volatile.
1730 *
1731 * A volatile key is automatically destroyed by the implementation when
1732 * the application instance terminates. In particular, a volatile key
1733 * is automatically destroyed on a power reset of the device.
1734 *
1735 * A key that is not volatile is persistent. Persistent keys are
1736 * preserved until the application explicitly destroys them or until an
1737 * implementation-specific device management event occurs (for example,
1738 * a factory reset).
1739 *
1740 * \param lifetime The lifetime value to query (value of type
1741 * ::psa_key_lifetime_t).
1742 *
1743 * \return \c 1 if the key is volatile, otherwise \c 0.
1744 */
1745#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
1746 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
1747 PSA_KEY_PERSISTENCE_VOLATILE)
1748
1749/** Construct a lifetime from a persistence level and a location.
1750 *
1751 * \param persistence The persistence level
1752 * (value of type ::psa_key_persistence_t).
1753 * \param location The location indicator
1754 * (value of type ::psa_key_location_t).
1755 *
1756 * \return The constructed lifetime value.
1757 */
1758#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
1759 ((location) << 8 | (persistence))
1760
1761/** The local storage area for persistent keys.
1762 *
1763 * This storage area is available on all systems that can store persistent
1764 * keys without delegating the storage to a third-party cryptoprocessor.
1765 *
1766 * See ::psa_key_location_t for more information.
1767 */
1768#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
1769
1770#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
1771
1772/** The minimum value for a key identifier chosen by the application.
1773 */
1774#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
1775/** The maximum value for a key identifier chosen by the application.
1776 */
1777#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
1778/** The minimum value for a key identifier chosen by the implementation.
1779 */
1780#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
1781/** The maximum value for a key identifier chosen by the implementation.
1782 */
1783#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
1784
1785/**@}*/
1786
1787/** \defgroup policy Key policies
1788 * @{
1789 */
1790
1791/** Whether the key may be exported.
1792 *
1793 * A public key or the public part of a key pair may always be exported
1794 * regardless of the value of this permission flag.
1795 *
1796 * If a key does not have export permission, implementations shall not
1797 * allow the key to be exported in plain form from the cryptoprocessor,
1798 * whether through psa_export_key() or through a proprietary interface.
1799 * The key may however be exportable in a wrapped form, i.e. in a form
1800 * where it is encrypted by another key.
1801 */
1802#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1803
1804/** Whether the key may be copied.
1805 *
1806 * This flag allows the use of psa_copy_key() to make a copy of the key
1807 * with the same policy or a more restrictive policy.
1808 *
1809 * For lifetimes for which the key is located in a secure element which
1810 * enforce the non-exportability of keys, copying a key outside the secure
1811 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
1812 * Copying the key inside the secure element is permitted with just
1813 * #PSA_KEY_USAGE_COPY if the secure element supports it.
1814 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
1815 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
1816 * is sufficient to permit the copy.
1817 */
1818#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
1819
1820/** Whether the key may be used to encrypt a message.
1821 *
1822 * This flag allows the key to be used for a symmetric encryption operation,
1823 * for an AEAD encryption-and-authentication operation,
1824 * or for an asymmetric encryption operation,
1825 * if otherwise permitted by the key's type and policy.
1826 *
1827 * For a key pair, this concerns the public key.
1828 */
1829#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1830
1831/** Whether the key may be used to decrypt a message.
1832 *
1833 * This flag allows the key to be used for a symmetric decryption operation,
1834 * for an AEAD decryption-and-verification operation,
1835 * or for an asymmetric decryption operation,
1836 * if otherwise permitted by the key's type and policy.
1837 *
1838 * For a key pair, this concerns the private key.
1839 */
1840#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1841
1842/** Whether the key may be used to sign a message.
1843 *
1844 * This flag allows the key to be used for a MAC calculation operation
1845 * or for an asymmetric signature operation,
1846 * if otherwise permitted by the key's type and policy.
1847 *
1848 * For a key pair, this concerns the private key.
1849 */
1850#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
1851
1852/** Whether the key may be used to verify a message signature.
1853 *
1854 * This flag allows the key to be used for a MAC verification operation
1855 * or for an asymmetric signature verification operation,
1856 * if otherwise permitted by by the key's type and policy.
1857 *
1858 * For a key pair, this concerns the public key.
1859 */
1860#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
1861
1862/** Whether the key may be used to derive other keys.
1863 */
1864#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
1865
1866/**@}*/
1867
1868/** \defgroup derivation Key derivation
1869 * @{
1870 */
1871
1872/** A secret input for key derivation.
1873 *
1874 * This should be a key of type #PSA_KEY_TYPE_DERIVE
1875 * (passed to psa_key_derivation_input_key())
1876 * or the shared secret resulting from a key agreement
1877 * (obtained via psa_key_derivation_key_agreement()).
1878 *
1879 * The secret can also be a direct input (passed to
1880 * key_derivation_input_bytes()). In this case, the derivation operation
1881 * may not be used to derive keys: the operation will only allow
1882 * psa_key_derivation_output_bytes(), not psa_key_derivation_output_key().
1883 */
1884#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
1885
1886/** A label for key derivation.
1887 *
1888 * This should be a direct input.
1889 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1890 */
1891#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
1892
1893/** A salt for key derivation.
1894 *
1895 * This should be a direct input.
1896 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1897 */
1898#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
1899
1900/** An information string for key derivation.
1901 *
1902 * This should be a direct input.
1903 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1904 */
1905#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
1906
1907/** A seed for key derivation.
1908 *
1909 * This should be a direct input.
1910 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
1911 */
1912#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
1913
1914/**@}*/
1915
1916#endif /* PSA_CRYPTO_VALUES_H */