![]() |
|
|||
File indexing completed on 2025-08-27 09:43:23
0001 /** 0002 * \file psa/crypto_sizes.h 0003 * 0004 * \brief PSA cryptography module: Mbed TLS buffer size macros 0005 * 0006 * \note This file may not be included directly. Applications must 0007 * include psa/crypto.h. 0008 * 0009 * This file contains the definitions of macros that are useful to 0010 * compute buffer sizes. The signatures and semantics of these macros 0011 * are standardized, but the definitions are not, because they depend on 0012 * the available algorithms and, in some cases, on permitted tolerances 0013 * on buffer sizes. 0014 * 0015 * In implementations with isolation between the application and the 0016 * cryptography module, implementers should take care to ensure that 0017 * the definitions that are exposed to applications match what the 0018 * module implements. 0019 * 0020 * Macros that compute sizes whose values do not depend on the 0021 * implementation are in crypto.h. 0022 */ 0023 /* 0024 * Copyright The Mbed TLS Contributors 0025 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0026 */ 0027 0028 #ifndef PSA_CRYPTO_SIZES_H 0029 #define PSA_CRYPTO_SIZES_H 0030 0031 /* 0032 * Include the build-time configuration information header. Here, we do not 0033 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which 0034 * is basically just an alias to it. This is to ease the maintenance of the 0035 * TF-PSA-Crypto repository which has a different build system and 0036 * configuration. 0037 */ 0038 #include "psa/build_info.h" 0039 0040 #define PSA_BITS_TO_BYTES(bits) (((bits) + 7u) / 8u) 0041 #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8u) 0042 #define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \ 0043 (c) : (b) : (a) <= (c) ? (c) : (a)) 0044 0045 #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \ 0046 (((length) + (block_size) - 1) / (block_size) * (block_size)) 0047 0048 /** The size of the output of psa_hash_finish(), in bytes. 0049 * 0050 * This is also the hash size that psa_hash_verify() expects. 0051 * 0052 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that 0053 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm 0054 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a 0055 * hash algorithm). 0056 * 0057 * \return The hash size for the specified hash algorithm. 0058 * If the hash algorithm is not recognized, return 0. 0059 */ 0060 #define PSA_HASH_LENGTH(alg) \ 0061 ( \ 0062 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16u : \ 0063 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20u : \ 0064 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20u : \ 0065 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28u : \ 0066 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32u : \ 0067 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48u : \ 0068 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64u : \ 0069 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28u : \ 0070 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32u : \ 0071 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28u : \ 0072 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32u : \ 0073 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48u : \ 0074 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64u : \ 0075 0u) 0076 0077 /** The input block size of a hash algorithm, in bytes. 0078 * 0079 * Hash algorithms process their input data in blocks. Hash operations will 0080 * retain any partial blocks until they have enough input to fill the block or 0081 * until the operation is finished. 0082 * This affects the output from psa_hash_suspend(). 0083 * 0084 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that 0085 * PSA_ALG_IS_HASH(\p alg) is true). 0086 * 0087 * \return The block size in bytes for the specified hash algorithm. 0088 * If the hash algorithm is not recognized, return 0. 0089 * An implementation can return either 0 or the correct size for a 0090 * hash algorithm that it recognizes, but does not support. 0091 */ 0092 #define PSA_HASH_BLOCK_LENGTH(alg) \ 0093 ( \ 0094 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64u : \ 0095 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64u : \ 0096 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64u : \ 0097 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64u : \ 0098 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64u : \ 0099 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128u : \ 0100 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128u : \ 0101 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128u : \ 0102 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128u : \ 0103 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144u : \ 0104 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136u : \ 0105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104u : \ 0106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72u : \ 0107 0u) 0108 0109 /** \def PSA_HASH_MAX_SIZE 0110 * 0111 * Maximum size of a hash. 0112 * 0113 * This macro expands to a compile-time constant integer. This value 0114 * is the maximum size of a hash in bytes. 0115 */ 0116 /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224, 0117 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for 0118 * HMAC-SHA3-512. */ 0119 /* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE, 0120 * see the note on MBEDTLS_MD_MAX_SIZE for details. */ 0121 #if defined(PSA_WANT_ALG_SHA3_224) 0122 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u 0123 #elif defined(PSA_WANT_ALG_SHA3_256) 0124 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 136u 0125 #elif defined(PSA_WANT_ALG_SHA_512) 0126 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u 0127 #elif defined(PSA_WANT_ALG_SHA_384) 0128 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u 0129 #elif defined(PSA_WANT_ALG_SHA3_384) 0130 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 104u 0131 #elif defined(PSA_WANT_ALG_SHA3_512) 0132 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 72u 0133 #elif defined(PSA_WANT_ALG_SHA_256) 0134 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 0135 #elif defined(PSA_WANT_ALG_SHA_224) 0136 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 0137 #else /* SHA-1 or smaller */ 0138 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 0139 #endif 0140 0141 #if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512) 0142 #define PSA_HASH_MAX_SIZE 64u 0143 #elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384) 0144 #define PSA_HASH_MAX_SIZE 48u 0145 #elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256) 0146 #define PSA_HASH_MAX_SIZE 32u 0147 #elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224) 0148 #define PSA_HASH_MAX_SIZE 28u 0149 #else /* SHA-1 or smaller */ 0150 #define PSA_HASH_MAX_SIZE 20u 0151 #endif 0152 0153 /** \def PSA_MAC_MAX_SIZE 0154 * 0155 * Maximum size of a MAC. 0156 * 0157 * This macro expands to a compile-time constant integer. This value 0158 * is the maximum size of a MAC in bytes. 0159 */ 0160 /* All non-HMAC MACs have a maximum size that's smaller than the 0161 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */ 0162 /* Note that the encoding of truncated MAC algorithms limits this value 0163 * to 64 bytes. 0164 */ 0165 #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE 0166 0167 /** The length of a tag for an AEAD algorithm, in bytes. 0168 * 0169 * This macro can be used to allocate a buffer of sufficient size to store the 0170 * tag output from psa_aead_finish(). 0171 * 0172 * See also #PSA_AEAD_TAG_MAX_SIZE. 0173 * 0174 * \param key_type The type of the AEAD key. 0175 * \param key_bits The size of the AEAD key in bits. 0176 * \param alg An AEAD algorithm 0177 * (\c PSA_ALG_XXX value such that 0178 * #PSA_ALG_IS_AEAD(\p alg) is true). 0179 * 0180 * \return The tag length for the specified algorithm and key. 0181 * If the AEAD algorithm does not have an identified 0182 * tag that can be distinguished from the rest of 0183 * the ciphertext, return 0. 0184 * If the key type or AEAD algorithm is not 0185 * recognized, or the parameters are incompatible, 0186 * return 0. 0187 */ 0188 #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \ 0189 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 0190 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 0191 ((void) (key_bits), 0u)) 0192 0193 /** The maximum tag size for all supported AEAD algorithms, in bytes. 0194 * 0195 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg). 0196 */ 0197 #define PSA_AEAD_TAG_MAX_SIZE 16u 0198 0199 /* The maximum size of an RSA key on this implementation, in bits. 0200 * This is a vendor-specific macro. 0201 * 0202 * Mbed TLS does not set a hard limit on the size of RSA keys: any key 0203 * whose parameters fit in a bignum is accepted. However large keys can 0204 * induce a large memory usage and long computation times. Unlike other 0205 * auxiliary macros in this file and in crypto.h, which reflect how the 0206 * library is configured, this macro defines how the library is 0207 * configured. This implementation refuses to import or generate an 0208 * RSA key whose size is larger than the value defined here. 0209 * 0210 * Note that an implementation may set different size limits for different 0211 * operations, and does not need to accept all key sizes up to the limit. */ 0212 #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u 0213 0214 /* The minimum size of an RSA key on this implementation, in bits. 0215 * This is a vendor-specific macro. 0216 * 0217 * Limits RSA key generation to a minimum due to avoid accidental misuse. 0218 * This value cannot be less than 128 bits. 0219 */ 0220 #if defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS) 0221 #define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS MBEDTLS_RSA_GEN_KEY_MIN_BITS 0222 #else 0223 #define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS 1024 0224 #endif 0225 0226 /* The maximum size of an DH key on this implementation, in bits. 0227 * This is a vendor-specific macro.*/ 0228 #if defined(PSA_WANT_DH_RFC7919_8192) 0229 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u 0230 #elif defined(PSA_WANT_DH_RFC7919_6144) 0231 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 6144u 0232 #elif defined(PSA_WANT_DH_RFC7919_4096) 0233 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 4096u 0234 #elif defined(PSA_WANT_DH_RFC7919_3072) 0235 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 3072u 0236 #elif defined(PSA_WANT_DH_RFC7919_2048) 0237 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 2048u 0238 #else 0239 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 0u 0240 #endif 0241 0242 /* The maximum size of an ECC key on this implementation, in bits. 0243 * This is a vendor-specific macro. */ 0244 #if defined(PSA_WANT_ECC_SECP_R1_521) 0245 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u 0246 #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) 0247 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u 0248 #elif defined(PSA_WANT_ECC_MONTGOMERY_448) 0249 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u 0250 #elif defined(PSA_WANT_ECC_SECP_R1_384) 0251 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u 0252 #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) 0253 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u 0254 #elif defined(PSA_WANT_ECC_SECP_R1_256) 0255 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 0256 #elif defined(PSA_WANT_ECC_SECP_K1_256) 0257 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 0258 #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) 0259 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 0260 #elif defined(PSA_WANT_ECC_MONTGOMERY_255) 0261 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u 0262 #elif defined(PSA_WANT_ECC_SECP_R1_224) 0263 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u 0264 #elif defined(PSA_WANT_ECC_SECP_K1_224) 0265 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u 0266 #elif defined(PSA_WANT_ECC_SECP_R1_192) 0267 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u 0268 #elif defined(PSA_WANT_ECC_SECP_K1_192) 0269 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u 0270 #else 0271 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u 0272 #endif 0273 0274 /** This macro returns the maximum supported length of the PSK for the 0275 * TLS-1.2 PSK-to-MS key derivation 0276 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)). 0277 * 0278 * The maximum supported length does not depend on the chosen hash algorithm. 0279 * 0280 * Quoting RFC 4279, Sect 5.3: 0281 * TLS implementations supporting these ciphersuites MUST support 0282 * arbitrary PSK identities up to 128 octets in length, and arbitrary 0283 * PSKs up to 64 octets in length. Supporting longer identities and 0284 * keys is RECOMMENDED. 0285 * 0286 * Therefore, no implementation should define a value smaller than 64 0287 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE. 0288 */ 0289 #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u 0290 0291 /* The expected size of input passed to psa_tls12_ecjpake_to_pms_input, 0292 * which is expected to work with P-256 curve only. */ 0293 #define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u 0294 0295 /* The size of a serialized K.X coordinate to be used in 0296 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256 0297 * curve. */ 0298 #define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u 0299 0300 /* The maximum number of iterations for PBKDF2 on this implementation, in bits. 0301 * This is a vendor-specific macro. This can be configured if necessary */ 0302 #define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU 0303 0304 /** The maximum size of a block cipher. */ 0305 #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u 0306 0307 /** The size of the output of psa_mac_sign_finish(), in bytes. 0308 * 0309 * This is also the MAC size that psa_mac_verify_finish() expects. 0310 * 0311 * \warning This macro may evaluate its arguments multiple times or 0312 * zero times, so you should not pass arguments that contain 0313 * side effects. 0314 * 0315 * \param key_type The type of the MAC key. 0316 * \param key_bits The size of the MAC key in bits. 0317 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that 0318 * #PSA_ALG_IS_MAC(\p alg) is true). 0319 * 0320 * \return The MAC size for the specified algorithm with 0321 * the specified key parameters. 0322 * \return 0 if the MAC algorithm is not recognized. 0323 * \return Either 0 or the correct size for a MAC algorithm that 0324 * the implementation recognizes, but does not support. 0325 * \return Unspecified if the key parameters are not consistent 0326 * with the algorithm. 0327 */ 0328 #define PSA_MAC_LENGTH(key_type, key_bits, alg) \ 0329 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \ 0330 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \ 0331 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 0332 ((void) (key_type), (void) (key_bits), 0u)) 0333 0334 /** The maximum size of the output of psa_aead_encrypt(), in bytes. 0335 * 0336 * If the size of the ciphertext buffer is at least this large, it is 0337 * guaranteed that psa_aead_encrypt() will not fail due to an 0338 * insufficient buffer size. Depending on the algorithm, the actual size of 0339 * the ciphertext may be smaller. 0340 * 0341 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length). 0342 * 0343 * \warning This macro may evaluate its arguments multiple times or 0344 * zero times, so you should not pass arguments that contain 0345 * side effects. 0346 * 0347 * \param key_type A symmetric key type that is 0348 * compatible with algorithm \p alg. 0349 * \param alg An AEAD algorithm 0350 * (\c PSA_ALG_XXX value such that 0351 * #PSA_ALG_IS_AEAD(\p alg) is true). 0352 * \param plaintext_length Size of the plaintext in bytes. 0353 * 0354 * \return The AEAD ciphertext size for the specified 0355 * algorithm. 0356 * If the key type or AEAD algorithm is not 0357 * recognized, or the parameters are incompatible, 0358 * return 0. 0359 */ 0360 #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \ 0361 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 0362 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 0363 0u) 0364 0365 /** A sufficient output buffer size for psa_aead_encrypt(), for any of the 0366 * supported key types and AEAD algorithms. 0367 * 0368 * If the size of the ciphertext buffer is at least this large, it is guaranteed 0369 * that psa_aead_encrypt() will not fail due to an insufficient buffer size. 0370 * 0371 * \note This macro returns a compile-time constant if its arguments are 0372 * compile-time constants. 0373 * 0374 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, 0375 * \p plaintext_length). 0376 * 0377 * \param plaintext_length Size of the plaintext in bytes. 0378 * 0379 * \return A sufficient output buffer size for any of the 0380 * supported key types and AEAD algorithms. 0381 * 0382 */ 0383 #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \ 0384 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE) 0385 0386 0387 /** The maximum size of the output of psa_aead_decrypt(), in bytes. 0388 * 0389 * If the size of the plaintext buffer is at least this large, it is 0390 * guaranteed that psa_aead_decrypt() will not fail due to an 0391 * insufficient buffer size. Depending on the algorithm, the actual size of 0392 * the plaintext may be smaller. 0393 * 0394 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length). 0395 * 0396 * \warning This macro may evaluate its arguments multiple times or 0397 * zero times, so you should not pass arguments that contain 0398 * side effects. 0399 * 0400 * \param key_type A symmetric key type that is 0401 * compatible with algorithm \p alg. 0402 * \param alg An AEAD algorithm 0403 * (\c PSA_ALG_XXX value such that 0404 * #PSA_ALG_IS_AEAD(\p alg) is true). 0405 * \param ciphertext_length Size of the plaintext in bytes. 0406 * 0407 * \return The AEAD ciphertext size for the specified 0408 * algorithm. 0409 * If the key type or AEAD algorithm is not 0410 * recognized, or the parameters are incompatible, 0411 * return 0. 0412 */ 0413 #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \ 0414 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 0415 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \ 0416 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 0417 0u) 0418 0419 /** A sufficient output buffer size for psa_aead_decrypt(), for any of the 0420 * supported key types and AEAD algorithms. 0421 * 0422 * If the size of the plaintext buffer is at least this large, it is guaranteed 0423 * that psa_aead_decrypt() will not fail due to an insufficient buffer size. 0424 * 0425 * \note This macro returns a compile-time constant if its arguments are 0426 * compile-time constants. 0427 * 0428 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, 0429 * \p ciphertext_length). 0430 * 0431 * \param ciphertext_length Size of the ciphertext in bytes. 0432 * 0433 * \return A sufficient output buffer size for any of the 0434 * supported key types and AEAD algorithms. 0435 * 0436 */ 0437 #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \ 0438 (ciphertext_length) 0439 0440 /** The default nonce size for an AEAD algorithm, in bytes. 0441 * 0442 * This macro can be used to allocate a buffer of sufficient size to 0443 * store the nonce output from #psa_aead_generate_nonce(). 0444 * 0445 * See also #PSA_AEAD_NONCE_MAX_SIZE. 0446 * 0447 * \note This is not the maximum size of nonce supported as input to 0448 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), 0449 * just the default size that is generated by #psa_aead_generate_nonce(). 0450 * 0451 * \warning This macro may evaluate its arguments multiple times or 0452 * zero times, so you should not pass arguments that contain 0453 * side effects. 0454 * 0455 * \param key_type A symmetric key type that is compatible with 0456 * algorithm \p alg. 0457 * 0458 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that 0459 * #PSA_ALG_IS_AEAD(\p alg) is true). 0460 * 0461 * \return The default nonce size for the specified key type and algorithm. 0462 * If the key type or AEAD algorithm is not recognized, 0463 * or the parameters are incompatible, return 0. 0464 */ 0465 #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ 0466 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \ 0467 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \ 0468 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \ 0469 0u : \ 0470 (key_type) == PSA_KEY_TYPE_CHACHA20 && \ 0471 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \ 0472 0u) 0473 0474 /** The maximum default nonce size among all supported pairs of key types and 0475 * AEAD algorithms, in bytes. 0476 * 0477 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH() 0478 * may return. 0479 * 0480 * \note This is not the maximum size of nonce supported as input to 0481 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), 0482 * just the largest size that may be generated by 0483 * #psa_aead_generate_nonce(). 0484 */ 0485 #define PSA_AEAD_NONCE_MAX_SIZE 13u 0486 0487 /** A sufficient output buffer size for psa_aead_update(). 0488 * 0489 * If the size of the output buffer is at least this large, it is 0490 * guaranteed that psa_aead_update() will not fail due to an 0491 * insufficient buffer size. The actual size of the output may be smaller 0492 * in any given call. 0493 * 0494 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length). 0495 * 0496 * \warning This macro may evaluate its arguments multiple times or 0497 * zero times, so you should not pass arguments that contain 0498 * side effects. 0499 * 0500 * \param key_type A symmetric key type that is 0501 * compatible with algorithm \p alg. 0502 * \param alg An AEAD algorithm 0503 * (\c PSA_ALG_XXX value such that 0504 * #PSA_ALG_IS_AEAD(\p alg) is true). 0505 * \param input_length Size of the input in bytes. 0506 * 0507 * \return A sufficient output buffer size for the specified 0508 * algorithm. 0509 * If the key type or AEAD algorithm is not 0510 * recognized, or the parameters are incompatible, 0511 * return 0. 0512 */ 0513 /* For all the AEAD modes defined in this specification, it is possible 0514 * to emit output without delay. However, hardware may not always be 0515 * capable of this. So for modes based on a block cipher, allow the 0516 * implementation to delay the output until it has a full block. */ 0517 #define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ 0518 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 0519 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 0520 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \ 0521 (input_length) : \ 0522 0u) 0523 0524 /** A sufficient output buffer size for psa_aead_update(), for any of the 0525 * supported key types and AEAD algorithms. 0526 * 0527 * If the size of the output buffer is at least this large, it is guaranteed 0528 * that psa_aead_update() will not fail due to an insufficient buffer size. 0529 * 0530 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 0531 * 0532 * \param input_length Size of the input in bytes. 0533 */ 0534 #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \ 0535 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length))) 0536 0537 /** A sufficient ciphertext buffer size for psa_aead_finish(). 0538 * 0539 * If the size of the ciphertext buffer is at least this large, it is 0540 * guaranteed that psa_aead_finish() will not fail due to an 0541 * insufficient ciphertext buffer size. The actual size of the output may 0542 * be smaller in any given call. 0543 * 0544 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE. 0545 * 0546 * \param key_type A symmetric key type that is 0547 compatible with algorithm \p alg. 0548 * \param alg An AEAD algorithm 0549 * (\c PSA_ALG_XXX value such that 0550 * #PSA_ALG_IS_AEAD(\p alg) is true). 0551 * 0552 * \return A sufficient ciphertext buffer size for the 0553 * specified algorithm. 0554 * If the key type or AEAD algorithm is not 0555 * recognized, or the parameters are incompatible, 0556 * return 0. 0557 */ 0558 #define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \ 0559 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 0560 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 0561 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 0562 0u) 0563 0564 /** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the 0565 * supported key types and AEAD algorithms. 0566 * 0567 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg). 0568 */ 0569 #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 0570 0571 /** A sufficient plaintext buffer size for psa_aead_verify(). 0572 * 0573 * If the size of the plaintext buffer is at least this large, it is 0574 * guaranteed that psa_aead_verify() will not fail due to an 0575 * insufficient plaintext buffer size. The actual size of the output may 0576 * be smaller in any given call. 0577 * 0578 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE. 0579 * 0580 * \param key_type A symmetric key type that is 0581 * compatible with algorithm \p alg. 0582 * \param alg An AEAD algorithm 0583 * (\c PSA_ALG_XXX value such that 0584 * #PSA_ALG_IS_AEAD(\p alg) is true). 0585 * 0586 * \return A sufficient plaintext buffer size for the 0587 * specified algorithm. 0588 * If the key type or AEAD algorithm is not 0589 * recognized, or the parameters are incompatible, 0590 * return 0. 0591 */ 0592 #define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \ 0593 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 0594 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 0595 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 0596 0u) 0597 0598 /** A sufficient plaintext buffer size for psa_aead_verify(), for any of the 0599 * supported key types and AEAD algorithms. 0600 * 0601 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg). 0602 */ 0603 #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 0604 0605 #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ 0606 (PSA_ALG_IS_RSA_OAEP(alg) ? \ 0607 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \ 0608 11u /*PKCS#1v1.5*/) 0609 0610 /** 0611 * \brief ECDSA signature size for a given curve bit size 0612 * 0613 * \param curve_bits Curve size in bits. 0614 * \return Signature size in bytes. 0615 * 0616 * \note This macro returns a compile-time constant if its argument is one. 0617 */ 0618 #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ 0619 (PSA_BITS_TO_BYTES(curve_bits) * 2u) 0620 0621 /** Sufficient signature buffer size for psa_sign_hash(). 0622 * 0623 * This macro returns a sufficient buffer size for a signature using a key 0624 * of the specified type and size, with the specified algorithm. 0625 * Note that the actual size of the signature may be smaller 0626 * (some algorithms produce a variable-size signature). 0627 * 0628 * \warning This function may call its arguments multiple times or 0629 * zero times, so you should not pass arguments that contain 0630 * side effects. 0631 * 0632 * \param key_type An asymmetric key type (this may indifferently be a 0633 * key pair type or a public key type). 0634 * \param key_bits The size of the key in bits. 0635 * \param alg The signature algorithm. 0636 * 0637 * \return If the parameters are valid and supported, return 0638 * a buffer size in bytes that guarantees that 0639 * psa_sign_hash() will not fail with 0640 * #PSA_ERROR_BUFFER_TOO_SMALL. 0641 * If the parameters are a valid combination that is not supported, 0642 * return either a sensible size or 0. 0643 * If the parameters are not valid, the 0644 * return value is unspecified. 0645 */ 0646 #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ 0647 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \ 0648 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ 0649 ((void) alg, 0u)) 0650 0651 #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \ 0652 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 0653 0654 /** \def PSA_SIGNATURE_MAX_SIZE 0655 * 0656 * Maximum size of an asymmetric signature. 0657 * 0658 * This macro expands to a compile-time constant integer. This value 0659 * is the maximum size of a signature in bytes. 0660 */ 0661 #define PSA_SIGNATURE_MAX_SIZE 1 0662 0663 #if (defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)) && \ 0664 (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE > PSA_SIGNATURE_MAX_SIZE) 0665 #undef PSA_SIGNATURE_MAX_SIZE 0666 #define PSA_SIGNATURE_MAX_SIZE PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE 0667 #endif 0668 #if (defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) || defined(PSA_WANT_ALG_RSA_PSS)) && \ 0669 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_SIGNATURE_MAX_SIZE) 0670 #undef PSA_SIGNATURE_MAX_SIZE 0671 #define PSA_SIGNATURE_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) 0672 #endif 0673 0674 /** Sufficient output buffer size for psa_asymmetric_encrypt(). 0675 * 0676 * This macro returns a sufficient buffer size for a ciphertext produced using 0677 * a key of the specified type and size, with the specified algorithm. 0678 * Note that the actual size of the ciphertext may be smaller, depending 0679 * on the algorithm. 0680 * 0681 * \warning This function may call its arguments multiple times or 0682 * zero times, so you should not pass arguments that contain 0683 * side effects. 0684 * 0685 * \param key_type An asymmetric key type (this may indifferently be a 0686 * key pair type or a public key type). 0687 * \param key_bits The size of the key in bits. 0688 * \param alg The asymmetric encryption algorithm. 0689 * 0690 * \return If the parameters are valid and supported, return 0691 * a buffer size in bytes that guarantees that 0692 * psa_asymmetric_encrypt() will not fail with 0693 * #PSA_ERROR_BUFFER_TOO_SMALL. 0694 * If the parameters are a valid combination that is not supported, 0695 * return either a sensible size or 0. 0696 * If the parameters are not valid, the 0697 * return value is unspecified. 0698 */ 0699 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ 0700 (PSA_KEY_TYPE_IS_RSA(key_type) ? \ 0701 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \ 0702 0u) 0703 0704 /** A sufficient output buffer size for psa_asymmetric_encrypt(), for any 0705 * supported asymmetric encryption. 0706 * 0707 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). 0708 */ 0709 /* This macro assumes that RSA is the only supported asymmetric encryption. */ 0710 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \ 0711 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) 0712 0713 /** Sufficient output buffer size for psa_asymmetric_decrypt(). 0714 * 0715 * This macro returns a sufficient buffer size for a plaintext produced using 0716 * a key of the specified type and size, with the specified algorithm. 0717 * Note that the actual size of the plaintext may be smaller, depending 0718 * on the algorithm. 0719 * 0720 * \warning This function may call its arguments multiple times or 0721 * zero times, so you should not pass arguments that contain 0722 * side effects. 0723 * 0724 * \param key_type An asymmetric key type (this may indifferently be a 0725 * key pair type or a public key type). 0726 * \param key_bits The size of the key in bits. 0727 * \param alg The asymmetric encryption algorithm. 0728 * 0729 * \return If the parameters are valid and supported, return 0730 * a buffer size in bytes that guarantees that 0731 * psa_asymmetric_decrypt() will not fail with 0732 * #PSA_ERROR_BUFFER_TOO_SMALL. 0733 * If the parameters are a valid combination that is not supported, 0734 * return either a sensible size or 0. 0735 * If the parameters are not valid, the 0736 * return value is unspecified. 0737 */ 0738 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ 0739 (PSA_KEY_TYPE_IS_RSA(key_type) ? \ 0740 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ 0741 0u) 0742 0743 /** A sufficient output buffer size for psa_asymmetric_decrypt(), for any 0744 * supported asymmetric decryption. 0745 * 0746 * This macro assumes that RSA is the only supported asymmetric encryption. 0747 * 0748 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). 0749 */ 0750 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \ 0751 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) 0752 0753 /* Maximum size of the ASN.1 encoding of an INTEGER with the specified 0754 * number of bits. 0755 * 0756 * This definition assumes that bits <= 2^19 - 9 so that the length field 0757 * is at most 3 bytes. The length of the encoding is the length of the 0758 * bit string padded to a whole number of bytes plus: 0759 * - 1 type byte; 0760 * - 1 to 3 length bytes; 0761 * - 0 to 1 bytes of leading 0 due to the sign bit. 0762 */ 0763 #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \ 0764 ((bits) / 8u + 5u) 0765 0766 /* Maximum size of the export encoding of an RSA public key. 0767 * Assumes that the public exponent is less than 2^32. 0768 * 0769 * RSAPublicKey ::= SEQUENCE { 0770 * modulus INTEGER, -- n 0771 * publicExponent INTEGER } -- e 0772 * 0773 * - 4 bytes of SEQUENCE overhead; 0774 * - n : INTEGER; 0775 * - 7 bytes for the public exponent. 0776 */ 0777 #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ 0778 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u) 0779 0780 /* Maximum size of the export encoding of an RSA key pair. 0781 * Assumes that the public exponent is less than 2^32 and that the size 0782 * difference between the two primes is at most 1 bit. 0783 * 0784 * RSAPrivateKey ::= SEQUENCE { 0785 * version Version, -- 0 0786 * modulus INTEGER, -- N-bit 0787 * publicExponent INTEGER, -- 32-bit 0788 * privateExponent INTEGER, -- N-bit 0789 * prime1 INTEGER, -- N/2-bit 0790 * prime2 INTEGER, -- N/2-bit 0791 * exponent1 INTEGER, -- N/2-bit 0792 * exponent2 INTEGER, -- N/2-bit 0793 * coefficient INTEGER, -- N/2-bit 0794 * } 0795 * 0796 * - 4 bytes of SEQUENCE overhead; 0797 * - 3 bytes of version; 0798 * - 7 half-size INTEGERs plus 2 full-size INTEGERs, 0799 * overapproximated as 9 half-size INTEGERS; 0800 * - 7 bytes for the public exponent. 0801 */ 0802 #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \ 0803 (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u) 0804 0805 /* Maximum size of the export encoding of a DSA public key. 0806 * 0807 * SubjectPublicKeyInfo ::= SEQUENCE { 0808 * algorithm AlgorithmIdentifier, 0809 * subjectPublicKey BIT STRING } -- contains DSAPublicKey 0810 * AlgorithmIdentifier ::= SEQUENCE { 0811 * algorithm OBJECT IDENTIFIER, 0812 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs 0813 * DSAPublicKey ::= INTEGER -- public key, Y 0814 * 0815 * - 3 * 4 bytes of SEQUENCE overhead; 0816 * - 1 + 1 + 7 bytes of algorithm (DSA OID); 0817 * - 4 bytes of BIT STRING overhead; 0818 * - 3 full-size INTEGERs (p, g, y); 0819 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits). 0820 */ 0821 #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ 0822 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u) 0823 0824 /* Maximum size of the export encoding of a DSA key pair. 0825 * 0826 * DSAPrivateKey ::= SEQUENCE { 0827 * version Version, -- 0 0828 * prime INTEGER, -- p 0829 * subprime INTEGER, -- q 0830 * generator INTEGER, -- g 0831 * public INTEGER, -- y 0832 * private INTEGER, -- x 0833 * } 0834 * 0835 * - 4 bytes of SEQUENCE overhead; 0836 * - 3 bytes of version; 0837 * - 3 full-size INTEGERs (p, g, y); 0838 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits). 0839 */ 0840 #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \ 0841 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u) 0842 0843 /* Maximum size of the export encoding of an ECC public key. 0844 * 0845 * The representation of an ECC public key is: 0846 * - The byte 0x04; 0847 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; 0848 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; 0849 * - where m is the bit size associated with the curve. 0850 * 0851 * - 1 byte + 2 * point size. 0852 */ 0853 #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \ 0854 (2u * PSA_BITS_TO_BYTES(key_bits) + 1u) 0855 0856 /* Maximum size of the export encoding of an ECC key pair. 0857 * 0858 * An ECC key pair is represented by the secret value. 0859 */ 0860 #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \ 0861 (PSA_BITS_TO_BYTES(key_bits)) 0862 0863 /* Maximum size of the export encoding of an DH key pair. 0864 * 0865 * An DH key pair is represented by the secret value. 0866 */ 0867 #define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \ 0868 (PSA_BITS_TO_BYTES(key_bits)) 0869 0870 /* Maximum size of the export encoding of an DH public key. 0871 */ 0872 #define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \ 0873 (PSA_BITS_TO_BYTES(key_bits)) 0874 0875 /** Sufficient output buffer size for psa_export_key() or 0876 * psa_export_public_key(). 0877 * 0878 * This macro returns a compile-time constant if its arguments are 0879 * compile-time constants. 0880 * 0881 * \warning This macro may evaluate its arguments multiple times or 0882 * zero times, so you should not pass arguments that contain 0883 * side effects. 0884 * 0885 * The following code illustrates how to allocate enough memory to export 0886 * a key by querying the key type and size at runtime. 0887 * \code{c} 0888 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 0889 * psa_status_t status; 0890 * status = psa_get_key_attributes(key, &attributes); 0891 * if (status != PSA_SUCCESS) handle_error(...); 0892 * psa_key_type_t key_type = psa_get_key_type(&attributes); 0893 * size_t key_bits = psa_get_key_bits(&attributes); 0894 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits); 0895 * psa_reset_key_attributes(&attributes); 0896 * uint8_t *buffer = malloc(buffer_size); 0897 * if (buffer == NULL) handle_error(...); 0898 * size_t buffer_length; 0899 * status = psa_export_key(key, buffer, buffer_size, &buffer_length); 0900 * if (status != PSA_SUCCESS) handle_error(...); 0901 * \endcode 0902 * 0903 * \param key_type A supported key type. 0904 * \param key_bits The size of the key in bits. 0905 * 0906 * \return If the parameters are valid and supported, return 0907 * a buffer size in bytes that guarantees that 0908 * psa_export_key() or psa_export_public_key() will not fail with 0909 * #PSA_ERROR_BUFFER_TOO_SMALL. 0910 * If the parameters are a valid combination that is not supported, 0911 * return either a sensible size or 0. 0912 * If the parameters are not valid, the return value is unspecified. 0913 */ 0914 #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \ 0915 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 0916 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 0917 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \ 0918 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 0919 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \ 0920 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 0921 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \ 0922 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 0923 0u) 0924 0925 /** Sufficient output buffer size for psa_export_public_key(). 0926 * 0927 * This macro returns a compile-time constant if its arguments are 0928 * compile-time constants. 0929 * 0930 * \warning This macro may evaluate its arguments multiple times or 0931 * zero times, so you should not pass arguments that contain 0932 * side effects. 0933 * 0934 * The following code illustrates how to allocate enough memory to export 0935 * a public key by querying the key type and size at runtime. 0936 * \code{c} 0937 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 0938 * psa_status_t status; 0939 * status = psa_get_key_attributes(key, &attributes); 0940 * if (status != PSA_SUCCESS) handle_error(...); 0941 * psa_key_type_t key_type = psa_get_key_type(&attributes); 0942 * size_t key_bits = psa_get_key_bits(&attributes); 0943 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits); 0944 * psa_reset_key_attributes(&attributes); 0945 * uint8_t *buffer = malloc(buffer_size); 0946 * if (buffer == NULL) handle_error(...); 0947 * size_t buffer_length; 0948 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length); 0949 * if (status != PSA_SUCCESS) handle_error(...); 0950 * \endcode 0951 * 0952 * \param key_type A public key or key pair key type. 0953 * \param key_bits The size of the key in bits. 0954 * 0955 * \return If the parameters are valid and supported, return 0956 * a buffer size in bytes that guarantees that 0957 * psa_export_public_key() will not fail with 0958 * #PSA_ERROR_BUFFER_TOO_SMALL. 0959 * If the parameters are a valid combination that is not 0960 * supported, return either a sensible size or 0. 0961 * If the parameters are not valid, 0962 * the return value is unspecified. 0963 * 0964 * If the parameters are valid and supported, 0965 * return the same result as 0966 * #PSA_EXPORT_KEY_OUTPUT_SIZE( 0967 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type), 0968 * \p key_bits). 0969 */ 0970 #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \ 0971 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 0972 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 0973 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 0974 0u) 0975 0976 /** Sufficient buffer size for exporting any asymmetric key pair. 0977 * 0978 * This macro expands to a compile-time constant integer. This value is 0979 * a sufficient buffer size when calling psa_export_key() to export any 0980 * asymmetric key pair, regardless of the exact key type and key size. 0981 * 0982 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). 0983 */ 0984 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE 1 0985 0986 #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) && \ 0987 (PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \ 0988 PSA_EXPORT_KEY_PAIR_MAX_SIZE) 0989 #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 0990 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 0991 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 0992 #endif 0993 #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) && \ 0994 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ 0995 PSA_EXPORT_KEY_PAIR_MAX_SIZE) 0996 #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 0997 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 0998 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 0999 #endif 1000 #if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_BASIC) && \ 1001 (PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \ 1002 PSA_EXPORT_KEY_PAIR_MAX_SIZE) 1003 #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 1004 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 1005 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1006 #endif 1007 1008 /** Sufficient buffer size for exporting any asymmetric public key. 1009 * 1010 * This macro expands to a compile-time constant integer. This value is 1011 * a sufficient buffer size when calling psa_export_key() or 1012 * psa_export_public_key() to export any asymmetric public key, 1013 * regardless of the exact key type and key size. 1014 * 1015 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). 1016 */ 1017 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1 1018 1019 #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \ 1020 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \ 1021 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1022 #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1023 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1024 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 1025 #endif 1026 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) && \ 1027 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ 1028 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1029 #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1030 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1031 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 1032 #endif 1033 #if defined(PSA_WANT_KEY_TYPE_DH_PUBLIC_KEY) && \ 1034 (PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \ 1035 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1036 #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1037 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1038 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1039 #endif 1040 1041 /** Sufficient output buffer size for psa_raw_key_agreement(). 1042 * 1043 * This macro returns a compile-time constant if its arguments are 1044 * compile-time constants. 1045 * 1046 * \warning This macro may evaluate its arguments multiple times or 1047 * zero times, so you should not pass arguments that contain 1048 * side effects. 1049 * 1050 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE. 1051 * 1052 * \param key_type A supported key type. 1053 * \param key_bits The size of the key in bits. 1054 * 1055 * \return If the parameters are valid and supported, return 1056 * a buffer size in bytes that guarantees that 1057 * psa_raw_key_agreement() will not fail with 1058 * #PSA_ERROR_BUFFER_TOO_SMALL. 1059 * If the parameters are a valid combination that 1060 * is not supported, return either a sensible size or 0. 1061 * If the parameters are not valid, 1062 * the return value is unspecified. 1063 */ 1064 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \ 1065 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \ 1066 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u) 1067 1068 /** Maximum size of the output from psa_raw_key_agreement(). 1069 * 1070 * This macro expands to a compile-time constant integer. This value is the 1071 * maximum size of the output any raw key agreement algorithm, in bytes. 1072 * 1073 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits). 1074 */ 1075 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1 1076 1077 #if defined(PSA_WANT_ALG_ECDH) && \ 1078 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) 1079 #undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1080 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) 1081 #endif 1082 #if defined(PSA_WANT_ALG_FFDH) && \ 1083 (PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) 1084 #undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1085 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1086 #endif 1087 1088 /** The default IV size for a cipher algorithm, in bytes. 1089 * 1090 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always 1091 * the default IV length for the algorithm. 1092 * 1093 * This macro can be used to allocate a buffer of sufficient size to 1094 * store the IV output from #psa_cipher_generate_iv() when using 1095 * a multi-part cipher operation. 1096 * 1097 * See also #PSA_CIPHER_IV_MAX_SIZE. 1098 * 1099 * \warning This macro may evaluate its arguments multiple times or 1100 * zero times, so you should not pass arguments that contain 1101 * side effects. 1102 * 1103 * \param key_type A symmetric key type that is compatible with algorithm \p alg. 1104 * 1105 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true). 1106 * 1107 * \return The default IV size for the specified key type and algorithm. 1108 * If the algorithm does not use an IV, return 0. 1109 * If the key type or cipher algorithm is not recognized, 1110 * or the parameters are incompatible, return 0. 1111 */ 1112 #define PSA_CIPHER_IV_LENGTH(key_type, alg) \ 1113 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \ 1114 ((alg) == PSA_ALG_CTR || \ 1115 (alg) == PSA_ALG_CFB || \ 1116 (alg) == PSA_ALG_OFB || \ 1117 (alg) == PSA_ALG_XTS || \ 1118 (alg) == PSA_ALG_CBC_NO_PADDING || \ 1119 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 1120 (key_type) == PSA_KEY_TYPE_CHACHA20 && \ 1121 (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \ 1122 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \ 1123 0u) 1124 1125 /** The maximum IV size for all supported cipher algorithms, in bytes. 1126 * 1127 * See also #PSA_CIPHER_IV_LENGTH(). 1128 */ 1129 #define PSA_CIPHER_IV_MAX_SIZE 16u 1130 1131 /** The maximum size of the output of psa_cipher_encrypt(), in bytes. 1132 * 1133 * If the size of the output buffer is at least this large, it is guaranteed 1134 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. 1135 * Depending on the algorithm, the actual size of the output might be smaller. 1136 * 1137 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length). 1138 * 1139 * \warning This macro may evaluate its arguments multiple times or 1140 * zero times, so you should not pass arguments that contain 1141 * side effects. 1142 * 1143 * \param key_type A symmetric key type that is compatible with algorithm 1144 * alg. 1145 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that 1146 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1147 * \param input_length Size of the input in bytes. 1148 * 1149 * \return A sufficient output size for the specified key type and 1150 * algorithm. If the key type or cipher algorithm is not 1151 * recognized, or the parameters are incompatible, 1152 * return 0. 1153 */ 1154 #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ 1155 (alg == PSA_ALG_CBC_PKCS7 ? \ 1156 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \ 1157 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ 1158 (input_length) + 1u) + \ 1159 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \ 1160 (PSA_ALG_IS_CIPHER(alg) ? \ 1161 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ 1162 0u)) 1163 1164 /** A sufficient output buffer size for psa_cipher_encrypt(), for any of the 1165 * supported key types and cipher algorithms. 1166 * 1167 * If the size of the output buffer is at least this large, it is guaranteed 1168 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. 1169 * 1170 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 1171 * 1172 * \param input_length Size of the input in bytes. 1173 * 1174 */ 1175 #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \ 1176 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \ 1177 (input_length) + 1u) + \ 1178 PSA_CIPHER_IV_MAX_SIZE) 1179 1180 /** The maximum size of the output of psa_cipher_decrypt(), in bytes. 1181 * 1182 * If the size of the output buffer is at least this large, it is guaranteed 1183 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. 1184 * Depending on the algorithm, the actual size of the output might be smaller. 1185 * 1186 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length). 1187 * 1188 * \param key_type A symmetric key type that is compatible with algorithm 1189 * alg. 1190 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that 1191 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1192 * \param input_length Size of the input in bytes. 1193 * 1194 * \return A sufficient output size for the specified key type and 1195 * algorithm. If the key type or cipher algorithm is not 1196 * recognized, or the parameters are incompatible, 1197 * return 0. 1198 */ 1199 #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ 1200 (PSA_ALG_IS_CIPHER(alg) && \ 1201 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \ 1202 (input_length) : \ 1203 0u) 1204 1205 /** A sufficient output buffer size for psa_cipher_decrypt(), for any of the 1206 * supported key types and cipher algorithms. 1207 * 1208 * If the size of the output buffer is at least this large, it is guaranteed 1209 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. 1210 * 1211 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 1212 * 1213 * \param input_length Size of the input in bytes. 1214 */ 1215 #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \ 1216 (input_length) 1217 1218 /** A sufficient output buffer size for psa_cipher_update(). 1219 * 1220 * If the size of the output buffer is at least this large, it is guaranteed 1221 * that psa_cipher_update() will not fail due to an insufficient buffer size. 1222 * The actual size of the output might be smaller in any given call. 1223 * 1224 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length). 1225 * 1226 * \param key_type A symmetric key type that is compatible with algorithm 1227 * alg. 1228 * \param alg A cipher algorithm (PSA_ALG_XXX value such that 1229 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1230 * \param input_length Size of the input in bytes. 1231 * 1232 * \return A sufficient output size for the specified key type and 1233 * algorithm. If the key type or cipher algorithm is not 1234 * recognized, or the parameters are incompatible, return 0. 1235 */ 1236 #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ 1237 (PSA_ALG_IS_CIPHER(alg) ? \ 1238 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \ 1239 (((alg) == PSA_ALG_CBC_PKCS7 || \ 1240 (alg) == PSA_ALG_CBC_NO_PADDING || \ 1241 (alg) == PSA_ALG_ECB_NO_PADDING) ? \ 1242 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ 1243 input_length) : \ 1244 (input_length)) : 0u) : \ 1245 0u) 1246 1247 /** A sufficient output buffer size for psa_cipher_update(), for any of the 1248 * supported key types and cipher algorithms. 1249 * 1250 * If the size of the output buffer is at least this large, it is guaranteed 1251 * that psa_cipher_update() will not fail due to an insufficient buffer size. 1252 * 1253 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 1254 * 1255 * \param input_length Size of the input in bytes. 1256 */ 1257 #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \ 1258 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length)) 1259 1260 /** A sufficient ciphertext buffer size for psa_cipher_finish(). 1261 * 1262 * If the size of the ciphertext buffer is at least this large, it is 1263 * guaranteed that psa_cipher_finish() will not fail due to an insufficient 1264 * ciphertext buffer size. The actual size of the output might be smaller in 1265 * any given call. 1266 * 1267 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE(). 1268 * 1269 * \param key_type A symmetric key type that is compatible with algorithm 1270 * alg. 1271 * \param alg A cipher algorithm (PSA_ALG_XXX value such that 1272 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1273 * \return A sufficient output size for the specified key type and 1274 * algorithm. If the key type or cipher algorithm is not 1275 * recognized, or the parameters are incompatible, return 0. 1276 */ 1277 #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \ 1278 (PSA_ALG_IS_CIPHER(alg) ? \ 1279 (alg == PSA_ALG_CBC_PKCS7 ? \ 1280 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 1281 0u) : \ 1282 0u) 1283 1284 /** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the 1285 * supported key types and cipher algorithms. 1286 * 1287 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg). 1288 */ 1289 #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \ 1290 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 1291 1292 #endif /* PSA_CRYPTO_SIZES_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |