132b31808SJens Wiklander /** 232b31808SJens Wiklander * \file psa/crypto_sizes.h 332b31808SJens Wiklander * 432b31808SJens Wiklander * \brief PSA cryptography module: Mbed TLS buffer size macros 532b31808SJens Wiklander * 632b31808SJens Wiklander * \note This file may not be included directly. Applications must 732b31808SJens Wiklander * include psa/crypto.h. 832b31808SJens Wiklander * 932b31808SJens Wiklander * This file contains the definitions of macros that are useful to 1032b31808SJens Wiklander * compute buffer sizes. The signatures and semantics of these macros 1132b31808SJens Wiklander * are standardized, but the definitions are not, because they depend on 1232b31808SJens Wiklander * the available algorithms and, in some cases, on permitted tolerances 1332b31808SJens Wiklander * on buffer sizes. 1432b31808SJens Wiklander * 1532b31808SJens Wiklander * In implementations with isolation between the application and the 1632b31808SJens Wiklander * cryptography module, implementers should take care to ensure that 1732b31808SJens Wiklander * the definitions that are exposed to applications match what the 1832b31808SJens Wiklander * module implements. 1932b31808SJens Wiklander * 2032b31808SJens Wiklander * Macros that compute sizes whose values do not depend on the 2132b31808SJens Wiklander * implementation are in crypto.h. 2232b31808SJens Wiklander */ 2332b31808SJens Wiklander /* 2432b31808SJens Wiklander * Copyright The Mbed TLS Contributors 25b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2632b31808SJens Wiklander */ 2732b31808SJens Wiklander 2832b31808SJens Wiklander #ifndef PSA_CRYPTO_SIZES_H 2932b31808SJens Wiklander #define PSA_CRYPTO_SIZES_H 3032b31808SJens Wiklander 31b0563631STom Van Eyck /* 32b0563631STom Van Eyck * Include the build-time configuration information header. Here, we do not 33b0563631STom Van Eyck * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which 34b0563631STom Van Eyck * is basically just an alias to it. This is to ease the maintenance of the 35b0563631STom Van Eyck * TF-PSA-Crypto repository which has a different build system and 36b0563631STom Van Eyck * configuration. 37b0563631STom Van Eyck */ 38b0563631STom Van Eyck #include "psa/build_info.h" 3932b31808SJens Wiklander 40b0563631STom Van Eyck #define PSA_BITS_TO_BYTES(bits) (((bits) + 7u) / 8u) 41b0563631STom Van Eyck #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8u) 42b0563631STom Van Eyck #define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \ 43b0563631STom Van Eyck (c) : (b) : (a) <= (c) ? (c) : (a)) 4432b31808SJens Wiklander 4532b31808SJens Wiklander #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \ 4632b31808SJens Wiklander (((length) + (block_size) - 1) / (block_size) * (block_size)) 4732b31808SJens Wiklander 4832b31808SJens Wiklander /** The size of the output of psa_hash_finish(), in bytes. 4932b31808SJens Wiklander * 5032b31808SJens Wiklander * This is also the hash size that psa_hash_verify() expects. 5132b31808SJens Wiklander * 5232b31808SJens Wiklander * \param alg A hash algorithm (\c PSA_ALG_XXX value such that 5332b31808SJens Wiklander * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm 5432b31808SJens Wiklander * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a 5532b31808SJens Wiklander * hash algorithm). 5632b31808SJens Wiklander * 5732b31808SJens Wiklander * \return The hash size for the specified hash algorithm. 5832b31808SJens Wiklander * If the hash algorithm is not recognized, return 0. 5932b31808SJens Wiklander */ 6032b31808SJens Wiklander #define PSA_HASH_LENGTH(alg) \ 6132b31808SJens Wiklander ( \ 62b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16u : \ 63b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20u : \ 64b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20u : \ 65b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28u : \ 66b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32u : \ 67b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48u : \ 68b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64u : \ 69b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28u : \ 70b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32u : \ 71b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28u : \ 72b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32u : \ 73b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48u : \ 74b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64u : \ 75b0563631STom Van Eyck 0u) 7632b31808SJens Wiklander 7732b31808SJens Wiklander /** The input block size of a hash algorithm, in bytes. 7832b31808SJens Wiklander * 7932b31808SJens Wiklander * Hash algorithms process their input data in blocks. Hash operations will 8032b31808SJens Wiklander * retain any partial blocks until they have enough input to fill the block or 8132b31808SJens Wiklander * until the operation is finished. 8232b31808SJens Wiklander * This affects the output from psa_hash_suspend(). 8332b31808SJens Wiklander * 8432b31808SJens Wiklander * \param alg A hash algorithm (\c PSA_ALG_XXX value such that 8532b31808SJens Wiklander * PSA_ALG_IS_HASH(\p alg) is true). 8632b31808SJens Wiklander * 8732b31808SJens Wiklander * \return The block size in bytes for the specified hash algorithm. 8832b31808SJens Wiklander * If the hash algorithm is not recognized, return 0. 8932b31808SJens Wiklander * An implementation can return either 0 or the correct size for a 9032b31808SJens Wiklander * hash algorithm that it recognizes, but does not support. 9132b31808SJens Wiklander */ 9232b31808SJens Wiklander #define PSA_HASH_BLOCK_LENGTH(alg) \ 9332b31808SJens Wiklander ( \ 94b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64u : \ 95b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64u : \ 96b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64u : \ 97b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64u : \ 98b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64u : \ 99b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128u : \ 100b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128u : \ 101b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128u : \ 102b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128u : \ 103b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144u : \ 104b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136u : \ 105b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104u : \ 106b0563631STom Van Eyck PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72u : \ 107b0563631STom Van Eyck 0u) 10832b31808SJens Wiklander 10932b31808SJens Wiklander /** \def PSA_HASH_MAX_SIZE 11032b31808SJens Wiklander * 11132b31808SJens Wiklander * Maximum size of a hash. 11232b31808SJens Wiklander * 11332b31808SJens Wiklander * This macro expands to a compile-time constant integer. This value 11432b31808SJens Wiklander * is the maximum size of a hash in bytes. 11532b31808SJens Wiklander */ 116b0563631STom Van Eyck /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224, 11732b31808SJens Wiklander * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for 11832b31808SJens Wiklander * HMAC-SHA3-512. */ 119b0563631STom Van Eyck /* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE, 120b0563631STom Van Eyck * see the note on MBEDTLS_MD_MAX_SIZE for details. */ 121b0563631STom Van Eyck #if defined(PSA_WANT_ALG_SHA3_224) 122b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u 123b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA3_256) 124b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 136u 125b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_512) 126b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u 127b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_384) 128b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u 129b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA3_384) 130b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 104u 131b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA3_512) 132b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 72u 133b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_256) 134b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 135b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_224) 136b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 137b0563631STom Van Eyck #else /* SHA-1 or smaller */ 138b0563631STom Van Eyck #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u 139b0563631STom Van Eyck #endif 140b0563631STom Van Eyck 141b0563631STom Van Eyck #if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512) 142b0563631STom Van Eyck #define PSA_HASH_MAX_SIZE 64u 143b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384) 144b0563631STom Van Eyck #define PSA_HASH_MAX_SIZE 48u 145b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256) 146b0563631STom Van Eyck #define PSA_HASH_MAX_SIZE 32u 147b0563631STom Van Eyck #elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224) 148b0563631STom Van Eyck #define PSA_HASH_MAX_SIZE 28u 149b0563631STom Van Eyck #else /* SHA-1 or smaller */ 150b0563631STom Van Eyck #define PSA_HASH_MAX_SIZE 20u 15132b31808SJens Wiklander #endif 15232b31808SJens Wiklander 15332b31808SJens Wiklander /** \def PSA_MAC_MAX_SIZE 15432b31808SJens Wiklander * 15532b31808SJens Wiklander * Maximum size of a MAC. 15632b31808SJens Wiklander * 15732b31808SJens Wiklander * This macro expands to a compile-time constant integer. This value 15832b31808SJens Wiklander * is the maximum size of a MAC in bytes. 15932b31808SJens Wiklander */ 16032b31808SJens Wiklander /* All non-HMAC MACs have a maximum size that's smaller than the 16132b31808SJens Wiklander * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */ 16232b31808SJens Wiklander /* Note that the encoding of truncated MAC algorithms limits this value 16332b31808SJens Wiklander * to 64 bytes. 16432b31808SJens Wiklander */ 16532b31808SJens Wiklander #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE 16632b31808SJens Wiklander 16732b31808SJens Wiklander /** The length of a tag for an AEAD algorithm, in bytes. 16832b31808SJens Wiklander * 16932b31808SJens Wiklander * This macro can be used to allocate a buffer of sufficient size to store the 17032b31808SJens Wiklander * tag output from psa_aead_finish(). 17132b31808SJens Wiklander * 17232b31808SJens Wiklander * See also #PSA_AEAD_TAG_MAX_SIZE. 17332b31808SJens Wiklander * 17432b31808SJens Wiklander * \param key_type The type of the AEAD key. 17532b31808SJens Wiklander * \param key_bits The size of the AEAD key in bits. 17632b31808SJens Wiklander * \param alg An AEAD algorithm 17732b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 17832b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 17932b31808SJens Wiklander * 18032b31808SJens Wiklander * \return The tag length for the specified algorithm and key. 18132b31808SJens Wiklander * If the AEAD algorithm does not have an identified 18232b31808SJens Wiklander * tag that can be distinguished from the rest of 18332b31808SJens Wiklander * the ciphertext, return 0. 18432b31808SJens Wiklander * If the key type or AEAD algorithm is not 18532b31808SJens Wiklander * recognized, or the parameters are incompatible, 18632b31808SJens Wiklander * return 0. 18732b31808SJens Wiklander */ 18832b31808SJens Wiklander #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \ 18932b31808SJens Wiklander (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 19032b31808SJens Wiklander PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 191b0563631STom Van Eyck ((void) (key_bits), 0u)) 19232b31808SJens Wiklander 19332b31808SJens Wiklander /** The maximum tag size for all supported AEAD algorithms, in bytes. 19432b31808SJens Wiklander * 19532b31808SJens Wiklander * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg). 19632b31808SJens Wiklander */ 197b0563631STom Van Eyck #define PSA_AEAD_TAG_MAX_SIZE 16u 19832b31808SJens Wiklander 19932b31808SJens Wiklander /* The maximum size of an RSA key on this implementation, in bits. 20032b31808SJens Wiklander * This is a vendor-specific macro. 20132b31808SJens Wiklander * 20232b31808SJens Wiklander * Mbed TLS does not set a hard limit on the size of RSA keys: any key 20332b31808SJens Wiklander * whose parameters fit in a bignum is accepted. However large keys can 20432b31808SJens Wiklander * induce a large memory usage and long computation times. Unlike other 20532b31808SJens Wiklander * auxiliary macros in this file and in crypto.h, which reflect how the 20632b31808SJens Wiklander * library is configured, this macro defines how the library is 20732b31808SJens Wiklander * configured. This implementation refuses to import or generate an 20832b31808SJens Wiklander * RSA key whose size is larger than the value defined here. 20932b31808SJens Wiklander * 21032b31808SJens Wiklander * Note that an implementation may set different size limits for different 21132b31808SJens Wiklander * operations, and does not need to accept all key sizes up to the limit. */ 212b0563631STom Van Eyck #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u 213b0563631STom Van Eyck 214b0563631STom Van Eyck /* The minimum size of an RSA key on this implementation, in bits. 215b0563631STom Van Eyck * This is a vendor-specific macro. 216b0563631STom Van Eyck * 217b0563631STom Van Eyck * Limits RSA key generation to a minimum due to avoid accidental misuse. 218b0563631STom Van Eyck * This value cannot be less than 128 bits. 219b0563631STom Van Eyck */ 220b0563631STom Van Eyck #if defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS) 221b0563631STom Van Eyck #define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS MBEDTLS_RSA_GEN_KEY_MIN_BITS 222b0563631STom Van Eyck #else 223b0563631STom Van Eyck #define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS 1024 224b0563631STom Van Eyck #endif 225b0563631STom Van Eyck 226b0563631STom Van Eyck /* The maximum size of an DH key on this implementation, in bits. 227b0563631STom Van Eyck * This is a vendor-specific macro.*/ 228b0563631STom Van Eyck #if defined(PSA_WANT_DH_RFC7919_8192) 229b0563631STom Van Eyck #define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u 230b0563631STom Van Eyck #elif defined(PSA_WANT_DH_RFC7919_6144) 231b0563631STom Van Eyck #define PSA_VENDOR_FFDH_MAX_KEY_BITS 6144u 232b0563631STom Van Eyck #elif defined(PSA_WANT_DH_RFC7919_4096) 233b0563631STom Van Eyck #define PSA_VENDOR_FFDH_MAX_KEY_BITS 4096u 234b0563631STom Van Eyck #elif defined(PSA_WANT_DH_RFC7919_3072) 235b0563631STom Van Eyck #define PSA_VENDOR_FFDH_MAX_KEY_BITS 3072u 236b0563631STom Van Eyck #elif defined(PSA_WANT_DH_RFC7919_2048) 237b0563631STom Van Eyck #define PSA_VENDOR_FFDH_MAX_KEY_BITS 2048u 238b0563631STom Van Eyck #else 239b0563631STom Van Eyck #define PSA_VENDOR_FFDH_MAX_KEY_BITS 0u 240b0563631STom Van Eyck #endif 24132b31808SJens Wiklander 24232b31808SJens Wiklander /* The maximum size of an ECC key on this implementation, in bits. 24332b31808SJens Wiklander * This is a vendor-specific macro. */ 244b0563631STom Van Eyck #if defined(PSA_WANT_ECC_SECP_R1_521) 245b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u 246b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) 247b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u 248b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_MONTGOMERY_448) 249b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u 250b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_R1_384) 251b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u 252b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) 253b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u 254b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_R1_256) 255b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 256b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_K1_256) 257b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 258b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) 259b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u 260b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_MONTGOMERY_255) 261b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u 262b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_R1_224) 263b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u 264b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_K1_224) 265b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u 266b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_R1_192) 267b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u 268b0563631STom Van Eyck #elif defined(PSA_WANT_ECC_SECP_K1_192) 269b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u 27032b31808SJens Wiklander #else 271b0563631STom Van Eyck #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u 27232b31808SJens Wiklander #endif 27332b31808SJens Wiklander 27432b31808SJens Wiklander /** This macro returns the maximum supported length of the PSK for the 27532b31808SJens Wiklander * TLS-1.2 PSK-to-MS key derivation 27632b31808SJens Wiklander * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)). 27732b31808SJens Wiklander * 27832b31808SJens Wiklander * The maximum supported length does not depend on the chosen hash algorithm. 27932b31808SJens Wiklander * 28032b31808SJens Wiklander * Quoting RFC 4279, Sect 5.3: 28132b31808SJens Wiklander * TLS implementations supporting these ciphersuites MUST support 28232b31808SJens Wiklander * arbitrary PSK identities up to 128 octets in length, and arbitrary 28332b31808SJens Wiklander * PSKs up to 64 octets in length. Supporting longer identities and 28432b31808SJens Wiklander * keys is RECOMMENDED. 28532b31808SJens Wiklander * 28632b31808SJens Wiklander * Therefore, no implementation should define a value smaller than 64 28732b31808SJens Wiklander * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE. 28832b31808SJens Wiklander */ 289b0563631STom Van Eyck #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u 29032b31808SJens Wiklander 29132b31808SJens Wiklander /* The expected size of input passed to psa_tls12_ecjpake_to_pms_input, 29232b31808SJens Wiklander * which is expected to work with P-256 curve only. */ 293b0563631STom Van Eyck #define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u 29432b31808SJens Wiklander 29532b31808SJens Wiklander /* The size of a serialized K.X coordinate to be used in 29632b31808SJens Wiklander * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256 29732b31808SJens Wiklander * curve. */ 298b0563631STom Van Eyck #define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u 299b0563631STom Van Eyck 300b0563631STom Van Eyck /* The maximum number of iterations for PBKDF2 on this implementation, in bits. 301b0563631STom Van Eyck * This is a vendor-specific macro. This can be configured if necessary */ 302b0563631STom Van Eyck #define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU 30332b31808SJens Wiklander 30432b31808SJens Wiklander /** The maximum size of a block cipher. */ 305b0563631STom Van Eyck #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u 30632b31808SJens Wiklander 30732b31808SJens Wiklander /** The size of the output of psa_mac_sign_finish(), in bytes. 30832b31808SJens Wiklander * 30932b31808SJens Wiklander * This is also the MAC size that psa_mac_verify_finish() expects. 31032b31808SJens Wiklander * 31132b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 31232b31808SJens Wiklander * zero times, so you should not pass arguments that contain 31332b31808SJens Wiklander * side effects. 31432b31808SJens Wiklander * 31532b31808SJens Wiklander * \param key_type The type of the MAC key. 31632b31808SJens Wiklander * \param key_bits The size of the MAC key in bits. 31732b31808SJens Wiklander * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that 31832b31808SJens Wiklander * #PSA_ALG_IS_MAC(\p alg) is true). 31932b31808SJens Wiklander * 32032b31808SJens Wiklander * \return The MAC size for the specified algorithm with 32132b31808SJens Wiklander * the specified key parameters. 32232b31808SJens Wiklander * \return 0 if the MAC algorithm is not recognized. 32332b31808SJens Wiklander * \return Either 0 or the correct size for a MAC algorithm that 32432b31808SJens Wiklander * the implementation recognizes, but does not support. 32532b31808SJens Wiklander * \return Unspecified if the key parameters are not consistent 32632b31808SJens Wiklander * with the algorithm. 32732b31808SJens Wiklander */ 32832b31808SJens Wiklander #define PSA_MAC_LENGTH(key_type, key_bits, alg) \ 32932b31808SJens Wiklander ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \ 33032b31808SJens Wiklander PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \ 33132b31808SJens Wiklander PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 332b0563631STom Van Eyck ((void) (key_type), (void) (key_bits), 0u)) 33332b31808SJens Wiklander 33432b31808SJens Wiklander /** The maximum size of the output of psa_aead_encrypt(), in bytes. 33532b31808SJens Wiklander * 33632b31808SJens Wiklander * If the size of the ciphertext buffer is at least this large, it is 33732b31808SJens Wiklander * guaranteed that psa_aead_encrypt() will not fail due to an 33832b31808SJens Wiklander * insufficient buffer size. Depending on the algorithm, the actual size of 33932b31808SJens Wiklander * the ciphertext may be smaller. 34032b31808SJens Wiklander * 34132b31808SJens Wiklander * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length). 34232b31808SJens Wiklander * 34332b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 34432b31808SJens Wiklander * zero times, so you should not pass arguments that contain 34532b31808SJens Wiklander * side effects. 34632b31808SJens Wiklander * 34732b31808SJens Wiklander * \param key_type A symmetric key type that is 34832b31808SJens Wiklander * compatible with algorithm \p alg. 34932b31808SJens Wiklander * \param alg An AEAD algorithm 35032b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 35132b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 35232b31808SJens Wiklander * \param plaintext_length Size of the plaintext in bytes. 35332b31808SJens Wiklander * 35432b31808SJens Wiklander * \return The AEAD ciphertext size for the specified 35532b31808SJens Wiklander * algorithm. 35632b31808SJens Wiklander * If the key type or AEAD algorithm is not 35732b31808SJens Wiklander * recognized, or the parameters are incompatible, 35832b31808SJens Wiklander * return 0. 35932b31808SJens Wiklander */ 36032b31808SJens Wiklander #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \ 36132b31808SJens Wiklander (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 36232b31808SJens Wiklander (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 363b0563631STom Van Eyck 0u) 36432b31808SJens Wiklander 36532b31808SJens Wiklander /** A sufficient output buffer size for psa_aead_encrypt(), for any of the 36632b31808SJens Wiklander * supported key types and AEAD algorithms. 36732b31808SJens Wiklander * 36832b31808SJens Wiklander * If the size of the ciphertext buffer is at least this large, it is guaranteed 36932b31808SJens Wiklander * that psa_aead_encrypt() will not fail due to an insufficient buffer size. 37032b31808SJens Wiklander * 37132b31808SJens Wiklander * \note This macro returns a compile-time constant if its arguments are 37232b31808SJens Wiklander * compile-time constants. 37332b31808SJens Wiklander * 37432b31808SJens Wiklander * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, 37532b31808SJens Wiklander * \p plaintext_length). 37632b31808SJens Wiklander * 37732b31808SJens Wiklander * \param plaintext_length Size of the plaintext in bytes. 37832b31808SJens Wiklander * 37932b31808SJens Wiklander * \return A sufficient output buffer size for any of the 38032b31808SJens Wiklander * supported key types and AEAD algorithms. 38132b31808SJens Wiklander * 38232b31808SJens Wiklander */ 38332b31808SJens Wiklander #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \ 38432b31808SJens Wiklander ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE) 38532b31808SJens Wiklander 38632b31808SJens Wiklander 38732b31808SJens Wiklander /** The maximum size of the output of psa_aead_decrypt(), in bytes. 38832b31808SJens Wiklander * 38932b31808SJens Wiklander * If the size of the plaintext buffer is at least this large, it is 39032b31808SJens Wiklander * guaranteed that psa_aead_decrypt() will not fail due to an 39132b31808SJens Wiklander * insufficient buffer size. Depending on the algorithm, the actual size of 39232b31808SJens Wiklander * the plaintext may be smaller. 39332b31808SJens Wiklander * 39432b31808SJens Wiklander * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length). 39532b31808SJens Wiklander * 39632b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 39732b31808SJens Wiklander * zero times, so you should not pass arguments that contain 39832b31808SJens Wiklander * side effects. 39932b31808SJens Wiklander * 40032b31808SJens Wiklander * \param key_type A symmetric key type that is 40132b31808SJens Wiklander * compatible with algorithm \p alg. 40232b31808SJens Wiklander * \param alg An AEAD algorithm 40332b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 40432b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 40532b31808SJens Wiklander * \param ciphertext_length Size of the plaintext in bytes. 40632b31808SJens Wiklander * 40732b31808SJens Wiklander * \return The AEAD ciphertext size for the specified 40832b31808SJens Wiklander * algorithm. 40932b31808SJens Wiklander * If the key type or AEAD algorithm is not 41032b31808SJens Wiklander * recognized, or the parameters are incompatible, 41132b31808SJens Wiklander * return 0. 41232b31808SJens Wiklander */ 41332b31808SJens Wiklander #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \ 41432b31808SJens Wiklander (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 41532b31808SJens Wiklander (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \ 41632b31808SJens Wiklander (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 417b0563631STom Van Eyck 0u) 41832b31808SJens Wiklander 41932b31808SJens Wiklander /** A sufficient output buffer size for psa_aead_decrypt(), for any of the 42032b31808SJens Wiklander * supported key types and AEAD algorithms. 42132b31808SJens Wiklander * 42232b31808SJens Wiklander * If the size of the plaintext buffer is at least this large, it is guaranteed 42332b31808SJens Wiklander * that psa_aead_decrypt() will not fail due to an insufficient buffer size. 42432b31808SJens Wiklander * 42532b31808SJens Wiklander * \note This macro returns a compile-time constant if its arguments are 42632b31808SJens Wiklander * compile-time constants. 42732b31808SJens Wiklander * 42832b31808SJens Wiklander * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, 42932b31808SJens Wiklander * \p ciphertext_length). 43032b31808SJens Wiklander * 43132b31808SJens Wiklander * \param ciphertext_length Size of the ciphertext in bytes. 43232b31808SJens Wiklander * 43332b31808SJens Wiklander * \return A sufficient output buffer size for any of the 43432b31808SJens Wiklander * supported key types and AEAD algorithms. 43532b31808SJens Wiklander * 43632b31808SJens Wiklander */ 43732b31808SJens Wiklander #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \ 43832b31808SJens Wiklander (ciphertext_length) 43932b31808SJens Wiklander 44032b31808SJens Wiklander /** The default nonce size for an AEAD algorithm, in bytes. 44132b31808SJens Wiklander * 44232b31808SJens Wiklander * This macro can be used to allocate a buffer of sufficient size to 44332b31808SJens Wiklander * store the nonce output from #psa_aead_generate_nonce(). 44432b31808SJens Wiklander * 44532b31808SJens Wiklander * See also #PSA_AEAD_NONCE_MAX_SIZE. 44632b31808SJens Wiklander * 44732b31808SJens Wiklander * \note This is not the maximum size of nonce supported as input to 44832b31808SJens Wiklander * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), 44932b31808SJens Wiklander * just the default size that is generated by #psa_aead_generate_nonce(). 45032b31808SJens Wiklander * 45132b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 45232b31808SJens Wiklander * zero times, so you should not pass arguments that contain 45332b31808SJens Wiklander * side effects. 45432b31808SJens Wiklander * 45532b31808SJens Wiklander * \param key_type A symmetric key type that is compatible with 45632b31808SJens Wiklander * algorithm \p alg. 45732b31808SJens Wiklander * 45832b31808SJens Wiklander * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that 45932b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 46032b31808SJens Wiklander * 46132b31808SJens Wiklander * \return The default nonce size for the specified key type and algorithm. 46232b31808SJens Wiklander * If the key type or AEAD algorithm is not recognized, 46332b31808SJens Wiklander * or the parameters are incompatible, return 0. 46432b31808SJens Wiklander */ 46532b31808SJens Wiklander #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ 46632b31808SJens Wiklander (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \ 467b0563631STom Van Eyck MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \ 468b0563631STom Van Eyck MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \ 469b0563631STom Van Eyck 0u : \ 47032b31808SJens Wiklander (key_type) == PSA_KEY_TYPE_CHACHA20 && \ 471b0563631STom Van Eyck MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \ 472b0563631STom Van Eyck 0u) 47332b31808SJens Wiklander 47432b31808SJens Wiklander /** The maximum default nonce size among all supported pairs of key types and 47532b31808SJens Wiklander * AEAD algorithms, in bytes. 47632b31808SJens Wiklander * 47732b31808SJens Wiklander * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH() 47832b31808SJens Wiklander * may return. 47932b31808SJens Wiklander * 48032b31808SJens Wiklander * \note This is not the maximum size of nonce supported as input to 48132b31808SJens Wiklander * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), 48232b31808SJens Wiklander * just the largest size that may be generated by 48332b31808SJens Wiklander * #psa_aead_generate_nonce(). 48432b31808SJens Wiklander */ 485b0563631STom Van Eyck #define PSA_AEAD_NONCE_MAX_SIZE 13u 48632b31808SJens Wiklander 48732b31808SJens Wiklander /** A sufficient output buffer size for psa_aead_update(). 48832b31808SJens Wiklander * 48932b31808SJens Wiklander * If the size of the output buffer is at least this large, it is 49032b31808SJens Wiklander * guaranteed that psa_aead_update() will not fail due to an 49132b31808SJens Wiklander * insufficient buffer size. The actual size of the output may be smaller 49232b31808SJens Wiklander * in any given call. 49332b31808SJens Wiklander * 49432b31808SJens Wiklander * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length). 49532b31808SJens Wiklander * 49632b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 49732b31808SJens Wiklander * zero times, so you should not pass arguments that contain 49832b31808SJens Wiklander * side effects. 49932b31808SJens Wiklander * 50032b31808SJens Wiklander * \param key_type A symmetric key type that is 50132b31808SJens Wiklander * compatible with algorithm \p alg. 50232b31808SJens Wiklander * \param alg An AEAD algorithm 50332b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 50432b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 50532b31808SJens Wiklander * \param input_length Size of the input in bytes. 50632b31808SJens Wiklander * 50732b31808SJens Wiklander * \return A sufficient output buffer size for the specified 50832b31808SJens Wiklander * algorithm. 50932b31808SJens Wiklander * If the key type or AEAD algorithm is not 51032b31808SJens Wiklander * recognized, or the parameters are incompatible, 51132b31808SJens Wiklander * return 0. 51232b31808SJens Wiklander */ 51332b31808SJens Wiklander /* For all the AEAD modes defined in this specification, it is possible 51432b31808SJens Wiklander * to emit output without delay. However, hardware may not always be 51532b31808SJens Wiklander * capable of this. So for modes based on a block cipher, allow the 51632b31808SJens Wiklander * implementation to delay the output until it has a full block. */ 51732b31808SJens Wiklander #define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ 51832b31808SJens Wiklander (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ 51932b31808SJens Wiklander PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 52032b31808SJens Wiklander PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \ 52132b31808SJens Wiklander (input_length) : \ 522b0563631STom Van Eyck 0u) 52332b31808SJens Wiklander 52432b31808SJens Wiklander /** A sufficient output buffer size for psa_aead_update(), for any of the 52532b31808SJens Wiklander * supported key types and AEAD algorithms. 52632b31808SJens Wiklander * 52732b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 52832b31808SJens Wiklander * that psa_aead_update() will not fail due to an insufficient buffer size. 52932b31808SJens Wiklander * 53032b31808SJens Wiklander * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 53132b31808SJens Wiklander * 53232b31808SJens Wiklander * \param input_length Size of the input in bytes. 53332b31808SJens Wiklander */ 53432b31808SJens Wiklander #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \ 53532b31808SJens Wiklander (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length))) 53632b31808SJens Wiklander 53732b31808SJens Wiklander /** A sufficient ciphertext buffer size for psa_aead_finish(). 53832b31808SJens Wiklander * 53932b31808SJens Wiklander * If the size of the ciphertext buffer is at least this large, it is 54032b31808SJens Wiklander * guaranteed that psa_aead_finish() will not fail due to an 54132b31808SJens Wiklander * insufficient ciphertext buffer size. The actual size of the output may 54232b31808SJens Wiklander * be smaller in any given call. 54332b31808SJens Wiklander * 54432b31808SJens Wiklander * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE. 54532b31808SJens Wiklander * 54632b31808SJens Wiklander * \param key_type A symmetric key type that is 54732b31808SJens Wiklander compatible with algorithm \p alg. 54832b31808SJens Wiklander * \param alg An AEAD algorithm 54932b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 55032b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 55132b31808SJens Wiklander * 55232b31808SJens Wiklander * \return A sufficient ciphertext buffer size for the 55332b31808SJens Wiklander * specified algorithm. 55432b31808SJens Wiklander * If the key type or AEAD algorithm is not 55532b31808SJens Wiklander * recognized, or the parameters are incompatible, 55632b31808SJens Wiklander * return 0. 55732b31808SJens Wiklander */ 55832b31808SJens Wiklander #define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \ 55932b31808SJens Wiklander (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 56032b31808SJens Wiklander PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 56132b31808SJens Wiklander PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 562b0563631STom Van Eyck 0u) 56332b31808SJens Wiklander 56432b31808SJens Wiklander /** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the 56532b31808SJens Wiklander * supported key types and AEAD algorithms. 56632b31808SJens Wiklander * 56732b31808SJens Wiklander * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg). 56832b31808SJens Wiklander */ 56932b31808SJens Wiklander #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 57032b31808SJens Wiklander 57132b31808SJens Wiklander /** A sufficient plaintext buffer size for psa_aead_verify(). 57232b31808SJens Wiklander * 57332b31808SJens Wiklander * If the size of the plaintext buffer is at least this large, it is 57432b31808SJens Wiklander * guaranteed that psa_aead_verify() will not fail due to an 57532b31808SJens Wiklander * insufficient plaintext buffer size. The actual size of the output may 57632b31808SJens Wiklander * be smaller in any given call. 57732b31808SJens Wiklander * 57832b31808SJens Wiklander * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE. 57932b31808SJens Wiklander * 58032b31808SJens Wiklander * \param key_type A symmetric key type that is 58132b31808SJens Wiklander * compatible with algorithm \p alg. 58232b31808SJens Wiklander * \param alg An AEAD algorithm 58332b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 58432b31808SJens Wiklander * #PSA_ALG_IS_AEAD(\p alg) is true). 58532b31808SJens Wiklander * 58632b31808SJens Wiklander * \return A sufficient plaintext buffer size for the 58732b31808SJens Wiklander * specified algorithm. 58832b31808SJens Wiklander * If the key type or AEAD algorithm is not 58932b31808SJens Wiklander * recognized, or the parameters are incompatible, 59032b31808SJens Wiklander * return 0. 59132b31808SJens Wiklander */ 59232b31808SJens Wiklander #define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \ 59332b31808SJens Wiklander (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ 59432b31808SJens Wiklander PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ 59532b31808SJens Wiklander PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 596b0563631STom Van Eyck 0u) 59732b31808SJens Wiklander 59832b31808SJens Wiklander /** A sufficient plaintext buffer size for psa_aead_verify(), for any of the 59932b31808SJens Wiklander * supported key types and AEAD algorithms. 60032b31808SJens Wiklander * 60132b31808SJens Wiklander * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg). 60232b31808SJens Wiklander */ 60332b31808SJens Wiklander #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 60432b31808SJens Wiklander 60532b31808SJens Wiklander #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ 60632b31808SJens Wiklander (PSA_ALG_IS_RSA_OAEP(alg) ? \ 607b0563631STom Van Eyck 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \ 608b0563631STom Van Eyck 11u /*PKCS#1v1.5*/) 60932b31808SJens Wiklander 61032b31808SJens Wiklander /** 61132b31808SJens Wiklander * \brief ECDSA signature size for a given curve bit size 61232b31808SJens Wiklander * 61332b31808SJens Wiklander * \param curve_bits Curve size in bits. 61432b31808SJens Wiklander * \return Signature size in bytes. 61532b31808SJens Wiklander * 61632b31808SJens Wiklander * \note This macro returns a compile-time constant if its argument is one. 61732b31808SJens Wiklander */ 61832b31808SJens Wiklander #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ 619b0563631STom Van Eyck (PSA_BITS_TO_BYTES(curve_bits) * 2u) 62032b31808SJens Wiklander 62132b31808SJens Wiklander /** Sufficient signature buffer size for psa_sign_hash(). 62232b31808SJens Wiklander * 62332b31808SJens Wiklander * This macro returns a sufficient buffer size for a signature using a key 62432b31808SJens Wiklander * of the specified type and size, with the specified algorithm. 62532b31808SJens Wiklander * Note that the actual size of the signature may be smaller 62632b31808SJens Wiklander * (some algorithms produce a variable-size signature). 62732b31808SJens Wiklander * 62832b31808SJens Wiklander * \warning This function may call its arguments multiple times or 62932b31808SJens Wiklander * zero times, so you should not pass arguments that contain 63032b31808SJens Wiklander * side effects. 63132b31808SJens Wiklander * 63232b31808SJens Wiklander * \param key_type An asymmetric key type (this may indifferently be a 63332b31808SJens Wiklander * key pair type or a public key type). 63432b31808SJens Wiklander * \param key_bits The size of the key in bits. 63532b31808SJens Wiklander * \param alg The signature algorithm. 63632b31808SJens Wiklander * 63732b31808SJens Wiklander * \return If the parameters are valid and supported, return 63832b31808SJens Wiklander * a buffer size in bytes that guarantees that 63932b31808SJens Wiklander * psa_sign_hash() will not fail with 64032b31808SJens Wiklander * #PSA_ERROR_BUFFER_TOO_SMALL. 64132b31808SJens Wiklander * If the parameters are a valid combination that is not supported, 64232b31808SJens Wiklander * return either a sensible size or 0. 64332b31808SJens Wiklander * If the parameters are not valid, the 64432b31808SJens Wiklander * return value is unspecified. 64532b31808SJens Wiklander */ 64632b31808SJens Wiklander #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ 64732b31808SJens Wiklander (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \ 64832b31808SJens Wiklander PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ 649b0563631STom Van Eyck ((void) alg, 0u)) 65032b31808SJens Wiklander 65132b31808SJens Wiklander #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \ 65232b31808SJens Wiklander PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 65332b31808SJens Wiklander 65432b31808SJens Wiklander /** \def PSA_SIGNATURE_MAX_SIZE 65532b31808SJens Wiklander * 65632b31808SJens Wiklander * Maximum size of an asymmetric signature. 65732b31808SJens Wiklander * 65832b31808SJens Wiklander * This macro expands to a compile-time constant integer. This value 65932b31808SJens Wiklander * is the maximum size of a signature in bytes. 66032b31808SJens Wiklander */ 661b0563631STom Van Eyck #define PSA_SIGNATURE_MAX_SIZE 1 662b0563631STom Van Eyck 663b0563631STom Van Eyck #if (defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)) && \ 664b0563631STom Van Eyck (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE > PSA_SIGNATURE_MAX_SIZE) 665b0563631STom Van Eyck #undef PSA_SIGNATURE_MAX_SIZE 666b0563631STom Van Eyck #define PSA_SIGNATURE_MAX_SIZE PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE 667b0563631STom Van Eyck #endif 668b0563631STom Van Eyck #if (defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) || defined(PSA_WANT_ALG_RSA_PSS)) && \ 669b0563631STom Van Eyck (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_SIGNATURE_MAX_SIZE) 670b0563631STom Van Eyck #undef PSA_SIGNATURE_MAX_SIZE 671b0563631STom Van Eyck #define PSA_SIGNATURE_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) 672b0563631STom Van Eyck #endif 67332b31808SJens Wiklander 67432b31808SJens Wiklander /** Sufficient output buffer size for psa_asymmetric_encrypt(). 67532b31808SJens Wiklander * 67632b31808SJens Wiklander * This macro returns a sufficient buffer size for a ciphertext produced using 67732b31808SJens Wiklander * a key of the specified type and size, with the specified algorithm. 67832b31808SJens Wiklander * Note that the actual size of the ciphertext may be smaller, depending 67932b31808SJens Wiklander * on the algorithm. 68032b31808SJens Wiklander * 68132b31808SJens Wiklander * \warning This function may call its arguments multiple times or 68232b31808SJens Wiklander * zero times, so you should not pass arguments that contain 68332b31808SJens Wiklander * side effects. 68432b31808SJens Wiklander * 68532b31808SJens Wiklander * \param key_type An asymmetric key type (this may indifferently be a 68632b31808SJens Wiklander * key pair type or a public key type). 68732b31808SJens Wiklander * \param key_bits The size of the key in bits. 68832b31808SJens Wiklander * \param alg The asymmetric encryption algorithm. 68932b31808SJens Wiklander * 69032b31808SJens Wiklander * \return If the parameters are valid and supported, return 69132b31808SJens Wiklander * a buffer size in bytes that guarantees that 69232b31808SJens Wiklander * psa_asymmetric_encrypt() will not fail with 69332b31808SJens Wiklander * #PSA_ERROR_BUFFER_TOO_SMALL. 69432b31808SJens Wiklander * If the parameters are a valid combination that is not supported, 69532b31808SJens Wiklander * return either a sensible size or 0. 69632b31808SJens Wiklander * If the parameters are not valid, the 69732b31808SJens Wiklander * return value is unspecified. 69832b31808SJens Wiklander */ 69932b31808SJens Wiklander #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ 70032b31808SJens Wiklander (PSA_KEY_TYPE_IS_RSA(key_type) ? \ 70132b31808SJens Wiklander ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \ 702b0563631STom Van Eyck 0u) 70332b31808SJens Wiklander 70432b31808SJens Wiklander /** A sufficient output buffer size for psa_asymmetric_encrypt(), for any 70532b31808SJens Wiklander * supported asymmetric encryption. 70632b31808SJens Wiklander * 70732b31808SJens Wiklander * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). 70832b31808SJens Wiklander */ 70932b31808SJens Wiklander /* This macro assumes that RSA is the only supported asymmetric encryption. */ 71032b31808SJens Wiklander #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \ 71132b31808SJens Wiklander (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) 71232b31808SJens Wiklander 71332b31808SJens Wiklander /** Sufficient output buffer size for psa_asymmetric_decrypt(). 71432b31808SJens Wiklander * 71532b31808SJens Wiklander * This macro returns a sufficient buffer size for a plaintext produced using 71632b31808SJens Wiklander * a key of the specified type and size, with the specified algorithm. 71732b31808SJens Wiklander * Note that the actual size of the plaintext may be smaller, depending 71832b31808SJens Wiklander * on the algorithm. 71932b31808SJens Wiklander * 72032b31808SJens Wiklander * \warning This function may call its arguments multiple times or 72132b31808SJens Wiklander * zero times, so you should not pass arguments that contain 72232b31808SJens Wiklander * side effects. 72332b31808SJens Wiklander * 72432b31808SJens Wiklander * \param key_type An asymmetric key type (this may indifferently be a 72532b31808SJens Wiklander * key pair type or a public key type). 72632b31808SJens Wiklander * \param key_bits The size of the key in bits. 72732b31808SJens Wiklander * \param alg The asymmetric encryption algorithm. 72832b31808SJens Wiklander * 72932b31808SJens Wiklander * \return If the parameters are valid and supported, return 73032b31808SJens Wiklander * a buffer size in bytes that guarantees that 73132b31808SJens Wiklander * psa_asymmetric_decrypt() will not fail with 73232b31808SJens Wiklander * #PSA_ERROR_BUFFER_TOO_SMALL. 73332b31808SJens Wiklander * If the parameters are a valid combination that is not supported, 73432b31808SJens Wiklander * return either a sensible size or 0. 73532b31808SJens Wiklander * If the parameters are not valid, the 73632b31808SJens Wiklander * return value is unspecified. 73732b31808SJens Wiklander */ 73832b31808SJens Wiklander #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ 73932b31808SJens Wiklander (PSA_KEY_TYPE_IS_RSA(key_type) ? \ 74032b31808SJens Wiklander PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ 741b0563631STom Van Eyck 0u) 74232b31808SJens Wiklander 74332b31808SJens Wiklander /** A sufficient output buffer size for psa_asymmetric_decrypt(), for any 74432b31808SJens Wiklander * supported asymmetric decryption. 74532b31808SJens Wiklander * 74632b31808SJens Wiklander * This macro assumes that RSA is the only supported asymmetric encryption. 74732b31808SJens Wiklander * 74832b31808SJens Wiklander * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). 74932b31808SJens Wiklander */ 75032b31808SJens Wiklander #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \ 75132b31808SJens Wiklander (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) 75232b31808SJens Wiklander 75332b31808SJens Wiklander /* Maximum size of the ASN.1 encoding of an INTEGER with the specified 75432b31808SJens Wiklander * number of bits. 75532b31808SJens Wiklander * 75632b31808SJens Wiklander * This definition assumes that bits <= 2^19 - 9 so that the length field 75732b31808SJens Wiklander * is at most 3 bytes. The length of the encoding is the length of the 75832b31808SJens Wiklander * bit string padded to a whole number of bytes plus: 75932b31808SJens Wiklander * - 1 type byte; 76032b31808SJens Wiklander * - 1 to 3 length bytes; 76132b31808SJens Wiklander * - 0 to 1 bytes of leading 0 due to the sign bit. 76232b31808SJens Wiklander */ 76332b31808SJens Wiklander #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \ 764b0563631STom Van Eyck ((bits) / 8u + 5u) 76532b31808SJens Wiklander 76632b31808SJens Wiklander /* Maximum size of the export encoding of an RSA public key. 76732b31808SJens Wiklander * Assumes that the public exponent is less than 2^32. 76832b31808SJens Wiklander * 76932b31808SJens Wiklander * RSAPublicKey ::= SEQUENCE { 77032b31808SJens Wiklander * modulus INTEGER, -- n 77132b31808SJens Wiklander * publicExponent INTEGER } -- e 77232b31808SJens Wiklander * 77332b31808SJens Wiklander * - 4 bytes of SEQUENCE overhead; 77432b31808SJens Wiklander * - n : INTEGER; 77532b31808SJens Wiklander * - 7 bytes for the public exponent. 77632b31808SJens Wiklander */ 77732b31808SJens Wiklander #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ 778b0563631STom Van Eyck (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u) 77932b31808SJens Wiklander 78032b31808SJens Wiklander /* Maximum size of the export encoding of an RSA key pair. 78132b31808SJens Wiklander * Assumes that the public exponent is less than 2^32 and that the size 78232b31808SJens Wiklander * difference between the two primes is at most 1 bit. 78332b31808SJens Wiklander * 78432b31808SJens Wiklander * RSAPrivateKey ::= SEQUENCE { 78532b31808SJens Wiklander * version Version, -- 0 78632b31808SJens Wiklander * modulus INTEGER, -- N-bit 78732b31808SJens Wiklander * publicExponent INTEGER, -- 32-bit 78832b31808SJens Wiklander * privateExponent INTEGER, -- N-bit 78932b31808SJens Wiklander * prime1 INTEGER, -- N/2-bit 79032b31808SJens Wiklander * prime2 INTEGER, -- N/2-bit 79132b31808SJens Wiklander * exponent1 INTEGER, -- N/2-bit 79232b31808SJens Wiklander * exponent2 INTEGER, -- N/2-bit 79332b31808SJens Wiklander * coefficient INTEGER, -- N/2-bit 79432b31808SJens Wiklander * } 79532b31808SJens Wiklander * 79632b31808SJens Wiklander * - 4 bytes of SEQUENCE overhead; 79732b31808SJens Wiklander * - 3 bytes of version; 79832b31808SJens Wiklander * - 7 half-size INTEGERs plus 2 full-size INTEGERs, 79932b31808SJens Wiklander * overapproximated as 9 half-size INTEGERS; 80032b31808SJens Wiklander * - 7 bytes for the public exponent. 80132b31808SJens Wiklander */ 80232b31808SJens Wiklander #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \ 803b0563631STom Van Eyck (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u) 80432b31808SJens Wiklander 80532b31808SJens Wiklander /* Maximum size of the export encoding of a DSA public key. 80632b31808SJens Wiklander * 80732b31808SJens Wiklander * SubjectPublicKeyInfo ::= SEQUENCE { 80832b31808SJens Wiklander * algorithm AlgorithmIdentifier, 80932b31808SJens Wiklander * subjectPublicKey BIT STRING } -- contains DSAPublicKey 81032b31808SJens Wiklander * AlgorithmIdentifier ::= SEQUENCE { 81132b31808SJens Wiklander * algorithm OBJECT IDENTIFIER, 81232b31808SJens Wiklander * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs 81332b31808SJens Wiklander * DSAPublicKey ::= INTEGER -- public key, Y 81432b31808SJens Wiklander * 81532b31808SJens Wiklander * - 3 * 4 bytes of SEQUENCE overhead; 81632b31808SJens Wiklander * - 1 + 1 + 7 bytes of algorithm (DSA OID); 81732b31808SJens Wiklander * - 4 bytes of BIT STRING overhead; 81832b31808SJens Wiklander * - 3 full-size INTEGERs (p, g, y); 81932b31808SJens Wiklander * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits). 82032b31808SJens Wiklander */ 82132b31808SJens Wiklander #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ 822b0563631STom Van Eyck (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u) 82332b31808SJens Wiklander 82432b31808SJens Wiklander /* Maximum size of the export encoding of a DSA key pair. 82532b31808SJens Wiklander * 82632b31808SJens Wiklander * DSAPrivateKey ::= SEQUENCE { 82732b31808SJens Wiklander * version Version, -- 0 82832b31808SJens Wiklander * prime INTEGER, -- p 82932b31808SJens Wiklander * subprime INTEGER, -- q 83032b31808SJens Wiklander * generator INTEGER, -- g 83132b31808SJens Wiklander * public INTEGER, -- y 83232b31808SJens Wiklander * private INTEGER, -- x 83332b31808SJens Wiklander * } 83432b31808SJens Wiklander * 83532b31808SJens Wiklander * - 4 bytes of SEQUENCE overhead; 83632b31808SJens Wiklander * - 3 bytes of version; 83732b31808SJens Wiklander * - 3 full-size INTEGERs (p, g, y); 83832b31808SJens Wiklander * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits). 83932b31808SJens Wiklander */ 84032b31808SJens Wiklander #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \ 841b0563631STom Van Eyck (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u) 84232b31808SJens Wiklander 84332b31808SJens Wiklander /* Maximum size of the export encoding of an ECC public key. 84432b31808SJens Wiklander * 84532b31808SJens Wiklander * The representation of an ECC public key is: 84632b31808SJens Wiklander * - The byte 0x04; 84732b31808SJens Wiklander * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; 84832b31808SJens Wiklander * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; 84932b31808SJens Wiklander * - where m is the bit size associated with the curve. 85032b31808SJens Wiklander * 85132b31808SJens Wiklander * - 1 byte + 2 * point size. 85232b31808SJens Wiklander */ 85332b31808SJens Wiklander #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \ 854b0563631STom Van Eyck (2u * PSA_BITS_TO_BYTES(key_bits) + 1u) 85532b31808SJens Wiklander 85632b31808SJens Wiklander /* Maximum size of the export encoding of an ECC key pair. 85732b31808SJens Wiklander * 85832b31808SJens Wiklander * An ECC key pair is represented by the secret value. 85932b31808SJens Wiklander */ 86032b31808SJens Wiklander #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \ 86132b31808SJens Wiklander (PSA_BITS_TO_BYTES(key_bits)) 86232b31808SJens Wiklander 863b0563631STom Van Eyck /* Maximum size of the export encoding of an DH key pair. 864b0563631STom Van Eyck * 865b0563631STom Van Eyck * An DH key pair is represented by the secret value. 866b0563631STom Van Eyck */ 867b0563631STom Van Eyck #define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \ 868b0563631STom Van Eyck (PSA_BITS_TO_BYTES(key_bits)) 869b0563631STom Van Eyck 870b0563631STom Van Eyck /* Maximum size of the export encoding of an DH public key. 871b0563631STom Van Eyck */ 872b0563631STom Van Eyck #define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \ 873b0563631STom Van Eyck (PSA_BITS_TO_BYTES(key_bits)) 874b0563631STom Van Eyck 87532b31808SJens Wiklander /** Sufficient output buffer size for psa_export_key() or 87632b31808SJens Wiklander * psa_export_public_key(). 87732b31808SJens Wiklander * 87832b31808SJens Wiklander * This macro returns a compile-time constant if its arguments are 87932b31808SJens Wiklander * compile-time constants. 88032b31808SJens Wiklander * 88132b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 88232b31808SJens Wiklander * zero times, so you should not pass arguments that contain 88332b31808SJens Wiklander * side effects. 88432b31808SJens Wiklander * 88532b31808SJens Wiklander * The following code illustrates how to allocate enough memory to export 88632b31808SJens Wiklander * a key by querying the key type and size at runtime. 88732b31808SJens Wiklander * \code{c} 88832b31808SJens Wiklander * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 88932b31808SJens Wiklander * psa_status_t status; 89032b31808SJens Wiklander * status = psa_get_key_attributes(key, &attributes); 89132b31808SJens Wiklander * if (status != PSA_SUCCESS) handle_error(...); 89232b31808SJens Wiklander * psa_key_type_t key_type = psa_get_key_type(&attributes); 89332b31808SJens Wiklander * size_t key_bits = psa_get_key_bits(&attributes); 89432b31808SJens Wiklander * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits); 89532b31808SJens Wiklander * psa_reset_key_attributes(&attributes); 89632b31808SJens Wiklander * uint8_t *buffer = malloc(buffer_size); 89732b31808SJens Wiklander * if (buffer == NULL) handle_error(...); 89832b31808SJens Wiklander * size_t buffer_length; 89932b31808SJens Wiklander * status = psa_export_key(key, buffer, buffer_size, &buffer_length); 90032b31808SJens Wiklander * if (status != PSA_SUCCESS) handle_error(...); 90132b31808SJens Wiklander * \endcode 90232b31808SJens Wiklander * 90332b31808SJens Wiklander * \param key_type A supported key type. 90432b31808SJens Wiklander * \param key_bits The size of the key in bits. 90532b31808SJens Wiklander * 90632b31808SJens Wiklander * \return If the parameters are valid and supported, return 90732b31808SJens Wiklander * a buffer size in bytes that guarantees that 90832b31808SJens Wiklander * psa_export_key() or psa_export_public_key() will not fail with 90932b31808SJens Wiklander * #PSA_ERROR_BUFFER_TOO_SMALL. 91032b31808SJens Wiklander * If the parameters are a valid combination that is not supported, 91132b31808SJens Wiklander * return either a sensible size or 0. 91232b31808SJens Wiklander * If the parameters are not valid, the return value is unspecified. 91332b31808SJens Wiklander */ 91432b31808SJens Wiklander #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \ 91532b31808SJens Wiklander (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 916b0563631STom Van Eyck PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 91732b31808SJens Wiklander (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \ 91832b31808SJens Wiklander (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 91932b31808SJens Wiklander (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \ 92032b31808SJens Wiklander (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 92132b31808SJens Wiklander PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \ 92232b31808SJens Wiklander PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 923b0563631STom Van Eyck 0u) 92432b31808SJens Wiklander 92532b31808SJens Wiklander /** Sufficient output buffer size for psa_export_public_key(). 92632b31808SJens Wiklander * 92732b31808SJens Wiklander * This macro returns a compile-time constant if its arguments are 92832b31808SJens Wiklander * compile-time constants. 92932b31808SJens Wiklander * 93032b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 93132b31808SJens Wiklander * zero times, so you should not pass arguments that contain 93232b31808SJens Wiklander * side effects. 93332b31808SJens Wiklander * 93432b31808SJens Wiklander * The following code illustrates how to allocate enough memory to export 93532b31808SJens Wiklander * a public key by querying the key type and size at runtime. 93632b31808SJens Wiklander * \code{c} 93732b31808SJens Wiklander * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 93832b31808SJens Wiklander * psa_status_t status; 93932b31808SJens Wiklander * status = psa_get_key_attributes(key, &attributes); 94032b31808SJens Wiklander * if (status != PSA_SUCCESS) handle_error(...); 94132b31808SJens Wiklander * psa_key_type_t key_type = psa_get_key_type(&attributes); 94232b31808SJens Wiklander * size_t key_bits = psa_get_key_bits(&attributes); 94332b31808SJens Wiklander * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits); 94432b31808SJens Wiklander * psa_reset_key_attributes(&attributes); 94532b31808SJens Wiklander * uint8_t *buffer = malloc(buffer_size); 94632b31808SJens Wiklander * if (buffer == NULL) handle_error(...); 94732b31808SJens Wiklander * size_t buffer_length; 94832b31808SJens Wiklander * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length); 94932b31808SJens Wiklander * if (status != PSA_SUCCESS) handle_error(...); 95032b31808SJens Wiklander * \endcode 95132b31808SJens Wiklander * 95232b31808SJens Wiklander * \param key_type A public key or key pair key type. 95332b31808SJens Wiklander * \param key_bits The size of the key in bits. 95432b31808SJens Wiklander * 95532b31808SJens Wiklander * \return If the parameters are valid and supported, return 95632b31808SJens Wiklander * a buffer size in bytes that guarantees that 95732b31808SJens Wiklander * psa_export_public_key() will not fail with 95832b31808SJens Wiklander * #PSA_ERROR_BUFFER_TOO_SMALL. 95932b31808SJens Wiklander * If the parameters are a valid combination that is not 96032b31808SJens Wiklander * supported, return either a sensible size or 0. 96132b31808SJens Wiklander * If the parameters are not valid, 96232b31808SJens Wiklander * the return value is unspecified. 96332b31808SJens Wiklander * 96432b31808SJens Wiklander * If the parameters are valid and supported, 96532b31808SJens Wiklander * return the same result as 96632b31808SJens Wiklander * #PSA_EXPORT_KEY_OUTPUT_SIZE( 96732b31808SJens Wiklander * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type), 96832b31808SJens Wiklander * \p key_bits). 96932b31808SJens Wiklander */ 97032b31808SJens Wiklander #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \ 97132b31808SJens Wiklander (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 97232b31808SJens Wiklander PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ 973b0563631STom Van Eyck PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ 974b0563631STom Van Eyck 0u) 97532b31808SJens Wiklander 97632b31808SJens Wiklander /** Sufficient buffer size for exporting any asymmetric key pair. 97732b31808SJens Wiklander * 97832b31808SJens Wiklander * This macro expands to a compile-time constant integer. This value is 97932b31808SJens Wiklander * a sufficient buffer size when calling psa_export_key() to export any 98032b31808SJens Wiklander * asymmetric key pair, regardless of the exact key type and key size. 98132b31808SJens Wiklander * 98232b31808SJens Wiklander * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). 98332b31808SJens Wiklander */ 984b0563631STom Van Eyck #define PSA_EXPORT_KEY_PAIR_MAX_SIZE 1 985b0563631STom Van Eyck 986b0563631STom Van Eyck #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) && \ 987b0563631STom Van Eyck (PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \ 988b0563631STom Van Eyck PSA_EXPORT_KEY_PAIR_MAX_SIZE) 989b0563631STom Van Eyck #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 99032b31808SJens Wiklander #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 991b0563631STom Van Eyck PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 992b0563631STom Van Eyck #endif 993b0563631STom Van Eyck #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) && \ 99432b31808SJens Wiklander (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ 995b0563631STom Van Eyck PSA_EXPORT_KEY_PAIR_MAX_SIZE) 996b0563631STom Van Eyck #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 997b0563631STom Van Eyck #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 998b0563631STom Van Eyck PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 999b0563631STom Van Eyck #endif 1000b0563631STom Van Eyck #if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_BASIC) && \ 1001b0563631STom Van Eyck (PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \ 1002b0563631STom Van Eyck PSA_EXPORT_KEY_PAIR_MAX_SIZE) 1003b0563631STom Van Eyck #undef PSA_EXPORT_KEY_PAIR_MAX_SIZE 1004b0563631STom Van Eyck #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ 1005b0563631STom Van Eyck PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1006b0563631STom Van Eyck #endif 100732b31808SJens Wiklander 100832b31808SJens Wiklander /** Sufficient buffer size for exporting any asymmetric public key. 100932b31808SJens Wiklander * 101032b31808SJens Wiklander * This macro expands to a compile-time constant integer. This value is 101132b31808SJens Wiklander * a sufficient buffer size when calling psa_export_key() or 101232b31808SJens Wiklander * psa_export_public_key() to export any asymmetric public key, 101332b31808SJens Wiklander * regardless of the exact key type and key size. 101432b31808SJens Wiklander * 101532b31808SJens Wiklander * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). 101632b31808SJens Wiklander */ 1017b0563631STom Van Eyck #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1 1018b0563631STom Van Eyck 1019b0563631STom Van Eyck #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \ 1020b0563631STom Van Eyck (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \ 1021b0563631STom Van Eyck PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1022b0563631STom Van Eyck #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 102332b31808SJens Wiklander #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1024b0563631STom Van Eyck PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 1025b0563631STom Van Eyck #endif 1026b0563631STom Van Eyck #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) && \ 102732b31808SJens Wiklander (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ 1028b0563631STom Van Eyck PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1029b0563631STom Van Eyck #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1030b0563631STom Van Eyck #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1031b0563631STom Van Eyck PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 1032b0563631STom Van Eyck #endif 1033b0563631STom Van Eyck #if defined(PSA_WANT_KEY_TYPE_DH_PUBLIC_KEY) && \ 1034b0563631STom Van Eyck (PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \ 1035b0563631STom Van Eyck PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1036b0563631STom Van Eyck #undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1037b0563631STom Van Eyck #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ 1038b0563631STom Van Eyck PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1039b0563631STom Van Eyck #endif 104032b31808SJens Wiklander 1041*c3deb3d6SEtienne Carriere #define PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE \ 1042*c3deb3d6SEtienne Carriere ((PSA_EXPORT_KEY_PAIR_MAX_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) ? \ 1043*c3deb3d6SEtienne Carriere PSA_EXPORT_KEY_PAIR_MAX_SIZE : PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) 1044*c3deb3d6SEtienne Carriere 104532b31808SJens Wiklander /** Sufficient output buffer size for psa_raw_key_agreement(). 104632b31808SJens Wiklander * 104732b31808SJens Wiklander * This macro returns a compile-time constant if its arguments are 104832b31808SJens Wiklander * compile-time constants. 104932b31808SJens Wiklander * 105032b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 105132b31808SJens Wiklander * zero times, so you should not pass arguments that contain 105232b31808SJens Wiklander * side effects. 105332b31808SJens Wiklander * 105432b31808SJens Wiklander * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE. 105532b31808SJens Wiklander * 105632b31808SJens Wiklander * \param key_type A supported key type. 105732b31808SJens Wiklander * \param key_bits The size of the key in bits. 105832b31808SJens Wiklander * 105932b31808SJens Wiklander * \return If the parameters are valid and supported, return 106032b31808SJens Wiklander * a buffer size in bytes that guarantees that 106132b31808SJens Wiklander * psa_raw_key_agreement() will not fail with 106232b31808SJens Wiklander * #PSA_ERROR_BUFFER_TOO_SMALL. 106332b31808SJens Wiklander * If the parameters are a valid combination that 106432b31808SJens Wiklander * is not supported, return either a sensible size or 0. 106532b31808SJens Wiklander * If the parameters are not valid, 106632b31808SJens Wiklander * the return value is unspecified. 106732b31808SJens Wiklander */ 106832b31808SJens Wiklander #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \ 1069b0563631STom Van Eyck ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \ 1070b0563631STom Van Eyck PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u) 107132b31808SJens Wiklander 107232b31808SJens Wiklander /** Maximum size of the output from psa_raw_key_agreement(). 107332b31808SJens Wiklander * 107432b31808SJens Wiklander * This macro expands to a compile-time constant integer. This value is the 107532b31808SJens Wiklander * maximum size of the output any raw key agreement algorithm, in bytes. 107632b31808SJens Wiklander * 107732b31808SJens Wiklander * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits). 107832b31808SJens Wiklander */ 1079b0563631STom Van Eyck #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1 1080b0563631STom Van Eyck 1081b0563631STom Van Eyck #if defined(PSA_WANT_ALG_ECDH) && \ 1082b0563631STom Van Eyck (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) 1083b0563631STom Van Eyck #undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1084b0563631STom Van Eyck #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) 1085b0563631STom Van Eyck #endif 1086b0563631STom Van Eyck #if defined(PSA_WANT_ALG_FFDH) && \ 1087b0563631STom Van Eyck (PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) 1088b0563631STom Van Eyck #undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1089b0563631STom Van Eyck #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) 1090b0563631STom Van Eyck #endif 109132b31808SJens Wiklander 1092*c3deb3d6SEtienne Carriere /** Maximum key length for ciphers. 1093*c3deb3d6SEtienne Carriere * 1094*c3deb3d6SEtienne Carriere * Since there is no additional PSA_WANT_xxx symbol to specifiy the size of 1095*c3deb3d6SEtienne Carriere * the key once a cipher is enabled (as it happens for asymmetric keys for 1096*c3deb3d6SEtienne Carriere * example), the maximum key length is taken into account for each cipher. 1097*c3deb3d6SEtienne Carriere * The resulting value will be the maximum cipher's key length given depending 1098*c3deb3d6SEtienne Carriere * on which ciphers are enabled. 1099*c3deb3d6SEtienne Carriere * 1100*c3deb3d6SEtienne Carriere * Note: max value for AES used below would be doubled if XTS were enabled, but 1101*c3deb3d6SEtienne Carriere * this mode is currently not supported in Mbed TLS implementation of PSA 1102*c3deb3d6SEtienne Carriere * APIs. 1103*c3deb3d6SEtienne Carriere */ 1104*c3deb3d6SEtienne Carriere #if (defined(PSA_WANT_KEY_TYPE_AES) || defined(PSA_WANT_KEY_TYPE_ARIA) || \ 1105*c3deb3d6SEtienne Carriere defined(PSA_WANT_KEY_TYPE_CAMELLIA) || defined(PSA_WANT_KEY_TYPE_CHACHA20)) 1106*c3deb3d6SEtienne Carriere #define PSA_CIPHER_MAX_KEY_LENGTH 32u 1107*c3deb3d6SEtienne Carriere #elif defined(PSA_WANT_KEY_TYPE_DES) 1108*c3deb3d6SEtienne Carriere #define PSA_CIPHER_MAX_KEY_LENGTH 24u 1109*c3deb3d6SEtienne Carriere #else 1110*c3deb3d6SEtienne Carriere #define PSA_CIPHER_MAX_KEY_LENGTH 0u 1111*c3deb3d6SEtienne Carriere #endif 1112*c3deb3d6SEtienne Carriere 111332b31808SJens Wiklander /** The default IV size for a cipher algorithm, in bytes. 111432b31808SJens Wiklander * 111532b31808SJens Wiklander * The IV that is generated as part of a call to #psa_cipher_encrypt() is always 111632b31808SJens Wiklander * the default IV length for the algorithm. 111732b31808SJens Wiklander * 111832b31808SJens Wiklander * This macro can be used to allocate a buffer of sufficient size to 111932b31808SJens Wiklander * store the IV output from #psa_cipher_generate_iv() when using 112032b31808SJens Wiklander * a multi-part cipher operation. 112132b31808SJens Wiklander * 112232b31808SJens Wiklander * See also #PSA_CIPHER_IV_MAX_SIZE. 112332b31808SJens Wiklander * 112432b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 112532b31808SJens Wiklander * zero times, so you should not pass arguments that contain 112632b31808SJens Wiklander * side effects. 112732b31808SJens Wiklander * 112832b31808SJens Wiklander * \param key_type A symmetric key type that is compatible with algorithm \p alg. 112932b31808SJens Wiklander * 113032b31808SJens Wiklander * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true). 113132b31808SJens Wiklander * 113232b31808SJens Wiklander * \return The default IV size for the specified key type and algorithm. 113332b31808SJens Wiklander * If the algorithm does not use an IV, return 0. 113432b31808SJens Wiklander * If the key type or cipher algorithm is not recognized, 113532b31808SJens Wiklander * or the parameters are incompatible, return 0. 113632b31808SJens Wiklander */ 113732b31808SJens Wiklander #define PSA_CIPHER_IV_LENGTH(key_type, alg) \ 113832b31808SJens Wiklander (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \ 113932b31808SJens Wiklander ((alg) == PSA_ALG_CTR || \ 114032b31808SJens Wiklander (alg) == PSA_ALG_CFB || \ 114132b31808SJens Wiklander (alg) == PSA_ALG_OFB || \ 114232b31808SJens Wiklander (alg) == PSA_ALG_XTS || \ 114332b31808SJens Wiklander (alg) == PSA_ALG_CBC_NO_PADDING || \ 114432b31808SJens Wiklander (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 114532b31808SJens Wiklander (key_type) == PSA_KEY_TYPE_CHACHA20 && \ 1146b0563631STom Van Eyck (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \ 1147b0563631STom Van Eyck (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \ 1148b0563631STom Van Eyck 0u) 114932b31808SJens Wiklander 115032b31808SJens Wiklander /** The maximum IV size for all supported cipher algorithms, in bytes. 115132b31808SJens Wiklander * 115232b31808SJens Wiklander * See also #PSA_CIPHER_IV_LENGTH(). 115332b31808SJens Wiklander */ 1154b0563631STom Van Eyck #define PSA_CIPHER_IV_MAX_SIZE 16u 115532b31808SJens Wiklander 115632b31808SJens Wiklander /** The maximum size of the output of psa_cipher_encrypt(), in bytes. 115732b31808SJens Wiklander * 115832b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 115932b31808SJens Wiklander * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. 116032b31808SJens Wiklander * Depending on the algorithm, the actual size of the output might be smaller. 116132b31808SJens Wiklander * 116232b31808SJens Wiklander * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length). 116332b31808SJens Wiklander * 116432b31808SJens Wiklander * \warning This macro may evaluate its arguments multiple times or 116532b31808SJens Wiklander * zero times, so you should not pass arguments that contain 116632b31808SJens Wiklander * side effects. 116732b31808SJens Wiklander * 116832b31808SJens Wiklander * \param key_type A symmetric key type that is compatible with algorithm 116932b31808SJens Wiklander * alg. 117032b31808SJens Wiklander * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that 117132b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 117232b31808SJens Wiklander * \param input_length Size of the input in bytes. 117332b31808SJens Wiklander * 117432b31808SJens Wiklander * \return A sufficient output size for the specified key type and 117532b31808SJens Wiklander * algorithm. If the key type or cipher algorithm is not 117632b31808SJens Wiklander * recognized, or the parameters are incompatible, 117732b31808SJens Wiklander * return 0. 117832b31808SJens Wiklander */ 117932b31808SJens Wiklander #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ 118032b31808SJens Wiklander (alg == PSA_ALG_CBC_PKCS7 ? \ 118132b31808SJens Wiklander (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \ 118232b31808SJens Wiklander PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ 1183b0563631STom Van Eyck (input_length) + 1u) + \ 1184b0563631STom Van Eyck PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \ 118532b31808SJens Wiklander (PSA_ALG_IS_CIPHER(alg) ? \ 118632b31808SJens Wiklander (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ 1187b0563631STom Van Eyck 0u)) 118832b31808SJens Wiklander 118932b31808SJens Wiklander /** A sufficient output buffer size for psa_cipher_encrypt(), for any of the 119032b31808SJens Wiklander * supported key types and cipher algorithms. 119132b31808SJens Wiklander * 119232b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 119332b31808SJens Wiklander * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. 119432b31808SJens Wiklander * 119532b31808SJens Wiklander * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 119632b31808SJens Wiklander * 119732b31808SJens Wiklander * \param input_length Size of the input in bytes. 119832b31808SJens Wiklander * 119932b31808SJens Wiklander */ 120032b31808SJens Wiklander #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \ 120132b31808SJens Wiklander (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \ 1202b0563631STom Van Eyck (input_length) + 1u) + \ 120332b31808SJens Wiklander PSA_CIPHER_IV_MAX_SIZE) 120432b31808SJens Wiklander 120532b31808SJens Wiklander /** The maximum size of the output of psa_cipher_decrypt(), in bytes. 120632b31808SJens Wiklander * 120732b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 120832b31808SJens Wiklander * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. 120932b31808SJens Wiklander * Depending on the algorithm, the actual size of the output might be smaller. 121032b31808SJens Wiklander * 121132b31808SJens Wiklander * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length). 121232b31808SJens Wiklander * 121332b31808SJens Wiklander * \param key_type A symmetric key type that is compatible with algorithm 121432b31808SJens Wiklander * alg. 121532b31808SJens Wiklander * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that 121632b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 121732b31808SJens Wiklander * \param input_length Size of the input in bytes. 121832b31808SJens Wiklander * 121932b31808SJens Wiklander * \return A sufficient output size for the specified key type and 122032b31808SJens Wiklander * algorithm. If the key type or cipher algorithm is not 122132b31808SJens Wiklander * recognized, or the parameters are incompatible, 122232b31808SJens Wiklander * return 0. 122332b31808SJens Wiklander */ 122432b31808SJens Wiklander #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ 122532b31808SJens Wiklander (PSA_ALG_IS_CIPHER(alg) && \ 122632b31808SJens Wiklander ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \ 122732b31808SJens Wiklander (input_length) : \ 1228b0563631STom Van Eyck 0u) 122932b31808SJens Wiklander 123032b31808SJens Wiklander /** A sufficient output buffer size for psa_cipher_decrypt(), for any of the 123132b31808SJens Wiklander * supported key types and cipher algorithms. 123232b31808SJens Wiklander * 123332b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 123432b31808SJens Wiklander * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. 123532b31808SJens Wiklander * 123632b31808SJens Wiklander * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 123732b31808SJens Wiklander * 123832b31808SJens Wiklander * \param input_length Size of the input in bytes. 123932b31808SJens Wiklander */ 124032b31808SJens Wiklander #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \ 124132b31808SJens Wiklander (input_length) 124232b31808SJens Wiklander 124332b31808SJens Wiklander /** A sufficient output buffer size for psa_cipher_update(). 124432b31808SJens Wiklander * 124532b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 124632b31808SJens Wiklander * that psa_cipher_update() will not fail due to an insufficient buffer size. 124732b31808SJens Wiklander * The actual size of the output might be smaller in any given call. 124832b31808SJens Wiklander * 124932b31808SJens Wiklander * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length). 125032b31808SJens Wiklander * 125132b31808SJens Wiklander * \param key_type A symmetric key type that is compatible with algorithm 125232b31808SJens Wiklander * alg. 125332b31808SJens Wiklander * \param alg A cipher algorithm (PSA_ALG_XXX value such that 125432b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 125532b31808SJens Wiklander * \param input_length Size of the input in bytes. 125632b31808SJens Wiklander * 125732b31808SJens Wiklander * \return A sufficient output size for the specified key type and 125832b31808SJens Wiklander * algorithm. If the key type or cipher algorithm is not 125932b31808SJens Wiklander * recognized, or the parameters are incompatible, return 0. 126032b31808SJens Wiklander */ 126132b31808SJens Wiklander #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ 126232b31808SJens Wiklander (PSA_ALG_IS_CIPHER(alg) ? \ 126332b31808SJens Wiklander (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \ 126432b31808SJens Wiklander (((alg) == PSA_ALG_CBC_PKCS7 || \ 126532b31808SJens Wiklander (alg) == PSA_ALG_CBC_NO_PADDING || \ 126632b31808SJens Wiklander (alg) == PSA_ALG_ECB_NO_PADDING) ? \ 126732b31808SJens Wiklander PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ 126832b31808SJens Wiklander input_length) : \ 1269b0563631STom Van Eyck (input_length)) : 0u) : \ 1270b0563631STom Van Eyck 0u) 127132b31808SJens Wiklander 127232b31808SJens Wiklander /** A sufficient output buffer size for psa_cipher_update(), for any of the 127332b31808SJens Wiklander * supported key types and cipher algorithms. 127432b31808SJens Wiklander * 127532b31808SJens Wiklander * If the size of the output buffer is at least this large, it is guaranteed 127632b31808SJens Wiklander * that psa_cipher_update() will not fail due to an insufficient buffer size. 127732b31808SJens Wiklander * 127832b31808SJens Wiklander * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). 127932b31808SJens Wiklander * 128032b31808SJens Wiklander * \param input_length Size of the input in bytes. 128132b31808SJens Wiklander */ 128232b31808SJens Wiklander #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \ 128332b31808SJens Wiklander (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length)) 128432b31808SJens Wiklander 128532b31808SJens Wiklander /** A sufficient ciphertext buffer size for psa_cipher_finish(). 128632b31808SJens Wiklander * 128732b31808SJens Wiklander * If the size of the ciphertext buffer is at least this large, it is 128832b31808SJens Wiklander * guaranteed that psa_cipher_finish() will not fail due to an insufficient 128932b31808SJens Wiklander * ciphertext buffer size. The actual size of the output might be smaller in 129032b31808SJens Wiklander * any given call. 129132b31808SJens Wiklander * 129232b31808SJens Wiklander * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE(). 129332b31808SJens Wiklander * 129432b31808SJens Wiklander * \param key_type A symmetric key type that is compatible with algorithm 129532b31808SJens Wiklander * alg. 129632b31808SJens Wiklander * \param alg A cipher algorithm (PSA_ALG_XXX value such that 129732b31808SJens Wiklander * #PSA_ALG_IS_CIPHER(\p alg) is true). 129832b31808SJens Wiklander * \return A sufficient output size for the specified key type and 129932b31808SJens Wiklander * algorithm. If the key type or cipher algorithm is not 130032b31808SJens Wiklander * recognized, or the parameters are incompatible, return 0. 130132b31808SJens Wiklander */ 130232b31808SJens Wiklander #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \ 130332b31808SJens Wiklander (PSA_ALG_IS_CIPHER(alg) ? \ 130432b31808SJens Wiklander (alg == PSA_ALG_CBC_PKCS7 ? \ 130532b31808SJens Wiklander PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ 1306b0563631STom Van Eyck 0u) : \ 1307b0563631STom Van Eyck 0u) 130832b31808SJens Wiklander 130932b31808SJens Wiklander /** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the 131032b31808SJens Wiklander * supported key types and cipher algorithms. 131132b31808SJens Wiklander * 131232b31808SJens Wiklander * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg). 131332b31808SJens Wiklander */ 131432b31808SJens Wiklander #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \ 131532b31808SJens Wiklander (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) 131632b31808SJens Wiklander 131732b31808SJens Wiklander #endif /* PSA_CRYPTO_SIZES_H */ 1318