Path: blob/master/thirdparty/mbedtls/include/psa/crypto_values.h
9904 views
/**1* \file psa/crypto_values.h2*3* \brief PSA cryptography module: macros to build and analyze integer values.4*5* \note This file may not be included directly. Applications must6* include psa/crypto.h. Drivers must include the appropriate driver7* header file.8*9* This file contains portable definitions of macros to build and analyze10* values of integral types that encode properties of cryptographic keys,11* designations of cryptographic algorithms, and error codes returned by12* the library.13*14* Note that many of the constants defined in this file are embedded in15* the persistent key store, as part of key metadata (including usage16* policies). As a consequence, they must not be changed (unless the storage17* format version changes).18*19* This header file only defines preprocessor macros.20*/21/*22* Copyright The Mbed TLS Contributors23* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later24*/2526#ifndef PSA_CRYPTO_VALUES_H27#define PSA_CRYPTO_VALUES_H28#include "mbedtls/private_access.h"2930/** \defgroup error Error codes31* @{32*/3334/* PSA error codes */3536/* Error codes are standardized across PSA domains (framework, crypto, storage,37* etc.). Do not change the values in this section or even the expansions38* of each macro: it must be possible to `#include` both this header39* and some other PSA component's headers in the same C source,40* which will lead to duplicate definitions of the `PSA_SUCCESS` and41* `PSA_ERROR_xxx` macros, which is ok if and only if the macros expand42* to the same sequence of tokens.43*44* If you must add a new45* value, check with the Arm PSA framework group to pick one that other46* domains aren't already using. */4748/* Tell uncrustify not to touch the constant definitions, otherwise49* it might change the spacing to something that is not PSA-compliant50* (e.g. adding a space after casts).51*52* *INDENT-OFF*53*/5455/** The action was completed successfully. */56#define PSA_SUCCESS ((psa_status_t)0)5758/** An error occurred that does not correspond to any defined59* failure cause.60*61* Implementations may use this error code if none of the other standard62* error codes are applicable. */63#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)6465/** The requested operation or a parameter is not supported66* by this implementation.67*68* Implementations should return this error code when an enumeration69* parameter such as a key type, algorithm, etc. is not recognized.70* If a combination of parameters is recognized and identified as71* not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */72#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)7374/** The requested action is denied by a policy.75*76* Implementations should return this error code when the parameters77* are recognized as valid and supported, and a policy explicitly78* denies the requested operation.79*80* If a subset of the parameters of a function call identify a81* forbidden operation, and another subset of the parameters are82* not valid or not supported, it is unspecified whether the function83* returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or84* #PSA_ERROR_INVALID_ARGUMENT. */85#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)8687/** An output buffer is too small.88*89* Applications can call the \c PSA_xxx_SIZE macro listed in the function90* description to determine a sufficient buffer size.91*92* Implementations should preferably return this error code only93* in cases when performing the operation with a larger output94* buffer would succeed. However implementations may return this95* error if a function has invalid or unsupported parameters in addition96* to the parameters that determine the necessary output buffer size. */97#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)9899/** Asking for an item that already exists100*101* Implementations should return this error, when attempting102* to write an item (like a key) that already exists. */103#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)104105/** Asking for an item that doesn't exist106*107* Implementations should return this error, if a requested item (like108* a key) does not exist. */109#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)110111/** The requested action cannot be performed in the current state.112*113* Multipart operations return this error when one of the114* functions is called out of sequence. Refer to the function115* descriptions for permitted sequencing of functions.116*117* Implementations shall not return this error code to indicate118* that a key either exists or not,119* but shall instead return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST120* as applicable.121*122* Implementations shall not return this error code to indicate that a123* key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE124* instead. */125#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)126127/** The parameters passed to the function are invalid.128*129* Implementations may return this error any time a parameter or130* combination of parameters are recognized as invalid.131*132* Implementations shall not return this error code to indicate that a133* key identifier is invalid, but shall return #PSA_ERROR_INVALID_HANDLE134* instead.135*/136#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)137138/** There is not enough runtime memory.139*140* If the action is carried out across multiple security realms, this141* error can refer to available memory in any of the security realms. */142#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)143144/** There is not enough persistent storage.145*146* Functions that modify the key storage return this error code if147* there is insufficient storage space on the host media. In addition,148* many functions that do not otherwise access storage may return this149* error code if the implementation requires a mandatory log entry for150* the requested action and the log storage space is full. */151#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)152153/** There was a communication failure inside the implementation.154*155* This can indicate a communication failure between the application156* and an external cryptoprocessor or between the cryptoprocessor and157* an external volatile or persistent memory. A communication failure158* may be transient or permanent depending on the cause.159*160* \warning If a function returns this error, it is undetermined161* whether the requested action has completed or not. Implementations162* should return #PSA_SUCCESS on successful completion whenever163* possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE164* if the requested action was completed successfully in an external165* cryptoprocessor but there was a breakdown of communication before166* the cryptoprocessor could report the status to the application.167*/168#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)169170/** There was a storage failure that may have led to data loss.171*172* This error indicates that some persistent storage is corrupted.173* It should not be used for a corruption of volatile memory174* (use #PSA_ERROR_CORRUPTION_DETECTED), for a communication error175* between the cryptoprocessor and its external storage (use176* #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is177* in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).178*179* Note that a storage failure does not indicate that any data that was180* previously read is invalid. However this previously read data may no181* longer be readable from storage.182*183* When a storage failure occurs, it is no longer possible to ensure184* the global integrity of the keystore. Depending on the global185* integrity guarantees offered by the implementation, access to other186* data may or may not fail even if the data is still readable but187* its integrity cannot be guaranteed.188*189* Implementations should only use this error code to report a190* permanent storage corruption. However application writers should191* keep in mind that transient errors while reading the storage may be192* reported using this error code. */193#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)194195/** A hardware failure was detected.196*197* A hardware failure may be transient or permanent depending on the198* cause. */199#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)200201/** A tampering attempt was detected.202*203* If an application receives this error code, there is no guarantee204* that previously accessed or computed data was correct and remains205* confidential. Applications should not perform any security function206* and should enter a safe failure state.207*208* Implementations may return this error code if they detect an invalid209* state that cannot happen during normal operation and that indicates210* that the implementation's security guarantees no longer hold. Depending211* on the implementation architecture and on its security and safety goals,212* the implementation may forcibly terminate the application.213*214* This error code is intended as a last resort when a security breach215* is detected and it is unsure whether the keystore data is still216* protected. Implementations shall only return this error code217* to report an alarm from a tampering detector, to indicate that218* the confidentiality of stored data can no longer be guaranteed,219* or to indicate that the integrity of previously returned data is now220* considered compromised. Implementations shall not use this error code221* to indicate a hardware failure that merely makes it impossible to222* perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,223* #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,224* #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code225* instead).226*227* This error indicates an attack against the application. Implementations228* shall not return this error code as a consequence of the behavior of229* the application itself. */230#define PSA_ERROR_CORRUPTION_DETECTED ((psa_status_t)-151)231232/** There is not enough entropy to generate random data needed233* for the requested action.234*235* This error indicates a failure of a hardware random generator.236* Application writers should note that this error can be returned not237* only by functions whose purpose is to generate random data, such238* as key, IV or nonce generation, but also by functions that execute239* an algorithm with a randomized result, as well as functions that240* use randomization of intermediate computations as a countermeasure241* to certain attacks.242*243* Implementations should avoid returning this error after psa_crypto_init()244* has succeeded. Implementations should generate sufficient245* entropy during initialization and subsequently use a cryptographically246* secure pseudorandom generator (PRNG). However implementations may return247* this error at any time if a policy requires the PRNG to be reseeded248* during normal operation. */249#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)250251/** The signature, MAC or hash is incorrect.252*253* Verification functions return this error if the verification254* calculations completed successfully, and the value to be verified255* was determined to be incorrect.256*257* If the value to verify has an invalid size, implementations may return258* either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */259#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)260261/** The decrypted padding is incorrect.262*263* \warning In some protocols, when decrypting data, it is essential that264* the behavior of the application does not depend on whether the padding265* is correct, down to precise timing. Applications should prefer266* protocols that use authenticated encryption rather than plain267* encryption. If the application must perform a decryption of268* unauthenticated data, the application writer should take care not269* to reveal whether the padding is invalid.270*271* Implementations should strive to make valid and invalid padding272* as close as possible to indistinguishable to an external observer.273* In particular, the timing of a decryption operation should not274* depend on the validity of the padding. */275#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)276277/** Return this error when there's insufficient data when attempting278* to read from a resource. */279#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)280281/** This can be returned if a function can no longer operate correctly.282* For example, if an essential initialization operation failed or283* a mutex operation failed. */284#define PSA_ERROR_SERVICE_FAILURE ((psa_status_t)-144)285286/** The key identifier is not valid. See also :ref:\`key-handles\`.287*/288#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)289290/** Stored data has been corrupted.291*292* This error indicates that some persistent storage has suffered corruption.293* It does not indicate the following situations, which have specific error294* codes:295*296* - A corruption of volatile memory - use #PSA_ERROR_CORRUPTION_DETECTED.297* - A communication error between the cryptoprocessor and its external298* storage - use #PSA_ERROR_COMMUNICATION_FAILURE.299* - When the storage is in a valid state but is full - use300* #PSA_ERROR_INSUFFICIENT_STORAGE.301* - When the storage fails for other reasons - use302* #PSA_ERROR_STORAGE_FAILURE.303* - When the stored data is not valid - use #PSA_ERROR_DATA_INVALID.304*305* \note A storage corruption does not indicate that any data that was306* previously read is invalid. However this previously read data might no307* longer be readable from storage.308*309* When a storage failure occurs, it is no longer possible to ensure the310* global integrity of the keystore.311*/312#define PSA_ERROR_DATA_CORRUPT ((psa_status_t)-152)313314/** Data read from storage is not valid for the implementation.315*316* This error indicates that some data read from storage does not have a valid317* format. It does not indicate the following situations, which have specific318* error codes:319*320* - When the storage or stored data is corrupted - use #PSA_ERROR_DATA_CORRUPT321* - When the storage fails for other reasons - use #PSA_ERROR_STORAGE_FAILURE322* - An invalid argument to the API - use #PSA_ERROR_INVALID_ARGUMENT323*324* This error is typically a result of either storage corruption on a325* cleartext storage backend, or an attempt to read data that was326* written by an incompatible version of the library.327*/328#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)329330/** The function that returns this status is defined as interruptible and331* still has work to do, thus the user should call the function again with the332* same operation context until it either returns #PSA_SUCCESS or any other333* error. This is not an error per se, more a notification of status.334*/335#define PSA_OPERATION_INCOMPLETE ((psa_status_t)-248)336337/* *INDENT-ON* */338339/**@}*/340341/** \defgroup crypto_types Key and algorithm types342* @{343*/344345/* Note that key type values, including ECC family and DH group values, are346* embedded in the persistent key store, as part of key metadata. As a347* consequence, they must not be changed (unless the storage format version348* changes).349*/350351/** An invalid key type value.352*353* Zero is not the encoding of any key type.354*/355#define PSA_KEY_TYPE_NONE ((psa_key_type_t) 0x0000)356357/** Vendor-defined key type flag.358*359* Key types defined by this standard will never have the360* #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types361* must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should362* respect the bitwise structure used by standard encodings whenever practical.363*/364#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t) 0x8000)365366#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t) 0x7000)367#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t) 0x1000)368#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t) 0x2000)369#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t) 0x4000)370#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t) 0x7000)371372#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t) 0x3000)373374/** Whether a key type is vendor-defined.375*376* See also #PSA_KEY_TYPE_VENDOR_FLAG.377*/378#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \379(((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)380381/** Whether a key type is an unstructured array of bytes.382*383* This encompasses both symmetric keys and non-key data.384*/385#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \386(((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_RAW || \387((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)388389/** Whether a key type is asymmetric: either a key pair or a public key. */390#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \391(((type) & PSA_KEY_TYPE_CATEGORY_MASK \392& ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \393PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)394/** Whether a key type is the public part of a key pair. */395#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \396(((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)397/** Whether a key type is a key pair containing a private part and a public398* part. */399#define PSA_KEY_TYPE_IS_KEY_PAIR(type) \400(((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)401/** The key pair type corresponding to a public key type.402*403* You may also pass a key pair type as \p type, it will be left unchanged.404*405* \param type A public key type or key pair type.406*407* \return The corresponding key pair type.408* If \p type is not a public key or a key pair,409* the return value is undefined.410*/411#define PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type) \412((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)413/** The public key type corresponding to a key pair type.414*415* You may also pass a public key type as \p type, it will be left unchanged.416*417* \param type A public key type or key pair type.418*419* \return The corresponding public key type.420* If \p type is not a public key or a key pair,421* the return value is undefined.422*/423#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) \424((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)425426/** Raw data.427*428* A "key" of this type cannot be used for any cryptographic operation.429* Applications may use this type to store arbitrary data in the keystore. */430#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t) 0x1001)431432/** HMAC key.433*434* The key policy determines which underlying hash algorithm the key can be435* used for.436*437* HMAC keys should generally have the same size as the underlying hash.438* This size can be calculated with #PSA_HASH_LENGTH(\c alg) where439* \c alg is the HMAC algorithm or the underlying hash algorithm. */440#define PSA_KEY_TYPE_HMAC ((psa_key_type_t) 0x1100)441442/** A secret for key derivation.443*444* This key type is for high-entropy secrets only. For low-entropy secrets,445* #PSA_KEY_TYPE_PASSWORD should be used instead.446*447* These keys can be used as the #PSA_KEY_DERIVATION_INPUT_SECRET or448* #PSA_KEY_DERIVATION_INPUT_PASSWORD input of key derivation algorithms.449*450* The key policy determines which key derivation algorithm the key451* can be used for.452*/453#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t) 0x1200)454455/** A low-entropy secret for password hashing or key derivation.456*457* This key type is suitable for passwords and passphrases which are typically458* intended to be memorizable by humans, and have a low entropy relative to459* their size. It can be used for randomly generated or derived keys with460* maximum or near-maximum entropy, but #PSA_KEY_TYPE_DERIVE is more suitable461* for such keys. It is not suitable for passwords with extremely low entropy,462* such as numerical PINs.463*464* These keys can be used as the #PSA_KEY_DERIVATION_INPUT_PASSWORD input of465* key derivation algorithms. Algorithms that accept such an input were466* designed to accept low-entropy secret and are known as password hashing or467* key stretching algorithms.468*469* These keys cannot be used as the #PSA_KEY_DERIVATION_INPUT_SECRET input of470* key derivation algorithms, as the algorithms that take such an input expect471* it to be high-entropy.472*473* The key policy determines which key derivation algorithm the key can be474* used for, among the permissible subset defined above.475*/476#define PSA_KEY_TYPE_PASSWORD ((psa_key_type_t) 0x1203)477478/** A secret value that can be used to verify a password hash.479*480* The key policy determines which key derivation algorithm the key481* can be used for, among the same permissible subset as for482* #PSA_KEY_TYPE_PASSWORD.483*/484#define PSA_KEY_TYPE_PASSWORD_HASH ((psa_key_type_t) 0x1205)485486/** A secret value that can be used in when computing a password hash.487*488* The key policy determines which key derivation algorithm the key489* can be used for, among the subset of algorithms that can use pepper.490*/491#define PSA_KEY_TYPE_PEPPER ((psa_key_type_t) 0x1206)492493/** Key for a cipher, AEAD or MAC algorithm based on the AES block cipher.494*495* The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or496* 32 bytes (AES-256).497*/498#define PSA_KEY_TYPE_AES ((psa_key_type_t) 0x2400)499500/** Key for a cipher, AEAD or MAC algorithm based on the501* ARIA block cipher. */502#define PSA_KEY_TYPE_ARIA ((psa_key_type_t) 0x2406)503504/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).505*506* The size of the key can be 64 bits (single DES), 128 bits (2-key 3DES) or507* 192 bits (3-key 3DES).508*509* Note that single DES and 2-key 3DES are weak and strongly510* deprecated and should only be used to decrypt legacy data. 3-key 3DES511* is weak and deprecated and should only be used in legacy protocols.512*/513#define PSA_KEY_TYPE_DES ((psa_key_type_t) 0x2301)514515/** Key for a cipher, AEAD or MAC algorithm based on the516* Camellia block cipher. */517#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t) 0x2403)518519/** Key for the ChaCha20 stream cipher or the Chacha20-Poly1305 AEAD algorithm.520*521* ChaCha20 and the ChaCha20_Poly1305 construction are defined in RFC 7539.522*523* \note For ChaCha20 and ChaCha20_Poly1305, Mbed TLS only supports524* 12-byte nonces.525*526* \note For ChaCha20, the initial counter value is 0. To encrypt or decrypt527* with the initial counter value 1, you can process and discard a528* 64-byte block before the real data.529*/530#define PSA_KEY_TYPE_CHACHA20 ((psa_key_type_t) 0x2004)531532/** RSA public key.533*534* The size of an RSA key is the bit size of the modulus.535*/536#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t) 0x4001)537/** RSA key pair (private and public key).538*539* The size of an RSA key is the bit size of the modulus.540*/541#define PSA_KEY_TYPE_RSA_KEY_PAIR ((psa_key_type_t) 0x7001)542/** Whether a key type is an RSA key (pair or public-only). */543#define PSA_KEY_TYPE_IS_RSA(type) \544(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)545546#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4100)547#define PSA_KEY_TYPE_ECC_KEY_PAIR_BASE ((psa_key_type_t) 0x7100)548#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t) 0x00ff)549/** Elliptic curve key pair.550*551* The size of an elliptic curve key is the bit size associated with the curve,552* i.e. the bit size of *q* for a curve over a field *F<sub>q</sub>*.553* See the documentation of `PSA_ECC_FAMILY_xxx` curve families for details.554*555* \param curve A value of type ::psa_ecc_family_t that556* identifies the ECC curve to be used.557*/558#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) \559(PSA_KEY_TYPE_ECC_KEY_PAIR_BASE | (curve))560/** Elliptic curve public key.561*562* The size of an elliptic curve public key is the same as the corresponding563* private key (see #PSA_KEY_TYPE_ECC_KEY_PAIR and the documentation of564* `PSA_ECC_FAMILY_xxx` curve families).565*566* \param curve A value of type ::psa_ecc_family_t that567* identifies the ECC curve to be used.568*/569#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \570(PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))571572/** Whether a key type is an elliptic curve key (pair or public-only). */573#define PSA_KEY_TYPE_IS_ECC(type) \574((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \575~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)576/** Whether a key type is an elliptic curve key pair. */577#define PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type) \578(((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \579PSA_KEY_TYPE_ECC_KEY_PAIR_BASE)580/** Whether a key type is an elliptic curve public key. */581#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \582(((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \583PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)584585/** Extract the curve from an elliptic curve key type. */586#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \587((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \588((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \5890))590591/** Check if the curve of given family is Weierstrass elliptic curve. */592#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)593594/** SEC Koblitz curves over prime fields.595*596* This family comprises the following curves:597* secp192k1, secp224k1, secp256k1.598* They are defined in _Standards for Efficient Cryptography_,599* _SEC 2: Recommended Elliptic Curve Domain Parameters_.600* https://www.secg.org/sec2-v2.pdf601*602* \note For secp224k1, the bit-size is 225 (size of a private value).603*604* \note Mbed TLS only supports secp192k1 and secp256k1.605*/606#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)607608/** SEC random curves over prime fields.609*610* This family comprises the following curves:611* secp192r1, secp224r1, secp256r1, secp384r1, secp521r1.612* They are defined in _Standards for Efficient Cryptography_,613* _SEC 2: Recommended Elliptic Curve Domain Parameters_.614* https://www.secg.org/sec2-v2.pdf615*/616#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)617/* SECP160R2 (SEC2 v1, obsolete, not supported in Mbed TLS) */618#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)619620/** SEC Koblitz curves over binary fields.621*622* This family comprises the following curves:623* sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.624* They are defined in _Standards for Efficient Cryptography_,625* _SEC 2: Recommended Elliptic Curve Domain Parameters_.626* https://www.secg.org/sec2-v2.pdf627*628* \note Mbed TLS does not support any curve in this family.629*/630#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)631632/** SEC random curves over binary fields.633*634* This family comprises the following curves:635* sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.636* They are defined in _Standards for Efficient Cryptography_,637* _SEC 2: Recommended Elliptic Curve Domain Parameters_.638* https://www.secg.org/sec2-v2.pdf639*640* \note Mbed TLS does not support any curve in this family.641*/642#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)643644/** SEC additional random curves over binary fields.645*646* This family comprises the following curve:647* sect163r2.648* It is defined in _Standards for Efficient Cryptography_,649* _SEC 2: Recommended Elliptic Curve Domain Parameters_.650* https://www.secg.org/sec2-v2.pdf651*652* \note Mbed TLS does not support any curve in this family.653*/654#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)655656/** Brainpool P random curves.657*658* This family comprises the following curves:659* brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,660* brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.661* It is defined in RFC 5639.662*663* \note Mbed TLS only supports the 256-bit, 384-bit and 512-bit curves664* in this family.665*/666#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)667668/** Curve25519 and Curve448.669*670* This family comprises the following Montgomery curves:671* - 255-bit: Bernstein et al.,672* _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.673* The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.674* - 448-bit: Hamburg,675* _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.676* The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.677*/678#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)679680/** The twisted Edwards curves Ed25519 and Ed448.681*682* These curves are suitable for EdDSA (#PSA_ALG_PURE_EDDSA for both curves,683* #PSA_ALG_ED25519PH for the 255-bit curve,684* #PSA_ALG_ED448PH for the 448-bit curve).685*686* This family comprises the following twisted Edwards curves:687* - 255-bit: Edwards25519, the twisted Edwards curve birationally equivalent688* to Curve25519.689* Bernstein et al., _Twisted Edwards curves_, Africacrypt 2008.690* - 448-bit: Edwards448, the twisted Edwards curve birationally equivalent691* to Curve448.692* Hamburg, _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.693*694* \note Mbed TLS does not support Edwards curves yet.695*/696#define PSA_ECC_FAMILY_TWISTED_EDWARDS ((psa_ecc_family_t) 0x42)697698#define PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE ((psa_key_type_t) 0x4200)699#define PSA_KEY_TYPE_DH_KEY_PAIR_BASE ((psa_key_type_t) 0x7200)700#define PSA_KEY_TYPE_DH_GROUP_MASK ((psa_key_type_t) 0x00ff)701/** Diffie-Hellman key pair.702*703* \param group A value of type ::psa_dh_family_t that identifies the704* Diffie-Hellman group to be used.705*/706#define PSA_KEY_TYPE_DH_KEY_PAIR(group) \707(PSA_KEY_TYPE_DH_KEY_PAIR_BASE | (group))708/** Diffie-Hellman public key.709*710* \param group A value of type ::psa_dh_family_t that identifies the711* Diffie-Hellman group to be used.712*/713#define PSA_KEY_TYPE_DH_PUBLIC_KEY(group) \714(PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE | (group))715716/** Whether a key type is a Diffie-Hellman key (pair or public-only). */717#define PSA_KEY_TYPE_IS_DH(type) \718((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) & \719~PSA_KEY_TYPE_DH_GROUP_MASK) == PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)720/** Whether a key type is a Diffie-Hellman key pair. */721#define PSA_KEY_TYPE_IS_DH_KEY_PAIR(type) \722(((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \723PSA_KEY_TYPE_DH_KEY_PAIR_BASE)724/** Whether a key type is a Diffie-Hellman public key. */725#define PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) \726(((type) & ~PSA_KEY_TYPE_DH_GROUP_MASK) == \727PSA_KEY_TYPE_DH_PUBLIC_KEY_BASE)728729/** Extract the group from a Diffie-Hellman key type. */730#define PSA_KEY_TYPE_DH_GET_FAMILY(type) \731((psa_dh_family_t) (PSA_KEY_TYPE_IS_DH(type) ? \732((type) & PSA_KEY_TYPE_DH_GROUP_MASK) : \7330))734735/** Diffie-Hellman groups defined in RFC 7919 Appendix A.736*737* This family includes groups with the following key sizes (in bits):738* 2048, 3072, 4096, 6144, 8192. A given implementation may support739* all of these sizes or only a subset.740*/741#define PSA_DH_FAMILY_RFC7919 ((psa_dh_family_t) 0x03)742743#define PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) \744(((type) >> 8) & 7)745/** The block size of a block cipher.746*747* \param type A cipher key type (value of type #psa_key_type_t).748*749* \return The block size for a block cipher, or 1 for a stream cipher.750* The return value is undefined if \p type is not a supported751* cipher key type.752*753* \note It is possible to build stream cipher algorithms on top of a block754* cipher, for example CTR mode (#PSA_ALG_CTR).755* This macro only takes the key type into account, so it cannot be756* used to determine the size of the data that #psa_cipher_update()757* might buffer for future processing in general.758*759* \note This macro returns a compile-time constant if its argument is one.760*761* \warning This macro may evaluate its argument multiple times.762*/763#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \764(((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \7651u << PSA_GET_KEY_TYPE_BLOCK_SIZE_EXPONENT(type) : \7660u)767768/* Note that algorithm values are embedded in the persistent key store,769* as part of key metadata. As a consequence, they must not be changed770* (unless the storage format version changes).771*/772773/** Vendor-defined algorithm flag.774*775* Algorithms defined by this standard will never have the #PSA_ALG_VENDOR_FLAG776* bit set. Vendors who define additional algorithms must use an encoding with777* the #PSA_ALG_VENDOR_FLAG bit set and should respect the bitwise structure778* used by standard encodings whenever practical.779*/780#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t) 0x80000000)781782#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t) 0x7f000000)783#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t) 0x02000000)784#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t) 0x03000000)785#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t) 0x04000000)786#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t) 0x05000000)787#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t) 0x06000000)788#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t) 0x07000000)789#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t) 0x08000000)790#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t) 0x09000000)791792/** Whether an algorithm is vendor-defined.793*794* See also #PSA_ALG_VENDOR_FLAG.795*/796#define PSA_ALG_IS_VENDOR_DEFINED(alg) \797(((alg) & PSA_ALG_VENDOR_FLAG) != 0)798799/** Whether the specified algorithm is a hash algorithm.800*801* \param alg An algorithm identifier (value of type #psa_algorithm_t).802*803* \return 1 if \p alg is a hash algorithm, 0 otherwise.804* This macro may return either 0 or 1 if \p alg is not a supported805* algorithm identifier.806*/807#define PSA_ALG_IS_HASH(alg) \808(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)809810/** Whether the specified algorithm is a MAC algorithm.811*812* \param alg An algorithm identifier (value of type #psa_algorithm_t).813*814* \return 1 if \p alg is a MAC algorithm, 0 otherwise.815* This macro may return either 0 or 1 if \p alg is not a supported816* algorithm identifier.817*/818#define PSA_ALG_IS_MAC(alg) \819(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)820821/** Whether the specified algorithm is a symmetric cipher algorithm.822*823* \param alg An algorithm identifier (value of type #psa_algorithm_t).824*825* \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.826* This macro may return either 0 or 1 if \p alg is not a supported827* algorithm identifier.828*/829#define PSA_ALG_IS_CIPHER(alg) \830(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)831832/** Whether the specified algorithm is an authenticated encryption833* with associated data (AEAD) algorithm.834*835* \param alg An algorithm identifier (value of type #psa_algorithm_t).836*837* \return 1 if \p alg is an AEAD algorithm, 0 otherwise.838* This macro may return either 0 or 1 if \p alg is not a supported839* algorithm identifier.840*/841#define PSA_ALG_IS_AEAD(alg) \842(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)843844/** Whether the specified algorithm is an asymmetric signature algorithm,845* also known as public-key signature algorithm.846*847* \param alg An algorithm identifier (value of type #psa_algorithm_t).848*849* \return 1 if \p alg is an asymmetric signature algorithm, 0 otherwise.850* This macro may return either 0 or 1 if \p alg is not a supported851* algorithm identifier.852*/853#define PSA_ALG_IS_SIGN(alg) \854(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)855856/** Whether the specified algorithm is an asymmetric encryption algorithm,857* also known as public-key encryption algorithm.858*859* \param alg An algorithm identifier (value of type #psa_algorithm_t).860*861* \return 1 if \p alg is an asymmetric encryption algorithm, 0 otherwise.862* This macro may return either 0 or 1 if \p alg is not a supported863* algorithm identifier.864*/865#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \866(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)867868/** Whether the specified algorithm is a key agreement algorithm.869*870* \param alg An algorithm identifier (value of type #psa_algorithm_t).871*872* \return 1 if \p alg is a key agreement algorithm, 0 otherwise.873* This macro may return either 0 or 1 if \p alg is not a supported874* algorithm identifier.875*/876#define PSA_ALG_IS_KEY_AGREEMENT(alg) \877(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)878879/** Whether the specified algorithm is a key derivation algorithm.880*881* \param alg An algorithm identifier (value of type #psa_algorithm_t).882*883* \return 1 if \p alg is a key derivation algorithm, 0 otherwise.884* This macro may return either 0 or 1 if \p alg is not a supported885* algorithm identifier.886*/887#define PSA_ALG_IS_KEY_DERIVATION(alg) \888(((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)889890/** Whether the specified algorithm is a key stretching / password hashing891* algorithm.892*893* A key stretching / password hashing algorithm is a key derivation algorithm894* that is suitable for use with a low-entropy secret such as a password.895* Equivalently, it's a key derivation algorithm that uses a896* #PSA_KEY_DERIVATION_INPUT_PASSWORD input step.897*898* \param alg An algorithm identifier (value of type #psa_algorithm_t).899*900* \return 1 if \p alg is a key stretching / password hashing algorithm, 0901* otherwise. This macro may return either 0 or 1 if \p alg is not a902* supported algorithm identifier.903*/904#define PSA_ALG_IS_KEY_DERIVATION_STRETCHING(alg) \905(PSA_ALG_IS_KEY_DERIVATION(alg) && \906(alg) & PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG)907908/** An invalid algorithm identifier value. */909/* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */910#define PSA_ALG_NONE ((psa_algorithm_t)0)911/* *INDENT-ON* */912913#define PSA_ALG_HASH_MASK ((psa_algorithm_t) 0x000000ff)914/** MD5 */915#define PSA_ALG_MD5 ((psa_algorithm_t) 0x02000003)916/** PSA_ALG_RIPEMD160 */917#define PSA_ALG_RIPEMD160 ((psa_algorithm_t) 0x02000004)918/** SHA1 */919#define PSA_ALG_SHA_1 ((psa_algorithm_t) 0x02000005)920/** SHA2-224 */921#define PSA_ALG_SHA_224 ((psa_algorithm_t) 0x02000008)922/** SHA2-256 */923#define PSA_ALG_SHA_256 ((psa_algorithm_t) 0x02000009)924/** SHA2-384 */925#define PSA_ALG_SHA_384 ((psa_algorithm_t) 0x0200000a)926/** SHA2-512 */927#define PSA_ALG_SHA_512 ((psa_algorithm_t) 0x0200000b)928/** SHA2-512/224 */929#define PSA_ALG_SHA_512_224 ((psa_algorithm_t) 0x0200000c)930/** SHA2-512/256 */931#define PSA_ALG_SHA_512_256 ((psa_algorithm_t) 0x0200000d)932/** SHA3-224 */933#define PSA_ALG_SHA3_224 ((psa_algorithm_t) 0x02000010)934/** SHA3-256 */935#define PSA_ALG_SHA3_256 ((psa_algorithm_t) 0x02000011)936/** SHA3-384 */937#define PSA_ALG_SHA3_384 ((psa_algorithm_t) 0x02000012)938/** SHA3-512 */939#define PSA_ALG_SHA3_512 ((psa_algorithm_t) 0x02000013)940/** The first 512 bits (64 bytes) of the SHAKE256 output.941*942* This is the prehashing for Ed448ph (see #PSA_ALG_ED448PH). For other943* scenarios where a hash function based on SHA3/SHAKE is desired, SHA3-512944* has the same output size and a (theoretically) higher security strength.945*/946#define PSA_ALG_SHAKE256_512 ((psa_algorithm_t) 0x02000015)947948/** In a hash-and-sign algorithm policy, allow any hash algorithm.949*950* This value may be used to form the algorithm usage field of a policy951* for a signature algorithm that is parametrized by a hash. The key952* may then be used to perform operations using the same signature953* algorithm parametrized with any supported hash.954*955* That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:956* - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS, #PSA_ALG_RSA_PSS_ANY_SALT,957* - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.958* Then you may create and use a key as follows:959* - Set the key usage field using #PSA_ALG_ANY_HASH, for example:960* ```961* psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); // or VERIFY962* psa_set_key_algorithm(&attributes, PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));963* ```964* - Import or generate key material.965* - Call psa_sign_hash() or psa_verify_hash(), passing966* an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each967* call to sign or verify a message may use a different hash.968* ```969* psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);970* psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);971* psa_sign_hash(key, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);972* ```973*974* This value may not be used to build other algorithms that are975* parametrized over a hash. For any valid use of this macro to build976* an algorithm \c alg, #PSA_ALG_IS_HASH_AND_SIGN(\c alg) is true.977*978* This value may not be used to build an algorithm specification to979* perform an operation. It is only valid to build policies.980*/981#define PSA_ALG_ANY_HASH ((psa_algorithm_t) 0x020000ff)982983#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t) 0x00c00000)984#define PSA_ALG_HMAC_BASE ((psa_algorithm_t) 0x03800000)985/** Macro to build an HMAC algorithm.986*987* For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.988*989* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that990* #PSA_ALG_IS_HASH(\p hash_alg) is true).991*992* \return The corresponding HMAC algorithm.993* \return Unspecified if \p hash_alg is not a supported994* hash algorithm.995*/996#define PSA_ALG_HMAC(hash_alg) \997(PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))998999#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \1000(PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))10011002/** Whether the specified algorithm is an HMAC algorithm.1003*1004* HMAC is a family of MAC algorithms that are based on a hash function.1005*1006* \param alg An algorithm identifier (value of type #psa_algorithm_t).1007*1008* \return 1 if \p alg is an HMAC algorithm, 0 otherwise.1009* This macro may return either 0 or 1 if \p alg is not a supported1010* algorithm identifier.1011*/1012#define PSA_ALG_IS_HMAC(alg) \1013(((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \1014PSA_ALG_HMAC_BASE)10151016/* In the encoding of a MAC algorithm, the bits corresponding to1017* PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is1018* truncated. As an exception, the value 0 means the untruncated algorithm,1019* whatever its length is. The length is encoded in 6 bits, so it can1020* reach up to 63; the largest MAC is 64 bytes so its trivial truncation1021* to full length is correctly encoded as 0 and any non-trivial truncation1022* is correctly encoded as a value between 1 and 63. */1023#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t) 0x003f0000)1024#define PSA_MAC_TRUNCATION_OFFSET 1610251026/* In the encoding of a MAC algorithm, the bit corresponding to1027* #PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm1028* is a wildcard algorithm. A key with such wildcard algorithm as permitted1029* algorithm policy can be used with any algorithm corresponding to the1030* same base class and having a (potentially truncated) MAC length greater or1031* equal than the one encoded in #PSA_ALG_MAC_TRUNCATION_MASK. */1032#define PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)10331034/** Macro to build a truncated MAC algorithm.1035*1036* A truncated MAC algorithm is identical to the corresponding MAC1037* algorithm except that the MAC value for the truncated algorithm1038* consists of only the first \p mac_length bytes of the MAC value1039* for the untruncated algorithm.1040*1041* \note This macro may allow constructing algorithm identifiers that1042* are not valid, either because the specified length is larger1043* than the untruncated MAC or because the specified length is1044* smaller than permitted by the implementation.1045*1046* \note It is implementation-defined whether a truncated MAC that1047* is truncated to the same length as the MAC of the untruncated1048* algorithm is considered identical to the untruncated algorithm1049* for policy comparison purposes.1050*1051* \param mac_alg A MAC algorithm identifier (value of type1052* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)1053* is true). This may be a truncated or untruncated1054* MAC algorithm.1055* \param mac_length Desired length of the truncated MAC in bytes.1056* This must be at most the full length of the MAC1057* and must be at least an implementation-specified1058* minimum. The implementation-specified minimum1059* shall not be zero.1060*1061* \return The corresponding MAC algorithm with the specified1062* length.1063* \return Unspecified if \p mac_alg is not a supported1064* MAC algorithm or if \p mac_length is too small or1065* too large for the specified MAC algorithm.1066*/1067#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \1068(((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \1069PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)) | \1070((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))10711072/** Macro to build the base MAC algorithm corresponding to a truncated1073* MAC algorithm.1074*1075* \param mac_alg A MAC algorithm identifier (value of type1076* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)1077* is true). This may be a truncated or untruncated1078* MAC algorithm.1079*1080* \return The corresponding base MAC algorithm.1081* \return Unspecified if \p mac_alg is not a supported1082* MAC algorithm.1083*/1084#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \1085((mac_alg) & ~(PSA_ALG_MAC_TRUNCATION_MASK | \1086PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG))10871088/** Length to which a MAC algorithm is truncated.1089*1090* \param mac_alg A MAC algorithm identifier (value of type1091* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)1092* is true).1093*1094* \return Length of the truncated MAC in bytes.1095* \return 0 if \p mac_alg is a non-truncated MAC algorithm.1096* \return Unspecified if \p mac_alg is not a supported1097* MAC algorithm.1098*/1099#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \1100(((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)11011102/** Macro to build a MAC minimum-MAC-length wildcard algorithm.1103*1104* A minimum-MAC-length MAC wildcard algorithm permits all MAC algorithms1105* sharing the same base algorithm, and where the (potentially truncated) MAC1106* length of the specific algorithm is equal to or larger then the wildcard1107* algorithm's minimum MAC length.1108*1109* \note When setting the minimum required MAC length to less than the1110* smallest MAC length allowed by the base algorithm, this effectively1111* becomes an 'any-MAC-length-allowed' policy for that base algorithm.1112*1113* \param mac_alg A MAC algorithm identifier (value of type1114* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p mac_alg)1115* is true).1116* \param min_mac_length Desired minimum length of the message authentication1117* code in bytes. This must be at most the untruncated1118* length of the MAC and must be at least 1.1119*1120* \return The corresponding MAC wildcard algorithm with the1121* specified minimum length.1122* \return Unspecified if \p mac_alg is not a supported MAC1123* algorithm or if \p min_mac_length is less than 1 or1124* too large for the specified MAC algorithm.1125*/1126#define PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(mac_alg, min_mac_length) \1127(PSA_ALG_TRUNCATED_MAC(mac_alg, min_mac_length) | \1128PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG)11291130#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t) 0x03c00000)1131/** The CBC-MAC construction over a block cipher1132*1133* \warning CBC-MAC is insecure in many cases.1134* A more secure mode, such as #PSA_ALG_CMAC, is recommended.1135*/1136#define PSA_ALG_CBC_MAC ((psa_algorithm_t) 0x03c00100)1137/** The CMAC construction over a block cipher */1138#define PSA_ALG_CMAC ((psa_algorithm_t) 0x03c00200)11391140/** Whether the specified algorithm is a MAC algorithm based on a block cipher.1141*1142* \param alg An algorithm identifier (value of type #psa_algorithm_t).1143*1144* \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.1145* This macro may return either 0 or 1 if \p alg is not a supported1146* algorithm identifier.1147*/1148#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \1149(((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \1150PSA_ALG_CIPHER_MAC_BASE)11511152#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t) 0x00800000)1153#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)11541155/** Whether the specified algorithm is a stream cipher.1156*1157* A stream cipher is a symmetric cipher that encrypts or decrypts messages1158* by applying a bitwise-xor with a stream of bytes that is generated1159* from a key.1160*1161* \param alg An algorithm identifier (value of type #psa_algorithm_t).1162*1163* \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.1164* This macro may return either 0 or 1 if \p alg is not a supported1165* algorithm identifier or if it is not a symmetric cipher algorithm.1166*/1167#define PSA_ALG_IS_STREAM_CIPHER(alg) \1168(((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \1169(PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))11701171/** The stream cipher mode of a stream cipher algorithm.1172*1173* The underlying stream cipher is determined by the key type.1174* - To use ChaCha20, use a key type of #PSA_KEY_TYPE_CHACHA20.1175*/1176#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t) 0x04800100)11771178/** The CTR stream cipher mode.1179*1180* CTR is a stream cipher which is built from a block cipher.1181* The underlying block cipher is determined by the key type.1182* For example, to use AES-128-CTR, use this algorithm with1183* a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).1184*/1185#define PSA_ALG_CTR ((psa_algorithm_t) 0x04c01000)11861187/** The CFB stream cipher mode.1188*1189* The underlying block cipher is determined by the key type.1190*/1191#define PSA_ALG_CFB ((psa_algorithm_t) 0x04c01100)11921193/** The OFB stream cipher mode.1194*1195* The underlying block cipher is determined by the key type.1196*/1197#define PSA_ALG_OFB ((psa_algorithm_t) 0x04c01200)11981199/** The XTS cipher mode.1200*1201* XTS is a cipher mode which is built from a block cipher. It requires at1202* least one full block of input, but beyond this minimum the input1203* does not need to be a whole number of blocks.1204*/1205#define PSA_ALG_XTS ((psa_algorithm_t) 0x0440ff00)12061207/** The Electronic Code Book (ECB) mode of a block cipher, with no padding.1208*1209* \warning ECB mode does not protect the confidentiality of the encrypted data1210* except in extremely narrow circumstances. It is recommended that applications1211* only use ECB if they need to construct an operating mode that the1212* implementation does not provide. Implementations are encouraged to provide1213* the modes that applications need in preference to supporting direct access1214* to ECB.1215*1216* The underlying block cipher is determined by the key type.1217*1218* This symmetric cipher mode can only be used with messages whose lengths are a1219* multiple of the block size of the chosen block cipher.1220*1221* ECB mode does not accept an initialization vector (IV). When using a1222* multi-part cipher operation with this algorithm, psa_cipher_generate_iv()1223* and psa_cipher_set_iv() must not be called.1224*/1225#define PSA_ALG_ECB_NO_PADDING ((psa_algorithm_t) 0x04404400)12261227/** The CBC block cipher chaining mode, with no padding.1228*1229* The underlying block cipher is determined by the key type.1230*1231* This symmetric cipher mode can only be used with messages whose lengths1232* are whole number of blocks for the chosen block cipher.1233*/1234#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t) 0x04404000)12351236/** The CBC block cipher chaining mode with PKCS#7 padding.1237*1238* The underlying block cipher is determined by the key type.1239*1240* This is the padding method defined by PKCS#7 (RFC 2315) §10.3.1241*/1242#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t) 0x04404100)12431244#define PSA_ALG_AEAD_FROM_BLOCK_FLAG ((psa_algorithm_t) 0x00400000)12451246/** Whether the specified algorithm is an AEAD mode on a block cipher.1247*1248* \param alg An algorithm identifier (value of type #psa_algorithm_t).1249*1250* \return 1 if \p alg is an AEAD algorithm which is an AEAD mode based on1251* a block cipher, 0 otherwise.1252* This macro may return either 0 or 1 if \p alg is not a supported1253* algorithm identifier.1254*/1255#define PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) \1256(((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_AEAD_FROM_BLOCK_FLAG)) == \1257(PSA_ALG_CATEGORY_AEAD | PSA_ALG_AEAD_FROM_BLOCK_FLAG))12581259/** The CCM authenticated encryption algorithm.1260*1261* The underlying block cipher is determined by the key type.1262*/1263#define PSA_ALG_CCM ((psa_algorithm_t) 0x05500100)12641265/** The CCM* cipher mode without authentication.1266*1267* This is CCM* as specified in IEEE 802.15.4 §7, with a tag length of 0.1268* For CCM* with a nonzero tag length, use the AEAD algorithm #PSA_ALG_CCM.1269*1270* The underlying block cipher is determined by the key type.1271*1272* Currently only 13-byte long IV's are supported.1273*/1274#define PSA_ALG_CCM_STAR_NO_TAG ((psa_algorithm_t) 0x04c01300)12751276/** The GCM authenticated encryption algorithm.1277*1278* The underlying block cipher is determined by the key type.1279*/1280#define PSA_ALG_GCM ((psa_algorithm_t) 0x05500200)12811282/** The Chacha20-Poly1305 AEAD algorithm.1283*1284* The ChaCha20_Poly1305 construction is defined in RFC 7539.1285*1286* Implementations must support 12-byte nonces, may support 8-byte nonces,1287* and should reject other sizes.1288*1289* Implementations must support 16-byte tags and should reject other sizes.1290*/1291#define PSA_ALG_CHACHA20_POLY1305 ((psa_algorithm_t) 0x05100500)12921293/* In the encoding of an AEAD algorithm, the bits corresponding to1294* PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.1295* The constants for default lengths follow this encoding.1296*/1297#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t) 0x003f0000)1298#define PSA_AEAD_TAG_LENGTH_OFFSET 1612991300/* In the encoding of an AEAD algorithm, the bit corresponding to1301* #PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG encodes the fact that the algorithm1302* is a wildcard algorithm. A key with such wildcard algorithm as permitted1303* algorithm policy can be used with any algorithm corresponding to the1304* same base class and having a tag length greater than or equal to the one1305* encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */1306#define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t) 0x00008000)13071308/** Macro to build a shortened AEAD algorithm.1309*1310* A shortened AEAD algorithm is similar to the corresponding AEAD1311* algorithm, but has an authentication tag that consists of fewer bytes.1312* Depending on the algorithm, the tag length may affect the calculation1313* of the ciphertext.1314*1315* \param aead_alg An AEAD algorithm identifier (value of type1316* #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)1317* is true).1318* \param tag_length Desired length of the authentication tag in bytes.1319*1320* \return The corresponding AEAD algorithm with the specified1321* length.1322* \return Unspecified if \p aead_alg is not a supported1323* AEAD algorithm or if \p tag_length is not valid1324* for the specified AEAD algorithm.1325*/1326#define PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, tag_length) \1327(((aead_alg) & ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | \1328PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)) | \1329((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \1330PSA_ALG_AEAD_TAG_LENGTH_MASK))13311332/** Retrieve the tag length of a specified AEAD algorithm1333*1334* \param aead_alg An AEAD algorithm identifier (value of type1335* #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p aead_alg)1336* is true).1337*1338* \return The tag length specified by the input algorithm.1339* \return Unspecified if \p aead_alg is not a supported1340* AEAD algorithm.1341*/1342#define PSA_ALG_AEAD_GET_TAG_LENGTH(aead_alg) \1343(((aead_alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> \1344PSA_AEAD_TAG_LENGTH_OFFSET)13451346/** Calculate the corresponding AEAD algorithm with the default tag length.1347*1348* \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that1349* #PSA_ALG_IS_AEAD(\p aead_alg) is true).1350*1351* \return The corresponding AEAD algorithm with the default1352* tag length for that algorithm.1353*/1354#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(aead_alg) \1355( \1356PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CCM) \1357PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_GCM) \1358PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, PSA_ALG_CHACHA20_POLY1305) \13590)1360#define PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE(aead_alg, ref) \1361PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, 0) == \1362PSA_ALG_AEAD_WITH_SHORTENED_TAG(ref, 0) ? \1363ref :13641365/** Macro to build an AEAD minimum-tag-length wildcard algorithm.1366*1367* A minimum-tag-length AEAD wildcard algorithm permits all AEAD algorithms1368* sharing the same base algorithm, and where the tag length of the specific1369* algorithm is equal to or larger then the minimum tag length specified by the1370* wildcard algorithm.1371*1372* \note When setting the minimum required tag length to less than the1373* smallest tag length allowed by the base algorithm, this effectively1374* becomes an 'any-tag-length-allowed' policy for that base algorithm.1375*1376* \param aead_alg An AEAD algorithm identifier (value of type1377* #psa_algorithm_t such that1378* #PSA_ALG_IS_AEAD(\p aead_alg) is true).1379* \param min_tag_length Desired minimum length of the authentication tag in1380* bytes. This must be at least 1 and at most the largest1381* allowed tag length of the algorithm.1382*1383* \return The corresponding AEAD wildcard algorithm with the1384* specified minimum length.1385* \return Unspecified if \p aead_alg is not a supported1386* AEAD algorithm or if \p min_tag_length is less than 11387* or too large for the specified AEAD algorithm.1388*/1389#define PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(aead_alg, min_tag_length) \1390(PSA_ALG_AEAD_WITH_SHORTENED_TAG(aead_alg, min_tag_length) | \1391PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)13921393#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t) 0x06000200)1394/** RSA PKCS#1 v1.5 signature with hashing.1395*1396* This is the signature scheme defined by RFC 80171397* (PKCS#1: RSA Cryptography Specifications) under the name1398* RSASSA-PKCS1-v1_5.1399*1400* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1401* #PSA_ALG_IS_HASH(\p hash_alg) is true).1402* This includes #PSA_ALG_ANY_HASH1403* when specifying the algorithm in a usage policy.1404*1405* \return The corresponding RSA PKCS#1 v1.5 signature algorithm.1406* \return Unspecified if \p hash_alg is not a supported1407* hash algorithm.1408*/1409#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \1410(PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1411/** Raw PKCS#1 v1.5 signature.1412*1413* The input to this algorithm is the DigestInfo structure used by1414* RFC 8017 (PKCS#1: RSA Cryptography Specifications), §9.21415* steps 3–6.1416*/1417#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE1418#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \1419(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)14201421#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t) 0x06000300)1422#define PSA_ALG_RSA_PSS_ANY_SALT_BASE ((psa_algorithm_t) 0x06001300)1423/** RSA PSS signature with hashing.1424*1425* This is the signature scheme defined by RFC 80171426* (PKCS#1: RSA Cryptography Specifications) under the name1427* RSASSA-PSS, with the message generation function MGF1, and with1428* a salt length equal to the length of the hash, or the largest1429* possible salt length for the algorithm and key size if that is1430* smaller than the hash length. The specified hash algorithm is1431* used to hash the input message, to create the salted hash, and1432* for the mask generation.1433*1434* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1435* #PSA_ALG_IS_HASH(\p hash_alg) is true).1436* This includes #PSA_ALG_ANY_HASH1437* when specifying the algorithm in a usage policy.1438*1439* \return The corresponding RSA PSS signature algorithm.1440* \return Unspecified if \p hash_alg is not a supported1441* hash algorithm.1442*/1443#define PSA_ALG_RSA_PSS(hash_alg) \1444(PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))14451446/** RSA PSS signature with hashing with relaxed verification.1447*1448* This algorithm has the same behavior as #PSA_ALG_RSA_PSS when signing,1449* but allows an arbitrary salt length (including \c 0) when verifying a1450* signature.1451*1452* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1453* #PSA_ALG_IS_HASH(\p hash_alg) is true).1454* This includes #PSA_ALG_ANY_HASH1455* when specifying the algorithm in a usage policy.1456*1457* \return The corresponding RSA PSS signature algorithm.1458* \return Unspecified if \p hash_alg is not a supported1459* hash algorithm.1460*/1461#define PSA_ALG_RSA_PSS_ANY_SALT(hash_alg) \1462(PSA_ALG_RSA_PSS_ANY_SALT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))14631464/** Whether the specified algorithm is RSA PSS with standard salt.1465*1466* \param alg An algorithm value or an algorithm policy wildcard.1467*1468* \return 1 if \p alg is of the form1469* #PSA_ALG_RSA_PSS(\c hash_alg),1470* where \c hash_alg is a hash algorithm or1471* #PSA_ALG_ANY_HASH. 0 otherwise.1472* This macro may return either 0 or 1 if \p alg is not1473* a supported algorithm identifier or policy.1474*/1475#define PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) \1476(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)14771478/** Whether the specified algorithm is RSA PSS with any salt.1479*1480* \param alg An algorithm value or an algorithm policy wildcard.1481*1482* \return 1 if \p alg is of the form1483* #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),1484* where \c hash_alg is a hash algorithm or1485* #PSA_ALG_ANY_HASH. 0 otherwise.1486* This macro may return either 0 or 1 if \p alg is not1487* a supported algorithm identifier or policy.1488*/1489#define PSA_ALG_IS_RSA_PSS_ANY_SALT(alg) \1490(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_ANY_SALT_BASE)14911492/** Whether the specified algorithm is RSA PSS.1493*1494* This includes any of the RSA PSS algorithm variants, regardless of the1495* constraints on salt length.1496*1497* \param alg An algorithm value or an algorithm policy wildcard.1498*1499* \return 1 if \p alg is of the form1500* #PSA_ALG_RSA_PSS(\c hash_alg) or1501* #PSA_ALG_RSA_PSS_ANY_SALT_BASE(\c hash_alg),1502* where \c hash_alg is a hash algorithm or1503* #PSA_ALG_ANY_HASH. 0 otherwise.1504* This macro may return either 0 or 1 if \p alg is not1505* a supported algorithm identifier or policy.1506*/1507#define PSA_ALG_IS_RSA_PSS(alg) \1508(PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg) || \1509PSA_ALG_IS_RSA_PSS_ANY_SALT(alg))15101511#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t) 0x06000600)1512/** ECDSA signature with hashing.1513*1514* This is the ECDSA signature scheme defined by ANSI X9.62,1515* with a random per-message secret number (*k*).1516*1517* The representation of the signature as a byte string consists of1518* the concatenation of the signature values *r* and *s*. Each of1519* *r* and *s* is encoded as an *N*-octet string, where *N* is the length1520* of the base point of the curve in octets. Each value is represented1521* in big-endian order (most significant octet first).1522*1523* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1524* #PSA_ALG_IS_HASH(\p hash_alg) is true).1525* This includes #PSA_ALG_ANY_HASH1526* when specifying the algorithm in a usage policy.1527*1528* \return The corresponding ECDSA signature algorithm.1529* \return Unspecified if \p hash_alg is not a supported1530* hash algorithm.1531*/1532#define PSA_ALG_ECDSA(hash_alg) \1533(PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1534/** ECDSA signature without hashing.1535*1536* This is the same signature scheme as #PSA_ALG_ECDSA(), but1537* without specifying a hash algorithm. This algorithm may only be1538* used to sign or verify a sequence of bytes that should be an1539* already-calculated hash. Note that the input is padded with1540* zeros on the left or truncated on the left as required to fit1541* the curve size.1542*/1543#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE1544#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t) 0x06000700)1545/** Deterministic ECDSA signature with hashing.1546*1547* This is the deterministic ECDSA signature scheme defined by RFC 6979.1548*1549* The representation of a signature is the same as with #PSA_ALG_ECDSA().1550*1551* Note that when this algorithm is used for verification, signatures1552* made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the1553* same private key are accepted. In other words,1554* #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from1555* #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.1556*1557* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1558* #PSA_ALG_IS_HASH(\p hash_alg) is true).1559* This includes #PSA_ALG_ANY_HASH1560* when specifying the algorithm in a usage policy.1561*1562* \return The corresponding deterministic ECDSA signature1563* algorithm.1564* \return Unspecified if \p hash_alg is not a supported1565* hash algorithm.1566*/1567#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \1568(PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1569#define PSA_ALG_ECDSA_DETERMINISTIC_FLAG ((psa_algorithm_t) 0x00000100)1570#define PSA_ALG_IS_ECDSA(alg) \1571(((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_ECDSA_DETERMINISTIC_FLAG) == \1572PSA_ALG_ECDSA_BASE)1573#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \1574(((alg) & PSA_ALG_ECDSA_DETERMINISTIC_FLAG) != 0)1575#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \1576(PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))1577#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \1578(PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))15791580/** Edwards-curve digital signature algorithm without prehashing (PureEdDSA),1581* using standard parameters.1582*1583* Contexts are not supported in the current version of this specification1584* because there is no suitable signature interface that can take the1585* context as a parameter. A future version of this specification may add1586* suitable functions and extend this algorithm to support contexts.1587*1588* PureEdDSA requires an elliptic curve key on a twisted Edwards curve.1589* In this specification, the following curves are supported:1590* - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 255-bit: Ed25519 as specified1591* in RFC 8032.1592* The curve is Edwards25519.1593* The hash function used internally is SHA-512.1594* - #PSA_ECC_FAMILY_TWISTED_EDWARDS, 448-bit: Ed448 as specified1595* in RFC 8032.1596* The curve is Edwards448.1597* The hash function used internally is the first 114 bytes of the1598* SHAKE256 output.1599*1600* This algorithm can be used with psa_sign_message() and1601* psa_verify_message(). Since there is no prehashing, it cannot be used1602* with psa_sign_hash() or psa_verify_hash().1603*1604* The signature format is the concatenation of R and S as defined by1605* RFC 8032 §5.1.6 and §5.2.6 (a 64-byte string for Ed25519, a 114-byte1606* string for Ed448).1607*/1608#define PSA_ALG_PURE_EDDSA ((psa_algorithm_t) 0x06000800)16091610#define PSA_ALG_HASH_EDDSA_BASE ((psa_algorithm_t) 0x06000900)1611#define PSA_ALG_IS_HASH_EDDSA(alg) \1612(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HASH_EDDSA_BASE)16131614/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),1615* using SHA-512 and the Edwards25519 curve.1616*1617* See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.1618*1619* This algorithm is Ed25519 as specified in RFC 8032.1620* The curve is Edwards25519.1621* The prehash is SHA-512.1622* The hash function used internally is SHA-512.1623*1624* This is a hash-and-sign algorithm: to calculate a signature,1625* you can either:1626* - call psa_sign_message() on the message;1627* - or calculate the SHA-512 hash of the message1628* with psa_hash_compute()1629* or with a multi-part hash operation started with psa_hash_setup(),1630* using the hash algorithm #PSA_ALG_SHA_512,1631* then sign the calculated hash with psa_sign_hash().1632* Verifying a signature is similar, using psa_verify_message() or1633* psa_verify_hash() instead of the signature function.1634*/1635#define PSA_ALG_ED25519PH \1636(PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHA_512 & PSA_ALG_HASH_MASK))16371638/** Edwards-curve digital signature algorithm with prehashing (HashEdDSA),1639* using SHAKE256 and the Edwards448 curve.1640*1641* See #PSA_ALG_PURE_EDDSA regarding context support and the signature format.1642*1643* This algorithm is Ed448 as specified in RFC 8032.1644* The curve is Edwards448.1645* The prehash is the first 64 bytes of the SHAKE256 output.1646* The hash function used internally is the first 114 bytes of the1647* SHAKE256 output.1648*1649* This is a hash-and-sign algorithm: to calculate a signature,1650* you can either:1651* - call psa_sign_message() on the message;1652* - or calculate the first 64 bytes of the SHAKE256 output of the message1653* with psa_hash_compute()1654* or with a multi-part hash operation started with psa_hash_setup(),1655* using the hash algorithm #PSA_ALG_SHAKE256_512,1656* then sign the calculated hash with psa_sign_hash().1657* Verifying a signature is similar, using psa_verify_message() or1658* psa_verify_hash() instead of the signature function.1659*/1660#define PSA_ALG_ED448PH \1661(PSA_ALG_HASH_EDDSA_BASE | (PSA_ALG_SHAKE256_512 & PSA_ALG_HASH_MASK))16621663/* Default definition, to be overridden if the library is extended with1664* more hash-and-sign algorithms that we want to keep out of this header1665* file. */1666#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) 016671668/** Whether the specified algorithm is a signature algorithm that can be used1669* with psa_sign_hash() and psa_verify_hash().1670*1671* This encompasses all strict hash-and-sign algorithms categorized by1672* PSA_ALG_IS_HASH_AND_SIGN(), as well as algorithms that follow the1673* paradigm more loosely:1674* - #PSA_ALG_RSA_PKCS1V15_SIGN_RAW (expects its input to be an encoded hash)1675* - #PSA_ALG_ECDSA_ANY (doesn't specify what kind of hash the input is)1676*1677* \param alg An algorithm identifier (value of type psa_algorithm_t).1678*1679* \return 1 if alg is a signature algorithm that can be used to sign a1680* hash. 0 if alg is a signature algorithm that can only be used1681* to sign a message. 0 if alg is not a signature algorithm.1682* This macro can return either 0 or 1 if alg is not a1683* supported algorithm identifier.1684*/1685#define PSA_ALG_IS_SIGN_HASH(alg) \1686(PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \1687PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_HASH_EDDSA(alg) || \1688PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg))16891690/** Whether the specified algorithm is a signature algorithm that can be used1691* with psa_sign_message() and psa_verify_message().1692*1693* \param alg An algorithm identifier (value of type #psa_algorithm_t).1694*1695* \return 1 if alg is a signature algorithm that can be used to sign a1696* message. 0 if \p alg is a signature algorithm that can only be used1697* to sign an already-calculated hash. 0 if \p alg is not a signature1698* algorithm. This macro can return either 0 or 1 if \p alg is not a1699* supported algorithm identifier.1700*/1701#define PSA_ALG_IS_SIGN_MESSAGE(alg) \1702(PSA_ALG_IS_SIGN_HASH(alg) || (alg) == PSA_ALG_PURE_EDDSA)17031704/** Whether the specified algorithm is a hash-and-sign algorithm.1705*1706* Hash-and-sign algorithms are asymmetric (public-key) signature algorithms1707* structured in two parts: first the calculation of a hash in a way that1708* does not depend on the key, then the calculation of a signature from the1709* hash value and the key. Hash-and-sign algorithms encode the hash1710* used for the hashing step, and you can call #PSA_ALG_SIGN_GET_HASH1711* to extract this algorithm.1712*1713* Thus, for a hash-and-sign algorithm,1714* `psa_sign_message(key, alg, input, ...)` is equivalent to1715* ```1716* psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), input, ..., hash, ...);1717* psa_sign_hash(key, alg, hash, ..., signature, ...);1718* ```1719* Most usefully, separating the hash from the signature allows the hash1720* to be calculated in multiple steps with psa_hash_setup(), psa_hash_update()1721* and psa_hash_finish(). Likewise psa_verify_message() is equivalent to1722* calculating the hash and then calling psa_verify_hash().1723*1724* \param alg An algorithm identifier (value of type #psa_algorithm_t).1725*1726* \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.1727* This macro may return either 0 or 1 if \p alg is not a supported1728* algorithm identifier.1729*/1730#define PSA_ALG_IS_HASH_AND_SIGN(alg) \1731(PSA_ALG_IS_SIGN_HASH(alg) && \1732((alg) & PSA_ALG_HASH_MASK) != 0)17331734/** Get the hash used by a hash-and-sign signature algorithm.1735*1736* A hash-and-sign algorithm is a signature algorithm which is1737* composed of two phases: first a hashing phase which does not use1738* the key and produces a hash of the input message, then a signing1739* phase which only uses the hash and the key and not the message1740* itself.1741*1742* \param alg A signature algorithm (\c PSA_ALG_XXX value such that1743* #PSA_ALG_IS_SIGN(\p alg) is true).1744*1745* \return The underlying hash algorithm if \p alg is a hash-and-sign1746* algorithm.1747* \return 0 if \p alg is a signature algorithm that does not1748* follow the hash-and-sign structure.1749* \return Unspecified if \p alg is not a signature algorithm or1750* if it is not supported by the implementation.1751*/1752#define PSA_ALG_SIGN_GET_HASH(alg) \1753(PSA_ALG_IS_HASH_AND_SIGN(alg) ? \1754((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \17550)17561757/** RSA PKCS#1 v1.5 encryption.1758*1759* \warning Calling psa_asymmetric_decrypt() with this algorithm as a1760* parameter is considered an inherently dangerous function1761* (CWE-242). Unless it is used in a side channel free and safe1762* way (eg. implementing the TLS protocol as per 7.4.7.1 of1763* RFC 5246), the calling code is vulnerable.1764*1765*/1766#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t) 0x07000200)17671768#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t) 0x07000300)1769/** RSA OAEP encryption.1770*1771* This is the encryption scheme defined by RFC 80171772* (PKCS#1: RSA Cryptography Specifications) under the name1773* RSAES-OAEP, with the message generation function MGF1.1774*1775* \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that1776* #PSA_ALG_IS_HASH(\p hash_alg) is true) to use1777* for MGF1.1778*1779* \return The corresponding RSA OAEP encryption algorithm.1780* \return Unspecified if \p hash_alg is not a supported1781* hash algorithm.1782*/1783#define PSA_ALG_RSA_OAEP(hash_alg) \1784(PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1785#define PSA_ALG_IS_RSA_OAEP(alg) \1786(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)1787#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \1788(PSA_ALG_IS_RSA_OAEP(alg) ? \1789((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \17900)17911792#define PSA_ALG_HKDF_BASE ((psa_algorithm_t) 0x08000100)1793/** Macro to build an HKDF algorithm.1794*1795* For example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)` is HKDF using HMAC-SHA-256.1796*1797* This key derivation algorithm uses the following inputs:1798* - #PSA_KEY_DERIVATION_INPUT_SALT is the salt used in the "extract" step.1799* It is optional; if omitted, the derivation uses an empty salt.1800* - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key used in the "extract" step.1801* - #PSA_KEY_DERIVATION_INPUT_INFO is the info string used in the "expand" step.1802* You must pass #PSA_KEY_DERIVATION_INPUT_SALT before #PSA_KEY_DERIVATION_INPUT_SECRET.1803* You may pass #PSA_KEY_DERIVATION_INPUT_INFO at any time after steup and before1804* starting to generate output.1805*1806* \warning HKDF processes the salt as follows: first hash it with hash_alg1807* if the salt is longer than the block size of the hash algorithm; then1808* pad with null bytes up to the block size. As a result, it is possible1809* for distinct salt inputs to result in the same outputs. To ensure1810* unique outputs, it is recommended to use a fixed length for salt values.1811*1812* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1813* #PSA_ALG_IS_HASH(\p hash_alg) is true).1814*1815* \return The corresponding HKDF algorithm.1816* \return Unspecified if \p hash_alg is not a supported1817* hash algorithm.1818*/1819#define PSA_ALG_HKDF(hash_alg) \1820(PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1821/** Whether the specified algorithm is an HKDF algorithm.1822*1823* HKDF is a family of key derivation algorithms that are based on a hash1824* function and the HMAC construction.1825*1826* \param alg An algorithm identifier (value of type #psa_algorithm_t).1827*1828* \return 1 if \c alg is an HKDF algorithm, 0 otherwise.1829* This macro may return either 0 or 1 if \c alg is not a supported1830* key derivation algorithm identifier.1831*/1832#define PSA_ALG_IS_HKDF(alg) \1833(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)1834#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \1835(PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))18361837#define PSA_ALG_HKDF_EXTRACT_BASE ((psa_algorithm_t) 0x08000400)1838/** Macro to build an HKDF-Extract algorithm.1839*1840* For example, `PSA_ALG_HKDF_EXTRACT(PSA_ALG_SHA_256)` is1841* HKDF-Extract using HMAC-SHA-256.1842*1843* This key derivation algorithm uses the following inputs:1844* - PSA_KEY_DERIVATION_INPUT_SALT is the salt.1845* - PSA_KEY_DERIVATION_INPUT_SECRET is the input keying material used in the1846* "extract" step.1847* The inputs are mandatory and must be passed in the order above.1848* Each input may only be passed once.1849*1850* \warning HKDF-Extract is not meant to be used on its own. PSA_ALG_HKDF1851* should be used instead if possible. PSA_ALG_HKDF_EXTRACT is provided1852* as a separate algorithm for the sake of protocols that use it as a1853* building block. It may also be a slight performance optimization1854* in applications that use HKDF with the same salt and key but many1855* different info strings.1856*1857* \warning HKDF processes the salt as follows: first hash it with hash_alg1858* if the salt is longer than the block size of the hash algorithm; then1859* pad with null bytes up to the block size. As a result, it is possible1860* for distinct salt inputs to result in the same outputs. To ensure1861* unique outputs, it is recommended to use a fixed length for salt values.1862*1863* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1864* #PSA_ALG_IS_HASH(\p hash_alg) is true).1865*1866* \return The corresponding HKDF-Extract algorithm.1867* \return Unspecified if \p hash_alg is not a supported1868* hash algorithm.1869*/1870#define PSA_ALG_HKDF_EXTRACT(hash_alg) \1871(PSA_ALG_HKDF_EXTRACT_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1872/** Whether the specified algorithm is an HKDF-Extract algorithm.1873*1874* HKDF-Extract is a family of key derivation algorithms that are based1875* on a hash function and the HMAC construction.1876*1877* \param alg An algorithm identifier (value of type #psa_algorithm_t).1878*1879* \return 1 if \c alg is an HKDF-Extract algorithm, 0 otherwise.1880* This macro may return either 0 or 1 if \c alg is not a supported1881* key derivation algorithm identifier.1882*/1883#define PSA_ALG_IS_HKDF_EXTRACT(alg) \1884(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE)18851886#define PSA_ALG_HKDF_EXPAND_BASE ((psa_algorithm_t) 0x08000500)1887/** Macro to build an HKDF-Expand algorithm.1888*1889* For example, `PSA_ALG_HKDF_EXPAND(PSA_ALG_SHA_256)` is1890* HKDF-Expand using HMAC-SHA-256.1891*1892* This key derivation algorithm uses the following inputs:1893* - PSA_KEY_DERIVATION_INPUT_SECRET is the pseudorandom key (PRK).1894* - PSA_KEY_DERIVATION_INPUT_INFO is the info string.1895*1896* The inputs are mandatory and must be passed in the order above.1897* Each input may only be passed once.1898*1899* \warning HKDF-Expand is not meant to be used on its own. `PSA_ALG_HKDF`1900* should be used instead if possible. `PSA_ALG_HKDF_EXPAND` is provided as1901* a separate algorithm for the sake of protocols that use it as a building1902* block. It may also be a slight performance optimization in applications1903* that use HKDF with the same salt and key but many different info strings.1904*1905* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1906* #PSA_ALG_IS_HASH(\p hash_alg) is true).1907*1908* \return The corresponding HKDF-Expand algorithm.1909* \return Unspecified if \p hash_alg is not a supported1910* hash algorithm.1911*/1912#define PSA_ALG_HKDF_EXPAND(hash_alg) \1913(PSA_ALG_HKDF_EXPAND_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))1914/** Whether the specified algorithm is an HKDF-Expand algorithm.1915*1916* HKDF-Expand is a family of key derivation algorithms that are based1917* on a hash function and the HMAC construction.1918*1919* \param alg An algorithm identifier (value of type #psa_algorithm_t).1920*1921* \return 1 if \c alg is an HKDF-Expand algorithm, 0 otherwise.1922* This macro may return either 0 or 1 if \c alg is not a supported1923* key derivation algorithm identifier.1924*/1925#define PSA_ALG_IS_HKDF_EXPAND(alg) \1926(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)19271928/** Whether the specified algorithm is an HKDF or HKDF-Extract or1929* HKDF-Expand algorithm.1930*1931*1932* \param alg An algorithm identifier (value of type #psa_algorithm_t).1933*1934* \return 1 if \c alg is any HKDF type algorithm, 0 otherwise.1935* This macro may return either 0 or 1 if \c alg is not a supported1936* key derivation algorithm identifier.1937*/1938#define PSA_ALG_IS_ANY_HKDF(alg) \1939(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE || \1940((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXTRACT_BASE || \1941((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_EXPAND_BASE)19421943#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t) 0x08000200)1944/** Macro to build a TLS-1.2 PRF algorithm.1945*1946* TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,1947* specified in Section 5 of RFC 5246. It is based on HMAC and can be1948* used with either SHA-256 or SHA-384.1949*1950* This key derivation algorithm uses the following inputs, which must be1951* passed in the order given here:1952* - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.1953* - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.1954* - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.1955*1956* For the application to TLS-1.2 key expansion, the seed is the1957* concatenation of ServerHello.Random + ClientHello.Random,1958* and the label is "key expansion".1959*1960* For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256)` represents the1961* TLS 1.2 PRF using HMAC-SHA-256.1962*1963* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that1964* #PSA_ALG_IS_HASH(\p hash_alg) is true).1965*1966* \return The corresponding TLS-1.2 PRF algorithm.1967* \return Unspecified if \p hash_alg is not a supported1968* hash algorithm.1969*/1970#define PSA_ALG_TLS12_PRF(hash_alg) \1971(PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))19721973/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.1974*1975* \param alg An algorithm identifier (value of type #psa_algorithm_t).1976*1977* \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.1978* This macro may return either 0 or 1 if \c alg is not a supported1979* key derivation algorithm identifier.1980*/1981#define PSA_ALG_IS_TLS12_PRF(alg) \1982(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)1983#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \1984(PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))19851986#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t) 0x08000300)1987/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.1988*1989* In a pure-PSK handshake in TLS 1.2, the master secret is derived1990* from the PreSharedKey (PSK) through the application of padding1991* (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).1992* The latter is based on HMAC and can be used with either SHA-2561993* or SHA-384.1994*1995* This key derivation algorithm uses the following inputs, which must be1996* passed in the order given here:1997* - #PSA_KEY_DERIVATION_INPUT_SEED is the seed.1998* - #PSA_KEY_DERIVATION_INPUT_OTHER_SECRET is the other secret for the1999* computation of the premaster secret. This input is optional;2000* if omitted, it defaults to a string of null bytes with the same length2001* as the secret (PSK) input.2002* - #PSA_KEY_DERIVATION_INPUT_SECRET is the secret key.2003* - #PSA_KEY_DERIVATION_INPUT_LABEL is the label.2004*2005* For the application to TLS-1.2, the seed (which is2006* forwarded to the TLS-1.2 PRF) is the concatenation of the2007* ClientHello.Random + ServerHello.Random,2008* the label is "master secret" or "extended master secret" and2009* the other secret depends on the key exchange specified in the cipher suite:2010* - for a plain PSK cipher suite (RFC 4279, Section 2), omit2011* PSA_KEY_DERIVATION_INPUT_OTHER_SECRET2012* - for a DHE-PSK (RFC 4279, Section 3) or ECDHE-PSK cipher suite2013* (RFC 5489, Section 2), the other secret should be the output of the2014* PSA_ALG_FFDH or PSA_ALG_ECDH key agreement performed with the peer.2015* The recommended way to pass this input is to use a key derivation2016* algorithm constructed as2017* PSA_ALG_KEY_AGREEMENT(ka_alg, PSA_ALG_TLS12_PSK_TO_MS(hash_alg))2018* and to call psa_key_derivation_key_agreement(). Alternatively,2019* this input may be an output of `psa_raw_key_agreement()` passed with2020* psa_key_derivation_input_bytes(), or an equivalent input passed with2021* psa_key_derivation_input_bytes() or psa_key_derivation_input_key().2022* - for a RSA-PSK cipher suite (RFC 4279, Section 4), the other secret2023* should be the 48-byte client challenge (the PreMasterSecret of2024* (RFC 5246, Section 7.4.7.1)) concatenation of the TLS version and2025* a 46-byte random string chosen by the client. On the server, this is2026* typically an output of psa_asymmetric_decrypt() using2027* PSA_ALG_RSA_PKCS1V15_CRYPT, passed to the key derivation operation2028* with `psa_key_derivation_input_bytes()`.2029*2030* For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256)` represents the2031* TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.2032*2033* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that2034* #PSA_ALG_IS_HASH(\p hash_alg) is true).2035*2036* \return The corresponding TLS-1.2 PSK to MS algorithm.2037* \return Unspecified if \p hash_alg is not a supported2038* hash algorithm.2039*/2040#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \2041(PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))20422043/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.2044*2045* \param alg An algorithm identifier (value of type #psa_algorithm_t).2046*2047* \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.2048* This macro may return either 0 or 1 if \c alg is not a supported2049* key derivation algorithm identifier.2050*/2051#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \2052(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)2053#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \2054(PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))20552056/* The TLS 1.2 ECJPAKE-to-PMS KDF. It takes the shared secret K (an EC point2057* in case of EC J-PAKE) and calculates SHA256(K.X) that the rest of TLS 1.22058* will use to derive the session secret, as defined by step 2 of2059* https://datatracker.ietf.org/doc/html/draft-cragie-tls-ecjpake-01#section-8.7.2060* Uses PSA_ALG_SHA_256.2061* This function takes a single input:2062* #PSA_KEY_DERIVATION_INPUT_SECRET is the shared secret K from EC J-PAKE.2063* The only supported curve is secp256r1 (the 256-bit curve in2064* #PSA_ECC_FAMILY_SECP_R1), so the input must be exactly 65 bytes.2065* The output has to be read as a single chunk of 32 bytes, defined as2066* PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE.2067*/2068#define PSA_ALG_TLS12_ECJPAKE_TO_PMS ((psa_algorithm_t) 0x08000609)20692070/* This flag indicates whether the key derivation algorithm is suitable for2071* use on low-entropy secrets such as password - these algorithms are also2072* known as key stretching or password hashing schemes. These are also the2073* algorithms that accepts inputs of type #PSA_KEY_DERIVATION_INPUT_PASSWORD.2074*2075* Those algorithms cannot be combined with a key agreement algorithm.2076*/2077#define PSA_ALG_KEY_DERIVATION_STRETCHING_FLAG ((psa_algorithm_t) 0x00800000)20782079#define PSA_ALG_PBKDF2_HMAC_BASE ((psa_algorithm_t) 0x08800100)2080/** Macro to build a PBKDF2-HMAC password hashing / key stretching algorithm.2081*2082* PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).2083* This macro specifies the PBKDF2 algorithm constructed using a PRF based on2084* HMAC with the specified hash.2085* For example, `PSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA_256)` specifies PBKDF22086* using the PRF HMAC-SHA-256.2087*2088* This key derivation algorithm uses the following inputs, which must be2089* provided in the following order:2090* - #PSA_KEY_DERIVATION_INPUT_COST is the iteration count.2091* This input step must be used exactly once.2092* - #PSA_KEY_DERIVATION_INPUT_SALT is the salt.2093* This input step must be used one or more times; if used several times, the2094* inputs will be concatenated. This can be used to build the final salt2095* from multiple sources, both public and secret (also known as pepper).2096* - #PSA_KEY_DERIVATION_INPUT_PASSWORD is the password to be hashed.2097* This input step must be used exactly once.2098*2099* \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that2100* #PSA_ALG_IS_HASH(\p hash_alg) is true).2101*2102* \return The corresponding PBKDF2-HMAC-XXX algorithm.2103* \return Unspecified if \p hash_alg is not a supported2104* hash algorithm.2105*/2106#define PSA_ALG_PBKDF2_HMAC(hash_alg) \2107(PSA_ALG_PBKDF2_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))21082109/** Whether the specified algorithm is a PBKDF2-HMAC algorithm.2110*2111* \param alg An algorithm identifier (value of type #psa_algorithm_t).2112*2113* \return 1 if \c alg is a PBKDF2-HMAC algorithm, 0 otherwise.2114* This macro may return either 0 or 1 if \c alg is not a supported2115* key derivation algorithm identifier.2116*/2117#define PSA_ALG_IS_PBKDF2_HMAC(alg) \2118(((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_PBKDF2_HMAC_BASE)2119#define PSA_ALG_PBKDF2_HMAC_GET_HASH(pbkdf2_alg) \2120(PSA_ALG_CATEGORY_HASH | ((pbkdf2_alg) & PSA_ALG_HASH_MASK))2121/** The PBKDF2-AES-CMAC-PRF-128 password hashing / key stretching algorithm.2122*2123* PBKDF2 is defined by PKCS#5, republished as RFC 8018 (section 5.2).2124* This macro specifies the PBKDF2 algorithm constructed using the2125* AES-CMAC-PRF-128 PRF specified by RFC 4615.2126*2127* This key derivation algorithm uses the same inputs as2128* #PSA_ALG_PBKDF2_HMAC() with the same constraints.2129*/2130#define PSA_ALG_PBKDF2_AES_CMAC_PRF_128 ((psa_algorithm_t) 0x08800200)21312132#define PSA_ALG_IS_PBKDF2(kdf_alg) \2133(PSA_ALG_IS_PBKDF2_HMAC(kdf_alg) || \2134((kdf_alg) == PSA_ALG_PBKDF2_AES_CMAC_PRF_128))21352136#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t) 0xfe00ffff)2137#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t) 0xffff0000)21382139/** Macro to build a combined algorithm that chains a key agreement with2140* a key derivation.2141*2142* \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such2143* that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).2144* \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such2145* that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).2146*2147* \return The corresponding key agreement and derivation2148* algorithm.2149* \return Unspecified if \p ka_alg is not a supported2150* key agreement algorithm or \p kdf_alg is not a2151* supported key derivation algorithm.2152*/2153#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \2154((ka_alg) | (kdf_alg))21552156#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \2157(((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)21582159#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \2160(((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)21612162/** Whether the specified algorithm is a raw key agreement algorithm.2163*2164* A raw key agreement algorithm is one that does not specify2165* a key derivation function.2166* Usually, raw key agreement algorithms are constructed directly with2167* a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are2168* constructed with #PSA_ALG_KEY_AGREEMENT().2169*2170* \param alg An algorithm identifier (value of type #psa_algorithm_t).2171*2172* \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.2173* This macro may return either 0 or 1 if \p alg is not a supported2174* algorithm identifier.2175*/2176#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \2177(PSA_ALG_IS_KEY_AGREEMENT(alg) && \2178PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)21792180#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \2181((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))21822183/** The finite-field Diffie-Hellman (DH) key agreement algorithm.2184*2185* The shared secret produced by key agreement is2186* `g^{ab}` in big-endian format.2187* It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`2188* in bits.2189*/2190#define PSA_ALG_FFDH ((psa_algorithm_t) 0x09010000)21912192/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.2193*2194* This includes the raw finite field Diffie-Hellman algorithm as well as2195* finite-field Diffie-Hellman followed by any supporter key derivation2196* algorithm.2197*2198* \param alg An algorithm identifier (value of type #psa_algorithm_t).2199*2200* \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.2201* This macro may return either 0 or 1 if \c alg is not a supported2202* key agreement algorithm identifier.2203*/2204#define PSA_ALG_IS_FFDH(alg) \2205(PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)22062207/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.2208*2209* The shared secret produced by key agreement is the x-coordinate of2210* the shared secret point. It is always `ceiling(m / 8)` bytes long where2211* `m` is the bit size associated with the curve, i.e. the bit size of the2212* order of the curve's coordinate field. When `m` is not a multiple of 8,2213* the byte containing the most significant bit of the shared secret2214* is padded with zero bits. The byte order is either little-endian2215* or big-endian depending on the curve type.2216*2217* - For Montgomery curves (curve types `PSA_ECC_FAMILY_CURVEXXX`),2218* the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`2219* in little-endian byte order.2220* The bit size is 448 for Curve448 and 255 for Curve25519.2221* - For Weierstrass curves over prime fields (curve types2222* `PSA_ECC_FAMILY_SECPXXX` and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`),2223* the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`2224* in big-endian byte order.2225* The bit size is `m = ceiling(log_2(p))` for the field `F_p`.2226* - For Weierstrass curves over binary fields (curve types2227* `PSA_ECC_FAMILY_SECTXXX`),2228* the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`2229* in big-endian byte order.2230* The bit size is `m` for the field `F_{2^m}`.2231*/2232#define PSA_ALG_ECDH ((psa_algorithm_t) 0x09020000)22332234/** Whether the specified algorithm is an elliptic curve Diffie-Hellman2235* algorithm.2236*2237* This includes the raw elliptic curve Diffie-Hellman algorithm as well as2238* elliptic curve Diffie-Hellman followed by any supporter key derivation2239* algorithm.2240*2241* \param alg An algorithm identifier (value of type #psa_algorithm_t).2242*2243* \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,2244* 0 otherwise.2245* This macro may return either 0 or 1 if \c alg is not a supported2246* key agreement algorithm identifier.2247*/2248#define PSA_ALG_IS_ECDH(alg) \2249(PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)22502251/** Whether the specified algorithm encoding is a wildcard.2252*2253* Wildcard values may only be used to set the usage algorithm field in2254* a policy, not to perform an operation.2255*2256* \param alg An algorithm identifier (value of type #psa_algorithm_t).2257*2258* \return 1 if \c alg is a wildcard algorithm encoding.2259* \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for2260* an operation).2261* \return This macro may return either 0 or 1 if \c alg is not a supported2262* algorithm identifier.2263*/2264#define PSA_ALG_IS_WILDCARD(alg) \2265(PSA_ALG_IS_HASH_AND_SIGN(alg) ? \2266PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \2267PSA_ALG_IS_MAC(alg) ? \2268(alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \2269PSA_ALG_IS_AEAD(alg) ? \2270(alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0 : \2271(alg) == PSA_ALG_ANY_HASH)22722273/** Get the hash used by a composite algorithm.2274*2275* \param alg An algorithm identifier (value of type #psa_algorithm_t).2276*2277* \return The underlying hash algorithm if alg is a composite algorithm that2278* uses a hash algorithm.2279*2280* \return \c 0 if alg is not a composite algorithm that uses a hash.2281*/2282#define PSA_ALG_GET_HASH(alg) \2283(((alg) & 0x000000ff) == 0 ? ((psa_algorithm_t) 0) : 0x02000000 | ((alg) & 0x000000ff))22842285/**@}*/22862287/** \defgroup key_lifetimes Key lifetimes2288* @{2289*/22902291/* Note that location and persistence level values are embedded in the2292* persistent key store, as part of key metadata. As a consequence, they2293* must not be changed (unless the storage format version changes).2294*/22952296/** The default lifetime for volatile keys.2297*2298* A volatile key only exists as long as the identifier to it is not destroyed.2299* The key material is guaranteed to be erased on a power reset.2300*2301* A key with this lifetime is typically stored in the RAM area of the2302* PSA Crypto subsystem. However this is an implementation choice.2303* If an implementation stores data about the key in a non-volatile memory,2304* it must release all the resources associated with the key and erase the2305* key material if the calling application terminates.2306*/2307#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t) 0x00000000)23082309/** The default lifetime for persistent keys.2310*2311* A persistent key remains in storage until it is explicitly destroyed or2312* until the corresponding storage area is wiped. This specification does2313* not define any mechanism to wipe a storage area, but integrations may2314* provide their own mechanism (for example to perform a factory reset,2315* to prepare for device refurbishment, or to uninstall an application).2316*2317* This lifetime value is the default storage area for the calling2318* application. Integrations of Mbed TLS may support other persistent lifetimes.2319* See ::psa_key_lifetime_t for more information.2320*/2321#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t) 0x00000001)23222323/** The persistence level of volatile keys.2324*2325* See ::psa_key_persistence_t for more information.2326*/2327#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t) 0x00)23282329/** The default persistence level for persistent keys.2330*2331* See ::psa_key_persistence_t for more information.2332*/2333#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t) 0x01)23342335/** A persistence level indicating that a key is never destroyed.2336*2337* See ::psa_key_persistence_t for more information.2338*/2339#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t) 0xff)23402341#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \2342((psa_key_persistence_t) ((lifetime) & 0x000000ff))23432344#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \2345((psa_key_location_t) ((lifetime) >> 8))23462347/** Whether a key lifetime indicates that the key is volatile.2348*2349* A volatile key is automatically destroyed by the implementation when2350* the application instance terminates. In particular, a volatile key2351* is automatically destroyed on a power reset of the device.2352*2353* A key that is not volatile is persistent. Persistent keys are2354* preserved until the application explicitly destroys them or until an2355* implementation-specific device management event occurs (for example,2356* a factory reset).2357*2358* \param lifetime The lifetime value to query (value of type2359* ::psa_key_lifetime_t).2360*2361* \return \c 1 if the key is volatile, otherwise \c 0.2362*/2363#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \2364(PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \2365PSA_KEY_PERSISTENCE_VOLATILE)23662367/** Whether a key lifetime indicates that the key is read-only.2368*2369* Read-only keys cannot be created or destroyed through the PSA Crypto API.2370* They must be created through platform-specific means that bypass the API.2371*2372* Some platforms may offer ways to destroy read-only keys. For example,2373* consider a platform with multiple levels of privilege, where a2374* low-privilege application can use a key but is not allowed to destroy2375* it, and the platform exposes the key to the application with a read-only2376* lifetime. High-privilege code can destroy the key even though the2377* application sees the key as read-only.2378*2379* \param lifetime The lifetime value to query (value of type2380* ::psa_key_lifetime_t).2381*2382* \return \c 1 if the key is read-only, otherwise \c 0.2383*/2384#define PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime) \2385(PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \2386PSA_KEY_PERSISTENCE_READ_ONLY)23872388/** Construct a lifetime from a persistence level and a location.2389*2390* \param persistence The persistence level2391* (value of type ::psa_key_persistence_t).2392* \param location The location indicator2393* (value of type ::psa_key_location_t).2394*2395* \return The constructed lifetime value.2396*/2397#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \2398((location) << 8 | (persistence))23992400/** The local storage area for persistent keys.2401*2402* This storage area is available on all systems that can store persistent2403* keys without delegating the storage to a third-party cryptoprocessor.2404*2405* See ::psa_key_location_t for more information.2406*/2407#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t) 0x000000)24082409#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t) 0x800000)24102411/* Note that key identifier values are embedded in the2412* persistent key store, as part of key metadata. As a consequence, they2413* must not be changed (unless the storage format version changes).2414*/24152416/** The null key identifier.2417*/2418/* *INDENT-OFF* (https://github.com/ARM-software/psa-arch-tests/issues/337) */2419#define PSA_KEY_ID_NULL ((psa_key_id_t)0)2420/* *INDENT-ON* */2421/** The minimum value for a key identifier chosen by the application.2422*/2423#define PSA_KEY_ID_USER_MIN ((psa_key_id_t) 0x00000001)2424/** The maximum value for a key identifier chosen by the application.2425*/2426#define PSA_KEY_ID_USER_MAX ((psa_key_id_t) 0x3fffffff)2427/** The minimum value for a key identifier chosen by the implementation.2428*/2429#define PSA_KEY_ID_VENDOR_MIN ((psa_key_id_t) 0x40000000)2430/** The maximum value for a key identifier chosen by the implementation.2431*/2432#define PSA_KEY_ID_VENDOR_MAX ((psa_key_id_t) 0x7fffffff)243324342435#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)24362437#define MBEDTLS_SVC_KEY_ID_INIT ((psa_key_id_t) 0)2438#define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) (id)2439#define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) (0)24402441/** Utility to initialize a key identifier at runtime.2442*2443* \param unused Unused parameter.2444* \param key_id Identifier of the key.2445*/2446static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(2447unsigned int unused, psa_key_id_t key_id)2448{2449(void) unused;24502451return key_id;2452}24532454/** Compare two key identifiers.2455*2456* \param id1 First key identifier.2457* \param id2 Second key identifier.2458*2459* \return Non-zero if the two key identifier are equal, zero otherwise.2460*/2461static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,2462mbedtls_svc_key_id_t id2)2463{2464return id1 == id2;2465}24662467/** Check whether a key identifier is null.2468*2469* \param key Key identifier.2470*2471* \return Non-zero if the key identifier is null, zero otherwise.2472*/2473static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2474{2475return key == 0;2476}24772478#else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */24792480#define MBEDTLS_SVC_KEY_ID_INIT ((mbedtls_svc_key_id_t){ 0, 0 })2481#define MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) ((id).MBEDTLS_PRIVATE(key_id))2482#define MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(id) ((id).MBEDTLS_PRIVATE(owner))24832484/** Utility to initialize a key identifier at runtime.2485*2486* \param owner_id Identifier of the key owner.2487* \param key_id Identifier of the key.2488*/2489static inline mbedtls_svc_key_id_t mbedtls_svc_key_id_make(2490mbedtls_key_owner_id_t owner_id, psa_key_id_t key_id)2491{2492return (mbedtls_svc_key_id_t){ .MBEDTLS_PRIVATE(key_id) = key_id,2493.MBEDTLS_PRIVATE(owner) = owner_id };2494}24952496/** Compare two key identifiers.2497*2498* \param id1 First key identifier.2499* \param id2 Second key identifier.2500*2501* \return Non-zero if the two key identifier are equal, zero otherwise.2502*/2503static inline int mbedtls_svc_key_id_equal(mbedtls_svc_key_id_t id1,2504mbedtls_svc_key_id_t id2)2505{2506return (id1.MBEDTLS_PRIVATE(key_id) == id2.MBEDTLS_PRIVATE(key_id)) &&2507mbedtls_key_owner_id_equal(id1.MBEDTLS_PRIVATE(owner), id2.MBEDTLS_PRIVATE(owner));2508}25092510/** Check whether a key identifier is null.2511*2512* \param key Key identifier.2513*2514* \return Non-zero if the key identifier is null, zero otherwise.2515*/2516static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)2517{2518return key.MBEDTLS_PRIVATE(key_id) == 0;2519}25202521#endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */25222523/**@}*/25242525/** \defgroup policy Key policies2526* @{2527*/25282529/* Note that key usage flags are embedded in the2530* persistent key store, as part of key metadata. As a consequence, they2531* must not be changed (unless the storage format version changes).2532*/25332534/** Whether the key may be exported.2535*2536* A public key or the public part of a key pair may always be exported2537* regardless of the value of this permission flag.2538*2539* If a key does not have export permission, implementations shall not2540* allow the key to be exported in plain form from the cryptoprocessor,2541* whether through psa_export_key() or through a proprietary interface.2542* The key may however be exportable in a wrapped form, i.e. in a form2543* where it is encrypted by another key.2544*/2545#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t) 0x00000001)25462547/** Whether the key may be copied.2548*2549* This flag allows the use of psa_copy_key() to make a copy of the key2550* with the same policy or a more restrictive policy.2551*2552* For lifetimes for which the key is located in a secure element which2553* enforce the non-exportability of keys, copying a key outside the secure2554* element also requires the usage flag #PSA_KEY_USAGE_EXPORT.2555* Copying the key inside the secure element is permitted with just2556* #PSA_KEY_USAGE_COPY if the secure element supports it.2557* For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or2558* #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY2559* is sufficient to permit the copy.2560*/2561#define PSA_KEY_USAGE_COPY ((psa_key_usage_t) 0x00000002)25622563/** Whether the key may be used to encrypt a message.2564*2565* This flag allows the key to be used for a symmetric encryption operation,2566* for an AEAD encryption-and-authentication operation,2567* or for an asymmetric encryption operation,2568* if otherwise permitted by the key's type and policy.2569*2570* For a key pair, this concerns the public key.2571*/2572#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t) 0x00000100)25732574/** Whether the key may be used to decrypt a message.2575*2576* This flag allows the key to be used for a symmetric decryption operation,2577* for an AEAD decryption-and-verification operation,2578* or for an asymmetric decryption operation,2579* if otherwise permitted by the key's type and policy.2580*2581* For a key pair, this concerns the private key.2582*/2583#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t) 0x00000200)25842585/** Whether the key may be used to sign a message.2586*2587* This flag allows the key to be used for a MAC calculation operation or for2588* an asymmetric message signature operation, if otherwise permitted by the2589* key’s type and policy.2590*2591* For a key pair, this concerns the private key.2592*/2593#define PSA_KEY_USAGE_SIGN_MESSAGE ((psa_key_usage_t) 0x00000400)25942595/** Whether the key may be used to verify a message.2596*2597* This flag allows the key to be used for a MAC verification operation or for2598* an asymmetric message signature verification operation, if otherwise2599* permitted by the key’s type and policy.2600*2601* For a key pair, this concerns the public key.2602*/2603#define PSA_KEY_USAGE_VERIFY_MESSAGE ((psa_key_usage_t) 0x00000800)26042605/** Whether the key may be used to sign a message.2606*2607* This flag allows the key to be used for a MAC calculation operation2608* or for an asymmetric signature operation,2609* if otherwise permitted by the key's type and policy.2610*2611* For a key pair, this concerns the private key.2612*/2613#define PSA_KEY_USAGE_SIGN_HASH ((psa_key_usage_t) 0x00001000)26142615/** Whether the key may be used to verify a message signature.2616*2617* This flag allows the key to be used for a MAC verification operation2618* or for an asymmetric signature verification operation,2619* if otherwise permitted by the key's type and policy.2620*2621* For a key pair, this concerns the public key.2622*/2623#define PSA_KEY_USAGE_VERIFY_HASH ((psa_key_usage_t) 0x00002000)26242625/** Whether the key may be used to derive other keys or produce a password2626* hash.2627*2628* This flag allows the key to be used for a key derivation operation or for2629* a key agreement operation, if otherwise permitted by the key's type and2630* policy.2631*2632* If this flag is present on all keys used in calls to2633* psa_key_derivation_input_key() for a key derivation operation, then it2634* permits calling psa_key_derivation_output_bytes() or2635* psa_key_derivation_output_key() at the end of the operation.2636*/2637#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t) 0x00004000)26382639/** Whether the key may be used to verify the result of a key derivation,2640* including password hashing.2641*2642* This flag allows the key to be used:2643*2644* This flag allows the key to be used in a key derivation operation, if2645* otherwise permitted by the key's type and policy.2646*2647* If this flag is present on all keys used in calls to2648* psa_key_derivation_input_key() for a key derivation operation, then it2649* permits calling psa_key_derivation_verify_bytes() or2650* psa_key_derivation_verify_key() at the end of the operation.2651*/2652#define PSA_KEY_USAGE_VERIFY_DERIVATION ((psa_key_usage_t) 0x00008000)26532654/**@}*/26552656/** \defgroup derivation Key derivation2657* @{2658*/26592660/* Key input steps are not embedded in the persistent storage, so you can2661* change them if needed: it's only an ABI change. */26622663/** A secret input for key derivation.2664*2665* This should be a key of type #PSA_KEY_TYPE_DERIVE2666* (passed to psa_key_derivation_input_key())2667* or the shared secret resulting from a key agreement2668* (obtained via psa_key_derivation_key_agreement()).2669*2670* The secret can also be a direct input (passed to2671* key_derivation_input_bytes()). In this case, the derivation operation2672* may not be used to derive keys: the operation will only allow2673* psa_key_derivation_output_bytes(),2674* psa_key_derivation_verify_bytes(), or2675* psa_key_derivation_verify_key(), but not2676* psa_key_derivation_output_key().2677*/2678#define PSA_KEY_DERIVATION_INPUT_SECRET ((psa_key_derivation_step_t) 0x0101)26792680/** A low-entropy secret input for password hashing / key stretching.2681*2682* This is usually a key of type #PSA_KEY_TYPE_PASSWORD (passed to2683* psa_key_derivation_input_key()) or a direct input (passed to2684* psa_key_derivation_input_bytes()) that is a password or passphrase. It can2685* also be high-entropy secret such as a key of type #PSA_KEY_TYPE_DERIVE or2686* the shared secret resulting from a key agreement.2687*2688* The secret can also be a direct input (passed to2689* key_derivation_input_bytes()). In this case, the derivation operation2690* may not be used to derive keys: the operation will only allow2691* psa_key_derivation_output_bytes(),2692* psa_key_derivation_verify_bytes(), or2693* psa_key_derivation_verify_key(), but not2694* psa_key_derivation_output_key().2695*/2696#define PSA_KEY_DERIVATION_INPUT_PASSWORD ((psa_key_derivation_step_t) 0x0102)26972698/** A high-entropy additional secret input for key derivation.2699*2700* This is typically the shared secret resulting from a key agreement obtained2701* via `psa_key_derivation_key_agreement()`. It may alternatively be a key of2702* type `PSA_KEY_TYPE_DERIVE` passed to `psa_key_derivation_input_key()`, or2703* a direct input passed to `psa_key_derivation_input_bytes()`.2704*/2705#define PSA_KEY_DERIVATION_INPUT_OTHER_SECRET \2706((psa_key_derivation_step_t) 0x0103)27072708/** A label for key derivation.2709*2710* This should be a direct input.2711* It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.2712*/2713#define PSA_KEY_DERIVATION_INPUT_LABEL ((psa_key_derivation_step_t) 0x0201)27142715/** A salt for key derivation.2716*2717* This should be a direct input.2718* It can also be a key of type #PSA_KEY_TYPE_RAW_DATA or2719* #PSA_KEY_TYPE_PEPPER.2720*/2721#define PSA_KEY_DERIVATION_INPUT_SALT ((psa_key_derivation_step_t) 0x0202)27222723/** An information string for key derivation.2724*2725* This should be a direct input.2726* It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.2727*/2728#define PSA_KEY_DERIVATION_INPUT_INFO ((psa_key_derivation_step_t) 0x0203)27292730/** A seed for key derivation.2731*2732* This should be a direct input.2733* It can also be a key of type #PSA_KEY_TYPE_RAW_DATA.2734*/2735#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t) 0x0204)27362737/** A cost parameter for password hashing / key stretching.2738*2739* This must be a direct input, passed to psa_key_derivation_input_integer().2740*/2741#define PSA_KEY_DERIVATION_INPUT_COST ((psa_key_derivation_step_t) 0x0205)27422743/**@}*/27442745/** \defgroup helper_macros Helper macros2746* @{2747*/27482749/* Helper macros */27502751/** Check if two AEAD algorithm identifiers refer to the same AEAD algorithm2752* regardless of the tag length they encode.2753*2754* \param aead_alg_1 An AEAD algorithm identifier.2755* \param aead_alg_2 An AEAD algorithm identifier.2756*2757* \return 1 if both identifiers refer to the same AEAD algorithm,2758* 0 otherwise.2759* Unspecified if neither \p aead_alg_1 nor \p aead_alg_2 are2760* a supported AEAD algorithm.2761*/2762#define MBEDTLS_PSA_ALG_AEAD_EQUAL(aead_alg_1, aead_alg_2) \2763(!(((aead_alg_1) ^ (aead_alg_2)) & \2764~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG)))27652766/**@}*/27672768/**@}*/27692770/** \defgroup interruptible Interruptible operations2771* @{2772*/27732774/** Maximum value for use with \c psa_interruptible_set_max_ops() to determine2775* the maximum number of ops allowed to be executed by an interruptible2776* function in a single call.2777*/2778#define PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED UINT32_MAX27792780/**@}*/27812782#endif /* PSA_CRYPTO_VALUES_H */278327842785