blob: 43f2f7b1f0b774a0a7a05eb6fa0b06b52b436495 [file] [log] [blame]
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001/**
2 * \file psa/crypto_sizes.h
3 *
4 * \brief PSA cryptography module: Mbed TLS buffer size macros
5 *
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02009 * This file contains the definitions of macros that are useful to
10 * compute buffer sizes. The signatures and semantics of these macros
11 * are standardized, but the definitions are not, because they depend on
12 * the available algorithms and, in some cases, on permitted tolerances
13 * on buffer sizes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +020014 *
Gilles Peskine07c91f52018-06-28 18:02:53 +020015 * In implementations with isolation between the application and the
16 * cryptography module, implementers should take care to ensure that
17 * the definitions that are exposed to applications match what the
18 * module implements.
19 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +020020 * Macros that compute sizes whose values do not depend on the
21 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020022 */
23/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020024 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000025 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine0cad07c2018-06-27 19:49:02 +020026 */
27
28#ifndef PSA_CRYPTO_SIZES_H
29#define PSA_CRYPTO_SIZES_H
30
31/* Include the Mbed TLS configuration file, the way Mbed TLS does it
32 * in each of its header files. */
33#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amerod58a00d2019-06-07 11:49:59 +010034#include "mbedtls/config.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020035#else
36#include MBEDTLS_CONFIG_FILE
37#endif
38
Gilles Peskinea7c26db2018-12-12 13:42:25 +010039#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
40#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
41
Gilles Peskine248010c2019-05-14 16:08:59 +020042#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
43 (((length) + (block_size) - 1) / (block_size) * (block_size))
44
Gilles Peskinea7c26db2018-12-12 13:42:25 +010045/** The size of the output of psa_hash_finish(), in bytes.
46 *
47 * This is also the hash size that psa_hash_verify() expects.
48 *
49 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
50 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
51 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
52 * hash algorithm).
53 *
54 * \return The hash size for the specified hash algorithm.
55 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010056 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010057#define PSA_HASH_LENGTH(alg) \
58 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010059 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
60 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
61 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
62 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
66 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
67 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
68 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
69 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
70 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
71 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
74 0)
75
Mateusz Starzyk272a3d42021-08-26 13:28:46 +020076/** The input block size of a hash algorithm, in bytes.
77 *
78 * Hash algorithms process their input data in blocks. Hash operations will
79 * retain any partial blocks until they have enough input to fill the block or
80 * until the operation is finished.
81 * This affects the output from psa_hash_suspend().
82 *
83 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
84 * PSA_ALG_IS_HASH(\p alg) is true).
85 *
86 * \return The block size in bytes for the specified hash algorithm.
87 * If the hash algorithm is not recognized, return 0.
88 * An implementation can return either 0 or the correct size for a
89 * hash algorithm that it recognizes, but does not support.
90 */
91#define PSA_HASH_BLOCK_LENGTH(alg) \
92 ( \
93 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
94 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 64 : \
95 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
96 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
97 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
98 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
99 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
100 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
101 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
102 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
103 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
104 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
108 0)
109
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200110/** \def PSA_HASH_MAX_SIZE
111 *
112 * Maximum size of a hash.
113 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100114 * This macro expands to a compile-time constant integer. This value
115 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200116 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200117/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
118 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
119 * HMAC-SHA3-512. */
Ronald Cron0859fe22021-10-18 09:10:31 +0200120#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200121#define PSA_HASH_MAX_SIZE 64
122#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
123#else
124#define PSA_HASH_MAX_SIZE 32
125#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
126#endif
127
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200128/** \def PSA_MAC_MAX_SIZE
129 *
130 * Maximum size of a MAC.
131 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100132 * This macro expands to a compile-time constant integer. This value
133 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200134 */
135/* All non-HMAC MACs have a maximum size that's smaller than the
136 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200137/* Note that the encoding of truncated MAC algorithms limits this value
138 * to 64 bytes.
139 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200140#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
141
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100142/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100143 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100144 * This macro can be used to allocate a buffer of sufficient size to store the
145 * tag output from psa_aead_finish().
146 *
147 * See also #PSA_AEAD_TAG_MAX_SIZE.
148 *
149 * \param key_type The type of the AEAD key.
150 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100151 * \param alg An AEAD algorithm
152 * (\c PSA_ALG_XXX value such that
153 * #PSA_ALG_IS_AEAD(\p alg) is true).
154 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200155 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100156 * If the AEAD algorithm does not have an identified
157 * tag that can be distinguished from the rest of
158 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100159 * If the key type or AEAD algorithm is not
160 * recognized, or the parameters are incompatible,
161 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100162 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100163#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200164 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200165 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100166 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100167
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200168/** The maximum tag size for all supported AEAD algorithms, in bytes.
169 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100170 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200171 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100172#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200173
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200174/* The maximum size of an RSA key on this implementation, in bits.
175 * This is a vendor-specific macro.
176 *
177 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
178 * whose parameters fit in a bignum is accepted. However large keys can
179 * induce a large memory usage and long computation times. Unlike other
180 * auxiliary macros in this file and in crypto.h, which reflect how the
181 * library is configured, this macro defines how the library is
182 * configured. This implementation refuses to import or generate an
183 * RSA key whose size is larger than the value defined here.
184 *
185 * Note that an implementation may set different size limits for different
186 * operations, and does not need to accept all key sizes up to the limit. */
187#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
188
189/* The maximum size of an ECC key on this implementation, in bits.
190 * This is a vendor-specific macro. */
191#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
192#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
193#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
194#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
195#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
196#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
197#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
198#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
199#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
200#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
201#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
202#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
203#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
204#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
205#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
206#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
207#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
208#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
209#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
210#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
211#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
212#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
213#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
214#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
215#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
216#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
217#else
218#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
219#endif
220
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100221/** This macro returns the maximum supported length of the PSK for the
222 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100223 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100224 *
225 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100226 *
227 * Quoting RFC 4279, Sect 5.3:
228 * TLS implementations supporting these ciphersuites MUST support
229 * arbitrary PSK identities up to 128 octets in length, and arbitrary
230 * PSKs up to 64 octets in length. Supporting longer identities and
231 * keys is RECOMMENDED.
232 *
233 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100234 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100235 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100236#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100237
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100238/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100239#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200240
Gilles Peskineacd4be32018-07-08 19:56:25 +0200241/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200242 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200243 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200244 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100245 * \warning This macro may evaluate its arguments multiple times or
246 * zero times, so you should not pass arguments that contain
247 * side effects.
248 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200249 * \param key_type The type of the MAC key.
250 * \param key_bits The size of the MAC key in bits.
251 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100252 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200253 *
254 * \return The MAC size for the specified algorithm with
255 * the specified key parameters.
256 * \return 0 if the MAC algorithm is not recognized.
257 * \return Either 0 or the correct size for a MAC algorithm that
258 * the implementation recognizes, but does not support.
259 * \return Unspecified if the key parameters are not consistent
260 * with the algorithm.
261 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100262#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
263 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
264 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
265 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 ((void) (key_type), (void) (key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200267
268/** The maximum size of the output of psa_aead_encrypt(), in bytes.
269 *
270 * If the size of the ciphertext buffer is at least this large, it is
271 * guaranteed that psa_aead_encrypt() will not fail due to an
272 * insufficient buffer size. Depending on the algorithm, the actual size of
273 * the ciphertext may be smaller.
274 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100275 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
276 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100277 * \warning This macro may evaluate its arguments multiple times or
278 * zero times, so you should not pass arguments that contain
279 * side effects.
280 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100281 * \param key_type A symmetric key type that is
282 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200283 * \param alg An AEAD algorithm
284 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100285 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200286 * \param plaintext_length Size of the plaintext in bytes.
287 *
288 * \return The AEAD ciphertext size for the specified
289 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100290 * If the key type or AEAD algorithm is not
291 * recognized, or the parameters are incompatible,
292 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200293 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100294#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200295 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200296 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297 0)
298
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200299/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
300 * supported key types and AEAD algorithms.
301 *
302 * If the size of the ciphertext buffer is at least this large, it is guaranteed
303 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
304 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100305 * \note This macro returns a compile-time constant if its arguments are
306 * compile-time constants.
307 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100308 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
309 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200310 *
311 * \param plaintext_length Size of the plaintext in bytes.
312 *
313 * \return A sufficient output buffer size for any of the
314 * supported key types and AEAD algorithms.
315 *
316 */
317#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
318 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
319
320
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200321/** The maximum size of the output of psa_aead_decrypt(), in bytes.
322 *
323 * If the size of the plaintext buffer is at least this large, it is
324 * guaranteed that psa_aead_decrypt() will not fail due to an
325 * insufficient buffer size. Depending on the algorithm, the actual size of
326 * the plaintext may be smaller.
327 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100328 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
329 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100330 * \warning This macro may evaluate its arguments multiple times or
331 * zero times, so you should not pass arguments that contain
332 * side effects.
333 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100334 * \param key_type A symmetric key type that is
335 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200336 * \param alg An AEAD algorithm
337 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100338 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200339 * \param ciphertext_length Size of the plaintext in bytes.
340 *
341 * \return The AEAD ciphertext size for the specified
342 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100343 * If the key type or AEAD algorithm is not
344 * recognized, or the parameters are incompatible,
345 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200346 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100347#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200348 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
350 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200351 0)
352
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200353/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
354 * supported key types and AEAD algorithms.
355 *
356 * If the size of the plaintext buffer is at least this large, it is guaranteed
357 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
358 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100359 * \note This macro returns a compile-time constant if its arguments are
360 * compile-time constants.
361 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100362 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
363 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200364 *
365 * \param ciphertext_length Size of the ciphertext in bytes.
366 *
367 * \return A sufficient output buffer size for any of the
368 * supported key types and AEAD algorithms.
369 *
370 */
371#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200373
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100374/** The default nonce size for an AEAD algorithm, in bytes.
375 *
376 * This macro can be used to allocate a buffer of sufficient size to
377 * store the nonce output from #psa_aead_generate_nonce().
378 *
379 * See also #PSA_AEAD_NONCE_MAX_SIZE.
380 *
381 * \note This is not the maximum size of nonce supported as input to
382 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
383 * just the default size that is generated by #psa_aead_generate_nonce().
384 *
385 * \warning This macro may evaluate its arguments multiple times or
386 * zero times, so you should not pass arguments that contain
387 * side effects.
388 *
389 * \param key_type A symmetric key type that is compatible with
390 * algorithm \p alg.
391 *
392 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
393 * #PSA_ALG_IS_AEAD(\p alg) is true).
394 *
395 * \return The default nonce size for the specified key type and algorithm.
396 * If the key type or AEAD algorithm is not recognized,
397 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100398 */
399#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100400 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
402 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
403 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100404 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100406 0)
407
408/** The maximum default nonce size among all supported pairs of key types and
409 * AEAD algorithms, in bytes.
410 *
411 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
412 * may return.
413 *
414 * \note This is not the maximum size of nonce supported as input to
415 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
416 * just the largest size that may be generated by
417 * #psa_aead_generate_nonce().
418 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100419#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100420
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200421/** A sufficient output buffer size for psa_aead_update().
422 *
423 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200424 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200425 * insufficient buffer size. The actual size of the output may be smaller
426 * in any given call.
427 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100428 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
429 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100430 * \warning This macro may evaluate its arguments multiple times or
431 * zero times, so you should not pass arguments that contain
432 * side effects.
433 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100434 * \param key_type A symmetric key type that is
435 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200436 * \param alg An AEAD algorithm
437 * (\c PSA_ALG_XXX value such that
438 * #PSA_ALG_IS_AEAD(\p alg) is true).
439 * \param input_length Size of the input in bytes.
440 *
441 * \return A sufficient output buffer size for the specified
442 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100443 * If the key type or AEAD algorithm is not
444 * recognized, or the parameters are incompatible,
445 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200446 */
447/* For all the AEAD modes defined in this specification, it is possible
448 * to emit output without delay. However, hardware may not always be
449 * capable of this. So for modes based on a block cipher, allow the
450 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100451#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200452 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
454 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
455 (input_length) : \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100456 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200457
458/** A sufficient output buffer size for psa_aead_update(), for any of the
459 * supported key types and AEAD algorithms.
460 *
461 * If the size of the output buffer is at least this large, it is guaranteed
462 * that psa_aead_update() will not fail due to an insufficient buffer size.
463 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100464 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200465 *
466 * \param input_length Size of the input in bytes.
467 */
468#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
469 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200470
471/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200472 *
473 * If the size of the ciphertext buffer is at least this large, it is
474 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200475 * insufficient ciphertext buffer size. The actual size of the output may
476 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200477 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100478 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
479 *
480 * \param key_type A symmetric key type that is
481 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200482 * \param alg An AEAD algorithm
483 * (\c PSA_ALG_XXX value such that
484 * #PSA_ALG_IS_AEAD(\p alg) is true).
485 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200486 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200487 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100488 * If the key type or AEAD algorithm is not
489 * recognized, or the parameters are incompatible,
490 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200491 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200492#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
493 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
495 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200496 0)
497
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200498/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
499 * supported key types and AEAD algorithms.
500 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100501 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200502 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200503#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200504
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200505/** A sufficient plaintext buffer size for psa_aead_verify().
506 *
507 * If the size of the plaintext buffer is at least this large, it is
508 * guaranteed that psa_aead_verify() will not fail due to an
509 * insufficient plaintext buffer size. The actual size of the output may
510 * be smaller in any given call.
511 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100512 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
513 *
514 * \param key_type A symmetric key type that is
515 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200516 * \param alg An AEAD algorithm
517 * (\c PSA_ALG_XXX value such that
518 * #PSA_ALG_IS_AEAD(\p alg) is true).
519 *
520 * \return A sufficient plaintext buffer size for the
521 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100522 * If the key type or AEAD algorithm is not
523 * recognized, or the parameters are incompatible,
524 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200525 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200526#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
527 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
529 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200530 0)
531
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200532/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
533 * supported key types and AEAD algorithms.
534 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100535 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200536 */
537#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
538
Jaeden Amero7f042142019-02-07 10:44:38 +0000539#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
540 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100541 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100542 11 /*PKCS#1v1.5*/)
543
544/**
545 * \brief ECDSA signature size for a given curve bit size
546 *
547 * \param curve_bits Curve size in bits.
548 * \return Signature size in bytes.
549 *
550 * \note This macro returns a compile-time constant if its argument is one.
551 */
552#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
553 (PSA_BITS_TO_BYTES(curve_bits) * 2)
554
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100555/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200556 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200557 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200558 * of the specified type and size, with the specified algorithm.
559 * Note that the actual size of the signature may be smaller
560 * (some algorithms produce a variable-size signature).
561 *
562 * \warning This function may call its arguments multiple times or
563 * zero times, so you should not pass arguments that contain
564 * side effects.
565 *
566 * \param key_type An asymmetric key type (this may indifferently be a
567 * key pair type or a public key type).
568 * \param key_bits The size of the key in bits.
569 * \param alg The signature algorithm.
570 *
571 * \return If the parameters are valid and supported, return
572 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100573 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200574 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100575 * If the parameters are a valid combination that is not supported,
576 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200577 * If the parameters are not valid, the
578 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200579 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100580#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200582 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 ((void) alg, 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200584
Gilles Peskine29755712019-11-08 15:49:40 +0100585#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
586 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
587
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100588/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100589 *
590 * Maximum size of an asymmetric signature.
591 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100592 * This macro expands to a compile-time constant integer. This value
593 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100594 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100595#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100596 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
597 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
598 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
599
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200600/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200601 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200602 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200603 * a key of the specified type and size, with the specified algorithm.
604 * Note that the actual size of the ciphertext may be smaller, depending
605 * on the algorithm.
606 *
607 * \warning This function may call its arguments multiple times or
608 * zero times, so you should not pass arguments that contain
609 * side effects.
610 *
611 * \param key_type An asymmetric key type (this may indifferently be a
612 * key pair type or a public key type).
613 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200614 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200615 *
616 * \return If the parameters are valid and supported, return
617 * a buffer size in bytes that guarantees that
618 * psa_asymmetric_encrypt() will not fail with
619 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100620 * If the parameters are a valid combination that is not supported,
621 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200622 * If the parameters are not valid, the
623 * return value is unspecified.
624 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200625#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
626 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100627 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200628 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200629
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200630/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
631 * supported asymmetric encryption.
632 *
633 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
634 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100635/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200636#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100637 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200638
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200639/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200640 *
Gilles Peskine76689602020-05-05 16:01:22 +0200641 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200642 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200643 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 * on the algorithm.
645 *
646 * \warning This function may call its arguments multiple times or
647 * zero times, so you should not pass arguments that contain
648 * side effects.
649 *
650 * \param key_type An asymmetric key type (this may indifferently be a
651 * key pair type or a public key type).
652 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200653 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200654 *
655 * \return If the parameters are valid and supported, return
656 * a buffer size in bytes that guarantees that
657 * psa_asymmetric_decrypt() will not fail with
658 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100659 * If the parameters are a valid combination that is not supported,
660 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200661 * If the parameters are not valid, the
662 * return value is unspecified.
663 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200664#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
665 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
666 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
667 0)
668
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200669/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
670 * supported asymmetric decryption.
671 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100672 * This macro assumes that RSA is the only supported asymmetric encryption.
673 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200674 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
675 */
676#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100677 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200678
Gilles Peskine1be949b2018-08-10 19:06:59 +0200679/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
680 * number of bits.
681 *
682 * This definition assumes that bits <= 2^19 - 9 so that the length field
683 * is at most 3 bytes. The length of the encoding is the length of the
684 * bit string padded to a whole number of bytes plus:
685 * - 1 type byte;
686 * - 1 to 3 length bytes;
687 * - 0 to 1 bytes of leading 0 due to the sign bit.
688 */
689#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
690 ((bits) / 8 + 5)
691
692/* Maximum size of the export encoding of an RSA public key.
693 * Assumes that the public exponent is less than 2^32.
694 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200695 * RSAPublicKey ::= SEQUENCE {
696 * modulus INTEGER, -- n
697 * publicExponent INTEGER } -- e
698 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000699 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200700 * - n : INTEGER;
701 * - 7 bytes for the public exponent.
702 */
703#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000704 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200705
706/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000707 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200708 * difference between the two primes is at most 1 bit.
709 *
710 * RSAPrivateKey ::= SEQUENCE {
711 * version Version, -- 0
712 * modulus INTEGER, -- N-bit
713 * publicExponent INTEGER, -- 32-bit
714 * privateExponent INTEGER, -- N-bit
715 * prime1 INTEGER, -- N/2-bit
716 * prime2 INTEGER, -- N/2-bit
717 * exponent1 INTEGER, -- N/2-bit
718 * exponent2 INTEGER, -- N/2-bit
719 * coefficient INTEGER, -- N/2-bit
720 * }
721 *
722 * - 4 bytes of SEQUENCE overhead;
723 * - 3 bytes of version;
724 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
725 * overapproximated as 9 half-size INTEGERS;
726 * - 7 bytes for the public exponent.
727 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200728#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200729 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
730
731/* Maximum size of the export encoding of a DSA public key.
732 *
733 * SubjectPublicKeyInfo ::= SEQUENCE {
734 * algorithm AlgorithmIdentifier,
735 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
736 * AlgorithmIdentifier ::= SEQUENCE {
737 * algorithm OBJECT IDENTIFIER,
bootstrap-prime7ef96ea2022-05-18 14:08:33 -0400738 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200739 * DSAPublicKey ::= INTEGER -- public key, Y
740 *
741 * - 3 * 4 bytes of SEQUENCE overhead;
742 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
743 * - 4 bytes of BIT STRING overhead;
744 * - 3 full-size INTEGERs (p, g, y);
745 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
746 */
747#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
748 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
749
750/* Maximum size of the export encoding of a DSA key pair.
751 *
752 * DSAPrivateKey ::= SEQUENCE {
753 * version Version, -- 0
754 * prime INTEGER, -- p
755 * subprime INTEGER, -- q
756 * generator INTEGER, -- g
757 * public INTEGER, -- y
758 * private INTEGER, -- x
759 * }
760 *
761 * - 4 bytes of SEQUENCE overhead;
762 * - 3 bytes of version;
763 * - 3 full-size INTEGERs (p, g, y);
764 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
765 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200766#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200767 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
768
769/* Maximum size of the export encoding of an ECC public key.
770 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000771 * The representation of an ECC public key is:
772 * - The byte 0x04;
773 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
774 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
775 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200776 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000777 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200778 */
779#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000780 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200781
782/* Maximum size of the export encoding of an ECC key pair.
783 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100784 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200785 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200786#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100787 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200788
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100789/** Sufficient output buffer size for psa_export_key() or
790 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200791 *
792 * This macro returns a compile-time constant if its arguments are
793 * compile-time constants.
794 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100795 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200796 * zero times, so you should not pass arguments that contain
797 * side effects.
798 *
799 * The following code illustrates how to allocate enough memory to export
800 * a key by querying the key type and size at runtime.
801 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200802 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200804 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200805 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200806 * psa_key_type_t key_type = psa_get_key_type(&attributes);
807 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100808 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200809 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200810 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200811 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200812 * size_t buffer_length;
813 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
814 * if (status != PSA_SUCCESS) handle_error(...);
815 * \endcode
816 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200817 * \param key_type A supported key type.
818 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200819 *
820 * \return If the parameters are valid and supported, return
821 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100822 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200823 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100824 * If the parameters are a valid combination that is not supported,
825 * return either a sensible size or 0.
826 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200827 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100828#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
829 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
830 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200831 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100832 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200833 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100834 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
835 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200836 0)
837
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200838/** Sufficient output buffer size for psa_export_public_key().
839 *
840 * This macro returns a compile-time constant if its arguments are
841 * compile-time constants.
842 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100843 * \warning This macro may evaluate its arguments multiple times or
844 * zero times, so you should not pass arguments that contain
845 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200846 *
847 * The following code illustrates how to allocate enough memory to export
848 * a public key by querying the key type and size at runtime.
849 * \code{c}
850 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
851 * psa_status_t status;
852 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100853 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200854 * psa_key_type_t key_type = psa_get_key_type(&attributes);
855 * size_t key_bits = psa_get_key_bits(&attributes);
856 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
857 * psa_reset_key_attributes(&attributes);
858 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100859 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200860 * size_t buffer_length;
861 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100862 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200863 * \endcode
864 *
865 * \param key_type A public key or key pair key type.
866 * \param key_bits The size of the key in bits.
867 *
868 * \return If the parameters are valid and supported, return
869 * a buffer size in bytes that guarantees that
870 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100871 * #PSA_ERROR_BUFFER_TOO_SMALL.
872 * If the parameters are a valid combination that is not
873 * supported, return either a sensible size or 0.
874 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200875 * the return value is unspecified.
876 *
877 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100878 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200879 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
880 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
881 * \p key_bits).
882 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100883#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
884 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
885 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200886 0)
887
888/** Sufficient buffer size for exporting any asymmetric key pair.
889 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100890 * This macro expands to a compile-time constant integer. This value is
891 * a sufficient buffer size when calling psa_export_key() to export any
892 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200893 *
894 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
895 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100896#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
897 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
898 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
899 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
900 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200901
902/** Sufficient buffer size for exporting any asymmetric public key.
903 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100904 * This macro expands to a compile-time constant integer. This value is
905 * a sufficient buffer size when calling psa_export_key() or
906 * psa_export_public_key() to export any asymmetric public key,
907 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200908 *
909 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
910 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100911#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
912 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
913 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
914 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
915 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200916
917/** Sufficient output buffer size for psa_raw_key_agreement().
918 *
919 * This macro returns a compile-time constant if its arguments are
920 * compile-time constants.
921 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100922 * \warning This macro may evaluate its arguments multiple times or
923 * zero times, so you should not pass arguments that contain
924 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200925 *
926 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
927 *
928 * \param key_type A supported key type.
929 * \param key_bits The size of the key in bits.
930 *
931 * \return If the parameters are valid and supported, return
932 * a buffer size in bytes that guarantees that
933 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100934 * #PSA_ERROR_BUFFER_TOO_SMALL.
935 * If the parameters are a valid combination that
936 * is not supported, return either a sensible size or 0.
937 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200938 * the return value is unspecified.
939 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100940/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200941#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
942 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100943 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200944 0)
945
946/** Maximum size of the output from psa_raw_key_agreement().
947 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100948 * This macro expands to a compile-time constant integer. This value is the
949 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200950 *
951 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
952 */
953#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100954 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200955
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100956/** The default IV size for a cipher algorithm, in bytes.
957 *
958 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
959 * the default IV length for the algorithm.
960 *
961 * This macro can be used to allocate a buffer of sufficient size to
962 * store the IV output from #psa_cipher_generate_iv() when using
963 * a multi-part cipher operation.
964 *
965 * See also #PSA_CIPHER_IV_MAX_SIZE.
966 *
967 * \warning This macro may evaluate its arguments multiple times or
968 * zero times, so you should not pass arguments that contain
969 * side effects.
970 *
971 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
972 *
973 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
974 *
975 * \return The default IV size for the specified key type and algorithm.
976 * If the algorithm does not use an IV, return 0.
977 * If the key type or cipher algorithm is not recognized,
978 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100979 */
980#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100981 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 ((alg) == PSA_ALG_CTR || \
983 (alg) == PSA_ALG_CFB || \
984 (alg) == PSA_ALG_OFB || \
985 (alg) == PSA_ALG_XTS || \
986 (alg) == PSA_ALG_CBC_NO_PADDING || \
987 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100988 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100989 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100990 0)
991
992/** The maximum IV size for all supported cipher algorithms, in bytes.
993 *
994 * See also #PSA_CIPHER_IV_LENGTH().
995 */
996#define PSA_CIPHER_IV_MAX_SIZE 16
997
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200998/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
999 *
1000 * If the size of the output buffer is at least this large, it is guaranteed
1001 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1002 * Depending on the algorithm, the actual size of the output might be smaller.
1003 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001004 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1005 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001006 * \warning This macro may evaluate its arguments multiple times or
1007 * zero times, so you should not pass arguments that contain
1008 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001009 *
1010 * \param key_type A symmetric key type that is compatible with algorithm
1011 * alg.
1012 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1013 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1014 * \param input_length Size of the input in bytes.
1015 *
1016 * \return A sufficient output size for the specified key type and
1017 * algorithm. If the key type or cipher algorithm is not
1018 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001019 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001020 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001021#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1022 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliott9bc96592021-07-08 16:50:01 +01001023 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1025 (input_length) + 1) + \
1026 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001027 (PSA_ALG_IS_CIPHER(alg) ? \
1028 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001030
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001031/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1032 * supported key types and cipher algorithms.
1033 *
1034 * If the size of the output buffer is at least this large, it is guaranteed
1035 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1036 *
1037 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1038 *
1039 * \param input_length Size of the input in bytes.
1040 *
1041 */
1042#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1043 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001044 (input_length) + 1) + \
1045 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001046
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001047/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1048 *
1049 * If the size of the output buffer is at least this large, it is guaranteed
1050 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1051 * Depending on the algorithm, the actual size of the output might be smaller.
1052 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001053 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001054 *
1055 * \param key_type A symmetric key type that is compatible with algorithm
1056 * alg.
1057 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1058 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1059 * \param input_length Size of the input in bytes.
1060 *
1061 * \return A sufficient output size for the specified key type and
1062 * algorithm. If the key type or cipher algorithm is not
1063 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001064 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001065 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001066#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1067 (PSA_ALG_IS_CIPHER(alg) && \
1068 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1069 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001070 0)
1071
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001072/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1073 * supported key types and cipher algorithms.
1074 *
1075 * If the size of the output buffer is at least this large, it is guaranteed
1076 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1077 *
1078 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1079 *
1080 * \param input_length Size of the input in bytes.
1081 */
1082#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1083 (input_length)
1084
1085/** A sufficient output buffer size for psa_cipher_update().
1086 *
1087 * If the size of the output buffer is at least this large, it is guaranteed
1088 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1089 * The actual size of the output might be smaller in any given call.
1090 *
1091 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1092 *
1093 * \param key_type A symmetric key type that is compatible with algorithm
1094 * alg.
1095 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1096 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1097 * \param input_length Size of the input in bytes.
1098 *
1099 * \return A sufficient output size for the specified key type and
1100 * algorithm. If the key type or cipher algorithm is not
1101 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001102 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001103#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1104 (PSA_ALG_IS_CIPHER(alg) ? \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1106 (((alg) == PSA_ALG_CBC_PKCS7 || \
1107 (alg) == PSA_ALG_CBC_NO_PADDING || \
1108 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1109 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001110 input_length) : \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001112 0)
1113
1114/** A sufficient output buffer size for psa_cipher_update(), for any of the
1115 * supported key types and cipher algorithms.
1116 *
1117 * If the size of the output buffer is at least this large, it is guaranteed
1118 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1119 *
1120 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1121 *
1122 * \param input_length Size of the input in bytes.
1123 */
1124#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001125 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001126
1127/** A sufficient ciphertext buffer size for psa_cipher_finish().
1128 *
1129 * If the size of the ciphertext buffer is at least this large, it is
1130 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1131 * ciphertext buffer size. The actual size of the output might be smaller in
1132 * any given call.
1133 *
1134 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1135 *
1136 * \param key_type A symmetric key type that is compatible with algorithm
1137 * alg.
1138 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1139 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1140 * \return A sufficient output size for the specified key type and
1141 * algorithm. If the key type or cipher algorithm is not
1142 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001143 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001144#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1145 (PSA_ALG_IS_CIPHER(alg) ? \
1146 (alg == PSA_ALG_CBC_PKCS7 ? \
1147 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1148 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001149 0)
1150
1151/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1152 * supported key types and cipher algorithms.
1153 *
1154 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1155 */
1156#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1157 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1158
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001159#endif /* PSA_CRYPTO_SIZES_H */