1*62c56f98SSadaf Ebrahimi /**
2*62c56f98SSadaf Ebrahimi * \file psa/crypto_values.h
3*62c56f98SSadaf Ebrahimi *
4*62c56f98SSadaf Ebrahimi * \brief PSA cryptography module: macros to build and analyze integer values.
5*62c56f98SSadaf Ebrahimi *
6*62c56f98SSadaf Ebrahimi * \note This file may not be included directly. Applications must
7*62c56f98SSadaf Ebrahimi * include psa/crypto.h. Drivers must include the appropriate driver
8*62c56f98SSadaf Ebrahimi * header file.
9*62c56f98SSadaf Ebrahimi *
10*62c56f98SSadaf Ebrahimi * This file contains portable definitions of macros to build and analyze
11*62c56f98SSadaf Ebrahimi * values of integral types that encode properties of cryptographic keys,
12*62c56f98SSadaf Ebrahimi * designations of cryptographic algorithms, and error codes returned by
13*62c56f98SSadaf Ebrahimi * the library.
14*62c56f98SSadaf Ebrahimi *
15*62c56f98SSadaf Ebrahimi * Note that many of the constants defined in this file are embedded in
16*62c56f98SSadaf Ebrahimi * the persistent key store, as part of key metadata (including usage
17*62c56f98SSadaf Ebrahimi * policies). As a consequence, they must not be changed (unless the storage
18*62c56f98SSadaf Ebrahimi * format version changes).
19*62c56f98SSadaf Ebrahimi *
20*62c56f98SSadaf Ebrahimi * This header file only defines preprocessor macros.
21*62c56f98SSadaf Ebrahimi */
22*62c56f98SSadaf Ebrahimi /*
23*62c56f98SSadaf Ebrahimi * Copyright The Mbed TLS Contributors
24*62c56f98SSadaf Ebrahimi * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
25*62c56f98SSadaf Ebrahimi */
26*62c56f98SSadaf Ebrahimi
27*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_VALUES_H
28*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_VALUES_H
29*62c56f98SSadaf Ebrahimi #include "mbedtls/private_access.h"
30*62c56f98SSadaf Ebrahimi
31*62c56f98SSadaf Ebrahimi /** \defgroup error Error codes
32*62c56f98SSadaf Ebrahimi * @{
33*62c56f98SSadaf Ebrahimi */
34*62c56f98SSadaf Ebrahimi
35*62c56f98SSadaf Ebrahimi /* PSA error codes */
36*62c56f98SSadaf Ebrahimi
37*62c56f98SSadaf Ebrahimi /* Error codes are standardized across PSA domains (framework, crypto, storage,
38*62c56f98SSadaf Ebrahimi * etc.). Do not change the values in this section or even the expansions
39*62c56f98SSadaf Ebrahimi * of each macro: it must be possible to `#include` both this header
40*62c56f98SSadaf Ebrahimi * and some other PSA component's headers in the same C source,
41*62c56f98SSadaf Ebrahimi * which will lead to duplicate definitions of the `PSA_SUCCESS` and
42*62c56f98SSadaf Ebrahimi * `PSA_ERROR_xxx` macros, which is ok if and only if the macros expand
43*62c56f98SSadaf Ebrahimi * to the same sequence of tokens.
44*62c56f98SSadaf Ebrahimi *
45*62c56f98SSadaf Ebrahimi * If you must add a new
46*62c56f98SSadaf Ebrahimi * value, check with the Arm PSA framework group to pick one that other
47*62c56f98SSadaf Ebrahimi * domains aren't already using. */
48*62c56f98SSadaf Ebrahimi
49*62c56f98SSadaf Ebrahimi /* Tell uncrustify not to touch the constant definitions, otherwise
50*62c56f98SSadaf Ebrahimi * it might change the spacing to something that is not PSA-compliant
51*62c56f98SSadaf Ebrahimi * (e.g. adding a space after casts).
52*62c56f98SSadaf Ebrahimi *
53*62c56f98SSadaf Ebrahimi * *INDENT-OFF*
54*62c56f98SSadaf Ebrahimi */
55*62c56f98SSadaf Ebrahimi
56*62c56f98SSadaf Ebrahimi /** The action was completed successfully. */
57*62c56f98SSadaf Ebrahimi #define PSA_SUCCESS ((psa_status_t)0)
58*62c56f98SSadaf Ebrahimi
59*62c56f98SSadaf Ebrahimi /** An error occurred that does not correspond to any defined
60*62c56f98SSadaf Ebrahimi * failure cause.
61*62c56f98SSadaf Ebrahimi *
62*62c56f98SSadaf Ebrahimi * Implementations may use this error code if none of the other standard
63*62c56f98SSadaf Ebrahimi * error codes are applicable. */
64*62c56f98SSadaf Ebrahimi #define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
65*62c56f98SSadaf Ebrahimi
66*62c56f98SSadaf Ebrahimi /** The requested operation or a parameter is not supported
67*62c56f98SSadaf Ebrahimi * by this implementation.
68*62c56f98SSadaf Ebrahimi *
69*62c56f98SSadaf Ebrahimi * Implementations should return this error code when an enumeration
70*62c56f98SSadaf Ebrahimi * parameter such as a key type, algorithm, etc. is not recognized.
71*62c56f98SSadaf Ebrahimi * If a combination of parameters is recognized and identified as
72*62c56f98SSadaf Ebrahimi * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
73*62c56f98SSadaf Ebrahimi #define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
74*62c56f98SSadaf Ebrahimi
75*62c56f98SSadaf Ebrahimi /** The requested action is denied by a policy.
76*62c56f98SSadaf Ebrahimi *
77*62c56f98SSadaf Ebrahimi * Implementations should return this error code when the parameters
78*62c56f98SSadaf Ebrahimi * are recognized as valid and supported, and a policy explicitly
79*62c56f98SSadaf Ebrahimi * denies the requested operation.
80*62c56f98SSadaf Ebrahimi *
81*62c56f98SSadaf Ebrahimi * If a subset of the parameters of a function call identify a
82*62c56f98SSadaf Ebrahimi * forbidden operation, and another subset of the parameters are
83*62c56f98SSadaf Ebrahimi * not valid or not supported, it is unspecified whether the function
84*62c56f98SSadaf Ebrahimi * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
85*62c56f98SSadaf Ebrahimi * #PSA_ERROR_INVALID_ARGUMENT. */
86*62c56f98SSadaf Ebrahimi #define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
87*62c56f98SSadaf Ebrahimi
88*62c56f98SSadaf Ebrahimi /** An output buffer is too small.
89*62c56f98SSadaf Ebrahimi *
90*62c56f98SSadaf Ebrahimi * Applications can call the \c PSA_xxx_SIZE macro listed in the function
91*62c56f98SSadaf Ebrahimi * description to determine a sufficient buffer size.
92*62c56f98SSadaf Ebrahimi *
93*62c56f98SSadaf Ebrahimi * Implementations should preferably return this error code only
94*62c56f98SSadaf Ebrahimi * in cases when performing the operation with a larger output
95*62c56f98SSadaf Ebrahimi * buffer would succeed. However implementations may return this
96*62c56f98SSadaf Ebrahimi * error if a function has invalid or unsupported parameters in addition
97*62c56f98SSadaf Ebrahimi * to the parameters that determine the necessary output buffer size. */
98*62c56f98SSadaf Ebrahimi #define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
99*62c56f98SSadaf Ebrahimi
100*62c56f98SSadaf Ebrahimi /** Asking for an item that already exists
101*62c56f98SSadaf Ebrahimi *
102*62c56f98SSadaf Ebrahimi * Implementations should return this error, when attempting
103*62c56f98SSadaf Ebrahimi * to write an item (like a key) that already exists. */
104*62c56f98SSadaf Ebrahimi #define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
105*62c56f98SSadaf Ebrahimi
106*62c56f98SSadaf Ebrahimi /** Asking for an item that doesn't exist
107*62c56f98SSadaf Ebrahimi *
108*62c56f98SSadaf Ebrahimi * Implementations should return this error, if a requested item (like
109*62c56f98SSadaf Ebrahimi * a key) does not exist. */
110*62c56f98SSadaf Ebrahimi #define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
111*62c56f98SSadaf Ebrahimi
112*62c56f98SSadaf Ebrahimi /** The requested action cannot be performed in the current state.
113*62c56f98SSadaf Ebrahimi *
114*62c56f98SSadaf Ebrahimi * Multipart operations return this error when one of the
115*62c56f98SSadaf Ebrahimi * functions is called out of sequence. Refer to the function
116*62c56f98SSadaf Ebrahimi * descriptions for permitted sequencing of functions.
117*62c56f98SSadaf Ebrahimi *
118*62c56f98SSadaf Ebrahimi * Implementations shall not return this error code to indicate
119*62c56f98SSadaf Ebrahimi * that a key either exists or not,
120*62c56f98SSadaf Ebrahimi * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
121*62c56f98SSadaf Ebrahimi * as applicable.
122*62c56f98SSadaf Ebrahimi *
123*62c56f98SSadaf Ebrahimi * Implementations shall not return this error code to indicate that a
124*62c56f98SSadaf Ebrahimi * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
125*62c56f98SSadaf Ebrahimi * instead. */
126*62c56f98SSadaf Ebrahimi #define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
127*62c56f98SSadaf Ebrahimi
128*62c56f98SSadaf Ebrahimi /** The parameters passed to the function are invalid.
129*62c56f98SSadaf Ebrahimi *
130*62c56f98SSadaf Ebrahimi * Implementations may return this error any time a parameter or
131*62c56f98SSadaf Ebrahimi * combination of parameters are recognized as invalid.
132*62c56f98SSadaf Ebrahimi *
133*62c56f98SSadaf Ebrahimi * Implementations shall not return this error code to indicate that a
134*62c56f98SSadaf Ebrahimi * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
135*62c56f98SSadaf Ebrahimi * instead.
136*62c56f98SSadaf Ebrahimi */
137*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
138*62c56f98SSadaf Ebrahimi
139*62c56f98SSadaf Ebrahimi /** There is not enough runtime memory.
140*62c56f98SSadaf Ebrahimi *
141*62c56f98SSadaf Ebrahimi * If the action is carried out across multiple security realms, this
142*62c56f98SSadaf Ebrahimi * error can refer to available memory in any of the security realms. */
143*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
144*62c56f98SSadaf Ebrahimi
145*62c56f98SSadaf Ebrahimi /** There is not enough persistent storage.
146*62c56f98SSadaf Ebrahimi *
147*62c56f98SSadaf Ebrahimi * Functions that modify the key storage return this error code if
148*62c56f98SSadaf Ebrahimi * there is insufficient storage space on the host media. In addition,
149*62c56f98SSadaf Ebrahimi * many functions that do not otherwise access storage may return this
150*62c56f98SSadaf Ebrahimi * error code if the implementation requires a mandatory log entry for
151*62c56f98SSadaf Ebrahimi * the requested action and the log storage space is full. */
152*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
153*62c56f98SSadaf Ebrahimi
154*62c56f98SSadaf Ebrahimi /** There was a communication failure inside the implementation.
155*62c56f98SSadaf Ebrahimi *
156*62c56f98SSadaf Ebrahimi * This can indicate a communication failure between the application
157*62c56f98SSadaf Ebrahimi * and an external cryptoprocessor or between the cryptoprocessor and
158*62c56f98SSadaf Ebrahimi * an external volatile or persistent memory. A communication failure
159*62c56f98SSadaf Ebrahimi * may be transient or permanent depending on the cause.
160*62c56f98SSadaf Ebrahimi *
161*62c56f98SSadaf Ebrahimi * \warning If a function returns this error, it is undetermined
162*62c56f98SSadaf Ebrahimi * whether the requested action has completed or not. Implementations
163*62c56f98SSadaf Ebrahimi * should return #PSA_SUCCESS on successful completion whenever
164*62c56f98SSadaf Ebrahimi * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
165*62c56f98SSadaf Ebrahimi * if the requested action was completed successfully in an external
166*62c56f98SSadaf Ebrahimi * cryptoprocessor but there was a breakdown of communication before
167*62c56f98SSadaf Ebrahimi * the cryptoprocessor could report the status to the application.
168*62c56f98SSadaf Ebrahimi */
169*62c56f98SSadaf Ebrahimi #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
170*62c56f98SSadaf Ebrahimi
171*62c56f98SSadaf Ebrahimi /** There was a storage failure that may have led to data loss.
172*62c56f98SSadaf Ebrahimi *
173*62c56f98SSadaf Ebrahimi * This error indicates that some persistent storage is corrupted.
174*62c56f98SSadaf Ebrahimi * It should not be used for a corruption of volatile memory
175*62c56f98SSadaf Ebrahimi * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
176*62c56f98SSadaf Ebrahimi * between the cryptoprocessor and its external storage (use
177*62c56f98SSadaf Ebrahimi * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
178*62c56f98SSadaf Ebrahimi * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
179*62c56f98SSadaf Ebrahimi *
180*62c56f98SSadaf Ebrahimi * Note that a storage failure does not indicate that any data that was
181*62c56f98SSadaf Ebrahimi * previously read is invalid. However this previously read data may no
182*62c56f98SSadaf Ebrahimi * longer be readable from storage.
183*62c56f98SSadaf Ebrahimi *
184*62c56f98SSadaf Ebrahimi * When a storage failure occurs, it is no longer possible to ensure
185*62c56f98SSadaf Ebrahimi * the global integrity of the keystore. Depending on the global
186*62c56f98SSadaf Ebrahimi * integrity guarantees offered by the implementation, access to other
187*62c56f98SSadaf Ebrahimi * data may or may not fail even if the data is still readable but
188*62c56f98SSadaf Ebrahimi * its integrity cannot be guaranteed.
189*62c56f98SSadaf Ebrahimi *
190*62c56f98SSadaf Ebrahimi * Implementations should only use this error code to report a
191*62c56f98SSadaf Ebrahimi * permanent storage corruption. However application writers should
192*62c56f98SSadaf Ebrahimi * keep in mind that transient errors while reading the storage may be
193*62c56f98SSadaf Ebrahimi * reported using this error code. */
194*62c56f98SSadaf Ebrahimi #define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
195*62c56f98SSadaf Ebrahimi
196*62c56f98SSadaf Ebrahimi /** A hardware failure was detected.
197*62c56f98SSadaf Ebrahimi *
198*62c56f98SSadaf Ebrahimi * A hardware failure may be transient or permanent depending on the
199*62c56f98SSadaf Ebrahimi * cause. */
200*62c56f98SSadaf Ebrahimi #define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
201*62c56f98SSadaf Ebrahimi
202*62c56f98SSadaf Ebrahimi /** A tampering attempt was detected.
203*62c56f98SSadaf Ebrahimi *
204*62c56f98SSadaf Ebrahimi * If an application receives this error code, there is no guarantee
205*62c56f98SSadaf Ebrahimi * that previously accessed or computed data was correct and remains
206*62c56f98SSadaf Ebrahimi * confidential. Applications should not perform any security function
207*62c56f98SSadaf Ebrahimi * and should enter a safe failure state.
208*62c56f98SSadaf Ebrahimi *
209*62c56f98SSadaf Ebrahimi * Implementations may return this error code if they detect an invalid
210*62c56f98SSadaf Ebrahimi * state that cannot happen during normal operation and that indicates
211*62c56f98SSadaf Ebrahimi * that the implementation's security guarantees no longer hold. Depending
212*62c56f98SSadaf Ebrahimi * on the implementation architecture and on its security and safety goals,
213*62c56f98SSadaf Ebrahimi * the implementation may forcibly terminate the application.
214*62c56f98SSadaf Ebrahimi *
215*62c56f98SSadaf Ebrahimi * This error code is intended as a last resort when a security breach
216*62c56f98SSadaf Ebrahimi * is detected and it is unsure whether the keystore data is still
217*62c56f98SSadaf Ebrahimi * protected. Implementations shall only return this error code
218*62c56f98SSadaf Ebrahimi * to report an alarm from a tampering detector, to indicate that
219*62c56f98SSadaf Ebrahimi * the confidentiality of stored data can no longer be guaranteed,
220*62c56f98SSadaf Ebrahimi * or to indicate that the integrity of previously returned data is now
221*62c56f98SSadaf Ebrahimi * considered compromised. Implementations shall not use this error code
222*62c56f98SSadaf Ebrahimi * to indicate a hardware failure that merely makes it impossible to
223*62c56f98SSadaf Ebrahimi * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
224*62c56f98SSadaf Ebrahimi * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
225*62c56f98SSadaf Ebrahimi * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
226*62c56f98SSadaf Ebrahimi * instead).
227*62c56f98SSadaf Ebrahimi *
228*62c56f98SSadaf Ebrahimi * This error indicates an attack against the application. Implementations
229*62c56f98SSadaf Ebrahimi * shall not return this error code as a consequence of the behavior of
230*62c56f98SSadaf Ebrahimi * the application itself. */
231*62c56f98SSadaf Ebrahimi #define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
232*62c56f98SSadaf Ebrahimi
233*62c56f98SSadaf Ebrahimi /** There is not enough entropy to generate random data needed
234*62c56f98SSadaf Ebrahimi * for the requested action.
235*62c56f98SSadaf Ebrahimi *
236*62c56f98SSadaf Ebrahimi * This error indicates a failure of a hardware random generator.
237*62c56f98SSadaf Ebrahimi * Application writers should note that this error can be returned not
238*62c56f98SSadaf Ebrahimi * only by functions whose purpose is to generate random data, such
239*62c56f98SSadaf Ebrahimi * as key, IV or nonce generation, but also by functions that execute
240*62c56f98SSadaf Ebrahimi * an algorithm with a randomized result, as well as functions that
241*62c56f98SSadaf Ebrahimi * use randomization of intermediate computations as a countermeasure
242*62c56f98SSadaf Ebrahimi * to certain attacks.
243*62c56f98SSadaf Ebrahimi *
244*62c56f98SSadaf Ebrahimi * Implementations should avoid returning this error after psa_crypto_init()
245*62c56f98SSadaf Ebrahimi * has succeeded. Implementations should generate sufficient
246*62c56f98SSadaf Ebrahimi * entropy during initialization and subsequently use a cryptographically
247*62c56f98SSadaf Ebrahimi * secure pseudorandom generator (PRNG). However implementations may return
248*62c56f98SSadaf Ebrahimi * this error at any time if a policy requires the PRNG to be reseeded
249*62c56f98SSadaf Ebrahimi * during normal operation. */
250*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
251*62c56f98SSadaf Ebrahimi
252*62c56f98SSadaf Ebrahimi /** The signature, MAC or hash is incorrect.
253*62c56f98SSadaf Ebrahimi *
254*62c56f98SSadaf Ebrahimi * Verification functions return this error if the verification
255*62c56f98SSadaf Ebrahimi * calculations completed successfully, and the value to be verified
256*62c56f98SSadaf Ebrahimi * was determined to be incorrect.
257*62c56f98SSadaf Ebrahimi *
258*62c56f98SSadaf Ebrahimi * If the value to verify has an invalid size, implementations may return
259*62c56f98SSadaf Ebrahimi * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
260*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
261*62c56f98SSadaf Ebrahimi
262*62c56f98SSadaf Ebrahimi /** The decrypted padding is incorrect.
263*62c56f98SSadaf Ebrahimi *
264*62c56f98SSadaf Ebrahimi * \warning In some protocols, when decrypting data, it is essential that
265*62c56f98SSadaf Ebrahimi * the behavior of the application does not depend on whether the padding
266*62c56f98SSadaf Ebrahimi * is correct, down to precise timing. Applications should prefer
267*62c56f98SSadaf Ebrahimi * protocols that use authenticated encryption rather than plain
268*62c56f98SSadaf Ebrahimi * encryption. If the application must perform a decryption of
269*62c56f98SSadaf Ebrahimi * unauthenticated data, the application writer should take care not
270*62c56f98SSadaf Ebrahimi * to reveal whether the padding is invalid.
271*62c56f98SSadaf Ebrahimi *
272*62c56f98SSadaf Ebrahimi * Implementations should strive to make valid and invalid padding
273*62c56f98SSadaf Ebrahimi * as close as possible to indistinguishable to an external observer.
274*62c56f98SSadaf Ebrahimi * In particular, the timing of a decryption operation should not
275*62c56f98SSadaf Ebrahimi * depend on the validity of the padding. */
276*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
277*62c56f98SSadaf Ebrahimi
278*62c56f98SSadaf Ebrahimi /** Return this error when there's insufficient data when attempting
279*62c56f98SSadaf Ebrahimi * to read from a resource. */
280*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
281*62c56f98SSadaf Ebrahimi
282*62c56f98SSadaf Ebrahimi /** The key identifier is not valid. See also :ref:\`key-handles\`.
283*62c56f98SSadaf Ebrahimi */
284*62c56f98SSadaf Ebrahimi #define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
285*62c56f98SSadaf Ebrahimi
286*62c56f98SSadaf Ebrahimi /** Stored data has been corrupted.
287*62c56f98SSadaf Ebrahimi *
288*62c56f98SSadaf Ebrahimi * This error indicates that some persistent storage has suffered corruption.
289*62c56f98SSadaf Ebrahimi * It does not indicate the following situations, which have specific error
290*62c56f98SSadaf Ebrahimi * codes:
291*62c56f98SSadaf Ebrahimi *
292*62c56f98SSadaf Ebrahimi * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
293*62c56f98SSadaf Ebrahimi * - A communication error between the cryptoprocessor and its external
294*62c56f98SSadaf Ebrahimi * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
295*62c56f98SSadaf Ebrahimi * - When the storage is in a valid state but is full - use
296*62c56f98SSadaf Ebrahimi * #PSA_ERROR_INSUFFICIENT_STORAGE.
297*62c56f98SSadaf Ebrahimi * - When the storage fails for other reasons - use
298*62c56f98SSadaf Ebrahimi * #PSA_ERROR_STORAGE_FAILURE.
299*62c56f98SSadaf Ebrahimi * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
300*62c56f98SSadaf Ebrahimi *
301*62c56f98SSadaf Ebrahimi * \note A storage corruption does not indicate that any data that was
302*62c56f98SSadaf Ebrahimi * previously read is invalid. However this previously read data might no
303*62c56f98SSadaf Ebrahimi * longer be readable from storage.
304*62c56f98SSadaf Ebrahimi *
305*62c56f98SSadaf Ebrahimi * When a storage failure occurs, it is no longer possible to ensure the
306*62c56f98SSadaf Ebrahimi * global integrity of the keystore.
307*62c56f98SSadaf Ebrahimi */
308*62c56f98SSadaf Ebrahimi #define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
309*62c56f98SSadaf Ebrahimi
310*62c56f98SSadaf Ebrahimi /** Data read from storage is not valid for the implementation.
311*62c56f98SSadaf Ebrahimi *
312*62c56f98SSadaf Ebrahimi * This error indicates that some data read from storage does not have a valid
313*62c56f98SSadaf Ebrahimi * format. It does not indicate the following situations, which have specific
314*62c56f98SSadaf Ebrahimi * error codes:
315*62c56f98SSadaf Ebrahimi *
316*62c56f98SSadaf Ebrahimi * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
317*62c56f98SSadaf Ebrahimi * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
318*62c56f98SSadaf Ebrahimi * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
319*62c56f98SSadaf Ebrahimi *
320*62c56f98SSadaf Ebrahimi * This error is typically a result of either storage corruption on a
321*62c56f98SSadaf Ebrahimi * cleartext storage backend, or an attempt to read data that was
322*62c56f98SSadaf Ebrahimi * written by an incompatible version of the library.
323*62c56f98SSadaf Ebrahimi */
324*62c56f98SSadaf Ebrahimi #define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
325*62c56f98SSadaf Ebrahimi
326*62c56f98SSadaf Ebrahimi /** The function that returns this status is defined as interruptible and
327*62c56f98SSadaf Ebrahimi * still has work to do, thus the user should call the function again with the
328*62c56f98SSadaf Ebrahimi * same operation context until it either returns #PSA_SUCCESS or any other
329*62c56f98SSadaf Ebrahimi * error. This is not an error per se, more a notification of status.
330*62c56f98SSadaf Ebrahimi */
331*62c56f98SSadaf Ebrahimi #define PSA_OPERATION_INCOMPLETE ((psa_status_t)-248)
332*62c56f98SSadaf Ebrahimi
333*62c56f98SSadaf Ebrahimi /* *INDENT-ON* */
334*62c56f98SSadaf Ebrahimi
335*62c56f98SSadaf Ebrahimi /**@}*/
336*62c56f98SSadaf Ebrahimi
337*62c56f98SSadaf Ebrahimi /** \defgroup crypto_types Key and algorithm types
338*62c56f98SSadaf Ebrahimi * @{
339*62c56f98SSadaf Ebrahimi */
340*62c56f98SSadaf Ebrahimi
341*62c56f98SSadaf Ebrahimi /* Note that key type values, including ECC family and DH group values, are
342*62c56f98SSadaf Ebrahimi * embedded in the persistent key store, as part of key metadata. As a
343*62c56f98SSadaf Ebrahimi * consequence, they must not be changed (unless the storage format version
344*62c56f98SSadaf Ebrahimi * changes).
345*62c56f98SSadaf Ebrahimi */
346*62c56f98SSadaf Ebrahimi
347*62c56f98SSadaf Ebrahimi /** An invalid key type value.
348*62c56f98SSadaf Ebrahimi *
349*62c56f98SSadaf Ebrahimi * Zero is not the encoding of any key type.
350*62c56f98SSadaf Ebrahimi */
351*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_NONE ((psa_key_type_t) 0x0000)
352*62c56f98SSadaf Ebrahimi
353*62c56f98SSadaf Ebrahimi /** Vendor-defined key type flag.
354*62c56f98SSadaf Ebrahimi *
355*62c56f98SSadaf Ebrahimi * Key types defined by this standard will never have the
356*62c56f98SSadaf Ebrahimi * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
357*62c56f98SSadaf Ebrahimi * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
358*62c56f98SSadaf Ebrahimi * respect the bitwise structure used by standard encodings whenever practical.
359*62c56f98SSadaf Ebrahimi */
360*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t) 0x8000)
361*62c56f98SSadaf Ebrahimi
362*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t) 0x7000)
363*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t) 0x1000)
364*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t) 0x2000)
365*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t) 0x4000)
366*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t) 0x7000)
367*62c56f98SSadaf Ebrahimi
368*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t) 0x3000)
369*62c56f98SSadaf Ebrahimi
370*62c56f98SSadaf Ebrahimi /** Whether a key type is vendor-defined.
371*62c56f98SSadaf Ebrahimi *
372*62c56f98SSadaf Ebrahimi * See also #PSA_KEY_TYPE_VENDOR_FLAG.
373*62c56f98SSadaf Ebrahimi */
374*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
375*62c56f98SSadaf Ebrahimi (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
376*62c56f98SSadaf Ebrahimi
377*62c56f98SSadaf Ebrahimi /** Whether a key type is an unstructured array of bytes.
378*62c56f98SSadaf Ebrahimi *
379*62c56f98SSadaf Ebrahimi * This encompasses both symmetric keys and non-key data.
380*62c56f98SSadaf Ebrahimi */
381*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
382*62c56f98SSadaf Ebrahimi (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
383*62c56f98SSadaf Ebrahimi ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
384*62c56f98SSadaf Ebrahimi
385*62c56f98SSadaf Ebrahimi /** Whether a key type is asymmetric: either a key pair or a public key. */
386*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
387*62c56f98SSadaf Ebrahimi (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
388*62c56f98SSadaf Ebrahimi & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
389*62c56f98SSadaf Ebrahimi PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
390*62c56f98SSadaf Ebrahimi /** Whether a key type is the public part of a key pair. */
391*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
392*62c56f98SSadaf Ebrahimi (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
393*62c56f98SSadaf Ebrahimi /** Whether a key type is a key pair containing a private part and a public
394*62c56f98SSadaf Ebrahimi * part. */
395*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
396*62c56f98SSadaf Ebrahimi (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
397*62c56f98SSadaf Ebrahimi /** The key pair type corresponding to a public key type.
398*62c56f98SSadaf Ebrahimi *
399*62c56f98SSadaf Ebrahimi * You may also pass a key pair type as \p type, it will be left unchanged.
400*62c56f98SSadaf Ebrahimi *
401*62c56f98SSadaf Ebrahimi * \param type A public key type or key pair type.
402*62c56f98SSadaf Ebrahimi *
403*62c56f98SSadaf Ebrahimi * \return The corresponding key pair type.
404*62c56f98SSadaf Ebrahimi * If \p type is not a public key or a key pair,
405*62c56f98SSadaf Ebrahimi * the return value is undefined.
406*62c56f98SSadaf Ebrahimi */
407*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
408*62c56f98SSadaf Ebrahimi ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
409*62c56f98SSadaf Ebrahimi /** The public key type corresponding to a key pair type.
410*62c56f98SSadaf Ebrahimi *
411*62c56f98SSadaf Ebrahimi * You may also pass a key pair type as \p type, it will be left unchanged.
412*62c56f98SSadaf Ebrahimi *
413*62c56f98SSadaf Ebrahimi * \param type A public key type or key pair type.
414*62c56f98SSadaf Ebrahimi *
415*62c56f98SSadaf Ebrahimi * \return The corresponding public key type.
416*62c56f98SSadaf Ebrahimi * If \p type is not a public key or a key pair,
417*62c56f98SSadaf Ebrahimi * the return value is undefined.
418*62c56f98SSadaf Ebrahimi */
419*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
420*62c56f98SSadaf Ebrahimi ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
421*62c56f98SSadaf Ebrahimi
422*62c56f98SSadaf Ebrahimi /** Raw data.
423*62c56f98SSadaf Ebrahimi *
424*62c56f98SSadaf Ebrahimi * A "key" of this type cannot be used for any cryptographic operation.
425*62c56f98SSadaf Ebrahimi * Applications may use this type to store arbitrary data in the keystore. */
426*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t) 0x1001)
427*62c56f98SSadaf Ebrahimi
428*62c56f98SSadaf Ebrahimi /** HMAC key.
429*62c56f98SSadaf Ebrahimi *
430*62c56f98SSadaf Ebrahimi * The key policy determines which underlying hash algorithm the key can be
431*62c56f98SSadaf Ebrahimi * used for.
432*62c56f98SSadaf Ebrahimi *
433*62c56f98SSadaf Ebrahimi * HMAC keys should generally have the same size as the underlying hash.
434*62c56f98SSadaf Ebrahimi * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
435*62c56f98SSadaf Ebrahimi * \c alg is the HMAC algorithm or the underlying hash algorithm. */
436*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_HMAC ((psa_key_type_t) 0x1100)
437*62c56f98SSadaf Ebrahimi
438*62c56f98SSadaf Ebrahimi /** A secret for key derivation.
439*62c56f98SSadaf Ebrahimi *
440*62c56f98SSadaf Ebrahimi * This key type is for high-entropy secrets only. For low-entropy secrets,
441*62c56f98SSadaf Ebrahimi * #PSA_KEY_TYPE_PASSWORD should be used instead.
442*62c56f98SSadaf Ebrahimi *
443*62c56f98SSadaf Ebrahimi * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
444*62c56f98SSadaf Ebrahimi * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
445*62c56f98SSadaf Ebrahimi *
446*62c56f98SSadaf Ebrahimi * The key policy determines which key derivation algorithm the key
447*62c56f98SSadaf Ebrahimi * can be used for.
448*62c56f98SSadaf Ebrahimi */
449*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DERIVE ((psa_key_type_t) 0x1200)
450*62c56f98SSadaf Ebrahimi
451*62c56f98SSadaf Ebrahimi /** A low-entropy secret for password hashing or key derivation.
452*62c56f98SSadaf Ebrahimi *
453*62c56f98SSadaf Ebrahimi * This key type is suitable for passwords and passphrases which are typically
454*62c56f98SSadaf Ebrahimi * intended to be memorizable by humans, and have a low entropy relative to
455*62c56f98SSadaf Ebrahimi * their size. It can be used for randomly generated or derived keys with
456*62c56f98SSadaf Ebrahimi * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
457*62c56f98SSadaf Ebrahimi * for such keys. It is not suitable for passwords with extremely low entropy,
458*62c56f98SSadaf Ebrahimi * such as numerical PINs.
459*62c56f98SSadaf Ebrahimi *
460*62c56f98SSadaf Ebrahimi * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
461*62c56f98SSadaf Ebrahimi * key derivation algorithms. Algorithms that accept such an input were
462*62c56f98SSadaf Ebrahimi * designed to accept low-entropy secret and are known as password hashing or
463*62c56f98SSadaf Ebrahimi * key stretching algorithms.
464*62c56f98SSadaf Ebrahimi *
465*62c56f98SSadaf Ebrahimi * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
466*62c56f98SSadaf Ebrahimi * key derivation algorithms, as the algorithms that take such an input expect
467*62c56f98SSadaf Ebrahimi * it to be high-entropy.
468*62c56f98SSadaf Ebrahimi *
469*62c56f98SSadaf Ebrahimi * The key policy determines which key derivation algorithm the key can be
470*62c56f98SSadaf Ebrahimi * used for, among the permissible subset defined above.
471*62c56f98SSadaf Ebrahimi */
472*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t) 0x1203)
473*62c56f98SSadaf Ebrahimi
474*62c56f98SSadaf Ebrahimi /** A secret value that can be used to verify a password hash.
475*62c56f98SSadaf Ebrahimi *
476*62c56f98SSadaf Ebrahimi * The key policy determines which key derivation algorithm the key
477*62c56f98SSadaf Ebrahimi * can be used for, among the same permissible subset as for
478*62c56f98SSadaf Ebrahimi * #PSA_KEY_TYPE_PASSWORD.
479*62c56f98SSadaf Ebrahimi */
480*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t) 0x1205)
481*62c56f98SSadaf Ebrahimi
482*62c56f98SSadaf Ebrahimi /** A secret value that can be used in when computing a password hash.
483*62c56f98SSadaf Ebrahimi *
484*62c56f98SSadaf Ebrahimi * The key policy determines which key derivation algorithm the key
485*62c56f98SSadaf Ebrahimi * can be used for, among the subset of algorithms that can use pepper.
486*62c56f98SSadaf Ebrahimi */
487*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_PEPPER ((psa_key_type_t) 0x1206)
488*62c56f98SSadaf Ebrahimi
489*62c56f98SSadaf Ebrahimi /** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
490*62c56f98SSadaf Ebrahimi *
491*62c56f98SSadaf Ebrahimi * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
492*62c56f98SSadaf Ebrahimi * 32 bytes (AES-256).
493*62c56f98SSadaf Ebrahimi */
494*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_AES ((psa_key_type_t) 0x2400)
495*62c56f98SSadaf Ebrahimi
496*62c56f98SSadaf Ebrahimi /** Key for a cipher, AEAD or MAC algorithm based on the
497*62c56f98SSadaf Ebrahimi * ARIA block cipher. */
498*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ARIA ((psa_key_type_t) 0x2406)
499*62c56f98SSadaf Ebrahimi
500*62c56f98SSadaf Ebrahimi /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
501*62c56f98SSadaf Ebrahimi *
502*62c56f98SSadaf Ebrahimi * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
503*62c56f98SSadaf Ebrahimi * 192 bits (3-key 3DES).
504*62c56f98SSadaf Ebrahimi *
505*62c56f98SSadaf Ebrahimi * Note that single DES and 2-key 3DES are weak and strongly
506*62c56f98SSadaf Ebrahimi * deprecated and should only be used to decrypt legacy data. 3-key 3DES
507*62c56f98SSadaf Ebrahimi * is weak and deprecated and should only be used in legacy protocols.
508*62c56f98SSadaf Ebrahimi */
509*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DES ((psa_key_type_t) 0x2301)
510*62c56f98SSadaf Ebrahimi
511*62c56f98SSadaf Ebrahimi /** Key for a cipher, AEAD or MAC algorithm based on the
512*62c56f98SSadaf Ebrahimi * Camellia block cipher. */
513*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t) 0x2403)
514*62c56f98SSadaf Ebrahimi
515*62c56f98SSadaf Ebrahimi /** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
516*62c56f98SSadaf Ebrahimi *
517*62c56f98SSadaf Ebrahimi * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
518*62c56f98SSadaf Ebrahimi *
519*62c56f98SSadaf Ebrahimi * \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports
520*62c56f98SSadaf Ebrahimi * 12-byte nonces.
521*62c56f98SSadaf Ebrahimi *
522*62c56f98SSadaf Ebrahimi * \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt
523*62c56f98SSadaf Ebrahimi * with the initial counter value 1, you can process and discard a
524*62c56f98SSadaf Ebrahimi * 64-byte block before the real data.
525*62c56f98SSadaf Ebrahimi */
526*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t) 0x2004)
527*62c56f98SSadaf Ebrahimi
528*62c56f98SSadaf Ebrahimi /** RSA public key.
529*62c56f98SSadaf Ebrahimi *
530*62c56f98SSadaf Ebrahimi * The size of an RSA key is the bit size of the modulus.
531*62c56f98SSadaf Ebrahimi */
532*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t) 0x4001)
533*62c56f98SSadaf Ebrahimi /** RSA key pair (private and public key).
534*62c56f98SSadaf Ebrahimi *
535*62c56f98SSadaf Ebrahimi * The size of an RSA key is the bit size of the modulus.
536*62c56f98SSadaf Ebrahimi */
537*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t) 0x7001)
538*62c56f98SSadaf Ebrahimi /** Whether a key type is an RSA key (pair or public-only). */
539*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_RSA(type) \
540*62c56f98SSadaf Ebrahimi (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
541*62c56f98SSadaf Ebrahimi
542*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4100)
543*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t) 0x7100)
544*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t) 0x00ff)
545*62c56f98SSadaf Ebrahimi /** Elliptic curve key pair.
546*62c56f98SSadaf Ebrahimi *
547*62c56f98SSadaf Ebrahimi * The size of an elliptic curve key is the bit size associated with the curve,
548*62c56f98SSadaf Ebrahimi * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
549*62c56f98SSadaf Ebrahimi * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
550*62c56f98SSadaf Ebrahimi *
551*62c56f98SSadaf Ebrahimi * \param curve A value of type ::psa_ecc_family_t that
552*62c56f98SSadaf Ebrahimi * identifies the ECC curve to be used.
553*62c56f98SSadaf Ebrahimi */
554*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
555*62c56f98SSadaf Ebrahimi (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
556*62c56f98SSadaf Ebrahimi /** Elliptic curve public key.
557*62c56f98SSadaf Ebrahimi *
558*62c56f98SSadaf Ebrahimi * The size of an elliptic curve public key is the same as the corresponding
559*62c56f98SSadaf Ebrahimi * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
560*62c56f98SSadaf Ebrahimi * `PSA_ECC_FAMILY_xxx` curve families).
561*62c56f98SSadaf Ebrahimi *
562*62c56f98SSadaf Ebrahimi * \param curve A value of type ::psa_ecc_family_t that
563*62c56f98SSadaf Ebrahimi * identifies the ECC curve to be used.
564*62c56f98SSadaf Ebrahimi */
565*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
566*62c56f98SSadaf Ebrahimi (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
567*62c56f98SSadaf Ebrahimi
568*62c56f98SSadaf Ebrahimi /** Whether a key type is an elliptic curve key (pair or public-only). */
569*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_ECC(type) \
570*62c56f98SSadaf Ebrahimi ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
571*62c56f98SSadaf Ebrahimi ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
572*62c56f98SSadaf Ebrahimi /** Whether a key type is an elliptic curve key pair. */
573*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
574*62c56f98SSadaf Ebrahimi (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
575*62c56f98SSadaf Ebrahimi PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
576*62c56f98SSadaf Ebrahimi /** Whether a key type is an elliptic curve public key. */
577*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
578*62c56f98SSadaf Ebrahimi (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
579*62c56f98SSadaf Ebrahimi PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
580*62c56f98SSadaf Ebrahimi
581*62c56f98SSadaf Ebrahimi /** Extract the curve from an elliptic curve key type. */
582*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
583*62c56f98SSadaf Ebrahimi ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
584*62c56f98SSadaf Ebrahimi ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
585*62c56f98SSadaf Ebrahimi 0))
586*62c56f98SSadaf Ebrahimi
587*62c56f98SSadaf Ebrahimi /** Check if the curve of given family is Weierstrass elliptic curve. */
588*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
589*62c56f98SSadaf Ebrahimi
590*62c56f98SSadaf Ebrahimi /** SEC Koblitz curves over prime fields.
591*62c56f98SSadaf Ebrahimi *
592*62c56f98SSadaf Ebrahimi * This family comprises the following curves:
593*62c56f98SSadaf Ebrahimi * secp192k1, secp224k1, secp256k1.
594*62c56f98SSadaf Ebrahimi * They are defined in _Standards for Efficient Cryptography_,
595*62c56f98SSadaf Ebrahimi * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
596*62c56f98SSadaf Ebrahimi * https://www.secg.org/sec2-v2.pdf
597*62c56f98SSadaf Ebrahimi */
598*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
599*62c56f98SSadaf Ebrahimi
600*62c56f98SSadaf Ebrahimi /** SEC random curves over prime fields.
601*62c56f98SSadaf Ebrahimi *
602*62c56f98SSadaf Ebrahimi * This family comprises the following curves:
603*62c56f98SSadaf Ebrahimi * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
604*62c56f98SSadaf Ebrahimi * They are defined in _Standards for Efficient Cryptography_,
605*62c56f98SSadaf Ebrahimi * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
606*62c56f98SSadaf Ebrahimi * https://www.secg.org/sec2-v2.pdf
607*62c56f98SSadaf Ebrahimi */
608*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
609*62c56f98SSadaf Ebrahimi /* SECP160R2 (SEC2 v1, obsolete) */
610*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
611*62c56f98SSadaf Ebrahimi
612*62c56f98SSadaf Ebrahimi /** SEC Koblitz curves over binary fields.
613*62c56f98SSadaf Ebrahimi *
614*62c56f98SSadaf Ebrahimi * This family comprises the following curves:
615*62c56f98SSadaf Ebrahimi * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
616*62c56f98SSadaf Ebrahimi * They are defined in _Standards for Efficient Cryptography_,
617*62c56f98SSadaf Ebrahimi * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
618*62c56f98SSadaf Ebrahimi * https://www.secg.org/sec2-v2.pdf
619*62c56f98SSadaf Ebrahimi */
620*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
621*62c56f98SSadaf Ebrahimi
622*62c56f98SSadaf Ebrahimi /** SEC random curves over binary fields.
623*62c56f98SSadaf Ebrahimi *
624*62c56f98SSadaf Ebrahimi * This family comprises the following curves:
625*62c56f98SSadaf Ebrahimi * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
626*62c56f98SSadaf Ebrahimi * They are defined in _Standards for Efficient Cryptography_,
627*62c56f98SSadaf Ebrahimi * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
628*62c56f98SSadaf Ebrahimi * https://www.secg.org/sec2-v2.pdf
629*62c56f98SSadaf Ebrahimi */
630*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
631*62c56f98SSadaf Ebrahimi
632*62c56f98SSadaf Ebrahimi /** SEC additional random curves over binary fields.
633*62c56f98SSadaf Ebrahimi *
634*62c56f98SSadaf Ebrahimi * This family comprises the following curve:
635*62c56f98SSadaf Ebrahimi * sect163r2.
636*62c56f98SSadaf Ebrahimi * It is defined in _Standards for Efficient Cryptography_,
637*62c56f98SSadaf Ebrahimi * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
638*62c56f98SSadaf Ebrahimi * https://www.secg.org/sec2-v2.pdf
639*62c56f98SSadaf Ebrahimi */
640*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
641*62c56f98SSadaf Ebrahimi
642*62c56f98SSadaf Ebrahimi /** Brainpool P random curves.
643*62c56f98SSadaf Ebrahimi *
644*62c56f98SSadaf Ebrahimi * This family comprises the following curves:
645*62c56f98SSadaf Ebrahimi * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
646*62c56f98SSadaf Ebrahimi * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
647*62c56f98SSadaf Ebrahimi * It is defined in RFC 5639.
648*62c56f98SSadaf Ebrahimi */
649*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
650*62c56f98SSadaf Ebrahimi
651*62c56f98SSadaf Ebrahimi /** Curve25519 and Curve448.
652*62c56f98SSadaf Ebrahimi *
653*62c56f98SSadaf Ebrahimi * This family comprises the following Montgomery curves:
654*62c56f98SSadaf Ebrahimi * - 255-bit: Bernstein et al.,
655*62c56f98SSadaf Ebrahimi * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
656*62c56f98SSadaf Ebrahimi * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
657*62c56f98SSadaf Ebrahimi * - 448-bit: Hamburg,
658*62c56f98SSadaf Ebrahimi * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
659*62c56f98SSadaf Ebrahimi * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
660*62c56f98SSadaf Ebrahimi */
661*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
662*62c56f98SSadaf Ebrahimi
663*62c56f98SSadaf Ebrahimi /** The twisted Edwards curves Ed25519 and Ed448.
664*62c56f98SSadaf Ebrahimi *
665*62c56f98SSadaf Ebrahimi * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
666*62c56f98SSadaf Ebrahimi * #PSA_ALG_ED25519PH for the 255-bit curve,
667*62c56f98SSadaf Ebrahimi * #PSA_ALG_ED448PH for the 448-bit curve).
668*62c56f98SSadaf Ebrahimi *
669*62c56f98SSadaf Ebrahimi * This family comprises the following twisted Edwards curves:
670*62c56f98SSadaf Ebrahimi * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
671*62c56f98SSadaf Ebrahimi * to Curve25519.
672*62c56f98SSadaf Ebrahimi * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
673*62c56f98SSadaf Ebrahimi * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
674*62c56f98SSadaf Ebrahimi * to Curve448.
675*62c56f98SSadaf Ebrahimi * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
676*62c56f98SSadaf Ebrahimi */
677*62c56f98SSadaf Ebrahimi #define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
678*62c56f98SSadaf Ebrahimi
679*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4200)
680*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t) 0x7200)
681*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t) 0x00ff)
682*62c56f98SSadaf Ebrahimi /** Diffie-Hellman key pair.
683*62c56f98SSadaf Ebrahimi *
684*62c56f98SSadaf Ebrahimi * \param group A value of type ::psa_dh_family_t that identifies the
685*62c56f98SSadaf Ebrahimi * Diffie-Hellman group to be used.
686*62c56f98SSadaf Ebrahimi */
687*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
688*62c56f98SSadaf Ebrahimi (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
689*62c56f98SSadaf Ebrahimi /** Diffie-Hellman public key.
690*62c56f98SSadaf Ebrahimi *
691*62c56f98SSadaf Ebrahimi * \param group A value of type ::psa_dh_family_t that identifies the
692*62c56f98SSadaf Ebrahimi * Diffie-Hellman group to be used.
693*62c56f98SSadaf Ebrahimi */
694*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
695*62c56f98SSadaf Ebrahimi (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
696*62c56f98SSadaf Ebrahimi
697*62c56f98SSadaf Ebrahimi /** Whether a key type is a Diffie-Hellman key (pair or public-only). */
698*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_DH(type) \
699*62c56f98SSadaf Ebrahimi ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
700*62c56f98SSadaf Ebrahimi ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
701*62c56f98SSadaf Ebrahimi /** Whether a key type is a Diffie-Hellman key pair. */
702*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
703*62c56f98SSadaf Ebrahimi (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
704*62c56f98SSadaf Ebrahimi PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
705*62c56f98SSadaf Ebrahimi /** Whether a key type is a Diffie-Hellman public key. */
706*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
707*62c56f98SSadaf Ebrahimi (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
708*62c56f98SSadaf Ebrahimi PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
709*62c56f98SSadaf Ebrahimi
710*62c56f98SSadaf Ebrahimi /** Extract the group from a Diffie-Hellman key type. */
711*62c56f98SSadaf Ebrahimi #define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
712*62c56f98SSadaf Ebrahimi ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
713*62c56f98SSadaf Ebrahimi ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
714*62c56f98SSadaf Ebrahimi 0))
715*62c56f98SSadaf Ebrahimi
716*62c56f98SSadaf Ebrahimi /** Diffie-Hellman groups defined in RFC 7919 Appendix A.
717*62c56f98SSadaf Ebrahimi *
718*62c56f98SSadaf Ebrahimi * This family includes groups with the following key sizes (in bits):
719*62c56f98SSadaf Ebrahimi * 2048, 3072, 4096, 6144, 8192. A given implementation may support
720*62c56f98SSadaf Ebrahimi * all of these sizes or only a subset.
721*62c56f98SSadaf Ebrahimi */
722*62c56f98SSadaf Ebrahimi #define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
723*62c56f98SSadaf Ebrahimi
724*62c56f98SSadaf Ebrahimi #define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
725*62c56f98SSadaf Ebrahimi (((type) >> 8) & 7)
726*62c56f98SSadaf Ebrahimi /** The block size of a block cipher.
727*62c56f98SSadaf Ebrahimi *
728*62c56f98SSadaf Ebrahimi * \param type A cipher key type (value of type #psa_key_type_t).
729*62c56f98SSadaf Ebrahimi *
730*62c56f98SSadaf Ebrahimi * \return The block size for a block cipher, or 1 for a stream cipher.
731*62c56f98SSadaf Ebrahimi * The return value is undefined if \p type is not a supported
732*62c56f98SSadaf Ebrahimi * cipher key type.
733*62c56f98SSadaf Ebrahimi *
734*62c56f98SSadaf Ebrahimi * \note It is possible to build stream cipher algorithms on top of a block
735*62c56f98SSadaf Ebrahimi * cipher, for example CTR mode (#PSA_ALG_CTR).
736*62c56f98SSadaf Ebrahimi * This macro only takes the key type into account, so it cannot be
737*62c56f98SSadaf Ebrahimi * used to determine the size of the data that #psa_cipher_update()
738*62c56f98SSadaf Ebrahimi * might buffer for future processing in general.
739*62c56f98SSadaf Ebrahimi *
740*62c56f98SSadaf Ebrahimi * \note This macro returns a compile-time constant if its argument is one.
741*62c56f98SSadaf Ebrahimi *
742*62c56f98SSadaf Ebrahimi * \warning This macro may evaluate its argument multiple times.
743*62c56f98SSadaf Ebrahimi */
744*62c56f98SSadaf Ebrahimi #define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
745*62c56f98SSadaf Ebrahimi (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
746*62c56f98SSadaf Ebrahimi 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
747*62c56f98SSadaf Ebrahimi 0u)
748*62c56f98SSadaf Ebrahimi
749*62c56f98SSadaf Ebrahimi /* Note that algorithm values are embedded in the persistent key store,
750*62c56f98SSadaf Ebrahimi * as part of key metadata. As a consequence, they must not be changed
751*62c56f98SSadaf Ebrahimi * (unless the storage format version changes).
752*62c56f98SSadaf Ebrahimi */
753*62c56f98SSadaf Ebrahimi
754*62c56f98SSadaf Ebrahimi /** Vendor-defined algorithm flag.
755*62c56f98SSadaf Ebrahimi *
756*62c56f98SSadaf Ebrahimi * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
757*62c56f98SSadaf Ebrahimi * bit set. Vendors who define additional algorithms must use an encoding with
758*62c56f98SSadaf Ebrahimi * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
759*62c56f98SSadaf Ebrahimi * used by standard encodings whenever practical.
760*62c56f98SSadaf Ebrahimi */
761*62c56f98SSadaf Ebrahimi #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t) 0x80000000)
762*62c56f98SSadaf Ebrahimi
763*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t) 0x7f000000)
764*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t) 0x02000000)
765*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t) 0x03000000)
766*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t) 0x04000000)
767*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t) 0x05000000)
768*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t) 0x06000000)
769*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t) 0x07000000)
770*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t) 0x08000000)
771*62c56f98SSadaf Ebrahimi #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t) 0x09000000)
772*62c56f98SSadaf Ebrahimi
773*62c56f98SSadaf Ebrahimi /** Whether an algorithm is vendor-defined.
774*62c56f98SSadaf Ebrahimi *
775*62c56f98SSadaf Ebrahimi * See also #PSA_ALG_VENDOR_FLAG.
776*62c56f98SSadaf Ebrahimi */
777*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_VENDOR_DEFINED(alg) \
778*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
779*62c56f98SSadaf Ebrahimi
780*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a hash algorithm.
781*62c56f98SSadaf Ebrahimi *
782*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
783*62c56f98SSadaf Ebrahimi *
784*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a hash algorithm, 0 otherwise.
785*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
786*62c56f98SSadaf Ebrahimi * algorithm identifier.
787*62c56f98SSadaf Ebrahimi */
788*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HASH(alg) \
789*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
790*62c56f98SSadaf Ebrahimi
791*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a MAC algorithm.
792*62c56f98SSadaf Ebrahimi *
793*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
794*62c56f98SSadaf Ebrahimi *
795*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
796*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
797*62c56f98SSadaf Ebrahimi * algorithm identifier.
798*62c56f98SSadaf Ebrahimi */
799*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_MAC(alg) \
800*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
801*62c56f98SSadaf Ebrahimi
802*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a symmetric cipher algorithm.
803*62c56f98SSadaf Ebrahimi *
804*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
805*62c56f98SSadaf Ebrahimi *
806*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
807*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
808*62c56f98SSadaf Ebrahimi * algorithm identifier.
809*62c56f98SSadaf Ebrahimi */
810*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_CIPHER(alg) \
811*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
812*62c56f98SSadaf Ebrahimi
813*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an authenticated encryption
814*62c56f98SSadaf Ebrahimi * with associated data (AEAD) algorithm.
815*62c56f98SSadaf Ebrahimi *
816*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
817*62c56f98SSadaf Ebrahimi *
818*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
819*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
820*62c56f98SSadaf Ebrahimi * algorithm identifier.
821*62c56f98SSadaf Ebrahimi */
822*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_AEAD(alg) \
823*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
824*62c56f98SSadaf Ebrahimi
825*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an asymmetric signature algorithm,
826*62c56f98SSadaf Ebrahimi * also known as public-key signature algorithm.
827*62c56f98SSadaf Ebrahimi *
828*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
829*62c56f98SSadaf Ebrahimi *
830*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
831*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
832*62c56f98SSadaf Ebrahimi * algorithm identifier.
833*62c56f98SSadaf Ebrahimi */
834*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_SIGN(alg) \
835*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
836*62c56f98SSadaf Ebrahimi
837*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an asymmetric encryption algorithm,
838*62c56f98SSadaf Ebrahimi * also known as public-key encryption algorithm.
839*62c56f98SSadaf Ebrahimi *
840*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
841*62c56f98SSadaf Ebrahimi *
842*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
843*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
844*62c56f98SSadaf Ebrahimi * algorithm identifier.
845*62c56f98SSadaf Ebrahimi */
846*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
847*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
848*62c56f98SSadaf Ebrahimi
849*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a key agreement algorithm.
850*62c56f98SSadaf Ebrahimi *
851*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
852*62c56f98SSadaf Ebrahimi *
853*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
854*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
855*62c56f98SSadaf Ebrahimi * algorithm identifier.
856*62c56f98SSadaf Ebrahimi */
857*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_KEY_AGREEMENT(alg) \
858*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
859*62c56f98SSadaf Ebrahimi
860*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a key derivation algorithm.
861*62c56f98SSadaf Ebrahimi *
862*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
863*62c56f98SSadaf Ebrahimi *
864*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
865*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
866*62c56f98SSadaf Ebrahimi * algorithm identifier.
867*62c56f98SSadaf Ebrahimi */
868*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_KEY_DERIVATION(alg) \
869*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
870*62c56f98SSadaf Ebrahimi
871*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a key stretching / password hashing
872*62c56f98SSadaf Ebrahimi * algorithm.
873*62c56f98SSadaf Ebrahimi *
874*62c56f98SSadaf Ebrahimi * A key stretching / password hashing algorithm is a key derivation algorithm
875*62c56f98SSadaf Ebrahimi * that is suitable for use with a low-entropy secret such as a password.
876*62c56f98SSadaf Ebrahimi * Equivalently, it's a key derivation algorithm that uses a
877*62c56f98SSadaf Ebrahimi * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
878*62c56f98SSadaf Ebrahimi *
879*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
880*62c56f98SSadaf Ebrahimi *
881*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
882*62c56f98SSadaf Ebrahimi * otherwise. This macro may return either 0 or 1 if \p alg is not a
883*62c56f98SSadaf Ebrahimi * supported algorithm identifier.
884*62c56f98SSadaf Ebrahimi */
885*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
886*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_KEY_DERIVATION(alg) && \
887*62c56f98SSadaf Ebrahimi (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
888*62c56f98SSadaf Ebrahimi
889*62c56f98SSadaf Ebrahimi /** An invalid algorithm identifier value. */
890*62c56f98SSadaf Ebrahimi /* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
891*62c56f98SSadaf Ebrahimi #define PSA_ALG_NONE ((psa_algorithm_t)0)
892*62c56f98SSadaf Ebrahimi /* *INDENT-ON* */
893*62c56f98SSadaf Ebrahimi
894*62c56f98SSadaf Ebrahimi #define PSA_ALG_HASH_MASK ((psa_algorithm_t) 0x000000ff)
895*62c56f98SSadaf Ebrahimi /** MD5 */
896*62c56f98SSadaf Ebrahimi #define PSA_ALG_MD5 ((psa_algorithm_t) 0x02000003)
897*62c56f98SSadaf Ebrahimi /** PSA_ALG_RIPEMD160 */
898*62c56f98SSadaf Ebrahimi #define PSA_ALG_RIPEMD160 ((psa_algorithm_t) 0x02000004)
899*62c56f98SSadaf Ebrahimi /** SHA1 */
900*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_1 ((psa_algorithm_t) 0x02000005)
901*62c56f98SSadaf Ebrahimi /** SHA2-224 */
902*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_224 ((psa_algorithm_t) 0x02000008)
903*62c56f98SSadaf Ebrahimi /** SHA2-256 */
904*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_256 ((psa_algorithm_t) 0x02000009)
905*62c56f98SSadaf Ebrahimi /** SHA2-384 */
906*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_384 ((psa_algorithm_t) 0x0200000a)
907*62c56f98SSadaf Ebrahimi /** SHA2-512 */
908*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_512 ((psa_algorithm_t) 0x0200000b)
909*62c56f98SSadaf Ebrahimi /** SHA2-512/224 */
910*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_512_224 ((psa_algorithm_t) 0x0200000c)
911*62c56f98SSadaf Ebrahimi /** SHA2-512/256 */
912*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA_512_256 ((psa_algorithm_t) 0x0200000d)
913*62c56f98SSadaf Ebrahimi /** SHA3-224 */
914*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA3_224 ((psa_algorithm_t) 0x02000010)
915*62c56f98SSadaf Ebrahimi /** SHA3-256 */
916*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA3_256 ((psa_algorithm_t) 0x02000011)
917*62c56f98SSadaf Ebrahimi /** SHA3-384 */
918*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA3_384 ((psa_algorithm_t) 0x02000012)
919*62c56f98SSadaf Ebrahimi /** SHA3-512 */
920*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHA3_512 ((psa_algorithm_t) 0x02000013)
921*62c56f98SSadaf Ebrahimi /** The first 512 bits (64 bytes) of the SHAKE256 output.
922*62c56f98SSadaf Ebrahimi *
923*62c56f98SSadaf Ebrahimi * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
924*62c56f98SSadaf Ebrahimi * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
925*62c56f98SSadaf Ebrahimi * has the same output size and a (theoretically) higher security strength.
926*62c56f98SSadaf Ebrahimi */
927*62c56f98SSadaf Ebrahimi #define PSA_ALG_SHAKE256_512 ((psa_algorithm_t) 0x02000015)
928*62c56f98SSadaf Ebrahimi
929*62c56f98SSadaf Ebrahimi /** In a hash-and-sign algorithm policy, allow any hash algorithm.
930*62c56f98SSadaf Ebrahimi *
931*62c56f98SSadaf Ebrahimi * This value may be used to form the algorithm usage field of a policy
932*62c56f98SSadaf Ebrahimi * for a signature algorithm that is parametrized by a hash. The key
933*62c56f98SSadaf Ebrahimi * may then be used to perform operations using the same signature
934*62c56f98SSadaf Ebrahimi * algorithm parametrized with any supported hash.
935*62c56f98SSadaf Ebrahimi *
936*62c56f98SSadaf Ebrahimi * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
937*62c56f98SSadaf Ebrahimi * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
938*62c56f98SSadaf Ebrahimi * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
939*62c56f98SSadaf Ebrahimi * Then you may create and use a key as follows:
940*62c56f98SSadaf Ebrahimi * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
941*62c56f98SSadaf Ebrahimi * ```
942*62c56f98SSadaf Ebrahimi * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
943*62c56f98SSadaf Ebrahimi * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
944*62c56f98SSadaf Ebrahimi * ```
945*62c56f98SSadaf Ebrahimi * - Import or generate key material.
946*62c56f98SSadaf Ebrahimi * - Call psa_sign_hash() or psa_verify_hash(), passing
947*62c56f98SSadaf Ebrahimi * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
948*62c56f98SSadaf Ebrahimi * call to sign or verify a message may use a different hash.
949*62c56f98SSadaf Ebrahimi * ```
950*62c56f98SSadaf Ebrahimi * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
951*62c56f98SSadaf Ebrahimi * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
952*62c56f98SSadaf Ebrahimi * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
953*62c56f98SSadaf Ebrahimi * ```
954*62c56f98SSadaf Ebrahimi *
955*62c56f98SSadaf Ebrahimi * This value may not be used to build other algorithms that are
956*62c56f98SSadaf Ebrahimi * parametrized over a hash. For any valid use of this macro to build
957*62c56f98SSadaf Ebrahimi * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
958*62c56f98SSadaf Ebrahimi *
959*62c56f98SSadaf Ebrahimi * This value may not be used to build an algorithm specification to
960*62c56f98SSadaf Ebrahimi * perform an operation. It is only valid to build policies.
961*62c56f98SSadaf Ebrahimi */
962*62c56f98SSadaf Ebrahimi #define PSA_ALG_ANY_HASH ((psa_algorithm_t) 0x020000ff)
963*62c56f98SSadaf Ebrahimi
964*62c56f98SSadaf Ebrahimi #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t) 0x00c00000)
965*62c56f98SSadaf Ebrahimi #define PSA_ALG_HMAC_BASE ((psa_algorithm_t) 0x03800000)
966*62c56f98SSadaf Ebrahimi /** Macro to build an HMAC algorithm.
967*62c56f98SSadaf Ebrahimi *
968*62c56f98SSadaf Ebrahimi * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
969*62c56f98SSadaf Ebrahimi *
970*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
971*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
972*62c56f98SSadaf Ebrahimi *
973*62c56f98SSadaf Ebrahimi * \return The corresponding HMAC algorithm.
974*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
975*62c56f98SSadaf Ebrahimi * hash algorithm.
976*62c56f98SSadaf Ebrahimi */
977*62c56f98SSadaf Ebrahimi #define PSA_ALG_HMAC(hash_alg) \
978*62c56f98SSadaf Ebrahimi (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
979*62c56f98SSadaf Ebrahimi
980*62c56f98SSadaf Ebrahimi #define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
981*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
982*62c56f98SSadaf Ebrahimi
983*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an HMAC algorithm.
984*62c56f98SSadaf Ebrahimi *
985*62c56f98SSadaf Ebrahimi * HMAC is a family of MAC algorithms that are based on a hash function.
986*62c56f98SSadaf Ebrahimi *
987*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
988*62c56f98SSadaf Ebrahimi *
989*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
990*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
991*62c56f98SSadaf Ebrahimi * algorithm identifier.
992*62c56f98SSadaf Ebrahimi */
993*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HMAC(alg) \
994*62c56f98SSadaf Ebrahimi (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
995*62c56f98SSadaf Ebrahimi PSA_ALG_HMAC_BASE)
996*62c56f98SSadaf Ebrahimi
997*62c56f98SSadaf Ebrahimi /* In the encoding of a MAC algorithm, the bits corresponding to
998*62c56f98SSadaf Ebrahimi * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
999*62c56f98SSadaf Ebrahimi * truncated. As an exception, the value 0 means the untruncated algorithm,
1000*62c56f98SSadaf Ebrahimi * whatever its length is. The length is encoded in 6 bits, so it can
1001*62c56f98SSadaf Ebrahimi * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
1002*62c56f98SSadaf Ebrahimi * to full length is correctly encoded as 0 and any non-trivial truncation
1003*62c56f98SSadaf Ebrahimi * is correctly encoded as a value between 1 and 63. */
1004*62c56f98SSadaf Ebrahimi #define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t) 0x003f0000)
1005*62c56f98SSadaf Ebrahimi #define PSA_MAC_TRUNCATION_OFFSET 16
1006*62c56f98SSadaf Ebrahimi
1007*62c56f98SSadaf Ebrahimi /* In the encoding of a MAC algorithm, the bit corresponding to
1008*62c56f98SSadaf Ebrahimi * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1009*62c56f98SSadaf Ebrahimi * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1010*62c56f98SSadaf Ebrahimi * algorithm policy can be used with any algorithm corresponding to the
1011*62c56f98SSadaf Ebrahimi * same base class and having a (potentially truncated) MAC length greater or
1012*62c56f98SSadaf Ebrahimi * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
1013*62c56f98SSadaf Ebrahimi #define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)
1014*62c56f98SSadaf Ebrahimi
1015*62c56f98SSadaf Ebrahimi /** Macro to build a truncated MAC algorithm.
1016*62c56f98SSadaf Ebrahimi *
1017*62c56f98SSadaf Ebrahimi * A truncated MAC algorithm is identical to the corresponding MAC
1018*62c56f98SSadaf Ebrahimi * algorithm except that the MAC value for the truncated algorithm
1019*62c56f98SSadaf Ebrahimi * consists of only the first \p mac_length bytes of the MAC value
1020*62c56f98SSadaf Ebrahimi * for the untruncated algorithm.
1021*62c56f98SSadaf Ebrahimi *
1022*62c56f98SSadaf Ebrahimi * \note This macro may allow constructing algorithm identifiers that
1023*62c56f98SSadaf Ebrahimi * are not valid, either because the specified length is larger
1024*62c56f98SSadaf Ebrahimi * than the untruncated MAC or because the specified length is
1025*62c56f98SSadaf Ebrahimi * smaller than permitted by the implementation.
1026*62c56f98SSadaf Ebrahimi *
1027*62c56f98SSadaf Ebrahimi * \note It is implementation-defined whether a truncated MAC that
1028*62c56f98SSadaf Ebrahimi * is truncated to the same length as the MAC of the untruncated
1029*62c56f98SSadaf Ebrahimi * algorithm is considered identical to the untruncated algorithm
1030*62c56f98SSadaf Ebrahimi * for policy comparison purposes.
1031*62c56f98SSadaf Ebrahimi *
1032*62c56f98SSadaf Ebrahimi * \param mac_alg A MAC algorithm identifier (value of type
1033*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1034*62c56f98SSadaf Ebrahimi * is true). This may be a truncated or untruncated
1035*62c56f98SSadaf Ebrahimi * MAC algorithm.
1036*62c56f98SSadaf Ebrahimi * \param mac_length Desired length of the truncated MAC in bytes.
1037*62c56f98SSadaf Ebrahimi * This must be at most the full length of the MAC
1038*62c56f98SSadaf Ebrahimi * and must be at least an implementation-specified
1039*62c56f98SSadaf Ebrahimi * minimum. The implementation-specified minimum
1040*62c56f98SSadaf Ebrahimi * shall not be zero.
1041*62c56f98SSadaf Ebrahimi *
1042*62c56f98SSadaf Ebrahimi * \return The corresponding MAC algorithm with the specified
1043*62c56f98SSadaf Ebrahimi * length.
1044*62c56f98SSadaf Ebrahimi * \return Unspecified if \p mac_alg is not a supported
1045*62c56f98SSadaf Ebrahimi * MAC algorithm or if \p mac_length is too small or
1046*62c56f98SSadaf Ebrahimi * too large for the specified MAC algorithm.
1047*62c56f98SSadaf Ebrahimi */
1048*62c56f98SSadaf Ebrahimi #define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
1049*62c56f98SSadaf Ebrahimi (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1050*62c56f98SSadaf Ebrahimi PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
1051*62c56f98SSadaf Ebrahimi ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1052*62c56f98SSadaf Ebrahimi
1053*62c56f98SSadaf Ebrahimi /** Macro to build the base MAC algorithm corresponding to a truncated
1054*62c56f98SSadaf Ebrahimi * MAC algorithm.
1055*62c56f98SSadaf Ebrahimi *
1056*62c56f98SSadaf Ebrahimi * \param mac_alg A MAC algorithm identifier (value of type
1057*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1058*62c56f98SSadaf Ebrahimi * is true). This may be a truncated or untruncated
1059*62c56f98SSadaf Ebrahimi * MAC algorithm.
1060*62c56f98SSadaf Ebrahimi *
1061*62c56f98SSadaf Ebrahimi * \return The corresponding base MAC algorithm.
1062*62c56f98SSadaf Ebrahimi * \return Unspecified if \p mac_alg is not a supported
1063*62c56f98SSadaf Ebrahimi * MAC algorithm.
1064*62c56f98SSadaf Ebrahimi */
1065*62c56f98SSadaf Ebrahimi #define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
1066*62c56f98SSadaf Ebrahimi ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1067*62c56f98SSadaf Ebrahimi PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
1068*62c56f98SSadaf Ebrahimi
1069*62c56f98SSadaf Ebrahimi /** Length to which a MAC algorithm is truncated.
1070*62c56f98SSadaf Ebrahimi *
1071*62c56f98SSadaf Ebrahimi * \param mac_alg A MAC algorithm identifier (value of type
1072*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1073*62c56f98SSadaf Ebrahimi * is true).
1074*62c56f98SSadaf Ebrahimi *
1075*62c56f98SSadaf Ebrahimi * \return Length of the truncated MAC in bytes.
1076*62c56f98SSadaf Ebrahimi * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
1077*62c56f98SSadaf Ebrahimi * \return Unspecified if \p mac_alg is not a supported
1078*62c56f98SSadaf Ebrahimi * MAC algorithm.
1079*62c56f98SSadaf Ebrahimi */
1080*62c56f98SSadaf Ebrahimi #define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
1081*62c56f98SSadaf Ebrahimi (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
1082*62c56f98SSadaf Ebrahimi
1083*62c56f98SSadaf Ebrahimi /** Macro to build a MAC minimum-MAC-length wildcard algorithm.
1084*62c56f98SSadaf Ebrahimi *
1085*62c56f98SSadaf Ebrahimi * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
1086*62c56f98SSadaf Ebrahimi * sharing the same base algorithm, and where the (potentially truncated) MAC
1087*62c56f98SSadaf Ebrahimi * length of the specific algorithm is equal to or larger then the wildcard
1088*62c56f98SSadaf Ebrahimi * algorithm's minimum MAC length.
1089*62c56f98SSadaf Ebrahimi *
1090*62c56f98SSadaf Ebrahimi * \note When setting the minimum required MAC length to less than the
1091*62c56f98SSadaf Ebrahimi * smallest MAC length allowed by the base algorithm, this effectively
1092*62c56f98SSadaf Ebrahimi * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
1093*62c56f98SSadaf Ebrahimi *
1094*62c56f98SSadaf Ebrahimi * \param mac_alg A MAC algorithm identifier (value of type
1095*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1096*62c56f98SSadaf Ebrahimi * is true).
1097*62c56f98SSadaf Ebrahimi * \param min_mac_length Desired minimum length of the message authentication
1098*62c56f98SSadaf Ebrahimi * code in bytes. This must be at most the untruncated
1099*62c56f98SSadaf Ebrahimi * length of the MAC and must be at least 1.
1100*62c56f98SSadaf Ebrahimi *
1101*62c56f98SSadaf Ebrahimi * \return The corresponding MAC wildcard algorithm with the
1102*62c56f98SSadaf Ebrahimi * specified minimum length.
1103*62c56f98SSadaf Ebrahimi * \return Unspecified if \p mac_alg is not a supported MAC
1104*62c56f98SSadaf Ebrahimi * algorithm or if \p min_mac_length is less than 1 or
1105*62c56f98SSadaf Ebrahimi * too large for the specified MAC algorithm.
1106*62c56f98SSadaf Ebrahimi */
1107*62c56f98SSadaf Ebrahimi #define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
1108*62c56f98SSadaf Ebrahimi (PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
1109*62c56f98SSadaf Ebrahimi PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)
1110*62c56f98SSadaf Ebrahimi
1111*62c56f98SSadaf Ebrahimi #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t) 0x03c00000)
1112*62c56f98SSadaf Ebrahimi /** The CBC-MAC construction over a block cipher
1113*62c56f98SSadaf Ebrahimi *
1114*62c56f98SSadaf Ebrahimi * \warning CBC-MAC is insecure in many cases.
1115*62c56f98SSadaf Ebrahimi * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1116*62c56f98SSadaf Ebrahimi */
1117*62c56f98SSadaf Ebrahimi #define PSA_ALG_CBC_MAC ((psa_algorithm_t) 0x03c00100)
1118*62c56f98SSadaf Ebrahimi /** The CMAC construction over a block cipher */
1119*62c56f98SSadaf Ebrahimi #define PSA_ALG_CMAC ((psa_algorithm_t) 0x03c00200)
1120*62c56f98SSadaf Ebrahimi
1121*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1122*62c56f98SSadaf Ebrahimi *
1123*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1124*62c56f98SSadaf Ebrahimi *
1125*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1126*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
1127*62c56f98SSadaf Ebrahimi * algorithm identifier.
1128*62c56f98SSadaf Ebrahimi */
1129*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
1130*62c56f98SSadaf Ebrahimi (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1131*62c56f98SSadaf Ebrahimi PSA_ALG_CIPHER_MAC_BASE)
1132*62c56f98SSadaf Ebrahimi
1133*62c56f98SSadaf Ebrahimi #define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t) 0x00800000)
1134*62c56f98SSadaf Ebrahimi #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)
1135*62c56f98SSadaf Ebrahimi
1136*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a stream cipher.
1137*62c56f98SSadaf Ebrahimi *
1138*62c56f98SSadaf Ebrahimi * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1139*62c56f98SSadaf Ebrahimi * by applying a bitwise-xor with a stream of bytes that is generated
1140*62c56f98SSadaf Ebrahimi * from a key.
1141*62c56f98SSadaf Ebrahimi *
1142*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1143*62c56f98SSadaf Ebrahimi *
1144*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1145*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
1146*62c56f98SSadaf Ebrahimi * algorithm identifier or if it is not a symmetric cipher algorithm.
1147*62c56f98SSadaf Ebrahimi */
1148*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_STREAM_CIPHER(alg) \
1149*62c56f98SSadaf Ebrahimi (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1150*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1151*62c56f98SSadaf Ebrahimi
1152*62c56f98SSadaf Ebrahimi /** The stream cipher mode of a stream cipher algorithm.
1153*62c56f98SSadaf Ebrahimi *
1154*62c56f98SSadaf Ebrahimi * The underlying stream cipher is determined by the key type.
1155*62c56f98SSadaf Ebrahimi * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
1156*62c56f98SSadaf Ebrahimi */
1157*62c56f98SSadaf Ebrahimi #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t) 0x04800100)
1158*62c56f98SSadaf Ebrahimi
1159*62c56f98SSadaf Ebrahimi /** The CTR stream cipher mode.
1160*62c56f98SSadaf Ebrahimi *
1161*62c56f98SSadaf Ebrahimi * CTR is a stream cipher which is built from a block cipher.
1162*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1163*62c56f98SSadaf Ebrahimi * For example, to use AES-128-CTR, use this algorithm with
1164*62c56f98SSadaf Ebrahimi * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1165*62c56f98SSadaf Ebrahimi */
1166*62c56f98SSadaf Ebrahimi #define PSA_ALG_CTR ((psa_algorithm_t) 0x04c01000)
1167*62c56f98SSadaf Ebrahimi
1168*62c56f98SSadaf Ebrahimi /** The CFB stream cipher mode.
1169*62c56f98SSadaf Ebrahimi *
1170*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1171*62c56f98SSadaf Ebrahimi */
1172*62c56f98SSadaf Ebrahimi #define PSA_ALG_CFB ((psa_algorithm_t) 0x04c01100)
1173*62c56f98SSadaf Ebrahimi
1174*62c56f98SSadaf Ebrahimi /** The OFB stream cipher mode.
1175*62c56f98SSadaf Ebrahimi *
1176*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1177*62c56f98SSadaf Ebrahimi */
1178*62c56f98SSadaf Ebrahimi #define PSA_ALG_OFB ((psa_algorithm_t) 0x04c01200)
1179*62c56f98SSadaf Ebrahimi
1180*62c56f98SSadaf Ebrahimi /** The XTS cipher mode.
1181*62c56f98SSadaf Ebrahimi *
1182*62c56f98SSadaf Ebrahimi * XTS is a cipher mode which is built from a block cipher. It requires at
1183*62c56f98SSadaf Ebrahimi * least one full block of input, but beyond this minimum the input
1184*62c56f98SSadaf Ebrahimi * does not need to be a whole number of blocks.
1185*62c56f98SSadaf Ebrahimi */
1186*62c56f98SSadaf Ebrahimi #define PSA_ALG_XTS ((psa_algorithm_t) 0x0440ff00)
1187*62c56f98SSadaf Ebrahimi
1188*62c56f98SSadaf Ebrahimi /** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1189*62c56f98SSadaf Ebrahimi *
1190*62c56f98SSadaf Ebrahimi * \warning ECB mode does not protect the confidentiality of the encrypted data
1191*62c56f98SSadaf Ebrahimi * except in extremely narrow circumstances. It is recommended that applications
1192*62c56f98SSadaf Ebrahimi * only use ECB if they need to construct an operating mode that the
1193*62c56f98SSadaf Ebrahimi * implementation does not provide. Implementations are encouraged to provide
1194*62c56f98SSadaf Ebrahimi * the modes that applications need in preference to supporting direct access
1195*62c56f98SSadaf Ebrahimi * to ECB.
1196*62c56f98SSadaf Ebrahimi *
1197*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1198*62c56f98SSadaf Ebrahimi *
1199*62c56f98SSadaf Ebrahimi * This symmetric cipher mode can only be used with messages whose lengths are a
1200*62c56f98SSadaf Ebrahimi * multiple of the block size of the chosen block cipher.
1201*62c56f98SSadaf Ebrahimi *
1202*62c56f98SSadaf Ebrahimi * ECB mode does not accept an initialization vector (IV). When using a
1203*62c56f98SSadaf Ebrahimi * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1204*62c56f98SSadaf Ebrahimi * and psa_cipher_set_iv() must not be called.
1205*62c56f98SSadaf Ebrahimi */
1206*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t) 0x04404400)
1207*62c56f98SSadaf Ebrahimi
1208*62c56f98SSadaf Ebrahimi /** The CBC block cipher chaining mode, with no padding.
1209*62c56f98SSadaf Ebrahimi *
1210*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1211*62c56f98SSadaf Ebrahimi *
1212*62c56f98SSadaf Ebrahimi * This symmetric cipher mode can only be used with messages whose lengths
1213*62c56f98SSadaf Ebrahimi * are whole number of blocks for the chosen block cipher.
1214*62c56f98SSadaf Ebrahimi */
1215*62c56f98SSadaf Ebrahimi #define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t) 0x04404000)
1216*62c56f98SSadaf Ebrahimi
1217*62c56f98SSadaf Ebrahimi /** The CBC block cipher chaining mode with PKCS#7 padding.
1218*62c56f98SSadaf Ebrahimi *
1219*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1220*62c56f98SSadaf Ebrahimi *
1221*62c56f98SSadaf Ebrahimi * This is the padding method defined by PKCS#7 (RFC 2315) §10.3.
1222*62c56f98SSadaf Ebrahimi */
1223*62c56f98SSadaf Ebrahimi #define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t) 0x04404100)
1224*62c56f98SSadaf Ebrahimi
1225*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)
1226*62c56f98SSadaf Ebrahimi
1227*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an AEAD mode on a block cipher.
1228*62c56f98SSadaf Ebrahimi *
1229*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1230*62c56f98SSadaf Ebrahimi *
1231*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1232*62c56f98SSadaf Ebrahimi * a block cipher, 0 otherwise.
1233*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
1234*62c56f98SSadaf Ebrahimi * algorithm identifier.
1235*62c56f98SSadaf Ebrahimi */
1236*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1237*62c56f98SSadaf Ebrahimi (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1238*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1239*62c56f98SSadaf Ebrahimi
1240*62c56f98SSadaf Ebrahimi /** The CCM authenticated encryption algorithm.
1241*62c56f98SSadaf Ebrahimi *
1242*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1243*62c56f98SSadaf Ebrahimi */
1244*62c56f98SSadaf Ebrahimi #define PSA_ALG_CCM ((psa_algorithm_t) 0x05500100)
1245*62c56f98SSadaf Ebrahimi
1246*62c56f98SSadaf Ebrahimi /** The CCM* cipher mode without authentication.
1247*62c56f98SSadaf Ebrahimi *
1248*62c56f98SSadaf Ebrahimi * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1249*62c56f98SSadaf Ebrahimi * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1250*62c56f98SSadaf Ebrahimi *
1251*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1252*62c56f98SSadaf Ebrahimi *
1253*62c56f98SSadaf Ebrahimi * Currently only 13-byte long IV's are supported.
1254*62c56f98SSadaf Ebrahimi */
1255*62c56f98SSadaf Ebrahimi #define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t) 0x04c01300)
1256*62c56f98SSadaf Ebrahimi
1257*62c56f98SSadaf Ebrahimi /** The GCM authenticated encryption algorithm.
1258*62c56f98SSadaf Ebrahimi *
1259*62c56f98SSadaf Ebrahimi * The underlying block cipher is determined by the key type.
1260*62c56f98SSadaf Ebrahimi */
1261*62c56f98SSadaf Ebrahimi #define PSA_ALG_GCM ((psa_algorithm_t) 0x05500200)
1262*62c56f98SSadaf Ebrahimi
1263*62c56f98SSadaf Ebrahimi /** The Chacha20-Poly1305 AEAD algorithm.
1264*62c56f98SSadaf Ebrahimi *
1265*62c56f98SSadaf Ebrahimi * The ChaCha20_Poly1305 construction is defined in RFC 7539.
1266*62c56f98SSadaf Ebrahimi *
1267*62c56f98SSadaf Ebrahimi * Implementations must support 12-byte nonces, may support 8-byte nonces,
1268*62c56f98SSadaf Ebrahimi * and should reject other sizes.
1269*62c56f98SSadaf Ebrahimi *
1270*62c56f98SSadaf Ebrahimi * Implementations must support 16-byte tags and should reject other sizes.
1271*62c56f98SSadaf Ebrahimi */
1272*62c56f98SSadaf Ebrahimi #define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t) 0x05100500)
1273*62c56f98SSadaf Ebrahimi
1274*62c56f98SSadaf Ebrahimi /* In the encoding of an AEAD algorithm, the bits corresponding to
1275*62c56f98SSadaf Ebrahimi * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1276*62c56f98SSadaf Ebrahimi * The constants for default lengths follow this encoding.
1277*62c56f98SSadaf Ebrahimi */
1278*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t) 0x003f0000)
1279*62c56f98SSadaf Ebrahimi #define PSA_AEAD_TAG_LENGTH_OFFSET 16
1280*62c56f98SSadaf Ebrahimi
1281*62c56f98SSadaf Ebrahimi /* In the encoding of an AEAD algorithm, the bit corresponding to
1282*62c56f98SSadaf Ebrahimi * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
1283*62c56f98SSadaf Ebrahimi * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1284*62c56f98SSadaf Ebrahimi * algorithm policy can be used with any algorithm corresponding to the
1285*62c56f98SSadaf Ebrahimi * same base class and having a tag length greater than or equal to the one
1286*62c56f98SSadaf Ebrahimi * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1287*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)
1288*62c56f98SSadaf Ebrahimi
1289*62c56f98SSadaf Ebrahimi /** Macro to build a shortened AEAD algorithm.
1290*62c56f98SSadaf Ebrahimi *
1291*62c56f98SSadaf Ebrahimi * A shortened AEAD algorithm is similar to the corresponding AEAD
1292*62c56f98SSadaf Ebrahimi * algorithm, but has an authentication tag that consists of fewer bytes.
1293*62c56f98SSadaf Ebrahimi * Depending on the algorithm, the tag length may affect the calculation
1294*62c56f98SSadaf Ebrahimi * of the ciphertext.
1295*62c56f98SSadaf Ebrahimi *
1296*62c56f98SSadaf Ebrahimi * \param aead_alg An AEAD algorithm identifier (value of type
1297*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
1298*62c56f98SSadaf Ebrahimi * is true).
1299*62c56f98SSadaf Ebrahimi * \param tag_length Desired length of the authentication tag in bytes.
1300*62c56f98SSadaf Ebrahimi *
1301*62c56f98SSadaf Ebrahimi * \return The corresponding AEAD algorithm with the specified
1302*62c56f98SSadaf Ebrahimi * length.
1303*62c56f98SSadaf Ebrahimi * \return Unspecified if \p aead_alg is not a supported
1304*62c56f98SSadaf Ebrahimi * AEAD algorithm or if \p tag_length is not valid
1305*62c56f98SSadaf Ebrahimi * for the specified AEAD algorithm.
1306*62c56f98SSadaf Ebrahimi */
1307*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
1308*62c56f98SSadaf Ebrahimi (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1309*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
1310*62c56f98SSadaf Ebrahimi ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1311*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_TAG_LENGTH_MASK))
1312*62c56f98SSadaf Ebrahimi
1313*62c56f98SSadaf Ebrahimi /** Retrieve the tag length of a specified AEAD algorithm
1314*62c56f98SSadaf Ebrahimi *
1315*62c56f98SSadaf Ebrahimi * \param aead_alg An AEAD algorithm identifier (value of type
1316*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
1317*62c56f98SSadaf Ebrahimi * is true).
1318*62c56f98SSadaf Ebrahimi *
1319*62c56f98SSadaf Ebrahimi * \return The tag length specified by the input algorithm.
1320*62c56f98SSadaf Ebrahimi * \return Unspecified if \p aead_alg is not a supported
1321*62c56f98SSadaf Ebrahimi * AEAD algorithm.
1322*62c56f98SSadaf Ebrahimi */
1323*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1324*62c56f98SSadaf Ebrahimi (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
1325*62c56f98SSadaf Ebrahimi PSA_AEAD_TAG_LENGTH_OFFSET)
1326*62c56f98SSadaf Ebrahimi
1327*62c56f98SSadaf Ebrahimi /** Calculate the corresponding AEAD algorithm with the default tag length.
1328*62c56f98SSadaf Ebrahimi *
1329*62c56f98SSadaf Ebrahimi * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
1330*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1331*62c56f98SSadaf Ebrahimi *
1332*62c56f98SSadaf Ebrahimi * \return The corresponding AEAD algorithm with the default
1333*62c56f98SSadaf Ebrahimi * tag length for that algorithm.
1334*62c56f98SSadaf Ebrahimi */
1335*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
1336*62c56f98SSadaf Ebrahimi ( \
1337*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1338*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1339*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
1340*62c56f98SSadaf Ebrahimi 0)
1341*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1342*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1343*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
1344*62c56f98SSadaf Ebrahimi ref :
1345*62c56f98SSadaf Ebrahimi
1346*62c56f98SSadaf Ebrahimi /** Macro to build an AEAD minimum-tag-length wildcard algorithm.
1347*62c56f98SSadaf Ebrahimi *
1348*62c56f98SSadaf Ebrahimi * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
1349*62c56f98SSadaf Ebrahimi * sharing the same base algorithm, and where the tag length of the specific
1350*62c56f98SSadaf Ebrahimi * algorithm is equal to or larger then the minimum tag length specified by the
1351*62c56f98SSadaf Ebrahimi * wildcard algorithm.
1352*62c56f98SSadaf Ebrahimi *
1353*62c56f98SSadaf Ebrahimi * \note When setting the minimum required tag length to less than the
1354*62c56f98SSadaf Ebrahimi * smallest tag length allowed by the base algorithm, this effectively
1355*62c56f98SSadaf Ebrahimi * becomes an 'any-tag-length-allowed' policy for that base algorithm.
1356*62c56f98SSadaf Ebrahimi *
1357*62c56f98SSadaf Ebrahimi * \param aead_alg An AEAD algorithm identifier (value of type
1358*62c56f98SSadaf Ebrahimi * #psa_algorithm_t such that
1359*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1360*62c56f98SSadaf Ebrahimi * \param min_tag_length Desired minimum length of the authentication tag in
1361*62c56f98SSadaf Ebrahimi * bytes. This must be at least 1 and at most the largest
1362*62c56f98SSadaf Ebrahimi * allowed tag length of the algorithm.
1363*62c56f98SSadaf Ebrahimi *
1364*62c56f98SSadaf Ebrahimi * \return The corresponding AEAD wildcard algorithm with the
1365*62c56f98SSadaf Ebrahimi * specified minimum length.
1366*62c56f98SSadaf Ebrahimi * \return Unspecified if \p aead_alg is not a supported
1367*62c56f98SSadaf Ebrahimi * AEAD algorithm or if \p min_tag_length is less than 1
1368*62c56f98SSadaf Ebrahimi * or too large for the specified AEAD algorithm.
1369*62c56f98SSadaf Ebrahimi */
1370*62c56f98SSadaf Ebrahimi #define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
1371*62c56f98SSadaf Ebrahimi (PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1372*62c56f98SSadaf Ebrahimi PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)
1373*62c56f98SSadaf Ebrahimi
1374*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t) 0x06000200)
1375*62c56f98SSadaf Ebrahimi /** RSA PKCS#1 v1.5 signature with hashing.
1376*62c56f98SSadaf Ebrahimi *
1377*62c56f98SSadaf Ebrahimi * This is the signature scheme defined by RFC 8017
1378*62c56f98SSadaf Ebrahimi * (PKCS#1: RSA Cryptography Specifications) under the name
1379*62c56f98SSadaf Ebrahimi * RSASSA-PKCS1-v1_5.
1380*62c56f98SSadaf Ebrahimi *
1381*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1382*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1383*62c56f98SSadaf Ebrahimi * This includes #PSA_ALG_ANY_HASH
1384*62c56f98SSadaf Ebrahimi * when specifying the algorithm in a usage policy.
1385*62c56f98SSadaf Ebrahimi *
1386*62c56f98SSadaf Ebrahimi * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
1387*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1388*62c56f98SSadaf Ebrahimi * hash algorithm.
1389*62c56f98SSadaf Ebrahimi */
1390*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1391*62c56f98SSadaf Ebrahimi (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1392*62c56f98SSadaf Ebrahimi /** Raw PKCS#1 v1.5 signature.
1393*62c56f98SSadaf Ebrahimi *
1394*62c56f98SSadaf Ebrahimi * The input to this algorithm is the DigestInfo structure used by
1395*62c56f98SSadaf Ebrahimi * RFC 8017 (PKCS#1: RSA Cryptography Specifications), §9.2
1396*62c56f98SSadaf Ebrahimi * steps 3–6.
1397*62c56f98SSadaf Ebrahimi */
1398*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1399*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1400*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1401*62c56f98SSadaf Ebrahimi
1402*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t) 0x06000300)
1403*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t) 0x06001300)
1404*62c56f98SSadaf Ebrahimi /** RSA PSS signature with hashing.
1405*62c56f98SSadaf Ebrahimi *
1406*62c56f98SSadaf Ebrahimi * This is the signature scheme defined by RFC 8017
1407*62c56f98SSadaf Ebrahimi * (PKCS#1: RSA Cryptography Specifications) under the name
1408*62c56f98SSadaf Ebrahimi * RSASSA-PSS, with the message generation function MGF1, and with
1409*62c56f98SSadaf Ebrahimi * a salt length equal to the length of the hash, or the largest
1410*62c56f98SSadaf Ebrahimi * possible salt length for the algorithm and key size if that is
1411*62c56f98SSadaf Ebrahimi * smaller than the hash length. The specified hash algorithm is
1412*62c56f98SSadaf Ebrahimi * used to hash the input message, to create the salted hash, and
1413*62c56f98SSadaf Ebrahimi * for the mask generation.
1414*62c56f98SSadaf Ebrahimi *
1415*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1416*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1417*62c56f98SSadaf Ebrahimi * This includes #PSA_ALG_ANY_HASH
1418*62c56f98SSadaf Ebrahimi * when specifying the algorithm in a usage policy.
1419*62c56f98SSadaf Ebrahimi *
1420*62c56f98SSadaf Ebrahimi * \return The corresponding RSA PSS signature algorithm.
1421*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1422*62c56f98SSadaf Ebrahimi * hash algorithm.
1423*62c56f98SSadaf Ebrahimi */
1424*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PSS(hash_alg) \
1425*62c56f98SSadaf Ebrahimi (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1426*62c56f98SSadaf Ebrahimi
1427*62c56f98SSadaf Ebrahimi /** RSA PSS signature with hashing with relaxed verification.
1428*62c56f98SSadaf Ebrahimi *
1429*62c56f98SSadaf Ebrahimi * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1430*62c56f98SSadaf Ebrahimi * but allows an arbitrary salt length (including \c 0) when verifying a
1431*62c56f98SSadaf Ebrahimi * signature.
1432*62c56f98SSadaf Ebrahimi *
1433*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1434*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1435*62c56f98SSadaf Ebrahimi * This includes #PSA_ALG_ANY_HASH
1436*62c56f98SSadaf Ebrahimi * when specifying the algorithm in a usage policy.
1437*62c56f98SSadaf Ebrahimi *
1438*62c56f98SSadaf Ebrahimi * \return The corresponding RSA PSS signature algorithm.
1439*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1440*62c56f98SSadaf Ebrahimi * hash algorithm.
1441*62c56f98SSadaf Ebrahimi */
1442*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \
1443*62c56f98SSadaf Ebrahimi (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1444*62c56f98SSadaf Ebrahimi
1445*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is RSA PSS with standard salt.
1446*62c56f98SSadaf Ebrahimi *
1447*62c56f98SSadaf Ebrahimi * \param alg An algorithm value or an algorithm policy wildcard.
1448*62c56f98SSadaf Ebrahimi *
1449*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is of the form
1450*62c56f98SSadaf Ebrahimi * #PSA_ALG_RSA_PSS(\c hash_alg),
1451*62c56f98SSadaf Ebrahimi * where \c hash_alg is a hash algorithm or
1452*62c56f98SSadaf Ebrahimi * #PSA_ALG_ANY_HASH. 0 otherwise.
1453*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not
1454*62c56f98SSadaf Ebrahimi * a supported algorithm identifier or policy.
1455*62c56f98SSadaf Ebrahimi */
1456*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \
1457*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1458*62c56f98SSadaf Ebrahimi
1459*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is RSA PSS with any salt.
1460*62c56f98SSadaf Ebrahimi *
1461*62c56f98SSadaf Ebrahimi * \param alg An algorithm value or an algorithm policy wildcard.
1462*62c56f98SSadaf Ebrahimi *
1463*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is of the form
1464*62c56f98SSadaf Ebrahimi * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1465*62c56f98SSadaf Ebrahimi * where \c hash_alg is a hash algorithm or
1466*62c56f98SSadaf Ebrahimi * #PSA_ALG_ANY_HASH. 0 otherwise.
1467*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not
1468*62c56f98SSadaf Ebrahimi * a supported algorithm identifier or policy.
1469*62c56f98SSadaf Ebrahimi */
1470*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \
1471*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1472*62c56f98SSadaf Ebrahimi
1473*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is RSA PSS.
1474*62c56f98SSadaf Ebrahimi *
1475*62c56f98SSadaf Ebrahimi * This includes any of the RSA PSS algorithm variants, regardless of the
1476*62c56f98SSadaf Ebrahimi * constraints on salt length.
1477*62c56f98SSadaf Ebrahimi *
1478*62c56f98SSadaf Ebrahimi * \param alg An algorithm value or an algorithm policy wildcard.
1479*62c56f98SSadaf Ebrahimi *
1480*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is of the form
1481*62c56f98SSadaf Ebrahimi * #PSA_ALG_RSA_PSS(\c hash_alg) or
1482*62c56f98SSadaf Ebrahimi * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1483*62c56f98SSadaf Ebrahimi * where \c hash_alg is a hash algorithm or
1484*62c56f98SSadaf Ebrahimi * #PSA_ALG_ANY_HASH. 0 otherwise.
1485*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not
1486*62c56f98SSadaf Ebrahimi * a supported algorithm identifier or policy.
1487*62c56f98SSadaf Ebrahimi */
1488*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RSA_PSS(alg) \
1489*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \
1490*62c56f98SSadaf Ebrahimi PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
1491*62c56f98SSadaf Ebrahimi
1492*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t) 0x06000600)
1493*62c56f98SSadaf Ebrahimi /** ECDSA signature with hashing.
1494*62c56f98SSadaf Ebrahimi *
1495*62c56f98SSadaf Ebrahimi * This is the ECDSA signature scheme defined by ANSI X9.62,
1496*62c56f98SSadaf Ebrahimi * with a random per-message secret number (*k*).
1497*62c56f98SSadaf Ebrahimi *
1498*62c56f98SSadaf Ebrahimi * The representation of the signature as a byte string consists of
1499*62c56f98SSadaf Ebrahimi * the concatenation of the signature values *r* and *s*. Each of
1500*62c56f98SSadaf Ebrahimi * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1501*62c56f98SSadaf Ebrahimi * of the base point of the curve in octets. Each value is represented
1502*62c56f98SSadaf Ebrahimi * in big-endian order (most significant octet first).
1503*62c56f98SSadaf Ebrahimi *
1504*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1505*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1506*62c56f98SSadaf Ebrahimi * This includes #PSA_ALG_ANY_HASH
1507*62c56f98SSadaf Ebrahimi * when specifying the algorithm in a usage policy.
1508*62c56f98SSadaf Ebrahimi *
1509*62c56f98SSadaf Ebrahimi * \return The corresponding ECDSA signature algorithm.
1510*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1511*62c56f98SSadaf Ebrahimi * hash algorithm.
1512*62c56f98SSadaf Ebrahimi */
1513*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECDSA(hash_alg) \
1514*62c56f98SSadaf Ebrahimi (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1515*62c56f98SSadaf Ebrahimi /** ECDSA signature without hashing.
1516*62c56f98SSadaf Ebrahimi *
1517*62c56f98SSadaf Ebrahimi * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1518*62c56f98SSadaf Ebrahimi * without specifying a hash algorithm. This algorithm may only be
1519*62c56f98SSadaf Ebrahimi * used to sign or verify a sequence of bytes that should be an
1520*62c56f98SSadaf Ebrahimi * already-calculated hash. Note that the input is padded with
1521*62c56f98SSadaf Ebrahimi * zeros on the left or truncated on the left as required to fit
1522*62c56f98SSadaf Ebrahimi * the curve size.
1523*62c56f98SSadaf Ebrahimi */
1524*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1525*62c56f98SSadaf Ebrahimi #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t) 0x06000700)
1526*62c56f98SSadaf Ebrahimi /** Deterministic ECDSA signature with hashing.
1527*62c56f98SSadaf Ebrahimi *
1528*62c56f98SSadaf Ebrahimi * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1529*62c56f98SSadaf Ebrahimi *
1530*62c56f98SSadaf Ebrahimi * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1531*62c56f98SSadaf Ebrahimi *
1532*62c56f98SSadaf Ebrahimi * Note that when this algorithm is used for verification, signatures
1533*62c56f98SSadaf Ebrahimi * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1534*62c56f98SSadaf Ebrahimi * same private key are accepted. In other words,
1535*62c56f98SSadaf Ebrahimi * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1536*62c56f98SSadaf Ebrahimi * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1537*62c56f98SSadaf Ebrahimi *
1538*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1539*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1540*62c56f98SSadaf Ebrahimi * This includes #PSA_ALG_ANY_HASH
1541*62c56f98SSadaf Ebrahimi * when specifying the algorithm in a usage policy.
1542*62c56f98SSadaf Ebrahimi *
1543*62c56f98SSadaf Ebrahimi * \return The corresponding deterministic ECDSA signature
1544*62c56f98SSadaf Ebrahimi * algorithm.
1545*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1546*62c56f98SSadaf Ebrahimi * hash algorithm.
1547*62c56f98SSadaf Ebrahimi */
1548*62c56f98SSadaf Ebrahimi #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1549*62c56f98SSadaf Ebrahimi (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1550*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t) 0x00000100)
1551*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_ECDSA(alg) \
1552*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
1553*62c56f98SSadaf Ebrahimi PSA_ALG_ECDSA_BASE)
1554*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1555*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
1556*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1557*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1558*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1559*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1560*62c56f98SSadaf Ebrahimi
1561*62c56f98SSadaf Ebrahimi /** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1562*62c56f98SSadaf Ebrahimi * using standard parameters.
1563*62c56f98SSadaf Ebrahimi *
1564*62c56f98SSadaf Ebrahimi * Contexts are not supported in the current version of this specification
1565*62c56f98SSadaf Ebrahimi * because there is no suitable signature interface that can take the
1566*62c56f98SSadaf Ebrahimi * context as a parameter. A future version of this specification may add
1567*62c56f98SSadaf Ebrahimi * suitable functions and extend this algorithm to support contexts.
1568*62c56f98SSadaf Ebrahimi *
1569*62c56f98SSadaf Ebrahimi * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1570*62c56f98SSadaf Ebrahimi * In this specification, the following curves are supported:
1571*62c56f98SSadaf Ebrahimi * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1572*62c56f98SSadaf Ebrahimi * in RFC 8032.
1573*62c56f98SSadaf Ebrahimi * The curve is Edwards25519.
1574*62c56f98SSadaf Ebrahimi * The hash function used internally is SHA-512.
1575*62c56f98SSadaf Ebrahimi * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1576*62c56f98SSadaf Ebrahimi * in RFC 8032.
1577*62c56f98SSadaf Ebrahimi * The curve is Edwards448.
1578*62c56f98SSadaf Ebrahimi * The hash function used internally is the first 114 bytes of the
1579*62c56f98SSadaf Ebrahimi * SHAKE256 output.
1580*62c56f98SSadaf Ebrahimi *
1581*62c56f98SSadaf Ebrahimi * This algorithm can be used with psa_sign_message() and
1582*62c56f98SSadaf Ebrahimi * psa_verify_message(). Since there is no prehashing, it cannot be used
1583*62c56f98SSadaf Ebrahimi * with psa_sign_hash() or psa_verify_hash().
1584*62c56f98SSadaf Ebrahimi *
1585*62c56f98SSadaf Ebrahimi * The signature format is the concatenation of R and S as defined by
1586*62c56f98SSadaf Ebrahimi * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1587*62c56f98SSadaf Ebrahimi * string for Ed448).
1588*62c56f98SSadaf Ebrahimi */
1589*62c56f98SSadaf Ebrahimi #define PSA_ALG_PURE_EDDSA ((psa_algorithm_t) 0x06000800)
1590*62c56f98SSadaf Ebrahimi
1591*62c56f98SSadaf Ebrahimi #define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t) 0x06000900)
1592*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HASH_EDDSA(alg) \
1593*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1594*62c56f98SSadaf Ebrahimi
1595*62c56f98SSadaf Ebrahimi /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1596*62c56f98SSadaf Ebrahimi * using SHA-512 and the Edwards25519 curve.
1597*62c56f98SSadaf Ebrahimi *
1598*62c56f98SSadaf Ebrahimi * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1599*62c56f98SSadaf Ebrahimi *
1600*62c56f98SSadaf Ebrahimi * This algorithm is Ed25519 as specified in RFC 8032.
1601*62c56f98SSadaf Ebrahimi * The curve is Edwards25519.
1602*62c56f98SSadaf Ebrahimi * The prehash is SHA-512.
1603*62c56f98SSadaf Ebrahimi * The hash function used internally is SHA-512.
1604*62c56f98SSadaf Ebrahimi *
1605*62c56f98SSadaf Ebrahimi * This is a hash-and-sign algorithm: to calculate a signature,
1606*62c56f98SSadaf Ebrahimi * you can either:
1607*62c56f98SSadaf Ebrahimi * - call psa_sign_message() on the message;
1608*62c56f98SSadaf Ebrahimi * - or calculate the SHA-512 hash of the message
1609*62c56f98SSadaf Ebrahimi * with psa_hash_compute()
1610*62c56f98SSadaf Ebrahimi * or with a multi-part hash operation started with psa_hash_setup(),
1611*62c56f98SSadaf Ebrahimi * using the hash algorithm #PSA_ALG_SHA_512,
1612*62c56f98SSadaf Ebrahimi * then sign the calculated hash with psa_sign_hash().
1613*62c56f98SSadaf Ebrahimi * Verifying a signature is similar, using psa_verify_message() or
1614*62c56f98SSadaf Ebrahimi * psa_verify_hash() instead of the signature function.
1615*62c56f98SSadaf Ebrahimi */
1616*62c56f98SSadaf Ebrahimi #define PSA_ALG_ED25519PH \
1617*62c56f98SSadaf Ebrahimi (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1618*62c56f98SSadaf Ebrahimi
1619*62c56f98SSadaf Ebrahimi /** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1620*62c56f98SSadaf Ebrahimi * using SHAKE256 and the Edwards448 curve.
1621*62c56f98SSadaf Ebrahimi *
1622*62c56f98SSadaf Ebrahimi * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1623*62c56f98SSadaf Ebrahimi *
1624*62c56f98SSadaf Ebrahimi * This algorithm is Ed448 as specified in RFC 8032.
1625*62c56f98SSadaf Ebrahimi * The curve is Edwards448.
1626*62c56f98SSadaf Ebrahimi * The prehash is the first 64 bytes of the SHAKE256 output.
1627*62c56f98SSadaf Ebrahimi * The hash function used internally is the first 114 bytes of the
1628*62c56f98SSadaf Ebrahimi * SHAKE256 output.
1629*62c56f98SSadaf Ebrahimi *
1630*62c56f98SSadaf Ebrahimi * This is a hash-and-sign algorithm: to calculate a signature,
1631*62c56f98SSadaf Ebrahimi * you can either:
1632*62c56f98SSadaf Ebrahimi * - call psa_sign_message() on the message;
1633*62c56f98SSadaf Ebrahimi * - or calculate the first 64 bytes of the SHAKE256 output of the message
1634*62c56f98SSadaf Ebrahimi * with psa_hash_compute()
1635*62c56f98SSadaf Ebrahimi * or with a multi-part hash operation started with psa_hash_setup(),
1636*62c56f98SSadaf Ebrahimi * using the hash algorithm #PSA_ALG_SHAKE256_512,
1637*62c56f98SSadaf Ebrahimi * then sign the calculated hash with psa_sign_hash().
1638*62c56f98SSadaf Ebrahimi * Verifying a signature is similar, using psa_verify_message() or
1639*62c56f98SSadaf Ebrahimi * psa_verify_hash() instead of the signature function.
1640*62c56f98SSadaf Ebrahimi */
1641*62c56f98SSadaf Ebrahimi #define PSA_ALG_ED448PH \
1642*62c56f98SSadaf Ebrahimi (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
1643*62c56f98SSadaf Ebrahimi
1644*62c56f98SSadaf Ebrahimi /* Default definition, to be overridden if the library is extended with
1645*62c56f98SSadaf Ebrahimi * more hash-and-sign algorithms that we want to keep out of this header
1646*62c56f98SSadaf Ebrahimi * file. */
1647*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1648*62c56f98SSadaf Ebrahimi
1649*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a signature algorithm that can be used
1650*62c56f98SSadaf Ebrahimi * with psa_sign_hash() and psa_verify_hash().
1651*62c56f98SSadaf Ebrahimi *
1652*62c56f98SSadaf Ebrahimi * This encompasses all strict hash-and-sign algorithms categorized by
1653*62c56f98SSadaf Ebrahimi * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1654*62c56f98SSadaf Ebrahimi * paradigm more loosely:
1655*62c56f98SSadaf Ebrahimi * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1656*62c56f98SSadaf Ebrahimi * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
1657*62c56f98SSadaf Ebrahimi *
1658*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type psa_algorithm_t).
1659*62c56f98SSadaf Ebrahimi *
1660*62c56f98SSadaf Ebrahimi * \return 1 if alg is a signature algorithm that can be used to sign a
1661*62c56f98SSadaf Ebrahimi * hash. 0 if alg is a signature algorithm that can only be used
1662*62c56f98SSadaf Ebrahimi * to sign a message. 0 if alg is not a signature algorithm.
1663*62c56f98SSadaf Ebrahimi * This macro can return either 0 or 1 if alg is not a
1664*62c56f98SSadaf Ebrahimi * supported algorithm identifier.
1665*62c56f98SSadaf Ebrahimi */
1666*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_SIGN_HASH(alg) \
1667*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1668*62c56f98SSadaf Ebrahimi PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
1669*62c56f98SSadaf Ebrahimi PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1670*62c56f98SSadaf Ebrahimi
1671*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a signature algorithm that can be used
1672*62c56f98SSadaf Ebrahimi * with psa_sign_message() and psa_verify_message().
1673*62c56f98SSadaf Ebrahimi *
1674*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1675*62c56f98SSadaf Ebrahimi *
1676*62c56f98SSadaf Ebrahimi * \return 1 if alg is a signature algorithm that can be used to sign a
1677*62c56f98SSadaf Ebrahimi * message. 0 if \p alg is a signature algorithm that can only be used
1678*62c56f98SSadaf Ebrahimi * to sign an already-calculated hash. 0 if \p alg is not a signature
1679*62c56f98SSadaf Ebrahimi * algorithm. This macro can return either 0 or 1 if \p alg is not a
1680*62c56f98SSadaf Ebrahimi * supported algorithm identifier.
1681*62c56f98SSadaf Ebrahimi */
1682*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_SIGN_MESSAGE(alg) \
1683*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)
1684*62c56f98SSadaf Ebrahimi
1685*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a hash-and-sign algorithm.
1686*62c56f98SSadaf Ebrahimi *
1687*62c56f98SSadaf Ebrahimi * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1688*62c56f98SSadaf Ebrahimi * structured in two parts: first the calculation of a hash in a way that
1689*62c56f98SSadaf Ebrahimi * does not depend on the key, then the calculation of a signature from the
1690*62c56f98SSadaf Ebrahimi * hash value and the key. Hash-and-sign algorithms encode the hash
1691*62c56f98SSadaf Ebrahimi * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1692*62c56f98SSadaf Ebrahimi * to extract this algorithm.
1693*62c56f98SSadaf Ebrahimi *
1694*62c56f98SSadaf Ebrahimi * Thus, for a hash-and-sign algorithm,
1695*62c56f98SSadaf Ebrahimi * `psa_sign_message(key, alg, input, ...)` is equivalent to
1696*62c56f98SSadaf Ebrahimi * ```
1697*62c56f98SSadaf Ebrahimi * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1698*62c56f98SSadaf Ebrahimi * psa_sign_hash(key, alg, hash, ..., signature, ...);
1699*62c56f98SSadaf Ebrahimi * ```
1700*62c56f98SSadaf Ebrahimi * Most usefully, separating the hash from the signature allows the hash
1701*62c56f98SSadaf Ebrahimi * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1702*62c56f98SSadaf Ebrahimi * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1703*62c56f98SSadaf Ebrahimi * calculating the hash and then calling psa_verify_hash().
1704*62c56f98SSadaf Ebrahimi *
1705*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1706*62c56f98SSadaf Ebrahimi *
1707*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1708*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
1709*62c56f98SSadaf Ebrahimi * algorithm identifier.
1710*62c56f98SSadaf Ebrahimi */
1711*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1712*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_SIGN_HASH(alg) && \
1713*62c56f98SSadaf Ebrahimi ((alg) & PSA_ALG_HASH_MASK) != 0)
1714*62c56f98SSadaf Ebrahimi
1715*62c56f98SSadaf Ebrahimi /** Get the hash used by a hash-and-sign signature algorithm.
1716*62c56f98SSadaf Ebrahimi *
1717*62c56f98SSadaf Ebrahimi * A hash-and-sign algorithm is a signature algorithm which is
1718*62c56f98SSadaf Ebrahimi * composed of two phases: first a hashing phase which does not use
1719*62c56f98SSadaf Ebrahimi * the key and produces a hash of the input message, then a signing
1720*62c56f98SSadaf Ebrahimi * phase which only uses the hash and the key and not the message
1721*62c56f98SSadaf Ebrahimi * itself.
1722*62c56f98SSadaf Ebrahimi *
1723*62c56f98SSadaf Ebrahimi * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1724*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_SIGN(\p alg) is true).
1725*62c56f98SSadaf Ebrahimi *
1726*62c56f98SSadaf Ebrahimi * \return The underlying hash algorithm if \p alg is a hash-and-sign
1727*62c56f98SSadaf Ebrahimi * algorithm.
1728*62c56f98SSadaf Ebrahimi * \return 0 if \p alg is a signature algorithm that does not
1729*62c56f98SSadaf Ebrahimi * follow the hash-and-sign structure.
1730*62c56f98SSadaf Ebrahimi * \return Unspecified if \p alg is not a signature algorithm or
1731*62c56f98SSadaf Ebrahimi * if it is not supported by the implementation.
1732*62c56f98SSadaf Ebrahimi */
1733*62c56f98SSadaf Ebrahimi #define PSA_ALG_SIGN_GET_HASH(alg) \
1734*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1735*62c56f98SSadaf Ebrahimi ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1736*62c56f98SSadaf Ebrahimi 0)
1737*62c56f98SSadaf Ebrahimi
1738*62c56f98SSadaf Ebrahimi /** RSA PKCS#1 v1.5 encryption.
1739*62c56f98SSadaf Ebrahimi *
1740*62c56f98SSadaf Ebrahimi * \warning Calling psa_asymmetric_decrypt() with this algorithm as a
1741*62c56f98SSadaf Ebrahimi * parameter is considered an inherently dangerous function
1742*62c56f98SSadaf Ebrahimi * (CWE-242). Unless it is used in a side channel free and safe
1743*62c56f98SSadaf Ebrahimi * way (eg. implementing the TLS protocol as per 7.4.7.1 of
1744*62c56f98SSadaf Ebrahimi * RFC 5246), the calling code is vulnerable.
1745*62c56f98SSadaf Ebrahimi *
1746*62c56f98SSadaf Ebrahimi */
1747*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t) 0x07000200)
1748*62c56f98SSadaf Ebrahimi
1749*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t) 0x07000300)
1750*62c56f98SSadaf Ebrahimi /** RSA OAEP encryption.
1751*62c56f98SSadaf Ebrahimi *
1752*62c56f98SSadaf Ebrahimi * This is the encryption scheme defined by RFC 8017
1753*62c56f98SSadaf Ebrahimi * (PKCS#1: RSA Cryptography Specifications) under the name
1754*62c56f98SSadaf Ebrahimi * RSAES-OAEP, with the message generation function MGF1.
1755*62c56f98SSadaf Ebrahimi *
1756*62c56f98SSadaf Ebrahimi * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1757*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1758*62c56f98SSadaf Ebrahimi * for MGF1.
1759*62c56f98SSadaf Ebrahimi *
1760*62c56f98SSadaf Ebrahimi * \return The corresponding RSA OAEP encryption algorithm.
1761*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1762*62c56f98SSadaf Ebrahimi * hash algorithm.
1763*62c56f98SSadaf Ebrahimi */
1764*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_OAEP(hash_alg) \
1765*62c56f98SSadaf Ebrahimi (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1766*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RSA_OAEP(alg) \
1767*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1768*62c56f98SSadaf Ebrahimi #define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1769*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_RSA_OAEP(alg) ? \
1770*62c56f98SSadaf Ebrahimi ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1771*62c56f98SSadaf Ebrahimi 0)
1772*62c56f98SSadaf Ebrahimi
1773*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF_BASE ((psa_algorithm_t) 0x08000100)
1774*62c56f98SSadaf Ebrahimi /** Macro to build an HKDF algorithm.
1775*62c56f98SSadaf Ebrahimi *
1776*62c56f98SSadaf Ebrahimi * For example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)` is HKDF using HMAC-SHA-256.
1777*62c56f98SSadaf Ebrahimi *
1778*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the following inputs:
1779*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
1780*62c56f98SSadaf Ebrahimi * It is optional; if omitted, the derivation uses an empty salt.
1781*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1782*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1783*62c56f98SSadaf Ebrahimi * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1784*62c56f98SSadaf Ebrahimi * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
1785*62c56f98SSadaf Ebrahimi * starting to generate output.
1786*62c56f98SSadaf Ebrahimi *
1787*62c56f98SSadaf Ebrahimi * \warning HKDF processes the salt as follows: first hash it with hash_alg
1788*62c56f98SSadaf Ebrahimi * if the salt is longer than the block size of the hash algorithm; then
1789*62c56f98SSadaf Ebrahimi * pad with null bytes up to the block size. As a result, it is possible
1790*62c56f98SSadaf Ebrahimi * for distinct salt inputs to result in the same outputs. To ensure
1791*62c56f98SSadaf Ebrahimi * unique outputs, it is recommended to use a fixed length for salt values.
1792*62c56f98SSadaf Ebrahimi *
1793*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1794*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1795*62c56f98SSadaf Ebrahimi *
1796*62c56f98SSadaf Ebrahimi * \return The corresponding HKDF algorithm.
1797*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1798*62c56f98SSadaf Ebrahimi * hash algorithm.
1799*62c56f98SSadaf Ebrahimi */
1800*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF(hash_alg) \
1801*62c56f98SSadaf Ebrahimi (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1802*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an HKDF algorithm.
1803*62c56f98SSadaf Ebrahimi *
1804*62c56f98SSadaf Ebrahimi * HKDF is a family of key derivation algorithms that are based on a hash
1805*62c56f98SSadaf Ebrahimi * function and the HMAC construction.
1806*62c56f98SSadaf Ebrahimi *
1807*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1808*62c56f98SSadaf Ebrahimi *
1809*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1810*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
1811*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
1812*62c56f98SSadaf Ebrahimi */
1813*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HKDF(alg) \
1814*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1815*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1816*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1817*62c56f98SSadaf Ebrahimi
1818*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF_EXTRACT_BASE ((psa_algorithm_t) 0x08000400)
1819*62c56f98SSadaf Ebrahimi /** Macro to build an HKDF-Extract algorithm.
1820*62c56f98SSadaf Ebrahimi *
1821*62c56f98SSadaf Ebrahimi * For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA_256)` is
1822*62c56f98SSadaf Ebrahimi * HKDF-Extract using HMAC-SHA-256.
1823*62c56f98SSadaf Ebrahimi *
1824*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the following inputs:
1825*62c56f98SSadaf Ebrahimi * - PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1826*62c56f98SSadaf Ebrahimi * - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the
1827*62c56f98SSadaf Ebrahimi * "extract" step.
1828*62c56f98SSadaf Ebrahimi * The inputs are mandatory and must be passed in the order above.
1829*62c56f98SSadaf Ebrahimi * Each input may only be passed once.
1830*62c56f98SSadaf Ebrahimi *
1831*62c56f98SSadaf Ebrahimi * \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF
1832*62c56f98SSadaf Ebrahimi * should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided
1833*62c56f98SSadaf Ebrahimi * as a separate algorithm for the sake of protocols that use it as a
1834*62c56f98SSadaf Ebrahimi * building block. It may also be a slight performance optimization
1835*62c56f98SSadaf Ebrahimi * in applications that use HKDF with the same salt and key but many
1836*62c56f98SSadaf Ebrahimi * different info strings.
1837*62c56f98SSadaf Ebrahimi *
1838*62c56f98SSadaf Ebrahimi * \warning HKDF processes the salt as follows: first hash it with hash_alg
1839*62c56f98SSadaf Ebrahimi * if the salt is longer than the block size of the hash algorithm; then
1840*62c56f98SSadaf Ebrahimi * pad with null bytes up to the block size. As a result, it is possible
1841*62c56f98SSadaf Ebrahimi * for distinct salt inputs to result in the same outputs. To ensure
1842*62c56f98SSadaf Ebrahimi * unique outputs, it is recommended to use a fixed length for salt values.
1843*62c56f98SSadaf Ebrahimi *
1844*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1845*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1846*62c56f98SSadaf Ebrahimi *
1847*62c56f98SSadaf Ebrahimi * \return The corresponding HKDF-Extract algorithm.
1848*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1849*62c56f98SSadaf Ebrahimi * hash algorithm.
1850*62c56f98SSadaf Ebrahimi */
1851*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF_EXTRACT(hash_alg) \
1852*62c56f98SSadaf Ebrahimi (PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1853*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an HKDF-Extract algorithm.
1854*62c56f98SSadaf Ebrahimi *
1855*62c56f98SSadaf Ebrahimi * HKDF-Extract is a family of key derivation algorithms that are based
1856*62c56f98SSadaf Ebrahimi * on a hash function and the HMAC construction.
1857*62c56f98SSadaf Ebrahimi *
1858*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1859*62c56f98SSadaf Ebrahimi *
1860*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.
1861*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
1862*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
1863*62c56f98SSadaf Ebrahimi */
1864*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HKDF_EXTRACT(alg) \
1865*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)
1866*62c56f98SSadaf Ebrahimi
1867*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF_EXPAND_BASE ((psa_algorithm_t) 0x08000500)
1868*62c56f98SSadaf Ebrahimi /** Macro to build an HKDF-Expand algorithm.
1869*62c56f98SSadaf Ebrahimi *
1870*62c56f98SSadaf Ebrahimi * For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA_256)` is
1871*62c56f98SSadaf Ebrahimi * HKDF-Expand using HMAC-SHA-256.
1872*62c56f98SSadaf Ebrahimi *
1873*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the following inputs:
1874*62c56f98SSadaf Ebrahimi * - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).
1875*62c56f98SSadaf Ebrahimi * - PSA_KEY_DERIVATION_INPUT_INFO is the info string.
1876*62c56f98SSadaf Ebrahimi *
1877*62c56f98SSadaf Ebrahimi * The inputs are mandatory and must be passed in the order above.
1878*62c56f98SSadaf Ebrahimi * Each input may only be passed once.
1879*62c56f98SSadaf Ebrahimi *
1880*62c56f98SSadaf Ebrahimi * \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`
1881*62c56f98SSadaf Ebrahimi * should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as
1882*62c56f98SSadaf Ebrahimi * a separate algorithm for the sake of protocols that use it as a building
1883*62c56f98SSadaf Ebrahimi * block. It may also be a slight performance optimization in applications
1884*62c56f98SSadaf Ebrahimi * that use HKDF with the same salt and key but many different info strings.
1885*62c56f98SSadaf Ebrahimi *
1886*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1887*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1888*62c56f98SSadaf Ebrahimi *
1889*62c56f98SSadaf Ebrahimi * \return The corresponding HKDF-Expand algorithm.
1890*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1891*62c56f98SSadaf Ebrahimi * hash algorithm.
1892*62c56f98SSadaf Ebrahimi */
1893*62c56f98SSadaf Ebrahimi #define PSA_ALG_HKDF_EXPAND(hash_alg) \
1894*62c56f98SSadaf Ebrahimi (PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1895*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an HKDF-Expand algorithm.
1896*62c56f98SSadaf Ebrahimi *
1897*62c56f98SSadaf Ebrahimi * HKDF-Expand is a family of key derivation algorithms that are based
1898*62c56f98SSadaf Ebrahimi * on a hash function and the HMAC construction.
1899*62c56f98SSadaf Ebrahimi *
1900*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1901*62c56f98SSadaf Ebrahimi *
1902*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.
1903*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
1904*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
1905*62c56f98SSadaf Ebrahimi */
1906*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_HKDF_EXPAND(alg) \
1907*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1908*62c56f98SSadaf Ebrahimi
1909*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an HKDF or HKDF-Extract or
1910*62c56f98SSadaf Ebrahimi * HKDF-Expand algorithm.
1911*62c56f98SSadaf Ebrahimi *
1912*62c56f98SSadaf Ebrahimi *
1913*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1914*62c56f98SSadaf Ebrahimi *
1915*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.
1916*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
1917*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
1918*62c56f98SSadaf Ebrahimi */
1919*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_ANY_HKDF(alg) \
1920*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE || \
1921*62c56f98SSadaf Ebrahimi ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE || \
1922*62c56f98SSadaf Ebrahimi ((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)
1923*62c56f98SSadaf Ebrahimi
1924*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t) 0x08000200)
1925*62c56f98SSadaf Ebrahimi /** Macro to build a TLS-1.2 PRF algorithm.
1926*62c56f98SSadaf Ebrahimi *
1927*62c56f98SSadaf Ebrahimi * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1928*62c56f98SSadaf Ebrahimi * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1929*62c56f98SSadaf Ebrahimi * used with either SHA-256 or SHA-384.
1930*62c56f98SSadaf Ebrahimi *
1931*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the following inputs, which must be
1932*62c56f98SSadaf Ebrahimi * passed in the order given here:
1933*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1934*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1935*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1936*62c56f98SSadaf Ebrahimi *
1937*62c56f98SSadaf Ebrahimi * For the application to TLS-1.2 key expansion, the seed is the
1938*62c56f98SSadaf Ebrahimi * concatenation of ServerHello.Random + ClientHello.Random,
1939*62c56f98SSadaf Ebrahimi * and the label is "key expansion".
1940*62c56f98SSadaf Ebrahimi *
1941*62c56f98SSadaf Ebrahimi * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256)` represents the
1942*62c56f98SSadaf Ebrahimi * TLS 1.2 PRF using HMAC-SHA-256.
1943*62c56f98SSadaf Ebrahimi *
1944*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1945*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1946*62c56f98SSadaf Ebrahimi *
1947*62c56f98SSadaf Ebrahimi * \return The corresponding TLS-1.2 PRF algorithm.
1948*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
1949*62c56f98SSadaf Ebrahimi * hash algorithm.
1950*62c56f98SSadaf Ebrahimi */
1951*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_PRF(hash_alg) \
1952*62c56f98SSadaf Ebrahimi (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1953*62c56f98SSadaf Ebrahimi
1954*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1955*62c56f98SSadaf Ebrahimi *
1956*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1957*62c56f98SSadaf Ebrahimi *
1958*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1959*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
1960*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
1961*62c56f98SSadaf Ebrahimi */
1962*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_TLS12_PRF(alg) \
1963*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1964*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1965*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1966*62c56f98SSadaf Ebrahimi
1967*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t) 0x08000300)
1968*62c56f98SSadaf Ebrahimi /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1969*62c56f98SSadaf Ebrahimi *
1970*62c56f98SSadaf Ebrahimi * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1971*62c56f98SSadaf Ebrahimi * from the PreSharedKey (PSK) through the application of padding
1972*62c56f98SSadaf Ebrahimi * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1973*62c56f98SSadaf Ebrahimi * The latter is based on HMAC and can be used with either SHA-256
1974*62c56f98SSadaf Ebrahimi * or SHA-384.
1975*62c56f98SSadaf Ebrahimi *
1976*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the following inputs, which must be
1977*62c56f98SSadaf Ebrahimi * passed in the order given here:
1978*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
1979*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
1980*62c56f98SSadaf Ebrahimi * computation of the premaster secret. This input is optional;
1981*62c56f98SSadaf Ebrahimi * if omitted, it defaults to a string of null bytes with the same length
1982*62c56f98SSadaf Ebrahimi * as the secret (PSK) input.
1983*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1984*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
1985*62c56f98SSadaf Ebrahimi *
1986*62c56f98SSadaf Ebrahimi * For the application to TLS-1.2, the seed (which is
1987*62c56f98SSadaf Ebrahimi * forwarded to the TLS-1.2 PRF) is the concatenation of the
1988*62c56f98SSadaf Ebrahimi * ClientHello.Random + ServerHello.Random,
1989*62c56f98SSadaf Ebrahimi * the label is "master secret" or "extended master secret" and
1990*62c56f98SSadaf Ebrahimi * the other secret depends on the key exchange specified in the cipher suite:
1991*62c56f98SSadaf Ebrahimi * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
1992*62c56f98SSadaf Ebrahimi * PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
1993*62c56f98SSadaf Ebrahimi * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
1994*62c56f98SSadaf Ebrahimi * (RFC 5489, Section 2), the other secret should be the output of the
1995*62c56f98SSadaf Ebrahimi * PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
1996*62c56f98SSadaf Ebrahimi * The recommended way to pass this input is to use a key derivation
1997*62c56f98SSadaf Ebrahimi * algorithm constructed as
1998*62c56f98SSadaf Ebrahimi * PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
1999*62c56f98SSadaf Ebrahimi * and to call psa_key_derivation_key_agreement(). Alternatively,
2000*62c56f98SSadaf Ebrahimi * this input may be an output of `psa_raw_key_agreement()` passed with
2001*62c56f98SSadaf Ebrahimi * psa_key_derivation_input_bytes(), or an equivalent input passed with
2002*62c56f98SSadaf Ebrahimi * psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
2003*62c56f98SSadaf Ebrahimi * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
2004*62c56f98SSadaf Ebrahimi * should be the 48-byte client challenge (the PreMasterSecret of
2005*62c56f98SSadaf Ebrahimi * (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
2006*62c56f98SSadaf Ebrahimi * a 46-byte random string chosen by the client. On the server, this is
2007*62c56f98SSadaf Ebrahimi * typically an output of psa_asymmetric_decrypt() using
2008*62c56f98SSadaf Ebrahimi * PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
2009*62c56f98SSadaf Ebrahimi * with `psa_key_derivation_input_bytes()`.
2010*62c56f98SSadaf Ebrahimi *
2011*62c56f98SSadaf Ebrahimi * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256)` represents the
2012*62c56f98SSadaf Ebrahimi * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
2013*62c56f98SSadaf Ebrahimi *
2014*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
2015*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
2016*62c56f98SSadaf Ebrahimi *
2017*62c56f98SSadaf Ebrahimi * \return The corresponding TLS-1.2 PSK to MS algorithm.
2018*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
2019*62c56f98SSadaf Ebrahimi * hash algorithm.
2020*62c56f98SSadaf Ebrahimi */
2021*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
2022*62c56f98SSadaf Ebrahimi (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2023*62c56f98SSadaf Ebrahimi
2024*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
2025*62c56f98SSadaf Ebrahimi *
2026*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2027*62c56f98SSadaf Ebrahimi *
2028*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
2029*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
2030*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
2031*62c56f98SSadaf Ebrahimi */
2032*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
2033*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
2034*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
2035*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
2036*62c56f98SSadaf Ebrahimi
2037*62c56f98SSadaf Ebrahimi /* The TLS 1.2 ECJPAKE-to-PMS KDF. It takes the shared secret K (an EC point
2038*62c56f98SSadaf Ebrahimi * in case of EC J-PAKE) and calculates SHA256(K.X) that the rest of TLS 1.2
2039*62c56f98SSadaf Ebrahimi * will use to derive the session secret, as defined by step 2 of
2040*62c56f98SSadaf Ebrahimi * https://datatracker.ietf.org/doc/html/draft-cragie-tls-ecjpake-01#section-8.7.
2041*62c56f98SSadaf Ebrahimi * Uses PSA_ALG_SHA_256.
2042*62c56f98SSadaf Ebrahimi * This function takes a single input:
2043*62c56f98SSadaf Ebrahimi * #PSA_KEY_DERIVATION_INPUT_SECRET is the shared secret K from EC J-PAKE.
2044*62c56f98SSadaf Ebrahimi * The only supported curve is secp256r1 (the 256-bit curve in
2045*62c56f98SSadaf Ebrahimi * #PSA_ECC_FAMILY_SECP_R1), so the input must be exactly 65 bytes.
2046*62c56f98SSadaf Ebrahimi * The output has to be read as a single chunk of 32 bytes, defined as
2047*62c56f98SSadaf Ebrahimi * PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE.
2048*62c56f98SSadaf Ebrahimi */
2049*62c56f98SSadaf Ebrahimi #define PSA_ALG_TLS12_ECJPAKE_TO_PMS ((psa_algorithm_t) 0x08000609)
2050*62c56f98SSadaf Ebrahimi
2051*62c56f98SSadaf Ebrahimi /* This flag indicates whether the key derivation algorithm is suitable for
2052*62c56f98SSadaf Ebrahimi * use on low-entropy secrets such as password - these algorithms are also
2053*62c56f98SSadaf Ebrahimi * known as key stretching or password hashing schemes. These are also the
2054*62c56f98SSadaf Ebrahimi * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
2055*62c56f98SSadaf Ebrahimi *
2056*62c56f98SSadaf Ebrahimi * Those algorithms cannot be combined with a key agreement algorithm.
2057*62c56f98SSadaf Ebrahimi */
2058*62c56f98SSadaf Ebrahimi #define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t) 0x00800000)
2059*62c56f98SSadaf Ebrahimi
2060*62c56f98SSadaf Ebrahimi #define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t) 0x08800100)
2061*62c56f98SSadaf Ebrahimi /** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
2062*62c56f98SSadaf Ebrahimi *
2063*62c56f98SSadaf Ebrahimi * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2064*62c56f98SSadaf Ebrahimi * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
2065*62c56f98SSadaf Ebrahimi * HMAC with the specified hash.
2066*62c56f98SSadaf Ebrahimi * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA_256)` specifies PBKDF2
2067*62c56f98SSadaf Ebrahimi * using the PRF HMAC-SHA-256.
2068*62c56f98SSadaf Ebrahimi *
2069*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the following inputs, which must be
2070*62c56f98SSadaf Ebrahimi * provided in the following order:
2071*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
2072*62c56f98SSadaf Ebrahimi * This input step must be used exactly once.
2073*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
2074*62c56f98SSadaf Ebrahimi * This input step must be used one or more times; if used several times, the
2075*62c56f98SSadaf Ebrahimi * inputs will be concatenated. This can be used to build the final salt
2076*62c56f98SSadaf Ebrahimi * from multiple sources, both public and secret (also known as pepper).
2077*62c56f98SSadaf Ebrahimi * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
2078*62c56f98SSadaf Ebrahimi * This input step must be used exactly once.
2079*62c56f98SSadaf Ebrahimi *
2080*62c56f98SSadaf Ebrahimi * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
2081*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_HASH(\p hash_alg) is true).
2082*62c56f98SSadaf Ebrahimi *
2083*62c56f98SSadaf Ebrahimi * \return The corresponding PBKDF2-HMAC-XXX algorithm.
2084*62c56f98SSadaf Ebrahimi * \return Unspecified if \p hash_alg is not a supported
2085*62c56f98SSadaf Ebrahimi * hash algorithm.
2086*62c56f98SSadaf Ebrahimi */
2087*62c56f98SSadaf Ebrahimi #define PSA_ALG_PBKDF2_HMAC(hash_alg) \
2088*62c56f98SSadaf Ebrahimi (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
2089*62c56f98SSadaf Ebrahimi
2090*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
2091*62c56f98SSadaf Ebrahimi *
2092*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2093*62c56f98SSadaf Ebrahimi *
2094*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
2095*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
2096*62c56f98SSadaf Ebrahimi * key derivation algorithm identifier.
2097*62c56f98SSadaf Ebrahimi */
2098*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_PBKDF2_HMAC(alg) \
2099*62c56f98SSadaf Ebrahimi (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
2100*62c56f98SSadaf Ebrahimi #define PSA_ALG_PBKDF2_HMAC_GET_HASH(pbkdf2_alg) \
2101*62c56f98SSadaf Ebrahimi (PSA_ALG_CATEGORY_HASH | ((pbkdf2_alg) & PSA_ALG_HASH_MASK))
2102*62c56f98SSadaf Ebrahimi /** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
2103*62c56f98SSadaf Ebrahimi *
2104*62c56f98SSadaf Ebrahimi * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
2105*62c56f98SSadaf Ebrahimi * This macro specifies the PBKDF2 algorithm constructed using the
2106*62c56f98SSadaf Ebrahimi * AES-CMAC-PRF-128 PRF specified by RFC 4615.
2107*62c56f98SSadaf Ebrahimi *
2108*62c56f98SSadaf Ebrahimi * This key derivation algorithm uses the same inputs as
2109*62c56f98SSadaf Ebrahimi * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
2110*62c56f98SSadaf Ebrahimi */
2111*62c56f98SSadaf Ebrahimi #define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t) 0x08800200)
2112*62c56f98SSadaf Ebrahimi
2113*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_PBKDF2(kdf_alg) \
2114*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg) || \
2115*62c56f98SSadaf Ebrahimi ((kdf_alg) == PSA_ALG_PBKDF2_AES_CMAC_PRF_128))
2116*62c56f98SSadaf Ebrahimi
2117*62c56f98SSadaf Ebrahimi #define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t) 0xfe00ffff)
2118*62c56f98SSadaf Ebrahimi #define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t) 0xffff0000)
2119*62c56f98SSadaf Ebrahimi
2120*62c56f98SSadaf Ebrahimi /** Macro to build a combined algorithm that chains a key agreement with
2121*62c56f98SSadaf Ebrahimi * a key derivation.
2122*62c56f98SSadaf Ebrahimi *
2123*62c56f98SSadaf Ebrahimi * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
2124*62c56f98SSadaf Ebrahimi * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
2125*62c56f98SSadaf Ebrahimi * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
2126*62c56f98SSadaf Ebrahimi * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
2127*62c56f98SSadaf Ebrahimi *
2128*62c56f98SSadaf Ebrahimi * \return The corresponding key agreement and derivation
2129*62c56f98SSadaf Ebrahimi * algorithm.
2130*62c56f98SSadaf Ebrahimi * \return Unspecified if \p ka_alg is not a supported
2131*62c56f98SSadaf Ebrahimi * key agreement algorithm or \p kdf_alg is not a
2132*62c56f98SSadaf Ebrahimi * supported key derivation algorithm.
2133*62c56f98SSadaf Ebrahimi */
2134*62c56f98SSadaf Ebrahimi #define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
2135*62c56f98SSadaf Ebrahimi ((ka_alg) | (kdf_alg))
2136*62c56f98SSadaf Ebrahimi
2137*62c56f98SSadaf Ebrahimi #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
2138*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
2139*62c56f98SSadaf Ebrahimi
2140*62c56f98SSadaf Ebrahimi #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
2141*62c56f98SSadaf Ebrahimi (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
2142*62c56f98SSadaf Ebrahimi
2143*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a raw key agreement algorithm.
2144*62c56f98SSadaf Ebrahimi *
2145*62c56f98SSadaf Ebrahimi * A raw key agreement algorithm is one that does not specify
2146*62c56f98SSadaf Ebrahimi * a key derivation function.
2147*62c56f98SSadaf Ebrahimi * Usually, raw key agreement algorithms are constructed directly with
2148*62c56f98SSadaf Ebrahimi * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
2149*62c56f98SSadaf Ebrahimi * constructed with #PSA_ALG_KEY_AGREEMENT().
2150*62c56f98SSadaf Ebrahimi *
2151*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2152*62c56f98SSadaf Ebrahimi *
2153*62c56f98SSadaf Ebrahimi * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
2154*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \p alg is not a supported
2155*62c56f98SSadaf Ebrahimi * algorithm identifier.
2156*62c56f98SSadaf Ebrahimi */
2157*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
2158*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
2159*62c56f98SSadaf Ebrahimi PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
2160*62c56f98SSadaf Ebrahimi
2161*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
2162*62c56f98SSadaf Ebrahimi ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
2163*62c56f98SSadaf Ebrahimi
2164*62c56f98SSadaf Ebrahimi /** The finite-field Diffie-Hellman (DH) key agreement algorithm.
2165*62c56f98SSadaf Ebrahimi *
2166*62c56f98SSadaf Ebrahimi * The shared secret produced by key agreement is
2167*62c56f98SSadaf Ebrahimi * `g^{ab}` in big-endian format.
2168*62c56f98SSadaf Ebrahimi * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
2169*62c56f98SSadaf Ebrahimi * in bits.
2170*62c56f98SSadaf Ebrahimi */
2171*62c56f98SSadaf Ebrahimi #define PSA_ALG_FFDH ((psa_algorithm_t) 0x09010000)
2172*62c56f98SSadaf Ebrahimi
2173*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
2174*62c56f98SSadaf Ebrahimi *
2175*62c56f98SSadaf Ebrahimi * This includes the raw finite field Diffie-Hellman algorithm as well as
2176*62c56f98SSadaf Ebrahimi * finite-field Diffie-Hellman followed by any supporter key derivation
2177*62c56f98SSadaf Ebrahimi * algorithm.
2178*62c56f98SSadaf Ebrahimi *
2179*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2180*62c56f98SSadaf Ebrahimi *
2181*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
2182*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
2183*62c56f98SSadaf Ebrahimi * key agreement algorithm identifier.
2184*62c56f98SSadaf Ebrahimi */
2185*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_FFDH(alg) \
2186*62c56f98SSadaf Ebrahimi (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
2187*62c56f98SSadaf Ebrahimi
2188*62c56f98SSadaf Ebrahimi /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
2189*62c56f98SSadaf Ebrahimi *
2190*62c56f98SSadaf Ebrahimi * The shared secret produced by key agreement is the x-coordinate of
2191*62c56f98SSadaf Ebrahimi * the shared secret point. It is always `ceiling(m / 8)` bytes long where
2192*62c56f98SSadaf Ebrahimi * `m` is the bit size associated with the curve, i.e. the bit size of the
2193*62c56f98SSadaf Ebrahimi * order of the curve's coordinate field. When `m` is not a multiple of 8,
2194*62c56f98SSadaf Ebrahimi * the byte containing the most significant bit of the shared secret
2195*62c56f98SSadaf Ebrahimi * is padded with zero bits. The byte order is either little-endian
2196*62c56f98SSadaf Ebrahimi * or big-endian depending on the curve type.
2197*62c56f98SSadaf Ebrahimi *
2198*62c56f98SSadaf Ebrahimi * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
2199*62c56f98SSadaf Ebrahimi * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2200*62c56f98SSadaf Ebrahimi * in little-endian byte order.
2201*62c56f98SSadaf Ebrahimi * The bit size is 448 for Curve448 and 255 for Curve25519.
2202*62c56f98SSadaf Ebrahimi * - For Weierstrass curves over prime fields (curve types
2203*62c56f98SSadaf Ebrahimi * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
2204*62c56f98SSadaf Ebrahimi * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2205*62c56f98SSadaf Ebrahimi * in big-endian byte order.
2206*62c56f98SSadaf Ebrahimi * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
2207*62c56f98SSadaf Ebrahimi * - For Weierstrass curves over binary fields (curve types
2208*62c56f98SSadaf Ebrahimi * `PSA_ECC_FAMILY_SECTXXX`),
2209*62c56f98SSadaf Ebrahimi * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2210*62c56f98SSadaf Ebrahimi * in big-endian byte order.
2211*62c56f98SSadaf Ebrahimi * The bit size is `m` for the field `F_{2^m}`.
2212*62c56f98SSadaf Ebrahimi */
2213*62c56f98SSadaf Ebrahimi #define PSA_ALG_ECDH ((psa_algorithm_t) 0x09020000)
2214*62c56f98SSadaf Ebrahimi
2215*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2216*62c56f98SSadaf Ebrahimi * algorithm.
2217*62c56f98SSadaf Ebrahimi *
2218*62c56f98SSadaf Ebrahimi * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2219*62c56f98SSadaf Ebrahimi * elliptic curve Diffie-Hellman followed by any supporter key derivation
2220*62c56f98SSadaf Ebrahimi * algorithm.
2221*62c56f98SSadaf Ebrahimi *
2222*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2223*62c56f98SSadaf Ebrahimi *
2224*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2225*62c56f98SSadaf Ebrahimi * 0 otherwise.
2226*62c56f98SSadaf Ebrahimi * This macro may return either 0 or 1 if \c alg is not a supported
2227*62c56f98SSadaf Ebrahimi * key agreement algorithm identifier.
2228*62c56f98SSadaf Ebrahimi */
2229*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_ECDH(alg) \
2230*62c56f98SSadaf Ebrahimi (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
2231*62c56f98SSadaf Ebrahimi
2232*62c56f98SSadaf Ebrahimi /** Whether the specified algorithm encoding is a wildcard.
2233*62c56f98SSadaf Ebrahimi *
2234*62c56f98SSadaf Ebrahimi * Wildcard values may only be used to set the usage algorithm field in
2235*62c56f98SSadaf Ebrahimi * a policy, not to perform an operation.
2236*62c56f98SSadaf Ebrahimi *
2237*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2238*62c56f98SSadaf Ebrahimi *
2239*62c56f98SSadaf Ebrahimi * \return 1 if \c alg is a wildcard algorithm encoding.
2240*62c56f98SSadaf Ebrahimi * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2241*62c56f98SSadaf Ebrahimi * an operation).
2242*62c56f98SSadaf Ebrahimi * \return This macro may return either 0 or 1 if \c alg is not a supported
2243*62c56f98SSadaf Ebrahimi * algorithm identifier.
2244*62c56f98SSadaf Ebrahimi */
2245*62c56f98SSadaf Ebrahimi #define PSA_ALG_IS_WILDCARD(alg) \
2246*62c56f98SSadaf Ebrahimi (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
2247*62c56f98SSadaf Ebrahimi PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
2248*62c56f98SSadaf Ebrahimi PSA_ALG_IS_MAC(alg) ? \
2249*62c56f98SSadaf Ebrahimi (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
2250*62c56f98SSadaf Ebrahimi PSA_ALG_IS_AEAD(alg) ? \
2251*62c56f98SSadaf Ebrahimi (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
2252*62c56f98SSadaf Ebrahimi (alg) == PSA_ALG_ANY_HASH)
2253*62c56f98SSadaf Ebrahimi
2254*62c56f98SSadaf Ebrahimi /** Get the hash used by a composite algorithm.
2255*62c56f98SSadaf Ebrahimi *
2256*62c56f98SSadaf Ebrahimi * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2257*62c56f98SSadaf Ebrahimi *
2258*62c56f98SSadaf Ebrahimi * \return The underlying hash algorithm if alg is a composite algorithm that
2259*62c56f98SSadaf Ebrahimi * uses a hash algorithm.
2260*62c56f98SSadaf Ebrahimi *
2261*62c56f98SSadaf Ebrahimi * \return \c 0 if alg is not a composite algorithm that uses a hash.
2262*62c56f98SSadaf Ebrahimi */
2263*62c56f98SSadaf Ebrahimi #define PSA_ALG_GET_HASH(alg) \
2264*62c56f98SSadaf Ebrahimi (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t) 0) : 0x02000000 | ((alg) & 0x000000ff))
2265*62c56f98SSadaf Ebrahimi
2266*62c56f98SSadaf Ebrahimi /**@}*/
2267*62c56f98SSadaf Ebrahimi
2268*62c56f98SSadaf Ebrahimi /** \defgroup key_lifetimes Key lifetimes
2269*62c56f98SSadaf Ebrahimi * @{
2270*62c56f98SSadaf Ebrahimi */
2271*62c56f98SSadaf Ebrahimi
2272*62c56f98SSadaf Ebrahimi /* Note that location and persistence level values are embedded in the
2273*62c56f98SSadaf Ebrahimi * persistent key store, as part of key metadata. As a consequence, they
2274*62c56f98SSadaf Ebrahimi * must not be changed (unless the storage format version changes).
2275*62c56f98SSadaf Ebrahimi */
2276*62c56f98SSadaf Ebrahimi
2277*62c56f98SSadaf Ebrahimi /** The default lifetime for volatile keys.
2278*62c56f98SSadaf Ebrahimi *
2279*62c56f98SSadaf Ebrahimi * A volatile key only exists as long as the identifier to it is not destroyed.
2280*62c56f98SSadaf Ebrahimi * The key material is guaranteed to be erased on a power reset.
2281*62c56f98SSadaf Ebrahimi *
2282*62c56f98SSadaf Ebrahimi * A key with this lifetime is typically stored in the RAM area of the
2283*62c56f98SSadaf Ebrahimi * PSA Crypto subsystem. However this is an implementation choice.
2284*62c56f98SSadaf Ebrahimi * If an implementation stores data about the key in a non-volatile memory,
2285*62c56f98SSadaf Ebrahimi * it must release all the resources associated with the key and erase the
2286*62c56f98SSadaf Ebrahimi * key material if the calling application terminates.
2287*62c56f98SSadaf Ebrahimi */
2288*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t) 0x00000000)
2289*62c56f98SSadaf Ebrahimi
2290*62c56f98SSadaf Ebrahimi /** The default lifetime for persistent keys.
2291*62c56f98SSadaf Ebrahimi *
2292*62c56f98SSadaf Ebrahimi * A persistent key remains in storage until it is explicitly destroyed or
2293*62c56f98SSadaf Ebrahimi * until the corresponding storage area is wiped. This specification does
2294*62c56f98SSadaf Ebrahimi * not define any mechanism to wipe a storage area, but integrations may
2295*62c56f98SSadaf Ebrahimi * provide their own mechanism (for example to perform a factory reset,
2296*62c56f98SSadaf Ebrahimi * to prepare for device refurbishment, or to uninstall an application).
2297*62c56f98SSadaf Ebrahimi *
2298*62c56f98SSadaf Ebrahimi * This lifetime value is the default storage area for the calling
2299*62c56f98SSadaf Ebrahimi * application. Integrations of Mbed TLS may support other persistent lifetimes.
2300*62c56f98SSadaf Ebrahimi * See ::psa_key_lifetime_t for more information.
2301*62c56f98SSadaf Ebrahimi */
2302*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t) 0x00000001)
2303*62c56f98SSadaf Ebrahimi
2304*62c56f98SSadaf Ebrahimi /** The persistence level of volatile keys.
2305*62c56f98SSadaf Ebrahimi *
2306*62c56f98SSadaf Ebrahimi * See ::psa_key_persistence_t for more information.
2307*62c56f98SSadaf Ebrahimi */
2308*62c56f98SSadaf Ebrahimi #define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t) 0x00)
2309*62c56f98SSadaf Ebrahimi
2310*62c56f98SSadaf Ebrahimi /** The default persistence level for persistent keys.
2311*62c56f98SSadaf Ebrahimi *
2312*62c56f98SSadaf Ebrahimi * See ::psa_key_persistence_t for more information.
2313*62c56f98SSadaf Ebrahimi */
2314*62c56f98SSadaf Ebrahimi #define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t) 0x01)
2315*62c56f98SSadaf Ebrahimi
2316*62c56f98SSadaf Ebrahimi /** A persistence level indicating that a key is never destroyed.
2317*62c56f98SSadaf Ebrahimi *
2318*62c56f98SSadaf Ebrahimi * See ::psa_key_persistence_t for more information.
2319*62c56f98SSadaf Ebrahimi */
2320*62c56f98SSadaf Ebrahimi #define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t) 0xff)
2321*62c56f98SSadaf Ebrahimi
2322*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
2323*62c56f98SSadaf Ebrahimi ((psa_key_persistence_t) ((lifetime) & 0x000000ff))
2324*62c56f98SSadaf Ebrahimi
2325*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
2326*62c56f98SSadaf Ebrahimi ((psa_key_location_t) ((lifetime) >> 8))
2327*62c56f98SSadaf Ebrahimi
2328*62c56f98SSadaf Ebrahimi /** Whether a key lifetime indicates that the key is volatile.
2329*62c56f98SSadaf Ebrahimi *
2330*62c56f98SSadaf Ebrahimi * A volatile key is automatically destroyed by the implementation when
2331*62c56f98SSadaf Ebrahimi * the application instance terminates. In particular, a volatile key
2332*62c56f98SSadaf Ebrahimi * is automatically destroyed on a power reset of the device.
2333*62c56f98SSadaf Ebrahimi *
2334*62c56f98SSadaf Ebrahimi * A key that is not volatile is persistent. Persistent keys are
2335*62c56f98SSadaf Ebrahimi * preserved until the application explicitly destroys them or until an
2336*62c56f98SSadaf Ebrahimi * implementation-specific device management event occurs (for example,
2337*62c56f98SSadaf Ebrahimi * a factory reset).
2338*62c56f98SSadaf Ebrahimi *
2339*62c56f98SSadaf Ebrahimi * \param lifetime The lifetime value to query (value of type
2340*62c56f98SSadaf Ebrahimi * ::psa_key_lifetime_t).
2341*62c56f98SSadaf Ebrahimi *
2342*62c56f98SSadaf Ebrahimi * \return \c 1 if the key is volatile, otherwise \c 0.
2343*62c56f98SSadaf Ebrahimi */
2344*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
2345*62c56f98SSadaf Ebrahimi (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2346*62c56f98SSadaf Ebrahimi PSA_KEY_PERSISTENCE_VOLATILE)
2347*62c56f98SSadaf Ebrahimi
2348*62c56f98SSadaf Ebrahimi /** Whether a key lifetime indicates that the key is read-only.
2349*62c56f98SSadaf Ebrahimi *
2350*62c56f98SSadaf Ebrahimi * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2351*62c56f98SSadaf Ebrahimi * They must be created through platform-specific means that bypass the API.
2352*62c56f98SSadaf Ebrahimi *
2353*62c56f98SSadaf Ebrahimi * Some platforms may offer ways to destroy read-only keys. For example,
2354*62c56f98SSadaf Ebrahimi * consider a platform with multiple levels of privilege, where a
2355*62c56f98SSadaf Ebrahimi * low-privilege application can use a key but is not allowed to destroy
2356*62c56f98SSadaf Ebrahimi * it, and the platform exposes the key to the application with a read-only
2357*62c56f98SSadaf Ebrahimi * lifetime. High-privilege code can destroy the key even though the
2358*62c56f98SSadaf Ebrahimi * application sees the key as read-only.
2359*62c56f98SSadaf Ebrahimi *
2360*62c56f98SSadaf Ebrahimi * \param lifetime The lifetime value to query (value of type
2361*62c56f98SSadaf Ebrahimi * ::psa_key_lifetime_t).
2362*62c56f98SSadaf Ebrahimi *
2363*62c56f98SSadaf Ebrahimi * \return \c 1 if the key is read-only, otherwise \c 0.
2364*62c56f98SSadaf Ebrahimi */
2365*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
2366*62c56f98SSadaf Ebrahimi (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2367*62c56f98SSadaf Ebrahimi PSA_KEY_PERSISTENCE_READ_ONLY)
2368*62c56f98SSadaf Ebrahimi
2369*62c56f98SSadaf Ebrahimi /** Construct a lifetime from a persistence level and a location.
2370*62c56f98SSadaf Ebrahimi *
2371*62c56f98SSadaf Ebrahimi * \param persistence The persistence level
2372*62c56f98SSadaf Ebrahimi * (value of type ::psa_key_persistence_t).
2373*62c56f98SSadaf Ebrahimi * \param location The location indicator
2374*62c56f98SSadaf Ebrahimi * (value of type ::psa_key_location_t).
2375*62c56f98SSadaf Ebrahimi *
2376*62c56f98SSadaf Ebrahimi * \return The constructed lifetime value.
2377*62c56f98SSadaf Ebrahimi */
2378*62c56f98SSadaf Ebrahimi #define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2379*62c56f98SSadaf Ebrahimi ((location) << 8 | (persistence))
2380*62c56f98SSadaf Ebrahimi
2381*62c56f98SSadaf Ebrahimi /** The local storage area for persistent keys.
2382*62c56f98SSadaf Ebrahimi *
2383*62c56f98SSadaf Ebrahimi * This storage area is available on all systems that can store persistent
2384*62c56f98SSadaf Ebrahimi * keys without delegating the storage to a third-party cryptoprocessor.
2385*62c56f98SSadaf Ebrahimi *
2386*62c56f98SSadaf Ebrahimi * See ::psa_key_location_t for more information.
2387*62c56f98SSadaf Ebrahimi */
2388*62c56f98SSadaf Ebrahimi #define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t) 0x000000)
2389*62c56f98SSadaf Ebrahimi
2390*62c56f98SSadaf Ebrahimi #define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t) 0x800000)
2391*62c56f98SSadaf Ebrahimi
2392*62c56f98SSadaf Ebrahimi /* Note that key identifier values are embedded in the
2393*62c56f98SSadaf Ebrahimi * persistent key store, as part of key metadata. As a consequence, they
2394*62c56f98SSadaf Ebrahimi * must not be changed (unless the storage format version changes).
2395*62c56f98SSadaf Ebrahimi */
2396*62c56f98SSadaf Ebrahimi
2397*62c56f98SSadaf Ebrahimi /** The null key identifier.
2398*62c56f98SSadaf Ebrahimi */
2399*62c56f98SSadaf Ebrahimi /* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */
2400*62c56f98SSadaf Ebrahimi #define PSA_KEY_ID_NULL ((psa_key_id_t)0)
2401*62c56f98SSadaf Ebrahimi /* *INDENT-ON* */
2402*62c56f98SSadaf Ebrahimi /** The minimum value for a key identifier chosen by the application.
2403*62c56f98SSadaf Ebrahimi */
2404*62c56f98SSadaf Ebrahimi #define PSA_KEY_ID_USER_MIN ((psa_key_id_t) 0x00000001)
2405*62c56f98SSadaf Ebrahimi /** The maximum value for a key identifier chosen by the application.
2406*62c56f98SSadaf Ebrahimi */
2407*62c56f98SSadaf Ebrahimi #define PSA_KEY_ID_USER_MAX ((psa_key_id_t) 0x3fffffff)
2408*62c56f98SSadaf Ebrahimi /** The minimum value for a key identifier chosen by the implementation.
2409*62c56f98SSadaf Ebrahimi */
2410*62c56f98SSadaf Ebrahimi #define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t) 0x40000000)
2411*62c56f98SSadaf Ebrahimi /** The maximum value for a key identifier chosen by the implementation.
2412*62c56f98SSadaf Ebrahimi */
2413*62c56f98SSadaf Ebrahimi #define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t) 0x7fffffff)
2414*62c56f98SSadaf Ebrahimi
2415*62c56f98SSadaf Ebrahimi
2416*62c56f98SSadaf Ebrahimi #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2417*62c56f98SSadaf Ebrahimi
2418*62c56f98SSadaf Ebrahimi #define MBEDTLS_SVC_KEY_ID_INIT ((psa_key_id_t) 0)
2419*62c56f98SSadaf Ebrahimi #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) (id)
2420*62c56f98SSadaf Ebrahimi #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) (0)
2421*62c56f98SSadaf Ebrahimi
2422*62c56f98SSadaf Ebrahimi /** Utility to initialize a key identifier at runtime.
2423*62c56f98SSadaf Ebrahimi *
2424*62c56f98SSadaf Ebrahimi * \param unused Unused parameter.
2425*62c56f98SSadaf Ebrahimi * \param key_id Identifier of the key.
2426*62c56f98SSadaf Ebrahimi */
mbedtls_svc_key_id_make(unsigned int unused,psa_key_id_t key_id)2427*62c56f98SSadaf Ebrahimi static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2428*62c56f98SSadaf Ebrahimi unsigned int unused, psa_key_id_t key_id)
2429*62c56f98SSadaf Ebrahimi {
2430*62c56f98SSadaf Ebrahimi (void) unused;
2431*62c56f98SSadaf Ebrahimi
2432*62c56f98SSadaf Ebrahimi return key_id;
2433*62c56f98SSadaf Ebrahimi }
2434*62c56f98SSadaf Ebrahimi
2435*62c56f98SSadaf Ebrahimi /** Compare two key identifiers.
2436*62c56f98SSadaf Ebrahimi *
2437*62c56f98SSadaf Ebrahimi * \param id1 First key identifier.
2438*62c56f98SSadaf Ebrahimi * \param id2 Second key identifier.
2439*62c56f98SSadaf Ebrahimi *
2440*62c56f98SSadaf Ebrahimi * \return Non-zero if the two key identifier are equal, zero otherwise.
2441*62c56f98SSadaf Ebrahimi */
mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,mbedtls_svc_key_id_t id2)2442*62c56f98SSadaf Ebrahimi static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2443*62c56f98SSadaf Ebrahimi mbedtls_svc_key_id_t id2)
2444*62c56f98SSadaf Ebrahimi {
2445*62c56f98SSadaf Ebrahimi return id1 == id2;
2446*62c56f98SSadaf Ebrahimi }
2447*62c56f98SSadaf Ebrahimi
2448*62c56f98SSadaf Ebrahimi /** Check whether a key identifier is null.
2449*62c56f98SSadaf Ebrahimi *
2450*62c56f98SSadaf Ebrahimi * \param key Key identifier.
2451*62c56f98SSadaf Ebrahimi *
2452*62c56f98SSadaf Ebrahimi * \return Non-zero if the key identifier is null, zero otherwise.
2453*62c56f98SSadaf Ebrahimi */
mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2454*62c56f98SSadaf Ebrahimi static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2455*62c56f98SSadaf Ebrahimi {
2456*62c56f98SSadaf Ebrahimi return key == 0;
2457*62c56f98SSadaf Ebrahimi }
2458*62c56f98SSadaf Ebrahimi
2459*62c56f98SSadaf Ebrahimi #else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2460*62c56f98SSadaf Ebrahimi
2461*62c56f98SSadaf Ebrahimi #define MBEDTLS_SVC_KEY_ID_INIT ((mbedtls_svc_key_id_t){ 0, 0 })
2462*62c56f98SSadaf Ebrahimi #define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) ((id).MBEDTLS_PRIVATE(key_id))
2463*62c56f98SSadaf Ebrahimi #define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) ((id).MBEDTLS_PRIVATE(owner))
2464*62c56f98SSadaf Ebrahimi
2465*62c56f98SSadaf Ebrahimi /** Utility to initialize a key identifier at runtime.
2466*62c56f98SSadaf Ebrahimi *
2467*62c56f98SSadaf Ebrahimi * \param owner_id Identifier of the key owner.
2468*62c56f98SSadaf Ebrahimi * \param key_id Identifier of the key.
2469*62c56f98SSadaf Ebrahimi */
mbedtls_svc_key_id_make(mbedtls_key_owner_id_t owner_id,psa_key_id_t key_id)2470*62c56f98SSadaf Ebrahimi static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2471*62c56f98SSadaf Ebrahimi mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id)
2472*62c56f98SSadaf Ebrahimi {
2473*62c56f98SSadaf Ebrahimi return (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,
2474*62c56f98SSadaf Ebrahimi .MBEDTLS_PRIVATE(owner) = owner_id };
2475*62c56f98SSadaf Ebrahimi }
2476*62c56f98SSadaf Ebrahimi
2477*62c56f98SSadaf Ebrahimi /** Compare two key identifiers.
2478*62c56f98SSadaf Ebrahimi *
2479*62c56f98SSadaf Ebrahimi * \param id1 First key identifier.
2480*62c56f98SSadaf Ebrahimi * \param id2 Second key identifier.
2481*62c56f98SSadaf Ebrahimi *
2482*62c56f98SSadaf Ebrahimi * \return Non-zero if the two key identifier are equal, zero otherwise.
2483*62c56f98SSadaf Ebrahimi */
mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,mbedtls_svc_key_id_t id2)2484*62c56f98SSadaf Ebrahimi static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,
2485*62c56f98SSadaf Ebrahimi mbedtls_svc_key_id_t id2)
2486*62c56f98SSadaf Ebrahimi {
2487*62c56f98SSadaf Ebrahimi return (id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id)) &&
2488*62c56f98SSadaf Ebrahimi mbedtls_key_owner_id_equal(id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner));
2489*62c56f98SSadaf Ebrahimi }
2490*62c56f98SSadaf Ebrahimi
2491*62c56f98SSadaf Ebrahimi /** Check whether a key identifier is null.
2492*62c56f98SSadaf Ebrahimi *
2493*62c56f98SSadaf Ebrahimi * \param key Key identifier.
2494*62c56f98SSadaf Ebrahimi *
2495*62c56f98SSadaf Ebrahimi * \return Non-zero if the key identifier is null, zero otherwise.
2496*62c56f98SSadaf Ebrahimi */
mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2497*62c56f98SSadaf Ebrahimi static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
2498*62c56f98SSadaf Ebrahimi {
2499*62c56f98SSadaf Ebrahimi return key.MBEDTLS_PRIVATE(key_id) == 0;
2500*62c56f98SSadaf Ebrahimi }
2501*62c56f98SSadaf Ebrahimi
2502*62c56f98SSadaf Ebrahimi #endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2503*62c56f98SSadaf Ebrahimi
2504*62c56f98SSadaf Ebrahimi /**@}*/
2505*62c56f98SSadaf Ebrahimi
2506*62c56f98SSadaf Ebrahimi /** \defgroup policy Key policies
2507*62c56f98SSadaf Ebrahimi * @{
2508*62c56f98SSadaf Ebrahimi */
2509*62c56f98SSadaf Ebrahimi
2510*62c56f98SSadaf Ebrahimi /* Note that key usage flags are embedded in the
2511*62c56f98SSadaf Ebrahimi * persistent key store, as part of key metadata. As a consequence, they
2512*62c56f98SSadaf Ebrahimi * must not be changed (unless the storage format version changes).
2513*62c56f98SSadaf Ebrahimi */
2514*62c56f98SSadaf Ebrahimi
2515*62c56f98SSadaf Ebrahimi /** Whether the key may be exported.
2516*62c56f98SSadaf Ebrahimi *
2517*62c56f98SSadaf Ebrahimi * A public key or the public part of a key pair may always be exported
2518*62c56f98SSadaf Ebrahimi * regardless of the value of this permission flag.
2519*62c56f98SSadaf Ebrahimi *
2520*62c56f98SSadaf Ebrahimi * If a key does not have export permission, implementations shall not
2521*62c56f98SSadaf Ebrahimi * allow the key to be exported in plain form from the cryptoprocessor,
2522*62c56f98SSadaf Ebrahimi * whether through psa_export_key() or through a proprietary interface.
2523*62c56f98SSadaf Ebrahimi * The key may however be exportable in a wrapped form, i.e. in a form
2524*62c56f98SSadaf Ebrahimi * where it is encrypted by another key.
2525*62c56f98SSadaf Ebrahimi */
2526*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t) 0x00000001)
2527*62c56f98SSadaf Ebrahimi
2528*62c56f98SSadaf Ebrahimi /** Whether the key may be copied.
2529*62c56f98SSadaf Ebrahimi *
2530*62c56f98SSadaf Ebrahimi * This flag allows the use of psa_copy_key() to make a copy of the key
2531*62c56f98SSadaf Ebrahimi * with the same policy or a more restrictive policy.
2532*62c56f98SSadaf Ebrahimi *
2533*62c56f98SSadaf Ebrahimi * For lifetimes for which the key is located in a secure element which
2534*62c56f98SSadaf Ebrahimi * enforce the non-exportability of keys, copying a key outside the secure
2535*62c56f98SSadaf Ebrahimi * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2536*62c56f98SSadaf Ebrahimi * Copying the key inside the secure element is permitted with just
2537*62c56f98SSadaf Ebrahimi * #PSA_KEY_USAGE_COPY if the secure element supports it.
2538*62c56f98SSadaf Ebrahimi * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
2539*62c56f98SSadaf Ebrahimi * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2540*62c56f98SSadaf Ebrahimi * is sufficient to permit the copy.
2541*62c56f98SSadaf Ebrahimi */
2542*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_COPY ((psa_key_usage_t) 0x00000002)
2543*62c56f98SSadaf Ebrahimi
2544*62c56f98SSadaf Ebrahimi /** Whether the key may be used to encrypt a message.
2545*62c56f98SSadaf Ebrahimi *
2546*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a symmetric encryption operation,
2547*62c56f98SSadaf Ebrahimi * for an AEAD encryption-and-authentication operation,
2548*62c56f98SSadaf Ebrahimi * or for an asymmetric encryption operation,
2549*62c56f98SSadaf Ebrahimi * if otherwise permitted by the key's type and policy.
2550*62c56f98SSadaf Ebrahimi *
2551*62c56f98SSadaf Ebrahimi * For a key pair, this concerns the public key.
2552*62c56f98SSadaf Ebrahimi */
2553*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t) 0x00000100)
2554*62c56f98SSadaf Ebrahimi
2555*62c56f98SSadaf Ebrahimi /** Whether the key may be used to decrypt a message.
2556*62c56f98SSadaf Ebrahimi *
2557*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a symmetric decryption operation,
2558*62c56f98SSadaf Ebrahimi * for an AEAD decryption-and-verification operation,
2559*62c56f98SSadaf Ebrahimi * or for an asymmetric decryption operation,
2560*62c56f98SSadaf Ebrahimi * if otherwise permitted by the key's type and policy.
2561*62c56f98SSadaf Ebrahimi *
2562*62c56f98SSadaf Ebrahimi * For a key pair, this concerns the private key.
2563*62c56f98SSadaf Ebrahimi */
2564*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t) 0x00000200)
2565*62c56f98SSadaf Ebrahimi
2566*62c56f98SSadaf Ebrahimi /** Whether the key may be used to sign a message.
2567*62c56f98SSadaf Ebrahimi *
2568*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a MAC calculation operation or for
2569*62c56f98SSadaf Ebrahimi * an asymmetric message signature operation, if otherwise permitted by the
2570*62c56f98SSadaf Ebrahimi * key’s type and policy.
2571*62c56f98SSadaf Ebrahimi *
2572*62c56f98SSadaf Ebrahimi * For a key pair, this concerns the private key.
2573*62c56f98SSadaf Ebrahimi */
2574*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t) 0x00000400)
2575*62c56f98SSadaf Ebrahimi
2576*62c56f98SSadaf Ebrahimi /** Whether the key may be used to verify a message.
2577*62c56f98SSadaf Ebrahimi *
2578*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a MAC verification operation or for
2579*62c56f98SSadaf Ebrahimi * an asymmetric message signature verification operation, if otherwise
2580*62c56f98SSadaf Ebrahimi * permitted by the key’s type and policy.
2581*62c56f98SSadaf Ebrahimi *
2582*62c56f98SSadaf Ebrahimi * For a key pair, this concerns the public key.
2583*62c56f98SSadaf Ebrahimi */
2584*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t) 0x00000800)
2585*62c56f98SSadaf Ebrahimi
2586*62c56f98SSadaf Ebrahimi /** Whether the key may be used to sign a message.
2587*62c56f98SSadaf Ebrahimi *
2588*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a MAC calculation operation
2589*62c56f98SSadaf Ebrahimi * or for an asymmetric signature operation,
2590*62c56f98SSadaf Ebrahimi * if otherwise permitted by the key's type and policy.
2591*62c56f98SSadaf Ebrahimi *
2592*62c56f98SSadaf Ebrahimi * For a key pair, this concerns the private key.
2593*62c56f98SSadaf Ebrahimi */
2594*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t) 0x00001000)
2595*62c56f98SSadaf Ebrahimi
2596*62c56f98SSadaf Ebrahimi /** Whether the key may be used to verify a message signature.
2597*62c56f98SSadaf Ebrahimi *
2598*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a MAC verification operation
2599*62c56f98SSadaf Ebrahimi * or for an asymmetric signature verification operation,
2600*62c56f98SSadaf Ebrahimi * if otherwise permitted by the key's type and policy.
2601*62c56f98SSadaf Ebrahimi *
2602*62c56f98SSadaf Ebrahimi * For a key pair, this concerns the public key.
2603*62c56f98SSadaf Ebrahimi */
2604*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t) 0x00002000)
2605*62c56f98SSadaf Ebrahimi
2606*62c56f98SSadaf Ebrahimi /** Whether the key may be used to derive other keys or produce a password
2607*62c56f98SSadaf Ebrahimi * hash.
2608*62c56f98SSadaf Ebrahimi *
2609*62c56f98SSadaf Ebrahimi * This flag allows the key to be used for a key derivation operation or for
2610*62c56f98SSadaf Ebrahimi * a key agreement operation, if otherwise permitted by the key's type and
2611*62c56f98SSadaf Ebrahimi * policy.
2612*62c56f98SSadaf Ebrahimi *
2613*62c56f98SSadaf Ebrahimi * If this flag is present on all keys used in calls to
2614*62c56f98SSadaf Ebrahimi * psa_key_derivation_input_key() for a key derivation operation, then it
2615*62c56f98SSadaf Ebrahimi * permits calling psa_key_derivation_output_bytes() or
2616*62c56f98SSadaf Ebrahimi * psa_key_derivation_output_key() at the end of the operation.
2617*62c56f98SSadaf Ebrahimi */
2618*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t) 0x00004000)
2619*62c56f98SSadaf Ebrahimi
2620*62c56f98SSadaf Ebrahimi /** Whether the key may be used to verify the result of a key derivation,
2621*62c56f98SSadaf Ebrahimi * including password hashing.
2622*62c56f98SSadaf Ebrahimi *
2623*62c56f98SSadaf Ebrahimi * This flag allows the key to be used:
2624*62c56f98SSadaf Ebrahimi *
2625*62c56f98SSadaf Ebrahimi * This flag allows the key to be used in a key derivation operation, if
2626*62c56f98SSadaf Ebrahimi * otherwise permitted by the key's type and policy.
2627*62c56f98SSadaf Ebrahimi *
2628*62c56f98SSadaf Ebrahimi * If this flag is present on all keys used in calls to
2629*62c56f98SSadaf Ebrahimi * psa_key_derivation_input_key() for a key derivation operation, then it
2630*62c56f98SSadaf Ebrahimi * permits calling psa_key_derivation_verify_bytes() or
2631*62c56f98SSadaf Ebrahimi * psa_key_derivation_verify_key() at the end of the operation.
2632*62c56f98SSadaf Ebrahimi */
2633*62c56f98SSadaf Ebrahimi #define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t) 0x00008000)
2634*62c56f98SSadaf Ebrahimi
2635*62c56f98SSadaf Ebrahimi /**@}*/
2636*62c56f98SSadaf Ebrahimi
2637*62c56f98SSadaf Ebrahimi /** \defgroup derivation Key derivation
2638*62c56f98SSadaf Ebrahimi * @{
2639*62c56f98SSadaf Ebrahimi */
2640*62c56f98SSadaf Ebrahimi
2641*62c56f98SSadaf Ebrahimi /* Key input steps are not embedded in the persistent storage, so you can
2642*62c56f98SSadaf Ebrahimi * change them if needed: it's only an ABI change. */
2643*62c56f98SSadaf Ebrahimi
2644*62c56f98SSadaf Ebrahimi /** A secret input for key derivation.
2645*62c56f98SSadaf Ebrahimi *
2646*62c56f98SSadaf Ebrahimi * This should be a key of type #PSA_KEY_TYPE_DERIVE
2647*62c56f98SSadaf Ebrahimi * (passed to psa_key_derivation_input_key())
2648*62c56f98SSadaf Ebrahimi * or the shared secret resulting from a key agreement
2649*62c56f98SSadaf Ebrahimi * (obtained via psa_key_derivation_key_agreement()).
2650*62c56f98SSadaf Ebrahimi *
2651*62c56f98SSadaf Ebrahimi * The secret can also be a direct input (passed to
2652*62c56f98SSadaf Ebrahimi * key_derivation_input_bytes()). In this case, the derivation operation
2653*62c56f98SSadaf Ebrahimi * may not be used to derive keys: the operation will only allow
2654*62c56f98SSadaf Ebrahimi * psa_key_derivation_output_bytes(),
2655*62c56f98SSadaf Ebrahimi * psa_key_derivation_verify_bytes(), or
2656*62c56f98SSadaf Ebrahimi * psa_key_derivation_verify_key(), but not
2657*62c56f98SSadaf Ebrahimi * psa_key_derivation_output_key().
2658*62c56f98SSadaf Ebrahimi */
2659*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t) 0x0101)
2660*62c56f98SSadaf Ebrahimi
2661*62c56f98SSadaf Ebrahimi /** A low-entropy secret input for password hashing / key stretching.
2662*62c56f98SSadaf Ebrahimi *
2663*62c56f98SSadaf Ebrahimi * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2664*62c56f98SSadaf Ebrahimi * psa_key_derivation_input_key()) or a direct input (passed to
2665*62c56f98SSadaf Ebrahimi * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2666*62c56f98SSadaf Ebrahimi * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2667*62c56f98SSadaf Ebrahimi * the shared secret resulting from a key agreement.
2668*62c56f98SSadaf Ebrahimi *
2669*62c56f98SSadaf Ebrahimi * The secret can also be a direct input (passed to
2670*62c56f98SSadaf Ebrahimi * key_derivation_input_bytes()). In this case, the derivation operation
2671*62c56f98SSadaf Ebrahimi * may not be used to derive keys: the operation will only allow
2672*62c56f98SSadaf Ebrahimi * psa_key_derivation_output_bytes(),
2673*62c56f98SSadaf Ebrahimi * psa_key_derivation_verify_bytes(), or
2674*62c56f98SSadaf Ebrahimi * psa_key_derivation_verify_key(), but not
2675*62c56f98SSadaf Ebrahimi * psa_key_derivation_output_key().
2676*62c56f98SSadaf Ebrahimi */
2677*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t) 0x0102)
2678*62c56f98SSadaf Ebrahimi
2679*62c56f98SSadaf Ebrahimi /** A high-entropy additional secret input for key derivation.
2680*62c56f98SSadaf Ebrahimi *
2681*62c56f98SSadaf Ebrahimi * This is typically the shared secret resulting from a key agreement obtained
2682*62c56f98SSadaf Ebrahimi * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
2683*62c56f98SSadaf Ebrahimi * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
2684*62c56f98SSadaf Ebrahimi * a direct input passed to `psa_key_derivation_input_bytes()`.
2685*62c56f98SSadaf Ebrahimi */
2686*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
2687*62c56f98SSadaf Ebrahimi ((psa_key_derivation_step_t) 0x0103)
2688*62c56f98SSadaf Ebrahimi
2689*62c56f98SSadaf Ebrahimi /** A label for key derivation.
2690*62c56f98SSadaf Ebrahimi *
2691*62c56f98SSadaf Ebrahimi * This should be a direct input.
2692*62c56f98SSadaf Ebrahimi * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2693*62c56f98SSadaf Ebrahimi */
2694*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t) 0x0201)
2695*62c56f98SSadaf Ebrahimi
2696*62c56f98SSadaf Ebrahimi /** A salt for key derivation.
2697*62c56f98SSadaf Ebrahimi *
2698*62c56f98SSadaf Ebrahimi * This should be a direct input.
2699*62c56f98SSadaf Ebrahimi * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2700*62c56f98SSadaf Ebrahimi * #PSA_KEY_TYPE_PEPPER.
2701*62c56f98SSadaf Ebrahimi */
2702*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t) 0x0202)
2703*62c56f98SSadaf Ebrahimi
2704*62c56f98SSadaf Ebrahimi /** An information string for key derivation.
2705*62c56f98SSadaf Ebrahimi *
2706*62c56f98SSadaf Ebrahimi * This should be a direct input.
2707*62c56f98SSadaf Ebrahimi * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2708*62c56f98SSadaf Ebrahimi */
2709*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t) 0x0203)
2710*62c56f98SSadaf Ebrahimi
2711*62c56f98SSadaf Ebrahimi /** A seed for key derivation.
2712*62c56f98SSadaf Ebrahimi *
2713*62c56f98SSadaf Ebrahimi * This should be a direct input.
2714*62c56f98SSadaf Ebrahimi * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
2715*62c56f98SSadaf Ebrahimi */
2716*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t) 0x0204)
2717*62c56f98SSadaf Ebrahimi
2718*62c56f98SSadaf Ebrahimi /** A cost parameter for password hashing / key stretching.
2719*62c56f98SSadaf Ebrahimi *
2720*62c56f98SSadaf Ebrahimi * This must be a direct input, passed to psa_key_derivation_input_integer().
2721*62c56f98SSadaf Ebrahimi */
2722*62c56f98SSadaf Ebrahimi #define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t) 0x0205)
2723*62c56f98SSadaf Ebrahimi
2724*62c56f98SSadaf Ebrahimi /**@}*/
2725*62c56f98SSadaf Ebrahimi
2726*62c56f98SSadaf Ebrahimi /** \defgroup helper_macros Helper macros
2727*62c56f98SSadaf Ebrahimi * @{
2728*62c56f98SSadaf Ebrahimi */
2729*62c56f98SSadaf Ebrahimi
2730*62c56f98SSadaf Ebrahimi /* Helper macros */
2731*62c56f98SSadaf Ebrahimi
2732*62c56f98SSadaf Ebrahimi /** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2733*62c56f98SSadaf Ebrahimi * regardless of the tag length they encode.
2734*62c56f98SSadaf Ebrahimi *
2735*62c56f98SSadaf Ebrahimi * \param aead_alg_1 An AEAD algorithm identifier.
2736*62c56f98SSadaf Ebrahimi * \param aead_alg_2 An AEAD algorithm identifier.
2737*62c56f98SSadaf Ebrahimi *
2738*62c56f98SSadaf Ebrahimi * \return 1 if both identifiers refer to the same AEAD algorithm,
2739*62c56f98SSadaf Ebrahimi * 0 otherwise.
2740*62c56f98SSadaf Ebrahimi * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2741*62c56f98SSadaf Ebrahimi * a supported AEAD algorithm.
2742*62c56f98SSadaf Ebrahimi */
2743*62c56f98SSadaf Ebrahimi #define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2744*62c56f98SSadaf Ebrahimi (!(((aead_alg_1) ^ (aead_alg_2)) & \
2745*62c56f98SSadaf Ebrahimi ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2746*62c56f98SSadaf Ebrahimi
2747*62c56f98SSadaf Ebrahimi /**@}*/
2748*62c56f98SSadaf Ebrahimi
2749*62c56f98SSadaf Ebrahimi /**@}*/
2750*62c56f98SSadaf Ebrahimi
2751*62c56f98SSadaf Ebrahimi /** \defgroup interruptible Interruptible operations
2752*62c56f98SSadaf Ebrahimi * @{
2753*62c56f98SSadaf Ebrahimi */
2754*62c56f98SSadaf Ebrahimi
2755*62c56f98SSadaf Ebrahimi /** Maximum value for use with \c psa_interruptible_set_max_ops() to determine
2756*62c56f98SSadaf Ebrahimi * the maximum number of ops allowed to be executed by an interruptible
2757*62c56f98SSadaf Ebrahimi * function in a single call.
2758*62c56f98SSadaf Ebrahimi */
2759*62c56f98SSadaf Ebrahimi #define PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED UINT32_MAX
2760*62c56f98SSadaf Ebrahimi
2761*62c56f98SSadaf Ebrahimi /**@}*/
2762*62c56f98SSadaf Ebrahimi
2763*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_VALUES_H */
2764