blob: 8906ebf5e2c430740dea19b5cbb44e92c9263102 [file] [log] [blame]
Antonio de Angelis377a1552018-11-22 17:02:40 +00001/*
Summer Qin614002c2023-01-19 15:22:39 +08002 * Copyright (c) 2018-2023, Arm Limited. All rights reserved.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto_sizes.h
Antonio de Angelis377a1552018-11-22 17:02:40 +00009 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +010010 * \brief PSA cryptography module: Mbed TLS buffer size macros
Antonio de Angelis377a1552018-11-22 17:02:40 +000011 *
12 * \note This file may not be included directly. Applications must
Jamie Foxcc31d402019-01-28 17:13:52 +000013 * include psa/crypto.h.
Antonio de Angelis377a1552018-11-22 17:02:40 +000014 *
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
Jamie Fox0e54ebc2019-04-09 14:21:04 +010027 * implementation are in crypto.h.
Antonio de Angelis377a1552018-11-22 17:02:40 +000028 */
29
30#ifndef PSA_CRYPTO_SIZES_H
31#define PSA_CRYPTO_SIZES_H
32
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +010033#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))
Jamie Fox0e54ebc2019-04-09 14:21:04 +010037
Antonio de Angelis04debbd2019-10-14 12:12:52 +010038#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
39 (((length) + (block_size) - 1) / (block_size) * (block_size))
40
Jamie Fox0e54ebc2019-04-09 14:21:04 +010041/** 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.
Jamie Fox0e54ebc2019-04-09 14:21:04 +010052 */
Maulik Patel13b27cf2021-05-14 11:44:53 +010053#define PSA_HASH_LENGTH(alg) \
54 ( \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +010055 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)
Jamie Fox0e54ebc2019-04-09 14:21:04 +010069
Summer Qinf07cc312022-01-05 16:52:54 +080070/** 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 ( \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +010087 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)
Summer Qinf07cc312022-01-05 16:52:54 +0800101
Antonio de Angelis377a1552018-11-22 17:02:40 +0000102/** \def PSA_HASH_MAX_SIZE
103 *
104 * Maximum size of a hash.
105 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100106 * This macro expands to a compile-time constant integer. This value
107 * is the maximum size of a hash in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000108 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100109/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100110 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
111 * HMAC-SHA3-512. */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100112/* 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 */
Joakim Andersson779cfe02023-06-30 12:40:23 +0200115#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100116#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
Summer Qin359167d2021-07-05 18:11:50 +0800134#endif
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100135
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) */
Joakim Andersson779cfe02023-06-30 12:40:23 +0200148/* Without any PSA configuration we must assume the maximum size possible. */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100149#define PSA_HASH_MAX_SIZE 64u
150#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u
151#endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000152
153/** \def PSA_MAC_MAX_SIZE
154 *
155 * Maximum size of a MAC.
156 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100157 * This macro expands to a compile-time constant integer. This value
158 * is the maximum size of a MAC in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000159 */
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. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100162/* Note that the encoding of truncated MAC algorithms limits this value
163 * to 64 bytes.
164 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000165#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
166
Summer Qin359167d2021-07-05 18:11:50 +0800167/** The length of a tag for an AEAD algorithm, in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100168 *
Summer Qin359167d2021-07-05 18:11:50 +0800169 * 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.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100176 * \param alg An AEAD algorithm
177 * (\c PSA_ALG_XXX value such that
178 * #PSA_ALG_IS_AEAD(\p alg) is true).
179 *
Summer Qin359167d2021-07-05 18:11:50 +0800180 * \return The tag length for the specified algorithm and key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100181 * If the AEAD algorithm does not have an identified
182 * tag that can be distinguished from the rest of
183 * the ciphertext, return 0.
Summer Qin359167d2021-07-05 18:11:50 +0800184 * If the key type or AEAD algorithm is not
185 * recognized, or the parameters are incompatible,
186 * return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100187 */
Summer Qin359167d2021-07-05 18:11:50 +0800188#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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100191 ((void) (key_bits), 0u))
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100192
Maulik Patel13b27cf2021-05-14 11:44:53 +0100193/** The maximum tag size for all supported AEAD algorithms, in bytes.
194 *
Summer Qin359167d2021-07-05 18:11:50 +0800195 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100196 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100197#define PSA_AEAD_TAG_MAX_SIZE 16u
Maulik Patel13b27cf2021-05-14 11:44:53 +0100198
Antonio de Angelis377a1552018-11-22 17:02:40 +0000199/* The maximum size of an RSA key on this implementation, in bits.
200 * This is a vendor-specific macro.
201 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100202 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000203 * 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. */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100212#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
Antonio de Angelis377a1552018-11-22 17:02:40 +0000232
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100233/* 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)
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100236#if defined(PSA_WANT_ECC_SECP_R1_521)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100237#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100238#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100239#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100240#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100241#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100242#elif defined(PSA_WANT_ECC_SECP_R1_384)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100243#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100244#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100245#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100246#elif defined(PSA_WANT_ECC_SECP_R1_256)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100247#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100248#elif defined(PSA_WANT_ECC_SECP_K1_256)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100249#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100250#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100251#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100252#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100253#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100254#elif defined(PSA_WANT_ECC_SECP_R1_224)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100255#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100256#elif defined(PSA_WANT_ECC_SECP_K1_224)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100257#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100258#elif defined(PSA_WANT_ECC_SECP_R1_192)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100259#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100260#elif defined(PSA_WANT_ECC_SECP_K1_192)
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100261#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100262#else
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100263#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100264#endif
265#else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Joakim Andersson779cfe02023-06-30 12:40:23 +0200266/* Without any PSA configuration we must assume the maximum size possible. */
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100267#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
268#endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100269
Maulik Patel13b27cf2021-05-14 11:44:53 +0100270/** This macro returns the maximum supported length of the PSK for the
271 * TLS-1.2 PSK-to-MS key derivation
Summer Qin359167d2021-07-05 18:11:50 +0800272 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100273 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100274 * The maximum supported length does not depend on the chosen hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100275 *
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
Maulik Patel13b27cf2021-05-14 11:44:53 +0100283 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100284 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100285#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u
Antonio de Angelis377a1552018-11-22 17:02:40 +0000286
Summer Qin614002c2023-01-19 15:22:39 +0800287/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
288 * which is expected to work with P-256 curve only. */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100289#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u
Summer Qin614002c2023-01-19 15:22:39 +0800290
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. */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100294#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
Summer Qin614002c2023-01-19 15:22:39 +0800300
Maulik Patel13b27cf2021-05-14 11:44:53 +0100301/** The maximum size of a block cipher. */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100302#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u
Maulik Patel13b27cf2021-05-14 11:44:53 +0100303
Antonio de Angelis377a1552018-11-22 17:02:40 +0000304/** 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 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100308 * \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 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000312 * \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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100315 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000316 *
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 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100325#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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100329 ((void) (key_type), (void) (key_bits), 0u))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000330
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 *
Summer Qin359167d2021-07-05 18:11:50 +0800338 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
339 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100340 * \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 *
Summer Qin359167d2021-07-05 18:11:50 +0800344 * \param key_type A symmetric key type that is
345 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000346 * \param alg An AEAD algorithm
347 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100348 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000349 * \param plaintext_length Size of the plaintext in bytes.
350 *
351 * \return The AEAD ciphertext size for the specified
352 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800353 * If the key type or AEAD algorithm is not
354 * recognized, or the parameters are incompatible,
355 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000356 */
Summer Qin359167d2021-07-05 18:11:50 +0800357#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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100360 0u)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000361
Maulik Patel13b27cf2021-05-14 11:44:53 +0100362/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800371 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
372 * \p plaintext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100373 *
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
Antonio de Angelis377a1552018-11-22 17:02:40 +0000384/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800391 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
392 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100393 * \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 *
Summer Qin359167d2021-07-05 18:11:50 +0800397 * \param key_type A symmetric key type that is
398 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000399 * \param alg An AEAD algorithm
400 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100401 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000402 * \param ciphertext_length Size of the plaintext in bytes.
403 *
404 * \return The AEAD ciphertext size for the specified
405 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800406 * If the key type or AEAD algorithm is not
407 * recognized, or the parameters are incompatible,
408 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000409 */
Summer Qin359167d2021-07-05 18:11:50 +0800410#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
411 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800412 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
413 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100414 0u)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100415
Maulik Patel13b27cf2021-05-14 11:44:53 +0100416/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800425 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
426 * \p ciphertext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100427 *
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) \
Summer Qind635cd02023-03-31 18:07:38 +0800435 (ciphertext_length)
Maulik Patel13b27cf2021-05-14 11:44:53 +0100436
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) \
Summer Qin359167d2021-07-05 18:11:50 +0800463 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100464 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \
465 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \
466 0u : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100467 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100468 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \
469 0u)
Maulik Patel13b27cf2021-05-14 11:44:53 +0100470
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 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100482#define PSA_AEAD_NONCE_MAX_SIZE 13u
Maulik Patel13b27cf2021-05-14 11:44:53 +0100483
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100484/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800491 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
492 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100493 * \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 *
Summer Qin359167d2021-07-05 18:11:50 +0800497 * \param key_type A symmetric key type that is
498 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100499 * \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.
Summer Qin359167d2021-07-05 18:11:50 +0800506 * If the key type or AEAD algorithm is not
507 * recognized, or the parameters are incompatible,
508 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100509 */
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. */
Summer Qin359167d2021-07-05 18:11:50 +0800514#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
515 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Summer Qind635cd02023-03-31 18:07:38 +0800516 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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100519 0u)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100520
Maulik Patel13b27cf2021-05-14 11:44:53 +0100521/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800527 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100528 *
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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100534/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800541 * 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.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100545 * \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.
Summer Qin359167d2021-07-05 18:11:50 +0800551 * If the key type or AEAD algorithm is not
552 * recognized, or the parameters are incompatible,
553 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100554 */
Summer Qin359167d2021-07-05 18:11:50 +0800555#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
556 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800557 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
558 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100559 0u)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100560
Maulik Patel13b27cf2021-05-14 11:44:53 +0100561/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
562 * supported key types and AEAD algorithms.
563 *
Summer Qin359167d2021-07-05 18:11:50 +0800564 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100565 */
566#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
567
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100568/** 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 *
Summer Qin359167d2021-07-05 18:11:50 +0800575 * 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.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100579 * \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.
Summer Qin359167d2021-07-05 18:11:50 +0800585 * If the key type or AEAD algorithm is not
586 * recognized, or the parameters are incompatible,
587 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100588 */
Summer Qin359167d2021-07-05 18:11:50 +0800589#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
590 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800591 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
592 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100593 0u)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000594
Maulik Patel13b27cf2021-05-14 11:44:53 +0100595/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
596 * supported key types and AEAD algorithms.
597 *
Summer Qin359167d2021-07-05 18:11:50 +0800598 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100599 */
600#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
601
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100602#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
603 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100604 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \
605 11u /*PKCS#1v1.5*/)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100606
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) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100616 (PSA_BITS_TO_BYTES(curve_bits) * 2u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100617
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100618/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000619 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100620 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000621 * 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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100636 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000637 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100638 * If the parameters are a valid combination that is not supported,
639 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000640 * If the parameters are not valid, the
641 * return value is unspecified.
642 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100643#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Summer Qind635cd02023-03-31 18:07:38 +0800644 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000645 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100646 ((void) alg, 0u))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000647
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100648#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
Antonio de Angelis377a1552018-11-22 17:02:40 +0000652 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100653 * Maximum size of an asymmetric signature.
654 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100655 * This macro expands to a compile-time constant integer. This value
656 * is the maximum size of a signature in bytes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100657 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100658#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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100670
671/** Sufficient output buffer size for psa_asymmetric_encrypt().
672 *
673 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000674 * 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.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100685 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000686 *
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.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100691 * If the parameters are a valid combination that is not supported,
692 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000693 * 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) ? \
Summer Qind635cd02023-03-31 18:07:38 +0800698 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100699 0u)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000700
Maulik Patel13b27cf2021-05-14 11:44:53 +0100701/** 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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100710/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000711 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100712 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000713 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100714 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000715 * 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.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100724 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000725 *
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.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100730 * If the parameters are a valid combination that is not supported,
731 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000732 * 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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100738 0u)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000739
Maulik Patel13b27cf2021-05-14 11:44:53 +0100740/** 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
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100750/* 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) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100761 ((bits) / 8u + 5u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100762
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) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100775 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100776
777/* Maximum size of the export encoding of an RSA key pair.
Summer Qin614002c2023-01-19 15:22:39 +0800778 * Assumes that the public exponent is less than 2^32 and that the size
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100779 * 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 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100799#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100800 (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100801
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,
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100809 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100810 * 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) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100819 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100820
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 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100837#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100838 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100839
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) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100851 (2u * PSA_BITS_TO_BYTES(key_bits) + 1u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100852
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 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100857#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100858 (PSA_BITS_TO_BYTES(key_bits))
859
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100860/* 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
Maulik Patel13b27cf2021-05-14 11:44:53 +0100872/** Sufficient output buffer size for psa_export_key() or
873 * psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100874 *
875 * This macro returns a compile-time constant if its arguments are
876 * compile-time constants.
877 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100878 * \warning This macro may evaluate its arguments multiple times or
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100879 * 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}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100885 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100886 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100887 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100888 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100889 * psa_key_type_t key_type = psa_get_key_type(&attributes);
890 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100891 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100892 * psa_reset_key_attributes(&attributes);
893 * uint8_t *buffer = malloc(buffer_size);
894 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100895 * 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 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100900 * \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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100913 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100914 (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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100920 0u)
Maulik Patel13b27cf2021-05-14 11:44:53 +0100921
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.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100933 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100934 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100935 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100936 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100937 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100938 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100939 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100940 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100941 * psa_reset_key_attributes(&attributes);
942 * uint8_t *buffer = malloc(buffer_size);
943 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100944 * 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 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100949 * \param key_type A public key or key pair key type.
950 * \param key_bits The size of the key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100951 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100952 * \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).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100966 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100967#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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100970 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
971 0u)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100972
Maulik Patel13b27cf2021-05-14 11:44:53 +0100973/** Sufficient buffer size for exporting any asymmetric key pair.
Maulik Patel28659c42021-01-06 14:09:22 +0000974 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100975 * 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.
Maulik Patel28659c42021-01-06 14:09:22 +0000978 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100979 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
980 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +0100981#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
Maulik Patel13b27cf2021-05-14 11:44:53 +01001004
1005/** Sufficient buffer size for exporting any asymmetric public key.
Maulik Patel28659c42021-01-06 14:09:22 +00001006 *
Maulik Patel13b27cf2021-05-14 11:44:53 +01001007 * 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 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001014#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
Maulik Patel13b27cf2021-05-14 11:44:53 +01001037
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.
Maulik Patel28659c42021-01-06 14:09:22 +00001042 *
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 *
Maulik Patel13b27cf2021-05-14 11:44:53 +01001047 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
Maulik Patel28659c42021-01-06 14:09:22 +00001048 *
Maulik Patel13b27cf2021-05-14 11:44:53 +01001049 * \param key_type A supported key type.
1050 * \param key_bits The size of the key in bits.
Maulik Patel28659c42021-01-06 14:09:22 +00001051 *
Maulik Patel13b27cf2021-05-14 11:44:53 +01001052 * \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.
Maulik Patel28659c42021-01-06 14:09:22 +00001060 */
Maulik Patel13b27cf2021-05-14 11:44:53 +01001061#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001062 ((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)
Maulik Patel28659c42021-01-06 14:09:22 +00001064
Maulik Patel13b27cf2021-05-14 11:44:53 +01001065/** Maximum size of the output from psa_raw_key_agreement().
Maulik Patel28659c42021-01-06 14:09:22 +00001066 *
Maulik Patel13b27cf2021-05-14 11:44:53 +01001067 * 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.
Maulik Patel28659c42021-01-06 14:09:22 +00001069 *
Maulik Patel13b27cf2021-05-14 11:44:53 +01001070 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
Maulik Patel28659c42021-01-06 14:09:22 +00001071 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001072#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
Maulik Patel28659c42021-01-06 14:09:22 +00001084
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 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001102 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
Maulik Patel28659c42021-01-06 14:09:22 +00001103 *
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.
Maulik Patel28659c42021-01-06 14:09:22 +00001108 */
1109#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001110 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Summer Qind635cd02023-03-31 18:07:38 +08001111 ((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) : \
Maulik Patel28659c42021-01-06 14:09:22 +00001117 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001118 (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \
1119 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \
1120 0u)
Maulik Patel28659c42021-01-06 14:09:22 +00001121
1122/** The maximum IV size for all supported cipher algorithms, in bytes.
1123 *
1124 * See also #PSA_CIPHER_IV_LENGTH().
1125 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001126#define PSA_CIPHER_IV_MAX_SIZE 16u
Maulik Patel28659c42021-01-06 14:09:22 +00001127
Maulik Patel13b27cf2021-05-14 11:44:53 +01001128/** 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 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001151#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))
Maulik Patel13b27cf2021-05-14 11:44:53 +01001160
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 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001172#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) + \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001175 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 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001196#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1197 (PSA_ALG_IS_CIPHER(alg) && \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001198 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001199 (input_length) : \
1200 0u)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001201
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 */
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001233#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)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001243
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) : \
Antonio de Angelis3fae3ae2023-10-01 00:28:56 +01001278 0u) : \
1279 0u)
Maulik Patel13b27cf2021-05-14 11:44:53 +01001280
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
Antonio de Angelis377a1552018-11-22 17:02:40 +00001289#endif /* PSA_CRYPTO_SIZES_H */