blob: 99be43395dda9d5b36de87b0611e86ed396d2344 [file] [log] [blame]
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001/**
2 * \file psa/crypto_values.h
3 *
4 * \brief PSA cryptography module: macros to build and analyze integer values.
5 *
6 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h. Drivers must include the appropriate driver
8 * header file.
9 *
10 * This file contains portable definitions of macros to build and analyze
11 * values of integral types that encode properties of cryptographic keys,
12 * designations of cryptographic algorithms, and error codes returned by
13 * the library.
14 *
Gilles Peskine79733992022-06-20 18:41:20 +020015 * Note that many of the constants defined in this file are embedded in
16 * the persistent key store, as part of key metadata (including usage
17 * policies). As a consequence, they must not be changed (unless the storage
18 * format version changes).
19 *
Gilles Peskinef3b731e2018-12-12 13:38:31 +010020 * This header file only defines preprocessor macros.
21 */
22/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020023 * Copyright The Mbed TLS Contributors
Gilles Peskinef3b731e2018-12-12 13:38:31 +010024 * SPDX-License-Identifier: Apache-2.0
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License"); you may
27 * not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
Gilles Peskinef3b731e2018-12-12 13:38:31 +010037 */
38
39#ifndef PSA_CRYPTO_VALUES_H
40#define PSA_CRYPTO_VALUES_H
Mateusz Starzyk363eb292021-05-19 17:32:44 +020041#include "mbedtls/private_access.h"
Gilles Peskinef3b731e2018-12-12 13:38:31 +010042
43/** \defgroup error Error codes
44 * @{
45 */
46
David Saadab4ecc272019-02-14 13:48:10 +020047/* PSA error codes */
48
Gilles Peskine79733992022-06-20 18:41:20 +020049/* Error codes are standardized across PSA domains (framework, crypto, storage,
50 * etc.). Do not change the values in this section. If you must add a new
51 * value, check with the Arm PSA framework group to pick one that other
52 * domains aren't already using. */
53
Gilles Peskinef3b731e2018-12-12 13:38:31 +010054/** The action was completed successfully. */
55#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056
57/** An error occurred that does not correspond to any defined
58 * failure cause.
59 *
60 * Implementations may use this error code if none of the other standard
61 * error codes are applicable. */
David Saadab4ecc272019-02-14 13:48:10 +020062#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010063
64/** The requested operation or a parameter is not supported
65 * by this implementation.
66 *
67 * Implementations should return this error code when an enumeration
68 * parameter such as a key type, algorithm, etc. is not recognized.
69 * If a combination of parameters is recognized and identified as
70 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
David Saadab4ecc272019-02-14 13:48:10 +020071#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010072
73/** The requested action is denied by a policy.
74 *
75 * Implementations should return this error code when the parameters
76 * are recognized as valid and supported, and a policy explicitly
77 * denies the requested operation.
78 *
79 * If a subset of the parameters of a function call identify a
80 * forbidden operation, and another subset of the parameters are
81 * not valid or not supported, it is unspecified whether the function
82 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
83 * #PSA_ERROR_INVALID_ARGUMENT. */
David Saadab4ecc272019-02-14 13:48:10 +020084#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010085
86/** An output buffer is too small.
87 *
88 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
89 * description to determine a sufficient buffer size.
90 *
91 * Implementations should preferably return this error code only
92 * in cases when performing the operation with a larger output
93 * buffer would succeed. However implementations may return this
94 * error if a function has invalid or unsupported parameters in addition
95 * to the parameters that determine the necessary output buffer size. */
David Saadab4ecc272019-02-14 13:48:10 +020096#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010097
David Saadab4ecc272019-02-14 13:48:10 +020098/** Asking for an item that already exists
Gilles Peskinef3b731e2018-12-12 13:38:31 +010099 *
David Saadab4ecc272019-02-14 13:48:10 +0200100 * Implementations should return this error, when attempting
101 * to write an item (like a key) that already exists. */
102#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100103
David Saadab4ecc272019-02-14 13:48:10 +0200104/** Asking for an item that doesn't exist
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100105 *
David Saadab4ecc272019-02-14 13:48:10 +0200106 * Implementations should return this error, if a requested item (like
107 * a key) does not exist. */
108#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100109
110/** The requested action cannot be performed in the current state.
111 *
112 * Multipart operations return this error when one of the
113 * functions is called out of sequence. Refer to the function
114 * descriptions for permitted sequencing of functions.
115 *
116 * Implementations shall not return this error code to indicate
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100117 * that a key either exists or not,
118 * but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100119 * as applicable.
120 *
121 * Implementations shall not return this error code to indicate that a
Ronald Croncf56a0a2020-08-04 09:51:30 +0200122 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100123 * instead. */
David Saadab4ecc272019-02-14 13:48:10 +0200124#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100125
126/** The parameters passed to the function are invalid.
127 *
128 * Implementations may return this error any time a parameter or
129 * combination of parameters are recognized as invalid.
130 *
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100131 * Implementations shall not return this error code to indicate that a
Ronald Croncf56a0a2020-08-04 09:51:30 +0200132 * key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100133 * instead.
134 */
David Saadab4ecc272019-02-14 13:48:10 +0200135#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100136
137/** There is not enough runtime memory.
138 *
139 * If the action is carried out across multiple security realms, this
140 * error can refer to available memory in any of the security realms. */
David Saadab4ecc272019-02-14 13:48:10 +0200141#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100142
143/** There is not enough persistent storage.
144 *
145 * Functions that modify the key storage return this error code if
146 * there is insufficient storage space on the host media. In addition,
147 * many functions that do not otherwise access storage may return this
148 * error code if the implementation requires a mandatory log entry for
149 * the requested action and the log storage space is full. */
David Saadab4ecc272019-02-14 13:48:10 +0200150#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100151
152/** There was a communication failure inside the implementation.
153 *
154 * This can indicate a communication failure between the application
155 * and an external cryptoprocessor or between the cryptoprocessor and
156 * an external volatile or persistent memory. A communication failure
157 * may be transient or permanent depending on the cause.
158 *
159 * \warning If a function returns this error, it is undetermined
160 * whether the requested action has completed or not. Implementations
Gilles Peskinebe061332019-07-18 13:52:30 +0200161 * should return #PSA_SUCCESS on successful completion whenever
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100162 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
163 * if the requested action was completed successfully in an external
164 * cryptoprocessor but there was a breakdown of communication before
165 * the cryptoprocessor could report the status to the application.
166 */
David Saadab4ecc272019-02-14 13:48:10 +0200167#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100168
169/** There was a storage failure that may have led to data loss.
170 *
171 * This error indicates that some persistent storage is corrupted.
172 * It should not be used for a corruption of volatile memory
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200173 * (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100174 * between the cryptoprocessor and its external storage (use
175 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
176 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
177 *
178 * Note that a storage failure does not indicate that any data that was
179 * previously read is invalid. However this previously read data may no
180 * longer be readable from storage.
181 *
182 * When a storage failure occurs, it is no longer possible to ensure
183 * the global integrity of the keystore. Depending on the global
184 * integrity guarantees offered by the implementation, access to other
185 * data may or may not fail even if the data is still readable but
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100186 * its integrity cannot be guaranteed.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100187 *
188 * Implementations should only use this error code to report a
189 * permanent storage corruption. However application writers should
190 * keep in mind that transient errors while reading the storage may be
191 * reported using this error code. */
David Saadab4ecc272019-02-14 13:48:10 +0200192#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100193
194/** A hardware failure was detected.
195 *
196 * A hardware failure may be transient or permanent depending on the
197 * cause. */
David Saadab4ecc272019-02-14 13:48:10 +0200198#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100199
200/** A tampering attempt was detected.
201 *
202 * If an application receives this error code, there is no guarantee
203 * that previously accessed or computed data was correct and remains
204 * confidential. Applications should not perform any security function
205 * and should enter a safe failure state.
206 *
207 * Implementations may return this error code if they detect an invalid
208 * state that cannot happen during normal operation and that indicates
209 * that the implementation's security guarantees no longer hold. Depending
210 * on the implementation architecture and on its security and safety goals,
211 * the implementation may forcibly terminate the application.
212 *
213 * This error code is intended as a last resort when a security breach
214 * is detected and it is unsure whether the keystore data is still
215 * protected. Implementations shall only return this error code
216 * to report an alarm from a tampering detector, to indicate that
217 * the confidentiality of stored data can no longer be guaranteed,
218 * or to indicate that the integrity of previously returned data is now
219 * considered compromised. Implementations shall not use this error code
220 * to indicate a hardware failure that merely makes it impossible to
221 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
222 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
223 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
224 * instead).
225 *
226 * This error indicates an attack against the application. Implementations
227 * shall not return this error code as a consequence of the behavior of
228 * the application itself. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200229#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100230
231/** There is not enough entropy to generate random data needed
232 * for the requested action.
233 *
234 * This error indicates a failure of a hardware random generator.
235 * Application writers should note that this error can be returned not
236 * only by functions whose purpose is to generate random data, such
237 * as key, IV or nonce generation, but also by functions that execute
238 * an algorithm with a randomized result, as well as functions that
239 * use randomization of intermediate computations as a countermeasure
240 * to certain attacks.
241 *
242 * Implementations should avoid returning this error after psa_crypto_init()
243 * has succeeded. Implementations should generate sufficient
244 * entropy during initialization and subsequently use a cryptographically
245 * secure pseudorandom generator (PRNG). However implementations may return
246 * this error at any time if a policy requires the PRNG to be reseeded
247 * during normal operation. */
David Saadab4ecc272019-02-14 13:48:10 +0200248#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100249
250/** The signature, MAC or hash is incorrect.
251 *
252 * Verification functions return this error if the verification
253 * calculations completed successfully, and the value to be verified
254 * was determined to be incorrect.
255 *
256 * If the value to verify has an invalid size, implementations may return
257 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
David Saadab4ecc272019-02-14 13:48:10 +0200258#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100259
260/** The decrypted padding is incorrect.
261 *
262 * \warning In some protocols, when decrypting data, it is essential that
263 * the behavior of the application does not depend on whether the padding
264 * is correct, down to precise timing. Applications should prefer
265 * protocols that use authenticated encryption rather than plain
266 * encryption. If the application must perform a decryption of
267 * unauthenticated data, the application writer should take care not
268 * to reveal whether the padding is invalid.
269 *
270 * Implementations should strive to make valid and invalid padding
271 * as close as possible to indistinguishable to an external observer.
272 * In particular, the timing of a decryption operation should not
273 * depend on the validity of the padding. */
David Saadab4ecc272019-02-14 13:48:10 +0200274#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100275
David Saadab4ecc272019-02-14 13:48:10 +0200276/** Return this error when there's insufficient data when attempting
277 * to read from a resource. */
278#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100279
Ronald Croncf56a0a2020-08-04 09:51:30 +0200280/** The key identifier is not valid. See also :ref:\`key-handles\`.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100281 */
David Saadab4ecc272019-02-14 13:48:10 +0200282#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100283
gabor-mezei-arm3d8b4f52020-11-09 16:36:46 +0100284/** Stored data has been corrupted.
285 *
286 * This error indicates that some persistent storage has suffered corruption.
287 * It does not indicate the following situations, which have specific error
288 * codes:
289 *
290 * - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.
291 * - A communication error between the cryptoprocessor and its external
292 * storage - use #PSA_ERROR_COMMUNICATION_FAILURE.
293 * - When the storage is in a valid state but is full - use
294 * #PSA_ERROR_INSUFFICIENT_STORAGE.
295 * - When the storage fails for other reasons - use
296 * #PSA_ERROR_STORAGE_FAILURE.
297 * - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.
298 *
299 * \note A storage corruption does not indicate that any data that was
300 * previously read is invalid. However this previously read data might no
301 * longer be readable from storage.
302 *
303 * When a storage failure occurs, it is no longer possible to ensure the
304 * global integrity of the keystore.
305 */
306#define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)
307
gabor-mezei-armfe309242020-11-09 17:39:56 +0100308/** Data read from storage is not valid for the implementation.
309 *
310 * This error indicates that some data read from storage does not have a valid
311 * format. It does not indicate the following situations, which have specific
312 * error codes:
313 *
314 * - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT
315 * - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE
316 * - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT
317 *
318 * This error is typically a result of either storage corruption on a
319 * cleartext storage backend, or an attempt to read data that was
320 * written by an incompatible version of the library.
321 */
322#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
323
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100324/**@}*/
325
326/** \defgroup crypto_types Key and algorithm types
327 * @{
328 */
329
Gilles Peskine79733992022-06-20 18:41:20 +0200330/* Note that key type values, including ECC family and DH group values, are
331 * embedded in the persistent key store, as part of key metadata. As a
332 * consequence, they must not be changed (unless the storage format version
333 * changes).
334 */
335
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100336/** An invalid key type value.
337 *
338 * Zero is not the encoding of any key type.
339 */
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100340#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x0000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100341
Andrew Thoelkedd49cf92019-09-24 13:11:49 +0100342/** Vendor-defined key type flag.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100343 *
344 * Key types defined by this standard will never have the
345 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
346 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
347 * respect the bitwise structure used by standard encodings whenever practical.
348 */
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100349#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x8000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100350
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100351#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7000)
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100352#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x1000)
353#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x2000)
354#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x4000)
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100355#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x7000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100356
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100357#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x3000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100358
Andrew Thoelkedd49cf92019-09-24 13:11:49 +0100359/** Whether a key type is vendor-defined.
360 *
361 * See also #PSA_KEY_TYPE_VENDOR_FLAG.
362 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100363#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
364 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
365
366/** Whether a key type is an unstructured array of bytes.
367 *
368 * This encompasses both symmetric keys and non-key data.
369 */
370#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100371 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \
372 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100373
374/** Whether a key type is asymmetric: either a key pair or a public key. */
375#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
376 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
377 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
378 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
379/** Whether a key type is the public part of a key pair. */
380#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
381 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
382/** Whether a key type is a key pair containing a private part and a public
383 * part. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200384#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100385 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
386/** The key pair type corresponding to a public key type.
387 *
388 * You may also pass a key pair type as \p type, it will be left unchanged.
389 *
390 * \param type A public key type or key pair type.
391 *
392 * \return The corresponding key pair type.
393 * If \p type is not a public key or a key pair,
394 * the return value is undefined.
395 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200396#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100397 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
398/** The public key type corresponding to a key pair type.
399 *
400 * You may also pass a key pair type as \p type, it will be left unchanged.
401 *
402 * \param type A public key type or key pair type.
403 *
404 * \return The corresponding public key type.
405 * If \p type is not a public key or a key pair,
406 * the return value is undefined.
407 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200408#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100409 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
410
411/** Raw data.
412 *
413 * A "key" of this type cannot be used for any cryptographic operation.
414 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100415#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x1001)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100416
417/** HMAC key.
418 *
419 * The key policy determines which underlying hash algorithm the key can be
420 * used for.
421 *
422 * HMAC keys should generally have the same size as the underlying hash.
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100423 * This size can be calculated with #PSA_HASH_LENGTH(\c alg) where
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100424 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100425#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x1100)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100426
427/** A secret for key derivation.
428 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +0200429 * This key type is for high-entropy secrets only. For low-entropy secrets,
430 * #PSA_KEY_TYPE_PASSWORD should be used instead.
431 *
432 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or
433 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.
434 *
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100435 * The key policy determines which key derivation algorithm the key
436 * can be used for.
437 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100438#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x1200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100439
Manuel Pégourié-Gonnard31cbbef2021-04-20 11:18:25 +0200440/** A low-entropy secret for password hashing or key derivation.
441 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +0200442 * This key type is suitable for passwords and passphrases which are typically
443 * intended to be memorizable by humans, and have a low entropy relative to
444 * their size. It can be used for randomly generated or derived keys with
Manuel Pégourié-Gonnardf9a68ad2021-05-07 12:11:38 +0200445 * maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +0200446 * for such keys. It is not suitable for passwords with extremely low entropy,
447 * such as numerical PINs.
448 *
449 * These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of
450 * key derivation algorithms. Algorithms that accept such an input were
451 * designed to accept low-entropy secret and are known as password hashing or
452 * key stretching algorithms.
453 *
454 * These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of
455 * key derivation algorithms, as the algorithms that take such an input expect
456 * it to be high-entropy.
457 *
458 * The key policy determines which key derivation algorithm the key can be
459 * used for, among the permissible subset defined above.
Manuel Pégourié-Gonnard31cbbef2021-04-20 11:18:25 +0200460 */
Manuel Pégourié-Gonnardc16033e2021-04-30 11:59:40 +0200461#define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t)0x1203)
Manuel Pégourié-Gonnard31cbbef2021-04-20 11:18:25 +0200462
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +0200463/** A secret value that can be used to verify a password hash.
464 *
465 * The key policy determines which key derivation algorithm the key
466 * can be used for, among the same permissible subset as for
467 * #PSA_KEY_TYPE_PASSWORD.
468 */
469#define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t)0x1205)
470
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +0200471/** A secret value that can be used in when computing a password hash.
Manuel Pégourié-Gonnard31cbbef2021-04-20 11:18:25 +0200472 *
473 * The key policy determines which key derivation algorithm the key
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +0200474 * can be used for, among the subset of algorithms that can use pepper.
Manuel Pégourié-Gonnard31cbbef2021-04-20 11:18:25 +0200475 */
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +0200476#define PSA_KEY_TYPE_PEPPER ((psa_key_type_t)0x1206)
Manuel Pégourié-Gonnard31cbbef2021-04-20 11:18:25 +0200477
Gilles Peskine737c6be2019-05-21 16:01:06 +0200478/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100479 *
480 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
481 * 32 bytes (AES-256).
482 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100483#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x2400)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100484
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200485/** Key for a cipher, AEAD or MAC algorithm based on the
486 * ARIA block cipher. */
487#define PSA_KEY_TYPE_ARIA ((psa_key_type_t)0x2406)
488
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100489/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
490 *
Gilles Peskine7e54a292021-03-16 18:21:34 +0100491 * The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or
492 * 192 bits (3-key 3DES).
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100493 *
494 * Note that single DES and 2-key 3DES are weak and strongly
495 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
496 * is weak and deprecated and should only be used in legacy protocols.
497 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100498#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x2301)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100499
Gilles Peskine737c6be2019-05-21 16:01:06 +0200500/** Key for a cipher, AEAD or MAC algorithm based on the
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100501 * Camellia block cipher. */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100502#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x2403)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100503
Gilles Peskine3e79c8e2019-05-06 15:20:04 +0200504/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.
505 *
506 * ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.
507 *
Gilles Peskine14d35542022-03-10 18:36:37 +0100508 * \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports
509 * 12-byte nonces.
510 *
511 * \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt
512 * with the initial counter value 1, you can process and discard a
513 * 64-byte block before the real data.
Gilles Peskine3e79c8e2019-05-06 15:20:04 +0200514 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100515#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t)0x2004)
Gilles Peskine3e79c8e2019-05-06 15:20:04 +0200516
Gilles Peskine6a427bf2021-03-16 18:19:18 +0100517/** RSA public key.
518 *
519 * The size of an RSA key is the bit size of the modulus.
520 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100521#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x4001)
Gilles Peskine6a427bf2021-03-16 18:19:18 +0100522/** RSA key pair (private and public key).
523 *
524 * The size of an RSA key is the bit size of the modulus.
525 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100526#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t)0x7001)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100527/** Whether a key type is an RSA key (pair or public-only). */
528#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200529 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100530
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100531#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x4100)
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100532#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t)0x7100)
533#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x00ff)
Andrew Thoelke214064e2019-09-25 22:16:21 +0100534/** Elliptic curve key pair.
535 *
Gilles Peskine6a427bf2021-03-16 18:19:18 +0100536 * The size of an elliptic curve key is the bit size associated with the curve,
537 * i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.
538 * See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.
539 *
Paul Elliott8ff510a2020-06-02 17:19:28 +0100540 * \param curve A value of type ::psa_ecc_family_t that
541 * identifies the ECC curve to be used.
Andrew Thoelke214064e2019-09-25 22:16:21 +0100542 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200543#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \
544 (PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))
Andrew Thoelke214064e2019-09-25 22:16:21 +0100545/** Elliptic curve public key.
546 *
Gilles Peskine6a427bf2021-03-16 18:19:18 +0100547 * The size of an elliptic curve public key is the same as the corresponding
548 * private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of
549 * `PSA_ECC_FAMILY_xxx` curve families).
550 *
Paul Elliott8ff510a2020-06-02 17:19:28 +0100551 * \param curve A value of type ::psa_ecc_family_t that
552 * identifies the ECC curve to be used.
Andrew Thoelke214064e2019-09-25 22:16:21 +0100553 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100554#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
555 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
556
557/** Whether a key type is an elliptic curve key (pair or public-only). */
558#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200559 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100560 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine5e9c9cc2018-12-12 14:02:48 +0100561/** Whether a key type is an elliptic curve key pair. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200562#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100563 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200564 PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)
Gilles Peskine5e9c9cc2018-12-12 14:02:48 +0100565/** Whether a key type is an elliptic curve public key. */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100566#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
567 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
568 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
569
570/** Extract the curve from an elliptic curve key type. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100571#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
572 ((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100573 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
574 0))
575
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +0100576/** Check if the curve of given family is Weierstrass elliptic curve. */
577#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
578
Gilles Peskine228abc52019-12-03 17:24:19 +0100579/** SEC Koblitz curves over prime fields.
580 *
581 * This family comprises the following curves:
582 * secp192k1, secp224k1, secp256k1.
583 * They are defined in _Standards for Efficient Cryptography_,
584 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
585 * https://www.secg.org/sec2-v2.pdf
586 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100587#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
Gilles Peskine228abc52019-12-03 17:24:19 +0100588
589/** SEC random curves over prime fields.
590 *
591 * This family comprises the following curves:
592 * secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
593 * They are defined in _Standards for Efficient Cryptography_,
594 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
595 * https://www.secg.org/sec2-v2.pdf
596 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100597#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
Gilles Peskine228abc52019-12-03 17:24:19 +0100598/* SECP160R2 (SEC2 v1, obsolete) */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100599#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
Gilles Peskine228abc52019-12-03 17:24:19 +0100600
601/** SEC Koblitz curves over binary fields.
602 *
603 * This family comprises the following curves:
604 * sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
605 * They are defined in _Standards for Efficient Cryptography_,
606 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
607 * https://www.secg.org/sec2-v2.pdf
608 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100609#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
Gilles Peskine228abc52019-12-03 17:24:19 +0100610
611/** SEC random curves over binary fields.
612 *
613 * This family comprises the following curves:
614 * sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
615 * They are defined in _Standards for Efficient Cryptography_,
616 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
617 * https://www.secg.org/sec2-v2.pdf
618 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100619#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
Gilles Peskine228abc52019-12-03 17:24:19 +0100620
621/** SEC additional random curves over binary fields.
622 *
623 * This family comprises the following curve:
624 * sect163r2.
625 * It is defined in _Standards for Efficient Cryptography_,
626 * _SEC 2: Recommended Elliptic Curve Domain Parameters_.
627 * https://www.secg.org/sec2-v2.pdf
628 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100629#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
Gilles Peskine228abc52019-12-03 17:24:19 +0100630
631/** Brainpool P random curves.
632 *
633 * This family comprises the following curves:
634 * brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
635 * brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
636 * It is defined in RFC 5639.
637 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100638#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
Gilles Peskine228abc52019-12-03 17:24:19 +0100639
640/** Curve25519 and Curve448.
641 *
642 * This family comprises the following Montgomery curves:
643 * - 255-bit: Bernstein et al.,
644 * _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
645 * The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
646 * - 448-bit: Hamburg,
647 * _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
648 * The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
649 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100650#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
Gilles Peskine228abc52019-12-03 17:24:19 +0100651
Gilles Peskine67546802021-02-24 21:49:40 +0100652/** The twisted Edwards curves Ed25519 and Ed448.
653 *
Gilles Peskine3a1101a2021-02-24 21:52:21 +0100654 * These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,
Gilles Peskinea00abc62021-03-16 18:25:14 +0100655 * #PSA_ALG_ED25519PH for the 255-bit curve,
Gilles Peskine3a1101a2021-02-24 21:52:21 +0100656 * #PSA_ALG_ED448PH for the 448-bit curve).
Gilles Peskine67546802021-02-24 21:49:40 +0100657 *
658 * This family comprises the following twisted Edwards curves:
Gilles Peskinea00abc62021-03-16 18:25:14 +0100659 * - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent
Gilles Peskine67546802021-02-24 21:49:40 +0100660 * to Curve25519.
661 * Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.
662 * - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent
663 * to Curve448.
664 * Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
665 */
666#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)
667
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100668#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t)0x4200)
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100669#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t)0x7200)
670#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t)0x00ff)
Andrew Thoelke214064e2019-09-25 22:16:21 +0100671/** Diffie-Hellman key pair.
672 *
Paul Elliott75e27032020-06-03 15:17:39 +0100673 * \param group A value of type ::psa_dh_family_t that identifies the
Andrew Thoelke214064e2019-09-25 22:16:21 +0100674 * Diffie-Hellman group to be used.
675 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200676#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \
677 (PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))
Andrew Thoelke214064e2019-09-25 22:16:21 +0100678/** Diffie-Hellman public key.
679 *
Paul Elliott75e27032020-06-03 15:17:39 +0100680 * \param group A value of type ::psa_dh_family_t that identifies the
Andrew Thoelke214064e2019-09-25 22:16:21 +0100681 * Diffie-Hellman group to be used.
682 */
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200683#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \
684 (PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))
685
686/** Whether a key type is a Diffie-Hellman key (pair or public-only). */
687#define PSA_KEY_TYPE_IS_DH(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200688 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200689 ~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
690/** Whether a key type is a Diffie-Hellman key pair. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200691#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200692 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200693 PSA_KEY_TYPE_DH_KEY_PAIR_BASE)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200694/** Whether a key type is a Diffie-Hellman public key. */
695#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \
696 (((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \
697 PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)
698
699/** Extract the group from a Diffie-Hellman key type. */
Paul Elliott75e27032020-06-03 15:17:39 +0100700#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \
701 ((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200702 ((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \
703 0))
704
Gilles Peskine228abc52019-12-03 17:24:19 +0100705/** Diffie-Hellman groups defined in RFC 7919 Appendix A.
706 *
707 * This family includes groups with the following key sizes (in bits):
708 * 2048, 3072, 4096, 6144, 8192. A given implementation may support
709 * all of these sizes or only a subset.
710 */
Paul Elliott75e27032020-06-03 15:17:39 +0100711#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)
Gilles Peskine228abc52019-12-03 17:24:19 +0100712
Gilles Peskine2eea95c2019-12-02 17:44:12 +0100713#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100714 (((type) >> 8) & 7)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100715/** The block size of a block cipher.
716 *
717 * \param type A cipher key type (value of type #psa_key_type_t).
718 *
719 * \return The block size for a block cipher, or 1 for a stream cipher.
720 * The return value is undefined if \p type is not a supported
721 * cipher key type.
722 *
723 * \note It is possible to build stream cipher algorithms on top of a block
724 * cipher, for example CTR mode (#PSA_ALG_CTR).
725 * This macro only takes the key type into account, so it cannot be
726 * used to determine the size of the data that #psa_cipher_update()
727 * might buffer for future processing in general.
728 *
729 * \note This macro returns a compile-time constant if its argument is one.
730 *
731 * \warning This macro may evaluate its argument multiple times.
732 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100733#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
Gilles Peskine2eea95c2019-12-02 17:44:12 +0100734 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100735 1u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \
Gilles Peskine2eea95c2019-12-02 17:44:12 +0100736 0u)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100737
Gilles Peskine79733992022-06-20 18:41:20 +0200738/* Note that algorithm values are embedded in the persistent key store,
739 * as part of key metadata. As a consequence, they must not be changed
740 * (unless the storage format version changes).
741 */
742
Andrew Thoelkedd49cf92019-09-24 13:11:49 +0100743/** Vendor-defined algorithm flag.
744 *
745 * Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG
746 * bit set. Vendors who define additional algorithms must use an encoding with
747 * the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure
748 * used by standard encodings whenever practical.
749 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100750#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
Andrew Thoelkedd49cf92019-09-24 13:11:49 +0100751
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100752#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
Bence Szépkútia2945512020-12-03 21:40:17 +0100753#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x02000000)
754#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x03000000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100755#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
Bence Szépkútia2945512020-12-03 21:40:17 +0100756#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x05000000)
757#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x06000000)
758#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x07000000)
759#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x08000000)
760#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x09000000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100761
Andrew Thoelkedd49cf92019-09-24 13:11:49 +0100762/** Whether an algorithm is vendor-defined.
763 *
764 * See also #PSA_ALG_VENDOR_FLAG.
765 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100766#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
767 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
768
769/** Whether the specified algorithm is a hash algorithm.
770 *
771 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
772 *
773 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
774 * This macro may return either 0 or 1 if \p alg is not a supported
775 * algorithm identifier.
776 */
777#define PSA_ALG_IS_HASH(alg) \
778 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
779
780/** Whether the specified algorithm is a MAC algorithm.
781 *
782 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
783 *
784 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
785 * This macro may return either 0 or 1 if \p alg is not a supported
786 * algorithm identifier.
787 */
788#define PSA_ALG_IS_MAC(alg) \
789 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
790
791/** Whether the specified algorithm is a symmetric cipher algorithm.
792 *
793 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
794 *
795 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
796 * This macro may return either 0 or 1 if \p alg is not a supported
797 * algorithm identifier.
798 */
799#define PSA_ALG_IS_CIPHER(alg) \
800 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
801
802/** Whether the specified algorithm is an authenticated encryption
803 * with associated data (AEAD) algorithm.
804 *
805 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
806 *
807 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
808 * This macro may return either 0 or 1 if \p alg is not a supported
809 * algorithm identifier.
810 */
811#define PSA_ALG_IS_AEAD(alg) \
812 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
813
Gilles Peskine4eb05a42020-05-26 17:07:16 +0200814/** Whether the specified algorithm is an asymmetric signature algorithm,
Gilles Peskine6cc0a202020-05-05 16:05:26 +0200815 * also known as public-key signature algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100816 *
817 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
818 *
Gilles Peskine6cc0a202020-05-05 16:05:26 +0200819 * \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100820 * This macro may return either 0 or 1 if \p alg is not a supported
821 * algorithm identifier.
822 */
823#define PSA_ALG_IS_SIGN(alg) \
824 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
825
Gilles Peskine6cc0a202020-05-05 16:05:26 +0200826/** Whether the specified algorithm is an asymmetric encryption algorithm,
827 * also known as public-key encryption algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100828 *
829 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
830 *
Gilles Peskine6cc0a202020-05-05 16:05:26 +0200831 * \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100832 * This macro may return either 0 or 1 if \p alg is not a supported
833 * algorithm identifier.
834 */
835#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
836 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
837
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100838/** Whether the specified algorithm is a key agreement algorithm.
839 *
840 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
841 *
842 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
843 * This macro may return either 0 or 1 if \p alg is not a supported
844 * algorithm identifier.
845 */
846#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Gilles Peskine47e79fb2019-02-08 11:24:59 +0100847 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100848
849/** Whether the specified algorithm is a key derivation algorithm.
850 *
851 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
852 *
853 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
854 * This macro may return either 0 or 1 if \p alg is not a supported
855 * algorithm identifier.
856 */
857#define PSA_ALG_IS_KEY_DERIVATION(alg) \
858 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
859
Manuel Pégourié-Gonnard234b1ec2021-04-20 13:07:21 +0200860/** Whether the specified algorithm is a key stretching / password hashing
861 * algorithm.
862 *
863 * A key stretching / password hashing algorithm is a key derivation algorithm
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +0200864 * that is suitable for use with a low-entropy secret such as a password.
865 * Equivalently, it's a key derivation algorithm that uses a
866 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.
Manuel Pégourié-Gonnard234b1ec2021-04-20 13:07:21 +0200867 *
868 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
869 *
Andrew Thoelkea0f4b592021-06-24 16:47:14 +0100870 * \return 1 if \p alg is a key stretching / password hashing algorithm, 0
Manuel Pégourié-Gonnard234b1ec2021-04-20 13:07:21 +0200871 * otherwise. This macro may return either 0 or 1 if \p alg is not a
872 * supported algorithm identifier.
873 */
874#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \
875 (PSA_ALG_IS_KEY_DERIVATION(alg) && \
876 (alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)
877
Mateusz Starzyk359b5ab2021-08-26 12:52:56 +0200878/** An invalid algorithm identifier value. */
879#define PSA_ALG_NONE ((psa_algorithm_t)0)
880
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100881#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Adrian L. Shaw21e71452019-09-20 16:01:11 +0100882/** MD5 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100883#define PSA_ALG_MD5 ((psa_algorithm_t)0x02000003)
Adrian L. Shaw21e71452019-09-20 16:01:11 +0100884/** PSA_ALG_RIPEMD160 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100885#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x02000004)
Adrian L. Shaw21e71452019-09-20 16:01:11 +0100886/** SHA1 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100887#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x02000005)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100888/** SHA2-224 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100889#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x02000008)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100890/** SHA2-256 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100891#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x02000009)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100892/** SHA2-384 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100893#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0200000a)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100894/** SHA2-512 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100895#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0200000b)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100896/** SHA2-512/224 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100897#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0200000c)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100898/** SHA2-512/256 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100899#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0200000d)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100900/** SHA3-224 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100901#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x02000010)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100902/** SHA3-256 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100903#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x02000011)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100904/** SHA3-384 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100905#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x02000012)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100906/** SHA3-512 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100907#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x02000013)
Gilles Peskine27354692021-03-03 17:45:06 +0100908/** The first 512 bits (64 bytes) of the SHAKE256 output.
Gilles Peskine3a1101a2021-02-24 21:52:21 +0100909 *
910 * This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other
911 * scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512
912 * has the same output size and a (theoretically) higher security strength.
913 */
Gilles Peskine27354692021-03-03 17:45:06 +0100914#define PSA_ALG_SHAKE256_512 ((psa_algorithm_t)0x02000015)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100915
Gilles Peskine763fb9a2019-01-28 13:29:01 +0100916/** In a hash-and-sign algorithm policy, allow any hash algorithm.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100917 *
Gilles Peskine763fb9a2019-01-28 13:29:01 +0100918 * This value may be used to form the algorithm usage field of a policy
919 * for a signature algorithm that is parametrized by a hash. The key
920 * may then be used to perform operations using the same signature
921 * algorithm parametrized with any supported hash.
922 *
923 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
Gilles Peskineacd2d0e2021-10-04 18:10:38 +0200924 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100925 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
Gilles Peskine763fb9a2019-01-28 13:29:01 +0100926 * Then you may create and use a key as follows:
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100927 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
928 * ```
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100929 * psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY
Gilles Peskine80b39ae2019-05-15 16:09:46 +0200930 * psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100931 * ```
932 * - Import or generate key material.
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100933 * - Call psa_sign_hash() or psa_verify_hash(), passing
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100934 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
935 * call to sign or verify a message may use a different hash.
936 * ```
Ronald Croncf56a0a2020-08-04 09:51:30 +0200937 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
938 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
939 * psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100940 * ```
941 *
942 * This value may not be used to build other algorithms that are
943 * parametrized over a hash. For any valid use of this macro to build
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100944 * an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100945 *
946 * This value may not be used to build an algorithm specification to
947 * perform an operation. It is only valid to build policies.
948 */
Bence Szépkútia2945512020-12-03 21:40:17 +0100949#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x020000ff)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100950
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100951#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Bence Szépkútia2945512020-12-03 21:40:17 +0100952#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x03800000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100953/** Macro to build an HMAC algorithm.
954 *
955 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
956 *
957 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
958 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
959 *
960 * \return The corresponding HMAC algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100961 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100962 * hash algorithm.
963 */
964#define PSA_ALG_HMAC(hash_alg) \
965 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
966
967#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
968 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
969
970/** Whether the specified algorithm is an HMAC algorithm.
971 *
972 * HMAC is a family of MAC algorithms that are based on a hash function.
973 *
974 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
975 *
976 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
977 * This macro may return either 0 or 1 if \p alg is not a supported
978 * algorithm identifier.
979 */
980#define PSA_ALG_IS_HMAC(alg) \
981 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
982 PSA_ALG_HMAC_BASE)
983
984/* In the encoding of a MAC algorithm, the bits corresponding to
985 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
986 * truncated. As an exception, the value 0 means the untruncated algorithm,
987 * whatever its length is. The length is encoded in 6 bits, so it can
988 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
989 * to full length is correctly encoded as 0 and any non-trivial truncation
990 * is correctly encoded as a value between 1 and 63. */
Bence Szépkútia2945512020-12-03 21:40:17 +0100991#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x003f0000)
992#define PSA_MAC_TRUNCATION_OFFSET 16
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100993
Steven Cooremand927ed72021-02-22 19:59:35 +0100994/* In the encoding of a MAC algorithm, the bit corresponding to
995 * #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
Steven Cooreman328f11c2021-03-02 11:44:51 +0100996 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
997 * algorithm policy can be used with any algorithm corresponding to the
Steven Cooremand927ed72021-02-22 19:59:35 +0100998 * same base class and having a (potentially truncated) MAC length greater or
999 * equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */
1000#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
1001
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001002/** Macro to build a truncated MAC algorithm.
1003 *
1004 * A truncated MAC algorithm is identical to the corresponding MAC
1005 * algorithm except that the MAC value for the truncated algorithm
1006 * consists of only the first \p mac_length bytes of the MAC value
1007 * for the untruncated algorithm.
1008 *
1009 * \note This macro may allow constructing algorithm identifiers that
1010 * are not valid, either because the specified length is larger
1011 * than the untruncated MAC or because the specified length is
1012 * smaller than permitted by the implementation.
1013 *
1014 * \note It is implementation-defined whether a truncated MAC that
1015 * is truncated to the same length as the MAC of the untruncated
1016 * algorithm is considered identical to the untruncated algorithm
1017 * for policy comparison purposes.
1018 *
Gilles Peskine434899f2018-10-19 11:30:26 +02001019 * \param mac_alg A MAC algorithm identifier (value of type
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001020 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001021 * is true). This may be a truncated or untruncated
1022 * MAC algorithm.
1023 * \param mac_length Desired length of the truncated MAC in bytes.
1024 * This must be at most the full length of the MAC
1025 * and must be at least an implementation-specified
1026 * minimum. The implementation-specified minimum
1027 * shall not be zero.
1028 *
1029 * \return The corresponding MAC algorithm with the specified
1030 * length.
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001031 * \return Unspecified if \p mac_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001032 * MAC algorithm or if \p mac_length is too small or
1033 * too large for the specified MAC algorithm.
1034 */
Steven Cooreman328f11c2021-03-02 11:44:51 +01001035#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
1036 (((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1037 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001038 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
1039
1040/** Macro to build the base MAC algorithm corresponding to a truncated
1041 * MAC algorithm.
1042 *
Gilles Peskine434899f2018-10-19 11:30:26 +02001043 * \param mac_alg A MAC algorithm identifier (value of type
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001044 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001045 * is true). This may be a truncated or untruncated
1046 * MAC algorithm.
1047 *
1048 * \return The corresponding base MAC algorithm.
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001049 * \return Unspecified if \p mac_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001050 * MAC algorithm.
1051 */
Steven Cooreman328f11c2021-03-02 11:44:51 +01001052#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
1053 ((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \
1054 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001055
1056/** Length to which a MAC algorithm is truncated.
1057 *
Gilles Peskine434899f2018-10-19 11:30:26 +02001058 * \param mac_alg A MAC algorithm identifier (value of type
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001059 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001060 * is true).
1061 *
1062 * \return Length of the truncated MAC in bytes.
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001063 * \return 0 if \p mac_alg is a non-truncated MAC algorithm.
1064 * \return Unspecified if \p mac_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001065 * MAC algorithm.
1066 */
Gilles Peskine434899f2018-10-19 11:30:26 +02001067#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
1068 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001069
Steven Cooremanee18b1f2021-02-08 11:44:21 +01001070/** Macro to build a MAC minimum-MAC-length wildcard algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001071 *
Steven Cooremana1d83222021-02-25 10:20:29 +01001072 * A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms
Steven Cooremanee18b1f2021-02-08 11:44:21 +01001073 * sharing the same base algorithm, and where the (potentially truncated) MAC
1074 * length of the specific algorithm is equal to or larger then the wildcard
1075 * algorithm's minimum MAC length.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001076 *
Steven Cooreman37389c72021-02-18 12:08:41 +01001077 * \note When setting the minimum required MAC length to less than the
1078 * smallest MAC length allowed by the base algorithm, this effectively
1079 * becomes an 'any-MAC-length-allowed' policy for that base algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001080 *
Steven Cooreman37389c72021-02-18 12:08:41 +01001081 * \param mac_alg A MAC algorithm identifier (value of type
1082 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)
1083 * is true).
1084 * \param min_mac_length Desired minimum length of the message authentication
1085 * code in bytes. This must be at most the untruncated
1086 * length of the MAC and must be at least 1.
1087 *
1088 * \return The corresponding MAC wildcard algorithm with the
1089 * specified minimum length.
1090 * \return Unspecified if \p mac_alg is not a supported MAC
1091 * algorithm or if \p min_mac_length is less than 1 or
1092 * too large for the specified MAC algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001093 */
Steven Cooreman328f11c2021-03-02 11:44:51 +01001094#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \
1095 ( PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \
1096 PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001097
Bence Szépkútia2945512020-12-03 21:40:17 +01001098#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x03c00000)
Adrian L. Shawfd2aed42019-07-11 15:47:40 +01001099/** The CBC-MAC construction over a block cipher
1100 *
1101 * \warning CBC-MAC is insecure in many cases.
1102 * A more secure mode, such as #PSA_ALG_CMAC, is recommended.
1103 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001104#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x03c00100)
Adrian L. Shawfd2aed42019-07-11 15:47:40 +01001105/** The CMAC construction over a block cipher */
Bence Szépkútia2945512020-12-03 21:40:17 +01001106#define PSA_ALG_CMAC ((psa_algorithm_t)0x03c00200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001107
1108/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
1109 *
1110 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1111 *
1112 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
1113 * This macro may return either 0 or 1 if \p alg is not a supported
1114 * algorithm identifier.
1115 */
1116#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
1117 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
1118 PSA_ALG_CIPHER_MAC_BASE)
1119
1120#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
1121#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1122
1123/** Whether the specified algorithm is a stream cipher.
1124 *
1125 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
1126 * by applying a bitwise-xor with a stream of bytes that is generated
1127 * from a key.
1128 *
1129 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1130 *
1131 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
1132 * This macro may return either 0 or 1 if \p alg is not a supported
1133 * algorithm identifier or if it is not a symmetric cipher algorithm.
1134 */
1135#define PSA_ALG_IS_STREAM_CIPHER(alg) \
1136 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
1137 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
1138
Bence Szépkúti1de907d2020-12-07 18:20:28 +01001139/** The stream cipher mode of a stream cipher algorithm.
1140 *
1141 * The underlying stream cipher is determined by the key type.
Bence Szépkúti99ffb2b2020-12-08 00:08:31 +01001142 * - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001143 */
Bence Szépkúti1de907d2020-12-07 18:20:28 +01001144#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800100)
Gilles Peskine3e79c8e2019-05-06 15:20:04 +02001145
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001146/** The CTR stream cipher mode.
1147 *
1148 * CTR is a stream cipher which is built from a block cipher.
1149 * The underlying block cipher is determined by the key type.
1150 * For example, to use AES-128-CTR, use this algorithm with
1151 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
1152 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001153#define PSA_ALG_CTR ((psa_algorithm_t)0x04c01000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001154
Adrian L. Shawfd2aed42019-07-11 15:47:40 +01001155/** The CFB stream cipher mode.
1156 *
1157 * The underlying block cipher is determined by the key type.
1158 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001159#define PSA_ALG_CFB ((psa_algorithm_t)0x04c01100)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001160
Adrian L. Shawfd2aed42019-07-11 15:47:40 +01001161/** The OFB stream cipher mode.
1162 *
1163 * The underlying block cipher is determined by the key type.
1164 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001165#define PSA_ALG_OFB ((psa_algorithm_t)0x04c01200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001166
1167/** The XTS cipher mode.
1168 *
1169 * XTS is a cipher mode which is built from a block cipher. It requires at
1170 * least one full block of input, but beyond this minimum the input
1171 * does not need to be a whole number of blocks.
1172 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001173#define PSA_ALG_XTS ((psa_algorithm_t)0x0440ff00)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001174
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02001175/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.
1176 *
Steven Cooremana6033e92020-08-25 11:47:50 +02001177 * \warning ECB mode does not protect the confidentiality of the encrypted data
1178 * except in extremely narrow circumstances. It is recommended that applications
1179 * only use ECB if they need to construct an operating mode that the
1180 * implementation does not provide. Implementations are encouraged to provide
1181 * the modes that applications need in preference to supporting direct access
1182 * to ECB.
1183 *
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02001184 * The underlying block cipher is determined by the key type.
1185 *
Steven Cooremana6033e92020-08-25 11:47:50 +02001186 * This symmetric cipher mode can only be used with messages whose lengths are a
1187 * multiple of the block size of the chosen block cipher.
1188 *
1189 * ECB mode does not accept an initialization vector (IV). When using a
1190 * multi-part cipher operation with this algorithm, psa_cipher_generate_iv()
1191 * and psa_cipher_set_iv() must not be called.
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02001192 */
1193#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t)0x04404400)
1194
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001195/** The CBC block cipher chaining mode, with no padding.
1196 *
1197 * The underlying block cipher is determined by the key type.
1198 *
1199 * This symmetric cipher mode can only be used with messages whose lengths
1200 * are whole number of blocks for the chosen block cipher.
1201 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001202#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04404000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001203
1204/** The CBC block cipher chaining mode with PKCS#7 padding.
1205 *
1206 * The underlying block cipher is determined by the key type.
1207 *
1208 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
1209 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001210#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04404100)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001211
Gilles Peskine679693e2019-05-06 15:10:16 +02001212#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
1213
1214/** Whether the specified algorithm is an AEAD mode on a block cipher.
1215 *
1216 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1217 *
1218 * \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on
1219 * a block cipher, 0 otherwise.
1220 * This macro may return either 0 or 1 if \p alg is not a supported
1221 * algorithm identifier.
1222 */
1223#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \
1224 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \
1225 (PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))
1226
Gilles Peskine9153ec02019-02-15 13:02:02 +01001227/** The CCM authenticated encryption algorithm.
Adrian L. Shawfd2aed42019-07-11 15:47:40 +01001228 *
1229 * The underlying block cipher is determined by the key type.
Gilles Peskine9153ec02019-02-15 13:02:02 +01001230 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001231#define PSA_ALG_CCM ((psa_algorithm_t)0x05500100)
Gilles Peskine9153ec02019-02-15 13:02:02 +01001232
Mateusz Starzyk594215b2021-10-14 12:23:06 +02001233/** The CCM* cipher mode without authentication.
1234 *
1235 * This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.
1236 * For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.
1237 *
1238 * The underlying block cipher is determined by the key type.
1239 *
1240 * Currently only 13-byte long IV's are supported.
1241 */
1242#define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t)0x04c01300)
1243
Gilles Peskine9153ec02019-02-15 13:02:02 +01001244/** The GCM authenticated encryption algorithm.
Adrian L. Shawfd2aed42019-07-11 15:47:40 +01001245 *
1246 * The underlying block cipher is determined by the key type.
Gilles Peskine9153ec02019-02-15 13:02:02 +01001247 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001248#define PSA_ALG_GCM ((psa_algorithm_t)0x05500200)
Gilles Peskine679693e2019-05-06 15:10:16 +02001249
1250/** The Chacha20-Poly1305 AEAD algorithm.
1251 *
1252 * The ChaCha20_Poly1305 construction is defined in RFC 7539.
Gilles Peskine3e79c8e2019-05-06 15:20:04 +02001253 *
1254 * Implementations must support 12-byte nonces, may support 8-byte nonces,
1255 * and should reject other sizes.
1256 *
1257 * Implementations must support 16-byte tags and should reject other sizes.
Gilles Peskine679693e2019-05-06 15:10:16 +02001258 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001259#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t)0x05100500)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001260
1261/* In the encoding of a AEAD algorithm, the bits corresponding to
1262 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
1263 * The constants for default lengths follow this encoding.
1264 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001265#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x003f0000)
1266#define PSA_AEAD_TAG_LENGTH_OFFSET 16
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001267
Steven Cooremand927ed72021-02-22 19:59:35 +01001268/* In the encoding of an AEAD algorithm, the bit corresponding to
1269 * #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm
Steven Cooreman328f11c2021-03-02 11:44:51 +01001270 * is a wildcard algorithm. A key with such wildcard algorithm as permitted
1271 * algorithm policy can be used with any algorithm corresponding to the
Steven Cooremand927ed72021-02-22 19:59:35 +01001272 * same base class and having a tag length greater than or equal to the one
1273 * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */
1274#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000)
1275
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001276/** Macro to build a shortened AEAD algorithm.
1277 *
1278 * A shortened AEAD algorithm is similar to the corresponding AEAD
1279 * algorithm, but has an authentication tag that consists of fewer bytes.
1280 * Depending on the algorithm, the tag length may affect the calculation
1281 * of the ciphertext.
1282 *
Gilles Peskine434899f2018-10-19 11:30:26 +02001283 * \param aead_alg An AEAD algorithm identifier (value of type
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001284 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001285 * is true).
1286 * \param tag_length Desired length of the authentication tag in bytes.
1287 *
1288 * \return The corresponding AEAD algorithm with the specified
1289 * length.
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001290 * \return Unspecified if \p aead_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001291 * AEAD algorithm or if \p tag_length is not valid
1292 * for the specified AEAD algorithm.
1293 */
Bence Szépkútia63b20d2020-12-16 11:36:46 +01001294#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \
Steven Cooreman328f11c2021-03-02 11:44:51 +01001295 (((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \
1296 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001297 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
1298 PSA_ALG_AEAD_TAG_LENGTH_MASK))
1299
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001300/** Retrieve the tag length of a specified AEAD algorithm
1301 *
1302 * \param aead_alg An AEAD algorithm identifier (value of type
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001303 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001304 * is true).
1305 *
1306 * \return The tag length specified by the input algorithm.
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001307 * \return Unspecified if \p aead_alg is not a supported
Gilles Peskine87353432021-03-08 17:25:03 +01001308 * AEAD algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001309 */
1310#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \
1311 (((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \
1312 PSA_AEAD_TAG_LENGTH_OFFSET )
1313
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001314/** Calculate the corresponding AEAD algorithm with the default tag length.
1315 *
Gilles Peskine434899f2018-10-19 11:30:26 +02001316 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7ef23be2021-03-08 17:19:47 +01001317 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001318 *
Gilles Peskine434899f2018-10-19 11:30:26 +02001319 * \return The corresponding AEAD algorithm with the default
1320 * tag length for that algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001321 */
Bence Szépkútia63b20d2020-12-16 11:36:46 +01001322#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \
Unknowne2e19952019-08-21 03:33:04 -04001323 ( \
Bence Szépkútia63b20d2020-12-16 11:36:46 +01001324 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \
1325 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \
1326 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001327 0)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01001328#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \
1329 PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \
1330 PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001331 ref :
1332
Steven Cooremanee18b1f2021-02-08 11:44:21 +01001333/** Macro to build an AEAD minimum-tag-length wildcard algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001334 *
Steven Cooremana1d83222021-02-25 10:20:29 +01001335 * A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001336 * sharing the same base algorithm, and where the tag length of the specific
Steven Cooremanee18b1f2021-02-08 11:44:21 +01001337 * algorithm is equal to or larger then the minimum tag length specified by the
1338 * wildcard algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001339 *
Steven Cooreman37389c72021-02-18 12:08:41 +01001340 * \note When setting the minimum required tag length to less than the
1341 * smallest tag length allowed by the base algorithm, this effectively
1342 * becomes an 'any-tag-length-allowed' policy for that base algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001343 *
Steven Cooreman37389c72021-02-18 12:08:41 +01001344 * \param aead_alg An AEAD algorithm identifier (value of type
1345 * #psa_algorithm_t such that
1346 * #PSA_ALG_IS_AEAD(\p aead_alg) is true).
1347 * \param min_tag_length Desired minimum length of the authentication tag in
1348 * bytes. This must be at least 1 and at most the largest
1349 * allowed tag length of the algorithm.
1350 *
1351 * \return The corresponding AEAD wildcard algorithm with the
1352 * specified minimum length.
1353 * \return Unspecified if \p aead_alg is not a supported
1354 * AEAD algorithm or if \p min_tag_length is less than 1
1355 * or too large for the specified AEAD algorithm.
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001356 */
Steven Cooreman5d814812021-02-18 12:11:39 +01001357#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \
Steven Cooreman328f11c2021-03-02 11:44:51 +01001358 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \
1359 PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001360
Bence Szépkútia2945512020-12-03 21:40:17 +01001361#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x06000200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001362/** RSA PKCS#1 v1.5 signature with hashing.
1363 *
1364 * This is the signature scheme defined by RFC 8017
1365 * (PKCS#1: RSA Cryptography Specifications) under the name
1366 * RSASSA-PKCS1-v1_5.
1367 *
1368 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1369 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001370 * This includes #PSA_ALG_ANY_HASH
1371 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001372 *
1373 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001374 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001375 * hash algorithm.
1376 */
1377#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
1378 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1379/** Raw PKCS#1 v1.5 signature.
1380 *
1381 * The input to this algorithm is the DigestInfo structure used by
1382 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1383 * steps 3&ndash;6.
1384 */
1385#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
1386#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
1387 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
1388
Bence Szépkútia2945512020-12-03 21:40:17 +01001389#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x06000300)
Gilles Peskineacd2d0e2021-10-04 18:10:38 +02001390#define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t)0x06001300)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001391/** RSA PSS signature with hashing.
1392 *
1393 * This is the signature scheme defined by RFC 8017
1394 * (PKCS#1: RSA Cryptography Specifications) under the name
1395 * RSASSA-PSS, with the message generation function MGF1, and with
1396 * a salt length equal to the length of the hash. The specified
1397 * hash algorithm is used to hash the input message, to create the
1398 * salted hash, and for the mask generation.
1399 *
1400 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1401 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001402 * This includes #PSA_ALG_ANY_HASH
1403 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001404 *
1405 * \return The corresponding RSA PSS signature algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001406 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001407 * hash algorithm.
1408 */
1409#define PSA_ALG_RSA_PSS(hash_alg) \
1410 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskineacd2d0e2021-10-04 18:10:38 +02001411
1412/** RSA PSS signature with hashing with relaxed verification.
1413 *
1414 * This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,
1415 * but allows an arbitrary salt length (including \c 0) when verifying a
1416 * signature.
1417 *
1418 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1419 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1420 * This includes #PSA_ALG_ANY_HASH
1421 * when specifying the algorithm in a usage policy.
1422 *
1423 * \return The corresponding RSA PSS signature algorithm.
1424 * \return Unspecified if \p hash_alg is not a supported
1425 * hash algorithm.
1426 */
1427#define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \
1428 (PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1429
1430/** Whether the specified algorithm is RSA PSS with standard salt.
1431 *
1432 * \param alg An algorithm value or an algorithm policy wildcard.
1433 *
1434 * \return 1 if \p alg is of the form
1435 * #PSA_ALG_RSA_PSS(\c hash_alg),
1436 * where \c hash_alg is a hash algorithm or
1437 * #PSA_ALG_ANY_HASH. 0 otherwise.
1438 * This macro may return either 0 or 1 if \p alg is not
1439 * a supported algorithm identifier or policy.
1440 */
1441#define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001442 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1443
Gilles Peskineacd2d0e2021-10-04 18:10:38 +02001444/** Whether the specified algorithm is RSA PSS with any salt.
1445 *
1446 * \param alg An algorithm value or an algorithm policy wildcard.
1447 *
1448 * \return 1 if \p alg is of the form
1449 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1450 * where \c hash_alg is a hash algorithm or
1451 * #PSA_ALG_ANY_HASH. 0 otherwise.
1452 * This macro may return either 0 or 1 if \p alg is not
1453 * a supported algorithm identifier or policy.
1454 */
1455#define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \
1456 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)
1457
1458/** Whether the specified algorithm is RSA PSS.
1459 *
1460 * This includes any of the RSA PSS algorithm variants, regardless of the
1461 * constraints on salt length.
1462 *
1463 * \param alg An algorithm value or an algorithm policy wildcard.
1464 *
1465 * \return 1 if \p alg is of the form
1466 * #PSA_ALG_RSA_PSS(\c hash_alg) or
1467 * #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),
1468 * where \c hash_alg is a hash algorithm or
1469 * #PSA_ALG_ANY_HASH. 0 otherwise.
1470 * This macro may return either 0 or 1 if \p alg is not
1471 * a supported algorithm identifier or policy.
1472 */
1473#define PSA_ALG_IS_RSA_PSS(alg) \
Gilles Peskinef6892de2021-10-08 16:28:32 +02001474 (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \
1475 PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))
Gilles Peskineacd2d0e2021-10-04 18:10:38 +02001476
Bence Szépkútia2945512020-12-03 21:40:17 +01001477#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x06000600)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001478/** ECDSA signature with hashing.
1479 *
1480 * This is the ECDSA signature scheme defined by ANSI X9.62,
1481 * with a random per-message secret number (*k*).
1482 *
1483 * The representation of the signature as a byte string consists of
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001484 * the concatenation of the signature values *r* and *s*. Each of
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001485 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1486 * of the base point of the curve in octets. Each value is represented
1487 * in big-endian order (most significant octet first).
1488 *
1489 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1490 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001491 * This includes #PSA_ALG_ANY_HASH
1492 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001493 *
1494 * \return The corresponding ECDSA signature algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001495 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001496 * hash algorithm.
1497 */
1498#define PSA_ALG_ECDSA(hash_alg) \
1499 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1500/** ECDSA signature without hashing.
1501 *
1502 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1503 * without specifying a hash algorithm. This algorithm may only be
1504 * used to sign or verify a sequence of bytes that should be an
1505 * already-calculated hash. Note that the input is padded with
1506 * zeros on the left or truncated on the left as required to fit
1507 * the curve size.
1508 */
1509#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
Bence Szépkútia2945512020-12-03 21:40:17 +01001510#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x06000700)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001511/** Deterministic ECDSA signature with hashing.
1512 *
1513 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1514 *
1515 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1516 *
1517 * Note that when this algorithm is used for verification, signatures
1518 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1519 * same private key are accepted. In other words,
1520 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1521 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1522 *
1523 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1524 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001525 * This includes #PSA_ALG_ANY_HASH
1526 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001527 *
1528 * \return The corresponding deterministic ECDSA signature
1529 * algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001530 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001531 * hash algorithm.
1532 */
1533#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1534 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Bence Szépkútia2945512020-12-03 21:40:17 +01001535#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00000100)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001536#define PSA_ALG_IS_ECDSA(alg) \
Gilles Peskine972630e2019-11-29 11:55:48 +01001537 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001538 PSA_ALG_ECDSA_BASE)
1539#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
Gilles Peskine972630e2019-11-29 11:55:48 +01001540 (((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001541#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1542 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1543#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1544 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1545
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001546/** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),
1547 * using standard parameters.
1548 *
1549 * Contexts are not supported in the current version of this specification
1550 * because there is no suitable signature interface that can take the
1551 * context as a parameter. A future version of this specification may add
1552 * suitable functions and extend this algorithm to support contexts.
1553 *
1554 * PureEdDSA requires an elliptic curve key on a twisted Edwards curve.
1555 * In this specification, the following curves are supported:
1556 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified
1557 * in RFC 8032.
1558 * The curve is Edwards25519.
1559 * The hash function used internally is SHA-512.
1560 * - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified
1561 * in RFC 8032.
1562 * The curve is Edwards448.
1563 * The hash function used internally is the first 114 bytes of the
Gilles Peskinee5fde542021-03-16 18:40:36 +01001564 * SHAKE256 output.
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001565 *
1566 * This algorithm can be used with psa_sign_message() and
1567 * psa_verify_message(). Since there is no prehashing, it cannot be used
1568 * with psa_sign_hash() or psa_verify_hash().
1569 *
1570 * The signature format is the concatenation of R and S as defined by
1571 * RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte
1572 * string for Ed448).
1573 */
1574#define PSA_ALG_PURE_EDDSA ((psa_algorithm_t)0x06000800)
1575
1576#define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t)0x06000900)
1577#define PSA_ALG_IS_HASH_EDDSA(alg) \
1578 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)
1579
1580/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
Gilles Peskinee36f8aa2021-03-01 10:20:20 +01001581 * using SHA-512 and the Edwards25519 curve.
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001582 *
1583 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1584 *
1585 * This algorithm is Ed25519 as specified in RFC 8032.
1586 * The curve is Edwards25519.
Gilles Peskineb13ead82021-03-01 10:28:29 +01001587 * The prehash is SHA-512.
Gilles Peskinee5fde542021-03-16 18:40:36 +01001588 * The hash function used internally is SHA-512.
Gilles Peskineb13ead82021-03-01 10:28:29 +01001589 *
1590 * This is a hash-and-sign algorithm: to calculate a signature,
1591 * you can either:
1592 * - call psa_sign_message() on the message;
1593 * - or calculate the SHA-512 hash of the message
1594 * with psa_hash_compute()
1595 * or with a multi-part hash operation started with psa_hash_setup(),
1596 * using the hash algorithm #PSA_ALG_SHA_512,
1597 * then sign the calculated hash with psa_sign_hash().
1598 * Verifying a signature is similar, using psa_verify_message() or
1599 * psa_verify_hash() instead of the signature function.
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001600 */
1601#define PSA_ALG_ED25519PH \
1602 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))
1603
1604/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),
1605 * using SHAKE256 and the Edwards448 curve.
1606 *
1607 * See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.
1608 *
1609 * This algorithm is Ed448 as specified in RFC 8032.
1610 * The curve is Edwards448.
Gilles Peskineb13ead82021-03-01 10:28:29 +01001611 * The prehash is the first 64 bytes of the SHAKE256 output.
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001612 * The hash function used internally is the first 114 bytes of the
Gilles Peskinee5fde542021-03-16 18:40:36 +01001613 * SHAKE256 output.
Gilles Peskineb13ead82021-03-01 10:28:29 +01001614 *
1615 * This is a hash-and-sign algorithm: to calculate a signature,
1616 * you can either:
1617 * - call psa_sign_message() on the message;
1618 * - or calculate the first 64 bytes of the SHAKE256 output of the message
1619 * with psa_hash_compute()
1620 * or with a multi-part hash operation started with psa_hash_setup(),
Gilles Peskine27354692021-03-03 17:45:06 +01001621 * using the hash algorithm #PSA_ALG_SHAKE256_512,
Gilles Peskineb13ead82021-03-01 10:28:29 +01001622 * then sign the calculated hash with psa_sign_hash().
1623 * Verifying a signature is similar, using psa_verify_message() or
1624 * psa_verify_hash() instead of the signature function.
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001625 */
1626#define PSA_ALG_ED448PH \
Gilles Peskine27354692021-03-03 17:45:06 +01001627 (PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))
Gilles Peskine3a1101a2021-02-24 21:52:21 +01001628
Gilles Peskine6d400852021-02-24 21:39:52 +01001629/* Default definition, to be overridden if the library is extended with
1630 * more hash-and-sign algorithms that we want to keep out of this header
1631 * file. */
1632#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 0
1633
Gilles Peskinef2fe31a2021-09-22 16:42:02 +02001634/** Whether the specified algorithm is a signature algorithm that can be used
1635 * with psa_sign_hash() and psa_verify_hash().
1636 *
1637 * This encompasses all strict hash-and-sign algorithms categorized by
1638 * PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the
1639 * paradigm more loosely:
1640 * - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)
1641 * - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)
1642 *
1643 * \param alg An algorithm identifier (value of type psa_algorithm_t).
1644 *
1645 * \return 1 if alg is a signature algorithm that can be used to sign a
1646 * hash. 0 if alg is a signature algorithm that can only be used
1647 * to sign a message. 0 if alg is not a signature algorithm.
1648 * This macro can return either 0 or 1 if alg is not a
1649 * supported algorithm identifier.
1650 */
1651#define PSA_ALG_IS_SIGN_HASH(alg) \
1652 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1653 PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \
1654 PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))
1655
1656/** Whether the specified algorithm is a signature algorithm that can be used
1657 * with psa_sign_message() and psa_verify_message().
1658 *
1659 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1660 *
1661 * \return 1 if alg is a signature algorithm that can be used to sign a
1662 * message. 0 if \p alg is a signature algorithm that can only be used
1663 * to sign an already-calculated hash. 0 if \p alg is not a signature
1664 * algorithm. This macro can return either 0 or 1 if \p alg is not a
1665 * supported algorithm identifier.
1666 */
1667#define PSA_ALG_IS_SIGN_MESSAGE(alg) \
1668 (PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA )
1669
Gilles Peskined35b4892019-01-14 16:02:15 +01001670/** Whether the specified algorithm is a hash-and-sign algorithm.
1671 *
Gilles Peskine6cc0a202020-05-05 16:05:26 +02001672 * Hash-and-sign algorithms are asymmetric (public-key) signature algorithms
1673 * structured in two parts: first the calculation of a hash in a way that
1674 * does not depend on the key, then the calculation of a signature from the
Gilles Peskinef7b41372021-09-22 16:15:05 +02001675 * hash value and the key. Hash-and-sign algorithms encode the hash
1676 * used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH
1677 * to extract this algorithm.
1678 *
1679 * Thus, for a hash-and-sign algorithm,
1680 * `psa_sign_message(key, alg, input, ...)` is equivalent to
1681 * ```
1682 * psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);
1683 * psa_sign_hash(key, alg, hash, ..., signature, ...);
1684 * ```
1685 * Most usefully, separating the hash from the signature allows the hash
1686 * to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()
1687 * and psa_hash_finish(). Likewise psa_verify_message() is equivalent to
1688 * calculating the hash and then calling psa_verify_hash().
Gilles Peskined35b4892019-01-14 16:02:15 +01001689 *
1690 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1691 *
1692 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1693 * This macro may return either 0 or 1 if \p alg is not a supported
1694 * algorithm identifier.
1695 */
1696#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
Gilles Peskinef7b41372021-09-22 16:15:05 +02001697 (PSA_ALG_IS_SIGN_HASH(alg) && \
1698 ((alg) & PSA_ALG_HASH_MASK) != 0)
Gilles Peskined35b4892019-01-14 16:02:15 +01001699
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001700/** Get the hash used by a hash-and-sign signature algorithm.
1701 *
1702 * A hash-and-sign algorithm is a signature algorithm which is
1703 * composed of two phases: first a hashing phase which does not use
1704 * the key and produces a hash of the input message, then a signing
1705 * phase which only uses the hash and the key and not the message
1706 * itself.
1707 *
1708 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1709 * #PSA_ALG_IS_SIGN(\p alg) is true).
1710 *
1711 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1712 * algorithm.
1713 * \return 0 if \p alg is a signature algorithm that does not
1714 * follow the hash-and-sign structure.
1715 * \return Unspecified if \p alg is not a signature algorithm or
1716 * if it is not supported by the implementation.
1717 */
1718#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskined35b4892019-01-14 16:02:15 +01001719 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001720 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1721 0)
1722
1723/** RSA PKCS#1 v1.5 encryption.
1724 */
Bence Szépkútia2945512020-12-03 21:40:17 +01001725#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x07000200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001726
Bence Szépkútia2945512020-12-03 21:40:17 +01001727#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x07000300)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001728/** RSA OAEP encryption.
1729 *
1730 * This is the encryption scheme defined by RFC 8017
1731 * (PKCS#1: RSA Cryptography Specifications) under the name
1732 * RSAES-OAEP, with the message generation function MGF1.
1733 *
1734 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1735 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1736 * for MGF1.
1737 *
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +02001738 * \return The corresponding RSA OAEP encryption algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001739 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001740 * hash algorithm.
1741 */
1742#define PSA_ALG_RSA_OAEP(hash_alg) \
1743 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1744#define PSA_ALG_IS_RSA_OAEP(alg) \
1745 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1746#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1747 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1748 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1749 0)
1750
Bence Szépkútia2945512020-12-03 21:40:17 +01001751#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x08000100)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001752/** Macro to build an HKDF algorithm.
1753 *
1754 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1755 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001756 * This key derivation algorithm uses the following inputs:
Gilles Peskine03410b52019-05-16 16:05:19 +02001757 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001758 * It is optional; if omitted, the derivation uses an empty salt.
Gilles Peskine03410b52019-05-16 16:05:19 +02001759 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.
1760 * - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.
1761 * You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.
1762 * You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001763 * starting to generate output.
1764 *
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001765 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1766 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1767 *
1768 * \return The corresponding HKDF algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001769 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001770 * hash algorithm.
1771 */
1772#define PSA_ALG_HKDF(hash_alg) \
1773 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1774/** Whether the specified algorithm is an HKDF algorithm.
1775 *
1776 * HKDF is a family of key derivation algorithms that are based on a hash
1777 * function and the HMAC construction.
1778 *
1779 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1780 *
1781 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1782 * This macro may return either 0 or 1 if \c alg is not a supported
1783 * key derivation algorithm identifier.
1784 */
1785#define PSA_ALG_IS_HKDF(alg) \
1786 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1787#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1788 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1789
Bence Szépkútia2945512020-12-03 21:40:17 +01001790#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x08000200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001791/** Macro to build a TLS-1.2 PRF algorithm.
1792 *
1793 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1794 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1795 * used with either SHA-256 or SHA-384.
1796 *
Gilles Peskineed87d312019-05-29 17:32:39 +02001797 * This key derivation algorithm uses the following inputs, which must be
1798 * passed in the order given here:
1799 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
Gilles Peskine2cb9e392019-05-21 15:58:13 +02001800 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1801 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
Gilles Peskine2cb9e392019-05-21 15:58:13 +02001802 *
1803 * For the application to TLS-1.2 key expansion, the seed is the
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001804 * concatenation of ServerHello.Random + ClientHello.Random,
Gilles Peskine2cb9e392019-05-21 15:58:13 +02001805 * and the label is "key expansion".
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001806 *
1807 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1808 * TLS 1.2 PRF using HMAC-SHA-256.
1809 *
1810 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1811 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1812 *
1813 * \return The corresponding TLS-1.2 PRF algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001814 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001815 * hash algorithm.
1816 */
1817#define PSA_ALG_TLS12_PRF(hash_alg) \
1818 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1819
1820/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1821 *
1822 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1823 *
1824 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1825 * This macro may return either 0 or 1 if \c alg is not a supported
1826 * key derivation algorithm identifier.
1827 */
1828#define PSA_ALG_IS_TLS12_PRF(alg) \
1829 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1830#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1831 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1832
Bence Szépkútia2945512020-12-03 21:40:17 +01001833#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x08000300)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001834/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1835 *
1836 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1837 * from the PreSharedKey (PSK) through the application of padding
1838 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1839 * The latter is based on HMAC and can be used with either SHA-256
1840 * or SHA-384.
1841 *
Gilles Peskineed87d312019-05-29 17:32:39 +02001842 * This key derivation algorithm uses the following inputs, which must be
1843 * passed in the order given here:
1844 * - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.
Przemek Stekiel37c81c42022-04-07 13:38:53 +02001845 * - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the
1846 * computation of the premaster secret. This input is optional;
1847 * if omitted, it defaults to a string of null bytes with the same length
1848 * as the secret (PSK) input.
Gilles Peskine2cb9e392019-05-21 15:58:13 +02001849 * - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.
1850 * - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.
Gilles Peskine2cb9e392019-05-21 15:58:13 +02001851 *
1852 * For the application to TLS-1.2, the seed (which is
1853 * forwarded to the TLS-1.2 PRF) is the concatenation of the
1854 * ClientHello.Random + ServerHello.Random,
Przemek Stekiel37c81c42022-04-07 13:38:53 +02001855 * the label is "master secret" or "extended master secret" and
1856 * the other secret depends on the key exchange specified in the cipher suite:
1857 * - for a plain PSK cipher suite (RFC 4279, Section 2), omit
1858 * PSA_KEY_DERIVATION_INPUT_OTHER_SECRET
1859 * - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite
1860 * (RFC 5489, Section 2), the other secret should be the output of the
1861 * PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.
1862 * The recommended way to pass this input is to use a key derivation
1863 * algorithm constructed as
1864 * PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))
1865 * and to call psa_key_derivation_key_agreement(). Alternatively,
1866 * this input may be an output of `psa_raw_key_agreement()` passed with
1867 * psa_key_derivation_input_bytes(), or an equivalent input passed with
1868 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key().
1869 * - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret
1870 * should be the 48-byte client challenge (the PreMasterSecret of
1871 * (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and
1872 * a 46-byte random string chosen by the client. On the server, this is
1873 * typically an output of psa_asymmetric_decrypt() using
1874 * PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation
1875 * with `psa_key_derivation_input_bytes()`.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001876 *
1877 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1878 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1879 *
1880 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1881 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1882 *
1883 * \return The corresponding TLS-1.2 PSK to MS algorithm.
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001884 * \return Unspecified if \p hash_alg is not a supported
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001885 * hash algorithm.
1886 */
1887#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1888 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1889
1890/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1891 *
1892 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1893 *
1894 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1895 * This macro may return either 0 or 1 if \c alg is not a supported
1896 * key derivation algorithm identifier.
1897 */
1898#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1899 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1900#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1901 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1902
Manuel Pégourié-Gonnard234b1ec2021-04-20 13:07:21 +02001903/* This flag indicates whether the key derivation algorithm is suitable for
1904 * use on low-entropy secrets such as password - these algorithms are also
1905 * known as key stretching or password hashing schemes. These are also the
1906 * algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.
Manuel Pégourié-Gonnard06638ae2021-05-04 10:19:37 +02001907 *
1908 * Those algorithms cannot be combined with a key agreement algorithm.
Manuel Pégourié-Gonnard234b1ec2021-04-20 13:07:21 +02001909 */
Manuel Pégourié-Gonnard06638ae2021-05-04 10:19:37 +02001910#define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t)0x00800000)
Manuel Pégourié-Gonnard234b1ec2021-04-20 13:07:21 +02001911
Manuel Pégourié-Gonnard06638ae2021-05-04 10:19:37 +02001912#define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t)0x08800100)
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02001913/** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.
Manuel Pégourié-Gonnard7da57912021-04-20 12:53:07 +02001914 *
1915 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02001916 * This macro specifies the PBKDF2 algorithm constructed using a PRF based on
1917 * HMAC with the specified hash.
1918 * For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA256)` specifies PBKDF2
1919 * using the PRF HMAC-SHA-256.
Manuel Pégourié-Gonnard7da57912021-04-20 12:53:07 +02001920 *
Manuel Pégourié-Gonnard3d722672021-04-30 12:42:36 +02001921 * This key derivation algorithm uses the following inputs, which must be
1922 * provided in the following order:
1923 * - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02001924 * This input step must be used exactly once.
1925 * - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.
1926 * This input step must be used one or more times; if used several times, the
1927 * inputs will be concatenated. This can be used to build the final salt
1928 * from multiple sources, both public and secret (also known as pepper).
Manuel Pégourié-Gonnard3d722672021-04-30 12:42:36 +02001929 * - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02001930 * This input step must be used exactly once.
Manuel Pégourié-Gonnard7da57912021-04-20 12:53:07 +02001931 *
1932 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1933 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1934 *
1935 * \return The corresponding PBKDF2-HMAC-XXX algorithm.
1936 * \return Unspecified if \p hash_alg is not a supported
1937 * hash algorithm.
1938 */
1939#define PSA_ALG_PBKDF2_HMAC(hash_alg) \
1940 (PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1941
1942/** Whether the specified algorithm is a PBKDF2-HMAC algorithm.
1943 *
1944 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1945 *
1946 * \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.
1947 * This macro may return either 0 or 1 if \c alg is not a supported
1948 * key derivation algorithm identifier.
1949 */
1950#define PSA_ALG_IS_PBKDF2_HMAC(alg) \
1951 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)
Manuel Pégourié-Gonnard7da57912021-04-20 12:53:07 +02001952
Manuel Pégourié-Gonnard6983b4f2021-05-03 11:41:49 +02001953/** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.
1954 *
1955 * PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).
1956 * This macro specifies the PBKDF2 algorithm constructed using the
1957 * AES-CMAC-PRF-128 PRF specified by RFC 4615.
1958 *
1959 * This key derivation algorithm uses the same inputs as
Manuel Pégourié-Gonnard5b79ee22021-05-04 10:34:56 +02001960 * #PSA_ALG_PBKDF2_HMAC() with the same constraints.
Manuel Pégourié-Gonnard6983b4f2021-05-03 11:41:49 +02001961 */
Manuel Pégourié-Gonnard06638ae2021-05-04 10:19:37 +02001962#define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t)0x08800200)
Manuel Pégourié-Gonnard6983b4f2021-05-03 11:41:49 +02001963
Bence Szépkútia2945512020-12-03 21:40:17 +01001964#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0xfe00ffff)
1965#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0xffff0000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001966
Gilles Peskine6843c292019-01-18 16:44:49 +01001967/** Macro to build a combined algorithm that chains a key agreement with
1968 * a key derivation.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001969 *
Gilles Peskine6843c292019-01-18 16:44:49 +01001970 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1971 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1972 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1973 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001974 *
Gilles Peskine6843c292019-01-18 16:44:49 +01001975 * \return The corresponding key agreement and derivation
1976 * algorithm.
1977 * \return Unspecified if \p ka_alg is not a supported
1978 * key agreement algorithm or \p kdf_alg is not a
1979 * supported key derivation algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001980 */
Gilles Peskine6843c292019-01-18 16:44:49 +01001981#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1982 ((ka_alg) | (kdf_alg))
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001983
1984#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1985 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1986
Gilles Peskine6843c292019-01-18 16:44:49 +01001987#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1988 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001989
Gilles Peskine47e79fb2019-02-08 11:24:59 +01001990/** Whether the specified algorithm is a raw key agreement algorithm.
1991 *
1992 * A raw key agreement algorithm is one that does not specify
1993 * a key derivation function.
1994 * Usually, raw key agreement algorithms are constructed directly with
1995 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
Ronald Cron96783552020-10-19 12:06:30 +02001996 * constructed with #PSA_ALG_KEY_AGREEMENT().
Gilles Peskine47e79fb2019-02-08 11:24:59 +01001997 *
1998 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1999 *
2000 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
2001 * This macro may return either 0 or 1 if \p alg is not a supported
2002 * algorithm identifier.
2003 */
Gilles Peskine6843c292019-01-18 16:44:49 +01002004#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
Gilles Peskine47e79fb2019-02-08 11:24:59 +01002005 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
2006 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
Gilles Peskine6843c292019-01-18 16:44:49 +01002007
2008#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
2009 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
2010
2011/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002012 *
Gilles Peskine2e37c0d2019-03-05 19:32:02 +01002013 * The shared secret produced by key agreement is
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002014 * `g^{ab}` in big-endian format.
2015 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
2016 * in bits.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002017 */
Bence Szépkútia2945512020-12-03 21:40:17 +01002018#define PSA_ALG_FFDH ((psa_algorithm_t)0x09010000)
Gilles Peskine6843c292019-01-18 16:44:49 +01002019
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002020/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
2021 *
Gilles Peskine2e37c0d2019-03-05 19:32:02 +01002022 * This includes the raw finite field Diffie-Hellman algorithm as well as
2023 * finite-field Diffie-Hellman followed by any supporter key derivation
2024 * algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002025 *
2026 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2027 *
2028 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
2029 * This macro may return either 0 or 1 if \c alg is not a supported
2030 * key agreement algorithm identifier.
2031 */
2032#define PSA_ALG_IS_FFDH(alg) \
Gilles Peskine6843c292019-01-18 16:44:49 +01002033 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002034
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002035/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
2036 *
Gilles Peskine6843c292019-01-18 16:44:49 +01002037 * The shared secret produced by key agreement is the x-coordinate of
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002038 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
2039 * `m` is the bit size associated with the curve, i.e. the bit size of the
2040 * order of the curve's coordinate field. When `m` is not a multiple of 8,
2041 * the byte containing the most significant bit of the shared secret
2042 * is padded with zero bits. The byte order is either little-endian
2043 * or big-endian depending on the curve type.
2044 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01002045 * - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002046 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2047 * in little-endian byte order.
2048 * The bit size is 448 for Curve448 and 255 for Curve25519.
2049 * - For Weierstrass curves over prime fields (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +01002050 * `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002051 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2052 * in big-endian byte order.
2053 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
2054 * - For Weierstrass curves over binary fields (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +01002055 * `PSA_ECC_FAMILY_SECTXXX`),
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002056 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
2057 * in big-endian byte order.
2058 * The bit size is `m` for the field `F_{2^m}`.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002059 */
Bence Szépkútia2945512020-12-03 21:40:17 +01002060#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
Gilles Peskine6843c292019-01-18 16:44:49 +01002061
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002062/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
2063 * algorithm.
2064 *
Gilles Peskine2e37c0d2019-03-05 19:32:02 +01002065 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
2066 * elliptic curve Diffie-Hellman followed by any supporter key derivation
2067 * algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002068 *
2069 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2070 *
2071 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
2072 * 0 otherwise.
2073 * This macro may return either 0 or 1 if \c alg is not a supported
2074 * key agreement algorithm identifier.
2075 */
2076#define PSA_ALG_IS_ECDH(alg) \
Gilles Peskine6843c292019-01-18 16:44:49 +01002077 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002078
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002079/** Whether the specified algorithm encoding is a wildcard.
2080 *
2081 * Wildcard values may only be used to set the usage algorithm field in
2082 * a policy, not to perform an operation.
2083 *
2084 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2085 *
2086 * \return 1 if \c alg is a wildcard algorithm encoding.
2087 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
2088 * an operation).
2089 * \return This macro may return either 0 or 1 if \c alg is not a supported
2090 * algorithm identifier.
2091 */
Steven Cooremand927ed72021-02-22 19:59:35 +01002092#define PSA_ALG_IS_WILDCARD(alg) \
2093 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
2094 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
2095 PSA_ALG_IS_MAC(alg) ? \
2096 (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
2097 PSA_ALG_IS_AEAD(alg) ? \
2098 (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \
Steven Cooremanee18b1f2021-02-08 11:44:21 +01002099 (alg) == PSA_ALG_ANY_HASH)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002100
Manuel Pégourié-Gonnard40b81bf2021-05-03 11:53:40 +02002101/** Get the hash used by a composite algorithm.
2102 *
2103 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
2104 *
2105 * \return The underlying hash algorithm if alg is a composite algorithm that
2106 * uses a hash algorithm.
2107 *
Manuel Pégourié-Gonnardf0c28ef2021-05-07 12:13:48 +02002108 * \return \c 0 if alg is not a composite algorithm that uses a hash.
Manuel Pégourié-Gonnard40b81bf2021-05-03 11:53:40 +02002109 */
2110#define PSA_ALG_GET_HASH(alg) \
Manuel Pégourié-Gonnardf0c28ef2021-05-07 12:13:48 +02002111 (((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t)0) : 0x02000000 | ((alg) & 0x000000ff))
Manuel Pégourié-Gonnard40b81bf2021-05-03 11:53:40 +02002112
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002113/**@}*/
2114
2115/** \defgroup key_lifetimes Key lifetimes
2116 * @{
2117 */
2118
Gilles Peskine79733992022-06-20 18:41:20 +02002119/* Note that location and persistence level values are embedded in the
2120 * persistent key store, as part of key metadata. As a consequence, they
2121 * must not be changed (unless the storage format version changes).
2122 */
2123
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002124/** The default lifetime for volatile keys.
2125 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002126 * A volatile key only exists as long as the identifier to it is not destroyed.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002127 * The key material is guaranteed to be erased on a power reset.
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002128 *
2129 * A key with this lifetime is typically stored in the RAM area of the
2130 * PSA Crypto subsystem. However this is an implementation choice.
2131 * If an implementation stores data about the key in a non-volatile memory,
2132 * it must release all the resources associated with the key and erase the
2133 * key material if the calling application terminates.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002134 */
2135#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
2136
Gilles Peskine5dcb74f2020-05-04 18:42:44 +02002137/** The default lifetime for persistent keys.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002138 *
2139 * A persistent key remains in storage until it is explicitly destroyed or
2140 * until the corresponding storage area is wiped. This specification does
Gilles Peskined0107b92020-08-18 23:05:06 +02002141 * not define any mechanism to wipe a storage area, but integrations may
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002142 * provide their own mechanism (for example to perform a factory reset,
2143 * to prepare for device refurbishment, or to uninstall an application).
2144 *
2145 * This lifetime value is the default storage area for the calling
Gilles Peskined0107b92020-08-18 23:05:06 +02002146 * application. Integrations of Mbed TLS may support other persistent lifetimes.
Gilles Peskine5dcb74f2020-05-04 18:42:44 +02002147 * See ::psa_key_lifetime_t for more information.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002148 */
2149#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
2150
Gilles Peskineaff11812020-05-04 19:03:10 +02002151/** The persistence level of volatile keys.
2152 *
2153 * See ::psa_key_persistence_t for more information.
2154 */
Gilles Peskinebbb3c182020-05-04 18:42:06 +02002155#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
Gilles Peskineaff11812020-05-04 19:03:10 +02002156
2157/** The default persistence level for persistent keys.
2158 *
2159 * See ::psa_key_persistence_t for more information.
2160 */
Gilles Peskineee04e692020-05-04 18:52:21 +02002161#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
Gilles Peskineaff11812020-05-04 19:03:10 +02002162
2163/** A persistence level indicating that a key is never destroyed.
2164 *
2165 * See ::psa_key_persistence_t for more information.
2166 */
Gilles Peskinebbb3c182020-05-04 18:42:06 +02002167#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002168
2169#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
Gilles Peskine4cfa4432020-05-06 13:44:32 +02002170 ((psa_key_persistence_t)((lifetime) & 0x000000ff))
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002171
2172#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
Gilles Peskine4cfa4432020-05-06 13:44:32 +02002173 ((psa_key_location_t)((lifetime) >> 8))
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002174
2175/** Whether a key lifetime indicates that the key is volatile.
2176 *
2177 * A volatile key is automatically destroyed by the implementation when
2178 * the application instance terminates. In particular, a volatile key
2179 * is automatically destroyed on a power reset of the device.
2180 *
2181 * A key that is not volatile is persistent. Persistent keys are
2182 * preserved until the application explicitly destroys them or until an
2183 * implementation-specific device management event occurs (for example,
2184 * a factory reset).
2185 *
2186 * \param lifetime The lifetime value to query (value of type
2187 * ::psa_key_lifetime_t).
2188 *
2189 * \return \c 1 if the key is volatile, otherwise \c 0.
2190 */
2191#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
2192 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
Steven Cooremandb064452020-06-01 12:29:26 +02002193 PSA_KEY_PERSISTENCE_VOLATILE)
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002194
Gilles Peskined133bb22021-04-21 20:05:59 +02002195/** Whether a key lifetime indicates that the key is read-only.
2196 *
2197 * Read-only keys cannot be created or destroyed through the PSA Crypto API.
2198 * They must be created through platform-specific means that bypass the API.
2199 *
2200 * Some platforms may offer ways to destroy read-only keys. For example,
Gilles Peskine91466c82021-06-07 23:21:50 +02002201 * consider a platform with multiple levels of privilege, where a
2202 * low-privilege application can use a key but is not allowed to destroy
2203 * it, and the platform exposes the key to the application with a read-only
2204 * lifetime. High-privilege code can destroy the key even though the
2205 * application sees the key as read-only.
Gilles Peskined133bb22021-04-21 20:05:59 +02002206 *
2207 * \param lifetime The lifetime value to query (value of type
2208 * ::psa_key_lifetime_t).
2209 *
2210 * \return \c 1 if the key is read-only, otherwise \c 0.
2211 */
2212#define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \
2213 (PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
2214 PSA_KEY_PERSISTENCE_READ_ONLY)
2215
Gilles Peskinec4ee2f32020-05-04 19:07:18 +02002216/** Construct a lifetime from a persistence level and a location.
2217 *
2218 * \param persistence The persistence level
2219 * (value of type ::psa_key_persistence_t).
2220 * \param location The location indicator
2221 * (value of type ::psa_key_location_t).
2222 *
2223 * \return The constructed lifetime value.
2224 */
2225#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
2226 ((location) << 8 | (persistence))
2227
Gilles Peskineaff11812020-05-04 19:03:10 +02002228/** The local storage area for persistent keys.
2229 *
2230 * This storage area is available on all systems that can store persistent
2231 * keys without delegating the storage to a third-party cryptoprocessor.
2232 *
2233 * See ::psa_key_location_t for more information.
2234 */
Gilles Peskineee04e692020-05-04 18:52:21 +02002235#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
Gilles Peskineaff11812020-05-04 19:03:10 +02002236
Gilles Peskinebbb3c182020-05-04 18:42:06 +02002237#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
Gilles Peskine2d2bb1d2020-02-05 19:07:18 +01002238
Gilles Peskine79733992022-06-20 18:41:20 +02002239/* Note that key identifier values are embedded in the
2240 * persistent key store, as part of key metadata. As a consequence, they
2241 * must not be changed (unless the storage format version changes).
2242 */
2243
Mateusz Starzykc5c5b932021-08-26 13:32:30 +02002244/** The null key identifier.
2245 */
2246#define PSA_KEY_ID_NULL ((psa_key_id_t)0)
Gilles Peskine4a231b82019-05-06 18:56:14 +02002247/** The minimum value for a key identifier chosen by the application.
2248 */
Ronald Cron039a98b2020-07-23 16:07:42 +02002249#define PSA_KEY_ID_USER_MIN ((psa_key_id_t)0x00000001)
Gilles Peskine280948a2019-05-16 15:27:14 +02002250/** The maximum value for a key identifier chosen by the application.
Gilles Peskine4a231b82019-05-06 18:56:14 +02002251 */
Ronald Cron039a98b2020-07-23 16:07:42 +02002252#define PSA_KEY_ID_USER_MAX ((psa_key_id_t)0x3fffffff)
Gilles Peskine280948a2019-05-16 15:27:14 +02002253/** The minimum value for a key identifier chosen by the implementation.
Gilles Peskine4a231b82019-05-06 18:56:14 +02002254 */
Ronald Cron039a98b2020-07-23 16:07:42 +02002255#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t)0x40000000)
Gilles Peskine280948a2019-05-16 15:27:14 +02002256/** The maximum value for a key identifier chosen by the implementation.
Gilles Peskine4a231b82019-05-06 18:56:14 +02002257 */
Ronald Cron039a98b2020-07-23 16:07:42 +02002258#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t)0x7fffffff)
Gilles Peskine4a231b82019-05-06 18:56:14 +02002259
Ronald Cron7424f0d2020-09-14 16:17:41 +02002260
2261#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2262
2263#define MBEDTLS_SVC_KEY_ID_INIT ( (psa_key_id_t)0 )
2264#define MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ( id )
2265#define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( id ) ( 0 )
2266
2267/** Utility to initialize a key identifier at runtime.
2268 *
2269 * \param unused Unused parameter.
2270 * \param key_id Identifier of the key.
2271 */
2272static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2273 unsigned int unused, psa_key_id_t key_id )
2274{
2275 (void)unused;
2276
2277 return( key_id );
2278}
2279
2280/** Compare two key identifiers.
2281 *
2282 * \param id1 First key identifier.
2283 * \param id2 Second key identifier.
2284 *
2285 * \return Non-zero if the two key identifier are equal, zero otherwise.
2286 */
2287static inline int mbedtls_svc_key_id_equal( mbedtls_svc_key_id_t id1,
2288 mbedtls_svc_key_id_t id2 )
2289{
2290 return( id1 == id2 );
2291}
2292
Ronald Cronc4d1b512020-07-31 11:26:37 +02002293/** Check whether a key identifier is null.
2294 *
2295 * \param key Key identifier.
2296 *
2297 * \return Non-zero if the key identifier is null, zero otherwise.
2298 */
2299static inline int mbedtls_svc_key_id_is_null( mbedtls_svc_key_id_t key )
2300{
2301 return( key == 0 );
2302}
2303
Ronald Cron7424f0d2020-09-14 16:17:41 +02002304#else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
2305
2306#define MBEDTLS_SVC_KEY_ID_INIT ( (mbedtls_svc_key_id_t){ 0, 0 } )
Antonio de Angelis67294742022-05-05 14:11:32 +01002307#define MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ( ( id ).MBEDTLS_PRIVATE(key_id) )
2308#define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( id ) ( ( id ).MBEDTLS_PRIVATE(owner) )
Ronald Cron7424f0d2020-09-14 16:17:41 +02002309
2310/** Utility to initialize a key identifier at runtime.
2311 *
2312 * \param owner_id Identifier of the key owner.
2313 * \param key_id Identifier of the key.
2314 */
2315static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(
2316 mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id )
2317{
Mateusz Starzyk363eb292021-05-19 17:32:44 +02002318 return( (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,
2319 .MBEDTLS_PRIVATE(owner) = owner_id } );
Ronald Cron7424f0d2020-09-14 16:17:41 +02002320}
2321
2322/** Compare two key identifiers.
2323 *
2324 * \param id1 First key identifier.
2325 * \param id2 Second key identifier.
2326 *
2327 * \return Non-zero if the two key identifier are equal, zero otherwise.
2328 */
2329static inline int mbedtls_svc_key_id_equal( mbedtls_svc_key_id_t id1,
2330 mbedtls_svc_key_id_t id2 )
2331{
Mateusz Starzyk363eb292021-05-19 17:32:44 +02002332 return( ( id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id) ) &&
2333 mbedtls_key_owner_id_equal( id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner) ) );
Ronald Cron7424f0d2020-09-14 16:17:41 +02002334}
2335
Ronald Cronc4d1b512020-07-31 11:26:37 +02002336/** Check whether a key identifier is null.
2337 *
2338 * \param key Key identifier.
2339 *
2340 * \return Non-zero if the key identifier is null, zero otherwise.
2341 */
2342static inline int mbedtls_svc_key_id_is_null( mbedtls_svc_key_id_t key )
2343{
Gilles Peskine52bb83e2021-05-28 12:59:49 +02002344 return( key.MBEDTLS_PRIVATE(key_id) == 0 );
Ronald Cronc4d1b512020-07-31 11:26:37 +02002345}
2346
Ronald Cron7424f0d2020-09-14 16:17:41 +02002347#endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002348
2349/**@}*/
2350
2351/** \defgroup policy Key policies
2352 * @{
2353 */
2354
Gilles Peskine79733992022-06-20 18:41:20 +02002355/* Note that key usage flags are embedded in the
2356 * persistent key store, as part of key metadata. As a consequence, they
2357 * must not be changed (unless the storage format version changes).
2358 */
2359
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002360/** Whether the key may be exported.
2361 *
2362 * A public key or the public part of a key pair may always be exported
2363 * regardless of the value of this permission flag.
2364 *
2365 * If a key does not have export permission, implementations shall not
2366 * allow the key to be exported in plain form from the cryptoprocessor,
2367 * whether through psa_export_key() or through a proprietary interface.
2368 * The key may however be exportable in a wrapped form, i.e. in a form
2369 * where it is encrypted by another key.
2370 */
2371#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
2372
Gilles Peskine8e0206a2019-05-14 14:24:28 +02002373/** Whether the key may be copied.
2374 *
Gilles Peskined6a8f5f2019-05-14 16:25:50 +02002375 * This flag allows the use of psa_copy_key() to make a copy of the key
Gilles Peskine8e0206a2019-05-14 14:24:28 +02002376 * with the same policy or a more restrictive policy.
2377 *
Gilles Peskined6a8f5f2019-05-14 16:25:50 +02002378 * For lifetimes for which the key is located in a secure element which
2379 * enforce the non-exportability of keys, copying a key outside the secure
2380 * element also requires the usage flag #PSA_KEY_USAGE_EXPORT.
2381 * Copying the key inside the secure element is permitted with just
2382 * #PSA_KEY_USAGE_COPY if the secure element supports it.
2383 * For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
Gilles Peskine8e0206a2019-05-14 14:24:28 +02002384 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
2385 * is sufficient to permit the copy.
2386 */
2387#define PSA_KEY_USAGE_COPY ((psa_key_usage_t)0x00000002)
2388
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002389/** Whether the key may be used to encrypt a message.
2390 *
2391 * This flag allows the key to be used for a symmetric encryption operation,
2392 * for an AEAD encryption-and-authentication operation,
2393 * or for an asymmetric encryption operation,
2394 * if otherwise permitted by the key's type and policy.
2395 *
2396 * For a key pair, this concerns the public key.
2397 */
2398#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
2399
2400/** Whether the key may be used to decrypt a message.
2401 *
2402 * This flag allows the key to be used for a symmetric decryption operation,
2403 * for an AEAD decryption-and-verification operation,
2404 * or for an asymmetric decryption operation,
2405 * if otherwise permitted by the key's type and policy.
2406 *
2407 * For a key pair, this concerns the private key.
2408 */
2409#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
2410
2411/** Whether the key may be used to sign a message.
2412 *
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002413 * This flag allows the key to be used for a MAC calculation operation or for
2414 * an asymmetric message signature operation, if otherwise permitted by the
2415 * key’s type and policy.
2416 *
2417 * For a key pair, this concerns the private key.
2418 */
2419#define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t)0x00000400)
2420
2421/** Whether the key may be used to verify a message.
2422 *
2423 * This flag allows the key to be used for a MAC verification operation or for
2424 * an asymmetric message signature verification operation, if otherwise
2425 * permitted by the key’s type and policy.
2426 *
2427 * For a key pair, this concerns the public key.
2428 */
2429#define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t)0x00000800)
2430
2431/** Whether the key may be used to sign a message.
2432 *
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002433 * This flag allows the key to be used for a MAC calculation operation
2434 * or for an asymmetric signature operation,
2435 * if otherwise permitted by the key's type and policy.
2436 *
2437 * For a key pair, this concerns the private key.
2438 */
Bence Szépkútia2945512020-12-03 21:40:17 +01002439#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t)0x00001000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002440
2441/** Whether the key may be used to verify a message signature.
2442 *
2443 * This flag allows the key to be used for a MAC verification operation
2444 * or for an asymmetric signature verification operation,
2445 * if otherwise permitted by by the key's type and policy.
2446 *
2447 * For a key pair, this concerns the public key.
2448 */
Bence Szépkútia2945512020-12-03 21:40:17 +01002449#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t)0x00002000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002450
Manuel Pégourié-Gonnard759438c2021-04-20 11:18:53 +02002451/** Whether the key may be used to derive other keys or produce a password
2452 * hash.
Andrew Thoelke52d18cd2021-06-25 11:03:57 +01002453 *
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01002454 * This flag allows the key to be used for a key derivation operation or for
2455 * a key agreement operation, if otherwise permitted by by the key's type and
2456 * policy.
Manuel Pégourié-Gonnard759438c2021-04-20 11:18:53 +02002457 *
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01002458 * If this flag is present on all keys used in calls to
2459 * psa_key_derivation_input_key() for a key derivation operation, then it
2460 * permits calling psa_key_derivation_output_bytes() or
2461 * psa_key_derivation_output_key() at the end of the operation.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002462 */
Bence Szépkútia2945512020-12-03 21:40:17 +01002463#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002464
Manuel Pégourié-Gonnard9023cac2021-05-03 10:23:12 +02002465/** Whether the key may be used to verify the result of a key derivation,
2466 * including password hashing.
Manuel Pégourié-Gonnard759438c2021-04-20 11:18:53 +02002467 *
Manuel Pégourié-Gonnard9023cac2021-05-03 10:23:12 +02002468 * This flag allows the key to be used:
Manuel Pégourié-Gonnard759438c2021-04-20 11:18:53 +02002469 *
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01002470 * This flag allows the key to be used in a key derivation operation, if
2471 * otherwise permitted by by the key's type and policy.
2472 *
2473 * If this flag is present on all keys used in calls to
2474 * psa_key_derivation_input_key() for a key derivation operation, then it
2475 * permits calling psa_key_derivation_verify_bytes() or
2476 * psa_key_derivation_verify_key() at the end of the operation.
Manuel Pégourié-Gonnard759438c2021-04-20 11:18:53 +02002477 */
Manuel Pégourié-Gonnard9023cac2021-05-03 10:23:12 +02002478#define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t)0x00008000)
Manuel Pégourié-Gonnard759438c2021-04-20 11:18:53 +02002479
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002480/**@}*/
2481
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002482/** \defgroup derivation Key derivation
2483 * @{
2484 */
2485
Gilles Peskine79733992022-06-20 18:41:20 +02002486/* Key input steps are not embedded in the persistent storage, so you can
2487 * change them if needed: it's only an ABI change. */
2488
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002489/** A secret input for key derivation.
2490 *
Gilles Peskine224b0d62019-09-23 18:13:17 +02002491 * This should be a key of type #PSA_KEY_TYPE_DERIVE
2492 * (passed to psa_key_derivation_input_key())
2493 * or the shared secret resulting from a key agreement
2494 * (obtained via psa_key_derivation_key_agreement()).
Gilles Peskine178c9aa2019-09-24 18:21:06 +02002495 *
2496 * The secret can also be a direct input (passed to
2497 * key_derivation_input_bytes()). In this case, the derivation operation
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01002498 * may not be used to derive keys: the operation will only allow
2499 * psa_key_derivation_output_bytes(),
2500 * psa_key_derivation_verify_bytes(), or
2501 * psa_key_derivation_verify_key(), but not
2502 * psa_key_derivation_output_key().
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002503 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002504#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t)0x0101)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002505
Manuel Pégourié-Gonnard5a679922021-04-20 11:30:11 +02002506/** A low-entropy secret input for password hashing / key stretching.
2507 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02002508 * This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to
2509 * psa_key_derivation_input_key()) or a direct input (passed to
2510 * psa_key_derivation_input_bytes()) that is a password or passphrase. It can
2511 * also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or
2512 * the shared secret resulting from a key agreement.
Manuel Pégourié-Gonnard5a679922021-04-20 11:30:11 +02002513 *
Manuel Pégourié-Gonnard730f62a2021-05-05 10:05:06 +02002514 * The secret can also be a direct input (passed to
2515 * key_derivation_input_bytes()). In this case, the derivation operation
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01002516 * may not be used to derive keys: the operation will only allow
2517 * psa_key_derivation_output_bytes(),
2518 * psa_key_derivation_verify_bytes(), or
2519 * psa_key_derivation_verify_key(), but not
2520 * psa_key_derivation_output_key().
Manuel Pégourié-Gonnard5a679922021-04-20 11:30:11 +02002521 */
2522#define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t)0x0102)
2523
Przemek Stekiel37c81c42022-04-07 13:38:53 +02002524/** A high-entropy additional secret input for key derivation.
2525 *
2526 * This is typically the shared secret resulting from a key agreement obtained
2527 * via `psa_key_derivation_key_agreement()`. It may alternatively be a key of
2528 * type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or
2529 * a direct input passed to `psa_key_derivation_input_bytes()`.
2530 */
2531#define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \
2532 ((psa_key_derivation_step_t)0x0103)
2533
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002534/** A label for key derivation.
2535 *
Gilles Peskine224b0d62019-09-23 18:13:17 +02002536 * This should be a direct input.
2537 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002538 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002539#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t)0x0201)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002540
2541/** A salt for key derivation.
2542 *
Gilles Peskine224b0d62019-09-23 18:13:17 +02002543 * This should be a direct input.
Manuel Pégourié-Gonnard5a679922021-04-20 11:30:11 +02002544 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or
2545 * #PSA_KEY_TYPE_PEPPER.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002546 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002547#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t)0x0202)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002548
2549/** An information string for key derivation.
2550 *
Gilles Peskine224b0d62019-09-23 18:13:17 +02002551 * This should be a direct input.
2552 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002553 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002554#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t)0x0203)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002555
Gilles Peskine2cb9e392019-05-21 15:58:13 +02002556/** A seed for key derivation.
2557 *
Gilles Peskine224b0d62019-09-23 18:13:17 +02002558 * This should be a direct input.
2559 * It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.
Gilles Peskine2cb9e392019-05-21 15:58:13 +02002560 */
2561#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
2562
Manuel Pégourié-Gonnard5a679922021-04-20 11:30:11 +02002563/** A cost parameter for password hashing / key stretching.
2564 *
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02002565 * This must be a direct input, passed to psa_key_derivation_input_integer().
Manuel Pégourié-Gonnard5a679922021-04-20 11:30:11 +02002566 */
2567#define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t)0x0205)
2568
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002569/**@}*/
2570
Bence Szépkútib639d432021-04-21 10:33:54 +02002571/** \defgroup helper_macros Helper macros
2572 * @{
2573 */
2574
2575/* Helper macros */
2576
2577/** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm
2578 * regardless of the tag length they encode.
2579 *
2580 * \param aead_alg_1 An AEAD algorithm identifier.
2581 * \param aead_alg_2 An AEAD algorithm identifier.
2582 *
2583 * \return 1 if both identifiers refer to the same AEAD algorithm,
2584 * 0 otherwise.
2585 * Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are
2586 * a supported AEAD algorithm.
2587 */
2588#define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \
2589 (!(((aead_alg_1) ^ (aead_alg_2)) & \
2590 ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))
2591
2592/**@}*/
2593
Gilles Peskinef3b731e2018-12-12 13:38:31 +01002594#endif /* PSA_CRYPTO_VALUES_H */