blob: 79e4c5ebea530fbb75f56b0a82535d6f7dd46599 [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
Jamie Fox0e54ebc2019-04-09 14:21:04 +010033#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
34#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
35
Antonio de Angelis04debbd2019-10-14 12:12:52 +010036#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
37 (((length) + (block_size) - 1) / (block_size) * (block_size))
38
Jamie Fox0e54ebc2019-04-09 14:21:04 +010039/** The size of the output of psa_hash_finish(), in bytes.
40 *
41 * This is also the hash size that psa_hash_verify() expects.
42 *
43 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
44 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
45 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
46 * hash algorithm).
47 *
48 * \return The hash size for the specified hash algorithm.
49 * If the hash algorithm is not recognized, return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +010050 */
Maulik Patel13b27cf2021-05-14 11:44:53 +010051#define PSA_HASH_LENGTH(alg) \
52 ( \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010053 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
54 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
55 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
56 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
57 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
58 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
59 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
60 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
61 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
62 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
66 0)
67
Summer Qinf07cc312022-01-05 16:52:54 +080068/** The input block size of a hash algorithm, in bytes.
69 *
70 * Hash algorithms process their input data in blocks. Hash operations will
71 * retain any partial blocks until they have enough input to fill the block or
72 * until the operation is finished.
73 * This affects the output from psa_hash_suspend().
74 *
75 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
76 * PSA_ALG_IS_HASH(\p alg) is true).
77 *
78 * \return The block size in bytes for the specified hash algorithm.
79 * If the hash algorithm is not recognized, return 0.
80 * An implementation can return either 0 or the correct size for a
81 * hash algorithm that it recognizes, but does not support.
82 */
83#define PSA_HASH_BLOCK_LENGTH(alg) \
84 ( \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
87 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
88 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
89 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
90 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
91 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
92 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
93 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
94 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
95 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
96 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
97 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
98 0)
99
Antonio de Angelis377a1552018-11-22 17:02:40 +0000100/** \def PSA_HASH_MAX_SIZE
101 *
102 * Maximum size of a hash.
103 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100104 * This macro expands to a compile-time constant integer. This value
105 * is the maximum size of a hash in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000106 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100107/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
108 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
109 * HMAC-SHA3-512. */
Joakim Andersson779cfe02023-06-30 12:40:23 +0200110#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Summer Qinf07cc312022-01-05 16:52:54 +0800111#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000112#define PSA_HASH_MAX_SIZE 64
113#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
Summer Qin359167d2021-07-05 18:11:50 +0800114#else
115#define PSA_HASH_MAX_SIZE 32
116#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
117#endif
Joakim Andersson779cfe02023-06-30 12:40:23 +0200118#else
119/* Without any PSA configuration we must assume the maximum size possible. */
120#define PSA_HASH_MAX_SIZE 64
121#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
122#endif
Antonio de Angelis377a1552018-11-22 17:02:40 +0000123
124/** \def PSA_MAC_MAX_SIZE
125 *
126 * Maximum size of a MAC.
127 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100128 * This macro expands to a compile-time constant integer. This value
129 * is the maximum size of a MAC in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000130 */
131/* All non-HMAC MACs have a maximum size that's smaller than the
132 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100133/* Note that the encoding of truncated MAC algorithms limits this value
134 * to 64 bytes.
135 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000136#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
137
Summer Qin359167d2021-07-05 18:11:50 +0800138/** The length of a tag for an AEAD algorithm, in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100139 *
Summer Qin359167d2021-07-05 18:11:50 +0800140 * This macro can be used to allocate a buffer of sufficient size to store the
141 * tag output from psa_aead_finish().
142 *
143 * See also #PSA_AEAD_TAG_MAX_SIZE.
144 *
145 * \param key_type The type of the AEAD key.
146 * \param key_bits The size of the AEAD key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100147 * \param alg An AEAD algorithm
148 * (\c PSA_ALG_XXX value such that
149 * #PSA_ALG_IS_AEAD(\p alg) is true).
150 *
Summer Qin359167d2021-07-05 18:11:50 +0800151 * \return The tag length for the specified algorithm and key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100152 * If the AEAD algorithm does not have an identified
153 * tag that can be distinguished from the rest of
154 * the ciphertext, return 0.
Summer Qin359167d2021-07-05 18:11:50 +0800155 * If the key type or AEAD algorithm is not
156 * recognized, or the parameters are incompatible,
157 * return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100158 */
Summer Qin359167d2021-07-05 18:11:50 +0800159#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
160 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
161 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
162 ((void) (key_bits), 0))
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100163
Maulik Patel13b27cf2021-05-14 11:44:53 +0100164/** The maximum tag size for all supported AEAD algorithms, in bytes.
165 *
Summer Qin359167d2021-07-05 18:11:50 +0800166 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100167 */
168#define PSA_AEAD_TAG_MAX_SIZE 16
169
Antonio de Angelis377a1552018-11-22 17:02:40 +0000170/* The maximum size of an RSA key on this implementation, in bits.
171 * This is a vendor-specific macro.
172 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100173 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000174 * whose parameters fit in a bignum is accepted. However large keys can
175 * induce a large memory usage and long computation times. Unlike other
176 * auxiliary macros in this file and in crypto.h, which reflect how the
177 * library is configured, this macro defines how the library is
178 * configured. This implementation refuses to import or generate an
179 * RSA key whose size is larger than the value defined here.
180 *
181 * Note that an implementation may set different size limits for different
182 * operations, and does not need to accept all key sizes up to the limit. */
183#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
184
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100185/* The maximum size of an ECC key on this implementation, in bits.
186 * This is a vendor-specific macro. */
187#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100188#if defined(PSA_WANT_ECC_SECP_R1_521)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100189#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100190#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
191#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
192#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
193#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
194#elif defined(PSA_WANT_ECC_SECP_R1_384)
195#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
196#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
197#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
198#elif defined(PSA_WANT_ECC_SECP_R1_256)
199#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
200#elif defined(PSA_WANT_ECC_SECP_K1_256)
201#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
202#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
203#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
204#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
205#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
206#elif defined(PSA_WANT_ECC_SECP_R1_224)
207#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
208#elif defined(PSA_WANT_ECC_SECP_K1_224)
209#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
210#elif defined(PSA_WANT_ECC_SECP_R1_192)
211#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
212#elif defined(PSA_WANT_ECC_SECP_K1_192)
213#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
214#else
215#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
216#endif
217#else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Joakim Andersson779cfe02023-06-30 12:40:23 +0200218/* Without any PSA configuration we must assume the maximum size possible. */
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100219#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
220#endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100221
Maulik Patel13b27cf2021-05-14 11:44:53 +0100222/** This macro returns the maximum supported length of the PSK for the
223 * TLS-1.2 PSK-to-MS key derivation
Summer Qin359167d2021-07-05 18:11:50 +0800224 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100225 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100226 * The maximum supported length does not depend on the chosen hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100227 *
228 * Quoting RFC 4279, Sect 5.3:
229 * TLS implementations supporting these ciphersuites MUST support
230 * arbitrary PSK identities up to 128 octets in length, and arbitrary
231 * PSKs up to 64 octets in length. Supporting longer identities and
232 * keys is RECOMMENDED.
233 *
234 * Therefore, no implementation should define a value smaller than 64
Maulik Patel13b27cf2021-05-14 11:44:53 +0100235 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100236 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100237#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000238
Summer Qin614002c2023-01-19 15:22:39 +0800239/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
240 * which is expected to work with P-256 curve only. */
241#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65
242
243/* The size of a serialized K.X coordinate to be used in
244 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
245 * curve. */
246#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32
247
Maulik Patel13b27cf2021-05-14 11:44:53 +0100248/** The maximum size of a block cipher. */
249#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
250
Antonio de Angelis377a1552018-11-22 17:02:40 +0000251/** The size of the output of psa_mac_sign_finish(), in bytes.
252 *
253 * This is also the MAC size that psa_mac_verify_finish() expects.
254 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100255 * \warning This macro may evaluate its arguments multiple times or
256 * zero times, so you should not pass arguments that contain
257 * side effects.
258 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000259 * \param key_type The type of the MAC key.
260 * \param key_bits The size of the MAC key in bits.
261 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100262 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000263 *
264 * \return The MAC size for the specified algorithm with
265 * the specified key parameters.
266 * \return 0 if the MAC algorithm is not recognized.
267 * \return Either 0 or the correct size for a MAC algorithm that
268 * the implementation recognizes, but does not support.
269 * \return Unspecified if the key parameters are not consistent
270 * with the algorithm.
271 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100272#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
273 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
274 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
275 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Summer Qind635cd02023-03-31 18:07:38 +0800276 ((void) (key_type), (void) (key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000277
278/** The maximum size of the output of psa_aead_encrypt(), in bytes.
279 *
280 * If the size of the ciphertext buffer is at least this large, it is
281 * guaranteed that psa_aead_encrypt() will not fail due to an
282 * insufficient buffer size. Depending on the algorithm, the actual size of
283 * the ciphertext may be smaller.
284 *
Summer Qin359167d2021-07-05 18:11:50 +0800285 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
286 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100287 * \warning This macro may evaluate its arguments multiple times or
288 * zero times, so you should not pass arguments that contain
289 * side effects.
290 *
Summer Qin359167d2021-07-05 18:11:50 +0800291 * \param key_type A symmetric key type that is
292 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000293 * \param alg An AEAD algorithm
294 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100295 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000296 * \param plaintext_length Size of the plaintext in bytes.
297 *
298 * \return The AEAD ciphertext size for the specified
299 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800300 * If the key type or AEAD algorithm is not
301 * recognized, or the parameters are incompatible,
302 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000303 */
Summer Qin359167d2021-07-05 18:11:50 +0800304#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
305 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
306 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000307 0)
308
Maulik Patel13b27cf2021-05-14 11:44:53 +0100309/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
310 * supported key types and AEAD algorithms.
311 *
312 * If the size of the ciphertext buffer is at least this large, it is guaranteed
313 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
314 *
315 * \note This macro returns a compile-time constant if its arguments are
316 * compile-time constants.
317 *
Summer Qin359167d2021-07-05 18:11:50 +0800318 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
319 * \p plaintext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100320 *
321 * \param plaintext_length Size of the plaintext in bytes.
322 *
323 * \return A sufficient output buffer size for any of the
324 * supported key types and AEAD algorithms.
325 *
326 */
327#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
328 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
329
330
Antonio de Angelis377a1552018-11-22 17:02:40 +0000331/** The maximum size of the output of psa_aead_decrypt(), in bytes.
332 *
333 * If the size of the plaintext buffer is at least this large, it is
334 * guaranteed that psa_aead_decrypt() will not fail due to an
335 * insufficient buffer size. Depending on the algorithm, the actual size of
336 * the plaintext may be smaller.
337 *
Summer Qin359167d2021-07-05 18:11:50 +0800338 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_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 ciphertext_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_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
358 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800359 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
360 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100361 0)
362
Maulik Patel13b27cf2021-05-14 11:44:53 +0100363/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
364 * supported key types and AEAD algorithms.
365 *
366 * If the size of the plaintext buffer is at least this large, it is guaranteed
367 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
368 *
369 * \note This macro returns a compile-time constant if its arguments are
370 * compile-time constants.
371 *
Summer Qin359167d2021-07-05 18:11:50 +0800372 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
373 * \p ciphertext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100374 *
375 * \param ciphertext_length Size of the ciphertext in bytes.
376 *
377 * \return A sufficient output buffer size for any of the
378 * supported key types and AEAD algorithms.
379 *
380 */
381#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Summer Qind635cd02023-03-31 18:07:38 +0800382 (ciphertext_length)
Maulik Patel13b27cf2021-05-14 11:44:53 +0100383
384/** The default nonce size for an AEAD algorithm, in bytes.
385 *
386 * This macro can be used to allocate a buffer of sufficient size to
387 * store the nonce output from #psa_aead_generate_nonce().
388 *
389 * See also #PSA_AEAD_NONCE_MAX_SIZE.
390 *
391 * \note This is not the maximum size of nonce supported as input to
392 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
393 * just the default size that is generated by #psa_aead_generate_nonce().
394 *
395 * \warning This macro may evaluate its arguments multiple times or
396 * zero times, so you should not pass arguments that contain
397 * side effects.
398 *
399 * \param key_type A symmetric key type that is compatible with
400 * algorithm \p alg.
401 *
402 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
403 * #PSA_ALG_IS_AEAD(\p alg) is true).
404 *
405 * \return The default nonce size for the specified key type and algorithm.
406 * If the key type or AEAD algorithm is not recognized,
407 * or the parameters are incompatible, return 0.
408 */
409#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Summer Qin359167d2021-07-05 18:11:50 +0800410 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Summer Qind635cd02023-03-31 18:07:38 +0800411 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
412 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
413 0 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100414 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Summer Qind635cd02023-03-31 18:07:38 +0800415 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100416 0)
417
418/** The maximum default nonce size among all supported pairs of key types and
419 * AEAD algorithms, in bytes.
420 *
421 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
422 * may return.
423 *
424 * \note This is not the maximum size of nonce supported as input to
425 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
426 * just the largest size that may be generated by
427 * #psa_aead_generate_nonce().
428 */
Summer Qin359167d2021-07-05 18:11:50 +0800429#define PSA_AEAD_NONCE_MAX_SIZE 13
Maulik Patel13b27cf2021-05-14 11:44:53 +0100430
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100431/** A sufficient output buffer size for psa_aead_update().
432 *
433 * If the size of the output buffer is at least this large, it is
434 * guaranteed that psa_aead_update() will not fail due to an
435 * insufficient buffer size. The actual size of the output may be smaller
436 * in any given call.
437 *
Summer Qin359167d2021-07-05 18:11:50 +0800438 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
439 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100440 * \warning This macro may evaluate its arguments multiple times or
441 * zero times, so you should not pass arguments that contain
442 * side effects.
443 *
Summer Qin359167d2021-07-05 18:11:50 +0800444 * \param key_type A symmetric key type that is
445 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100446 * \param alg An AEAD algorithm
447 * (\c PSA_ALG_XXX value such that
448 * #PSA_ALG_IS_AEAD(\p alg) is true).
449 * \param input_length Size of the input in bytes.
450 *
451 * \return A sufficient output buffer size for the specified
452 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800453 * If the key type or AEAD algorithm is not
454 * recognized, or the parameters are incompatible,
455 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100456 */
457/* For all the AEAD modes defined in this specification, it is possible
458 * to emit output without delay. However, hardware may not always be
459 * capable of this. So for modes based on a block cipher, allow the
460 * implementation to delay the output until it has a full block. */
Summer Qin359167d2021-07-05 18:11:50 +0800461#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
462 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Summer Qind635cd02023-03-31 18:07:38 +0800463 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
464 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
465 (input_length) : \
Summer Qin359167d2021-07-05 18:11:50 +0800466 0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100467
Maulik Patel13b27cf2021-05-14 11:44:53 +0100468/** A sufficient output buffer size for psa_aead_update(), for any of the
469 * supported key types and AEAD algorithms.
470 *
471 * If the size of the output buffer is at least this large, it is guaranteed
472 * that psa_aead_update() will not fail due to an insufficient buffer size.
473 *
Summer Qin359167d2021-07-05 18:11:50 +0800474 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100475 *
476 * \param input_length Size of the input in bytes.
477 */
478#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
479 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
480
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100481/** A sufficient ciphertext buffer size for psa_aead_finish().
482 *
483 * If the size of the ciphertext buffer is at least this large, it is
484 * guaranteed that psa_aead_finish() will not fail due to an
485 * insufficient ciphertext buffer size. The actual size of the output may
486 * be smaller in any given call.
487 *
Summer Qin359167d2021-07-05 18:11:50 +0800488 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
489 *
490 * \param key_type A symmetric key type that is
491 compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100492 * \param alg An AEAD algorithm
493 * (\c PSA_ALG_XXX value such that
494 * #PSA_ALG_IS_AEAD(\p alg) is true).
495 *
496 * \return A sufficient ciphertext buffer size for the
497 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800498 * If the key type or AEAD algorithm is not
499 * recognized, or the parameters are incompatible,
500 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100501 */
Summer Qin359167d2021-07-05 18:11:50 +0800502#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
503 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800504 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
505 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100506 0)
507
Maulik Patel13b27cf2021-05-14 11:44:53 +0100508/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
509 * supported key types and AEAD algorithms.
510 *
Summer Qin359167d2021-07-05 18:11:50 +0800511 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100512 */
513#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
514
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100515/** A sufficient plaintext buffer size for psa_aead_verify().
516 *
517 * If the size of the plaintext buffer is at least this large, it is
518 * guaranteed that psa_aead_verify() will not fail due to an
519 * insufficient plaintext buffer size. The actual size of the output may
520 * be smaller in any given call.
521 *
Summer Qin359167d2021-07-05 18:11:50 +0800522 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
523 *
524 * \param key_type A symmetric key type that is
525 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100526 * \param alg An AEAD algorithm
527 * (\c PSA_ALG_XXX value such that
528 * #PSA_ALG_IS_AEAD(\p alg) is true).
529 *
530 * \return A sufficient plaintext buffer size for the
531 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800532 * If the key type or AEAD algorithm is not
533 * recognized, or the parameters are incompatible,
534 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100535 */
Summer Qin359167d2021-07-05 18:11:50 +0800536#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
537 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800538 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
539 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000540 0)
541
Maulik Patel13b27cf2021-05-14 11:44:53 +0100542/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
543 * supported key types and AEAD algorithms.
544 *
Summer Qin359167d2021-07-05 18:11:50 +0800545 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100546 */
547#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
548
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100549#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
550 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100551 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100552 11 /*PKCS#1v1.5*/)
553
554/**
555 * \brief ECDSA signature size for a given curve bit size
556 *
557 * \param curve_bits Curve size in bits.
558 * \return Signature size in bytes.
559 *
560 * \note This macro returns a compile-time constant if its argument is one.
561 */
562#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
563 (PSA_BITS_TO_BYTES(curve_bits) * 2)
564
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100565/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000566 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100567 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000568 * of the specified type and size, with the specified algorithm.
569 * Note that the actual size of the signature may be smaller
570 * (some algorithms produce a variable-size signature).
571 *
572 * \warning This function may call its arguments multiple times or
573 * zero times, so you should not pass arguments that contain
574 * side effects.
575 *
576 * \param key_type An asymmetric key type (this may indifferently be a
577 * key pair type or a public key type).
578 * \param key_bits The size of the key in bits.
579 * \param alg The signature algorithm.
580 *
581 * \return If the parameters are valid and supported, return
582 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100583 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000584 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100585 * If the parameters are a valid combination that is not supported,
586 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000587 * If the parameters are not valid, the
588 * return value is unspecified.
589 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100590#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Summer Qind635cd02023-03-31 18:07:38 +0800591 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000592 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Summer Qind635cd02023-03-31 18:07:38 +0800593 ((void) alg, 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000594
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100595#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
596 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
597
598/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000599 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100600 * Maximum size of an asymmetric signature.
601 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100602 * This macro expands to a compile-time constant integer. This value
603 * is the maximum size of a signature in bytes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100604 */
605#define PSA_SIGNATURE_MAX_SIZE \
606 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
607 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
608 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
609
610/** Sufficient output buffer size for psa_asymmetric_encrypt().
611 *
612 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000613 * a key of the specified type and size, with the specified algorithm.
614 * Note that the actual size of the ciphertext may be smaller, depending
615 * on the algorithm.
616 *
617 * \warning This function may call its arguments multiple times or
618 * zero times, so you should not pass arguments that contain
619 * side effects.
620 *
621 * \param key_type An asymmetric key type (this may indifferently be a
622 * key pair type or a public key type).
623 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100624 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000625 *
626 * \return If the parameters are valid and supported, return
627 * a buffer size in bytes that guarantees that
628 * psa_asymmetric_encrypt() will not fail with
629 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100630 * If the parameters are a valid combination that is not supported,
631 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000632 * If the parameters are not valid, the
633 * return value is unspecified.
634 */
635#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
636 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Summer Qind635cd02023-03-31 18:07:38 +0800637 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000638 0)
639
Maulik Patel13b27cf2021-05-14 11:44:53 +0100640/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
641 * supported asymmetric encryption.
642 *
643 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
644 */
645/* This macro assumes that RSA is the only supported asymmetric encryption. */
646#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
647 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
648
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100649/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000650 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100651 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000652 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100653 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000654 * on the algorithm.
655 *
656 * \warning This function may call its arguments multiple times or
657 * zero times, so you should not pass arguments that contain
658 * side effects.
659 *
660 * \param key_type An asymmetric key type (this may indifferently be a
661 * key pair type or a public key type).
662 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100663 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000664 *
665 * \return If the parameters are valid and supported, return
666 * a buffer size in bytes that guarantees that
667 * psa_asymmetric_decrypt() will not fail with
668 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100669 * If the parameters are a valid combination that is not supported,
670 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000671 * If the parameters are not valid, the
672 * return value is unspecified.
673 */
674#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
675 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
676 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
677 0)
678
Maulik Patel13b27cf2021-05-14 11:44:53 +0100679/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
680 * supported asymmetric decryption.
681 *
682 * This macro assumes that RSA is the only supported asymmetric encryption.
683 *
684 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
685 */
686#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
687 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
688
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100689/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
690 * number of bits.
691 *
692 * This definition assumes that bits <= 2^19 - 9 so that the length field
693 * is at most 3 bytes. The length of the encoding is the length of the
694 * bit string padded to a whole number of bytes plus:
695 * - 1 type byte;
696 * - 1 to 3 length bytes;
697 * - 0 to 1 bytes of leading 0 due to the sign bit.
698 */
699#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
700 ((bits) / 8 + 5)
701
702/* Maximum size of the export encoding of an RSA public key.
703 * Assumes that the public exponent is less than 2^32.
704 *
705 * RSAPublicKey ::= SEQUENCE {
706 * modulus INTEGER, -- n
707 * publicExponent INTEGER } -- e
708 *
709 * - 4 bytes of SEQUENCE overhead;
710 * - n : INTEGER;
711 * - 7 bytes for the public exponent.
712 */
713#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
714 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
715
716/* Maximum size of the export encoding of an RSA key pair.
Summer Qin614002c2023-01-19 15:22:39 +0800717 * Assumes that the public exponent is less than 2^32 and that the size
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100718 * difference between the two primes is at most 1 bit.
719 *
720 * RSAPrivateKey ::= SEQUENCE {
721 * version Version, -- 0
722 * modulus INTEGER, -- N-bit
723 * publicExponent INTEGER, -- 32-bit
724 * privateExponent INTEGER, -- N-bit
725 * prime1 INTEGER, -- N/2-bit
726 * prime2 INTEGER, -- N/2-bit
727 * exponent1 INTEGER, -- N/2-bit
728 * exponent2 INTEGER, -- N/2-bit
729 * coefficient INTEGER, -- N/2-bit
730 * }
731 *
732 * - 4 bytes of SEQUENCE overhead;
733 * - 3 bytes of version;
734 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
735 * overapproximated as 9 half-size INTEGERS;
736 * - 7 bytes for the public exponent.
737 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100738#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100739 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
740
741/* Maximum size of the export encoding of a DSA public key.
742 *
743 * SubjectPublicKeyInfo ::= SEQUENCE {
744 * algorithm AlgorithmIdentifier,
745 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
746 * AlgorithmIdentifier ::= SEQUENCE {
747 * algorithm OBJECT IDENTIFIER,
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100748 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100749 * DSAPublicKey ::= INTEGER -- public key, Y
750 *
751 * - 3 * 4 bytes of SEQUENCE overhead;
752 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
753 * - 4 bytes of BIT STRING overhead;
754 * - 3 full-size INTEGERs (p, g, y);
755 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
756 */
757#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
758 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
759
760/* Maximum size of the export encoding of a DSA key pair.
761 *
762 * DSAPrivateKey ::= SEQUENCE {
763 * version Version, -- 0
764 * prime INTEGER, -- p
765 * subprime INTEGER, -- q
766 * generator INTEGER, -- g
767 * public INTEGER, -- y
768 * private INTEGER, -- x
769 * }
770 *
771 * - 4 bytes of SEQUENCE overhead;
772 * - 3 bytes of version;
773 * - 3 full-size INTEGERs (p, g, y);
774 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
775 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100776#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100777 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
778
779/* Maximum size of the export encoding of an ECC public key.
780 *
781 * The representation of an ECC public key is:
782 * - The byte 0x04;
783 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
784 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
785 * - where m is the bit size associated with the curve.
786 *
787 * - 1 byte + 2 * point size.
788 */
789#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
790 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
791
792/* Maximum size of the export encoding of an ECC key pair.
793 *
794 * An ECC key pair is represented by the secret value.
795 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100796#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100797 (PSA_BITS_TO_BYTES(key_bits))
798
Maulik Patel13b27cf2021-05-14 11:44:53 +0100799/** Sufficient output buffer size for psa_export_key() or
800 * psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100801 *
802 * This macro returns a compile-time constant if its arguments are
803 * compile-time constants.
804 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100805 * \warning This macro may evaluate its arguments multiple times or
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100806 * zero times, so you should not pass arguments that contain
807 * side effects.
808 *
809 * The following code illustrates how to allocate enough memory to export
810 * a key by querying the key type and size at runtime.
811 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100812 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100813 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100814 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100815 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100816 * psa_key_type_t key_type = psa_get_key_type(&attributes);
817 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100818 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100819 * psa_reset_key_attributes(&attributes);
820 * uint8_t *buffer = malloc(buffer_size);
821 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100822 * size_t buffer_length;
823 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
824 * if (status != PSA_SUCCESS) handle_error(...);
825 * \endcode
826 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100827 * \param key_type A supported key type.
828 * \param key_bits The size of the key in bits.
829 *
830 * \return If the parameters are valid and supported, return
831 * a buffer size in bytes that guarantees that
832 * psa_export_key() or psa_export_public_key() will not fail with
833 * #PSA_ERROR_BUFFER_TOO_SMALL.
834 * If the parameters are a valid combination that is not supported,
835 * return either a sensible size or 0.
836 * If the parameters are not valid, the return value is unspecified.
837 */
838#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
839 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
840 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
841 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
842 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
843 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
844 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
845 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
846 0)
847
848/** Sufficient output buffer size for psa_export_public_key().
849 *
850 * This macro returns a compile-time constant if its arguments are
851 * compile-time constants.
852 *
853 * \warning This macro may evaluate its arguments multiple times or
854 * zero times, so you should not pass arguments that contain
855 * side effects.
856 *
857 * The following code illustrates how to allocate enough memory to export
858 * a public key by querying the key type and size at runtime.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100859 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100860 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100861 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100862 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100863 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100864 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100865 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100866 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100867 * psa_reset_key_attributes(&attributes);
868 * uint8_t *buffer = malloc(buffer_size);
869 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100870 * size_t buffer_length;
871 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
872 * if (status != PSA_SUCCESS) handle_error(...);
873 * \endcode
874 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100875 * \param key_type A public key or key pair key type.
876 * \param key_bits The size of the key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100877 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100878 * \return If the parameters are valid and supported, return
879 * a buffer size in bytes that guarantees that
880 * psa_export_public_key() will not fail with
881 * #PSA_ERROR_BUFFER_TOO_SMALL.
882 * If the parameters are a valid combination that is not
883 * supported, return either a sensible size or 0.
884 * If the parameters are not valid,
885 * the return value is unspecified.
886 *
887 * If the parameters are valid and supported,
888 * return the same result as
889 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
890 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
891 * \p key_bits).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100892 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100893#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
894 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
895 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100896 0)
897
Maulik Patel13b27cf2021-05-14 11:44:53 +0100898/** Sufficient buffer size for exporting any asymmetric key pair.
Maulik Patel28659c42021-01-06 14:09:22 +0000899 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100900 * This macro expands to a compile-time constant integer. This value is
901 * a sufficient buffer size when calling psa_export_key() to export any
902 * asymmetric key pair, regardless of the exact key type and key size.
Maulik Patel28659c42021-01-06 14:09:22 +0000903 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100904 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
905 */
906#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
907 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
908 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
909 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
910 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
911
912/** Sufficient buffer size for exporting any asymmetric public key.
Maulik Patel28659c42021-01-06 14:09:22 +0000913 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100914 * This macro expands to a compile-time constant integer. This value is
915 * a sufficient buffer size when calling psa_export_key() or
916 * psa_export_public_key() to export any asymmetric public key,
917 * regardless of the exact key type and key size.
918 *
919 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
920 */
921#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
922 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
923 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
924 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
925 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
926
927/** Sufficient output buffer size for psa_raw_key_agreement().
928 *
929 * This macro returns a compile-time constant if its arguments are
930 * compile-time constants.
Maulik Patel28659c42021-01-06 14:09:22 +0000931 *
932 * \warning This macro may evaluate its arguments multiple times or
933 * zero times, so you should not pass arguments that contain
934 * side effects.
935 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100936 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
Maulik Patel28659c42021-01-06 14:09:22 +0000937 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100938 * \param key_type A supported key type.
939 * \param key_bits The size of the key in bits.
Maulik Patel28659c42021-01-06 14:09:22 +0000940 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100941 * \return If the parameters are valid and supported, return
942 * a buffer size in bytes that guarantees that
943 * psa_raw_key_agreement() will not fail with
944 * #PSA_ERROR_BUFFER_TOO_SMALL.
945 * If the parameters are a valid combination that
946 * is not supported, return either a sensible size or 0.
947 * If the parameters are not valid,
948 * the return value is unspecified.
Maulik Patel28659c42021-01-06 14:09:22 +0000949 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100950/* FFDH is not yet supported in PSA. */
951#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
952 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
953 PSA_BITS_TO_BYTES(key_bits) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000954 0)
955
Maulik Patel13b27cf2021-05-14 11:44:53 +0100956/** Maximum size of the output from psa_raw_key_agreement().
Maulik Patel28659c42021-01-06 14:09:22 +0000957 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100958 * This macro expands to a compile-time constant integer. This value is the
959 * maximum size of the output any raw key agreement algorithm, in bytes.
Maulik Patel28659c42021-01-06 14:09:22 +0000960 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100961 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
Maulik Patel28659c42021-01-06 14:09:22 +0000962 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100963#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
964 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
Maulik Patel28659c42021-01-06 14:09:22 +0000965
966/** The default IV size for a cipher algorithm, in bytes.
967 *
968 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
969 * the default IV length for the algorithm.
970 *
971 * This macro can be used to allocate a buffer of sufficient size to
972 * store the IV output from #psa_cipher_generate_iv() when using
973 * a multi-part cipher operation.
974 *
975 * See also #PSA_CIPHER_IV_MAX_SIZE.
976 *
977 * \warning This macro may evaluate its arguments multiple times or
978 * zero times, so you should not pass arguments that contain
979 * side effects.
980 *
981 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
982 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100983 * \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 +0000984 *
985 * \return The default IV size for the specified key type and algorithm.
986 * If the algorithm does not use an IV, return 0.
987 * If the key type or cipher algorithm is not recognized,
988 * or the parameters are incompatible, return 0.
Maulik Patel28659c42021-01-06 14:09:22 +0000989 */
990#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100991 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Summer Qind635cd02023-03-31 18:07:38 +0800992 ((alg) == PSA_ALG_CTR || \
993 (alg) == PSA_ALG_CFB || \
994 (alg) == PSA_ALG_OFB || \
995 (alg) == PSA_ALG_XTS || \
996 (alg) == PSA_ALG_CBC_NO_PADDING || \
997 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000998 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Summer Qind635cd02023-03-31 18:07:38 +0800999 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
1000 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
1001 0)
Maulik Patel28659c42021-01-06 14:09:22 +00001002
1003/** The maximum IV size for all supported cipher algorithms, in bytes.
1004 *
1005 * See also #PSA_CIPHER_IV_LENGTH().
1006 */
1007#define PSA_CIPHER_IV_MAX_SIZE 16
1008
Maulik Patel13b27cf2021-05-14 11:44:53 +01001009/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1010 *
1011 * If the size of the output buffer is at least this large, it is guaranteed
1012 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1013 * Depending on the algorithm, the actual size of the output might be smaller.
1014 *
1015 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1016 *
1017 * \warning This macro may evaluate its arguments multiple times or
1018 * zero times, so you should not pass arguments that contain
1019 * side effects.
1020 *
1021 * \param key_type A symmetric key type that is compatible with algorithm
1022 * alg.
1023 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1024 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1025 * \param input_length Size of the input in bytes.
1026 *
1027 * \return A sufficient output size for the specified key type and
1028 * algorithm. If the key type or cipher algorithm is not
1029 * recognized, or the parameters are incompatible,
1030 * return 0.
1031 */
1032#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1033 (alg == PSA_ALG_CBC_PKCS7 ? \
Summer Qinf07cc312022-01-05 16:52:54 +08001034 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Summer Qind635cd02023-03-31 18:07:38 +08001035 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1036 (input_length) + 1) + \
1037 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001038 (PSA_ALG_IS_CIPHER(alg) ? \
1039 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Summer Qind635cd02023-03-31 18:07:38 +08001040 0))
Maulik Patel13b27cf2021-05-14 11:44:53 +01001041
1042/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1043 * supported key types and cipher algorithms.
1044 *
1045 * If the size of the output buffer is at least this large, it is guaranteed
1046 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1047 *
1048 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1049 *
1050 * \param input_length Size of the input in bytes.
1051 *
1052 */
1053#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1054 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1055 (input_length) + 1) + \
1056 PSA_CIPHER_IV_MAX_SIZE)
1057
1058/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1059 *
1060 * If the size of the output buffer is at least this large, it is guaranteed
1061 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1062 * Depending on the algorithm, the actual size of the output might be smaller.
1063 *
1064 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
1065 *
1066 * \param key_type A symmetric key type that is compatible with algorithm
1067 * alg.
1068 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1069 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1070 * \param input_length Size of the input in bytes.
1071 *
1072 * \return A sufficient output size for the specified key type and
1073 * algorithm. If the key type or cipher algorithm is not
1074 * recognized, or the parameters are incompatible,
1075 * return 0.
1076 */
1077#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1078 (PSA_ALG_IS_CIPHER(alg) && \
1079 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1080 (input_length) : \
1081 0)
1082
1083/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1084 * supported key types and cipher algorithms.
1085 *
1086 * If the size of the output buffer is at least this large, it is guaranteed
1087 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1088 *
1089 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1090 *
1091 * \param input_length Size of the input in bytes.
1092 */
1093#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1094 (input_length)
1095
1096/** A sufficient output buffer size for psa_cipher_update().
1097 *
1098 * If the size of the output buffer is at least this large, it is guaranteed
1099 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1100 * The actual size of the output might be smaller in any given call.
1101 *
1102 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1103 *
1104 * \param key_type A symmetric key type that is compatible with algorithm
1105 * alg.
1106 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1107 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1108 * \param input_length Size of the input in bytes.
1109 *
1110 * \return A sufficient output size for the specified key type and
1111 * algorithm. If the key type or cipher algorithm is not
1112 * recognized, or the parameters are incompatible, return 0.
1113 */
1114#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1115 (PSA_ALG_IS_CIPHER(alg) ? \
Summer Qind635cd02023-03-31 18:07:38 +08001116 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1117 (((alg) == PSA_ALG_CBC_PKCS7 || \
1118 (alg) == PSA_ALG_CBC_NO_PADDING || \
1119 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1120 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001121 input_length) : \
Summer Qind635cd02023-03-31 18:07:38 +08001122 (input_length)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001123 0)
1124
1125/** A sufficient output buffer size for psa_cipher_update(), for any of the
1126 * supported key types and cipher algorithms.
1127 *
1128 * If the size of the output buffer is at least this large, it is guaranteed
1129 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1130 *
1131 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1132 *
1133 * \param input_length Size of the input in bytes.
1134 */
1135#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1136 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1137
1138/** A sufficient ciphertext buffer size for psa_cipher_finish().
1139 *
1140 * If the size of the ciphertext buffer is at least this large, it is
1141 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1142 * ciphertext buffer size. The actual size of the output might be smaller in
1143 * any given call.
1144 *
1145 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1146 *
1147 * \param key_type A symmetric key type that is compatible with algorithm
1148 * alg.
1149 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1150 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1151 * \return A sufficient output size for the specified key type and
1152 * algorithm. If the key type or cipher algorithm is not
1153 * recognized, or the parameters are incompatible, return 0.
1154 */
1155#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1156 (PSA_ALG_IS_CIPHER(alg) ? \
1157 (alg == PSA_ALG_CBC_PKCS7 ? \
1158 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1159 0) : \
1160 0)
1161
1162/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1163 * supported key types and cipher algorithms.
1164 *
1165 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1166 */
1167#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1168 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1169
Antonio de Angelis377a1552018-11-22 17:02:40 +00001170#endif /* PSA_CRYPTO_SIZES_H */