blob: 6b8ccbb9a4f5e99cc303e31d658972fe356a084d [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
Ronald Cronf6236f02023-01-26 16:22:25 +010043/*
44 * Include the build-time configuration information file. Here, we do not
45 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
46 * is basically just an alias to it. This is to ease the maintenance of the
47 * PSA cryptography repository which has a different build system and
48 * configuration.
49 */
50#include "psa/build_info.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020051
Gilles Peskinea7c26db2018-12-12 13:42:25 +010052#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
53#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
54
Gilles Peskine248010c2019-05-14 16:08:59 +020055#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
56 (((length) + (block_size) - 1) / (block_size) * (block_size))
57
Gilles Peskinea7c26db2018-12-12 13:42:25 +010058/** The size of the output of psa_hash_finish(), in bytes.
59 *
60 * This is also the hash size that psa_hash_verify() expects.
61 *
62 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
63 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
64 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
65 * hash algorithm).
66 *
67 * \return The hash size for the specified hash algorithm.
68 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010069 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010070#define PSA_HASH_LENGTH(alg) \
71 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010072 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
85 0)
86
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +020087/** The input block size of a hash algorithm, in bytes.
88 *
89 * Hash algorithms process their input data in blocks. Hash operations will
90 * retain any partial blocks until they have enough input to fill the block or
91 * until the operation is finished.
92 * This affects the output from psa_hash_suspend().
93 *
94 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
95 * PSA_ALG_IS_HASH(\p alg) is true).
96 *
97 * \return The block size in bytes for the specified hash algorithm.
98 * If the hash algorithm is not recognized, return 0.
99 * An implementation can return either 0 or the correct size for a
100 * hash algorithm that it recognizes, but does not support.
101 */
102#define PSA_HASH_BLOCK_LENGTH(alg) \
103 ( \
104 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
109 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
110 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
111 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
112 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
113 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
114 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
115 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
116 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
117 0)
118
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200119/** \def PSA_HASH_MAX_SIZE
120 *
121 * Maximum size of a hash.
122 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100123 * This macro expands to a compile-time constant integer. This value
124 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200125 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200126/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
127 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
128 * HMAC-SHA3-512. */
Ronald Cronfcaba242021-10-18 09:10:31 +0200129#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200130#define PSA_HASH_MAX_SIZE 64
131#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
132#else
133#define PSA_HASH_MAX_SIZE 32
134#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
135#endif
136
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200137/** \def PSA_MAC_MAX_SIZE
138 *
139 * Maximum size of a MAC.
140 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100141 * This macro expands to a compile-time constant integer. This value
142 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200143 */
144/* All non-HMAC MACs have a maximum size that's smaller than the
145 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200146/* Note that the encoding of truncated MAC algorithms limits this value
147 * to 64 bytes.
148 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200149#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
150
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100151/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100152 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100153 * This macro can be used to allocate a buffer of sufficient size to store the
154 * tag output from psa_aead_finish().
155 *
156 * See also #PSA_AEAD_TAG_MAX_SIZE.
157 *
158 * \param key_type The type of the AEAD key.
159 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100160 * \param alg An AEAD algorithm
161 * (\c PSA_ALG_XXX value such that
162 * #PSA_ALG_IS_AEAD(\p alg) is true).
163 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200164 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100165 * If the AEAD algorithm does not have an identified
166 * tag that can be distinguished from the rest of
167 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100168 * If the key type or AEAD algorithm is not
169 * recognized, or the parameters are incompatible,
170 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100171 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100172#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200173 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200174 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100175 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100176
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200177/** The maximum tag size for all supported AEAD algorithms, in bytes.
178 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100179 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200180 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100181#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200182
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200183/* The maximum size of an RSA key on this implementation, in bits.
184 * This is a vendor-specific macro.
185 *
186 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
187 * whose parameters fit in a bignum is accepted. However large keys can
188 * induce a large memory usage and long computation times. Unlike other
189 * auxiliary macros in this file and in crypto.h, which reflect how the
190 * library is configured, this macro defines how the library is
191 * configured. This implementation refuses to import or generate an
192 * RSA key whose size is larger than the value defined here.
193 *
194 * Note that an implementation may set different size limits for different
195 * operations, and does not need to accept all key sizes up to the limit. */
196#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
197
198/* The maximum size of an ECC key on this implementation, in bits.
199 * This is a vendor-specific macro. */
Valerio Setti271c12e2023-03-23 16:30:27 +0100200#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200201#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Valerio Setti271c12e2023-03-23 16:30:27 +0100202#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200203#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
Valerio Setti271c12e2023-03-23 16:30:27 +0100204#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200205#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
Valerio Setti271c12e2023-03-23 16:30:27 +0100206#elif defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200207#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Valerio Setti271c12e2023-03-23 16:30:27 +0100208#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200209#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Valerio Setti271c12e2023-03-23 16:30:27 +0100210#elif defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200211#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100212#elif defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200213#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100214#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200215#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100216#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200217#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
Valerio Setti271c12e2023-03-23 16:30:27 +0100218#elif defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200219#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Valerio Setti271c12e2023-03-23 16:30:27 +0100220#elif defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200221#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Valerio Setti271c12e2023-03-23 16:30:27 +0100222#elif defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200223#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
Valerio Setti271c12e2023-03-23 16:30:27 +0100224#elif defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200225#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
226#else
227#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
228#endif
229
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100230/** This macro returns the maximum supported length of the PSK for the
231 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100232 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100233 *
234 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100235 *
236 * Quoting RFC 4279, Sect 5.3:
237 * TLS implementations supporting these ciphersuites MUST support
238 * arbitrary PSK identities up to 128 octets in length, and arbitrary
239 * PSKs up to 64 octets in length. Supporting longer identities and
240 * keys is RECOMMENDED.
241 *
242 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100243 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100244 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100245#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100246
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400247/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
248 * which is expected to work with P-256 curve only. */
249#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65
250
251/* The size of a serialized K.X coordinate to be used in
252 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
253 * curve. */
254#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32
255
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530256/* The maximum number of iterations for PBKDF2 on this implementation, in bits.
257 * This is a vendor-specific macro. This can be configured if necessary */
258#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffff
259
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100260/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100261#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200262
Gilles Peskineacd4be32018-07-08 19:56:25 +0200263/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200264 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200265 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200266 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100267 * \warning This macro may evaluate its arguments multiple times or
268 * zero times, so you should not pass arguments that contain
269 * side effects.
270 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200271 * \param key_type The type of the MAC key.
272 * \param key_bits The size of the MAC key in bits.
273 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100274 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200275 *
276 * \return The MAC size for the specified algorithm with
277 * the specified key parameters.
278 * \return 0 if the MAC algorithm is not recognized.
279 * \return Either 0 or the correct size for a MAC algorithm that
280 * the implementation recognizes, but does not support.
281 * \return Unspecified if the key parameters are not consistent
282 * with the algorithm.
283 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100284#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
285 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
286 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
287 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 ((void) (key_type), (void) (key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200289
290/** The maximum size of the output of psa_aead_encrypt(), in bytes.
291 *
292 * If the size of the ciphertext buffer is at least this large, it is
293 * guaranteed that psa_aead_encrypt() will not fail due to an
294 * insufficient buffer size. Depending on the algorithm, the actual size of
295 * the ciphertext may be smaller.
296 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100297 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
298 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100299 * \warning This macro may evaluate its arguments multiple times or
300 * zero times, so you should not pass arguments that contain
301 * side effects.
302 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100303 * \param key_type A symmetric key type that is
304 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200305 * \param alg An AEAD algorithm
306 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100307 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200308 * \param plaintext_length Size of the plaintext in bytes.
309 *
310 * \return The AEAD ciphertext size for the specified
311 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100312 * If the key type or AEAD algorithm is not
313 * recognized, or the parameters are incompatible,
314 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200315 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100316#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200317 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200318 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200319 0)
320
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200321/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
322 * supported key types and AEAD algorithms.
323 *
324 * If the size of the ciphertext buffer is at least this large, it is guaranteed
325 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
326 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100327 * \note This macro returns a compile-time constant if its arguments are
328 * compile-time constants.
329 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100330 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
331 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200332 *
333 * \param plaintext_length Size of the plaintext in bytes.
334 *
335 * \return A sufficient output buffer size for any of the
336 * supported key types and AEAD algorithms.
337 *
338 */
339#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
340 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
341
342
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200343/** The maximum size of the output of psa_aead_decrypt(), in bytes.
344 *
345 * If the size of the plaintext buffer is at least this large, it is
346 * guaranteed that psa_aead_decrypt() will not fail due to an
347 * insufficient buffer size. Depending on the algorithm, the actual size of
348 * the plaintext may be smaller.
349 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100350 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
351 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100352 * \warning This macro may evaluate its arguments multiple times or
353 * zero times, so you should not pass arguments that contain
354 * side effects.
355 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100356 * \param key_type A symmetric key type that is
357 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200358 * \param alg An AEAD algorithm
359 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100360 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200361 * \param ciphertext_length Size of the plaintext in bytes.
362 *
363 * \return The AEAD ciphertext size for the specified
364 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100365 * If the key type or AEAD algorithm is not
366 * recognized, or the parameters are incompatible,
367 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200368 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100369#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200370 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
372 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200373 0)
374
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200375/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
376 * supported key types and AEAD algorithms.
377 *
378 * If the size of the plaintext buffer is at least this large, it is guaranteed
379 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
380 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100381 * \note This macro returns a compile-time constant if its arguments are
382 * compile-time constants.
383 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100384 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
385 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200386 *
387 * \param ciphertext_length Size of the ciphertext in bytes.
388 *
389 * \return A sufficient output buffer size for any of the
390 * supported key types and AEAD algorithms.
391 *
392 */
393#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200395
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100396/** The default nonce size for an AEAD algorithm, in bytes.
397 *
398 * This macro can be used to allocate a buffer of sufficient size to
399 * store the nonce output from #psa_aead_generate_nonce().
400 *
401 * See also #PSA_AEAD_NONCE_MAX_SIZE.
402 *
403 * \note This is not the maximum size of nonce supported as input to
404 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
405 * just the default size that is generated by #psa_aead_generate_nonce().
406 *
407 * \warning This macro may evaluate its arguments multiple times or
408 * zero times, so you should not pass arguments that contain
409 * side effects.
410 *
411 * \param key_type A symmetric key type that is compatible with
412 * algorithm \p alg.
413 *
414 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
415 * #PSA_ALG_IS_AEAD(\p alg) is true).
416 *
417 * \return The default nonce size for the specified key type and algorithm.
418 * If the key type or AEAD algorithm is not recognized,
419 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100420 */
421#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100422 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
424 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
425 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100426 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100428 0)
429
430/** The maximum default nonce size among all supported pairs of key types and
431 * AEAD algorithms, in bytes.
432 *
433 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
434 * may return.
435 *
436 * \note This is not the maximum size of nonce supported as input to
437 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
438 * just the largest size that may be generated by
439 * #psa_aead_generate_nonce().
440 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100441#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100442
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200443/** A sufficient output buffer size for psa_aead_update().
444 *
445 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200446 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200447 * insufficient buffer size. The actual size of the output may be smaller
448 * in any given call.
449 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100450 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
451 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100452 * \warning This macro may evaluate its arguments multiple times or
453 * zero times, so you should not pass arguments that contain
454 * side effects.
455 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100456 * \param key_type A symmetric key type that is
457 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200458 * \param alg An AEAD algorithm
459 * (\c PSA_ALG_XXX value such that
460 * #PSA_ALG_IS_AEAD(\p alg) is true).
461 * \param input_length Size of the input in bytes.
462 *
463 * \return A sufficient output buffer size for the specified
464 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100465 * If the key type or AEAD algorithm is not
466 * recognized, or the parameters are incompatible,
467 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200468 */
469/* For all the AEAD modes defined in this specification, it is possible
470 * to emit output without delay. However, hardware may not always be
471 * capable of this. So for modes based on a block cipher, allow the
472 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100473#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200474 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
476 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
477 (input_length) : \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100478 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200479
480/** A sufficient output buffer size for psa_aead_update(), for any of the
481 * supported key types and AEAD algorithms.
482 *
483 * If the size of the output buffer is at least this large, it is guaranteed
484 * that psa_aead_update() will not fail due to an insufficient buffer size.
485 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100486 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200487 *
488 * \param input_length Size of the input in bytes.
489 */
490#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
491 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200492
493/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200494 *
495 * If the size of the ciphertext buffer is at least this large, it is
496 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200497 * insufficient ciphertext buffer size. The actual size of the output may
498 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200499 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100500 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
501 *
502 * \param key_type A symmetric key type that is
503 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200504 * \param alg An AEAD algorithm
505 * (\c PSA_ALG_XXX value such that
506 * #PSA_ALG_IS_AEAD(\p alg) is true).
507 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200508 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200509 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100510 * If the key type or AEAD algorithm is not
511 * recognized, or the parameters are incompatible,
512 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200513 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200514#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
515 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
517 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200518 0)
519
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200520/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
521 * supported key types and AEAD algorithms.
522 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100523 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200524 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200525#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200526
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200527/** A sufficient plaintext buffer size for psa_aead_verify().
528 *
529 * If the size of the plaintext buffer is at least this large, it is
530 * guaranteed that psa_aead_verify() will not fail due to an
531 * insufficient plaintext buffer size. The actual size of the output may
532 * be smaller in any given call.
533 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100534 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
535 *
536 * \param key_type A symmetric key type that is
537 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200538 * \param alg An AEAD algorithm
539 * (\c PSA_ALG_XXX value such that
540 * #PSA_ALG_IS_AEAD(\p alg) is true).
541 *
542 * \return A sufficient plaintext buffer size for the
543 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100544 * If the key type or AEAD algorithm is not
545 * recognized, or the parameters are incompatible,
546 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200547 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200548#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
549 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
551 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200552 0)
553
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200554/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
555 * supported key types and AEAD algorithms.
556 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100557 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200558 */
559#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
560
Jaeden Amero7f042142019-02-07 10:44:38 +0000561#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
562 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100563 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100564 11 /*PKCS#1v1.5*/)
565
566/**
567 * \brief ECDSA signature size for a given curve bit size
568 *
569 * \param curve_bits Curve size in bits.
570 * \return Signature size in bytes.
571 *
572 * \note This macro returns a compile-time constant if its argument is one.
573 */
574#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
575 (PSA_BITS_TO_BYTES(curve_bits) * 2)
576
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100577/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200578 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200579 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200580 * of the specified type and size, with the specified algorithm.
581 * Note that the actual size of the signature may be smaller
582 * (some algorithms produce a variable-size signature).
583 *
584 * \warning This function may call its arguments multiple times or
585 * zero times, so you should not pass arguments that contain
586 * side effects.
587 *
588 * \param key_type An asymmetric key type (this may indifferently be a
589 * key pair type or a public key type).
590 * \param key_bits The size of the key in bits.
591 * \param alg The signature algorithm.
592 *
593 * \return If the parameters are valid and supported, return
594 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100595 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200596 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100597 * If the parameters are a valid combination that is not supported,
598 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200599 * If the parameters are not valid, the
600 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200601 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100602#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200604 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 ((void) alg, 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200606
Gilles Peskine29755712019-11-08 15:49:40 +0100607#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
608 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
609
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100610/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100611 *
612 * Maximum size of an asymmetric signature.
613 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100614 * This macro expands to a compile-time constant integer. This value
615 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100616 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100617#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100618 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
619 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
620 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
621
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200622/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200623 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200624 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200625 * a key of the specified type and size, with the specified algorithm.
626 * Note that the actual size of the ciphertext may be smaller, depending
627 * on the algorithm.
628 *
629 * \warning This function may call its arguments multiple times or
630 * zero times, so you should not pass arguments that contain
631 * side effects.
632 *
633 * \param key_type An asymmetric key type (this may indifferently be a
634 * key pair type or a public key type).
635 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200636 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200637 *
638 * \return If the parameters are valid and supported, return
639 * a buffer size in bytes that guarantees that
640 * psa_asymmetric_encrypt() will not fail with
641 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100642 * If the parameters are a valid combination that is not supported,
643 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 * If the parameters are not valid, the
645 * return value is unspecified.
646 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200647#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
648 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200650 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200651
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200652/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
653 * supported asymmetric encryption.
654 *
655 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
656 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100657/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200658#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100659 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200660
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200661/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200662 *
Gilles Peskine76689602020-05-05 16:01:22 +0200663 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200664 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200665 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200666 * on the algorithm.
667 *
668 * \warning This function may call its arguments multiple times or
669 * zero times, so you should not pass arguments that contain
670 * side effects.
671 *
672 * \param key_type An asymmetric key type (this may indifferently be a
673 * key pair type or a public key type).
674 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200675 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200676 *
677 * \return If the parameters are valid and supported, return
678 * a buffer size in bytes that guarantees that
679 * psa_asymmetric_decrypt() will not fail with
680 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100681 * If the parameters are a valid combination that is not supported,
682 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200683 * If the parameters are not valid, the
684 * return value is unspecified.
685 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200686#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
687 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
688 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
689 0)
690
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200691/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
692 * supported asymmetric decryption.
693 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100694 * This macro assumes that RSA is the only supported asymmetric encryption.
695 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200696 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
697 */
698#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100699 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200700
Gilles Peskine1be949b2018-08-10 19:06:59 +0200701/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
702 * number of bits.
703 *
704 * This definition assumes that bits <= 2^19 - 9 so that the length field
705 * is at most 3 bytes. The length of the encoding is the length of the
706 * bit string padded to a whole number of bytes plus:
707 * - 1 type byte;
708 * - 1 to 3 length bytes;
709 * - 0 to 1 bytes of leading 0 due to the sign bit.
710 */
711#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
712 ((bits) / 8 + 5)
713
714/* Maximum size of the export encoding of an RSA public key.
715 * Assumes that the public exponent is less than 2^32.
716 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200717 * RSAPublicKey ::= SEQUENCE {
718 * modulus INTEGER, -- n
719 * publicExponent INTEGER } -- e
720 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000721 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200722 * - n : INTEGER;
723 * - 7 bytes for the public exponent.
724 */
725#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000726 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200727
728/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000729 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200730 * difference between the two primes is at most 1 bit.
731 *
732 * RSAPrivateKey ::= SEQUENCE {
733 * version Version, -- 0
734 * modulus INTEGER, -- N-bit
735 * publicExponent INTEGER, -- 32-bit
736 * privateExponent INTEGER, -- N-bit
737 * prime1 INTEGER, -- N/2-bit
738 * prime2 INTEGER, -- N/2-bit
739 * exponent1 INTEGER, -- N/2-bit
740 * exponent2 INTEGER, -- N/2-bit
741 * coefficient INTEGER, -- N/2-bit
742 * }
743 *
744 * - 4 bytes of SEQUENCE overhead;
745 * - 3 bytes of version;
746 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
747 * overapproximated as 9 half-size INTEGERS;
748 * - 7 bytes for the public exponent.
749 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200750#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200751 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
752
753/* Maximum size of the export encoding of a DSA public key.
754 *
755 * SubjectPublicKeyInfo ::= SEQUENCE {
756 * algorithm AlgorithmIdentifier,
757 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
758 * AlgorithmIdentifier ::= SEQUENCE {
759 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400760 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200761 * DSAPublicKey ::= INTEGER -- public key, Y
762 *
763 * - 3 * 4 bytes of SEQUENCE overhead;
764 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
765 * - 4 bytes of BIT STRING overhead;
766 * - 3 full-size INTEGERs (p, g, y);
767 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
768 */
769#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
770 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
771
772/* Maximum size of the export encoding of a DSA key pair.
773 *
774 * DSAPrivateKey ::= SEQUENCE {
775 * version Version, -- 0
776 * prime INTEGER, -- p
777 * subprime INTEGER, -- q
778 * generator INTEGER, -- g
779 * public INTEGER, -- y
780 * private INTEGER, -- x
781 * }
782 *
783 * - 4 bytes of SEQUENCE overhead;
784 * - 3 bytes of version;
785 * - 3 full-size INTEGERs (p, g, y);
786 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
787 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200788#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200789 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
790
791/* Maximum size of the export encoding of an ECC public key.
792 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000793 * The representation of an ECC public key is:
794 * - The byte 0x04;
795 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
796 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
797 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200798 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000799 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200800 */
801#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000802 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803
804/* Maximum size of the export encoding of an ECC key pair.
805 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100806 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200807 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200808#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100809 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200810
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100811/** Sufficient output buffer size for psa_export_key() or
812 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200813 *
814 * This macro returns a compile-time constant if its arguments are
815 * compile-time constants.
816 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100817 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200818 * zero times, so you should not pass arguments that contain
819 * side effects.
820 *
821 * The following code illustrates how to allocate enough memory to export
822 * a key by querying the key type and size at runtime.
823 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200824 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200825 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200826 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200827 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200828 * psa_key_type_t key_type = psa_get_key_type(&attributes);
829 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100830 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200831 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200832 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200833 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200834 * size_t buffer_length;
835 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
836 * if (status != PSA_SUCCESS) handle_error(...);
837 * \endcode
838 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200839 * \param key_type A supported key type.
840 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200841 *
842 * \return If the parameters are valid and supported, return
843 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100844 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200845 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100846 * If the parameters are a valid combination that is not supported,
847 * return either a sensible size or 0.
848 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200849 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100850#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
851 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
852 (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 +0200853 (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 +0100854 (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 +0200855 (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 +0100856 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
857 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 +0200858 0)
859
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200860/** Sufficient output buffer size for psa_export_public_key().
861 *
862 * This macro returns a compile-time constant if its arguments are
863 * compile-time constants.
864 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100865 * \warning This macro may evaluate its arguments multiple times or
866 * zero times, so you should not pass arguments that contain
867 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200868 *
869 * The following code illustrates how to allocate enough memory to export
870 * a public key by querying the key type and size at runtime.
871 * \code{c}
872 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
873 * psa_status_t status;
874 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100875 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200876 * psa_key_type_t key_type = psa_get_key_type(&attributes);
877 * size_t key_bits = psa_get_key_bits(&attributes);
878 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
879 * psa_reset_key_attributes(&attributes);
880 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100881 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200882 * size_t buffer_length;
883 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100884 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200885 * \endcode
886 *
887 * \param key_type A public key or key pair key type.
888 * \param key_bits The size of the key in bits.
889 *
890 * \return If the parameters are valid and supported, return
891 * a buffer size in bytes that guarantees that
892 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100893 * #PSA_ERROR_BUFFER_TOO_SMALL.
894 * If the parameters are a valid combination that is not
895 * supported, return either a sensible size or 0.
896 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200897 * the return value is unspecified.
898 *
899 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100900 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200901 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
902 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
903 * \p key_bits).
904 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100905#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
906 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
907 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 +0200908 0)
909
910/** Sufficient buffer size for exporting any asymmetric key pair.
911 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100912 * This macro expands to a compile-time constant integer. This value is
913 * a sufficient buffer size when calling psa_export_key() to export any
914 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200915 *
916 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
917 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100918#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
919 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
920 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
921 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
922 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200923
924/** Sufficient buffer size for exporting any asymmetric public key.
925 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100926 * This macro expands to a compile-time constant integer. This value is
927 * a sufficient buffer size when calling psa_export_key() or
928 * psa_export_public_key() to export any asymmetric public key,
929 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200930 *
931 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
932 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100933#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
934 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
935 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
936 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
937 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200938
939/** Sufficient output buffer size for psa_raw_key_agreement().
940 *
941 * This macro returns a compile-time constant if its arguments are
942 * compile-time constants.
943 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100944 * \warning This macro may evaluate its arguments multiple times or
945 * zero times, so you should not pass arguments that contain
946 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200947 *
948 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
949 *
950 * \param key_type A supported key type.
951 * \param key_bits The size of the key in bits.
952 *
953 * \return If the parameters are valid and supported, return
954 * a buffer size in bytes that guarantees that
955 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100956 * #PSA_ERROR_BUFFER_TOO_SMALL.
957 * If the parameters are a valid combination that
958 * is not supported, return either a sensible size or 0.
959 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200960 * the return value is unspecified.
961 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100962/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200963#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
964 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100965 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200966 0)
967
968/** Maximum size of the output from psa_raw_key_agreement().
969 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100970 * This macro expands to a compile-time constant integer. This value is the
971 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200972 *
973 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
974 */
975#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100976 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200977
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100978/** The default IV size for a cipher algorithm, in bytes.
979 *
980 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
981 * the default IV length for the algorithm.
982 *
983 * This macro can be used to allocate a buffer of sufficient size to
984 * store the IV output from #psa_cipher_generate_iv() when using
985 * a multi-part cipher operation.
986 *
987 * See also #PSA_CIPHER_IV_MAX_SIZE.
988 *
989 * \warning This macro may evaluate its arguments multiple times or
990 * zero times, so you should not pass arguments that contain
991 * side effects.
992 *
993 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
994 *
995 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
996 *
997 * \return The default IV size for the specified key type and algorithm.
998 * If the algorithm does not use an IV, return 0.
999 * If the key type or cipher algorithm is not recognized,
1000 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001001 */
1002#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001003 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 ((alg) == PSA_ALG_CTR || \
1005 (alg) == PSA_ALG_CFB || \
1006 (alg) == PSA_ALG_OFB || \
1007 (alg) == PSA_ALG_XTS || \
1008 (alg) == PSA_ALG_CBC_NO_PADDING || \
1009 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001010 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
1012 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
1013 0)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001014
1015/** The maximum IV size for all supported cipher algorithms, in bytes.
1016 *
1017 * See also #PSA_CIPHER_IV_LENGTH().
1018 */
1019#define PSA_CIPHER_IV_MAX_SIZE 16
1020
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001021/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1022 *
1023 * If the size of the output buffer is at least this large, it is guaranteed
1024 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1025 * Depending on the algorithm, the actual size of the output might be smaller.
1026 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001027 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1028 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001029 * \warning This macro may evaluate its arguments multiple times or
1030 * zero times, so you should not pass arguments that contain
1031 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001032 *
1033 * \param key_type A symmetric key type that is compatible with algorithm
1034 * alg.
1035 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1036 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1037 * \param input_length Size of the input in bytes.
1038 *
1039 * \return A sufficient output size for the specified key type and
1040 * algorithm. If the key type or cipher algorithm is not
1041 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001042 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001043 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001044#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1045 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliottc22950c2021-07-08 16:50:01 +01001046 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1048 (input_length) + 1) + \
1049 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001050 (PSA_ALG_IS_CIPHER(alg) ? \
1051 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001053
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001054/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1055 * supported key types and cipher algorithms.
1056 *
1057 * If the size of the output buffer is at least this large, it is guaranteed
1058 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1059 *
1060 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1061 *
1062 * \param input_length Size of the input in bytes.
1063 *
1064 */
1065#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1066 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001067 (input_length) + 1) + \
1068 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001069
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001070/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1071 *
1072 * If the size of the output buffer is at least this large, it is guaranteed
1073 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1074 * Depending on the algorithm, the actual size of the output might be smaller.
1075 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001076 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001077 *
1078 * \param key_type A symmetric key type that is compatible with algorithm
1079 * alg.
1080 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1081 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1082 * \param input_length Size of the input in bytes.
1083 *
1084 * \return A sufficient output size for the specified key type and
1085 * algorithm. If the key type or cipher algorithm is not
1086 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001087 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001088 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001089#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1090 (PSA_ALG_IS_CIPHER(alg) && \
1091 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1092 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001093 0)
1094
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001095/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1096 * supported key types and cipher algorithms.
1097 *
1098 * If the size of the output buffer is at least this large, it is guaranteed
1099 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1100 *
1101 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1102 *
1103 * \param input_length Size of the input in bytes.
1104 */
1105#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1106 (input_length)
1107
1108/** A sufficient output buffer size for psa_cipher_update().
1109 *
1110 * If the size of the output buffer is at least this large, it is guaranteed
1111 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1112 * The actual size of the output might be smaller in any given call.
1113 *
1114 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1115 *
1116 * \param key_type A symmetric key type that is compatible with algorithm
1117 * alg.
1118 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1119 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1120 * \param input_length Size of the input in bytes.
1121 *
1122 * \return A sufficient output size for the specified key type and
1123 * algorithm. If the key type or cipher algorithm is not
1124 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001125 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001126#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1127 (PSA_ALG_IS_CIPHER(alg) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1129 (((alg) == PSA_ALG_CBC_PKCS7 || \
1130 (alg) == PSA_ALG_CBC_NO_PADDING || \
1131 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1132 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001133 input_length) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001135 0)
1136
1137/** A sufficient output buffer size for psa_cipher_update(), for any of the
1138 * supported key types and cipher algorithms.
1139 *
1140 * If the size of the output buffer is at least this large, it is guaranteed
1141 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1142 *
1143 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1144 *
1145 * \param input_length Size of the input in bytes.
1146 */
1147#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001148 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001149
1150/** A sufficient ciphertext buffer size for psa_cipher_finish().
1151 *
1152 * If the size of the ciphertext buffer is at least this large, it is
1153 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1154 * ciphertext buffer size. The actual size of the output might be smaller in
1155 * any given call.
1156 *
1157 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1158 *
1159 * \param key_type A symmetric key type that is compatible with algorithm
1160 * alg.
1161 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1162 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1163 * \return A sufficient output size for the specified key type and
1164 * algorithm. If the key type or cipher algorithm is not
1165 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001166 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001167#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1168 (PSA_ALG_IS_CIPHER(alg) ? \
1169 (alg == PSA_ALG_CBC_PKCS7 ? \
1170 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1171 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001172 0)
1173
1174/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1175 * supported key types and cipher algorithms.
1176 *
1177 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1178 */
1179#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1180 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1181
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001182#endif /* PSA_CRYPTO_SIZES_H */