1 /* 2 * Copyright (c) 2018-2023, Arm Limited. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 /** 8 * \file psa/crypto_sizes.h 9 * 10 * \brief PSA cryptography module: Mbed TLS buffer size macros 11 * 12 * \note This file may not be included directly. Applications must 13 * include psa/crypto.h. 14 * 15 * This file contains the definitions of macros that are useful to 16 * compute buffer sizes. The signatures and semantics of these macros 17 * are standardized, but the definitions are not, because they depend on 18 * the available algorithms and, in some cases, on permitted tolerances 19 * on buffer sizes. 20 * 21 * In implementations with isolation between the application and the 22 * cryptography module, implementers should take care to ensure that 23 * the definitions that are exposed to applications match what the 24 * module implements. 25 * 26 * Macros that compute sizes whose values do not depend on the 27 * implementation are in crypto.h. 28 */ 29 30 #ifndef PSA_CRYPTO_SIZES_H 31 #define PSA_CRYPTO_SIZES_H 32 33 #define PSA_BITS_TO_BYTES(bits) (((bits) + 7u) / 8u) 34 #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8u) 35 #define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \ 36 (c) : (b) : (a) <= (c) ? (c) : (a)) 37 38 #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \ 39 (((length) + (block_size) - 1) / (block_size) * (block_size)) 40 41 /** The size of the output of psa_hash_finish(), in bytes. 42 * 43 * This is also the hash size that psa_hash_verify() expects. 44 * 45 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that 46 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm 47 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a 48 * hash algorithm). 49 * 50 * \return The hash size for the specified hash algorithm. 51 * If the hash algorithm is not recognized, return 0. 52 */ 53 #define PSA_HASH_LENGTH(alg) \ 54 ( \ 55 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16u : \ 56 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20u : \ 57 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20u : \ 58 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28u : \ 59 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32u : \ 60 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48u : \ 61 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64u : \ 62 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28u : \ 63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32u : \ 64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28u : \ 65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32u : \ 66 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48u : \ 67 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64u : \ 68 0u) 69 70 /** The input block size of a hash algorithm, in bytes. 71 * 72 * Hash algorithms process their input data in blocks. Hash operations will 73 * retain any partial blocks until they have enough input to fill the block or 74 * until the operation is finished. 75 * This affects the output from psa_hash_suspend(). 76 * 77 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that 78 * PSA_ALG_IS_HASH(\p alg) is true). 79 * 80 * \return The block size in bytes for the specified hash algorithm. 81 * If the hash algorithm is not recognized, return 0. 82 * An implementation can return either 0 or the correct size for a 83 * hash algorithm that it recognizes, but does not support. 84 */ 85 #define PSA_HASH_BLOCK_LENGTH(alg) \ 86 ( \ 87 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64u : \ 88 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64u : \ 89 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64u : \ 90 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64u : \ 91 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64u : \ 92 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128u : \ 93 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128u : \ 94 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128u : \ 95 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128u : \ 96 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144u : \ 97 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136u : \ 98 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104u : \ 99 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72u : \ 100 0u) 101 102 /** \def PSA_HASH_MAX_SIZE 103 * 104 * Maximum size of a hash. 105 * 106 * This macro expands to a compile-time constant integer. This value 107 * is the maximum size of a hash in bytes. 108 */ 109 /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224, 110 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for 111 * HMAC-SHA3-512. */ 112 /* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE, 113 * see the note on MBEDTLS_MD_MAX_SIZE for details. 114 */ 115 #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) 116 #if defined(PSA_WANT_ALG_SHA3_224) 117 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u 118 #elif defined(PSA_WANT_ALG_SHA3_256) 119 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 136u 120 #elif defined(PSA_WANT_ALG_SHA_512) 121 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u 122 #elif defined(PSA_WANT_ALG_SHA_384) 123 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u 124 #elif defined(PSA_WANT_ALG_SHA3_384) 125 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 104u 126 #elif defined(PSA_WANT_ALG_SHA3_512) 127 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 72u 128 #elif defined(PSA_WANT_ALG_SHA_256) 129 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 130 #elif defined(PSA_WANT_ALG_SHA_224) 131 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 132 #else /* SHA-1 or smaller */ 133 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 134 #endif 135 136 #if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512) 137 #define PSA_HASH_MAX_SIZE 64u 138 #elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384) 139 #define PSA_HASH_MAX_SIZE 48u 140 #elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256) 141 #define PSA_HASH_MAX_SIZE 32u 142 #elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224) 143 #define PSA_HASH_MAX_SIZE 28u 144 #else /* SHA-1 or smaller */ 145 #define PSA_HASH_MAX_SIZE 20u 146 #endif 147 #else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */ 148 /* Without any PSA configuration we must assume the maximum size possible. */ 149 #define PSA_HASH_MAX_SIZE 64u 150 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u 151 #endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */ 152 153 /** \def PSA_MAC_MAX_SIZE 154 * 155 * Maximum size of a MAC. 156 * 157 * This macro expands to a compile-time constant integer. This value 158 * is the maximum size of a MAC in bytes. 159 */ 160 /* All non-HMAC MACs have a maximum size that's smaller than the 161 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */ 162 /* Note that the encoding of truncated MAC algorithms limits this value 163 * to 64 bytes. 164 */ 165 #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE 166 167 /** The length of a tag for an AEAD algorithm, in bytes. 168 * 169 * This macro can be used to allocate a buffer of sufficient size to store the 170 * tag output from psa_aead_finish(). 171 * 172 * See also #PSA_AEAD_TAG_MAX_SIZE. 173 * 174 * \param key_type The type of the AEAD key. 175 * \param key_bits The size of the AEAD key in bits. 176 * \param alg An AEAD algorithm 177 * (\c PSA_ALG_XXX value such that 178 * #PSA_ALG_IS_AEAD(\p alg) is true). 179 * 180 * \return The tag length for the specified algorithm and key. 181 * If the AEAD algorithm does not have an identified 182 * tag that can be distinguished from the rest of 183 * the ciphertext, return 0. 184 * If the key type or AEAD algorithm is not 185 * recognized, or the parameters are incompatible, 186 * return 0. 187 */ 188 #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \ 189 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 190 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 191 ((void) (key_bits), 0u)) 192 193 /** The maximum tag size for all supported AEAD algorithms, in bytes. 194 * 195 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg). 196 */ 197 #define PSA_AEAD_TAG_MAX_SIZE 16u 198 199 /* The maximum size of an RSA key on this implementation, in bits. 200 * This is a vendor-specific macro. 201 * 202 * Mbed TLS does not set a hard limit on the size of RSA keys: any key 203 * whose parameters fit in a bignum is accepted. However large keys can 204 * induce a large memory usage and long computation times. Unlike other 205 * auxiliary macros in this file and in crypto.h, which reflect how the 206 * library is configured, this macro defines how the library is 207 * configured. This implementation refuses to import or generate an 208 * RSA key whose size is larger than the value defined here. 209 * 210 * Note that an implementation may set different size limits for different 211 * operations, and does not need to accept all key sizes up to the limit. */ 212 #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u 213 214 /* The minimum size of an RSA key on this implementation, in bits. 215 * This is a vendor-specific macro. 216 * 217 * Limits RSA key generation to a minimum due to avoid accidental misuse. 218 * This value cannot be less than 128 bits. 219 */ 220 #if defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS) 221 #define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS MBEDTLS_RSA_GEN_KEY_MIN_BITS 222 #else 223 #define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS 1024 224 #endif 225 226 /* The maximum size of an DH key on this implementation, in bits. 227 * 228 * Note that an implementation may set different size limits for different 229 * operations, and does not need to accept all key sizes up to the limit. 230 */ 231 #define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u 232 233 /* The maximum size of an ECC key on this implementation, in bits. 234 * This is a vendor-specific macro. */ 235 #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) 236 #if defined(PSA_WANT_ECC_SECP_R1_521) 237 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u 238 #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) 239 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u 240 #elif defined(PSA_WANT_ECC_MONTGOMERY_448) 241 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u 242 #elif defined(PSA_WANT_ECC_SECP_R1_384) 243 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u 244 #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) 245 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u 246 #elif defined(PSA_WANT_ECC_SECP_R1_256) 247 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 248 #elif defined(PSA_WANT_ECC_SECP_K1_256) 249 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 250 #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) 251 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 252 #elif defined(PSA_WANT_ECC_MONTGOMERY_255) 253 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u 254 #elif defined(PSA_WANT_ECC_SECP_R1_224) 255 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u 256 #elif defined(PSA_WANT_ECC_SECP_K1_224) 257 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u 258 #elif defined(PSA_WANT_ECC_SECP_R1_192) 259 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u 260 #elif defined(PSA_WANT_ECC_SECP_K1_192) 261 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u 262 #else 263 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u 264 #endif 265 #else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */ 266 /* Without any PSA configuration we must assume the maximum size possible. */ 267 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521 268 #endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */ 269 270 /** This macro returns the maximum supported length of the PSK for the 271 * TLS-1.2 PSK-to-MS key derivation 272 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)). 273 * 274 * The maximum supported length does not depend on the chosen hash algorithm. 275 * 276 * Quoting RFC 4279, Sect 5.3: 277 * TLS implementations supporting these ciphersuites MUST support 278 * arbitrary PSK identities up to 128 octets in length, and arbitrary 279 * PSKs up to 64 octets in length. Supporting longer identities and 280 * keys is RECOMMENDED. 281 * 282 * Therefore, no implementation should define a value smaller than 64 283 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE. 284 */ 285 #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u 286 287 /* The expected size of input passed to psa_tls12_ecjpake_to_pms_input, 288 * which is expected to work with P-256 curve only. */ 289 #define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u 290 291 /* The size of a serialized K.X coordinate to be used in 292 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256 293 * curve. */ 294 #define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u 295 296 /* The maximum number of iterations for PBKDF2 on this implementation, in bits. 297 * This is a vendor-specific macro. This can be configured if necessary. 298 */ 299 #define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU 300 301 /** The maximum size of a block cipher. */ 302 #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u 303 304 /** The size of the output of psa_mac_sign_finish(), in bytes. 305 * 306 * This is also the MAC size that psa_mac_verify_finish() expects. 307 * 308 * \warning This macro may evaluate its arguments multiple times or 309 * zero times, so you should not pass arguments that contain 310 * side effects. 311 * 312 * \param key_type The type of the MAC key. 313 * \param key_bits The size of the MAC key in bits. 314 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that 315 * #PSA_ALG_IS_MAC(\p alg) is true). 316 * 317 * \return The MAC size for the specified algorithm with 318 * the specified key parameters. 319 * \return 0 if the MAC algorithm is not recognized. 320 * \return Either 0 or the correct size for a MAC algorithm that 321 * the implementation recognizes, but does not support. 322 * \return Unspecified if the key parameters are not consistent 323 * with the algorithm. 324 */ 325 #define PSA_MAC_LENGTH(key_type, key_bits, alg) \ 326 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \ 327 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \ 328 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 329 ((void) (key_type), (void) (key_bits), 0u)) 330 331 /** The maximum size of the output of psa_aead_encrypt(), in bytes. 332 * 333 * If the size of the ciphertext buffer is at least this large, it is 334 * guaranteed that psa_aead_encrypt() will not fail due to an 335 * insufficient buffer size. Depending on the algorithm, the actual size of 336 * the ciphertext may be smaller. 337 * 338 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length). 339 * 340 * \warning This macro may evaluate its arguments multiple times or 341 * zero times, so you should not pass arguments that contain 342 * side effects. 343 * 344 * \param key_type A symmetric key type that is 345 * compatible with algorithm \p alg. 346 * \param alg An AEAD algorithm 347 * (\c PSA_ALG_XXX value such that 348 * #PSA_ALG_IS_AEAD(\p alg) is true). 349 * \param plaintext_length Size of the plaintext in bytes. 350 * 351 * \return The AEAD ciphertext size for the specified 352 * algorithm. 353 * If the key type or AEAD algorithm is not 354 * recognized, or the parameters are incompatible, 355 * return 0. 356 */ 357 #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \ 358 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 359 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 360 0u) 361 362 /** A sufficient output buffer size for psa_aead_encrypt(), for any of the 363 * supported key types and AEAD algorithms. 364 * 365 * If the size of the ciphertext buffer is at least this large, it is guaranteed 366 * that psa_aead_encrypt() will not fail due to an insufficient buffer size. 367 * 368 * \note This macro returns a compile-time constant if its arguments are 369 * compile-time constants. 370 * 371 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, 372 * \p plaintext_length). 373 * 374 * \param plaintext_length Size of the plaintext in bytes. 375 * 376 * \return A sufficient output buffer size for any of the 377 * supported key types and AEAD algorithms. 378 * 379 */ 380 #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \ 381 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE) 382 383 384 /** The maximum size of the output of psa_aead_decrypt(), in bytes. 385 * 386 * If the size of the plaintext buffer is at least this large, it is 387 * guaranteed that psa_aead_decrypt() will not fail due to an 388 * insufficient buffer size. Depending on the algorithm, the actual size of 389 * the plaintext may be smaller. 390 * 391 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length). 392 * 393 * \warning This macro may evaluate its arguments multiple times or 394 * zero times, so you should not pass arguments that contain 395 * side effects. 396 * 397 * \param key_type A symmetric key type that is 398 * compatible with algorithm \p alg. 399 * \param alg An AEAD algorithm 400 * (\c PSA_ALG_XXX value such that 401 * #PSA_ALG_IS_AEAD(\p alg) is true). 402 * \param ciphertext_length Size of the plaintext in bytes. 403 * 404 * \return The AEAD ciphertext size for the specified 405 * algorithm. 406 * If the key type or AEAD algorithm is not 407 * recognized, or the parameters are incompatible, 408 * return 0. 409 */ 410 #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \ 411 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 412 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \ 413 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 414 0u) 415 416 /** A sufficient output buffer size for psa_aead_decrypt(), for any of the 417 * supported key types and AEAD algorithms. 418 * 419 * If the size of the plaintext buffer is at least this large, it is guaranteed 420 * that psa_aead_decrypt() will not fail due to an insufficient buffer size. 421 * 422 * \note This macro returns a compile-time constant if its arguments are 423 * compile-time constants. 424 * 425 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, 426 * \p ciphertext_length). 427 * 428 * \param ciphertext_length Size of the ciphertext in bytes. 429 * 430 * \return A sufficient output buffer size for any of the 431 * supported key types and AEAD algorithms. 432 * 433 */ 434 #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \ 435 (ciphertext_length) 436 437 /** The default nonce size for an AEAD algorithm, in bytes. 438 * 439 * This macro can be used to allocate a buffer of sufficient size to 440 * store the nonce output from #psa_aead_generate_nonce(). 441 * 442 * See also #PSA_AEAD_NONCE_MAX_SIZE. 443 * 444 * \note This is not the maximum size of nonce supported as input to 445 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), 446 * just the default size that is generated by #psa_aead_generate_nonce(). 447 * 448 * \warning This macro may evaluate its arguments multiple times or 449 * zero times, so you should not pass arguments that contain 450 * side effects. 451 * 452 * \param key_type A symmetric key type that is compatible with 453 * algorithm \p alg. 454 * 455 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that 456 * #PSA_ALG_IS_AEAD(\p alg) is true). 457 * 458 * \return The default nonce size for the specified key type and algorithm. 459 * If the key type or AEAD algorithm is not recognized, 460 * or the parameters are incompatible, return 0. 461 */ 462 #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ 463 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \ 464 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \ 465 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \ 466 0u : \ 467 (key_type) == PSA_KEY_TYPE_CHACHA20 && \ 468 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \ 469 0u) 470 471 /** The maximum default nonce size among all supported pairs of key types and 472 * AEAD algorithms, in bytes. 473 * 474 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH() 475 * may return. 476 * 477 * \note This is not the maximum size of nonce supported as input to 478 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), 479 * just the largest size that may be generated by 480 * #psa_aead_generate_nonce(). 481 */ 482 #define PSA_AEAD_NONCE_MAX_SIZE 13u 483 484 /** A sufficient output buffer size for psa_aead_update(). 485 * 486 * If the size of the output buffer is at least this large, it is 487 * guaranteed that psa_aead_update() will not fail due to an 488 * insufficient buffer size. The actual size of the output may be smaller 489 * in any given call. 490 * 491 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length). 492 * 493 * \warning This macro may evaluate its arguments multiple times or 494 * zero times, so you should not pass arguments that contain 495 * side effects. 496 * 497 * \param key_type A symmetric key type that is 498 * compatible with algorithm \p alg. 499 * \param alg An AEAD algorithm 500 * (\c PSA_ALG_XXX value such that 501 * #PSA_ALG_IS_AEAD(\p alg) is true). 502 * \param input_length Size of the input in bytes. 503 * 504 * \return A sufficient output buffer size for the specified 505 * algorithm. 506 * If the key type or AEAD algorithm is not 507 * recognized, or the parameters are incompatible, 508 * return 0. 509 */ 510 /* For all the AEAD modes defined in this specification, it is possible 511 * to emit output without delay. However, hardware may not always be 512 * capable of this. So for modes based on a block cipher, allow the 513 * implementation to delay the output until it has a full block. */ 514 #define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ 515 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 516 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 517 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \ 518 (input_length) : \ 519 0u) 520 521 /** A sufficient output buffer size for psa_aead_update(), for any of the 522 * supported key types and AEAD algorithms. 523 * 524 * If the size of the output buffer is at least this large, it is guaranteed 525 * that psa_aead_update() will not fail due to an insufficient buffer size. 526 * 527 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 528 * 529 * \param input_length Size of the input in bytes. 530 */ 531 #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \ 532 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length))) 533 534 /** A sufficient ciphertext buffer size for psa_aead_finish(). 535 * 536 * If the size of the ciphertext buffer is at least this large, it is 537 * guaranteed that psa_aead_finish() will not fail due to an 538 * insufficient ciphertext buffer size. The actual size of the output may 539 * be smaller in any given call. 540 * 541 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE. 542 * 543 * \param key_type A symmetric key type that is 544 compatible with algorithm \p alg. 545 * \param alg An AEAD algorithm 546 * (\c PSA_ALG_XXX value such that 547 * #PSA_ALG_IS_AEAD(\p alg) is true). 548 * 549 * \return A sufficient ciphertext buffer size for the 550 * specified algorithm. 551 * If the key type or AEAD algorithm is not 552 * recognized, or the parameters are incompatible, 553 * return 0. 554 */ 555 #define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \ 556 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 557 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 558 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 559 0u) 560 561 /** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the 562 * supported key types and AEAD algorithms. 563 * 564 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg). 565 */ 566 #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 567 568 /** A sufficient plaintext buffer size for psa_aead_verify(). 569 * 570 * If the size of the plaintext buffer is at least this large, it is 571 * guaranteed that psa_aead_verify() will not fail due to an 572 * insufficient plaintext buffer size. The actual size of the output may 573 * be smaller in any given call. 574 * 575 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE. 576 * 577 * \param key_type A symmetric key type that is 578 * compatible with algorithm \p alg. 579 * \param alg An AEAD algorithm 580 * (\c PSA_ALG_XXX value such that 581 * #PSA_ALG_IS_AEAD(\p alg) is true). 582 * 583 * \return A sufficient plaintext buffer size for the 584 * specified algorithm. 585 * If the key type or AEAD algorithm is not 586 * recognized, or the parameters are incompatible, 587 * return 0. 588 */ 589 #define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \ 590 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 591 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 592 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 593 0u) 594 595 /** A sufficient plaintext buffer size for psa_aead_verify(), for any of the 596 * supported key types and AEAD algorithms. 597 * 598 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg). 599 */ 600 #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 601 602 #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ 603 (PSA_ALG_IS_RSA_OAEP(alg) ? \ 604 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \ 605 11u /*PKCS#1v1.5*/) 606 607 /** 608 * \brief ECDSA signature size for a given curve bit size 609 * 610 * \param curve_bits Curve size in bits. 611 * \return Signature size in bytes. 612 * 613 * \note This macro returns a compile-time constant if its argument is one. 614 */ 615 #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ 616 (PSA_BITS_TO_BYTES(curve_bits) * 2u) 617 618 /** Sufficient signature buffer size for psa_sign_hash(). 619 * 620 * This macro returns a sufficient buffer size for a signature using a key 621 * of the specified type and size, with the specified algorithm. 622 * Note that the actual size of the signature may be smaller 623 * (some algorithms produce a variable-size signature). 624 * 625 * \warning This function may call its arguments multiple times or 626 * zero times, so you should not pass arguments that contain 627 * side effects. 628 * 629 * \param key_type An asymmetric key type (this may indifferently be a 630 * key pair type or a public key type). 631 * \param key_bits The size of the key in bits. 632 * \param alg The signature algorithm. 633 * 634 * \return If the parameters are valid and supported, return 635 * a buffer size in bytes that guarantees that 636 * psa_sign_hash() will not fail with 637 * #PSA_ERROR_BUFFER_TOO_SMALL. 638 * If the parameters are a valid combination that is not supported, 639 * return either a sensible size or 0. 640 * If the parameters are not valid, the 641 * return value is unspecified. 642 */ 643 #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ 644 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \ 645 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ 646 ((void) alg, 0u)) 647 648 #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \ 649 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 650 651 /** \def PSA_SIGNATURE_MAX_SIZE 652 * 653 * Maximum size of an asymmetric signature. 654 * 655 * This macro expands to a compile-time constant integer. This value 656 * is the maximum size of a signature in bytes. 657 */ 658 #define PSA_SIGNATURE_MAX_SIZE 1 659 660 #if (defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)) && \ 661 (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE > PSA_SIGNATURE_MAX_SIZE) 662 #undef PSA_SIGNATURE_MAX_SIZE 663 #define PSA_SIGNATURE_MAX_SIZE PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE 664 #endif 665 #if (defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) || defined(PSA_WANT_ALG_RSA_PSS)) && \ 666 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_SIGNATURE_MAX_SIZE) 667 #undef PSA_SIGNATURE_MAX_SIZE 668 #define PSA_SIGNATURE_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) 669 #endif 670 671 /** Sufficient output buffer size for psa_asymmetric_encrypt(). 672 * 673 * This macro returns a sufficient buffer size for a ciphertext produced using 674 * a key of the specified type and size, with the specified algorithm. 675 * Note that the actual size of the ciphertext may be smaller, depending 676 * on the algorithm. 677 * 678 * \warning This function may call its arguments multiple times or 679 * zero times, so you should not pass arguments that contain 680 * side effects. 681 * 682 * \param key_type An asymmetric key type (this may indifferently be a 683 * key pair type or a public key type). 684 * \param key_bits The size of the key in bits. 685 * \param alg The asymmetric encryption algorithm. 686 * 687 * \return If the parameters are valid and supported, return 688 * a buffer size in bytes that guarantees that 689 * psa_asymmetric_encrypt() will not fail with 690 * #PSA_ERROR_BUFFER_TOO_SMALL. 691 * If the parameters are a valid combination that is not supported, 692 * return either a sensible size or 0. 693 * If the parameters are not valid, the 694 * return value is unspecified. 695 */ 696 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ 697 (PSA_KEY_TYPE_IS_RSA(key_type) ? \ 698 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \ 699 0u) 700 701 /** A sufficient output buffer size for psa_asymmetric_encrypt(), for any 702 * supported asymmetric encryption. 703 * 704 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). 705 */ 706 /* This macro assumes that RSA is the only supported asymmetric encryption. */ 707 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \ 708 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) 709 710 /** Sufficient output buffer size for psa_asymmetric_decrypt(). 711 * 712 * This macro returns a sufficient buffer size for a plaintext produced using 713 * a key of the specified type and size, with the specified algorithm. 714 * Note that the actual size of the plaintext may be smaller, depending 715 * on the algorithm. 716 * 717 * \warning This function may call its arguments multiple times or 718 * zero times, so you should not pass arguments that contain 719 * side effects. 720 * 721 * \param key_type An asymmetric key type (this may indifferently be a 722 * key pair type or a public key type). 723 * \param key_bits The size of the key in bits. 724 * \param alg The asymmetric encryption algorithm. 725 * 726 * \return If the parameters are valid and supported, return 727 * a buffer size in bytes that guarantees that 728 * psa_asymmetric_decrypt() will not fail with 729 * #PSA_ERROR_BUFFER_TOO_SMALL. 730 * If the parameters are a valid combination that is not supported, 731 * return either a sensible size or 0. 732 * If the parameters are not valid, the 733 * return value is unspecified. 734 */ 735 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ 736 (PSA_KEY_TYPE_IS_RSA(key_type) ? \ 737 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ 738 0u) 739 740 /** A sufficient output buffer size for psa_asymmetric_decrypt(), for any 741 * supported asymmetric decryption. 742 * 743 * This macro assumes that RSA is the only supported asymmetric encryption. 744 * 745 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). 746 */ 747 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \ 748 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) 749 750 /* Maximum size of the ASN.1 encoding of an INTEGER with the specified 751 * number of bits. 752 * 753 * This definition assumes that bits <= 2^19 - 9 so that the length field 754 * is at most 3 bytes. The length of the encoding is the length of the 755 * bit string padded to a whole number of bytes plus: 756 * - 1 type byte; 757 * - 1 to 3 length bytes; 758 * - 0 to 1 bytes of leading 0 due to the sign bit. 759 */ 760 #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \ 761 ((bits) / 8u + 5u) 762 763 /* Maximum size of the export encoding of an RSA public key. 764 * Assumes that the public exponent is less than 2^32. 765 * 766 * RSAPublicKey ::= SEQUENCE { 767 * modulus INTEGER, -- n 768 * publicExponent INTEGER } -- e 769 * 770 * - 4 bytes of SEQUENCE overhead; 771 * - n : INTEGER; 772 * - 7 bytes for the public exponent. 773 */ 774 #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ 775 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u) 776 777 /* Maximum size of the export encoding of an RSA key pair. 778 * Assumes that the public exponent is less than 2^32 and that the size 779 * difference between the two primes is at most 1 bit. 780 * 781 * RSAPrivateKey ::= SEQUENCE { 782 * version Version, -- 0 783 * modulus INTEGER, -- N-bit 784 * publicExponent INTEGER, -- 32-bit 785 * privateExponent INTEGER, -- N-bit 786 * prime1 INTEGER, -- N/2-bit 787 * prime2 INTEGER, -- N/2-bit 788 * exponent1 INTEGER, -- N/2-bit 789 * exponent2 INTEGER, -- N/2-bit 790 * coefficient INTEGER, -- N/2-bit 791 * } 792 * 793 * - 4 bytes of SEQUENCE overhead; 794 * - 3 bytes of version; 795 * - 7 half-size INTEGERs plus 2 full-size INTEGERs, 796 * overapproximated as 9 half-size INTEGERS; 797 * - 7 bytes for the public exponent. 798 */ 799 #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \ 800 (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u) 801 802 /* Maximum size of the export encoding of a DSA public key. 803 * 804 * SubjectPublicKeyInfo ::= SEQUENCE { 805 * algorithm AlgorithmIdentifier, 806 * subjectPublicKey BIT STRING } -- contains DSAPublicKey 807 * AlgorithmIdentifier ::= SEQUENCE { 808 * algorithm OBJECT IDENTIFIER, 809 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs 810 * DSAPublicKey ::= INTEGER -- public key, Y 811 * 812 * - 3 * 4 bytes of SEQUENCE overhead; 813 * - 1 + 1 + 7 bytes of algorithm (DSA OID); 814 * - 4 bytes of BIT STRING overhead; 815 * - 3 full-size INTEGERs (p, g, y); 816 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits). 817 */ 818 #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ 819 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u) 820 821 /* Maximum size of the export encoding of a DSA key pair. 822 * 823 * DSAPrivateKey ::= SEQUENCE { 824 * version Version, -- 0 825 * prime INTEGER, -- p 826 * subprime INTEGER, -- q 827 * generator INTEGER, -- g 828 * public INTEGER, -- y 829 * private INTEGER, -- x 830 * } 831 * 832 * - 4 bytes of SEQUENCE overhead; 833 * - 3 bytes of version; 834 * - 3 full-size INTEGERs (p, g, y); 835 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits). 836 */ 837 #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \ 838 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u) 839 840 /* Maximum size of the export encoding of an ECC public key. 841 * 842 * The representation of an ECC public key is: 843 * - The byte 0x04; 844 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; 845 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; 846 * - where m is the bit size associated with the curve. 847 * 848 * - 1 byte + 2 * point size. 849 */ 850 #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \ 851 (2u * PSA_BITS_TO_BYTES(key_bits) + 1u) 852 853 /* Maximum size of the export encoding of an ECC key pair. 854 * 855 * An ECC key pair is represented by the secret value. 856 */ 857 #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \ 858 (PSA_BITS_TO_BYTES(key_bits)) 859 860 /* Maximum size of the export encoding of an DH key pair. 861 * 862 * An DH key pair is represented by the secret value. 863 */ 864 #define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \ 865 (PSA_BITS_TO_BYTES(key_bits)) 866 867 /* Maximum size of the export encoding of an DH public key. 868 */ 869 #define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \ 870 (PSA_BITS_TO_BYTES(key_bits)) 871 872 /** Sufficient output buffer size for psa_export_key() or 873 * psa_export_public_key(). 874 * 875 * This macro returns a compile-time constant if its arguments are 876 * compile-time constants. 877 * 878 * \warning This macro may evaluate its arguments multiple times or 879 * zero times, so you should not pass arguments that contain 880 * side effects. 881 * 882 * The following code illustrates how to allocate enough memory to export 883 * a key by querying the key type and size at runtime. 884 * \code{c} 885 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 886 * psa_status_t status; 887 * status = psa_get_key_attributes(key, &attributes); 888 * if (status != PSA_SUCCESS) handle_error(...); 889 * psa_key_type_t key_type = psa_get_key_type(&attributes); 890 * size_t key_bits = psa_get_key_bits(&attributes); 891 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits); 892 * psa_reset_key_attributes(&attributes); 893 * uint8_t *buffer = malloc(buffer_size); 894 * if (buffer == NULL) handle_error(...); 895 * size_t buffer_length; 896 * status = psa_export_key(key, buffer, buffer_size, &buffer_length); 897 * if (status != PSA_SUCCESS) handle_error(...); 898 * \endcode 899 * 900 * \param key_type A supported key type. 901 * \param key_bits The size of the key in bits. 902 * 903 * \return If the parameters are valid and supported, return 904 * a buffer size in bytes that guarantees that 905 * psa_export_key() or psa_export_public_key() will not fail with 906 * #PSA_ERROR_BUFFER_TOO_SMALL. 907 * If the parameters are a valid combination that is not supported, 908 * return either a sensible size or 0. 909 * If the parameters are not valid, the return value is unspecified. 910 */ 911 #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \ 912 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 913 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 914 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \ 915 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 916 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \ 917 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 918 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \ 919 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 920 0u) 921 922 /** Sufficient output buffer size for psa_export_public_key(). 923 * 924 * This macro returns a compile-time constant if its arguments are 925 * compile-time constants. 926 * 927 * \warning This macro may evaluate its arguments multiple times or 928 * zero times, so you should not pass arguments that contain 929 * side effects. 930 * 931 * The following code illustrates how to allocate enough memory to export 932 * a public key by querying the key type and size at runtime. 933 * \code{c} 934 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 935 * psa_status_t status; 936 * status = psa_get_key_attributes(key, &attributes); 937 * if (status != PSA_SUCCESS) handle_error(...); 938 * psa_key_type_t key_type = psa_get_key_type(&attributes); 939 * size_t key_bits = psa_get_key_bits(&attributes); 940 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits); 941 * psa_reset_key_attributes(&attributes); 942 * uint8_t *buffer = malloc(buffer_size); 943 * if (buffer == NULL) handle_error(...); 944 * size_t buffer_length; 945 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length); 946 * if (status != PSA_SUCCESS) handle_error(...); 947 * \endcode 948 * 949 * \param key_type A public key or key pair key type. 950 * \param key_bits The size of the key in bits. 951 * 952 * \return If the parameters are valid and supported, return 953 * a buffer size in bytes that guarantees that 954 * psa_export_public_key() will not fail with 955 * #PSA_ERROR_BUFFER_TOO_SMALL. 956 * If the parameters are a valid combination that is not 957 * supported, return either a sensible size or 0. 958 * If the parameters are not valid, 959 * the return value is unspecified. 960 * 961 * If the parameters are valid and supported, 962 * return the same result as 963 * #PSA_EXPORT_KEY_OUTPUT_SIZE( 964 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type), 965 * \p key_bits). 966 */ 967 #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \ 968 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 969 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 970 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 971 0u) 972 973 /** Sufficient buffer size for exporting any asymmetric key pair. 974 * 975 * This macro expands to a compile-time constant integer. This value is 976 * a sufficient buffer size when calling psa_export_key() to export any 977 * asymmetric key pair, regardless of the exact key type and key size. 978 * 979 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). 980 */ 981 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE 1 982 983 #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) && \ 984 (PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \ 985 PSA_EXPORT_KEY_PAIR_MAX_SIZE) 986 #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 987 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 988 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 989 #endif 990 #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) && \ 991 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ 992 PSA_EXPORT_KEY_PAIR_MAX_SIZE) 993 #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 994 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 995 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 996 #endif 997 #if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_BASIC) && \ 998 (PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \ 999 PSA_EXPORT_KEY_PAIR_MAX_SIZE) 1000 #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 1001 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 1002 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1003 #endif 1004 1005 /** Sufficient buffer size for exporting any asymmetric public key. 1006 * 1007 * This macro expands to a compile-time constant integer. This value is 1008 * a sufficient buffer size when calling psa_export_key() or 1009 * psa_export_public_key() to export any asymmetric public key, 1010 * regardless of the exact key type and key size. 1011 * 1012 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). 1013 */ 1014 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1 1015 1016 #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \ 1017 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \ 1018 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1019 #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1020 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1021 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 1022 #endif 1023 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) && \ 1024 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ 1025 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1026 #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1027 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1028 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 1029 #endif 1030 #if defined(PSA_WANT_KEY_TYPE_DH_PUBLIC_KEY) && \ 1031 (PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \ 1032 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1033 #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1034 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1035 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1036 #endif 1037 1038 /** Sufficient output buffer size for psa_raw_key_agreement(). 1039 * 1040 * This macro returns a compile-time constant if its arguments are 1041 * compile-time constants. 1042 * 1043 * \warning This macro may evaluate its arguments multiple times or 1044 * zero times, so you should not pass arguments that contain 1045 * side effects. 1046 * 1047 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE. 1048 * 1049 * \param key_type A supported key type. 1050 * \param key_bits The size of the key in bits. 1051 * 1052 * \return If the parameters are valid and supported, return 1053 * a buffer size in bytes that guarantees that 1054 * psa_raw_key_agreement() will not fail with 1055 * #PSA_ERROR_BUFFER_TOO_SMALL. 1056 * If the parameters are a valid combination that 1057 * is not supported, return either a sensible size or 0. 1058 * If the parameters are not valid, 1059 * the return value is unspecified. 1060 */ 1061 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \ 1062 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \ 1063 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u) 1064 1065 /** Maximum size of the output from psa_raw_key_agreement(). 1066 * 1067 * This macro expands to a compile-time constant integer. This value is the 1068 * maximum size of the output any raw key agreement algorithm, in bytes. 1069 * 1070 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits). 1071 */ 1072 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1 1073 1074 #if defined(PSA_WANT_ALG_ECDH) && \ 1075 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) 1076 #undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1077 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) 1078 #endif 1079 #if defined(PSA_WANT_ALG_FFDH) && \ 1080 (PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) 1081 #undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1082 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1083 #endif 1084 1085 /** The default IV size for a cipher algorithm, in bytes. 1086 * 1087 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always 1088 * the default IV length for the algorithm. 1089 * 1090 * This macro can be used to allocate a buffer of sufficient size to 1091 * store the IV output from #psa_cipher_generate_iv() when using 1092 * a multi-part cipher operation. 1093 * 1094 * See also #PSA_CIPHER_IV_MAX_SIZE. 1095 * 1096 * \warning This macro may evaluate its arguments multiple times or 1097 * zero times, so you should not pass arguments that contain 1098 * side effects. 1099 * 1100 * \param key_type A symmetric key type that is compatible with algorithm \p alg. 1101 * 1102 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true). 1103 * 1104 * \return The default IV size for the specified key type and algorithm. 1105 * If the algorithm does not use an IV, return 0. 1106 * If the key type or cipher algorithm is not recognized, 1107 * or the parameters are incompatible, return 0. 1108 */ 1109 #define PSA_CIPHER_IV_LENGTH(key_type, alg) \ 1110 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \ 1111 ((alg) == PSA_ALG_CTR || \ 1112 (alg) == PSA_ALG_CFB || \ 1113 (alg) == PSA_ALG_OFB || \ 1114 (alg) == PSA_ALG_XTS || \ 1115 (alg) == PSA_ALG_CBC_NO_PADDING || \ 1116 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 1117 (key_type) == PSA_KEY_TYPE_CHACHA20 && \ 1118 (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \ 1119 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \ 1120 0u) 1121 1122 /** The maximum IV size for all supported cipher algorithms, in bytes. 1123 * 1124 * See also #PSA_CIPHER_IV_LENGTH(). 1125 */ 1126 #define PSA_CIPHER_IV_MAX_SIZE 16u 1127 1128 /** The maximum size of the output of psa_cipher_encrypt(), in bytes. 1129 * 1130 * If the size of the output buffer is at least this large, it is guaranteed 1131 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. 1132 * Depending on the algorithm, the actual size of the output might be smaller. 1133 * 1134 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length). 1135 * 1136 * \warning This macro may evaluate its arguments multiple times or 1137 * zero times, so you should not pass arguments that contain 1138 * side effects. 1139 * 1140 * \param key_type A symmetric key type that is compatible with algorithm 1141 * alg. 1142 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that 1143 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1144 * \param input_length Size of the input in bytes. 1145 * 1146 * \return A sufficient output size for the specified key type and 1147 * algorithm. If the key type or cipher algorithm is not 1148 * recognized, or the parameters are incompatible, 1149 * return 0. 1150 */ 1151 #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ 1152 (alg == PSA_ALG_CBC_PKCS7 ? \ 1153 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \ 1154 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ 1155 (input_length) + 1u) + \ 1156 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \ 1157 (PSA_ALG_IS_CIPHER(alg) ? \ 1158 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ 1159 0u)) 1160 1161 /** A sufficient output buffer size for psa_cipher_encrypt(), for any of the 1162 * supported key types and cipher algorithms. 1163 * 1164 * If the size of the output buffer is at least this large, it is guaranteed 1165 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. 1166 * 1167 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 1168 * 1169 * \param input_length Size of the input in bytes. 1170 * 1171 */ 1172 #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \ 1173 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \ 1174 (input_length) + 1u) + \ 1175 PSA_CIPHER_IV_MAX_SIZE) 1176 1177 /** The maximum size of the output of psa_cipher_decrypt(), in bytes. 1178 * 1179 * If the size of the output buffer is at least this large, it is guaranteed 1180 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. 1181 * Depending on the algorithm, the actual size of the output might be smaller. 1182 * 1183 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length). 1184 * 1185 * \param key_type A symmetric key type that is compatible with algorithm 1186 * alg. 1187 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that 1188 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1189 * \param input_length Size of the input in bytes. 1190 * 1191 * \return A sufficient output size for the specified key type and 1192 * algorithm. If the key type or cipher algorithm is not 1193 * recognized, or the parameters are incompatible, 1194 * return 0. 1195 */ 1196 #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ 1197 (PSA_ALG_IS_CIPHER(alg) && \ 1198 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \ 1199 (input_length) : \ 1200 0u) 1201 1202 /** A sufficient output buffer size for psa_cipher_decrypt(), for any of the 1203 * supported key types and cipher algorithms. 1204 * 1205 * If the size of the output buffer is at least this large, it is guaranteed 1206 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. 1207 * 1208 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 1209 * 1210 * \param input_length Size of the input in bytes. 1211 */ 1212 #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \ 1213 (input_length) 1214 1215 /** A sufficient output buffer size for psa_cipher_update(). 1216 * 1217 * If the size of the output buffer is at least this large, it is guaranteed 1218 * that psa_cipher_update() will not fail due to an insufficient buffer size. 1219 * The actual size of the output might be smaller in any given call. 1220 * 1221 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length). 1222 * 1223 * \param key_type A symmetric key type that is compatible with algorithm 1224 * alg. 1225 * \param alg A cipher algorithm (PSA_ALG_XXX value such that 1226 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1227 * \param input_length Size of the input in bytes. 1228 * 1229 * \return A sufficient output size for the specified key type and 1230 * algorithm. If the key type or cipher algorithm is not 1231 * recognized, or the parameters are incompatible, return 0. 1232 */ 1233 #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ 1234 (PSA_ALG_IS_CIPHER(alg) ? \ 1235 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \ 1236 (((alg) == PSA_ALG_CBC_PKCS7 || \ 1237 (alg) == PSA_ALG_CBC_NO_PADDING || \ 1238 (alg) == PSA_ALG_ECB_NO_PADDING) ? \ 1239 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ 1240 input_length) : \ 1241 (input_length)) : 0u) : \ 1242 0u) 1243 1244 /** A sufficient output buffer size for psa_cipher_update(), for any of the 1245 * supported key types and cipher algorithms. 1246 * 1247 * If the size of the output buffer is at least this large, it is guaranteed 1248 * that psa_cipher_update() will not fail due to an insufficient buffer size. 1249 * 1250 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 1251 * 1252 * \param input_length Size of the input in bytes. 1253 */ 1254 #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \ 1255 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length)) 1256 1257 /** A sufficient ciphertext buffer size for psa_cipher_finish(). 1258 * 1259 * If the size of the ciphertext buffer is at least this large, it is 1260 * guaranteed that psa_cipher_finish() will not fail due to an insufficient 1261 * ciphertext buffer size. The actual size of the output might be smaller in 1262 * any given call. 1263 * 1264 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE(). 1265 * 1266 * \param key_type A symmetric key type that is compatible with algorithm 1267 * alg. 1268 * \param alg A cipher algorithm (PSA_ALG_XXX value such that 1269 * #PSA_ALG_IS_CIPHER(\p alg) is true). 1270 * \return A sufficient output size for the specified key type and 1271 * algorithm. If the key type or cipher algorithm is not 1272 * recognized, or the parameters are incompatible, return 0. 1273 */ 1274 #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \ 1275 (PSA_ALG_IS_CIPHER(alg) ? \ 1276 (alg == PSA_ALG_CBC_PKCS7 ? \ 1277 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 1278 0u) : \ 1279 0u) 1280 1281 /** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the 1282 * supported key types and cipher algorithms. 1283 * 1284 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg). 1285 */ 1286 #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \ 1287 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 1288 1289 #endif /* PSA_CRYPTO_SIZES_H */ 1290