Path: blob/master/thirdparty/mbedtls/include/psa/crypto_sizes.h
9904 views
/**1* \file psa/crypto_sizes.h2*3* \brief PSA cryptography module: Mbed TLS buffer size macros4*5* \note This file may not be included directly. Applications must6* include psa/crypto.h.7*8* This file contains the definitions of macros that are useful to9* compute buffer sizes. The signatures and semantics of these macros10* are standardized, but the definitions are not, because they depend on11* the available algorithms and, in some cases, on permitted tolerances12* on buffer sizes.13*14* In implementations with isolation between the application and the15* cryptography module, implementers should take care to ensure that16* the definitions that are exposed to applications match what the17* module implements.18*19* Macros that compute sizes whose values do not depend on the20* implementation are in crypto.h.21*/22/*23* Copyright The Mbed TLS Contributors24* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later25*/2627#ifndef PSA_CRYPTO_SIZES_H28#define PSA_CRYPTO_SIZES_H2930/*31* Include the build-time configuration information header. Here, we do not32* include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which33* is basically just an alias to it. This is to ease the maintenance of the34* TF-PSA-Crypto repository which has a different build system and35* configuration.36*/37#include "psa/build_info.h"3839#define PSA_BITS_TO_BYTES(bits) (((bits) + 7u) / 8u)40#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8u)41#define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \42(c) : (b) : (a) <= (c) ? (c) : (a))4344#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \45(((length) + (block_size) - 1) / (block_size) * (block_size))4647/** The size of the output of psa_hash_finish(), in bytes.48*49* This is also the hash size that psa_hash_verify() expects.50*51* \param alg A hash algorithm (\c PSA_ALG_XXX value such that52* #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm53* (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a54* hash algorithm).55*56* \return The hash size for the specified hash algorithm.57* If the hash algorithm is not recognized, return 0.58*/59#define PSA_HASH_LENGTH(alg) \60( \61PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16u : \62PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20u : \63PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20u : \64PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28u : \65PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32u : \66PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48u : \67PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64u : \68PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28u : \69PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32u : \70PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28u : \71PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32u : \72PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48u : \73PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64u : \740u)7576/** The input block size of a hash algorithm, in bytes.77*78* Hash algorithms process their input data in blocks. Hash operations will79* retain any partial blocks until they have enough input to fill the block or80* until the operation is finished.81* This affects the output from psa_hash_suspend().82*83* \param alg A hash algorithm (\c PSA_ALG_XXX value such that84* PSA_ALG_IS_HASH(\p alg) is true).85*86* \return The block size in bytes for the specified hash algorithm.87* If the hash algorithm is not recognized, return 0.88* An implementation can return either 0 or the correct size for a89* hash algorithm that it recognizes, but does not support.90*/91#define PSA_HASH_BLOCK_LENGTH(alg) \92( \93PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64u : \94PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64u : \95PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64u : \96PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64u : \97PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64u : \98PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128u : \99PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128u : \100PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128u : \101PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128u : \102PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144u : \103PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136u : \104PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104u : \105PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72u : \1060u)107108/** \def PSA_HASH_MAX_SIZE109*110* Maximum size of a hash.111*112* This macro expands to a compile-time constant integer. This value113* is the maximum size of a hash in bytes.114*/115/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224,116* 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for117* HMAC-SHA3-512. */118/* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE,119* see the note on MBEDTLS_MD_MAX_SIZE for details. */120#if defined(PSA_WANT_ALG_SHA3_224)121#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u122#elif defined(PSA_WANT_ALG_SHA3_256)123#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 136u124#elif defined(PSA_WANT_ALG_SHA_512)125#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u126#elif defined(PSA_WANT_ALG_SHA_384)127#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u128#elif defined(PSA_WANT_ALG_SHA3_384)129#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 104u130#elif defined(PSA_WANT_ALG_SHA3_512)131#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 72u132#elif defined(PSA_WANT_ALG_SHA_256)133#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u134#elif defined(PSA_WANT_ALG_SHA_224)135#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u136#else /* SHA-1 or smaller */137#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u138#endif139140#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512)141#define PSA_HASH_MAX_SIZE 64u142#elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384)143#define PSA_HASH_MAX_SIZE 48u144#elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256)145#define PSA_HASH_MAX_SIZE 32u146#elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224)147#define PSA_HASH_MAX_SIZE 28u148#else /* SHA-1 or smaller */149#define PSA_HASH_MAX_SIZE 20u150#endif151152/** \def PSA_MAC_MAX_SIZE153*154* Maximum size of a MAC.155*156* This macro expands to a compile-time constant integer. This value157* is the maximum size of a MAC in bytes.158*/159/* All non-HMAC MACs have a maximum size that's smaller than the160* minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */161/* Note that the encoding of truncated MAC algorithms limits this value162* to 64 bytes.163*/164#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE165166/** The length of a tag for an AEAD algorithm, in bytes.167*168* This macro can be used to allocate a buffer of sufficient size to store the169* tag output from psa_aead_finish().170*171* See also #PSA_AEAD_TAG_MAX_SIZE.172*173* \param key_type The type of the AEAD key.174* \param key_bits The size of the AEAD key in bits.175* \param alg An AEAD algorithm176* (\c PSA_ALG_XXX value such that177* #PSA_ALG_IS_AEAD(\p alg) is true).178*179* \return The tag length for the specified algorithm and key.180* If the AEAD algorithm does not have an identified181* tag that can be distinguished from the rest of182* the ciphertext, return 0.183* If the key type or AEAD algorithm is not184* recognized, or the parameters are incompatible,185* return 0.186*/187#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \188(PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \189PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \190((void) (key_bits), 0u))191192/** The maximum tag size for all supported AEAD algorithms, in bytes.193*194* See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).195*/196#define PSA_AEAD_TAG_MAX_SIZE 16u197198/* The maximum size of an RSA key on this implementation, in bits.199* This is a vendor-specific macro.200*201* Mbed TLS does not set a hard limit on the size of RSA keys: any key202* whose parameters fit in a bignum is accepted. However large keys can203* induce a large memory usage and long computation times. Unlike other204* auxiliary macros in this file and in crypto.h, which reflect how the205* library is configured, this macro defines how the library is206* configured. This implementation refuses to import or generate an207* RSA key whose size is larger than the value defined here.208*209* Note that an implementation may set different size limits for different210* operations, and does not need to accept all key sizes up to the limit. */211#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u212213/* The minimum size of an RSA key on this implementation, in bits.214* This is a vendor-specific macro.215*216* Limits RSA key generation to a minimum due to avoid accidental misuse.217* This value cannot be less than 128 bits.218*/219#if defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)220#define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS MBEDTLS_RSA_GEN_KEY_MIN_BITS221#else222#define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS 1024223#endif224225/* The maximum size of an DH key on this implementation, in bits.226* This is a vendor-specific macro.*/227#if defined(PSA_WANT_DH_RFC7919_8192)228#define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u229#elif defined(PSA_WANT_DH_RFC7919_6144)230#define PSA_VENDOR_FFDH_MAX_KEY_BITS 6144u231#elif defined(PSA_WANT_DH_RFC7919_4096)232#define PSA_VENDOR_FFDH_MAX_KEY_BITS 4096u233#elif defined(PSA_WANT_DH_RFC7919_3072)234#define PSA_VENDOR_FFDH_MAX_KEY_BITS 3072u235#elif defined(PSA_WANT_DH_RFC7919_2048)236#define PSA_VENDOR_FFDH_MAX_KEY_BITS 2048u237#else238#define PSA_VENDOR_FFDH_MAX_KEY_BITS 0u239#endif240241/* The maximum size of an ECC key on this implementation, in bits.242* This is a vendor-specific macro. */243#if defined(PSA_WANT_ECC_SECP_R1_521)244#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u245#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)246#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u247#elif defined(PSA_WANT_ECC_MONTGOMERY_448)248#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u249#elif defined(PSA_WANT_ECC_SECP_R1_384)250#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u251#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)252#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u253#elif defined(PSA_WANT_ECC_SECP_R1_256)254#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u255#elif defined(PSA_WANT_ECC_SECP_K1_256)256#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u257#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)258#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u259#elif defined(PSA_WANT_ECC_MONTGOMERY_255)260#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u261#elif defined(PSA_WANT_ECC_SECP_R1_224)262#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u263#elif defined(PSA_WANT_ECC_SECP_K1_224)264#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u265#elif defined(PSA_WANT_ECC_SECP_R1_192)266#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u267#elif defined(PSA_WANT_ECC_SECP_K1_192)268#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u269#else270#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u271#endif272273/** This macro returns the maximum supported length of the PSK for the274* TLS-1.2 PSK-to-MS key derivation275* (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).276*277* The maximum supported length does not depend on the chosen hash algorithm.278*279* Quoting RFC 4279, Sect 5.3:280* TLS implementations supporting these ciphersuites MUST support281* arbitrary PSK identities up to 128 octets in length, and arbitrary282* PSKs up to 64 octets in length. Supporting longer identities and283* keys is RECOMMENDED.284*285* Therefore, no implementation should define a value smaller than 64286* for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.287*/288#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u289290/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,291* which is expected to work with P-256 curve only. */292#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u293294/* The size of a serialized K.X coordinate to be used in295* psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256296* curve. */297#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u298299/* The maximum number of iterations for PBKDF2 on this implementation, in bits.300* This is a vendor-specific macro. This can be configured if necessary */301#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU302303/** The maximum size of a block cipher. */304#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u305306/** The size of the output of psa_mac_sign_finish(), in bytes.307*308* This is also the MAC size that psa_mac_verify_finish() expects.309*310* \warning This macro may evaluate its arguments multiple times or311* zero times, so you should not pass arguments that contain312* side effects.313*314* \param key_type The type of the MAC key.315* \param key_bits The size of the MAC key in bits.316* \param alg A MAC algorithm (\c PSA_ALG_XXX value such that317* #PSA_ALG_IS_MAC(\p alg) is true).318*319* \return The MAC size for the specified algorithm with320* the specified key parameters.321* \return 0 if the MAC algorithm is not recognized.322* \return Either 0 or the correct size for a MAC algorithm that323* the implementation recognizes, but does not support.324* \return Unspecified if the key parameters are not consistent325* with the algorithm.326*/327#define PSA_MAC_LENGTH(key_type, key_bits, alg) \328((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \329PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \330PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \331((void) (key_type), (void) (key_bits), 0u))332333/** The maximum size of the output of psa_aead_encrypt(), in bytes.334*335* If the size of the ciphertext buffer is at least this large, it is336* guaranteed that psa_aead_encrypt() will not fail due to an337* insufficient buffer size. Depending on the algorithm, the actual size of338* the ciphertext may be smaller.339*340* See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).341*342* \warning This macro may evaluate its arguments multiple times or343* zero times, so you should not pass arguments that contain344* side effects.345*346* \param key_type A symmetric key type that is347* compatible with algorithm \p alg.348* \param alg An AEAD algorithm349* (\c PSA_ALG_XXX value such that350* #PSA_ALG_IS_AEAD(\p alg) is true).351* \param plaintext_length Size of the plaintext in bytes.352*353* \return The AEAD ciphertext size for the specified354* algorithm.355* If the key type or AEAD algorithm is not356* recognized, or the parameters are incompatible,357* return 0.358*/359#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \360(PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \361(plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \3620u)363364/** A sufficient output buffer size for psa_aead_encrypt(), for any of the365* supported key types and AEAD algorithms.366*367* If the size of the ciphertext buffer is at least this large, it is guaranteed368* that psa_aead_encrypt() will not fail due to an insufficient buffer size.369*370* \note This macro returns a compile-time constant if its arguments are371* compile-time constants.372*373* See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,374* \p plaintext_length).375*376* \param plaintext_length Size of the plaintext in bytes.377*378* \return A sufficient output buffer size for any of the379* supported key types and AEAD algorithms.380*381*/382#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \383((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)384385386/** The maximum size of the output of psa_aead_decrypt(), in bytes.387*388* If the size of the plaintext buffer is at least this large, it is389* guaranteed that psa_aead_decrypt() will not fail due to an390* insufficient buffer size. Depending on the algorithm, the actual size of391* the plaintext may be smaller.392*393* See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).394*395* \warning This macro may evaluate its arguments multiple times or396* zero times, so you should not pass arguments that contain397* side effects.398*399* \param key_type A symmetric key type that is400* compatible with algorithm \p alg.401* \param alg An AEAD algorithm402* (\c PSA_ALG_XXX value such that403* #PSA_ALG_IS_AEAD(\p alg) is true).404* \param ciphertext_length Size of the plaintext in bytes.405*406* \return The AEAD ciphertext size for the specified407* algorithm.408* If the key type or AEAD algorithm is not409* recognized, or the parameters are incompatible,410* return 0.411*/412#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \413(PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \414(ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \415(ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \4160u)417418/** A sufficient output buffer size for psa_aead_decrypt(), for any of the419* supported key types and AEAD algorithms.420*421* If the size of the plaintext buffer is at least this large, it is guaranteed422* that psa_aead_decrypt() will not fail due to an insufficient buffer size.423*424* \note This macro returns a compile-time constant if its arguments are425* compile-time constants.426*427* See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,428* \p ciphertext_length).429*430* \param ciphertext_length Size of the ciphertext in bytes.431*432* \return A sufficient output buffer size for any of the433* supported key types and AEAD algorithms.434*435*/436#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \437(ciphertext_length)438439/** The default nonce size for an AEAD algorithm, in bytes.440*441* This macro can be used to allocate a buffer of sufficient size to442* store the nonce output from #psa_aead_generate_nonce().443*444* See also #PSA_AEAD_NONCE_MAX_SIZE.445*446* \note This is not the maximum size of nonce supported as input to447* #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),448* just the default size that is generated by #psa_aead_generate_nonce().449*450* \warning This macro may evaluate its arguments multiple times or451* zero times, so you should not pass arguments that contain452* side effects.453*454* \param key_type A symmetric key type that is compatible with455* algorithm \p alg.456*457* \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that458* #PSA_ALG_IS_AEAD(\p alg) is true).459*460* \return The default nonce size for the specified key type and algorithm.461* If the key type or AEAD algorithm is not recognized,462* or the parameters are incompatible, return 0.463*/464#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \465(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \466MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \467MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \4680u : \469(key_type) == PSA_KEY_TYPE_CHACHA20 && \470MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \4710u)472473/** The maximum default nonce size among all supported pairs of key types and474* AEAD algorithms, in bytes.475*476* This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()477* may return.478*479* \note This is not the maximum size of nonce supported as input to480* #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),481* just the largest size that may be generated by482* #psa_aead_generate_nonce().483*/484#define PSA_AEAD_NONCE_MAX_SIZE 13u485486/** A sufficient output buffer size for psa_aead_update().487*488* If the size of the output buffer is at least this large, it is489* guaranteed that psa_aead_update() will not fail due to an490* insufficient buffer size. The actual size of the output may be smaller491* in any given call.492*493* See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).494*495* \warning This macro may evaluate its arguments multiple times or496* zero times, so you should not pass arguments that contain497* side effects.498*499* \param key_type A symmetric key type that is500* compatible with algorithm \p alg.501* \param alg An AEAD algorithm502* (\c PSA_ALG_XXX value such that503* #PSA_ALG_IS_AEAD(\p alg) is true).504* \param input_length Size of the input in bytes.505*506* \return A sufficient output buffer size for the specified507* algorithm.508* If the key type or AEAD algorithm is not509* recognized, or the parameters are incompatible,510* return 0.511*/512/* For all the AEAD modes defined in this specification, it is possible513* to emit output without delay. However, hardware may not always be514* capable of this. So for modes based on a block cipher, allow the515* implementation to delay the output until it has a full block. */516#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \517(PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \518PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \519PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \520(input_length) : \5210u)522523/** A sufficient output buffer size for psa_aead_update(), for any of the524* supported key types and AEAD algorithms.525*526* If the size of the output buffer is at least this large, it is guaranteed527* that psa_aead_update() will not fail due to an insufficient buffer size.528*529* See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).530*531* \param input_length Size of the input in bytes.532*/533#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \534(PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))535536/** A sufficient ciphertext buffer size for psa_aead_finish().537*538* If the size of the ciphertext buffer is at least this large, it is539* guaranteed that psa_aead_finish() will not fail due to an540* insufficient ciphertext buffer size. The actual size of the output may541* be smaller in any given call.542*543* See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.544*545* \param key_type A symmetric key type that is546compatible with algorithm \p alg.547* \param alg An AEAD algorithm548* (\c PSA_ALG_XXX value such that549* #PSA_ALG_IS_AEAD(\p alg) is true).550*551* \return A sufficient ciphertext buffer size for the552* specified algorithm.553* If the key type or AEAD algorithm is not554* recognized, or the parameters are incompatible,555* return 0.556*/557#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \558(PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \559PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \560PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \5610u)562563/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the564* supported key types and AEAD algorithms.565*566* See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).567*/568#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)569570/** A sufficient plaintext buffer size for psa_aead_verify().571*572* If the size of the plaintext buffer is at least this large, it is573* guaranteed that psa_aead_verify() will not fail due to an574* insufficient plaintext buffer size. The actual size of the output may575* be smaller in any given call.576*577* See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.578*579* \param key_type A symmetric key type that is580* compatible with algorithm \p alg.581* \param alg An AEAD algorithm582* (\c PSA_ALG_XXX value such that583* #PSA_ALG_IS_AEAD(\p alg) is true).584*585* \return A sufficient plaintext buffer size for the586* specified algorithm.587* If the key type or AEAD algorithm is not588* recognized, or the parameters are incompatible,589* return 0.590*/591#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \592(PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \593PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \594PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \5950u)596597/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the598* supported key types and AEAD algorithms.599*600* See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).601*/602#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)603604#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \605(PSA_ALG_IS_RSA_OAEP(alg) ? \6062u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \60711u /*PKCS#1v1.5*/)608609/**610* \brief ECDSA signature size for a given curve bit size611*612* \param curve_bits Curve size in bits.613* \return Signature size in bytes.614*615* \note This macro returns a compile-time constant if its argument is one.616*/617#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \618(PSA_BITS_TO_BYTES(curve_bits) * 2u)619620/** Sufficient signature buffer size for psa_sign_hash().621*622* This macro returns a sufficient buffer size for a signature using a key623* of the specified type and size, with the specified algorithm.624* Note that the actual size of the signature may be smaller625* (some algorithms produce a variable-size signature).626*627* \warning This function may call its arguments multiple times or628* zero times, so you should not pass arguments that contain629* side effects.630*631* \param key_type An asymmetric key type (this may indifferently be a632* key pair type or a public key type).633* \param key_bits The size of the key in bits.634* \param alg The signature algorithm.635*636* \return If the parameters are valid and supported, return637* a buffer size in bytes that guarantees that638* psa_sign_hash() will not fail with639* #PSA_ERROR_BUFFER_TOO_SMALL.640* If the parameters are a valid combination that is not supported,641* return either a sensible size or 0.642* If the parameters are not valid, the643* return value is unspecified.644*/645#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \646(PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \647PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \648((void) alg, 0u))649650#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \651PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)652653/** \def PSA_SIGNATURE_MAX_SIZE654*655* Maximum size of an asymmetric signature.656*657* This macro expands to a compile-time constant integer. This value658* is the maximum size of a signature in bytes.659*/660#define PSA_SIGNATURE_MAX_SIZE 1661662#if (defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)) && \663(PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE > PSA_SIGNATURE_MAX_SIZE)664#undef PSA_SIGNATURE_MAX_SIZE665#define PSA_SIGNATURE_MAX_SIZE PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE666#endif667#if (defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) || defined(PSA_WANT_ALG_RSA_PSS)) && \668(PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_SIGNATURE_MAX_SIZE)669#undef PSA_SIGNATURE_MAX_SIZE670#define PSA_SIGNATURE_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)671#endif672673/** Sufficient output buffer size for psa_asymmetric_encrypt().674*675* This macro returns a sufficient buffer size for a ciphertext produced using676* a key of the specified type and size, with the specified algorithm.677* Note that the actual size of the ciphertext may be smaller, depending678* on the algorithm.679*680* \warning This function may call its arguments multiple times or681* zero times, so you should not pass arguments that contain682* side effects.683*684* \param key_type An asymmetric key type (this may indifferently be a685* key pair type or a public key type).686* \param key_bits The size of the key in bits.687* \param alg The asymmetric encryption algorithm.688*689* \return If the parameters are valid and supported, return690* a buffer size in bytes that guarantees that691* psa_asymmetric_encrypt() will not fail with692* #PSA_ERROR_BUFFER_TOO_SMALL.693* If the parameters are a valid combination that is not supported,694* return either a sensible size or 0.695* If the parameters are not valid, the696* return value is unspecified.697*/698#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \699(PSA_KEY_TYPE_IS_RSA(key_type) ? \700((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \7010u)702703/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any704* supported asymmetric encryption.705*706* See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).707*/708/* This macro assumes that RSA is the only supported asymmetric encryption. */709#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \710(PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))711712/** Sufficient output buffer size for psa_asymmetric_decrypt().713*714* This macro returns a sufficient buffer size for a plaintext produced using715* a key of the specified type and size, with the specified algorithm.716* Note that the actual size of the plaintext may be smaller, depending717* on the algorithm.718*719* \warning This function may call its arguments multiple times or720* zero times, so you should not pass arguments that contain721* side effects.722*723* \param key_type An asymmetric key type (this may indifferently be a724* key pair type or a public key type).725* \param key_bits The size of the key in bits.726* \param alg The asymmetric encryption algorithm.727*728* \return If the parameters are valid and supported, return729* a buffer size in bytes that guarantees that730* psa_asymmetric_decrypt() will not fail with731* #PSA_ERROR_BUFFER_TOO_SMALL.732* If the parameters are a valid combination that is not supported,733* return either a sensible size or 0.734* If the parameters are not valid, the735* return value is unspecified.736*/737#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \738(PSA_KEY_TYPE_IS_RSA(key_type) ? \739PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \7400u)741742/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any743* supported asymmetric decryption.744*745* This macro assumes that RSA is the only supported asymmetric encryption.746*747* See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).748*/749#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \750(PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))751752/* Maximum size of the ASN.1 encoding of an INTEGER with the specified753* number of bits.754*755* This definition assumes that bits <= 2^19 - 9 so that the length field756* is at most 3 bytes. The length of the encoding is the length of the757* bit string padded to a whole number of bytes plus:758* - 1 type byte;759* - 1 to 3 length bytes;760* - 0 to 1 bytes of leading 0 due to the sign bit.761*/762#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \763((bits) / 8u + 5u)764765/* Maximum size of the export encoding of an RSA public key.766* Assumes that the public exponent is less than 2^32.767*768* RSAPublicKey ::= SEQUENCE {769* modulus INTEGER, -- n770* publicExponent INTEGER } -- e771*772* - 4 bytes of SEQUENCE overhead;773* - n : INTEGER;774* - 7 bytes for the public exponent.775*/776#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \777(PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u)778779/* Maximum size of the export encoding of an RSA key pair.780* Assumes that the public exponent is less than 2^32 and that the size781* difference between the two primes is at most 1 bit.782*783* RSAPrivateKey ::= SEQUENCE {784* version Version, -- 0785* modulus INTEGER, -- N-bit786* publicExponent INTEGER, -- 32-bit787* privateExponent INTEGER, -- N-bit788* prime1 INTEGER, -- N/2-bit789* prime2 INTEGER, -- N/2-bit790* exponent1 INTEGER, -- N/2-bit791* exponent2 INTEGER, -- N/2-bit792* coefficient INTEGER, -- N/2-bit793* }794*795* - 4 bytes of SEQUENCE overhead;796* - 3 bytes of version;797* - 7 half-size INTEGERs plus 2 full-size INTEGERs,798* overapproximated as 9 half-size INTEGERS;799* - 7 bytes for the public exponent.800*/801#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \802(9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u)803804/* Maximum size of the export encoding of a DSA public key.805*806* SubjectPublicKeyInfo ::= SEQUENCE {807* algorithm AlgorithmIdentifier,808* subjectPublicKey BIT STRING } -- contains DSAPublicKey809* AlgorithmIdentifier ::= SEQUENCE {810* algorithm OBJECT IDENTIFIER,811* parameters Dss-Params } -- SEQUENCE of 3 INTEGERs812* DSAPublicKey ::= INTEGER -- public key, Y813*814* - 3 * 4 bytes of SEQUENCE overhead;815* - 1 + 1 + 7 bytes of algorithm (DSA OID);816* - 4 bytes of BIT STRING overhead;817* - 3 full-size INTEGERs (p, g, y);818* - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).819*/820#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \821(PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u)822823/* Maximum size of the export encoding of a DSA key pair.824*825* DSAPrivateKey ::= SEQUENCE {826* version Version, -- 0827* prime INTEGER, -- p828* subprime INTEGER, -- q829* generator INTEGER, -- g830* public INTEGER, -- y831* private INTEGER, -- x832* }833*834* - 4 bytes of SEQUENCE overhead;835* - 3 bytes of version;836* - 3 full-size INTEGERs (p, g, y);837* - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).838*/839#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \840(PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u)841842/* Maximum size of the export encoding of an ECC public key.843*844* The representation of an ECC public key is:845* - The byte 0x04;846* - `x_P` as a `ceiling(m/8)`-byte string, big-endian;847* - `y_P` as a `ceiling(m/8)`-byte string, big-endian;848* - where m is the bit size associated with the curve.849*850* - 1 byte + 2 * point size.851*/852#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \853(2u * PSA_BITS_TO_BYTES(key_bits) + 1u)854855/* Maximum size of the export encoding of an ECC key pair.856*857* An ECC key pair is represented by the secret value.858*/859#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \860(PSA_BITS_TO_BYTES(key_bits))861862/* Maximum size of the export encoding of an DH key pair.863*864* An DH key pair is represented by the secret value.865*/866#define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \867(PSA_BITS_TO_BYTES(key_bits))868869/* Maximum size of the export encoding of an DH public key.870*/871#define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \872(PSA_BITS_TO_BYTES(key_bits))873874/** Sufficient output buffer size for psa_export_key() or875* psa_export_public_key().876*877* This macro returns a compile-time constant if its arguments are878* compile-time constants.879*880* \warning This macro may evaluate its arguments multiple times or881* zero times, so you should not pass arguments that contain882* side effects.883*884* The following code illustrates how to allocate enough memory to export885* a key by querying the key type and size at runtime.886* \code{c}887* psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;888* psa_status_t status;889* status = psa_get_key_attributes(key, &attributes);890* if (status != PSA_SUCCESS) handle_error(...);891* psa_key_type_t key_type = psa_get_key_type(&attributes);892* size_t key_bits = psa_get_key_bits(&attributes);893* size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);894* psa_reset_key_attributes(&attributes);895* uint8_t *buffer = malloc(buffer_size);896* if (buffer == NULL) handle_error(...);897* size_t buffer_length;898* status = psa_export_key(key, buffer, buffer_size, &buffer_length);899* if (status != PSA_SUCCESS) handle_error(...);900* \endcode901*902* \param key_type A supported key type.903* \param key_bits The size of the key in bits.904*905* \return If the parameters are valid and supported, return906* a buffer size in bytes that guarantees that907* psa_export_key() or psa_export_public_key() will not fail with908* #PSA_ERROR_BUFFER_TOO_SMALL.909* If the parameters are a valid combination that is not supported,910* return either a sensible size or 0.911* If the parameters are not valid, the return value is unspecified.912*/913#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \914(PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \915PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \916(key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \917(key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \918(key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \919(key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \920PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \921PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \9220u)923924/** Sufficient output buffer size for psa_export_public_key().925*926* This macro returns a compile-time constant if its arguments are927* compile-time constants.928*929* \warning This macro may evaluate its arguments multiple times or930* zero times, so you should not pass arguments that contain931* side effects.932*933* The following code illustrates how to allocate enough memory to export934* a public key by querying the key type and size at runtime.935* \code{c}936* psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;937* psa_status_t status;938* status = psa_get_key_attributes(key, &attributes);939* if (status != PSA_SUCCESS) handle_error(...);940* psa_key_type_t key_type = psa_get_key_type(&attributes);941* size_t key_bits = psa_get_key_bits(&attributes);942* size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);943* psa_reset_key_attributes(&attributes);944* uint8_t *buffer = malloc(buffer_size);945* if (buffer == NULL) handle_error(...);946* size_t buffer_length;947* status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);948* if (status != PSA_SUCCESS) handle_error(...);949* \endcode950*951* \param key_type A public key or key pair key type.952* \param key_bits The size of the key in bits.953*954* \return If the parameters are valid and supported, return955* a buffer size in bytes that guarantees that956* psa_export_public_key() will not fail with957* #PSA_ERROR_BUFFER_TOO_SMALL.958* If the parameters are a valid combination that is not959* supported, return either a sensible size or 0.960* If the parameters are not valid,961* the return value is unspecified.962*963* If the parameters are valid and supported,964* return the same result as965* #PSA_EXPORT_KEY_OUTPUT_SIZE(966* \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),967* \p key_bits).968*/969#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \970(PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \971PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \972PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \9730u)974975/** Sufficient buffer size for exporting any asymmetric key pair.976*977* This macro expands to a compile-time constant integer. This value is978* a sufficient buffer size when calling psa_export_key() to export any979* asymmetric key pair, regardless of the exact key type and key size.980*981* See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).982*/983#define PSA_EXPORT_KEY_PAIR_MAX_SIZE 1984985#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) && \986(PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \987PSA_EXPORT_KEY_PAIR_MAX_SIZE)988#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE989#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \990PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)991#endif992#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) && \993(PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \994PSA_EXPORT_KEY_PAIR_MAX_SIZE)995#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE996#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \997PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)998#endif999#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_BASIC) && \1000(PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \1001PSA_EXPORT_KEY_PAIR_MAX_SIZE)1002#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE1003#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \1004PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)1005#endif10061007/** Sufficient buffer size for exporting any asymmetric public key.1008*1009* This macro expands to a compile-time constant integer. This value is1010* a sufficient buffer size when calling psa_export_key() or1011* psa_export_public_key() to export any asymmetric public key,1012* regardless of the exact key type and key size.1013*1014* See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).1015*/1016#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 110171018#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \1019(PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \1020PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)1021#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE1022#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \1023PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)1024#endif1025#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) && \1026(PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \1027PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)1028#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE1029#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \1030PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)1031#endif1032#if defined(PSA_WANT_KEY_TYPE_DH_PUBLIC_KEY) && \1033(PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \1034PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)1035#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE1036#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \1037PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)1038#endif10391040#define PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE \1041((PSA_EXPORT_KEY_PAIR_MAX_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) ? \1042PSA_EXPORT_KEY_PAIR_MAX_SIZE : PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)10431044/** Sufficient output buffer size for psa_raw_key_agreement().1045*1046* This macro returns a compile-time constant if its arguments are1047* compile-time constants.1048*1049* \warning This macro may evaluate its arguments multiple times or1050* zero times, so you should not pass arguments that contain1051* side effects.1052*1053* See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.1054*1055* \param key_type A supported key type.1056* \param key_bits The size of the key in bits.1057*1058* \return If the parameters are valid and supported, return1059* a buffer size in bytes that guarantees that1060* psa_raw_key_agreement() will not fail with1061* #PSA_ERROR_BUFFER_TOO_SMALL.1062* If the parameters are a valid combination that1063* is not supported, return either a sensible size or 0.1064* If the parameters are not valid,1065* the return value is unspecified.1066*/1067#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \1068((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \1069PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u)10701071/** Maximum size of the output from psa_raw_key_agreement().1072*1073* This macro expands to a compile-time constant integer. This value is the1074* maximum size of the output any raw key agreement algorithm, in bytes.1075*1076* See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).1077*/1078#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 110791080#if defined(PSA_WANT_ALG_ECDH) && \1081(PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE)1082#undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE1083#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)1084#endif1085#if defined(PSA_WANT_ALG_FFDH) && \1086(PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE)1087#undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE1088#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS)1089#endif10901091/** Maximum key length for ciphers.1092*1093* Since there is no additional PSA_WANT_xxx symbol to specifiy the size of1094* the key once a cipher is enabled (as it happens for asymmetric keys for1095* example), the maximum key length is taken into account for each cipher.1096* The resulting value will be the maximum cipher's key length given depending1097* on which ciphers are enabled.1098*1099* Note: max value for AES used below would be doubled if XTS were enabled, but1100* this mode is currently not supported in Mbed TLS implementation of PSA1101* APIs.1102*/1103#if (defined(PSA_WANT_KEY_TYPE_AES) || defined(PSA_WANT_KEY_TYPE_ARIA) || \1104defined(PSA_WANT_KEY_TYPE_CAMELLIA) || defined(PSA_WANT_KEY_TYPE_CHACHA20))1105#define PSA_CIPHER_MAX_KEY_LENGTH 32u1106#elif defined(PSA_WANT_KEY_TYPE_DES)1107#define PSA_CIPHER_MAX_KEY_LENGTH 24u1108#else1109#define PSA_CIPHER_MAX_KEY_LENGTH 0u1110#endif11111112/** The default IV size for a cipher algorithm, in bytes.1113*1114* The IV that is generated as part of a call to #psa_cipher_encrypt() is always1115* the default IV length for the algorithm.1116*1117* This macro can be used to allocate a buffer of sufficient size to1118* store the IV output from #psa_cipher_generate_iv() when using1119* a multi-part cipher operation.1120*1121* See also #PSA_CIPHER_IV_MAX_SIZE.1122*1123* \warning This macro may evaluate its arguments multiple times or1124* zero times, so you should not pass arguments that contain1125* side effects.1126*1127* \param key_type A symmetric key type that is compatible with algorithm \p alg.1128*1129* \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).1130*1131* \return The default IV size for the specified key type and algorithm.1132* If the algorithm does not use an IV, return 0.1133* If the key type or cipher algorithm is not recognized,1134* or the parameters are incompatible, return 0.1135*/1136#define PSA_CIPHER_IV_LENGTH(key_type, alg) \1137(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \1138((alg) == PSA_ALG_CTR || \1139(alg) == PSA_ALG_CFB || \1140(alg) == PSA_ALG_OFB || \1141(alg) == PSA_ALG_XTS || \1142(alg) == PSA_ALG_CBC_NO_PADDING || \1143(alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \1144(key_type) == PSA_KEY_TYPE_CHACHA20 && \1145(alg) == PSA_ALG_STREAM_CIPHER ? 12u : \1146(alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \11470u)11481149/** The maximum IV size for all supported cipher algorithms, in bytes.1150*1151* See also #PSA_CIPHER_IV_LENGTH().1152*/1153#define PSA_CIPHER_IV_MAX_SIZE 16u11541155/** The maximum size of the output of psa_cipher_encrypt(), in bytes.1156*1157* If the size of the output buffer is at least this large, it is guaranteed1158* that psa_cipher_encrypt() will not fail due to an insufficient buffer size.1159* Depending on the algorithm, the actual size of the output might be smaller.1160*1161* See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).1162*1163* \warning This macro may evaluate its arguments multiple times or1164* zero times, so you should not pass arguments that contain1165* side effects.1166*1167* \param key_type A symmetric key type that is compatible with algorithm1168* alg.1169* \param alg A cipher algorithm (\c PSA_ALG_XXX value such that1170* #PSA_ALG_IS_CIPHER(\p alg) is true).1171* \param input_length Size of the input in bytes.1172*1173* \return A sufficient output size for the specified key type and1174* algorithm. If the key type or cipher algorithm is not1175* recognized, or the parameters are incompatible,1176* return 0.1177*/1178#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \1179(alg == PSA_ALG_CBC_PKCS7 ? \1180(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \1181PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \1182(input_length) + 1u) + \1183PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \1184(PSA_ALG_IS_CIPHER(alg) ? \1185(input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \11860u))11871188/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the1189* supported key types and cipher algorithms.1190*1191* If the size of the output buffer is at least this large, it is guaranteed1192* that psa_cipher_encrypt() will not fail due to an insufficient buffer size.1193*1194* See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).1195*1196* \param input_length Size of the input in bytes.1197*1198*/1199#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \1200(PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \1201(input_length) + 1u) + \1202PSA_CIPHER_IV_MAX_SIZE)12031204/** The maximum size of the output of psa_cipher_decrypt(), in bytes.1205*1206* If the size of the output buffer is at least this large, it is guaranteed1207* that psa_cipher_decrypt() will not fail due to an insufficient buffer size.1208* Depending on the algorithm, the actual size of the output might be smaller.1209*1210* See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).1211*1212* \param key_type A symmetric key type that is compatible with algorithm1213* alg.1214* \param alg A cipher algorithm (\c PSA_ALG_XXX value such that1215* #PSA_ALG_IS_CIPHER(\p alg) is true).1216* \param input_length Size of the input in bytes.1217*1218* \return A sufficient output size for the specified key type and1219* algorithm. If the key type or cipher algorithm is not1220* recognized, or the parameters are incompatible,1221* return 0.1222*/1223#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \1224(PSA_ALG_IS_CIPHER(alg) && \1225((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \1226(input_length) : \12270u)12281229/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the1230* supported key types and cipher algorithms.1231*1232* If the size of the output buffer is at least this large, it is guaranteed1233* that psa_cipher_decrypt() will not fail due to an insufficient buffer size.1234*1235* See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).1236*1237* \param input_length Size of the input in bytes.1238*/1239#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \1240(input_length)12411242/** A sufficient output buffer size for psa_cipher_update().1243*1244* If the size of the output buffer is at least this large, it is guaranteed1245* that psa_cipher_update() will not fail due to an insufficient buffer size.1246* The actual size of the output might be smaller in any given call.1247*1248* See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).1249*1250* \param key_type A symmetric key type that is compatible with algorithm1251* alg.1252* \param alg A cipher algorithm (PSA_ALG_XXX value such that1253* #PSA_ALG_IS_CIPHER(\p alg) is true).1254* \param input_length Size of the input in bytes.1255*1256* \return A sufficient output size for the specified key type and1257* algorithm. If the key type or cipher algorithm is not1258* recognized, or the parameters are incompatible, return 0.1259*/1260#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \1261(PSA_ALG_IS_CIPHER(alg) ? \1262(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \1263(((alg) == PSA_ALG_CBC_PKCS7 || \1264(alg) == PSA_ALG_CBC_NO_PADDING || \1265(alg) == PSA_ALG_ECB_NO_PADDING) ? \1266PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \1267input_length) : \1268(input_length)) : 0u) : \12690u)12701271/** A sufficient output buffer size for psa_cipher_update(), for any of the1272* supported key types and cipher algorithms.1273*1274* If the size of the output buffer is at least this large, it is guaranteed1275* that psa_cipher_update() will not fail due to an insufficient buffer size.1276*1277* See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).1278*1279* \param input_length Size of the input in bytes.1280*/1281#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \1282(PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))12831284/** A sufficient ciphertext buffer size for psa_cipher_finish().1285*1286* If the size of the ciphertext buffer is at least this large, it is1287* guaranteed that psa_cipher_finish() will not fail due to an insufficient1288* ciphertext buffer size. The actual size of the output might be smaller in1289* any given call.1290*1291* See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().1292*1293* \param key_type A symmetric key type that is compatible with algorithm1294* alg.1295* \param alg A cipher algorithm (PSA_ALG_XXX value such that1296* #PSA_ALG_IS_CIPHER(\p alg) is true).1297* \return A sufficient output size for the specified key type and1298* algorithm. If the key type or cipher algorithm is not1299* recognized, or the parameters are incompatible, return 0.1300*/1301#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \1302(PSA_ALG_IS_CIPHER(alg) ? \1303(alg == PSA_ALG_CBC_PKCS7 ? \1304PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \13050u) : \13060u)13071308/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the1309* supported key types and cipher algorithms.1310*1311* See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).1312*/1313#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \1314(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)13151316#endif /* PSA_CRYPTO_SIZES_H */131713181319