blob: 10151c6afd52eed417360e46e0355ebcaa002fe8 [file] [log] [blame]
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001/**
2 * \file psa/crypto_sizes.h
3 *
4 * \brief PSA cryptography module: Mbed TLS buffer size macros
5 *
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02009 * This file contains the definitions of macros that are useful to
10 * compute buffer sizes. The signatures and semantics of these macros
11 * are standardized, but the definitions are not, because they depend on
12 * the available algorithms and, in some cases, on permitted tolerances
13 * on buffer sizes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +020014 *
Gilles Peskine07c91f52018-06-28 18:02:53 +020015 * In implementations with isolation between the application and the
16 * cryptography module, implementers should take care to ensure that
17 * the definitions that are exposed to applications match what the
18 * module implements.
19 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +020020 * Macros that compute sizes whose values do not depend on the
21 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020022 */
23/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020024 * Copyright The Mbed TLS Contributors
Gilles Peskine0cad07c2018-06-27 19:49:02 +020025 * 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.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020038 */
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)
Jaeden Amerod58a00d2019-06-07 11:49:59 +010046#include "mbedtls/config.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020047#else
48#include MBEDTLS_CONFIG_FILE
49#endif
50
Gilles Peskinea7c26db2018-12-12 13:42:25 +010051#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
52#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
53
Gilles Peskine248010c2019-05-14 16:08:59 +020054#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
55 (((length) + (block_size) - 1) / (block_size) * (block_size))
56
Gilles Peskinea7c26db2018-12-12 13:42:25 +010057/** 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.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010068 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010069#define PSA_HASH_LENGTH(alg) \
70 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010071 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
Gilles Peskineaf3baab2018-06-27 22:55:52 +020088/** \def PSA_HASH_MAX_SIZE
89 *
90 * Maximum size of a hash.
91 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +010092 * This macro expands to a compile-time constant integer. This value
93 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +020094 */
Gilles Peskine3052f532018-09-17 14:13:26 +020095/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
96 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
97 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +020098#if defined(MBEDTLS_SHA512_C)
99#define PSA_HASH_MAX_SIZE 64
100#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
101#else
102#define PSA_HASH_MAX_SIZE 32
103#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
104#endif
105
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200106/** \def PSA_MAC_MAX_SIZE
107 *
108 * Maximum size of a MAC.
109 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100110 * This macro expands to a compile-time constant integer. This value
111 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200112 */
113/* All non-HMAC MACs have a maximum size that's smaller than the
114 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200115/* Note that the encoding of truncated MAC algorithms limits this value
116 * to 64 bytes.
117 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200118#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
119
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100120/** The tag size for an AEAD algorithm, in bytes.
121 *
122 * \param alg An AEAD algorithm
123 * (\c PSA_ALG_XXX value such that
124 * #PSA_ALG_IS_AEAD(\p alg) is true).
125 *
126 * \return The tag size for the specified algorithm.
127 * If the AEAD algorithm does not have an identified
128 * tag that can be distinguished from the rest of
129 * the ciphertext, return 0.
130 * If the AEAD algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100131 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100132#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
133 (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \
134 ((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100135 0)
136
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200137/** The maximum tag size for all supported AEAD algorithms, in bytes.
138 *
139 * See also #PSA_AEAD_TAG_LENGTH(\p alg).
140 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100141#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200142
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200143/* The maximum size of an RSA key on this implementation, in bits.
144 * This is a vendor-specific macro.
145 *
146 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
147 * whose parameters fit in a bignum is accepted. However large keys can
148 * induce a large memory usage and long computation times. Unlike other
149 * auxiliary macros in this file and in crypto.h, which reflect how the
150 * library is configured, this macro defines how the library is
151 * configured. This implementation refuses to import or generate an
152 * RSA key whose size is larger than the value defined here.
153 *
154 * Note that an implementation may set different size limits for different
155 * operations, and does not need to accept all key sizes up to the limit. */
156#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
157
158/* The maximum size of an ECC key on this implementation, in bits.
159 * This is a vendor-specific macro. */
160#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
161#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
162#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
163#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
164#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
165#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
166#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
167#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
168#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
169#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
170#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
171#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
172#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
173#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
174#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
175#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
176#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
177#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
178#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
179#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
180#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
181#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
182#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
183#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
184#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
185#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
186#else
187#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
188#endif
189
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100190/** This macro returns the maximum supported length of the PSK for the
191 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100192 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100193 *
194 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100195 *
196 * Quoting RFC 4279, Sect 5.3:
197 * TLS implementations supporting these ciphersuites MUST support
198 * arbitrary PSK identities up to 128 octets in length, and arbitrary
199 * PSKs up to 64 octets in length. Supporting longer identities and
200 * keys is RECOMMENDED.
201 *
202 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100203 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100204 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100205#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100206
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100207/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100208#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200209
Gilles Peskineacd4be32018-07-08 19:56:25 +0200210/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200211 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200212 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200213 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100214 * \warning This macro may evaluate its arguments multiple times or
215 * zero times, so you should not pass arguments that contain
216 * side effects.
217 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200218 * \param key_type The type of the MAC key.
219 * \param key_bits The size of the MAC key in bits.
220 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100221 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200222 *
223 * \return The MAC size for the specified algorithm with
224 * the specified key parameters.
225 * \return 0 if the MAC algorithm is not recognized.
226 * \return Either 0 or the correct size for a MAC algorithm that
227 * the implementation recognizes, but does not support.
228 * \return Unspecified if the key parameters are not consistent
229 * with the algorithm.
230 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100231#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
232 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
233 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
234 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200235 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200236
237/** The maximum size of the output of psa_aead_encrypt(), in bytes.
238 *
239 * If the size of the ciphertext buffer is at least this large, it is
240 * guaranteed that psa_aead_encrypt() will not fail due to an
241 * insufficient buffer size. Depending on the algorithm, the actual size of
242 * the ciphertext may be smaller.
243 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100244 * \warning This macro may evaluate its arguments multiple times or
245 * zero times, so you should not pass arguments that contain
246 * side effects.
247 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200248 * \param alg An AEAD algorithm
249 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100250 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200251 * \param plaintext_length Size of the plaintext in bytes.
252 *
253 * \return The AEAD ciphertext size for the specified
254 * algorithm.
255 * If the AEAD algorithm is not recognized, return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200256 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100257#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
258 (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \
259 (plaintext_length) + PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200260 0)
261
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200262/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
263 * supported key types and AEAD algorithms.
264 *
265 * If the size of the ciphertext buffer is at least this large, it is guaranteed
266 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
267 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100268 * \note This macro returns a compile-time constant if its arguments are
269 * compile-time constants.
270 *
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200271 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, \p plaintext_length).
272 *
273 * \param plaintext_length Size of the plaintext in bytes.
274 *
275 * \return A sufficient output buffer size for any of the
276 * supported key types and AEAD algorithms.
277 *
278 */
279#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
280 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
281
282
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200283/** The maximum size of the output of psa_aead_decrypt(), in bytes.
284 *
285 * If the size of the plaintext buffer is at least this large, it is
286 * guaranteed that psa_aead_decrypt() will not fail due to an
287 * insufficient buffer size. Depending on the algorithm, the actual size of
288 * the plaintext may be smaller.
289 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100290 * \warning This macro may evaluate its arguments multiple times or
291 * zero times, so you should not pass arguments that contain
292 * side effects.
293 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200294 * \param alg An AEAD algorithm
295 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100296 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297 * \param ciphertext_length Size of the plaintext in bytes.
298 *
299 * \return The AEAD ciphertext size for the specified
300 * algorithm.
301 * If the AEAD algorithm is not recognized, return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200302 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100303#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
304 (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \
305 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200306 0)
307
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200308/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
309 * supported key types and AEAD algorithms.
310 *
311 * If the size of the plaintext buffer is at least this large, it is guaranteed
312 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
313 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100314 * \note This macro returns a compile-time constant if its arguments are
315 * compile-time constants.
316 *
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200317 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, \p ciphertext_length).
318 *
319 * \param ciphertext_length Size of the ciphertext in bytes.
320 *
321 * \return A sufficient output buffer size for any of the
322 * supported key types and AEAD algorithms.
323 *
324 */
325#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
326 (ciphertext_length)
327
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100328/** The default nonce size for an AEAD algorithm, in bytes.
329 *
330 * This macro can be used to allocate a buffer of sufficient size to
331 * store the nonce output from #psa_aead_generate_nonce().
332 *
333 * See also #PSA_AEAD_NONCE_MAX_SIZE.
334 *
335 * \note This is not the maximum size of nonce supported as input to
336 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
337 * just the default size that is generated by #psa_aead_generate_nonce().
338 *
339 * \warning This macro may evaluate its arguments multiple times or
340 * zero times, so you should not pass arguments that contain
341 * side effects.
342 *
343 * \param key_type A symmetric key type that is compatible with
344 * algorithm \p alg.
345 *
346 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
347 * #PSA_ALG_IS_AEAD(\p alg) is true).
348 *
349 * \return The default nonce size for the specified key type and algorithm.
350 * If the key type or AEAD algorithm is not recognized,
351 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100352 */
353#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100354 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
355 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM ? 13 : \
356 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_GCM ? 12 : \
357 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100358 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +0100359 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100360 0)
361
362/** The maximum default nonce size among all supported pairs of key types and
363 * AEAD algorithms, in bytes.
364 *
365 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
366 * may return.
367 *
368 * \note This is not the maximum size of nonce supported as input to
369 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
370 * just the largest size that may be generated by
371 * #psa_aead_generate_nonce().
372 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100373#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100374
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200375/** A sufficient output buffer size for psa_aead_update().
376 *
377 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200378 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200379 * insufficient buffer size. The actual size of the output may be smaller
380 * in any given call.
381 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100382 * \warning This macro may evaluate its arguments multiple times or
383 * zero times, so you should not pass arguments that contain
384 * side effects.
385 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200386 * \param alg An AEAD algorithm
387 * (\c PSA_ALG_XXX value such that
388 * #PSA_ALG_IS_AEAD(\p alg) is true).
389 * \param input_length Size of the input in bytes.
390 *
391 * \return A sufficient output buffer size for the specified
392 * algorithm.
393 * If the AEAD algorithm is not recognized, return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200394 */
395/* For all the AEAD modes defined in this specification, it is possible
396 * to emit output without delay. However, hardware may not always be
397 * capable of this. So for modes based on a block cipher, allow the
398 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100399#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
400 (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \
401 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
402 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
403 (input_length) : \
404 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200405
406/** A sufficient output buffer size for psa_aead_update(), for any of the
407 * supported key types and AEAD algorithms.
408 *
409 * If the size of the output buffer is at least this large, it is guaranteed
410 * that psa_aead_update() will not fail due to an insufficient buffer size.
411 *
412 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p alg, \p input_length).
413 *
414 * \param input_length Size of the input in bytes.
415 */
416#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
417 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200418
419/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200420 *
421 * If the size of the ciphertext buffer is at least this large, it is
422 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200423 * insufficient ciphertext buffer size. The actual size of the output may
424 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200425 *
426 * \param alg An AEAD algorithm
427 * (\c PSA_ALG_XXX value such that
428 * #PSA_ALG_IS_AEAD(\p alg) is true).
429 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200430 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200431 * specified algorithm.
432 * If the AEAD algorithm is not recognized, return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200433 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100434#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
435 (PSA_AEAD_NONCE_LENGTH(key_type, alg) && PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
436 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200437 0)
438
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200439/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
440 * supported key types and AEAD algorithms.
441 *
442 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p alg).
443 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200444#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200445
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200446/** A sufficient plaintext buffer size for psa_aead_verify().
447 *
448 * If the size of the plaintext buffer is at least this large, it is
449 * guaranteed that psa_aead_verify() will not fail due to an
450 * insufficient plaintext buffer size. The actual size of the output may
451 * be smaller in any given call.
452 *
453 * \param alg An AEAD algorithm
454 * (\c PSA_ALG_XXX value such that
455 * #PSA_ALG_IS_AEAD(\p alg) is true).
456 *
457 * \return A sufficient plaintext buffer size for the
458 * specified algorithm.
459 * If the AEAD algorithm is not recognized, return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200460 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100461#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
462 (PSA_AEAD_NONCE_LENGTH(key_type, alg) && PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
463 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200464 0)
465
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200466/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
467 * supported key types and AEAD algorithms.
468 *
469 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p alg).
470 */
471#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
472
Jaeden Amero7f042142019-02-07 10:44:38 +0000473#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
474 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100475 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100476 11 /*PKCS#1v1.5*/)
477
478/**
479 * \brief ECDSA signature size for a given curve bit size
480 *
481 * \param curve_bits Curve size in bits.
482 * \return Signature size in bytes.
483 *
484 * \note This macro returns a compile-time constant if its argument is one.
485 */
486#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
487 (PSA_BITS_TO_BYTES(curve_bits) * 2)
488
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100489/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200490 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200491 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200492 * of the specified type and size, with the specified algorithm.
493 * Note that the actual size of the signature may be smaller
494 * (some algorithms produce a variable-size signature).
495 *
496 * \warning This function may call its arguments multiple times or
497 * zero times, so you should not pass arguments that contain
498 * side effects.
499 *
500 * \param key_type An asymmetric key type (this may indifferently be a
501 * key pair type or a public key type).
502 * \param key_bits The size of the key in bits.
503 * \param alg The signature algorithm.
504 *
505 * \return If the parameters are valid and supported, return
506 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100507 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200508 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100509 * If the parameters are a valid combination that is not supported,
510 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200511 * If the parameters are not valid, the
512 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200513 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100514#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200515 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
516 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
517 ((void)alg, 0))
518
Gilles Peskine29755712019-11-08 15:49:40 +0100519#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
520 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
521
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100522/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100523 *
524 * Maximum size of an asymmetric signature.
525 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100526 * This macro expands to a compile-time constant integer. This value
527 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100528 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100529#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100530 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
531 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
532 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
533
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200534/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200535 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200536 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200537 * a key of the specified type and size, with the specified algorithm.
538 * Note that the actual size of the ciphertext may be smaller, depending
539 * on the algorithm.
540 *
541 * \warning This function may call its arguments multiple times or
542 * zero times, so you should not pass arguments that contain
543 * side effects.
544 *
545 * \param key_type An asymmetric key type (this may indifferently be a
546 * key pair type or a public key type).
547 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200548 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200549 *
550 * \return If the parameters are valid and supported, return
551 * a buffer size in bytes that guarantees that
552 * psa_asymmetric_encrypt() will not fail with
553 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100554 * If the parameters are a valid combination that is not supported,
555 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200556 * If the parameters are not valid, the
557 * return value is unspecified.
558 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200559#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
560 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
561 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
562 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200563
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200564/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
565 * supported asymmetric encryption.
566 *
567 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
568 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100569/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200570#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100571 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200572
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200573/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200574 *
Gilles Peskine76689602020-05-05 16:01:22 +0200575 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200576 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200577 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200578 * on the algorithm.
579 *
580 * \warning This function may call its arguments multiple times or
581 * zero times, so you should not pass arguments that contain
582 * side effects.
583 *
584 * \param key_type An asymmetric key type (this may indifferently be a
585 * key pair type or a public key type).
586 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200587 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200588 *
589 * \return If the parameters are valid and supported, return
590 * a buffer size in bytes that guarantees that
591 * psa_asymmetric_decrypt() will not fail with
592 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100593 * If the parameters are a valid combination that is not supported,
594 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200595 * If the parameters are not valid, the
596 * return value is unspecified.
597 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200598#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
599 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
600 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
601 0)
602
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200603/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
604 * supported asymmetric decryption.
605 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100606 * This macro assumes that RSA is the only supported asymmetric encryption.
607 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200608 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
609 */
610#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100611 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200612
Gilles Peskine1be949b2018-08-10 19:06:59 +0200613/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
614 * number of bits.
615 *
616 * This definition assumes that bits <= 2^19 - 9 so that the length field
617 * is at most 3 bytes. The length of the encoding is the length of the
618 * bit string padded to a whole number of bytes plus:
619 * - 1 type byte;
620 * - 1 to 3 length bytes;
621 * - 0 to 1 bytes of leading 0 due to the sign bit.
622 */
623#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
624 ((bits) / 8 + 5)
625
626/* Maximum size of the export encoding of an RSA public key.
627 * Assumes that the public exponent is less than 2^32.
628 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200629 * RSAPublicKey ::= SEQUENCE {
630 * modulus INTEGER, -- n
631 * publicExponent INTEGER } -- e
632 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000633 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200634 * - n : INTEGER;
635 * - 7 bytes for the public exponent.
636 */
637#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000638 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200639
640/* Maximum size of the export encoding of an RSA key pair.
641 * Assumes thatthe public exponent is less than 2^32 and that the size
642 * difference between the two primes is at most 1 bit.
643 *
644 * RSAPrivateKey ::= SEQUENCE {
645 * version Version, -- 0
646 * modulus INTEGER, -- N-bit
647 * publicExponent INTEGER, -- 32-bit
648 * privateExponent INTEGER, -- N-bit
649 * prime1 INTEGER, -- N/2-bit
650 * prime2 INTEGER, -- N/2-bit
651 * exponent1 INTEGER, -- N/2-bit
652 * exponent2 INTEGER, -- N/2-bit
653 * coefficient INTEGER, -- N/2-bit
654 * }
655 *
656 * - 4 bytes of SEQUENCE overhead;
657 * - 3 bytes of version;
658 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
659 * overapproximated as 9 half-size INTEGERS;
660 * - 7 bytes for the public exponent.
661 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200662#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200663 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
664
665/* Maximum size of the export encoding of a DSA public key.
666 *
667 * SubjectPublicKeyInfo ::= SEQUENCE {
668 * algorithm AlgorithmIdentifier,
669 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
670 * AlgorithmIdentifier ::= SEQUENCE {
671 * algorithm OBJECT IDENTIFIER,
672 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
673 * DSAPublicKey ::= INTEGER -- public key, Y
674 *
675 * - 3 * 4 bytes of SEQUENCE overhead;
676 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
677 * - 4 bytes of BIT STRING overhead;
678 * - 3 full-size INTEGERs (p, g, y);
679 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
680 */
681#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
682 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
683
684/* Maximum size of the export encoding of a DSA key pair.
685 *
686 * DSAPrivateKey ::= SEQUENCE {
687 * version Version, -- 0
688 * prime INTEGER, -- p
689 * subprime INTEGER, -- q
690 * generator INTEGER, -- g
691 * public INTEGER, -- y
692 * private INTEGER, -- x
693 * }
694 *
695 * - 4 bytes of SEQUENCE overhead;
696 * - 3 bytes of version;
697 * - 3 full-size INTEGERs (p, g, y);
698 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
699 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200700#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200701 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
702
703/* Maximum size of the export encoding of an ECC public key.
704 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000705 * The representation of an ECC public key is:
706 * - The byte 0x04;
707 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
708 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
709 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200710 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000711 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200712 */
713#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200715
716/* Maximum size of the export encoding of an ECC key pair.
717 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100718 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200719 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200720#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100721 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200722
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100723/** Sufficient output buffer size for psa_export_key() or
724 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200725 *
726 * This macro returns a compile-time constant if its arguments are
727 * compile-time constants.
728 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100729 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200730 * zero times, so you should not pass arguments that contain
731 * side effects.
732 *
733 * The following code illustrates how to allocate enough memory to export
734 * a key by querying the key type and size at runtime.
735 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200736 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200737 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200738 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200739 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200740 * psa_key_type_t key_type = psa_get_key_type(&attributes);
741 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100742 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200743 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200744 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200745 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200746 * size_t buffer_length;
747 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
748 * if (status != PSA_SUCCESS) handle_error(...);
749 * \endcode
750 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200751 * \param key_type A supported key type.
752 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200753 *
754 * \return If the parameters are valid and supported, return
755 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100756 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200757 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100758 * If the parameters are a valid combination that is not supported,
759 * return either a sensible size or 0.
760 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200761 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100762#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
763 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
764 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200765 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100766 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200767 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100768 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
769 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200770 0)
771
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200772/** Sufficient output buffer size for psa_export_public_key().
773 *
774 * This macro returns a compile-time constant if its arguments are
775 * compile-time constants.
776 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100777 * \warning This macro may evaluate its arguments multiple times or
778 * zero times, so you should not pass arguments that contain
779 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200780 *
781 * The following code illustrates how to allocate enough memory to export
782 * a public key by querying the key type and size at runtime.
783 * \code{c}
784 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
785 * psa_status_t status;
786 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100787 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200788 * psa_key_type_t key_type = psa_get_key_type(&attributes);
789 * size_t key_bits = psa_get_key_bits(&attributes);
790 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
791 * psa_reset_key_attributes(&attributes);
792 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100793 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200794 * size_t buffer_length;
795 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100796 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200797 * \endcode
798 *
799 * \param key_type A public key or key pair key type.
800 * \param key_bits The size of the key in bits.
801 *
802 * \return If the parameters are valid and supported, return
803 * a buffer size in bytes that guarantees that
804 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100805 * #PSA_ERROR_BUFFER_TOO_SMALL.
806 * If the parameters are a valid combination that is not
807 * supported, return either a sensible size or 0.
808 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200809 * the return value is unspecified.
810 *
811 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100812 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200813 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
814 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
815 * \p key_bits).
816 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100817#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
818 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
819 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200820 0)
821
822/** Sufficient buffer size for exporting any asymmetric key pair.
823 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100824 * This macro expands to a compile-time constant integer. This value is
825 * a sufficient buffer size when calling psa_export_key() to export any
826 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200827 *
828 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
829 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100830#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
831 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
832 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
833 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
834 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200835
836/** Sufficient buffer size for exporting any asymmetric public key.
837 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100838 * This macro expands to a compile-time constant integer. This value is
839 * a sufficient buffer size when calling psa_export_key() or
840 * psa_export_public_key() to export any asymmetric public key,
841 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200842 *
843 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
844 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100845#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
846 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
847 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
848 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
849 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200850
851/** Sufficient output buffer size for psa_raw_key_agreement().
852 *
853 * This macro returns a compile-time constant if its arguments are
854 * compile-time constants.
855 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100856 * \warning This macro may evaluate its arguments multiple times or
857 * zero times, so you should not pass arguments that contain
858 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200859 *
860 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
861 *
862 * \param key_type A supported key type.
863 * \param key_bits The size of the key in bits.
864 *
865 * \return If the parameters are valid and supported, return
866 * a buffer size in bytes that guarantees that
867 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100868 * #PSA_ERROR_BUFFER_TOO_SMALL.
869 * If the parameters are a valid combination that
870 * is not supported, return either a sensible size or 0.
871 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200872 * the return value is unspecified.
873 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100874/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200875#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
876 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100877 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200878 0)
879
880/** Maximum size of the output from psa_raw_key_agreement().
881 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100882 * This macro expands to a compile-time constant integer. This value is the
883 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200884 *
885 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
886 */
887#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100888 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200889
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100890/** The default IV size for a cipher algorithm, in bytes.
891 *
892 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
893 * the default IV length for the algorithm.
894 *
895 * This macro can be used to allocate a buffer of sufficient size to
896 * store the IV output from #psa_cipher_generate_iv() when using
897 * a multi-part cipher operation.
898 *
899 * See also #PSA_CIPHER_IV_MAX_SIZE.
900 *
901 * \warning This macro may evaluate its arguments multiple times or
902 * zero times, so you should not pass arguments that contain
903 * side effects.
904 *
905 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
906 *
907 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
908 *
909 * \return The default IV size for the specified key type and algorithm.
910 * If the algorithm does not use an IV, return 0.
911 * If the key type or cipher algorithm is not recognized,
912 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100913 */
914#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100915 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100916 ((alg) == PSA_ALG_CTR || \
917 (alg) == PSA_ALG_CFB || \
918 (alg) == PSA_ALG_OFB || \
919 (alg) == PSA_ALG_XTS || \
920 (alg) == PSA_ALG_CBC_NO_PADDING || \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100921 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100922 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúticbe39532020-12-08 00:01:31 +0100923 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100924 0)
925
926/** The maximum IV size for all supported cipher algorithms, in bytes.
927 *
928 * See also #PSA_CIPHER_IV_LENGTH().
929 */
930#define PSA_CIPHER_IV_MAX_SIZE 16
931
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200932/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
933 *
934 * If the size of the output buffer is at least this large, it is guaranteed
935 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
936 * Depending on the algorithm, the actual size of the output might be smaller.
937 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200938 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
939 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100940 * \warning This macro may evaluate its arguments multiple times or
941 * zero times, so you should not pass arguments that contain
942 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200943 *
944 * \param key_type A symmetric key type that is compatible with algorithm
945 * alg.
946 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
947 * #PSA_ALG_IS_CIPHER(\p alg) is true).
948 * \param input_length Size of the input in bytes.
949 *
950 * \return A sufficient output size for the specified key type and
951 * algorithm. If the key type or cipher algorithm is not
952 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100953 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200954 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100955#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
956 (alg == PSA_ALG_CBC_PKCS7 ? \
957 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arm9c3b5072021-03-10 15:57:44 +0100958 (input_length) + 1) + \
959 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100960 (PSA_ALG_IS_CIPHER(alg) ? \
961 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
962 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200963
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200964/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
965 * supported key types and cipher algorithms.
966 *
967 * If the size of the output buffer is at least this large, it is guaranteed
968 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
969 *
970 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
971 *
972 * \param input_length Size of the input in bytes.
973 *
974 */
975#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
976 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +0100977 (input_length) + 1) + \
978 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200979
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200980/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
981 *
982 * If the size of the output buffer is at least this large, it is guaranteed
983 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
984 * Depending on the algorithm, the actual size of the output might be smaller.
985 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200986 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200987 *
988 * \param key_type A symmetric key type that is compatible with algorithm
989 * alg.
990 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
991 * #PSA_ALG_IS_CIPHER(\p alg) is true).
992 * \param input_length Size of the input in bytes.
993 *
994 * \return A sufficient output size for the specified key type and
995 * algorithm. If the key type or cipher algorithm is not
996 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100997 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200998 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +0200999#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1000 (PSA_ALG_IS_CIPHER(alg) && \
1001 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1002 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001003 0)
1004
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001005/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1006 * supported key types and cipher algorithms.
1007 *
1008 * If the size of the output buffer is at least this large, it is guaranteed
1009 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1010 *
1011 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1012 *
1013 * \param input_length Size of the input in bytes.
1014 */
1015#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1016 (input_length)
1017
1018/** A sufficient output buffer size for psa_cipher_update().
1019 *
1020 * If the size of the output buffer is at least this large, it is guaranteed
1021 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1022 * The actual size of the output might be smaller in any given call.
1023 *
1024 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1025 *
1026 * \param key_type A symmetric key type that is compatible with algorithm
1027 * alg.
1028 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1029 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1030 * \param input_length Size of the input in bytes.
1031 *
1032 * \return A sufficient output size for the specified key type and
1033 * algorithm. If the key type or cipher algorithm is not
1034 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001035 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001036#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1037 (PSA_ALG_IS_CIPHER(alg) ? \
1038 (((alg) == PSA_ALG_CBC_PKCS7 || \
1039 (alg) == PSA_ALG_CBC_NO_PADDING || \
1040 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1041 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1042 input_length) : \
1043 (input_length)) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001044 0)
1045
1046/** A sufficient output buffer size for psa_cipher_update(), for any of the
1047 * supported key types and cipher algorithms.
1048 *
1049 * If the size of the output buffer is at least this large, it is guaranteed
1050 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1051 *
1052 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1053 *
1054 * \param input_length Size of the input in bytes.
1055 */
1056#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001057 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001058
1059/** A sufficient ciphertext buffer size for psa_cipher_finish().
1060 *
1061 * If the size of the ciphertext buffer is at least this large, it is
1062 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1063 * ciphertext buffer size. The actual size of the output might be smaller in
1064 * any given call.
1065 *
1066 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1067 *
1068 * \param key_type A symmetric key type that is compatible with algorithm
1069 * alg.
1070 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1071 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1072 * \return A sufficient output size for the specified key type and
1073 * algorithm. If the key type or cipher algorithm is not
1074 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001075 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001076#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1077 (PSA_ALG_IS_CIPHER(alg) ? \
1078 (alg == PSA_ALG_CBC_PKCS7 ? \
1079 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1080 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001081 0)
1082
1083/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1084 * supported key types and cipher algorithms.
1085 *
1086 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1087 */
1088#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1089 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1090
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001091#endif /* PSA_CRYPTO_SIZES_H */