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