blob: 63bd6075345b2da5a43188d10746d29aa9c99db7 [file] [log] [blame]
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001/**
2 * \file psa/crypto_sizes.h
3 *
4 * \brief PSA cryptography module: Mbed TLS buffer size macros
5 *
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02009 * This file contains the definitions of macros that are useful to
10 * compute buffer sizes. The signatures and semantics of these macros
11 * are standardized, but the definitions are not, because they depend on
12 * the available algorithms and, in some cases, on permitted tolerances
13 * on buffer sizes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +020014 *
Gilles Peskine07c91f52018-06-28 18:02:53 +020015 * In implementations with isolation between the application and the
16 * cryptography module, implementers should take care to ensure that
17 * the definitions that are exposed to applications match what the
18 * module implements.
19 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +020020 * Macros that compute sizes whose values do not depend on the
21 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020022 */
23/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020024 * Copyright The Mbed TLS Contributors
Gilles Peskine0cad07c2018-06-27 19:49:02 +020025 * SPDX-License-Identifier: Apache-2.0
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020038 */
39
40#ifndef PSA_CRYPTO_SIZES_H
41#define PSA_CRYPTO_SIZES_H
42
43/* Include the Mbed TLS configuration file, the way Mbed TLS does it
44 * in each of its header files. */
Bence Szépkútic662b362021-05-27 11:25:03 +020045#include "mbedtls/build_info.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020046
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
48#define PSA_BYTES_TO_BITS(bytes) ((bytes)*8)
Gilles Peskinea7c26db2018-12-12 13:42:25 +010049
Gilles Peskine248010c2019-05-14 16:08:59 +020050#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051 (((length) + (block_size)-1) / (block_size) * (block_size))
Gilles Peskine248010c2019-05-14 16:08:59 +020052
Gilles Peskinea7c26db2018-12-12 13:42:25 +010053/** The size of the output of psa_hash_finish(), in bytes.
54 *
55 * This is also the hash size that psa_hash_verify() expects.
56 *
57 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
58 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
59 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
60 * hash algorithm).
61 *
62 * \return The hash size for the specified hash algorithm.
63 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010064 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065#define PSA_HASH_LENGTH(alg) \
66 (PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
67 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
68 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
69 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
70 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
71 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
79 0)
Gilles Peskinea7c26db2018-12-12 13:42:25 +010080
Gilles Peskineaf3baab2018-06-27 22:55:52 +020081/** \def PSA_HASH_MAX_SIZE
82 *
83 * Maximum size of a hash.
84 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +010085 * This macro expands to a compile-time constant integer. This value
86 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +020087 */
Gilles Peskine3052f532018-09-17 14:13:26 +020088/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
89 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
90 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +020091#if defined(MBEDTLS_SHA512_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092# define PSA_HASH_MAX_SIZE 64
93# define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
Gilles Peskine0cad07c2018-06-27 19:49:02 +020094#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020095# define PSA_HASH_MAX_SIZE 32
96# define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
Gilles Peskine0cad07c2018-06-27 19:49:02 +020097#endif
98
Gilles Peskineaf3baab2018-06-27 22:55:52 +020099/** \def PSA_MAC_MAX_SIZE
100 *
101 * Maximum size of a MAC.
102 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100103 * This macro expands to a compile-time constant integer. This value
104 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200105 */
106/* All non-HMAC MACs have a maximum size that's smaller than the
107 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200108/* Note that the encoding of truncated MAC algorithms limits this value
109 * to 64 bytes.
110 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200111#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
112
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100113/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100114 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100115 * This macro can be used to allocate a buffer of sufficient size to store the
116 * tag output from psa_aead_finish().
117 *
118 * See also #PSA_AEAD_TAG_MAX_SIZE.
119 *
120 * \param key_type The type of the AEAD key.
121 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100122 * \param alg An AEAD algorithm
123 * (\c PSA_ALG_XXX value such that
124 * #PSA_ALG_IS_AEAD(\p alg) is true).
125 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200126 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100127 * If the AEAD algorithm does not have an identified
128 * tag that can be distinguished from the rest of
129 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100130 * If the key type or AEAD algorithm is not
131 * recognized, or the parameters are incompatible,
132 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100133 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
135 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
136 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
137 ((void)(key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100138
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200139/** The maximum tag size for all supported AEAD algorithms, in bytes.
140 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100141 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200142 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200144
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200145/* The maximum size of an RSA key on this implementation, in bits.
146 * This is a vendor-specific macro.
147 *
148 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
149 * whose parameters fit in a bignum is accepted. However large keys can
150 * induce a large memory usage and long computation times. Unlike other
151 * auxiliary macros in this file and in crypto.h, which reflect how the
152 * library is configured, this macro defines how the library is
153 * configured. This implementation refuses to import or generate an
154 * RSA key whose size is larger than the value defined here.
155 *
156 * Note that an implementation may set different size limits for different
157 * operations, and does not need to accept all key sizes up to the limit. */
158#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
159
160/* The maximum size of an ECC key on this implementation, in bits.
161 * This is a vendor-specific macro. */
162#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163# define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200164#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165# define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200166#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200167# define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200168#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169# define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200170#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171# define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200172#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173# define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200174#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175# define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200176#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177# define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200178#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179# define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200180#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181# define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200182#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200183# define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200184#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185# define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200186#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200187# define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200188#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200189# define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200190#endif
191
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100192/** This macro returns the maximum supported length of the PSK for the
193 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100194 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100195 *
196 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100197 *
198 * Quoting RFC 4279, Sect 5.3:
199 * TLS implementations supporting these ciphersuites MUST support
200 * arbitrary PSK identities up to 128 octets in length, and arbitrary
201 * PSKs up to 64 octets in length. Supporting longer identities and
202 * keys is RECOMMENDED.
203 *
204 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100205 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100206 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100207#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100208
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100209/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100210#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200211
Gilles Peskineacd4be32018-07-08 19:56:25 +0200212/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200213 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200214 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200215 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100216 * \warning This macro may evaluate its arguments multiple times or
217 * zero times, so you should not pass arguments that contain
218 * side effects.
219 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200220 * \param key_type The type of the MAC key.
221 * \param key_bits The size of the MAC key in bits.
222 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100223 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200224 *
225 * \return The MAC size for the specified algorithm with
226 * the specified key parameters.
227 * \return 0 if the MAC algorithm is not recognized.
228 * \return Either 0 or the correct size for a MAC algorithm that
229 * the implementation recognizes, but does not support.
230 * \return Unspecified if the key parameters are not consistent
231 * with the algorithm.
232 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200233#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
234 ((alg)&PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
235 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
236 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? \
237 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
238 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200239
240/** The maximum size of the output of psa_aead_encrypt(), in bytes.
241 *
242 * If the size of the ciphertext buffer is at least this large, it is
243 * guaranteed that psa_aead_encrypt() will not fail due to an
244 * insufficient buffer size. Depending on the algorithm, the actual size of
245 * the ciphertext may be smaller.
246 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100247 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
248 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100249 * \warning This macro may evaluate its arguments multiple times or
250 * zero times, so you should not pass arguments that contain
251 * side effects.
252 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100253 * \param key_type A symmetric key type that is
254 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200255 * \param alg An AEAD algorithm
256 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100257 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200258 * \param plaintext_length Size of the plaintext in bytes.
259 *
260 * \return The AEAD ciphertext size for the specified
261 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100262 * If the key type or AEAD algorithm is not
263 * recognized, or the parameters are incompatible,
264 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200265 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100266#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200267 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200268 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
269 0)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200270
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200271/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
272 * supported key types and AEAD algorithms.
273 *
274 * If the size of the ciphertext buffer is at least this large, it is guaranteed
275 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
276 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100277 * \note This macro returns a compile-time constant if its arguments are
278 * compile-time constants.
279 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100280 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
281 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200282 *
283 * \param plaintext_length Size of the plaintext in bytes.
284 *
285 * \return A sufficient output buffer size for any of the
286 * supported key types and AEAD algorithms.
287 *
288 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200289#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200290 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
291
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200292/** The maximum size of the output of psa_aead_decrypt(), in bytes.
293 *
294 * If the size of the plaintext buffer is at least this large, it is
295 * guaranteed that psa_aead_decrypt() will not fail due to an
296 * insufficient buffer size. Depending on the algorithm, the actual size of
297 * the plaintext may be smaller.
298 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100299 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
300 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100301 * \warning This macro may evaluate its arguments multiple times or
302 * zero times, so you should not pass arguments that contain
303 * side effects.
304 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100305 * \param key_type A symmetric key type that is
306 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200307 * \param alg An AEAD algorithm
308 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100309 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200310 * \param ciphertext_length Size of the plaintext in bytes.
311 *
312 * \return The AEAD ciphertext size for the specified
313 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100314 * If the key type or AEAD algorithm is not
315 * recognized, or the parameters are incompatible,
316 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200317 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100318#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200319 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200320 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
321 (ciphertext_length)-PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
322 0)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200323
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200324/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
325 * supported key types and AEAD algorithms.
326 *
327 * If the size of the plaintext buffer is at least this large, it is guaranteed
328 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
329 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100330 * \note This macro returns a compile-time constant if its arguments are
331 * compile-time constants.
332 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100333 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
334 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200335 *
336 * \param ciphertext_length Size of the ciphertext in bytes.
337 *
338 * \return A sufficient output buffer size for any of the
339 * supported key types and AEAD algorithms.
340 *
341 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200343
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100344/** The default nonce size for an AEAD algorithm, in bytes.
345 *
346 * This macro can be used to allocate a buffer of sufficient size to
347 * store the nonce output from #psa_aead_generate_nonce().
348 *
349 * See also #PSA_AEAD_NONCE_MAX_SIZE.
350 *
351 * \note This is not the maximum size of nonce supported as input to
352 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
353 * just the default size that is generated by #psa_aead_generate_nonce().
354 *
355 * \warning This macro may evaluate its arguments multiple times or
356 * zero times, so you should not pass arguments that contain
357 * side effects.
358 *
359 * \param key_type A symmetric key type that is compatible with
360 * algorithm \p alg.
361 *
362 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
363 * #PSA_ALG_IS_AEAD(\p alg) is true).
364 *
365 * \return The default nonce size for the specified key type and algorithm.
366 * If the key type or AEAD algorithm is not recognized,
367 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100368 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200369#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
370 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
371 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
372 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
373 0 : \
374 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
375 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? \
376 12 : \
377 0)
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100378
379/** The maximum default nonce size among all supported pairs of key types and
380 * AEAD algorithms, in bytes.
381 *
382 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
383 * may return.
384 *
385 * \note This is not the maximum size of nonce supported as input to
386 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
387 * just the largest size that may be generated by
388 * #psa_aead_generate_nonce().
389 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100390#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100391
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200392/** A sufficient output buffer size for psa_aead_update().
393 *
394 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200395 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200396 * insufficient buffer size. The actual size of the output may be smaller
397 * in any given call.
398 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100399 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
400 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100401 * \warning This macro may evaluate its arguments multiple times or
402 * zero times, so you should not pass arguments that contain
403 * side effects.
404 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100405 * \param key_type A symmetric key type that is
406 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200407 * \param alg An AEAD algorithm
408 * (\c PSA_ALG_XXX value such that
409 * #PSA_ALG_IS_AEAD(\p alg) is true).
410 * \param input_length Size of the input in bytes.
411 *
412 * \return A sufficient output buffer size for the specified
413 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100414 * If the key type or AEAD algorithm is not
415 * recognized, or the parameters are incompatible,
416 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200417 */
418/* For all the AEAD modes defined in this specification, it is possible
419 * to emit output without delay. However, hardware may not always be
420 * capable of this. So for modes based on a block cipher, allow the
421 * implementation to delay the output until it has a full block. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200422#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
423 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
424 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
425 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
426 (input_length)) : \
427 (input_length) : \
428 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200429
430/** A sufficient output buffer size for psa_aead_update(), for any of the
431 * supported key types and AEAD algorithms.
432 *
433 * If the size of the output buffer is at least this large, it is guaranteed
434 * that psa_aead_update() will not fail due to an insufficient buffer size.
435 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100436 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200437 *
438 * \param input_length Size of the input in bytes.
439 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200440#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200441 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200442
443/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200444 *
445 * If the size of the ciphertext buffer is at least this large, it is
446 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200447 * insufficient ciphertext buffer size. The actual size of the output may
448 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200449 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100450 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
451 *
452 * \param key_type A symmetric key type that is
453 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200454 * \param alg An AEAD algorithm
455 * (\c PSA_ALG_XXX value such that
456 * #PSA_ALG_IS_AEAD(\p alg) is true).
457 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200458 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200459 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100460 * If the key type or AEAD algorithm is not
461 * recognized, or the parameters are incompatible,
462 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200463 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200464#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
465 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
466 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
467 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
468 0)
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200469
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200470/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
471 * supported key types and AEAD algorithms.
472 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100473 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200474 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200475#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200476
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200477/** A sufficient plaintext buffer size for psa_aead_verify().
478 *
479 * If the size of the plaintext buffer is at least this large, it is
480 * guaranteed that psa_aead_verify() will not fail due to an
481 * insufficient plaintext buffer size. The actual size of the output may
482 * be smaller in any given call.
483 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100484 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
485 *
486 * \param key_type A symmetric key type that is
487 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200488 * \param alg An AEAD algorithm
489 * (\c PSA_ALG_XXX value such that
490 * #PSA_ALG_IS_AEAD(\p alg) is true).
491 *
492 * \return A sufficient plaintext buffer size for the
493 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100494 * If the key type or AEAD algorithm is not
495 * recognized, or the parameters are incompatible,
496 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200497 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200498#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
499 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
500 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
501 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
502 0)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200503
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200504/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
505 * supported key types and AEAD algorithms.
506 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100507 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200508 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200509#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200510
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200511#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
512 (PSA_ALG_IS_RSA_OAEP(alg) ? \
513 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
514 11 /*PKCS#1v1.5*/)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100515
516/**
517 * \brief ECDSA signature size for a given curve bit size
518 *
519 * \param curve_bits Curve size in bits.
520 * \return Signature size in bytes.
521 *
522 * \note This macro returns a compile-time constant if its argument is one.
523 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200524#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100525
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100526/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200527 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200528 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200529 * of the specified type and size, with the specified algorithm.
530 * Note that the actual size of the signature may be smaller
531 * (some algorithms produce a variable-size signature).
532 *
533 * \warning This function may call its arguments multiple times or
534 * zero times, so you should not pass arguments that contain
535 * side effects.
536 *
537 * \param key_type An asymmetric key type (this may indifferently be a
538 * key pair type or a public key type).
539 * \param key_bits The size of the key in bits.
540 * \param alg The signature algorithm.
541 *
542 * \return If the parameters are valid and supported, return
543 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100544 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200545 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100546 * If the parameters are a valid combination that is not supported,
547 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200548 * If the parameters are not valid, the
549 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200550 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200551#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
552 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
553 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
554 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
555 ((void)alg, 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200556
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200557#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100558 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
559
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100560/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100561 *
562 * Maximum size of an asymmetric signature.
563 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100564 * This macro expands to a compile-time constant integer. This value
565 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100566 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200567#define PSA_SIGNATURE_MAX_SIZE \
568 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
569 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
570 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
571 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
Gilles Peskine29755712019-11-08 15:49:40 +0100572
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200573/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200574 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200575 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200576 * a key of the specified type and size, with the specified algorithm.
577 * Note that the actual size of the ciphertext may be smaller, depending
578 * on the algorithm.
579 *
580 * \warning This function may call its arguments multiple times or
581 * zero times, so you should not pass arguments that contain
582 * side effects.
583 *
584 * \param key_type An asymmetric key type (this may indifferently be a
585 * key pair type or a public key type).
586 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200587 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200588 *
589 * \return If the parameters are valid and supported, return
590 * a buffer size in bytes that guarantees that
591 * psa_asymmetric_encrypt() will not fail with
592 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100593 * If the parameters are a valid combination that is not supported,
594 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200595 * If the parameters are not valid, the
596 * return value is unspecified.
597 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200598#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
599 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
600 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
601 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200602
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200603/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
604 * supported asymmetric encryption.
605 *
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200606 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p
607 * alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200608 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100609/* This macro assumes that RSA is the only supported asymmetric encryption. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200610#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100611 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200612
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200613/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200614 *
Gilles Peskine76689602020-05-05 16:01:22 +0200615 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200616 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200617 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200618 * on the algorithm.
619 *
620 * \warning This function may call its arguments multiple times or
621 * zero times, so you should not pass arguments that contain
622 * side effects.
623 *
624 * \param key_type An asymmetric key type (this may indifferently be a
625 * key pair type or a public key type).
626 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200627 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200628 *
629 * \return If the parameters are valid and supported, return
630 * a buffer size in bytes that guarantees that
631 * psa_asymmetric_decrypt() will not fail with
632 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100633 * If the parameters are a valid combination that is not supported,
634 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200635 * If the parameters are not valid, the
636 * return value is unspecified.
637 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200638#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
639 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
640 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
641 0)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200642
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200643/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
644 * supported asymmetric decryption.
645 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100646 * This macro assumes that RSA is the only supported asymmetric encryption.
647 *
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200648 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p
649 * alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200650 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200651#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100652 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200653
Gilles Peskine1be949b2018-08-10 19:06:59 +0200654/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
655 * number of bits.
656 *
657 * This definition assumes that bits <= 2^19 - 9 so that the length field
658 * is at most 3 bytes. The length of the encoding is the length of the
659 * bit string padded to a whole number of bytes plus:
660 * - 1 type byte;
661 * - 1 to 3 length bytes;
662 * - 0 to 1 bytes of leading 0 due to the sign bit.
663 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200664#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) ((bits) / 8 + 5)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200665
666/* Maximum size of the export encoding of an RSA public key.
667 * Assumes that the public exponent is less than 2^32.
668 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200669 * RSAPublicKey ::= SEQUENCE {
670 * modulus INTEGER, -- n
671 * publicExponent INTEGER } -- e
672 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000673 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200674 * - n : INTEGER;
675 * - 7 bytes for the public exponent.
676 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200677#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000678 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200679
680/* Maximum size of the export encoding of an RSA key pair.
681 * Assumes thatthe public exponent is less than 2^32 and that the size
682 * difference between the two primes is at most 1 bit.
683 *
684 * RSAPrivateKey ::= SEQUENCE {
685 * version Version, -- 0
686 * modulus INTEGER, -- N-bit
687 * publicExponent INTEGER, -- 32-bit
688 * privateExponent INTEGER, -- N-bit
689 * prime1 INTEGER, -- N/2-bit
690 * prime2 INTEGER, -- N/2-bit
691 * exponent1 INTEGER, -- N/2-bit
692 * exponent2 INTEGER, -- N/2-bit
693 * coefficient INTEGER, -- N/2-bit
694 * }
695 *
696 * - 4 bytes of SEQUENCE overhead;
697 * - 3 bytes of version;
698 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
699 * overapproximated as 9 half-size INTEGERS;
700 * - 7 bytes for the public exponent.
701 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200702#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200703 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
704
705/* Maximum size of the export encoding of a DSA public key.
706 *
707 * SubjectPublicKeyInfo ::= SEQUENCE {
708 * algorithm AlgorithmIdentifier,
709 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
710 * AlgorithmIdentifier ::= SEQUENCE {
711 * algorithm OBJECT IDENTIFIER,
712 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
713 * DSAPublicKey ::= INTEGER -- public key, Y
714 *
715 * - 3 * 4 bytes of SEQUENCE overhead;
716 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
717 * - 4 bytes of BIT STRING overhead;
718 * - 3 full-size INTEGERs (p, g, y);
719 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
720 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200721#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200722 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
723
724/* Maximum size of the export encoding of a DSA key pair.
725 *
726 * DSAPrivateKey ::= SEQUENCE {
727 * version Version, -- 0
728 * prime INTEGER, -- p
729 * subprime INTEGER, -- q
730 * generator INTEGER, -- g
731 * public INTEGER, -- y
732 * private INTEGER, -- x
733 * }
734 *
735 * - 4 bytes of SEQUENCE overhead;
736 * - 3 bytes of version;
737 * - 3 full-size INTEGERs (p, g, y);
738 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
739 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200740#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200741 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
742
743/* Maximum size of the export encoding of an ECC public key.
744 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000745 * The representation of an ECC public key is:
746 * - The byte 0x04;
747 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
748 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
749 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200750 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000751 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200752 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200753#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000754 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200755
756/* Maximum size of the export encoding of an ECC key pair.
757 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100758 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200759 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200760#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100761 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200762
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100763/** Sufficient output buffer size for psa_export_key() or
764 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200765 *
766 * This macro returns a compile-time constant if its arguments are
767 * compile-time constants.
768 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100769 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200770 * zero times, so you should not pass arguments that contain
771 * side effects.
772 *
773 * The following code illustrates how to allocate enough memory to export
774 * a key by querying the key type and size at runtime.
775 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200776 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200777 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200778 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200779 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200780 * psa_key_type_t key_type = psa_get_key_type(&attributes);
781 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100782 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200783 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200784 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200785 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200786 * size_t buffer_length;
787 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
788 * if (status != PSA_SUCCESS) handle_error(...);
789 * \endcode
790 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200791 * \param key_type A supported key type.
792 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200793 *
794 * \return If the parameters are valid and supported, return
795 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100796 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100798 * If the parameters are a valid combination that is not supported,
799 * return either a sensible size or 0.
800 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200801 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200802#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
803 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? \
804 PSA_BITS_TO_BYTES(key_bits) : \
805 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? \
806 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
807 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? \
808 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
809 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? \
810 PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
811 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? \
812 PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
813 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
814 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
815 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? \
816 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
817 0)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200818
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200819/** Sufficient output buffer size for psa_export_public_key().
820 *
821 * This macro returns a compile-time constant if its arguments are
822 * compile-time constants.
823 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100824 * \warning This macro may evaluate its arguments multiple times or
825 * zero times, so you should not pass arguments that contain
826 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200827 *
828 * The following code illustrates how to allocate enough memory to export
829 * a public key by querying the key type and size at runtime.
830 * \code{c}
831 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
832 * psa_status_t status;
833 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100834 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200835 * psa_key_type_t key_type = psa_get_key_type(&attributes);
836 * size_t key_bits = psa_get_key_bits(&attributes);
837 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
838 * psa_reset_key_attributes(&attributes);
839 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100840 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200841 * size_t buffer_length;
842 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100843 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200844 * \endcode
845 *
846 * \param key_type A public key or key pair key type.
847 * \param key_bits The size of the key in bits.
848 *
849 * \return If the parameters are valid and supported, return
850 * a buffer size in bytes that guarantees that
851 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100852 * #PSA_ERROR_BUFFER_TOO_SMALL.
853 * If the parameters are a valid combination that is not
854 * supported, return either a sensible size or 0.
855 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200856 * the return value is unspecified.
857 *
858 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100859 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200860 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
861 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
862 * \p key_bits).
863 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200864#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
865 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
866 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
867 PSA_KEY_TYPE_IS_ECC(key_type) ? \
868 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
869 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200870
871/** Sufficient buffer size for exporting any asymmetric key pair.
872 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100873 * This macro expands to a compile-time constant integer. This value is
874 * a sufficient buffer size when calling psa_export_key() to export any
875 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200876 *
877 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
878 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200879#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
880 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
881 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE( \
882 PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
883 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
884 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200885
886/** Sufficient buffer size for exporting any asymmetric public key.
887 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100888 * This macro expands to a compile-time constant integer. This value is
889 * a sufficient buffer size when calling psa_export_key() or
890 * psa_export_public_key() to export any asymmetric public key,
891 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200892 *
893 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
894 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200895#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
896 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
897 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE( \
898 PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
899 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
900 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE( \
901 PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200902
903/** Sufficient output buffer size for psa_raw_key_agreement().
904 *
905 * This macro returns a compile-time constant if its arguments are
906 * compile-time constants.
907 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100908 * \warning This macro may evaluate its arguments multiple times or
909 * zero times, so you should not pass arguments that contain
910 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200911 *
912 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
913 *
914 * \param key_type A supported key type.
915 * \param key_bits The size of the key in bits.
916 *
917 * \return If the parameters are valid and supported, return
918 * a buffer size in bytes that guarantees that
919 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100920 * #PSA_ERROR_BUFFER_TOO_SMALL.
921 * If the parameters are a valid combination that
922 * is not supported, return either a sensible size or 0.
923 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200924 * the return value is unspecified.
925 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100926/* FFDH is not yet supported in PSA. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200927#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
928 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_BITS_TO_BYTES(key_bits) : 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200929
930/** Maximum size of the output from psa_raw_key_agreement().
931 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100932 * This macro expands to a compile-time constant integer. This value is the
933 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200934 *
935 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
936 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200937#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100938 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200939
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100940/** The default IV size for a cipher algorithm, in bytes.
941 *
942 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
943 * the default IV length for the algorithm.
944 *
945 * This macro can be used to allocate a buffer of sufficient size to
946 * store the IV output from #psa_cipher_generate_iv() when using
947 * a multi-part cipher operation.
948 *
949 * See also #PSA_CIPHER_IV_MAX_SIZE.
950 *
951 * \warning This macro may evaluate its arguments multiple times or
952 * zero times, so you should not pass arguments that contain
953 * side effects.
954 *
955 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
956 *
957 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
958 *
959 * \return The default IV size for the specified key type and algorithm.
960 * If the algorithm does not use an IV, return 0.
961 * If the key type or cipher algorithm is not recognized,
962 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100963 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200964#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
965 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
966 ((alg) == PSA_ALG_CTR || (alg) == PSA_ALG_CFB || \
967 (alg) == PSA_ALG_OFB || (alg) == PSA_ALG_XTS || \
968 (alg) == PSA_ALG_CBC_NO_PADDING || (alg) == PSA_ALG_CBC_PKCS7) ? \
969 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
970 (key_type) == PSA_KEY_TYPE_CHACHA20 && (alg) == PSA_ALG_STREAM_CIPHER ? \
971 12 : \
972 0)
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100973
974/** The maximum IV size for all supported cipher algorithms, in bytes.
975 *
976 * See also #PSA_CIPHER_IV_LENGTH().
977 */
978#define PSA_CIPHER_IV_MAX_SIZE 16
979
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200980/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
981 *
982 * If the size of the output buffer is at least this large, it is guaranteed
983 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
984 * Depending on the algorithm, the actual size of the output might be smaller.
985 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200986 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
987 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100988 * \warning This macro may evaluate its arguments multiple times or
989 * zero times, so you should not pass arguments that contain
990 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200991 *
992 * \param key_type A symmetric key type that is compatible with algorithm
993 * alg.
994 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
995 * #PSA_ALG_IS_CIPHER(\p alg) is true).
996 * \param input_length Size of the input in bytes.
997 *
998 * \return A sufficient output size for the specified key type and
999 * algorithm. If the key type or cipher algorithm is not
1000 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001001 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001002 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001003#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1004 (alg == PSA_ALG_CBC_PKCS7 ? \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001005 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1006 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1007 (input_length) + 1) + \
1008 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1009 0) : \
1010 (PSA_ALG_IS_CIPHER(alg) ? \
1011 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1012 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001013
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001014/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1015 * supported key types and cipher algorithms.
1016 *
1017 * If the size of the output buffer is at least this large, it is guaranteed
1018 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1019 *
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001020 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p
1021 * input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001022 *
1023 * \param input_length Size of the input in bytes.
1024 *
1025 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001026#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1027 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1028 (input_length) + 1) + \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001029 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001030
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001031/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1032 *
1033 * If the size of the output buffer is at least this large, it is guaranteed
1034 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1035 * Depending on the algorithm, the actual size of the output might be smaller.
1036 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001037 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001038 *
1039 * \param key_type A symmetric key type that is compatible with algorithm
1040 * alg.
1041 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1042 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1043 * \param input_length Size of the input in bytes.
1044 *
1045 * \return A sufficient output size for the specified key type and
1046 * algorithm. If the key type or cipher algorithm is not
1047 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001048 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001049 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001050#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1051 (PSA_ALG_IS_CIPHER(alg) && ((key_type)&PSA_KEY_TYPE_CATEGORY_MASK) == \
1052 PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1053 (input_length) : \
1054 0)
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001055
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001056/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1057 * supported key types and cipher algorithms.
1058 *
1059 * If the size of the output buffer is at least this large, it is guaranteed
1060 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1061 *
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001062 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p
1063 * input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001064 *
1065 * \param input_length Size of the input in bytes.
1066 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001067#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) (input_length)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001068
1069/** A sufficient output buffer size for psa_cipher_update().
1070 *
1071 * If the size of the output buffer is at least this large, it is guaranteed
1072 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1073 * The actual size of the output might be smaller in any given call.
1074 *
1075 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1076 *
1077 * \param key_type A symmetric key type that is compatible with algorithm
1078 * alg.
1079 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1080 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1081 * \param input_length Size of the input in bytes.
1082 *
1083 * \return A sufficient output size for the specified key type and
1084 * algorithm. If the key type or cipher algorithm is not
1085 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001086 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001087#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1088 (PSA_ALG_IS_CIPHER(alg) ? \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001089 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1090 (((alg) == PSA_ALG_CBC_PKCS7 || \
1091 (alg) == PSA_ALG_CBC_NO_PADDING || \
1092 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1093 PSA_ROUND_UP_TO_MULTIPLE( \
1094 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), input_length) : \
1095 (input_length)) : \
1096 0) : \
1097 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001098
1099/** A sufficient output buffer size for psa_cipher_update(), for any of the
1100 * supported key types and cipher algorithms.
1101 *
1102 * If the size of the output buffer is at least this large, it is guaranteed
1103 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1104 *
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001105 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p
1106 * input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001107 *
1108 * \param input_length Size of the input in bytes.
1109 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001110#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001111 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001112
1113/** A sufficient ciphertext buffer size for psa_cipher_finish().
1114 *
1115 * If the size of the ciphertext buffer is at least this large, it is
1116 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1117 * ciphertext buffer size. The actual size of the output might be smaller in
1118 * any given call.
1119 *
1120 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1121 *
1122 * \param key_type A symmetric key type that is compatible with algorithm
1123 * alg.
1124 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1125 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1126 * \return A sufficient output size for the specified key type and
1127 * algorithm. If the key type or cipher algorithm is not
1128 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001129 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001130#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1131 (PSA_ALG_IS_CIPHER(alg) ? \
1132 (alg == PSA_ALG_CBC_PKCS7 ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1133 0) : \
1134 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001135
1136/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1137 * supported key types and cipher algorithms.
1138 *
1139 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1140 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001141#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001142
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001143#endif /* PSA_CRYPTO_SIZES_H */