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