1 /**
2  * \file psa/crypto_sizes.h
3  *
4  * \brief PSA cryptography module: Mbed TLS buffer size macros
5  *
6  * \note This file may not be included directly. Applications must
7  * include psa/crypto.h.
8  *
9  * This file contains the definitions of macros that are useful to
10  * compute buffer sizes. The signatures and semantics of these macros
11  * are standardized, but the definitions are not, because they depend on
12  * the available algorithms and, in some cases, on permitted tolerances
13  * on buffer sizes.
14  *
15  * In implementations with isolation between the application and the
16  * cryptography module, implementers should take care to ensure that
17  * the definitions that are exposed to applications match what the
18  * module implements.
19  *
20  * Macros that compute sizes whose values do not depend on the
21  * implementation are in crypto.h.
22  */
23 /*
24  *  Copyright The Mbed TLS Contributors
25  *  SPDX-License-Identifier: Apache-2.0
26  *
27  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
28  *  not use this file except in compliance with the License.
29  *  You may obtain a copy of the License at
30  *
31  *  http://www.apache.org/licenses/LICENSE-2.0
32  *
33  *  Unless required by applicable law or agreed to in writing, software
34  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36  *  See the License for the specific language governing permissions and
37  *  limitations under the License.
38  */
39 
40 #ifndef PSA_CRYPTO_SIZES_H
41 #define PSA_CRYPTO_SIZES_H
42 
43 /* Include the Mbed TLS configuration file, the way Mbed TLS does it
44  * in each of its header files. */
45 #if !defined(MBEDTLS_CONFIG_FILE)
46 #include "mbedtls/config.h"
47 #else
48 #include MBEDTLS_CONFIG_FILE
49 #endif
50 
51 #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
52 #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
53 
54 #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
55     (((length) + (block_size) - 1) / (block_size) * (block_size))
56 
57 /** The size of the output of psa_hash_finish(), in bytes.
58  *
59  * This is also the hash size that psa_hash_verify() expects.
60  *
61  * \param alg   A hash algorithm (\c PSA_ALG_XXX value such that
62  *              #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
63  *              (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
64  *              hash algorithm).
65  *
66  * \return The hash size for the specified hash algorithm.
67  *         If the hash algorithm is not recognized, return 0.
68  */
69 #define PSA_HASH_LENGTH(alg)                                        \
70     (                                                               \
71         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 :            \
72         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 :            \
73         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 :            \
74         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 :      \
75         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 :          \
76         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 :        \
77         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 :        \
78         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 :        \
79         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 :        \
80         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 :    \
81         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 :    \
82         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 :       \
83         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 :       \
84         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 :       \
85         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 :       \
86         0)
87 
88 /** The input block size of a hash algorithm, in bytes.
89  *
90  * Hash algorithms process their input data in blocks. Hash operations will
91  * retain any partial blocks until they have enough input to fill the block or
92  * until the operation is finished.
93  * This affects the output from psa_hash_suspend().
94  *
95  * \param alg   A hash algorithm (\c PSA_ALG_XXX value such that
96  *              PSA_ALG_IS_HASH(\p alg) is true).
97  *
98  * \return      The block size in bytes for the specified hash algorithm.
99  *              If the hash algorithm is not recognized, return 0.
100  *              An implementation can return either 0 or the correct size for a
101  *              hash algorithm that it recognizes, but does not support.
102  */
103 #define PSA_HASH_BLOCK_LENGTH(alg)                                  \
104     (                                                               \
105         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 :            \
106         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 64 :            \
107         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 :            \
108         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 :      \
109         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 :          \
110         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 :        \
111         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 :        \
112         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 :       \
113         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 :       \
114         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 :   \
115         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 :   \
116         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 :      \
117         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 :      \
118         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 :      \
119         PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 :       \
120         0)
121 
122 /** \def PSA_HASH_MAX_SIZE
123  *
124  * Maximum size of a hash.
125  *
126  * This macro expands to a compile-time constant integer. This value
127  * is the maximum size of a hash in bytes.
128  */
129 /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
130  * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
131  * HMAC-SHA3-512. */
132 #if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
133 #define PSA_HASH_MAX_SIZE 64
134 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
135 #else
136 #define PSA_HASH_MAX_SIZE 32
137 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
138 #endif
139 
140 /** \def PSA_MAC_MAX_SIZE
141  *
142  * Maximum size of a MAC.
143  *
144  * This macro expands to a compile-time constant integer. This value
145  * is the maximum size of a MAC in bytes.
146  */
147 /* All non-HMAC MACs have a maximum size that's smaller than the
148  * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
149 /* Note that the encoding of truncated MAC algorithms limits this value
150  * to 64 bytes.
151  */
152 #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
153 
154 /** The length of a tag for an AEAD algorithm, in bytes.
155  *
156  * This macro can be used to allocate a buffer of sufficient size to store the
157  * tag output from psa_aead_finish().
158  *
159  * See also #PSA_AEAD_TAG_MAX_SIZE.
160  *
161  * \param key_type            The type of the AEAD key.
162  * \param key_bits            The size of the AEAD key in bits.
163  * \param alg                 An AEAD algorithm
164  *                            (\c PSA_ALG_XXX value such that
165  *                            #PSA_ALG_IS_AEAD(\p alg) is true).
166  *
167  * \return                    The tag length for the specified algorithm and key.
168  *                            If the AEAD algorithm does not have an identified
169  *                            tag that can be distinguished from the rest of
170  *                            the ciphertext, return 0.
171  *                            If the key type or AEAD algorithm is not
172  *                            recognized, or the parameters are incompatible,
173  *                            return 0.
174  */
175 #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg)                        \
176     (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ?                            \
177      PSA_ALG_AEAD_GET_TAG_LENGTH(alg) :                                     \
178      ((void) (key_bits), 0))
179 
180 /** The maximum tag size for all supported AEAD algorithms, in bytes.
181  *
182  * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
183  */
184 #define PSA_AEAD_TAG_MAX_SIZE       16
185 
186 /* The maximum size of an RSA key on this implementation, in bits.
187  * This is a vendor-specific macro.
188  *
189  * Mbed TLS does not set a hard limit on the size of RSA keys: any key
190  * whose parameters fit in a bignum is accepted. However large keys can
191  * induce a large memory usage and long computation times. Unlike other
192  * auxiliary macros in this file and in crypto.h, which reflect how the
193  * library is configured, this macro defines how the library is
194  * configured. This implementation refuses to import or generate an
195  * RSA key whose size is larger than the value defined here.
196  *
197  * Note that an implementation may set different size limits for different
198  * operations, and does not need to accept all key sizes up to the limit. */
199 #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
200 
201 /* The maximum size of an ECC key on this implementation, in bits.
202  * This is a vendor-specific macro. */
203 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
204 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
205 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
206 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
207 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
208 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
209 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
210 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
211 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
212 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
213 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
214 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
215 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
216 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
217 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
218 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
219 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
220 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
221 #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
222 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
223 #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
224 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
225 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
226 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
227 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
228 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
229 #else
230 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
231 #endif
232 
233 /** This macro returns the maximum supported length of the PSK for the
234  * TLS-1.2 PSK-to-MS key derivation
235  * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
236  *
237  * The maximum supported length does not depend on the chosen hash algorithm.
238  *
239  * Quoting RFC 4279, Sect 5.3:
240  * TLS implementations supporting these ciphersuites MUST support
241  * arbitrary PSK identities up to 128 octets in length, and arbitrary
242  * PSKs up to 64 octets in length.  Supporting longer identities and
243  * keys is RECOMMENDED.
244  *
245  * Therefore, no implementation should define a value smaller than 64
246  * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
247  */
248 #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
249 
250 /** The maximum size of a block cipher. */
251 #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
252 
253 /** The size of the output of psa_mac_sign_finish(), in bytes.
254  *
255  * This is also the MAC size that psa_mac_verify_finish() expects.
256  *
257  * \warning This macro may evaluate its arguments multiple times or
258  *          zero times, so you should not pass arguments that contain
259  *          side effects.
260  *
261  * \param key_type      The type of the MAC key.
262  * \param key_bits      The size of the MAC key in bits.
263  * \param alg           A MAC algorithm (\c PSA_ALG_XXX value such that
264  *                      #PSA_ALG_IS_MAC(\p alg) is true).
265  *
266  * \return              The MAC size for the specified algorithm with
267  *                      the specified key parameters.
268  * \return              0 if the MAC algorithm is not recognized.
269  * \return              Either 0 or the correct size for a MAC algorithm that
270  *                      the implementation recognizes, but does not support.
271  * \return              Unspecified if the key parameters are not consistent
272  *                      with the algorithm.
273  */
274 #define PSA_MAC_LENGTH(key_type, key_bits, alg)                                   \
275     ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) :        \
276      PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) :         \
277      PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
278      ((void)(key_type), (void)(key_bits), 0))
279 
280 /** The maximum size of the output of psa_aead_encrypt(), in bytes.
281  *
282  * If the size of the ciphertext buffer is at least this large, it is
283  * guaranteed that psa_aead_encrypt() will not fail due to an
284  * insufficient buffer size. Depending on the algorithm, the actual size of
285  * the ciphertext may be smaller.
286  *
287  * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
288  *
289  * \warning This macro may evaluate its arguments multiple times or
290  *          zero times, so you should not pass arguments that contain
291  *          side effects.
292  *
293  * \param key_type            A symmetric key type that is
294  *                            compatible with algorithm \p alg.
295  * \param alg                 An AEAD algorithm
296  *                            (\c PSA_ALG_XXX value such that
297  *                            #PSA_ALG_IS_AEAD(\p alg) is true).
298  * \param plaintext_length    Size of the plaintext in bytes.
299  *
300  * \return                    The AEAD ciphertext size for the specified
301  *                            algorithm.
302  *                            If the key type or AEAD algorithm is not
303  *                            recognized, or the parameters are incompatible,
304  *                            return 0.
305  */
306 #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
307     (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ?                      \
308      (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) :          \
309      0)
310 
311 /** A sufficient output buffer size for psa_aead_encrypt(), for any of the
312  *  supported key types and AEAD algorithms.
313  *
314  * If the size of the ciphertext buffer is at least this large, it is guaranteed
315  * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
316  *
317  * \note This macro returns a compile-time constant if its arguments are
318  *       compile-time constants.
319  *
320  * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
321  * \p plaintext_length).
322  *
323  * \param plaintext_length    Size of the plaintext in bytes.
324  *
325  * \return                    A sufficient output buffer size for any of the
326  *                            supported key types and AEAD algorithms.
327  *
328  */
329 #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length)          \
330     ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
331 
332 
333 /** The maximum size of the output of psa_aead_decrypt(), in bytes.
334  *
335  * If the size of the plaintext buffer is at least this large, it is
336  * guaranteed that psa_aead_decrypt() will not fail due to an
337  * insufficient buffer size. Depending on the algorithm, the actual size of
338  * the plaintext may be smaller.
339  *
340  * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
341  *
342  * \warning This macro may evaluate its arguments multiple times or
343  *          zero times, so you should not pass arguments that contain
344  *          side effects.
345  *
346  * \param key_type            A symmetric key type that is
347  *                            compatible with algorithm \p alg.
348  * \param alg                 An AEAD algorithm
349  *                            (\c PSA_ALG_XXX value such that
350  *                            #PSA_ALG_IS_AEAD(\p alg) is true).
351  * \param ciphertext_length   Size of the plaintext in bytes.
352  *
353  * \return                    The AEAD ciphertext size for the specified
354  *                            algorithm.
355  *                            If the key type or AEAD algorithm is not
356  *                            recognized, or the parameters are incompatible,
357  *                            return 0.
358  */
359 #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
360     (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 &&                      \
361          (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ?      \
362          (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) :      \
363      0)
364 
365 /** A sufficient output buffer size for psa_aead_decrypt(), for any of the
366  *  supported key types and AEAD algorithms.
367  *
368  * If the size of the plaintext buffer is at least this large, it is guaranteed
369  * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
370  *
371  * \note This macro returns a compile-time constant if its arguments are
372  *       compile-time constants.
373  *
374  * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
375  * \p ciphertext_length).
376  *
377  * \param ciphertext_length   Size of the ciphertext in bytes.
378  *
379  * \return                    A sufficient output buffer size for any of the
380  *                            supported key types and AEAD algorithms.
381  *
382  */
383 #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length)     \
384      (ciphertext_length)
385 
386 /** The default nonce size for an AEAD algorithm, in bytes.
387  *
388  * This macro can be used to allocate a buffer of sufficient size to
389  * store the nonce output from #psa_aead_generate_nonce().
390  *
391  * See also #PSA_AEAD_NONCE_MAX_SIZE.
392  *
393  * \note This is not the maximum size of nonce supported as input to
394  *       #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
395  *       just the default size that is generated by #psa_aead_generate_nonce().
396  *
397  * \warning This macro may evaluate its arguments multiple times or
398  *          zero times, so you should not pass arguments that contain
399  *          side effects.
400  *
401  * \param key_type  A symmetric key type that is compatible with
402  *                  algorithm \p alg.
403  *
404  * \param alg       An AEAD algorithm (\c PSA_ALG_XXX value such that
405  *                  #PSA_ALG_IS_AEAD(\p alg) is true).
406  *
407  * \return The default nonce size for the specified key type and algorithm.
408  *         If the key type or AEAD algorithm is not recognized,
409  *         or the parameters are incompatible, return 0.
410  */
411 #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
412     (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
413           MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
414           MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
415           0 : \
416      (key_type) == PSA_KEY_TYPE_CHACHA20 && \
417           MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
418      0)
419 
420 /** The maximum default nonce size among all supported pairs of key types and
421  *  AEAD algorithms, in bytes.
422  *
423  * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
424  * may return.
425  *
426  * \note This is not the maximum size of nonce supported as input to
427  *       #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
428  *       just the largest size that may be generated by
429  *       #psa_aead_generate_nonce().
430  */
431 #define PSA_AEAD_NONCE_MAX_SIZE 13
432 
433 /** A sufficient output buffer size for psa_aead_update().
434  *
435  * If the size of the output buffer is at least this large, it is
436  * guaranteed that psa_aead_update() will not fail due to an
437  * insufficient buffer size. The actual size of the output may be smaller
438  * in any given call.
439  *
440  * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
441  *
442  * \warning This macro may evaluate its arguments multiple times or
443  *          zero times, so you should not pass arguments that contain
444  *          side effects.
445  *
446  * \param key_type            A symmetric key type that is
447  *                            compatible with algorithm \p alg.
448  * \param alg                 An AEAD algorithm
449  *                            (\c PSA_ALG_XXX value such that
450  *                            #PSA_ALG_IS_AEAD(\p alg) is true).
451  * \param input_length        Size of the input in bytes.
452  *
453  * \return                    A sufficient output buffer size for the specified
454  *                            algorithm.
455  *                            If the key type or AEAD algorithm is not
456  *                            recognized, or the parameters are incompatible,
457  *                            return 0.
458  */
459 /* For all the AEAD modes defined in this specification, it is possible
460  * to emit output without delay. However, hardware may not always be
461  * capable of this. So for modes based on a block cipher, allow the
462  * implementation to delay the output until it has a full block. */
463 #define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length)                             \
464     (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ?                                             \
465          PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ?                                              \
466          PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
467          (input_length) : \
468      0)
469 
470 /** A sufficient output buffer size for psa_aead_update(), for any of the
471  *  supported key types and AEAD algorithms.
472  *
473  * If the size of the output buffer is at least this large, it is guaranteed
474  * that psa_aead_update() will not fail due to an insufficient buffer size.
475  *
476  * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
477  *
478  * \param input_length      Size of the input in bytes.
479  */
480 #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length)                           \
481     (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
482 
483 /** A sufficient ciphertext buffer size for psa_aead_finish().
484  *
485  * If the size of the ciphertext buffer is at least this large, it is
486  * guaranteed that psa_aead_finish() will not fail due to an
487  * insufficient ciphertext buffer size. The actual size of the output may
488  * be smaller in any given call.
489  *
490  * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
491  *
492  * \param key_type            A symmetric key type that is
493                               compatible with algorithm \p alg.
494  * \param alg                 An AEAD algorithm
495  *                            (\c PSA_ALG_XXX value such that
496  *                            #PSA_ALG_IS_AEAD(\p alg) is true).
497  *
498  * \return                    A sufficient ciphertext buffer size for the
499  *                            specified algorithm.
500  *                            If the key type or AEAD algorithm is not
501  *                            recognized, or the parameters are incompatible,
502  *                            return 0.
503  */
504 #define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
505     (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 &&  \
506          PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ?    \
507          PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
508      0)
509 
510 /** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
511  *  supported key types and AEAD algorithms.
512  *
513  * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
514  */
515 #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE     (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
516 
517 /** A sufficient plaintext buffer size for psa_aead_verify().
518  *
519  * If the size of the plaintext buffer is at least this large, it is
520  * guaranteed that psa_aead_verify() will not fail due to an
521  * insufficient plaintext buffer size. The actual size of the output may
522  * be smaller in any given call.
523  *
524  * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
525  *
526  * \param key_type            A symmetric key type that is
527  *                            compatible with algorithm \p alg.
528  * \param alg                 An AEAD algorithm
529  *                            (\c PSA_ALG_XXX value such that
530  *                            #PSA_ALG_IS_AEAD(\p alg) is true).
531  *
532  * \return                    A sufficient plaintext buffer size for the
533  *                            specified algorithm.
534  *                            If the key type or AEAD algorithm is not
535  *                            recognized, or the parameters are incompatible,
536  *                            return 0.
537  */
538 #define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
539     (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 &&  \
540          PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ?    \
541          PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
542      0)
543 
544 /** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
545  *  supported key types and AEAD algorithms.
546  *
547  * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
548  */
549 #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE     (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
550 
551 #define PSA_RSA_MINIMUM_PADDING_SIZE(alg)                         \
552     (PSA_ALG_IS_RSA_OAEP(alg) ?                                   \
553      2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 :    \
554      11 /*PKCS#1v1.5*/)
555 
556 /**
557  * \brief ECDSA signature size for a given curve bit size
558  *
559  * \param curve_bits    Curve size in bits.
560  * \return              Signature size in bytes.
561  *
562  * \note This macro returns a compile-time constant if its argument is one.
563  */
564 #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits)    \
565     (PSA_BITS_TO_BYTES(curve_bits) * 2)
566 
567 /** Sufficient signature buffer size for psa_sign_hash().
568  *
569  * This macro returns a sufficient buffer size for a signature using a key
570  * of the specified type and size, with the specified algorithm.
571  * Note that the actual size of the signature may be smaller
572  * (some algorithms produce a variable-size signature).
573  *
574  * \warning This function may call its arguments multiple times or
575  *          zero times, so you should not pass arguments that contain
576  *          side effects.
577  *
578  * \param key_type  An asymmetric key type (this may indifferently be a
579  *                  key pair type or a public key type).
580  * \param key_bits  The size of the key in bits.
581  * \param alg       The signature algorithm.
582  *
583  * \return If the parameters are valid and supported, return
584  *         a buffer size in bytes that guarantees that
585  *         psa_sign_hash() will not fail with
586  *         #PSA_ERROR_BUFFER_TOO_SMALL.
587  *         If the parameters are a valid combination that is not supported,
588  *         return either a sensible size or 0.
589  *         If the parameters are not valid, the
590  *         return value is unspecified.
591  */
592 #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)        \
593     (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
594      PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
595      ((void)alg, 0))
596 
597 #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE     \
598     PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
599 
600 /** \def PSA_SIGNATURE_MAX_SIZE
601  *
602  * Maximum size of an asymmetric signature.
603  *
604  * This macro expands to a compile-time constant integer. This value
605  * is the maximum size of a signature in bytes.
606  */
607 #define PSA_SIGNATURE_MAX_SIZE                               \
608     (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
609      PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) :                   \
610      PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
611 
612 /** Sufficient output buffer size for psa_asymmetric_encrypt().
613  *
614  * This macro returns a sufficient buffer size for a ciphertext produced using
615  * a key of the specified type and size, with the specified algorithm.
616  * Note that the actual size of the ciphertext may be smaller, depending
617  * on the algorithm.
618  *
619  * \warning This function may call its arguments multiple times or
620  *          zero times, so you should not pass arguments that contain
621  *          side effects.
622  *
623  * \param key_type  An asymmetric key type (this may indifferently be a
624  *                  key pair type or a public key type).
625  * \param key_bits  The size of the key in bits.
626  * \param alg       The asymmetric encryption algorithm.
627  *
628  * \return If the parameters are valid and supported, return
629  *         a buffer size in bytes that guarantees that
630  *         psa_asymmetric_encrypt() will not fail with
631  *         #PSA_ERROR_BUFFER_TOO_SMALL.
632  *         If the parameters are a valid combination that is not supported,
633  *         return either a sensible size or 0.
634  *         If the parameters are not valid, the
635  *         return value is unspecified.
636  */
637 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)     \
638     (PSA_KEY_TYPE_IS_RSA(key_type) ?                                    \
639      ((void)alg, PSA_BITS_TO_BYTES(key_bits)) :                         \
640      0)
641 
642 /** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
643  *  supported asymmetric encryption.
644  *
645  * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
646  */
647 /* This macro assumes that RSA is the only supported asymmetric encryption. */
648 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE          \
649     (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
650 
651 /** Sufficient output buffer size for psa_asymmetric_decrypt().
652  *
653  * This macro returns a sufficient buffer size for a plaintext produced using
654  * a key of the specified type and size, with the specified algorithm.
655  * Note that the actual size of the plaintext may be smaller, depending
656  * on the algorithm.
657  *
658  * \warning This function may call its arguments multiple times or
659  *          zero times, so you should not pass arguments that contain
660  *          side effects.
661  *
662  * \param key_type  An asymmetric key type (this may indifferently be a
663  *                  key pair type or a public key type).
664  * \param key_bits  The size of the key in bits.
665  * \param alg       The asymmetric encryption algorithm.
666  *
667  * \return If the parameters are valid and supported, return
668  *         a buffer size in bytes that guarantees that
669  *         psa_asymmetric_decrypt() will not fail with
670  *         #PSA_ERROR_BUFFER_TOO_SMALL.
671  *         If the parameters are a valid combination that is not supported,
672  *         return either a sensible size or 0.
673  *         If the parameters are not valid, the
674  *         return value is unspecified.
675  */
676 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)     \
677     (PSA_KEY_TYPE_IS_RSA(key_type) ?                                    \
678      PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) :  \
679      0)
680 
681 /** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
682  *  supported asymmetric decryption.
683  *
684  * This macro assumes that RSA is the only supported asymmetric encryption.
685  *
686  * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
687  */
688 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE          \
689     (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
690 
691 /* Maximum size of the ASN.1 encoding of an INTEGER with the specified
692  * number of bits.
693  *
694  * This definition assumes that bits <= 2^19 - 9 so that the length field
695  * is at most 3 bytes. The length of the encoding is the length of the
696  * bit string padded to a whole number of bytes plus:
697  * - 1 type byte;
698  * - 1 to 3 length bytes;
699  * - 0 to 1 bytes of leading 0 due to the sign bit.
700  */
701 #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits)      \
702     ((bits) / 8 + 5)
703 
704 /* Maximum size of the export encoding of an RSA public key.
705  * Assumes that the public exponent is less than 2^32.
706  *
707  * RSAPublicKey  ::=  SEQUENCE  {
708  *    modulus            INTEGER,    -- n
709  *    publicExponent     INTEGER  }  -- e
710  *
711  * - 4 bytes of SEQUENCE overhead;
712  * - n : INTEGER;
713  * - 7 bytes for the public exponent.
714  */
715 #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits)        \
716     (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
717 
718 /* Maximum size of the export encoding of an RSA key pair.
719  * Assumes thatthe public exponent is less than 2^32 and that the size
720  * difference between the two primes is at most 1 bit.
721  *
722  * RSAPrivateKey ::= SEQUENCE {
723  *     version           Version,  -- 0
724  *     modulus           INTEGER,  -- N-bit
725  *     publicExponent    INTEGER,  -- 32-bit
726  *     privateExponent   INTEGER,  -- N-bit
727  *     prime1            INTEGER,  -- N/2-bit
728  *     prime2            INTEGER,  -- N/2-bit
729  *     exponent1         INTEGER,  -- N/2-bit
730  *     exponent2         INTEGER,  -- N/2-bit
731  *     coefficient       INTEGER,  -- N/2-bit
732  * }
733  *
734  * - 4 bytes of SEQUENCE overhead;
735  * - 3 bytes of version;
736  * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
737  *   overapproximated as 9 half-size INTEGERS;
738  * - 7 bytes for the public exponent.
739  */
740 #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits)   \
741     (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
742 
743 /* Maximum size of the export encoding of a DSA public key.
744  *
745  * SubjectPublicKeyInfo  ::=  SEQUENCE  {
746  *      algorithm            AlgorithmIdentifier,
747  *      subjectPublicKey     BIT STRING  } -- contains DSAPublicKey
748  * AlgorithmIdentifier  ::=  SEQUENCE  {
749  *      algorithm               OBJECT IDENTIFIER,
750  *      parameters              Dss-Parms  } -- SEQUENCE of 3 INTEGERs
751  * DSAPublicKey  ::=  INTEGER -- public key, Y
752  *
753  * - 3 * 4 bytes of SEQUENCE overhead;
754  * - 1 + 1 + 7 bytes of algorithm (DSA OID);
755  * - 4 bytes of BIT STRING overhead;
756  * - 3 full-size INTEGERs (p, g, y);
757  * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
758  */
759 #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits)        \
760     (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
761 
762 /* Maximum size of the export encoding of a DSA key pair.
763  *
764  * DSAPrivateKey ::= SEQUENCE {
765  *     version             Version,  -- 0
766  *     prime               INTEGER,  -- p
767  *     subprime            INTEGER,  -- q
768  *     generator           INTEGER,  -- g
769  *     public              INTEGER,  -- y
770  *     private             INTEGER,  -- x
771  * }
772  *
773  * - 4 bytes of SEQUENCE overhead;
774  * - 3 bytes of version;
775  * - 3 full-size INTEGERs (p, g, y);
776  * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
777  */
778 #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits)   \
779     (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
780 
781 /* Maximum size of the export encoding of an ECC public key.
782  *
783  * The representation of an ECC public key is:
784  *      - The byte 0x04;
785  *      - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
786  *      - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
787  *      - where m is the bit size associated with the curve.
788  *
789  * - 1 byte + 2 * point size.
790  */
791 #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits)        \
792     (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
793 
794 /* Maximum size of the export encoding of an ECC key pair.
795  *
796  * An ECC key pair is represented by the secret value.
797  */
798 #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits)   \
799     (PSA_BITS_TO_BYTES(key_bits))
800 
801 /** Sufficient output buffer size for psa_export_key() or
802  * psa_export_public_key().
803  *
804  * This macro returns a compile-time constant if its arguments are
805  * compile-time constants.
806  *
807  * \warning This macro may evaluate its arguments multiple times or
808  *          zero times, so you should not pass arguments that contain
809  *          side effects.
810  *
811  * The following code illustrates how to allocate enough memory to export
812  * a key by querying the key type and size at runtime.
813  * \code{c}
814  * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
815  * psa_status_t status;
816  * status = psa_get_key_attributes(key, &attributes);
817  * if (status != PSA_SUCCESS) handle_error(...);
818  * psa_key_type_t key_type = psa_get_key_type(&attributes);
819  * size_t key_bits = psa_get_key_bits(&attributes);
820  * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
821  * psa_reset_key_attributes(&attributes);
822  * uint8_t *buffer = malloc(buffer_size);
823  * if (buffer == NULL) handle_error(...);
824  * size_t buffer_length;
825  * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
826  * if (status != PSA_SUCCESS) handle_error(...);
827  * \endcode
828  *
829  * \param key_type  A supported key type.
830  * \param key_bits  The size of the key in bits.
831  *
832  * \return If the parameters are valid and supported, return
833  *         a buffer size in bytes that guarantees that
834  *         psa_export_key() or psa_export_public_key() will not fail with
835  *         #PSA_ERROR_BUFFER_TOO_SMALL.
836  *         If the parameters are a valid combination that is not supported,
837  *         return either a sensible size or 0.
838  *         If the parameters are not valid, the return value is unspecified.
839  */
840 #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits)                                              \
841     (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) :                         \
842      (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) :     \
843      (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
844      (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) :     \
845      (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
846      PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) :      \
847      PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) :  \
848      0)
849 
850 /** Sufficient output buffer size for psa_export_public_key().
851  *
852  * This macro returns a compile-time constant if its arguments are
853  * compile-time constants.
854  *
855  * \warning This macro may evaluate its arguments multiple times or
856  *          zero times, so you should not pass arguments that contain
857  *          side effects.
858  *
859  * The following code illustrates how to allocate enough memory to export
860  * a public key by querying the key type and size at runtime.
861  * \code{c}
862  * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
863  * psa_status_t status;
864  * status = psa_get_key_attributes(key, &attributes);
865  * if (status != PSA_SUCCESS) handle_error(...);
866  * psa_key_type_t key_type = psa_get_key_type(&attributes);
867  * size_t key_bits = psa_get_key_bits(&attributes);
868  * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
869  * psa_reset_key_attributes(&attributes);
870  * uint8_t *buffer = malloc(buffer_size);
871  * if (buffer == NULL) handle_error(...);
872  * size_t buffer_length;
873  * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
874  * if (status != PSA_SUCCESS) handle_error(...);
875  * \endcode
876  *
877  * \param key_type      A public key or key pair key type.
878  * \param key_bits      The size of the key in bits.
879  *
880  * \return              If the parameters are valid and supported, return
881  *                      a buffer size in bytes that guarantees that
882  *                      psa_export_public_key() will not fail with
883  *                      #PSA_ERROR_BUFFER_TOO_SMALL.
884  *                      If the parameters are a valid combination that is not
885  *                      supported, return either a sensible size or 0.
886  *                      If the parameters are not valid,
887  *                      the return value is unspecified.
888  *
889  *                      If the parameters are valid and supported,
890  *                      return the same result as
891  *                      #PSA_EXPORT_KEY_OUTPUT_SIZE(
892  *                          \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
893  *                          \p key_bits).
894  */
895 #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits)                           \
896     (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
897      PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
898      0)
899 
900 /** Sufficient buffer size for exporting any asymmetric key pair.
901  *
902  * This macro expands to a compile-time constant integer. This value is
903  * a sufficient buffer size when calling psa_export_key() to export any
904  * asymmetric key pair, regardless of the exact key type and key size.
905  *
906  * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
907  */
908 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE                                            \
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      PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) :        \
912      PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
913 
914 /** Sufficient buffer size for exporting any asymmetric public key.
915  *
916  * This macro expands to a compile-time constant integer. This value is
917  * a sufficient buffer size when calling psa_export_key() or
918  * psa_export_public_key() to export any asymmetric public key,
919  * regardless of the exact key type and key size.
920  *
921  * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
922  */
923 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE                                          \
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      PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) :      \
927      PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
928 
929 /** Sufficient output buffer size for psa_raw_key_agreement().
930  *
931  * This macro returns a compile-time constant if its arguments are
932  * compile-time constants.
933  *
934  * \warning This macro may evaluate its arguments multiple times or
935  *          zero times, so you should not pass arguments that contain
936  *          side effects.
937  *
938  * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
939  *
940  * \param key_type      A supported key type.
941  * \param key_bits      The size of the key in bits.
942  *
943  * \return              If the parameters are valid and supported, return
944  *                      a buffer size in bytes that guarantees that
945  *                      psa_raw_key_agreement() will not fail with
946  *                      #PSA_ERROR_BUFFER_TOO_SMALL.
947  *                      If the parameters are a valid combination that
948  *                      is not supported, return either a sensible size or 0.
949  *                      If the parameters are not valid,
950  *                      the return value is unspecified.
951  */
952 /* FFDH is not yet supported in PSA. */
953 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits)   \
954     (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ?                   \
955      PSA_BITS_TO_BYTES(key_bits) :                              \
956      0)
957 
958 /** Maximum size of the output from psa_raw_key_agreement().
959  *
960  * This macro expands to a compile-time constant integer. This value is the
961  * maximum size of the output any raw key agreement algorithm, in bytes.
962  *
963  * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
964  */
965 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE   \
966     (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
967 
968 /** The default IV size for a cipher algorithm, in bytes.
969  *
970  * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
971  * the default IV length for the algorithm.
972  *
973  * This macro can be used to allocate a buffer of sufficient size to
974  * store the IV output from #psa_cipher_generate_iv() when using
975  * a multi-part cipher operation.
976  *
977  * See also #PSA_CIPHER_IV_MAX_SIZE.
978  *
979  * \warning This macro may evaluate its arguments multiple times or
980  *          zero times, so you should not pass arguments that contain
981  *          side effects.
982  *
983  * \param key_type  A symmetric key type that is compatible with algorithm \p alg.
984  *
985  * \param alg       A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
986  *
987  * \return The default IV size for the specified key type and algorithm.
988  *         If the algorithm does not use an IV, return 0.
989  *         If the key type or cipher algorithm is not recognized,
990  *         or the parameters are incompatible, return 0.
991  */
992 #define PSA_CIPHER_IV_LENGTH(key_type, alg) \
993     (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
994         ((alg) == PSA_ALG_CTR || \
995          (alg) == PSA_ALG_CFB || \
996          (alg) == PSA_ALG_OFB || \
997          (alg) == PSA_ALG_XTS || \
998          (alg) == PSA_ALG_CBC_NO_PADDING || \
999          (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1000      (key_type) == PSA_KEY_TYPE_CHACHA20 && \
1001          (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
1002      0)
1003 
1004 /** The maximum IV size for all supported cipher algorithms, in bytes.
1005  *
1006  * See also #PSA_CIPHER_IV_LENGTH().
1007  */
1008 #define PSA_CIPHER_IV_MAX_SIZE 16
1009 
1010 /** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1011  *
1012  * If the size of the output buffer is at least this large, it is guaranteed
1013  * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1014  * Depending on the algorithm, the actual size of the output might be smaller.
1015  *
1016  * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1017  *
1018  * \warning This macro may evaluate its arguments multiple times or
1019  *          zero times, so you should not pass arguments that contain
1020  *          side effects.
1021  *
1022  * \param key_type      A symmetric key type that is compatible with algorithm
1023  *                      alg.
1024  * \param alg           A cipher algorithm (\c PSA_ALG_XXX value such that
1025  *                      #PSA_ALG_IS_CIPHER(\p alg) is true).
1026  * \param input_length  Size of the input in bytes.
1027  *
1028  * \return              A sufficient output size for the specified key type and
1029  *                      algorithm. If the key type or cipher algorithm is not
1030  *                      recognized, or the parameters are incompatible,
1031  *                      return 0.
1032  */
1033 #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length)             \
1034     (alg == PSA_ALG_CBC_PKCS7 ?                                                 \
1035      (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ?                            \
1036      PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type),          \
1037                               (input_length) + 1) +                             \
1038      PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) :                             \
1039      (PSA_ALG_IS_CIPHER(alg) ?                                                  \
1040       (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) :                \
1041      0))
1042 
1043 /** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1044  *  supported key types and cipher algorithms.
1045  *
1046  * If the size of the output buffer is at least this large, it is guaranteed
1047  * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1048  *
1049  * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1050  *
1051  * \param input_length  Size of the input in bytes.
1052  *
1053  */
1054 #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length)                        \
1055     (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE,                  \
1056                               (input_length) + 1) +                             \
1057      PSA_CIPHER_IV_MAX_SIZE)
1058 
1059 /** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1060  *
1061  * If the size of the output buffer is at least this large, it is guaranteed
1062  * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1063  * Depending on the algorithm, the actual size of the output might be smaller.
1064  *
1065  * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
1066  *
1067  * \param key_type      A symmetric key type that is compatible with algorithm
1068  *                      alg.
1069  * \param alg           A cipher algorithm (\c PSA_ALG_XXX value such that
1070  *                      #PSA_ALG_IS_CIPHER(\p alg) is true).
1071  * \param input_length  Size of the input in bytes.
1072  *
1073  * \return              A sufficient output size for the specified key type and
1074  *                      algorithm. If the key type or cipher algorithm is not
1075  *                      recognized, or the parameters are incompatible,
1076  *                      return 0.
1077  */
1078 #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length)                 \
1079     (PSA_ALG_IS_CIPHER(alg) &&                                                      \
1080      ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1081      (input_length) :                                                               \
1082      0)
1083 
1084 /** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1085  *  supported key types and cipher algorithms.
1086  *
1087  * If the size of the output buffer is at least this large, it is guaranteed
1088  * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1089  *
1090  * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1091  *
1092  * \param input_length  Size of the input in bytes.
1093  */
1094 #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length)    \
1095     (input_length)
1096 
1097 /** A sufficient output buffer size for psa_cipher_update().
1098  *
1099  * If the size of the output buffer is at least this large, it is guaranteed
1100  * that psa_cipher_update() will not fail due to an insufficient buffer size.
1101  * The actual size of the output might be smaller in any given call.
1102  *
1103  * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1104  *
1105  * \param key_type      A symmetric key type that is compatible with algorithm
1106  *                      alg.
1107  * \param alg           A cipher algorithm (PSA_ALG_XXX value such that
1108  *                      #PSA_ALG_IS_CIPHER(\p alg) is true).
1109  * \param input_length  Size of the input in bytes.
1110  *
1111  * \return              A sufficient output size for the specified key type and
1112  *                      algorithm. If the key type or cipher algorithm is not
1113  *                      recognized, or the parameters are incompatible, return 0.
1114  */
1115 #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length)              \
1116     (PSA_ALG_IS_CIPHER(alg) ?                                                   \
1117     (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ?                             \
1118      (((alg) == PSA_ALG_CBC_PKCS7      ||                                       \
1119        (alg) == PSA_ALG_CBC_NO_PADDING ||                                       \
1120        (alg) == PSA_ALG_ECB_NO_PADDING) ?                                       \
1121       PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type),         \
1122                                 input_length) :                                 \
1123       (input_length)) : 0) :                                                    \
1124      0)
1125 
1126 /** A sufficient output buffer size for psa_cipher_update(), for any of the
1127  *  supported key types and cipher algorithms.
1128  *
1129  * If the size of the output buffer is at least this large, it is guaranteed
1130  * that psa_cipher_update() will not fail due to an insufficient buffer size.
1131  *
1132  * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1133  *
1134  * \param input_length  Size of the input in bytes.
1135  */
1136 #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length)     \
1137     (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1138 
1139 /** A sufficient ciphertext buffer size for psa_cipher_finish().
1140  *
1141  * If the size of the ciphertext buffer is at least this large, it is
1142  * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1143  * ciphertext buffer size. The actual size of the output might be smaller in
1144  * any given call.
1145  *
1146  * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1147  *
1148  * \param key_type      A symmetric key type that is compatible with algorithm
1149  *                      alg.
1150  * \param alg           A cipher algorithm (PSA_ALG_XXX value such that
1151  *                      #PSA_ALG_IS_CIPHER(\p alg) is true).
1152  * \return              A sufficient output size for the specified key type and
1153  *                      algorithm. If the key type or cipher algorithm is not
1154  *                      recognized, or the parameters are incompatible, return 0.
1155  */
1156 #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)    \
1157     (PSA_ALG_IS_CIPHER(alg) ?                           \
1158      (alg == PSA_ALG_CBC_PKCS7 ?                        \
1159       PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) :         \
1160       0) :                                              \
1161      0)
1162 
1163 /** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1164  *  supported key types and cipher algorithms.
1165  *
1166  * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1167  */
1168 #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE           \
1169     (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1170 
1171 #endif /* PSA_CRYPTO_SIZES_H */
1172