blob: 14aa6b5144a14000f257fc1a5ce3cf4325406945 [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. */
Summer Qinf07cc312022-01-05 16:52:54 +0800110#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000111#define PSA_HASH_MAX_SIZE 64
112#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
Summer Qin359167d2021-07-05 18:11:50 +0800113#else
114#define PSA_HASH_MAX_SIZE 32
115#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
116#endif
Antonio de Angelis377a1552018-11-22 17:02:40 +0000117
118/** \def PSA_MAC_MAX_SIZE
119 *
120 * Maximum size of a MAC.
121 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100122 * This macro expands to a compile-time constant integer. This value
123 * is the maximum size of a MAC in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000124 */
125/* All non-HMAC MACs have a maximum size that's smaller than the
126 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100127/* Note that the encoding of truncated MAC algorithms limits this value
128 * to 64 bytes.
129 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000130#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
131
Summer Qin359167d2021-07-05 18:11:50 +0800132/** The length of a tag for an AEAD algorithm, in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100133 *
Summer Qin359167d2021-07-05 18:11:50 +0800134 * This macro can be used to allocate a buffer of sufficient size to store the
135 * tag output from psa_aead_finish().
136 *
137 * See also #PSA_AEAD_TAG_MAX_SIZE.
138 *
139 * \param key_type The type of the AEAD key.
140 * \param key_bits The size of the AEAD key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100141 * \param alg An AEAD algorithm
142 * (\c PSA_ALG_XXX value such that
143 * #PSA_ALG_IS_AEAD(\p alg) is true).
144 *
Summer Qin359167d2021-07-05 18:11:50 +0800145 * \return The tag length for the specified algorithm and key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100146 * If the AEAD algorithm does not have an identified
147 * tag that can be distinguished from the rest of
148 * the ciphertext, return 0.
Summer Qin359167d2021-07-05 18:11:50 +0800149 * If the key type or AEAD algorithm is not
150 * recognized, or the parameters are incompatible,
151 * return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100152 */
Summer Qin359167d2021-07-05 18:11:50 +0800153#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
154 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
155 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
156 ((void) (key_bits), 0))
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100157
Maulik Patel13b27cf2021-05-14 11:44:53 +0100158/** The maximum tag size for all supported AEAD algorithms, in bytes.
159 *
Summer Qin359167d2021-07-05 18:11:50 +0800160 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100161 */
162#define PSA_AEAD_TAG_MAX_SIZE 16
163
Antonio de Angelis377a1552018-11-22 17:02:40 +0000164/* The maximum size of an RSA key on this implementation, in bits.
165 * This is a vendor-specific macro.
166 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100167 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000168 * whose parameters fit in a bignum is accepted. However large keys can
169 * induce a large memory usage and long computation times. Unlike other
170 * auxiliary macros in this file and in crypto.h, which reflect how the
171 * library is configured, this macro defines how the library is
172 * configured. This implementation refuses to import or generate an
173 * RSA key whose size is larger than the value defined here.
174 *
175 * Note that an implementation may set different size limits for different
176 * operations, and does not need to accept all key sizes up to the limit. */
177#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
178
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100179/* The maximum size of an ECC key on this implementation, in bits.
180 * This is a vendor-specific macro. */
181#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100182#if defined(PSA_WANT_ECC_SECP_R1_521)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100183#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100184#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
185#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
186#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
187#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
188#elif defined(PSA_WANT_ECC_SECP_R1_384)
189#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
190#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
191#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
192#elif defined(PSA_WANT_ECC_SECP_R1_256)
193#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
194#elif defined(PSA_WANT_ECC_SECP_K1_256)
195#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
196#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
197#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
198#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
199#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
200#elif defined(PSA_WANT_ECC_SECP_R1_224)
201#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
202#elif defined(PSA_WANT_ECC_SECP_K1_224)
203#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
204#elif defined(PSA_WANT_ECC_SECP_R1_192)
205#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
206#elif defined(PSA_WANT_ECC_SECP_K1_192)
207#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
208#else
209#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
210#endif
211#else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
212#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
213#endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100214
Maulik Patel13b27cf2021-05-14 11:44:53 +0100215/** This macro returns the maximum supported length of the PSK for the
216 * TLS-1.2 PSK-to-MS key derivation
Summer Qin359167d2021-07-05 18:11:50 +0800217 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100218 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100219 * The maximum supported length does not depend on the chosen hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100220 *
221 * Quoting RFC 4279, Sect 5.3:
222 * TLS implementations supporting these ciphersuites MUST support
223 * arbitrary PSK identities up to 128 octets in length, and arbitrary
224 * PSKs up to 64 octets in length. Supporting longer identities and
225 * keys is RECOMMENDED.
226 *
227 * Therefore, no implementation should define a value smaller than 64
Maulik Patel13b27cf2021-05-14 11:44:53 +0100228 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100229 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100230#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000231
Summer Qin614002c2023-01-19 15:22:39 +0800232/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
233 * which is expected to work with P-256 curve only. */
234#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65
235
236/* The size of a serialized K.X coordinate to be used in
237 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
238 * curve. */
239#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32
240
Maulik Patel13b27cf2021-05-14 11:44:53 +0100241/** The maximum size of a block cipher. */
242#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
243
Antonio de Angelis377a1552018-11-22 17:02:40 +0000244/** The size of the output of psa_mac_sign_finish(), in bytes.
245 *
246 * This is also the MAC size that psa_mac_verify_finish() expects.
247 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100248 * \warning This macro may evaluate its arguments multiple times or
249 * zero times, so you should not pass arguments that contain
250 * side effects.
251 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000252 * \param key_type The type of the MAC key.
253 * \param key_bits The size of the MAC key in bits.
254 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100255 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000256 *
257 * \return The MAC size for the specified algorithm with
258 * the specified key parameters.
259 * \return 0 if the MAC algorithm is not recognized.
260 * \return Either 0 or the correct size for a MAC algorithm that
261 * the implementation recognizes, but does not support.
262 * \return Unspecified if the key parameters are not consistent
263 * with the algorithm.
264 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100265#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
266 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
267 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
268 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Summer Qind635cd02023-03-31 18:07:38 +0800269 ((void) (key_type), (void) (key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000270
271/** The maximum size of the output of psa_aead_encrypt(), in bytes.
272 *
273 * If the size of the ciphertext buffer is at least this large, it is
274 * guaranteed that psa_aead_encrypt() will not fail due to an
275 * insufficient buffer size. Depending on the algorithm, the actual size of
276 * the ciphertext may be smaller.
277 *
Summer Qin359167d2021-07-05 18:11:50 +0800278 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
279 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100280 * \warning This macro may evaluate its arguments multiple times or
281 * zero times, so you should not pass arguments that contain
282 * side effects.
283 *
Summer Qin359167d2021-07-05 18:11:50 +0800284 * \param key_type A symmetric key type that is
285 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000286 * \param alg An AEAD algorithm
287 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100288 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000289 * \param plaintext_length Size of the plaintext in bytes.
290 *
291 * \return The AEAD ciphertext size for the specified
292 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800293 * If the key type or AEAD algorithm is not
294 * recognized, or the parameters are incompatible,
295 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000296 */
Summer Qin359167d2021-07-05 18:11:50 +0800297#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
298 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
299 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000300 0)
301
Maulik Patel13b27cf2021-05-14 11:44:53 +0100302/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
303 * supported key types and AEAD algorithms.
304 *
305 * If the size of the ciphertext buffer is at least this large, it is guaranteed
306 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
307 *
308 * \note This macro returns a compile-time constant if its arguments are
309 * compile-time constants.
310 *
Summer Qin359167d2021-07-05 18:11:50 +0800311 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
312 * \p plaintext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100313 *
314 * \param plaintext_length Size of the plaintext in bytes.
315 *
316 * \return A sufficient output buffer size for any of the
317 * supported key types and AEAD algorithms.
318 *
319 */
320#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
321 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
322
323
Antonio de Angelis377a1552018-11-22 17:02:40 +0000324/** The maximum size of the output of psa_aead_decrypt(), in bytes.
325 *
326 * If the size of the plaintext buffer is at least this large, it is
327 * guaranteed that psa_aead_decrypt() will not fail due to an
328 * insufficient buffer size. Depending on the algorithm, the actual size of
329 * the plaintext may be smaller.
330 *
Summer Qin359167d2021-07-05 18:11:50 +0800331 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
332 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100333 * \warning This macro may evaluate its arguments multiple times or
334 * zero times, so you should not pass arguments that contain
335 * side effects.
336 *
Summer Qin359167d2021-07-05 18:11:50 +0800337 * \param key_type A symmetric key type that is
338 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000339 * \param alg An AEAD algorithm
340 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100341 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000342 * \param ciphertext_length Size of the plaintext in bytes.
343 *
344 * \return The AEAD ciphertext size for the specified
345 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800346 * If the key type or AEAD algorithm is not
347 * recognized, or the parameters are incompatible,
348 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000349 */
Summer Qin359167d2021-07-05 18:11:50 +0800350#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
351 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800352 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
353 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100354 0)
355
Maulik Patel13b27cf2021-05-14 11:44:53 +0100356/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
357 * supported key types and AEAD algorithms.
358 *
359 * If the size of the plaintext buffer is at least this large, it is guaranteed
360 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
361 *
362 * \note This macro returns a compile-time constant if its arguments are
363 * compile-time constants.
364 *
Summer Qin359167d2021-07-05 18:11:50 +0800365 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
366 * \p ciphertext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100367 *
368 * \param ciphertext_length Size of the ciphertext in bytes.
369 *
370 * \return A sufficient output buffer size for any of the
371 * supported key types and AEAD algorithms.
372 *
373 */
374#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Summer Qind635cd02023-03-31 18:07:38 +0800375 (ciphertext_length)
Maulik Patel13b27cf2021-05-14 11:44:53 +0100376
377/** The default nonce size for an AEAD algorithm, in bytes.
378 *
379 * This macro can be used to allocate a buffer of sufficient size to
380 * store the nonce output from #psa_aead_generate_nonce().
381 *
382 * See also #PSA_AEAD_NONCE_MAX_SIZE.
383 *
384 * \note This is not the maximum size of nonce supported as input to
385 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
386 * just the default size that is generated by #psa_aead_generate_nonce().
387 *
388 * \warning This macro may evaluate its arguments multiple times or
389 * zero times, so you should not pass arguments that contain
390 * side effects.
391 *
392 * \param key_type A symmetric key type that is compatible with
393 * algorithm \p alg.
394 *
395 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
396 * #PSA_ALG_IS_AEAD(\p alg) is true).
397 *
398 * \return The default nonce size for the specified key type and algorithm.
399 * If the key type or AEAD algorithm is not recognized,
400 * or the parameters are incompatible, return 0.
401 */
402#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Summer Qin359167d2021-07-05 18:11:50 +0800403 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Summer Qind635cd02023-03-31 18:07:38 +0800404 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
405 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
406 0 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100407 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Summer Qind635cd02023-03-31 18:07:38 +0800408 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100409 0)
410
411/** The maximum default nonce size among all supported pairs of key types and
412 * AEAD algorithms, in bytes.
413 *
414 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
415 * may return.
416 *
417 * \note This is not the maximum size of nonce supported as input to
418 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
419 * just the largest size that may be generated by
420 * #psa_aead_generate_nonce().
421 */
Summer Qin359167d2021-07-05 18:11:50 +0800422#define PSA_AEAD_NONCE_MAX_SIZE 13
Maulik Patel13b27cf2021-05-14 11:44:53 +0100423
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100424/** A sufficient output buffer size for psa_aead_update().
425 *
426 * If the size of the output buffer is at least this large, it is
427 * guaranteed that psa_aead_update() will not fail due to an
428 * insufficient buffer size. The actual size of the output may be smaller
429 * in any given call.
430 *
Summer Qin359167d2021-07-05 18:11:50 +0800431 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
432 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100433 * \warning This macro may evaluate its arguments multiple times or
434 * zero times, so you should not pass arguments that contain
435 * side effects.
436 *
Summer Qin359167d2021-07-05 18:11:50 +0800437 * \param key_type A symmetric key type that is
438 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100439 * \param alg An AEAD algorithm
440 * (\c PSA_ALG_XXX value such that
441 * #PSA_ALG_IS_AEAD(\p alg) is true).
442 * \param input_length Size of the input in bytes.
443 *
444 * \return A sufficient output buffer size for the specified
445 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800446 * If the key type or AEAD algorithm is not
447 * recognized, or the parameters are incompatible,
448 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100449 */
450/* For all the AEAD modes defined in this specification, it is possible
451 * to emit output without delay. However, hardware may not always be
452 * capable of this. So for modes based on a block cipher, allow the
453 * implementation to delay the output until it has a full block. */
Summer Qin359167d2021-07-05 18:11:50 +0800454#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
455 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Summer Qind635cd02023-03-31 18:07:38 +0800456 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
457 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
458 (input_length) : \
Summer Qin359167d2021-07-05 18:11:50 +0800459 0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100460
Maulik Patel13b27cf2021-05-14 11:44:53 +0100461/** A sufficient output buffer size for psa_aead_update(), for any of the
462 * supported key types and AEAD algorithms.
463 *
464 * If the size of the output buffer is at least this large, it is guaranteed
465 * that psa_aead_update() will not fail due to an insufficient buffer size.
466 *
Summer Qin359167d2021-07-05 18:11:50 +0800467 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100468 *
469 * \param input_length Size of the input in bytes.
470 */
471#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
472 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
473
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100474/** A sufficient ciphertext buffer size for psa_aead_finish().
475 *
476 * If the size of the ciphertext buffer is at least this large, it is
477 * guaranteed that psa_aead_finish() will not fail due to an
478 * insufficient ciphertext buffer size. The actual size of the output may
479 * be smaller in any given call.
480 *
Summer Qin359167d2021-07-05 18:11:50 +0800481 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
482 *
483 * \param key_type A symmetric key type that is
484 compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100485 * \param alg An AEAD algorithm
486 * (\c PSA_ALG_XXX value such that
487 * #PSA_ALG_IS_AEAD(\p alg) is true).
488 *
489 * \return A sufficient ciphertext buffer size for the
490 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800491 * If the key type or AEAD algorithm is not
492 * recognized, or the parameters are incompatible,
493 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100494 */
Summer Qin359167d2021-07-05 18:11:50 +0800495#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
496 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800497 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
498 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100499 0)
500
Maulik Patel13b27cf2021-05-14 11:44:53 +0100501/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
502 * supported key types and AEAD algorithms.
503 *
Summer Qin359167d2021-07-05 18:11:50 +0800504 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100505 */
506#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
507
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100508/** A sufficient plaintext buffer size for psa_aead_verify().
509 *
510 * If the size of the plaintext buffer is at least this large, it is
511 * guaranteed that psa_aead_verify() will not fail due to an
512 * insufficient plaintext buffer size. The actual size of the output may
513 * be smaller in any given call.
514 *
Summer Qin359167d2021-07-05 18:11:50 +0800515 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
516 *
517 * \param key_type A symmetric key type that is
518 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519 * \param alg An AEAD algorithm
520 * (\c PSA_ALG_XXX value such that
521 * #PSA_ALG_IS_AEAD(\p alg) is true).
522 *
523 * \return A sufficient plaintext buffer size for the
524 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800525 * If the key type or AEAD algorithm is not
526 * recognized, or the parameters are incompatible,
527 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100528 */
Summer Qin359167d2021-07-05 18:11:50 +0800529#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
530 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Summer Qind635cd02023-03-31 18:07:38 +0800531 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
532 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000533 0)
534
Maulik Patel13b27cf2021-05-14 11:44:53 +0100535/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
536 * supported key types and AEAD algorithms.
537 *
Summer Qin359167d2021-07-05 18:11:50 +0800538 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100539 */
540#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
541
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100542#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
543 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100544 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100545 11 /*PKCS#1v1.5*/)
546
547/**
548 * \brief ECDSA signature size for a given curve bit size
549 *
550 * \param curve_bits Curve size in bits.
551 * \return Signature size in bytes.
552 *
553 * \note This macro returns a compile-time constant if its argument is one.
554 */
555#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
556 (PSA_BITS_TO_BYTES(curve_bits) * 2)
557
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100558/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000559 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100560 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000561 * of the specified type and size, with the specified algorithm.
562 * Note that the actual size of the signature may be smaller
563 * (some algorithms produce a variable-size signature).
564 *
565 * \warning This function may call its arguments multiple times or
566 * zero times, so you should not pass arguments that contain
567 * side effects.
568 *
569 * \param key_type An asymmetric key type (this may indifferently be a
570 * key pair type or a public key type).
571 * \param key_bits The size of the key in bits.
572 * \param alg The signature algorithm.
573 *
574 * \return If the parameters are valid and supported, return
575 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100576 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000577 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100578 * If the parameters are a valid combination that is not supported,
579 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000580 * If the parameters are not valid, the
581 * return value is unspecified.
582 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100583#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Summer Qind635cd02023-03-31 18:07:38 +0800584 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000585 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Summer Qind635cd02023-03-31 18:07:38 +0800586 ((void) alg, 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000587
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100588#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
589 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
590
591/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000592 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100593 * Maximum size of an asymmetric signature.
594 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100595 * This macro expands to a compile-time constant integer. This value
596 * is the maximum size of a signature in bytes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100597 */
598#define PSA_SIGNATURE_MAX_SIZE \
599 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
600 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
601 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
602
603/** Sufficient output buffer size for psa_asymmetric_encrypt().
604 *
605 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000606 * a key of the specified type and size, with the specified algorithm.
607 * Note that the actual size of the ciphertext may be smaller, depending
608 * on the algorithm.
609 *
610 * \warning This function may call its arguments multiple times or
611 * zero times, so you should not pass arguments that contain
612 * side effects.
613 *
614 * \param key_type An asymmetric key type (this may indifferently be a
615 * key pair type or a public key type).
616 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100617 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000618 *
619 * \return If the parameters are valid and supported, return
620 * a buffer size in bytes that guarantees that
621 * psa_asymmetric_encrypt() will not fail with
622 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100623 * If the parameters are a valid combination that is not supported,
624 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000625 * If the parameters are not valid, the
626 * return value is unspecified.
627 */
628#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
629 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Summer Qind635cd02023-03-31 18:07:38 +0800630 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000631 0)
632
Maulik Patel13b27cf2021-05-14 11:44:53 +0100633/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
634 * supported asymmetric encryption.
635 *
636 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
637 */
638/* This macro assumes that RSA is the only supported asymmetric encryption. */
639#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
640 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
641
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100642/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000643 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100644 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000645 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100646 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000647 * on the algorithm.
648 *
649 * \warning This function may call its arguments multiple times or
650 * zero times, so you should not pass arguments that contain
651 * side effects.
652 *
653 * \param key_type An asymmetric key type (this may indifferently be a
654 * key pair type or a public key type).
655 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100656 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000657 *
658 * \return If the parameters are valid and supported, return
659 * a buffer size in bytes that guarantees that
660 * psa_asymmetric_decrypt() will not fail with
661 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100662 * If the parameters are a valid combination that is not supported,
663 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000664 * If the parameters are not valid, the
665 * return value is unspecified.
666 */
667#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
668 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
669 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
670 0)
671
Maulik Patel13b27cf2021-05-14 11:44:53 +0100672/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
673 * supported asymmetric decryption.
674 *
675 * This macro assumes that RSA is the only supported asymmetric encryption.
676 *
677 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
678 */
679#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
680 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
681
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100682/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
683 * number of bits.
684 *
685 * This definition assumes that bits <= 2^19 - 9 so that the length field
686 * is at most 3 bytes. The length of the encoding is the length of the
687 * bit string padded to a whole number of bytes plus:
688 * - 1 type byte;
689 * - 1 to 3 length bytes;
690 * - 0 to 1 bytes of leading 0 due to the sign bit.
691 */
692#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
693 ((bits) / 8 + 5)
694
695/* Maximum size of the export encoding of an RSA public key.
696 * Assumes that the public exponent is less than 2^32.
697 *
698 * RSAPublicKey ::= SEQUENCE {
699 * modulus INTEGER, -- n
700 * publicExponent INTEGER } -- e
701 *
702 * - 4 bytes of SEQUENCE overhead;
703 * - n : INTEGER;
704 * - 7 bytes for the public exponent.
705 */
706#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
707 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
708
709/* Maximum size of the export encoding of an RSA key pair.
Summer Qin614002c2023-01-19 15:22:39 +0800710 * Assumes that the public exponent is less than 2^32 and that the size
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100711 * difference between the two primes is at most 1 bit.
712 *
713 * RSAPrivateKey ::= SEQUENCE {
714 * version Version, -- 0
715 * modulus INTEGER, -- N-bit
716 * publicExponent INTEGER, -- 32-bit
717 * privateExponent INTEGER, -- N-bit
718 * prime1 INTEGER, -- N/2-bit
719 * prime2 INTEGER, -- N/2-bit
720 * exponent1 INTEGER, -- N/2-bit
721 * exponent2 INTEGER, -- N/2-bit
722 * coefficient INTEGER, -- N/2-bit
723 * }
724 *
725 * - 4 bytes of SEQUENCE overhead;
726 * - 3 bytes of version;
727 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
728 * overapproximated as 9 half-size INTEGERS;
729 * - 7 bytes for the public exponent.
730 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100731#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100732 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
733
734/* Maximum size of the export encoding of a DSA public key.
735 *
736 * SubjectPublicKeyInfo ::= SEQUENCE {
737 * algorithm AlgorithmIdentifier,
738 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
739 * AlgorithmIdentifier ::= SEQUENCE {
740 * algorithm OBJECT IDENTIFIER,
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100741 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100742 * DSAPublicKey ::= INTEGER -- public key, Y
743 *
744 * - 3 * 4 bytes of SEQUENCE overhead;
745 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
746 * - 4 bytes of BIT STRING overhead;
747 * - 3 full-size INTEGERs (p, g, y);
748 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
749 */
750#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
751 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
752
753/* Maximum size of the export encoding of a DSA key pair.
754 *
755 * DSAPrivateKey ::= SEQUENCE {
756 * version Version, -- 0
757 * prime INTEGER, -- p
758 * subprime INTEGER, -- q
759 * generator INTEGER, -- g
760 * public INTEGER, -- y
761 * private INTEGER, -- x
762 * }
763 *
764 * - 4 bytes of SEQUENCE overhead;
765 * - 3 bytes of version;
766 * - 3 full-size INTEGERs (p, g, y);
767 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
768 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100769#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100770 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
771
772/* Maximum size of the export encoding of an ECC public key.
773 *
774 * The representation of an ECC public key is:
775 * - The byte 0x04;
776 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
777 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
778 * - where m is the bit size associated with the curve.
779 *
780 * - 1 byte + 2 * point size.
781 */
782#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
783 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
784
785/* Maximum size of the export encoding of an ECC key pair.
786 *
787 * An ECC key pair is represented by the secret value.
788 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100789#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100790 (PSA_BITS_TO_BYTES(key_bits))
791
Maulik Patel13b27cf2021-05-14 11:44:53 +0100792/** Sufficient output buffer size for psa_export_key() or
793 * psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100794 *
795 * This macro returns a compile-time constant if its arguments are
796 * compile-time constants.
797 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100798 * \warning This macro may evaluate its arguments multiple times or
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100799 * zero times, so you should not pass arguments that contain
800 * side effects.
801 *
802 * The following code illustrates how to allocate enough memory to export
803 * a key by querying the key type and size at runtime.
804 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100805 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100806 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100807 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100808 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100809 * psa_key_type_t key_type = psa_get_key_type(&attributes);
810 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100811 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100812 * psa_reset_key_attributes(&attributes);
813 * uint8_t *buffer = malloc(buffer_size);
814 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100815 * size_t buffer_length;
816 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
817 * if (status != PSA_SUCCESS) handle_error(...);
818 * \endcode
819 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100820 * \param key_type A supported key type.
821 * \param key_bits The size of the key in bits.
822 *
823 * \return If the parameters are valid and supported, return
824 * a buffer size in bytes that guarantees that
825 * psa_export_key() or psa_export_public_key() will not fail with
826 * #PSA_ERROR_BUFFER_TOO_SMALL.
827 * If the parameters are a valid combination that is not supported,
828 * return either a sensible size or 0.
829 * If the parameters are not valid, the return value is unspecified.
830 */
831#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
832 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
833 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
834 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
835 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
836 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
837 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
838 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
839 0)
840
841/** Sufficient output buffer size for psa_export_public_key().
842 *
843 * This macro returns a compile-time constant if its arguments are
844 * compile-time constants.
845 *
846 * \warning This macro may evaluate its arguments multiple times or
847 * zero times, so you should not pass arguments that contain
848 * side effects.
849 *
850 * The following code illustrates how to allocate enough memory to export
851 * a public key by querying the key type and size at runtime.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100852 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100853 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100854 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100855 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100856 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100857 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100858 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100859 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100860 * psa_reset_key_attributes(&attributes);
861 * uint8_t *buffer = malloc(buffer_size);
862 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100863 * size_t buffer_length;
864 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
865 * if (status != PSA_SUCCESS) handle_error(...);
866 * \endcode
867 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100868 * \param key_type A public key or key pair key type.
869 * \param key_bits The size of the key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100870 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100871 * \return If the parameters are valid and supported, return
872 * a buffer size in bytes that guarantees that
873 * psa_export_public_key() will not fail with
874 * #PSA_ERROR_BUFFER_TOO_SMALL.
875 * If the parameters are a valid combination that is not
876 * supported, return either a sensible size or 0.
877 * If the parameters are not valid,
878 * the return value is unspecified.
879 *
880 * If the parameters are valid and supported,
881 * return the same result as
882 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
883 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
884 * \p key_bits).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100885 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100886#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
887 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
888 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100889 0)
890
Maulik Patel13b27cf2021-05-14 11:44:53 +0100891/** Sufficient buffer size for exporting any asymmetric key pair.
Maulik Patel28659c42021-01-06 14:09:22 +0000892 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100893 * This macro expands to a compile-time constant integer. This value is
894 * a sufficient buffer size when calling psa_export_key() to export any
895 * asymmetric key pair, regardless of the exact key type and key size.
Maulik Patel28659c42021-01-06 14:09:22 +0000896 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100897 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
898 */
899#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
900 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
901 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
902 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
903 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
904
905/** Sufficient buffer size for exporting any asymmetric public key.
Maulik Patel28659c42021-01-06 14:09:22 +0000906 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100907 * This macro expands to a compile-time constant integer. This value is
908 * a sufficient buffer size when calling psa_export_key() or
909 * psa_export_public_key() to export any asymmetric public key,
910 * regardless of the exact key type and key size.
911 *
912 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
913 */
914#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
915 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
916 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
917 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
918 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
919
920/** Sufficient output buffer size for psa_raw_key_agreement().
921 *
922 * This macro returns a compile-time constant if its arguments are
923 * compile-time constants.
Maulik Patel28659c42021-01-06 14:09:22 +0000924 *
925 * \warning This macro may evaluate its arguments multiple times or
926 * zero times, so you should not pass arguments that contain
927 * side effects.
928 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100929 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
Maulik Patel28659c42021-01-06 14:09:22 +0000930 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100931 * \param key_type A supported key type.
932 * \param key_bits The size of the key in bits.
Maulik Patel28659c42021-01-06 14:09:22 +0000933 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100934 * \return If the parameters are valid and supported, return
935 * a buffer size in bytes that guarantees that
936 * psa_raw_key_agreement() will not fail with
937 * #PSA_ERROR_BUFFER_TOO_SMALL.
938 * If the parameters are a valid combination that
939 * is not supported, return either a sensible size or 0.
940 * If the parameters are not valid,
941 * the return value is unspecified.
Maulik Patel28659c42021-01-06 14:09:22 +0000942 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100943/* FFDH is not yet supported in PSA. */
944#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
945 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
946 PSA_BITS_TO_BYTES(key_bits) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000947 0)
948
Maulik Patel13b27cf2021-05-14 11:44:53 +0100949/** Maximum size of the output from psa_raw_key_agreement().
Maulik Patel28659c42021-01-06 14:09:22 +0000950 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100951 * This macro expands to a compile-time constant integer. This value is the
952 * maximum size of the output any raw key agreement algorithm, in bytes.
Maulik Patel28659c42021-01-06 14:09:22 +0000953 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100954 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
Maulik Patel28659c42021-01-06 14:09:22 +0000955 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100956#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
957 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
Maulik Patel28659c42021-01-06 14:09:22 +0000958
959/** The default IV size for a cipher algorithm, in bytes.
960 *
961 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
962 * the default IV length for the algorithm.
963 *
964 * This macro can be used to allocate a buffer of sufficient size to
965 * store the IV output from #psa_cipher_generate_iv() when using
966 * a multi-part cipher operation.
967 *
968 * See also #PSA_CIPHER_IV_MAX_SIZE.
969 *
970 * \warning This macro may evaluate its arguments multiple times or
971 * zero times, so you should not pass arguments that contain
972 * side effects.
973 *
974 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
975 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100976 * \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 +0000977 *
978 * \return The default IV size for the specified key type and algorithm.
979 * If the algorithm does not use an IV, return 0.
980 * If the key type or cipher algorithm is not recognized,
981 * or the parameters are incompatible, return 0.
Maulik Patel28659c42021-01-06 14:09:22 +0000982 */
983#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100984 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Summer Qind635cd02023-03-31 18:07:38 +0800985 ((alg) == PSA_ALG_CTR || \
986 (alg) == PSA_ALG_CFB || \
987 (alg) == PSA_ALG_OFB || \
988 (alg) == PSA_ALG_XTS || \
989 (alg) == PSA_ALG_CBC_NO_PADDING || \
990 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000991 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Summer Qind635cd02023-03-31 18:07:38 +0800992 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
993 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
994 0)
Maulik Patel28659c42021-01-06 14:09:22 +0000995
996/** The maximum IV size for all supported cipher algorithms, in bytes.
997 *
998 * See also #PSA_CIPHER_IV_LENGTH().
999 */
1000#define PSA_CIPHER_IV_MAX_SIZE 16
1001
Maulik Patel13b27cf2021-05-14 11:44:53 +01001002/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1003 *
1004 * If the size of the output buffer is at least this large, it is guaranteed
1005 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1006 * Depending on the algorithm, the actual size of the output might be smaller.
1007 *
1008 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1009 *
1010 * \warning This macro may evaluate its arguments multiple times or
1011 * zero times, so you should not pass arguments that contain
1012 * side effects.
1013 *
1014 * \param key_type A symmetric key type that is compatible with algorithm
1015 * alg.
1016 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1017 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1018 * \param input_length Size of the input in bytes.
1019 *
1020 * \return A sufficient output size for the specified key type and
1021 * algorithm. If the key type or cipher algorithm is not
1022 * recognized, or the parameters are incompatible,
1023 * return 0.
1024 */
1025#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1026 (alg == PSA_ALG_CBC_PKCS7 ? \
Summer Qinf07cc312022-01-05 16:52:54 +08001027 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Summer Qind635cd02023-03-31 18:07:38 +08001028 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1029 (input_length) + 1) + \
1030 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001031 (PSA_ALG_IS_CIPHER(alg) ? \
1032 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Summer Qind635cd02023-03-31 18:07:38 +08001033 0))
Maulik Patel13b27cf2021-05-14 11:44:53 +01001034
1035/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1036 * supported key types and cipher algorithms.
1037 *
1038 * If the size of the output buffer is at least this large, it is guaranteed
1039 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1040 *
1041 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1042 *
1043 * \param input_length Size of the input in bytes.
1044 *
1045 */
1046#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1047 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1048 (input_length) + 1) + \
1049 PSA_CIPHER_IV_MAX_SIZE)
1050
1051/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1052 *
1053 * If the size of the output buffer is at least this large, it is guaranteed
1054 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1055 * Depending on the algorithm, the actual size of the output might be smaller.
1056 *
1057 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
1058 *
1059 * \param key_type A symmetric key type that is compatible with algorithm
1060 * alg.
1061 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1062 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1063 * \param input_length Size of the input in bytes.
1064 *
1065 * \return A sufficient output size for the specified key type and
1066 * algorithm. If the key type or cipher algorithm is not
1067 * recognized, or the parameters are incompatible,
1068 * return 0.
1069 */
1070#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1071 (PSA_ALG_IS_CIPHER(alg) && \
1072 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1073 (input_length) : \
1074 0)
1075
1076/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1077 * supported key types and cipher algorithms.
1078 *
1079 * If the size of the output buffer is at least this large, it is guaranteed
1080 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1081 *
1082 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1083 *
1084 * \param input_length Size of the input in bytes.
1085 */
1086#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1087 (input_length)
1088
1089/** A sufficient output buffer size for psa_cipher_update().
1090 *
1091 * If the size of the output buffer is at least this large, it is guaranteed
1092 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1093 * The actual size of the output might be smaller in any given call.
1094 *
1095 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1096 *
1097 * \param key_type A symmetric key type that is compatible with algorithm
1098 * alg.
1099 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1100 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1101 * \param input_length Size of the input in bytes.
1102 *
1103 * \return A sufficient output size for the specified key type and
1104 * algorithm. If the key type or cipher algorithm is not
1105 * recognized, or the parameters are incompatible, return 0.
1106 */
1107#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1108 (PSA_ALG_IS_CIPHER(alg) ? \
Summer Qind635cd02023-03-31 18:07:38 +08001109 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1110 (((alg) == PSA_ALG_CBC_PKCS7 || \
1111 (alg) == PSA_ALG_CBC_NO_PADDING || \
1112 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1113 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001114 input_length) : \
Summer Qind635cd02023-03-31 18:07:38 +08001115 (input_length)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001116 0)
1117
1118/** A sufficient output buffer size for psa_cipher_update(), for any of the
1119 * supported key types and cipher algorithms.
1120 *
1121 * If the size of the output buffer is at least this large, it is guaranteed
1122 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1123 *
1124 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1125 *
1126 * \param input_length Size of the input in bytes.
1127 */
1128#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1129 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1130
1131/** A sufficient ciphertext buffer size for psa_cipher_finish().
1132 *
1133 * If the size of the ciphertext buffer is at least this large, it is
1134 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1135 * ciphertext buffer size. The actual size of the output might be smaller in
1136 * any given call.
1137 *
1138 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1139 *
1140 * \param key_type A symmetric key type that is compatible with algorithm
1141 * alg.
1142 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1143 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1144 * \return A sufficient output size for the specified key type and
1145 * algorithm. If the key type or cipher algorithm is not
1146 * recognized, or the parameters are incompatible, return 0.
1147 */
1148#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1149 (PSA_ALG_IS_CIPHER(alg) ? \
1150 (alg == PSA_ALG_CBC_PKCS7 ? \
1151 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1152 0) : \
1153 0)
1154
1155/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1156 * supported key types and cipher algorithms.
1157 *
1158 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1159 */
1160#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1161 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1162
Antonio de Angelis377a1552018-11-22 17:02:40 +00001163#endif /* PSA_CRYPTO_SIZES_H */