blob: 37f72054f79fd2e55546f278d4508b16dd01e9af [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
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100256/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100257#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200258
Gilles Peskineacd4be32018-07-08 19:56:25 +0200259/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200260 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200261 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200262 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100263 * \warning This macro may evaluate its arguments multiple times or
264 * zero times, so you should not pass arguments that contain
265 * side effects.
266 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200267 * \param key_type The type of the MAC key.
268 * \param key_bits The size of the MAC key in bits.
269 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100270 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200271 *
272 * \return The MAC size for the specified algorithm with
273 * the specified key parameters.
274 * \return 0 if the MAC algorithm is not recognized.
275 * \return Either 0 or the correct size for a MAC algorithm that
276 * the implementation recognizes, but does not support.
277 * \return Unspecified if the key parameters are not consistent
278 * with the algorithm.
279 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100280#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
281 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
282 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
283 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 ((void) (key_type), (void) (key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200285
286/** The maximum size of the output of psa_aead_encrypt(), in bytes.
287 *
288 * If the size of the ciphertext buffer is at least this large, it is
289 * guaranteed that psa_aead_encrypt() will not fail due to an
290 * insufficient buffer size. Depending on the algorithm, the actual size of
291 * the ciphertext may be smaller.
292 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100293 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
294 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100295 * \warning This macro may evaluate its arguments multiple times or
296 * zero times, so you should not pass arguments that contain
297 * side effects.
298 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100299 * \param key_type A symmetric key type that is
300 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200301 * \param alg An AEAD algorithm
302 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100303 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200304 * \param plaintext_length Size of the plaintext in bytes.
305 *
306 * \return The AEAD ciphertext size for the specified
307 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100308 * If the key type or AEAD algorithm is not
309 * recognized, or the parameters are incompatible,
310 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200311 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100312#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200313 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200314 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200315 0)
316
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200317/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
318 * supported key types and AEAD algorithms.
319 *
320 * If the size of the ciphertext buffer is at least this large, it is guaranteed
321 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
322 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100323 * \note This macro returns a compile-time constant if its arguments are
324 * compile-time constants.
325 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100326 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
327 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200328 *
329 * \param plaintext_length Size of the plaintext in bytes.
330 *
331 * \return A sufficient output buffer size for any of the
332 * supported key types and AEAD algorithms.
333 *
334 */
335#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
336 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
337
338
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200339/** The maximum size of the output of psa_aead_decrypt(), in bytes.
340 *
341 * If the size of the plaintext buffer is at least this large, it is
342 * guaranteed that psa_aead_decrypt() will not fail due to an
343 * insufficient buffer size. Depending on the algorithm, the actual size of
344 * the plaintext may be smaller.
345 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100346 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
347 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100348 * \warning This macro may evaluate its arguments multiple times or
349 * zero times, so you should not pass arguments that contain
350 * side effects.
351 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100352 * \param key_type A symmetric key type that is
353 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200354 * \param alg An AEAD algorithm
355 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100356 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200357 * \param ciphertext_length Size of the plaintext in bytes.
358 *
359 * \return The AEAD ciphertext size for the specified
360 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100361 * If the key type or AEAD algorithm is not
362 * recognized, or the parameters are incompatible,
363 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200364 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100365#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200366 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
368 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200369 0)
370
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200371/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
372 * supported key types and AEAD algorithms.
373 *
374 * If the size of the plaintext buffer is at least this large, it is guaranteed
375 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
376 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100377 * \note This macro returns a compile-time constant if its arguments are
378 * compile-time constants.
379 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100380 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
381 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200382 *
383 * \param ciphertext_length Size of the ciphertext in bytes.
384 *
385 * \return A sufficient output buffer size for any of the
386 * supported key types and AEAD algorithms.
387 *
388 */
389#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200391
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100392/** The default nonce size for an AEAD algorithm, in bytes.
393 *
394 * This macro can be used to allocate a buffer of sufficient size to
395 * store the nonce output from #psa_aead_generate_nonce().
396 *
397 * See also #PSA_AEAD_NONCE_MAX_SIZE.
398 *
399 * \note This is not the maximum size of nonce supported as input to
400 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
401 * just the default size that is generated by #psa_aead_generate_nonce().
402 *
403 * \warning This macro may evaluate its arguments multiple times or
404 * zero times, so you should not pass arguments that contain
405 * side effects.
406 *
407 * \param key_type A symmetric key type that is compatible with
408 * algorithm \p alg.
409 *
410 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
411 * #PSA_ALG_IS_AEAD(\p alg) is true).
412 *
413 * \return The default nonce size for the specified key type and algorithm.
414 * If the key type or AEAD algorithm is not recognized,
415 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100416 */
417#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100418 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
420 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
421 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100422 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100424 0)
425
426/** The maximum default nonce size among all supported pairs of key types and
427 * AEAD algorithms, in bytes.
428 *
429 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
430 * may return.
431 *
432 * \note This is not the maximum size of nonce supported as input to
433 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
434 * just the largest size that may be generated by
435 * #psa_aead_generate_nonce().
436 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100437#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100438
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200439/** A sufficient output buffer size for psa_aead_update().
440 *
441 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200442 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200443 * insufficient buffer size. The actual size of the output may be smaller
444 * in any given call.
445 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100446 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
447 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100448 * \warning This macro may evaluate its arguments multiple times or
449 * zero times, so you should not pass arguments that contain
450 * side effects.
451 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100452 * \param key_type A symmetric key type that is
453 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200454 * \param alg An AEAD algorithm
455 * (\c PSA_ALG_XXX value such that
456 * #PSA_ALG_IS_AEAD(\p alg) is true).
457 * \param input_length Size of the input in bytes.
458 *
459 * \return A sufficient output buffer size for the specified
460 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100461 * If the key type or AEAD algorithm is not
462 * recognized, or the parameters are incompatible,
463 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200464 */
465/* For all the AEAD modes defined in this specification, it is possible
466 * to emit output without delay. However, hardware may not always be
467 * capable of this. So for modes based on a block cipher, allow the
468 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100469#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200470 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
472 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
473 (input_length) : \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100474 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200475
476/** A sufficient output buffer size for psa_aead_update(), for any of the
477 * supported key types and AEAD algorithms.
478 *
479 * If the size of the output buffer is at least this large, it is guaranteed
480 * that psa_aead_update() will not fail due to an insufficient buffer size.
481 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100482 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200483 *
484 * \param input_length Size of the input in bytes.
485 */
486#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
487 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200488
489/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200490 *
491 * If the size of the ciphertext buffer is at least this large, it is
492 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200493 * insufficient ciphertext buffer size. The actual size of the output may
494 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200495 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100496 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
497 *
498 * \param key_type A symmetric key type that is
499 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200500 * \param alg An AEAD algorithm
501 * (\c PSA_ALG_XXX value such that
502 * #PSA_ALG_IS_AEAD(\p alg) is true).
503 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200504 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200505 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100506 * If the key type or AEAD algorithm is not
507 * recognized, or the parameters are incompatible,
508 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200509 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200510#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
511 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
513 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200514 0)
515
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200516/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
517 * supported key types and AEAD algorithms.
518 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100519 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200520 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200521#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200522
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200523/** A sufficient plaintext buffer size for psa_aead_verify().
524 *
525 * If the size of the plaintext buffer is at least this large, it is
526 * guaranteed that psa_aead_verify() will not fail due to an
527 * insufficient plaintext buffer size. The actual size of the output may
528 * be smaller in any given call.
529 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100530 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
531 *
532 * \param key_type A symmetric key type that is
533 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200534 * \param alg An AEAD algorithm
535 * (\c PSA_ALG_XXX value such that
536 * #PSA_ALG_IS_AEAD(\p alg) is true).
537 *
538 * \return A sufficient plaintext buffer size for the
539 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100540 * If the key type or AEAD algorithm is not
541 * recognized, or the parameters are incompatible,
542 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200543 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200544#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
545 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
547 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200548 0)
549
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200550/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
551 * supported key types and AEAD algorithms.
552 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100553 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200554 */
555#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
556
Jaeden Amero7f042142019-02-07 10:44:38 +0000557#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
558 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100559 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100560 11 /*PKCS#1v1.5*/)
561
562/**
563 * \brief ECDSA signature size for a given curve bit size
564 *
565 * \param curve_bits Curve size in bits.
566 * \return Signature size in bytes.
567 *
568 * \note This macro returns a compile-time constant if its argument is one.
569 */
570#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
571 (PSA_BITS_TO_BYTES(curve_bits) * 2)
572
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100573/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200574 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200575 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200576 * of the specified type and size, with the specified algorithm.
577 * Note that the actual size of the signature may be smaller
578 * (some algorithms produce a variable-size signature).
579 *
580 * \warning This function may call its arguments multiple times or
581 * zero times, so you should not pass arguments that contain
582 * side effects.
583 *
584 * \param key_type An asymmetric key type (this may indifferently be a
585 * key pair type or a public key type).
586 * \param key_bits The size of the key in bits.
587 * \param alg The signature algorithm.
588 *
589 * \return If the parameters are valid and supported, return
590 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100591 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200592 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100593 * If the parameters are a valid combination that is not supported,
594 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200595 * If the parameters are not valid, the
596 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200597 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100598#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200600 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 ((void) alg, 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200602
Gilles Peskine29755712019-11-08 15:49:40 +0100603#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
604 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
605
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100606/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100607 *
608 * Maximum size of an asymmetric signature.
609 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100610 * This macro expands to a compile-time constant integer. This value
611 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100612 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100613#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100614 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
615 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
616 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
617
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200618/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200619 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200620 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200621 * a key of the specified type and size, with the specified algorithm.
622 * Note that the actual size of the ciphertext may be smaller, depending
623 * on the algorithm.
624 *
625 * \warning This function may call its arguments multiple times or
626 * zero times, so you should not pass arguments that contain
627 * side effects.
628 *
629 * \param key_type An asymmetric key type (this may indifferently be a
630 * key pair type or a public key type).
631 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200632 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200633 *
634 * \return If the parameters are valid and supported, return
635 * a buffer size in bytes that guarantees that
636 * psa_asymmetric_encrypt() will not fail with
637 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100638 * If the parameters are a valid combination that is not supported,
639 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200640 * If the parameters are not valid, the
641 * return value is unspecified.
642 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200643#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
644 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200646 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200647
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200648/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
649 * supported asymmetric encryption.
650 *
651 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
652 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100653/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200654#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100655 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200656
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200657/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200658 *
Gilles Peskine76689602020-05-05 16:01:22 +0200659 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200660 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200661 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200662 * on the algorithm.
663 *
664 * \warning This function may call its arguments multiple times or
665 * zero times, so you should not pass arguments that contain
666 * side effects.
667 *
668 * \param key_type An asymmetric key type (this may indifferently be a
669 * key pair type or a public key type).
670 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200671 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200672 *
673 * \return If the parameters are valid and supported, return
674 * a buffer size in bytes that guarantees that
675 * psa_asymmetric_decrypt() will not fail with
676 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100677 * If the parameters are a valid combination that is not supported,
678 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200679 * If the parameters are not valid, the
680 * return value is unspecified.
681 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200682#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
683 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
684 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
685 0)
686
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200687/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
688 * supported asymmetric decryption.
689 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100690 * This macro assumes that RSA is the only supported asymmetric encryption.
691 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200692 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
693 */
694#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100695 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200696
Gilles Peskine1be949b2018-08-10 19:06:59 +0200697/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
698 * number of bits.
699 *
700 * This definition assumes that bits <= 2^19 - 9 so that the length field
701 * is at most 3 bytes. The length of the encoding is the length of the
702 * bit string padded to a whole number of bytes plus:
703 * - 1 type byte;
704 * - 1 to 3 length bytes;
705 * - 0 to 1 bytes of leading 0 due to the sign bit.
706 */
707#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
708 ((bits) / 8 + 5)
709
710/* Maximum size of the export encoding of an RSA public key.
711 * Assumes that the public exponent is less than 2^32.
712 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200713 * RSAPublicKey ::= SEQUENCE {
714 * modulus INTEGER, -- n
715 * publicExponent INTEGER } -- e
716 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000717 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200718 * - n : INTEGER;
719 * - 7 bytes for the public exponent.
720 */
721#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000722 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723
724/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000725 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200726 * difference between the two primes is at most 1 bit.
727 *
728 * RSAPrivateKey ::= SEQUENCE {
729 * version Version, -- 0
730 * modulus INTEGER, -- N-bit
731 * publicExponent INTEGER, -- 32-bit
732 * privateExponent INTEGER, -- N-bit
733 * prime1 INTEGER, -- N/2-bit
734 * prime2 INTEGER, -- N/2-bit
735 * exponent1 INTEGER, -- N/2-bit
736 * exponent2 INTEGER, -- N/2-bit
737 * coefficient INTEGER, -- N/2-bit
738 * }
739 *
740 * - 4 bytes of SEQUENCE overhead;
741 * - 3 bytes of version;
742 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
743 * overapproximated as 9 half-size INTEGERS;
744 * - 7 bytes for the public exponent.
745 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200746#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200747 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
748
749/* Maximum size of the export encoding of a DSA public key.
750 *
751 * SubjectPublicKeyInfo ::= SEQUENCE {
752 * algorithm AlgorithmIdentifier,
753 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
754 * AlgorithmIdentifier ::= SEQUENCE {
755 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400756 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200757 * DSAPublicKey ::= INTEGER -- public key, Y
758 *
759 * - 3 * 4 bytes of SEQUENCE overhead;
760 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
761 * - 4 bytes of BIT STRING overhead;
762 * - 3 full-size INTEGERs (p, g, y);
763 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
764 */
765#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
766 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
767
768/* Maximum size of the export encoding of a DSA key pair.
769 *
770 * DSAPrivateKey ::= SEQUENCE {
771 * version Version, -- 0
772 * prime INTEGER, -- p
773 * subprime INTEGER, -- q
774 * generator INTEGER, -- g
775 * public INTEGER, -- y
776 * private INTEGER, -- x
777 * }
778 *
779 * - 4 bytes of SEQUENCE overhead;
780 * - 3 bytes of version;
781 * - 3 full-size INTEGERs (p, g, y);
782 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
783 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200784#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200785 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
786
787/* Maximum size of the export encoding of an ECC public key.
788 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000789 * The representation of an ECC public key is:
790 * - The byte 0x04;
791 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
792 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
793 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200794 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000795 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200796 */
797#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000798 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200799
800/* Maximum size of the export encoding of an ECC key pair.
801 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100802 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200804#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100805 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200806
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100807/** Sufficient output buffer size for psa_export_key() or
808 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809 *
810 * This macro returns a compile-time constant if its arguments are
811 * compile-time constants.
812 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100813 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200814 * zero times, so you should not pass arguments that contain
815 * side effects.
816 *
817 * The following code illustrates how to allocate enough memory to export
818 * a key by querying the key type and size at runtime.
819 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200820 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200821 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200822 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200823 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200824 * psa_key_type_t key_type = psa_get_key_type(&attributes);
825 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100826 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200827 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200828 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200829 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200830 * size_t buffer_length;
831 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
832 * if (status != PSA_SUCCESS) handle_error(...);
833 * \endcode
834 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200835 * \param key_type A supported key type.
836 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200837 *
838 * \return If the parameters are valid and supported, return
839 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100840 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200841 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100842 * If the parameters are a valid combination that is not supported,
843 * return either a sensible size or 0.
844 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200845 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100846#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
847 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
848 (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 +0200849 (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 +0100850 (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 +0200851 (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 +0100852 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
853 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 +0200854 0)
855
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200856/** Sufficient output buffer size for psa_export_public_key().
857 *
858 * This macro returns a compile-time constant if its arguments are
859 * compile-time constants.
860 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100861 * \warning This macro may evaluate its arguments multiple times or
862 * zero times, so you should not pass arguments that contain
863 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200864 *
865 * The following code illustrates how to allocate enough memory to export
866 * a public key by querying the key type and size at runtime.
867 * \code{c}
868 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
869 * psa_status_t status;
870 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100871 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200872 * psa_key_type_t key_type = psa_get_key_type(&attributes);
873 * size_t key_bits = psa_get_key_bits(&attributes);
874 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
875 * psa_reset_key_attributes(&attributes);
876 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100877 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200878 * size_t buffer_length;
879 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100880 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200881 * \endcode
882 *
883 * \param key_type A public key or key pair key type.
884 * \param key_bits The size of the key in bits.
885 *
886 * \return If the parameters are valid and supported, return
887 * a buffer size in bytes that guarantees that
888 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100889 * #PSA_ERROR_BUFFER_TOO_SMALL.
890 * If the parameters are a valid combination that is not
891 * supported, return either a sensible size or 0.
892 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200893 * the return value is unspecified.
894 *
895 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100896 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200897 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
898 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
899 * \p key_bits).
900 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100901#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
902 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
903 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 +0200904 0)
905
906/** Sufficient buffer size for exporting any asymmetric key pair.
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() to export any
910 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200911 *
912 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
913 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100914#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
915 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
916 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
917 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
918 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200919
920/** Sufficient buffer size for exporting any asymmetric public key.
921 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100922 * This macro expands to a compile-time constant integer. This value is
923 * a sufficient buffer size when calling psa_export_key() or
924 * psa_export_public_key() to export any asymmetric public key,
925 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200926 *
927 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
928 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100929#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
930 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
931 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
932 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
933 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200934
935/** Sufficient output buffer size for psa_raw_key_agreement().
936 *
937 * This macro returns a compile-time constant if its arguments are
938 * compile-time constants.
939 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100940 * \warning This macro may evaluate its arguments multiple times or
941 * zero times, so you should not pass arguments that contain
942 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200943 *
944 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
945 *
946 * \param key_type A supported key type.
947 * \param key_bits The size of the key in bits.
948 *
949 * \return If the parameters are valid and supported, return
950 * a buffer size in bytes that guarantees that
951 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100952 * #PSA_ERROR_BUFFER_TOO_SMALL.
953 * If the parameters are a valid combination that
954 * is not supported, return either a sensible size or 0.
955 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200956 * the return value is unspecified.
957 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100958/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200959#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
960 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100961 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200962 0)
963
964/** Maximum size of the output from psa_raw_key_agreement().
965 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100966 * This macro expands to a compile-time constant integer. This value is the
967 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200968 *
969 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
970 */
971#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100972 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200973
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100974/** The default IV size for a cipher algorithm, in bytes.
975 *
976 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
977 * the default IV length for the algorithm.
978 *
979 * This macro can be used to allocate a buffer of sufficient size to
980 * store the IV output from #psa_cipher_generate_iv() when using
981 * a multi-part cipher operation.
982 *
983 * See also #PSA_CIPHER_IV_MAX_SIZE.
984 *
985 * \warning This macro may evaluate its arguments multiple times or
986 * zero times, so you should not pass arguments that contain
987 * side effects.
988 *
989 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
990 *
991 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
992 *
993 * \return The default IV size for the specified key type and algorithm.
994 * If the algorithm does not use an IV, return 0.
995 * If the key type or cipher algorithm is not recognized,
996 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100997 */
998#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100999 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 ((alg) == PSA_ALG_CTR || \
1001 (alg) == PSA_ALG_CFB || \
1002 (alg) == PSA_ALG_OFB || \
1003 (alg) == PSA_ALG_XTS || \
1004 (alg) == PSA_ALG_CBC_NO_PADDING || \
1005 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001006 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
1008 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
1009 0)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001010
1011/** The maximum IV size for all supported cipher algorithms, in bytes.
1012 *
1013 * See also #PSA_CIPHER_IV_LENGTH().
1014 */
1015#define PSA_CIPHER_IV_MAX_SIZE 16
1016
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001017/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1018 *
1019 * If the size of the output buffer is at least this large, it is guaranteed
1020 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1021 * Depending on the algorithm, the actual size of the output might be smaller.
1022 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001023 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1024 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001025 * \warning This macro may evaluate its arguments multiple times or
1026 * zero times, so you should not pass arguments that contain
1027 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001028 *
1029 * \param key_type A symmetric key type that is compatible with algorithm
1030 * alg.
1031 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1032 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1033 * \param input_length Size of the input in bytes.
1034 *
1035 * \return A sufficient output size for the specified key type and
1036 * algorithm. If the key type or cipher algorithm is not
1037 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001038 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001039 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001040#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1041 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliottc22950c2021-07-08 16:50:01 +01001042 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1044 (input_length) + 1) + \
1045 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001046 (PSA_ALG_IS_CIPHER(alg) ? \
1047 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001049
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001050/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1051 * supported key types and cipher algorithms.
1052 *
1053 * If the size of the output buffer is at least this large, it is guaranteed
1054 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1055 *
1056 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1057 *
1058 * \param input_length Size of the input in bytes.
1059 *
1060 */
1061#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1062 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001063 (input_length) + 1) + \
1064 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001065
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001066/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1067 *
1068 * If the size of the output buffer is at least this large, it is guaranteed
1069 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1070 * Depending on the algorithm, the actual size of the output might be smaller.
1071 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001072 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001073 *
1074 * \param key_type A symmetric key type that is compatible with algorithm
1075 * alg.
1076 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1077 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1078 * \param input_length Size of the input in bytes.
1079 *
1080 * \return A sufficient output size for the specified key type and
1081 * algorithm. If the key type or cipher algorithm is not
1082 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001083 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001084 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001085#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1086 (PSA_ALG_IS_CIPHER(alg) && \
1087 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1088 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001089 0)
1090
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001091/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1092 * supported key types and cipher algorithms.
1093 *
1094 * If the size of the output buffer is at least this large, it is guaranteed
1095 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1096 *
1097 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1098 *
1099 * \param input_length Size of the input in bytes.
1100 */
1101#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1102 (input_length)
1103
1104/** A sufficient output buffer size for psa_cipher_update().
1105 *
1106 * If the size of the output buffer is at least this large, it is guaranteed
1107 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1108 * The actual size of the output might be smaller in any given call.
1109 *
1110 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1111 *
1112 * \param key_type A symmetric key type that is compatible with algorithm
1113 * alg.
1114 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1115 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1116 * \param input_length Size of the input in bytes.
1117 *
1118 * \return A sufficient output size for the specified key type and
1119 * algorithm. If the key type or cipher algorithm is not
1120 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001121 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001122#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1123 (PSA_ALG_IS_CIPHER(alg) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1125 (((alg) == PSA_ALG_CBC_PKCS7 || \
1126 (alg) == PSA_ALG_CBC_NO_PADDING || \
1127 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1128 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001129 input_length) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001131 0)
1132
1133/** A sufficient output buffer size for psa_cipher_update(), for any of the
1134 * supported key types and cipher algorithms.
1135 *
1136 * If the size of the output buffer is at least this large, it is guaranteed
1137 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1138 *
1139 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1140 *
1141 * \param input_length Size of the input in bytes.
1142 */
1143#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001144 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001145
1146/** A sufficient ciphertext buffer size for psa_cipher_finish().
1147 *
1148 * If the size of the ciphertext buffer is at least this large, it is
1149 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1150 * ciphertext buffer size. The actual size of the output might be smaller in
1151 * any given call.
1152 *
1153 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1154 *
1155 * \param key_type A symmetric key type that is compatible with algorithm
1156 * alg.
1157 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1158 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1159 * \return A sufficient output size for the specified key type and
1160 * algorithm. If the key type or cipher algorithm is not
1161 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001162 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001163#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1164 (PSA_ALG_IS_CIPHER(alg) ? \
1165 (alg == PSA_ALG_CBC_PKCS7 ? \
1166 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1167 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001168 0)
1169
1170/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1171 * supported key types and cipher algorithms.
1172 *
1173 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1174 */
1175#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1176 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1177
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001178#endif /* PSA_CRYPTO_SIZES_H */