blob: 9bbcb34446e35e12dbec2997dffee39d73241fd0 [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. */
Bence Szépkútic662b362021-05-27 11:25:03 +020045#include "mbedtls/build_info.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020046
Gilles Peskinea7c26db2018-12-12 13:42:25 +010047#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
48#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
49
Gilles Peskine248010c2019-05-14 16:08:59 +020050#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
51 (((length) + (block_size) - 1) / (block_size) * (block_size))
52
Gilles Peskinea7c26db2018-12-12 13:42:25 +010053/** The size of the output of psa_hash_finish(), in bytes.
54 *
55 * This is also the hash size that psa_hash_verify() expects.
56 *
57 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
58 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
59 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
60 * hash algorithm).
61 *
62 * \return The hash size for the specified hash algorithm.
63 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010064 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010065#define PSA_HASH_LENGTH(alg) \
66 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010067 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
68 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
69 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
70 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
71 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
80 0)
81
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +020082/** The input block size of a hash algorithm, in bytes.
83 *
84 * Hash algorithms process their input data in blocks. Hash operations will
85 * retain any partial blocks until they have enough input to fill the block or
86 * until the operation is finished.
87 * This affects the output from psa_hash_suspend().
88 *
89 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
90 * PSA_ALG_IS_HASH(\p alg) is true).
91 *
92 * \return The block size in bytes for the specified hash algorithm.
93 * If the hash algorithm is not recognized, return 0.
94 * An implementation can return either 0 or the correct size for a
95 * hash algorithm that it recognizes, but does not support.
96 */
97#define PSA_HASH_BLOCK_LENGTH(alg) \
98 ( \
99 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
100 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
101 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
102 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
103 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
104 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
109 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
110 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
111 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
112 0)
113
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200114/** \def PSA_HASH_MAX_SIZE
115 *
116 * Maximum size of a hash.
117 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100118 * This macro expands to a compile-time constant integer. This value
119 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200120 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200121/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
122 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
123 * HMAC-SHA3-512. */
Ronald Cronfcaba242021-10-18 09:10:31 +0200124#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200125#define PSA_HASH_MAX_SIZE 64
126#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
127#else
128#define PSA_HASH_MAX_SIZE 32
129#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
130#endif
131
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200132/** \def PSA_MAC_MAX_SIZE
133 *
134 * Maximum size of a MAC.
135 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100136 * This macro expands to a compile-time constant integer. This value
137 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200138 */
139/* All non-HMAC MACs have a maximum size that's smaller than the
140 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200141/* Note that the encoding of truncated MAC algorithms limits this value
142 * to 64 bytes.
143 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200144#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
145
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100146/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100147 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100148 * This macro can be used to allocate a buffer of sufficient size to store the
149 * tag output from psa_aead_finish().
150 *
151 * See also #PSA_AEAD_TAG_MAX_SIZE.
152 *
153 * \param key_type The type of the AEAD key.
154 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100155 * \param alg An AEAD algorithm
156 * (\c PSA_ALG_XXX value such that
157 * #PSA_ALG_IS_AEAD(\p alg) is true).
158 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200159 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100160 * If the AEAD algorithm does not have an identified
161 * tag that can be distinguished from the rest of
162 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100163 * If the key type or AEAD algorithm is not
164 * recognized, or the parameters are incompatible,
165 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100166 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100167#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200168 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200169 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100170 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100171
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200172/** The maximum tag size for all supported AEAD algorithms, in bytes.
173 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100174 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200175 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100176#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200177
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200178/* The maximum size of an RSA key on this implementation, in bits.
179 * This is a vendor-specific macro.
180 *
181 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
182 * whose parameters fit in a bignum is accepted. However large keys can
183 * induce a large memory usage and long computation times. Unlike other
184 * auxiliary macros in this file and in crypto.h, which reflect how the
185 * library is configured, this macro defines how the library is
186 * configured. This implementation refuses to import or generate an
187 * RSA key whose size is larger than the value defined here.
188 *
189 * Note that an implementation may set different size limits for different
190 * operations, and does not need to accept all key sizes up to the limit. */
191#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
192
193/* The maximum size of an ECC key on this implementation, in bits.
194 * This is a vendor-specific macro. */
195#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
196#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
197#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
198#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
199#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
200#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
201#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
202#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
203#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
204#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
205#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
206#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
207#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
208#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
209#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
210#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
211#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
212#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
213#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
214#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
215#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
216#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
217#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
218#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
219#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
220#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
221#else
222#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
223#endif
224
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100225/** This macro returns the maximum supported length of the PSK for the
226 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100227 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100228 *
229 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100230 *
231 * Quoting RFC 4279, Sect 5.3:
232 * TLS implementations supporting these ciphersuites MUST support
233 * arbitrary PSK identities up to 128 octets in length, and arbitrary
234 * PSKs up to 64 octets in length. Supporting longer identities and
235 * keys is RECOMMENDED.
236 *
237 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100238 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100239 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100240#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100241
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100242/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100243#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200244
Gilles Peskineacd4be32018-07-08 19:56:25 +0200245/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200246 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200247 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200248 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100249 * \warning This macro may evaluate its arguments multiple times or
250 * zero times, so you should not pass arguments that contain
251 * side effects.
252 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200253 * \param key_type The type of the MAC key.
254 * \param key_bits The size of the MAC key in bits.
255 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100256 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200257 *
258 * \return The MAC size for the specified algorithm with
259 * the specified key parameters.
260 * \return 0 if the MAC algorithm is not recognized.
261 * \return Either 0 or the correct size for a MAC algorithm that
262 * the implementation recognizes, but does not support.
263 * \return Unspecified if the key parameters are not consistent
264 * with the algorithm.
265 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100266#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
267 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
268 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
269 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200270 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200271
272/** The maximum size of the output of psa_aead_encrypt(), in bytes.
273 *
274 * If the size of the ciphertext buffer is at least this large, it is
275 * guaranteed that psa_aead_encrypt() will not fail due to an
276 * insufficient buffer size. Depending on the algorithm, the actual size of
277 * the ciphertext may be smaller.
278 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100279 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
280 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100281 * \warning This macro may evaluate its arguments multiple times or
282 * zero times, so you should not pass arguments that contain
283 * side effects.
284 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100285 * \param key_type A symmetric key type that is
286 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200287 * \param alg An AEAD algorithm
288 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100289 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200290 * \param plaintext_length Size of the plaintext in bytes.
291 *
292 * \return The AEAD ciphertext size for the specified
293 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100294 * If the key type or AEAD algorithm is not
295 * recognized, or the parameters are incompatible,
296 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100298#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200299 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200300 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200301 0)
302
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200303/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
304 * supported key types and AEAD algorithms.
305 *
306 * If the size of the ciphertext buffer is at least this large, it is guaranteed
307 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
308 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100309 * \note This macro returns a compile-time constant if its arguments are
310 * compile-time constants.
311 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100312 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
313 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200314 *
315 * \param plaintext_length Size of the plaintext in bytes.
316 *
317 * \return A sufficient output buffer size for any of the
318 * supported key types and AEAD algorithms.
319 *
320 */
321#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
322 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
323
324
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200325/** The maximum size of the output of psa_aead_decrypt(), in bytes.
326 *
327 * If the size of the plaintext buffer is at least this large, it is
328 * guaranteed that psa_aead_decrypt() will not fail due to an
329 * insufficient buffer size. Depending on the algorithm, the actual size of
330 * the plaintext may be smaller.
331 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100332 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
333 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100334 * \warning This macro may evaluate its arguments multiple times or
335 * zero times, so you should not pass arguments that contain
336 * side effects.
337 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100338 * \param key_type A symmetric key type that is
339 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200340 * \param alg An AEAD algorithm
341 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100342 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200343 * \param ciphertext_length Size of the plaintext in bytes.
344 *
345 * \return The AEAD ciphertext size for the specified
346 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100347 * If the key type or AEAD algorithm is not
348 * recognized, or the parameters are incompatible,
349 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200350 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100351#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200352 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
353 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
354 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200355 0)
356
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200357/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
358 * supported key types and AEAD algorithms.
359 *
360 * If the size of the plaintext buffer is at least this large, it is guaranteed
361 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
362 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100363 * \note This macro returns a compile-time constant if its arguments are
364 * compile-time constants.
365 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100366 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
367 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200368 *
369 * \param ciphertext_length Size of the ciphertext in bytes.
370 *
371 * \return A sufficient output buffer size for any of the
372 * supported key types and AEAD algorithms.
373 *
374 */
375#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
376 (ciphertext_length)
377
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100378/** The default nonce size for an AEAD algorithm, in bytes.
379 *
380 * This macro can be used to allocate a buffer of sufficient size to
381 * store the nonce output from #psa_aead_generate_nonce().
382 *
383 * See also #PSA_AEAD_NONCE_MAX_SIZE.
384 *
385 * \note This is not the maximum size of nonce supported as input to
386 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
387 * just the default size that is generated by #psa_aead_generate_nonce().
388 *
389 * \warning This macro may evaluate its arguments multiple times or
390 * zero times, so you should not pass arguments that contain
391 * side effects.
392 *
393 * \param key_type A symmetric key type that is compatible with
394 * algorithm \p alg.
395 *
396 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
397 * #PSA_ALG_IS_AEAD(\p alg) is true).
398 *
399 * \return The default nonce size for the specified key type and algorithm.
400 * If the key type or AEAD algorithm is not recognized,
401 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100402 */
403#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100404 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Bence Szépkútib639d432021-04-21 10:33:54 +0200405 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
406 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100407 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100408 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkútib639d432021-04-21 10:33:54 +0200409 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100410 0)
411
412/** The maximum default nonce size among all supported pairs of key types and
413 * AEAD algorithms, in bytes.
414 *
415 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
416 * may return.
417 *
418 * \note This is not the maximum size of nonce supported as input to
419 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
420 * just the largest size that may be generated by
421 * #psa_aead_generate_nonce().
422 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100423#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100424
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200425/** A sufficient output buffer size for psa_aead_update().
426 *
427 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200428 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200429 * insufficient buffer size. The actual size of the output may be smaller
430 * in any given call.
431 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100432 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
433 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100434 * \warning This macro may evaluate its arguments multiple times or
435 * zero times, so you should not pass arguments that contain
436 * side effects.
437 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100438 * \param key_type A symmetric key type that is
439 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200440 * \param alg An AEAD algorithm
441 * (\c PSA_ALG_XXX value such that
442 * #PSA_ALG_IS_AEAD(\p alg) is true).
443 * \param input_length Size of the input in bytes.
444 *
445 * \return A sufficient output buffer size for the specified
446 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100447 * If the key type or AEAD algorithm is not
448 * recognized, or the parameters are incompatible,
449 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200450 */
451/* For all the AEAD modes defined in this specification, it is possible
452 * to emit output without delay. However, hardware may not always be
453 * capable of this. So for modes based on a block cipher, allow the
454 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100455#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200456 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100457 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
458 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
459 (input_length) : \
460 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200461
462/** A sufficient output buffer size for psa_aead_update(), for any of the
463 * supported key types and AEAD algorithms.
464 *
465 * If the size of the output buffer is at least this large, it is guaranteed
466 * that psa_aead_update() will not fail due to an insufficient buffer size.
467 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100468 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200469 *
470 * \param input_length Size of the input in bytes.
471 */
472#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
473 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200474
475/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200476 *
477 * If the size of the ciphertext buffer is at least this large, it is
478 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200479 * insufficient ciphertext buffer size. The actual size of the output may
480 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200481 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100482 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
483 *
484 * \param key_type A symmetric key type that is
485 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200486 * \param alg An AEAD algorithm
487 * (\c PSA_ALG_XXX value such that
488 * #PSA_ALG_IS_AEAD(\p alg) is true).
489 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200490 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200491 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100492 * If the key type or AEAD algorithm is not
493 * recognized, or the parameters are incompatible,
494 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200495 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200496#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
497 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
498 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
499 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200500 0)
501
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200502/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
503 * supported key types and AEAD algorithms.
504 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100505 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200506 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200507#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200508
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200509/** A sufficient plaintext buffer size for psa_aead_verify().
510 *
511 * If the size of the plaintext buffer is at least this large, it is
512 * guaranteed that psa_aead_verify() will not fail due to an
513 * insufficient plaintext buffer size. The actual size of the output may
514 * be smaller in any given call.
515 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100516 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
517 *
518 * \param key_type A symmetric key type that is
519 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200520 * \param alg An AEAD algorithm
521 * (\c PSA_ALG_XXX value such that
522 * #PSA_ALG_IS_AEAD(\p alg) is true).
523 *
524 * \return A sufficient plaintext buffer size for the
525 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100526 * If the key type or AEAD algorithm is not
527 * recognized, or the parameters are incompatible,
528 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200529 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200530#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
531 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
532 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
533 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200534 0)
535
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200536/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
537 * supported key types and AEAD algorithms.
538 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100539 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200540 */
541#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
542
Jaeden Amero7f042142019-02-07 10:44:38 +0000543#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
544 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100545 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100546 11 /*PKCS#1v1.5*/)
547
548/**
549 * \brief ECDSA signature size for a given curve bit size
550 *
551 * \param curve_bits Curve size in bits.
552 * \return Signature size in bytes.
553 *
554 * \note This macro returns a compile-time constant if its argument is one.
555 */
556#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
557 (PSA_BITS_TO_BYTES(curve_bits) * 2)
558
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200560 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200561 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200562 * of the specified type and size, with the specified algorithm.
563 * Note that the actual size of the signature may be smaller
564 * (some algorithms produce a variable-size signature).
565 *
566 * \warning This function may call its arguments multiple times or
567 * zero times, so you should not pass arguments that contain
568 * side effects.
569 *
570 * \param key_type An asymmetric key type (this may indifferently be a
571 * key pair type or a public key type).
572 * \param key_bits The size of the key in bits.
573 * \param alg The signature algorithm.
574 *
575 * \return If the parameters are valid and supported, return
576 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100577 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200578 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100579 * If the parameters are a valid combination that is not supported,
580 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200581 * If the parameters are not valid, the
582 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200583 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100584#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200585 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
586 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
587 ((void)alg, 0))
588
Gilles Peskine29755712019-11-08 15:49:40 +0100589#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
590 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
591
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100592/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100593 *
594 * Maximum size of an asymmetric signature.
595 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100596 * This macro expands to a compile-time constant integer. This value
597 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100598 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100599#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100600 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
601 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
602 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
603
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200604/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200605 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200606 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200607 * a key of the specified type and size, with the specified algorithm.
608 * Note that the actual size of the ciphertext may be smaller, depending
609 * on the algorithm.
610 *
611 * \warning This function may call its arguments multiple times or
612 * zero times, so you should not pass arguments that contain
613 * side effects.
614 *
615 * \param key_type An asymmetric key type (this may indifferently be a
616 * key pair type or a public key type).
617 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200618 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200619 *
620 * \return If the parameters are valid and supported, return
621 * a buffer size in bytes that guarantees that
622 * psa_asymmetric_encrypt() will not fail with
623 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100624 * If the parameters are a valid combination that is not supported,
625 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200626 * If the parameters are not valid, the
627 * return value is unspecified.
628 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200629#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
630 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
631 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
632 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200633
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200634/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
635 * supported asymmetric encryption.
636 *
637 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
638 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100639/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200640#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100641 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200642
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200643/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 *
Gilles Peskine76689602020-05-05 16:01:22 +0200645 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200646 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200647 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200648 * on the algorithm.
649 *
650 * \warning This function may call its arguments multiple times or
651 * zero times, so you should not pass arguments that contain
652 * side effects.
653 *
654 * \param key_type An asymmetric key type (this may indifferently be a
655 * key pair type or a public key type).
656 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200657 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200658 *
659 * \return If the parameters are valid and supported, return
660 * a buffer size in bytes that guarantees that
661 * psa_asymmetric_decrypt() will not fail with
662 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100663 * If the parameters are a valid combination that is not supported,
664 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200665 * If the parameters are not valid, the
666 * return value is unspecified.
667 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200668#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
669 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
670 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
671 0)
672
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200673/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
674 * supported asymmetric decryption.
675 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100676 * This macro assumes that RSA is the only supported asymmetric encryption.
677 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200678 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
679 */
680#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100681 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200682
Gilles Peskine1be949b2018-08-10 19:06:59 +0200683/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
684 * number of bits.
685 *
686 * This definition assumes that bits <= 2^19 - 9 so that the length field
687 * is at most 3 bytes. The length of the encoding is the length of the
688 * bit string padded to a whole number of bytes plus:
689 * - 1 type byte;
690 * - 1 to 3 length bytes;
691 * - 0 to 1 bytes of leading 0 due to the sign bit.
692 */
693#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
694 ((bits) / 8 + 5)
695
696/* Maximum size of the export encoding of an RSA public key.
697 * Assumes that the public exponent is less than 2^32.
698 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200699 * RSAPublicKey ::= SEQUENCE {
700 * modulus INTEGER, -- n
701 * publicExponent INTEGER } -- e
702 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000703 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200704 * - n : INTEGER;
705 * - 7 bytes for the public exponent.
706 */
707#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000708 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200709
710/* Maximum size of the export encoding of an RSA key pair.
711 * Assumes thatthe public exponent is less than 2^32 and that the size
712 * difference between the two primes is at most 1 bit.
713 *
714 * RSAPrivateKey ::= SEQUENCE {
715 * version Version, -- 0
716 * modulus INTEGER, -- N-bit
717 * publicExponent INTEGER, -- 32-bit
718 * privateExponent INTEGER, -- N-bit
719 * prime1 INTEGER, -- N/2-bit
720 * prime2 INTEGER, -- N/2-bit
721 * exponent1 INTEGER, -- N/2-bit
722 * exponent2 INTEGER, -- N/2-bit
723 * coefficient INTEGER, -- N/2-bit
724 * }
725 *
726 * - 4 bytes of SEQUENCE overhead;
727 * - 3 bytes of version;
728 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
729 * overapproximated as 9 half-size INTEGERS;
730 * - 7 bytes for the public exponent.
731 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200732#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200733 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
734
735/* Maximum size of the export encoding of a DSA public key.
736 *
737 * SubjectPublicKeyInfo ::= SEQUENCE {
738 * algorithm AlgorithmIdentifier,
739 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
740 * AlgorithmIdentifier ::= SEQUENCE {
741 * algorithm OBJECT IDENTIFIER,
742 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
743 * DSAPublicKey ::= INTEGER -- public key, Y
744 *
745 * - 3 * 4 bytes of SEQUENCE overhead;
746 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
747 * - 4 bytes of BIT STRING overhead;
748 * - 3 full-size INTEGERs (p, g, y);
749 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
750 */
751#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
752 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
753
754/* Maximum size of the export encoding of a DSA key pair.
755 *
756 * DSAPrivateKey ::= SEQUENCE {
757 * version Version, -- 0
758 * prime INTEGER, -- p
759 * subprime INTEGER, -- q
760 * generator INTEGER, -- g
761 * public INTEGER, -- y
762 * private INTEGER, -- x
763 * }
764 *
765 * - 4 bytes of SEQUENCE overhead;
766 * - 3 bytes of version;
767 * - 3 full-size INTEGERs (p, g, y);
768 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
769 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200771 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
772
773/* Maximum size of the export encoding of an ECC public key.
774 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000775 * The representation of an ECC public key is:
776 * - The byte 0x04;
777 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
778 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
779 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200780 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200782 */
783#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200785
786/* Maximum size of the export encoding of an ECC key pair.
787 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100788 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200789 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200790#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100791 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200792
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100793/** Sufficient output buffer size for psa_export_key() or
794 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200795 *
796 * This macro returns a compile-time constant if its arguments are
797 * compile-time constants.
798 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100799 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200800 * zero times, so you should not pass arguments that contain
801 * side effects.
802 *
803 * The following code illustrates how to allocate enough memory to export
804 * a key by querying the key type and size at runtime.
805 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200806 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200807 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200808 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200810 * psa_key_type_t key_type = psa_get_key_type(&attributes);
811 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100812 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200813 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200814 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200815 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200816 * size_t buffer_length;
817 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
818 * if (status != PSA_SUCCESS) handle_error(...);
819 * \endcode
820 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200821 * \param key_type A supported key type.
822 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200823 *
824 * \return If the parameters are valid and supported, return
825 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100826 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200827 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100828 * If the parameters are a valid combination that is not supported,
829 * return either a sensible size or 0.
830 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200831 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100832#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
833 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
834 (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 +0200835 (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 +0100836 (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 +0200837 (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 +0100838 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
839 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 +0200840 0)
841
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200842/** Sufficient output buffer size for psa_export_public_key().
843 *
844 * This macro returns a compile-time constant if its arguments are
845 * compile-time constants.
846 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100847 * \warning This macro may evaluate its arguments multiple times or
848 * zero times, so you should not pass arguments that contain
849 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200850 *
851 * The following code illustrates how to allocate enough memory to export
852 * a public key by querying the key type and size at runtime.
853 * \code{c}
854 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
855 * psa_status_t status;
856 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100857 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200858 * psa_key_type_t key_type = psa_get_key_type(&attributes);
859 * size_t key_bits = psa_get_key_bits(&attributes);
860 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
861 * psa_reset_key_attributes(&attributes);
862 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100863 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200864 * size_t buffer_length;
865 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100866 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200867 * \endcode
868 *
869 * \param key_type A public key or key pair key type.
870 * \param key_bits The size of the key in bits.
871 *
872 * \return If the parameters are valid and supported, return
873 * a buffer size in bytes that guarantees that
874 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100875 * #PSA_ERROR_BUFFER_TOO_SMALL.
876 * If the parameters are a valid combination that is not
877 * supported, return either a sensible size or 0.
878 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200879 * the return value is unspecified.
880 *
881 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100882 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200883 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
884 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
885 * \p key_bits).
886 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100887#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
888 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
889 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 +0200890 0)
891
892/** Sufficient buffer size for exporting any asymmetric key pair.
893 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100894 * This macro expands to a compile-time constant integer. This value is
895 * a sufficient buffer size when calling psa_export_key() to export any
896 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200897 *
898 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
899 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100900#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
901 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
902 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
903 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
904 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200905
906/** Sufficient buffer size for exporting any asymmetric public key.
907 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100908 * This macro expands to a compile-time constant integer. This value is
909 * a sufficient buffer size when calling psa_export_key() or
910 * psa_export_public_key() to export any asymmetric public key,
911 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200912 *
913 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
914 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100915#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
916 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
917 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
918 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
919 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200920
921/** Sufficient output buffer size for psa_raw_key_agreement().
922 *
923 * This macro returns a compile-time constant if its arguments are
924 * compile-time constants.
925 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100926 * \warning This macro may evaluate its arguments multiple times or
927 * zero times, so you should not pass arguments that contain
928 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200929 *
930 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
931 *
932 * \param key_type A supported key type.
933 * \param key_bits The size of the key in bits.
934 *
935 * \return If the parameters are valid and supported, return
936 * a buffer size in bytes that guarantees that
937 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100938 * #PSA_ERROR_BUFFER_TOO_SMALL.
939 * If the parameters are a valid combination that
940 * is not supported, return either a sensible size or 0.
941 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200942 * the return value is unspecified.
943 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100944/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200945#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
946 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100947 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200948 0)
949
950/** Maximum size of the output from psa_raw_key_agreement().
951 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100952 * This macro expands to a compile-time constant integer. This value is the
953 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200954 *
955 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
956 */
957#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100958 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200959
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100960/** The default IV size for a cipher algorithm, in bytes.
961 *
962 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
963 * the default IV length for the algorithm.
964 *
965 * This macro can be used to allocate a buffer of sufficient size to
966 * store the IV output from #psa_cipher_generate_iv() when using
967 * a multi-part cipher operation.
968 *
969 * See also #PSA_CIPHER_IV_MAX_SIZE.
970 *
971 * \warning This macro may evaluate its arguments multiple times or
972 * zero times, so you should not pass arguments that contain
973 * side effects.
974 *
975 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
976 *
977 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
978 *
979 * \return The default IV size for the specified key type and algorithm.
980 * If the algorithm does not use an IV, return 0.
981 * If the key type or cipher algorithm is not recognized,
982 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100983 */
984#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100985 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100986 ((alg) == PSA_ALG_CTR || \
987 (alg) == PSA_ALG_CFB || \
988 (alg) == PSA_ALG_OFB || \
989 (alg) == PSA_ALG_XTS || \
990 (alg) == PSA_ALG_CBC_NO_PADDING || \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100991 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100992 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúticbe39532020-12-08 00:01:31 +0100993 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200994 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
995 0)
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100996
997/** The maximum IV size for all supported cipher algorithms, in bytes.
998 *
999 * See also #PSA_CIPHER_IV_LENGTH().
1000 */
1001#define PSA_CIPHER_IV_MAX_SIZE 16
1002
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001003/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1004 *
1005 * If the size of the output buffer is at least this large, it is guaranteed
1006 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1007 * Depending on the algorithm, the actual size of the output might be smaller.
1008 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001009 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1010 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001011 * \warning This macro may evaluate its arguments multiple times or
1012 * zero times, so you should not pass arguments that contain
1013 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001014 *
1015 * \param key_type A symmetric key type that is compatible with algorithm
1016 * alg.
1017 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1018 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1019 * \param input_length Size of the input in bytes.
1020 *
1021 * \return A sufficient output size for the specified key type and
1022 * algorithm. If the key type or cipher algorithm is not
1023 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001024 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001025 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001026#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1027 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliottc22950c2021-07-08 16:50:01 +01001028 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001029 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arm9c3b5072021-03-10 15:57:44 +01001030 (input_length) + 1) + \
Paul Elliotta02003b2021-07-07 17:20:06 +01001031 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001032 (PSA_ALG_IS_CIPHER(alg) ? \
1033 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1034 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001035
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001036/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1037 * supported key types and cipher algorithms.
1038 *
1039 * If the size of the output buffer is at least this large, it is guaranteed
1040 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1041 *
1042 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1043 *
1044 * \param input_length Size of the input in bytes.
1045 *
1046 */
1047#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1048 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001049 (input_length) + 1) + \
1050 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001051
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001052/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1053 *
1054 * If the size of the output buffer is at least this large, it is guaranteed
1055 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1056 * Depending on the algorithm, the actual size of the output might be smaller.
1057 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001058 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001059 *
1060 * \param key_type A symmetric key type that is compatible with algorithm
1061 * alg.
1062 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1063 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1064 * \param input_length Size of the input in bytes.
1065 *
1066 * \return A sufficient output size for the specified key type and
1067 * algorithm. If the key type or cipher algorithm is not
1068 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001069 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001070 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001071#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1072 (PSA_ALG_IS_CIPHER(alg) && \
1073 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1074 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001075 0)
1076
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001077/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1078 * supported key types and cipher algorithms.
1079 *
1080 * If the size of the output buffer is at least this large, it is guaranteed
1081 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1082 *
1083 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1084 *
1085 * \param input_length Size of the input in bytes.
1086 */
1087#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1088 (input_length)
1089
1090/** A sufficient output buffer size for psa_cipher_update().
1091 *
1092 * If the size of the output buffer is at least this large, it is guaranteed
1093 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1094 * The actual size of the output might be smaller in any given call.
1095 *
1096 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1097 *
1098 * \param key_type A symmetric key type that is compatible with algorithm
1099 * alg.
1100 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1101 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1102 * \param input_length Size of the input in bytes.
1103 *
1104 * \return A sufficient output size for the specified key type and
1105 * algorithm. If the key type or cipher algorithm is not
1106 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001107 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001108#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1109 (PSA_ALG_IS_CIPHER(alg) ? \
Paul Elliott6603e2b2021-07-08 16:53:42 +01001110 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001111 (((alg) == PSA_ALG_CBC_PKCS7 || \
1112 (alg) == PSA_ALG_CBC_NO_PADDING || \
1113 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1114 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1115 input_length) : \
Paul Elliott6603e2b2021-07-08 16:53:42 +01001116 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001117 0)
1118
1119/** A sufficient output buffer size for psa_cipher_update(), for any of the
1120 * supported key types and cipher algorithms.
1121 *
1122 * If the size of the output buffer is at least this large, it is guaranteed
1123 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1124 *
1125 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1126 *
1127 * \param input_length Size of the input in bytes.
1128 */
1129#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001130 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001131
1132/** A sufficient ciphertext buffer size for psa_cipher_finish().
1133 *
1134 * If the size of the ciphertext buffer is at least this large, it is
1135 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1136 * ciphertext buffer size. The actual size of the output might be smaller in
1137 * any given call.
1138 *
1139 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1140 *
1141 * \param key_type A symmetric key type that is compatible with algorithm
1142 * alg.
1143 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1144 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1145 * \return A sufficient output size for the specified key type and
1146 * algorithm. If the key type or cipher algorithm is not
1147 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001148 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001149#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1150 (PSA_ALG_IS_CIPHER(alg) ? \
1151 (alg == PSA_ALG_CBC_PKCS7 ? \
1152 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1153 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001154 0)
1155
1156/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1157 * supported key types and cipher algorithms.
1158 *
1159 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1160 */
1161#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1162 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1163
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001164#endif /* PSA_CRYPTO_SIZES_H */