blob: 75d6582de1a64c5c0377e20c8bfaef01a93d27a5 [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)
Przemek Stekiel654bef02022-12-15 13:28:02 +010054#define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \
55 (c) : (b) : (a) <= (c) ? (c) : (a))
Gilles Peskinea7c26db2018-12-12 13:42:25 +010056
Gilles Peskine248010c2019-05-14 16:08:59 +020057#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
58 (((length) + (block_size) - 1) / (block_size) * (block_size))
59
Gilles Peskinea7c26db2018-12-12 13:42:25 +010060/** The size of the output of psa_hash_finish(), in bytes.
61 *
62 * This is also the hash size that psa_hash_verify() expects.
63 *
64 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
65 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
66 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
67 * hash algorithm).
68 *
69 * \return The hash size for the specified hash algorithm.
70 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010071 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010072#define PSA_HASH_LENGTH(alg) \
73 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010074 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
87 0)
88
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +020089/** The input block size of a hash algorithm, in bytes.
90 *
91 * Hash algorithms process their input data in blocks. Hash operations will
92 * retain any partial blocks until they have enough input to fill the block or
93 * until the operation is finished.
94 * This affects the output from psa_hash_suspend().
95 *
96 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
97 * PSA_ALG_IS_HASH(\p alg) is true).
98 *
99 * \return The block size in bytes for the specified hash algorithm.
100 * If the hash algorithm is not recognized, return 0.
101 * An implementation can return either 0 or the correct size for a
102 * hash algorithm that it recognizes, but does not support.
103 */
104#define PSA_HASH_BLOCK_LENGTH(alg) \
105 ( \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
109 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
110 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
111 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
112 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
113 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
114 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
115 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
116 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
117 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
118 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
119 0)
120
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200121/** \def PSA_HASH_MAX_SIZE
122 *
123 * Maximum size of a hash.
124 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100125 * This macro expands to a compile-time constant integer. This value
126 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200127 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200128/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
129 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
130 * HMAC-SHA3-512. */
Ronald Cronfcaba242021-10-18 09:10:31 +0200131#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200132#define PSA_HASH_MAX_SIZE 64
133#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
134#else
135#define PSA_HASH_MAX_SIZE 32
136#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
137#endif
138
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200139/** \def PSA_MAC_MAX_SIZE
140 *
141 * Maximum size of a MAC.
142 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100143 * This macro expands to a compile-time constant integer. This value
144 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200145 */
146/* All non-HMAC MACs have a maximum size that's smaller than the
147 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200148/* Note that the encoding of truncated MAC algorithms limits this value
149 * to 64 bytes.
150 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200151#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
152
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100153/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100154 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100155 * This macro can be used to allocate a buffer of sufficient size to store the
156 * tag output from psa_aead_finish().
157 *
158 * See also #PSA_AEAD_TAG_MAX_SIZE.
159 *
160 * \param key_type The type of the AEAD key.
161 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100162 * \param alg An AEAD algorithm
163 * (\c PSA_ALG_XXX value such that
164 * #PSA_ALG_IS_AEAD(\p alg) is true).
165 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200166 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100167 * If the AEAD algorithm does not have an identified
168 * tag that can be distinguished from the rest of
169 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100170 * If the key type or AEAD algorithm is not
171 * recognized, or the parameters are incompatible,
172 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100173 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100174#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200175 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200176 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100177 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100178
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200179/** The maximum tag size for all supported AEAD algorithms, in bytes.
180 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100181 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200182 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100183#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200184
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200185/* The maximum size of an RSA key on this implementation, in bits.
186 * This is a vendor-specific macro.
187 *
188 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
189 * whose parameters fit in a bignum is accepted. However large keys can
190 * induce a large memory usage and long computation times. Unlike other
191 * auxiliary macros in this file and in crypto.h, which reflect how the
192 * library is configured, this macro defines how the library is
193 * configured. This implementation refuses to import or generate an
194 * RSA key whose size is larger than the value defined here.
195 *
196 * Note that an implementation may set different size limits for different
197 * operations, and does not need to accept all key sizes up to the limit. */
198#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
199
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200200/* The maximum size of an DH key on this implementation, in bits.
Przemek Stekieled23b612022-12-01 15:00:41 +0100201 *
202 * Note that an implementation may set different size limits for different
203 * operations, and does not need to accept all key sizes up to the limit. */
204#define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192
205
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200206/* The maximum size of an ECC key on this implementation, in bits.
207 * This is a vendor-specific macro. */
Valerio Setti271c12e2023-03-23 16:30:27 +0100208#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200209#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Valerio Setti271c12e2023-03-23 16:30:27 +0100210#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200211#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
Valerio Setti271c12e2023-03-23 16:30:27 +0100212#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200213#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
Valerio Setti271c12e2023-03-23 16:30:27 +0100214#elif defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200215#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Valerio Setti271c12e2023-03-23 16:30:27 +0100216#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200217#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Valerio Setti271c12e2023-03-23 16:30:27 +0100218#elif defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200219#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100220#elif defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200221#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100222#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200223#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100224#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200225#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
Valerio Setti271c12e2023-03-23 16:30:27 +0100226#elif defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200227#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Valerio Setti271c12e2023-03-23 16:30:27 +0100228#elif defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200229#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Valerio Setti271c12e2023-03-23 16:30:27 +0100230#elif defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200231#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
Valerio Setti271c12e2023-03-23 16:30:27 +0100232#elif defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200233#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
234#else
235#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
236#endif
237
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100238/** This macro returns the maximum supported length of the PSK for the
239 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100240 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100241 *
242 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100243 *
244 * Quoting RFC 4279, Sect 5.3:
245 * TLS implementations supporting these ciphersuites MUST support
246 * arbitrary PSK identities up to 128 octets in length, and arbitrary
247 * PSKs up to 64 octets in length. Supporting longer identities and
248 * keys is RECOMMENDED.
249 *
250 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100251 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100252 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100253#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100254
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400255/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
256 * which is expected to work with P-256 curve only. */
257#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65
258
259/* The size of a serialized K.X coordinate to be used in
260 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
261 * curve. */
262#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32
263
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530264/* The maximum number of iterations for PBKDF2 on this implementation, in bits.
265 * This is a vendor-specific macro. This can be configured if necessary */
266#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffff
267
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100268/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100269#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200270
Gilles Peskineacd4be32018-07-08 19:56:25 +0200271/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200272 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200273 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200274 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100275 * \warning This macro may evaluate its arguments multiple times or
276 * zero times, so you should not pass arguments that contain
277 * side effects.
278 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200279 * \param key_type The type of the MAC key.
280 * \param key_bits The size of the MAC key in bits.
281 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100282 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200283 *
284 * \return The MAC size for the specified algorithm with
285 * the specified key parameters.
286 * \return 0 if the MAC algorithm is not recognized.
287 * \return Either 0 or the correct size for a MAC algorithm that
288 * the implementation recognizes, but does not support.
289 * \return Unspecified if the key parameters are not consistent
290 * with the algorithm.
291 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100292#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
293 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
294 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
295 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 ((void) (key_type), (void) (key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297
298/** The maximum size of the output of psa_aead_encrypt(), in bytes.
299 *
300 * If the size of the ciphertext buffer is at least this large, it is
301 * guaranteed that psa_aead_encrypt() will not fail due to an
302 * insufficient buffer size. Depending on the algorithm, the actual size of
303 * the ciphertext may be smaller.
304 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100305 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
306 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100307 * \warning This macro may evaluate its arguments multiple times or
308 * zero times, so you should not pass arguments that contain
309 * side effects.
310 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100311 * \param key_type A symmetric key type that is
312 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200313 * \param alg An AEAD algorithm
314 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100315 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200316 * \param plaintext_length Size of the plaintext in bytes.
317 *
318 * \return The AEAD ciphertext size for the specified
319 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100320 * If the key type or AEAD algorithm is not
321 * recognized, or the parameters are incompatible,
322 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200323 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100324#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200325 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200326 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200327 0)
328
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200329/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
330 * supported key types and AEAD algorithms.
331 *
332 * If the size of the ciphertext buffer is at least this large, it is guaranteed
333 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
334 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100335 * \note This macro returns a compile-time constant if its arguments are
336 * compile-time constants.
337 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100338 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
339 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200340 *
341 * \param plaintext_length Size of the plaintext in bytes.
342 *
343 * \return A sufficient output buffer size for any of the
344 * supported key types and AEAD algorithms.
345 *
346 */
347#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
348 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
349
350
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200351/** The maximum size of the output of psa_aead_decrypt(), in bytes.
352 *
353 * If the size of the plaintext buffer is at least this large, it is
354 * guaranteed that psa_aead_decrypt() will not fail due to an
355 * insufficient buffer size. Depending on the algorithm, the actual size of
356 * the plaintext may be smaller.
357 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100358 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
359 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100360 * \warning This macro may evaluate its arguments multiple times or
361 * zero times, so you should not pass arguments that contain
362 * side effects.
363 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100364 * \param key_type A symmetric key type that is
365 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200366 * \param alg An AEAD algorithm
367 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100368 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200369 * \param ciphertext_length Size of the plaintext in bytes.
370 *
371 * \return The AEAD ciphertext size for the specified
372 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100373 * If the key type or AEAD algorithm is not
374 * recognized, or the parameters are incompatible,
375 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200376 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100377#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200378 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
380 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200381 0)
382
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200383/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
384 * supported key types and AEAD algorithms.
385 *
386 * If the size of the plaintext buffer is at least this large, it is guaranteed
387 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
388 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100389 * \note This macro returns a compile-time constant if its arguments are
390 * compile-time constants.
391 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100392 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
393 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200394 *
395 * \param ciphertext_length Size of the ciphertext in bytes.
396 *
397 * \return A sufficient output buffer size for any of the
398 * supported key types and AEAD algorithms.
399 *
400 */
401#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200403
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100404/** The default nonce size for an AEAD algorithm, in bytes.
405 *
406 * This macro can be used to allocate a buffer of sufficient size to
407 * store the nonce output from #psa_aead_generate_nonce().
408 *
409 * See also #PSA_AEAD_NONCE_MAX_SIZE.
410 *
411 * \note This is not the maximum size of nonce supported as input to
412 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
413 * just the default size that is generated by #psa_aead_generate_nonce().
414 *
415 * \warning This macro may evaluate its arguments multiple times or
416 * zero times, so you should not pass arguments that contain
417 * side effects.
418 *
419 * \param key_type A symmetric key type that is compatible with
420 * algorithm \p alg.
421 *
422 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
423 * #PSA_ALG_IS_AEAD(\p alg) is true).
424 *
425 * \return The default nonce size for the specified key type and algorithm.
426 * If the key type or AEAD algorithm is not recognized,
427 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100428 */
429#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100430 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
432 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
433 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100434 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100436 0)
437
438/** The maximum default nonce size among all supported pairs of key types and
439 * AEAD algorithms, in bytes.
440 *
441 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
442 * may return.
443 *
444 * \note This is not the maximum size of nonce supported as input to
445 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
446 * just the largest size that may be generated by
447 * #psa_aead_generate_nonce().
448 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100449#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100450
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200451/** A sufficient output buffer size for psa_aead_update().
452 *
453 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200454 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200455 * insufficient buffer size. The actual size of the output may be smaller
456 * in any given call.
457 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100458 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
459 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100460 * \warning This macro may evaluate its arguments multiple times or
461 * zero times, so you should not pass arguments that contain
462 * side effects.
463 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100464 * \param key_type A symmetric key type that is
465 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200466 * \param alg An AEAD algorithm
467 * (\c PSA_ALG_XXX value such that
468 * #PSA_ALG_IS_AEAD(\p alg) is true).
469 * \param input_length Size of the input in bytes.
470 *
471 * \return A sufficient output buffer size for the specified
472 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100473 * If the key type or AEAD algorithm is not
474 * recognized, or the parameters are incompatible,
475 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200476 */
477/* For all the AEAD modes defined in this specification, it is possible
478 * to emit output without delay. However, hardware may not always be
479 * capable of this. So for modes based on a block cipher, allow the
480 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100481#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200482 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
484 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
485 (input_length) : \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100486 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200487
488/** A sufficient output buffer size for psa_aead_update(), for any of the
489 * supported key types and AEAD algorithms.
490 *
491 * If the size of the output buffer is at least this large, it is guaranteed
492 * that psa_aead_update() will not fail due to an insufficient buffer size.
493 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100494 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200495 *
496 * \param input_length Size of the input in bytes.
497 */
498#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
499 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200500
501/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200502 *
503 * If the size of the ciphertext buffer is at least this large, it is
504 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200505 * insufficient ciphertext buffer size. The actual size of the output may
506 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200507 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100508 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
509 *
510 * \param key_type A symmetric key type that is
511 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200512 * \param alg An AEAD algorithm
513 * (\c PSA_ALG_XXX value such that
514 * #PSA_ALG_IS_AEAD(\p alg) is true).
515 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200516 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200517 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100518 * If the key type or AEAD algorithm is not
519 * recognized, or the parameters are incompatible,
520 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200521 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200522#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
523 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
525 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200526 0)
527
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200528/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
529 * supported key types and AEAD algorithms.
530 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100531 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200532 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200533#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200534
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200535/** A sufficient plaintext buffer size for psa_aead_verify().
536 *
537 * If the size of the plaintext buffer is at least this large, it is
538 * guaranteed that psa_aead_verify() will not fail due to an
539 * insufficient plaintext buffer size. The actual size of the output may
540 * be smaller in any given call.
541 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100542 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
543 *
544 * \param key_type A symmetric key type that is
545 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200546 * \param alg An AEAD algorithm
547 * (\c PSA_ALG_XXX value such that
548 * #PSA_ALG_IS_AEAD(\p alg) is true).
549 *
550 * \return A sufficient plaintext buffer size for the
551 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100552 * If the key type or AEAD algorithm is not
553 * recognized, or the parameters are incompatible,
554 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200555 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200556#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
557 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
559 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200560 0)
561
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200562/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
563 * supported key types and AEAD algorithms.
564 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100565 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200566 */
567#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
568
Jaeden Amero7f042142019-02-07 10:44:38 +0000569#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
570 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100571 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100572 11 /*PKCS#1v1.5*/)
573
574/**
575 * \brief ECDSA signature size for a given curve bit size
576 *
577 * \param curve_bits Curve size in bits.
578 * \return Signature size in bytes.
579 *
580 * \note This macro returns a compile-time constant if its argument is one.
581 */
582#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
583 (PSA_BITS_TO_BYTES(curve_bits) * 2)
584
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100585/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200586 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200587 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200588 * of the specified type and size, with the specified algorithm.
589 * Note that the actual size of the signature may be smaller
590 * (some algorithms produce a variable-size signature).
591 *
592 * \warning This function may call its arguments multiple times or
593 * zero times, so you should not pass arguments that contain
594 * side effects.
595 *
596 * \param key_type An asymmetric key type (this may indifferently be a
597 * key pair type or a public key type).
598 * \param key_bits The size of the key in bits.
599 * \param alg The signature algorithm.
600 *
601 * \return If the parameters are valid and supported, return
602 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100603 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200604 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100605 * If the parameters are a valid combination that is not supported,
606 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200607 * If the parameters are not valid, the
608 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200609 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100610#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200612 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 ((void) alg, 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200614
Gilles Peskine29755712019-11-08 15:49:40 +0100615#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
616 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
617
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100618/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100619 *
620 * Maximum size of an asymmetric signature.
621 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100622 * This macro expands to a compile-time constant integer. This value
623 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100624 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100625#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100626 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
627 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
628 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
629
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200630/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200631 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200632 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200633 * a key of the specified type and size, with the specified algorithm.
634 * Note that the actual size of the ciphertext may be smaller, depending
635 * on the algorithm.
636 *
637 * \warning This function may call its arguments multiple times or
638 * zero times, so you should not pass arguments that contain
639 * side effects.
640 *
641 * \param key_type An asymmetric key type (this may indifferently be a
642 * key pair type or a public key type).
643 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200644 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200645 *
646 * \return If the parameters are valid and supported, return
647 * a buffer size in bytes that guarantees that
648 * psa_asymmetric_encrypt() will not fail with
649 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100650 * If the parameters are a valid combination that is not supported,
651 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200652 * If the parameters are not valid, the
653 * return value is unspecified.
654 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200655#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
656 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200658 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200659
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200660/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
661 * supported asymmetric encryption.
662 *
663 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
664 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100665/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200666#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100667 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200668
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200669/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200670 *
Gilles Peskine76689602020-05-05 16:01:22 +0200671 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200672 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200673 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200674 * on the algorithm.
675 *
676 * \warning This function may call its arguments multiple times or
677 * zero times, so you should not pass arguments that contain
678 * side effects.
679 *
680 * \param key_type An asymmetric key type (this may indifferently be a
681 * key pair type or a public key type).
682 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200683 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200684 *
685 * \return If the parameters are valid and supported, return
686 * a buffer size in bytes that guarantees that
687 * psa_asymmetric_decrypt() will not fail with
688 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100689 * If the parameters are a valid combination that is not supported,
690 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200691 * If the parameters are not valid, the
692 * return value is unspecified.
693 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200694#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
695 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
696 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
697 0)
698
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200699/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
700 * supported asymmetric decryption.
701 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100702 * This macro assumes that RSA is the only supported asymmetric encryption.
703 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200704 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
705 */
706#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100707 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200708
Gilles Peskine1be949b2018-08-10 19:06:59 +0200709/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
710 * number of bits.
711 *
712 * This definition assumes that bits <= 2^19 - 9 so that the length field
713 * is at most 3 bytes. The length of the encoding is the length of the
714 * bit string padded to a whole number of bytes plus:
715 * - 1 type byte;
716 * - 1 to 3 length bytes;
717 * - 0 to 1 bytes of leading 0 due to the sign bit.
718 */
719#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
720 ((bits) / 8 + 5)
721
722/* Maximum size of the export encoding of an RSA public key.
723 * Assumes that the public exponent is less than 2^32.
724 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200725 * RSAPublicKey ::= SEQUENCE {
726 * modulus INTEGER, -- n
727 * publicExponent INTEGER } -- e
728 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000729 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200730 * - n : INTEGER;
731 * - 7 bytes for the public exponent.
732 */
733#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000734 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200735
736/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000737 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200738 * difference between the two primes is at most 1 bit.
739 *
740 * RSAPrivateKey ::= SEQUENCE {
741 * version Version, -- 0
742 * modulus INTEGER, -- N-bit
743 * publicExponent INTEGER, -- 32-bit
744 * privateExponent INTEGER, -- N-bit
745 * prime1 INTEGER, -- N/2-bit
746 * prime2 INTEGER, -- N/2-bit
747 * exponent1 INTEGER, -- N/2-bit
748 * exponent2 INTEGER, -- N/2-bit
749 * coefficient INTEGER, -- N/2-bit
750 * }
751 *
752 * - 4 bytes of SEQUENCE overhead;
753 * - 3 bytes of version;
754 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
755 * overapproximated as 9 half-size INTEGERS;
756 * - 7 bytes for the public exponent.
757 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200758#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200759 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
760
761/* Maximum size of the export encoding of a DSA public key.
762 *
763 * SubjectPublicKeyInfo ::= SEQUENCE {
764 * algorithm AlgorithmIdentifier,
765 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
766 * AlgorithmIdentifier ::= SEQUENCE {
767 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400768 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200769 * DSAPublicKey ::= INTEGER -- public key, Y
770 *
771 * - 3 * 4 bytes of SEQUENCE overhead;
772 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
773 * - 4 bytes of BIT STRING overhead;
774 * - 3 full-size INTEGERs (p, g, y);
775 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
776 */
777#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
778 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
779
780/* Maximum size of the export encoding of a DSA key pair.
781 *
782 * DSAPrivateKey ::= SEQUENCE {
783 * version Version, -- 0
784 * prime INTEGER, -- p
785 * subprime INTEGER, -- q
786 * generator INTEGER, -- g
787 * public INTEGER, -- y
788 * private INTEGER, -- x
789 * }
790 *
791 * - 4 bytes of SEQUENCE overhead;
792 * - 3 bytes of version;
793 * - 3 full-size INTEGERs (p, g, y);
794 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
795 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200796#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
798
799/* Maximum size of the export encoding of an ECC public key.
800 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000801 * The representation of an ECC public key is:
802 * - The byte 0x04;
803 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
804 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
805 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200806 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000807 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200808 */
809#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000810 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200811
812/* Maximum size of the export encoding of an ECC key pair.
813 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100814 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200815 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200816#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100817 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200818
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200819/* Maximum size of the export encoding of an DH key pair.
Przemek Stekieled23b612022-12-01 15:00:41 +0100820 *
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200821 * An DH key pair is represented by the secret value.
Przemek Stekieled23b612022-12-01 15:00:41 +0100822 */
823#define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \
824 (PSA_BITS_TO_BYTES(key_bits))
825
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200826/* Maximum size of the export encoding of an DH public key.
Przemek Stekieled23b612022-12-01 15:00:41 +0100827 */
828#define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \
829 (PSA_BITS_TO_BYTES(key_bits))
830
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100831/** Sufficient output buffer size for psa_export_key() or
832 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200833 *
834 * This macro returns a compile-time constant if its arguments are
835 * compile-time constants.
836 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100837 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200838 * zero times, so you should not pass arguments that contain
839 * side effects.
840 *
841 * The following code illustrates how to allocate enough memory to export
842 * a key by querying the key type and size at runtime.
843 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200844 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200845 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200846 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200847 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200848 * psa_key_type_t key_type = psa_get_key_type(&attributes);
849 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100850 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200851 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200852 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200853 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200854 * size_t buffer_length;
855 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
856 * if (status != PSA_SUCCESS) handle_error(...);
857 * \endcode
858 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200859 * \param key_type A supported key type.
860 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200861 *
862 * \return If the parameters are valid and supported, return
863 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100864 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200865 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100866 * If the parameters are a valid combination that is not supported,
867 * return either a sensible size or 0.
868 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200869 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100870#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
871 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100872 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100873 (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 +0200874 (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 +0100875 (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 +0200876 (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 +0100877 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
878 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 +0200879 0)
880
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200881/** Sufficient output buffer size for psa_export_public_key().
882 *
883 * This macro returns a compile-time constant if its arguments are
884 * compile-time constants.
885 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100886 * \warning This macro may evaluate its arguments multiple times or
887 * zero times, so you should not pass arguments that contain
888 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200889 *
890 * The following code illustrates how to allocate enough memory to export
891 * a public key by querying the key type and size at runtime.
892 * \code{c}
893 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
894 * psa_status_t status;
895 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100896 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200897 * psa_key_type_t key_type = psa_get_key_type(&attributes);
898 * size_t key_bits = psa_get_key_bits(&attributes);
899 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
900 * psa_reset_key_attributes(&attributes);
901 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100902 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200903 * size_t buffer_length;
904 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100905 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200906 * \endcode
907 *
908 * \param key_type A public key or key pair key type.
909 * \param key_bits The size of the key in bits.
910 *
911 * \return If the parameters are valid and supported, return
912 * a buffer size in bytes that guarantees that
913 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100914 * #PSA_ERROR_BUFFER_TOO_SMALL.
915 * If the parameters are a valid combination that is not
916 * supported, return either a sensible size or 0.
917 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200918 * the return value is unspecified.
919 *
920 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100921 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200922 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
923 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
924 * \p key_bits).
925 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100926#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
927 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
928 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100929 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200930 0)
931
932/** Sufficient buffer size for exporting any asymmetric key pair.
933 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100934 * This macro expands to a compile-time constant integer. This value is
935 * a sufficient buffer size when calling psa_export_key() to export any
936 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200937 *
938 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
939 */
Przemek Stekiel5357a7a2023-04-27 11:22:36 +0200940#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
941 PSA_MAX_OF_THREE(PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS), \
942 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS), \
943 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200944
945/** Sufficient buffer size for exporting any asymmetric public key.
946 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100947 * This macro expands to a compile-time constant integer. This value is
948 * a sufficient buffer size when calling psa_export_key() or
949 * psa_export_public_key() to export any asymmetric public key,
950 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200951 *
952 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
953 */
Przemek Stekiel5357a7a2023-04-27 11:22:36 +0200954#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
955 PSA_MAX_OF_THREE(PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS), \
Przemek Stekiel654bef02022-12-15 13:28:02 +0100956 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS), \
957 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS))
958
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200959
960/** Sufficient output buffer size for psa_raw_key_agreement().
961 *
962 * This macro returns a compile-time constant if its arguments are
963 * compile-time constants.
964 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100965 * \warning This macro may evaluate its arguments multiple times or
966 * zero times, so you should not pass arguments that contain
967 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200968 *
969 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
970 *
971 * \param key_type A supported key type.
972 * \param key_bits The size of the key in bits.
973 *
974 * \return If the parameters are valid and supported, return
975 * a buffer size in bytes that guarantees that
976 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100977 * #PSA_ERROR_BUFFER_TOO_SMALL.
978 * If the parameters are a valid combination that
979 * is not supported, return either a sensible size or 0.
980 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200981 * the return value is unspecified.
982 */
983#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
Przemek Stekiel654bef02022-12-15 13:28:02 +0100984 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \
985 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200986
987/** Maximum size of the output from psa_raw_key_agreement().
988 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100989 * This macro expands to a compile-time constant integer. This value is the
990 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200991 *
992 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
993 */
Przemek Stekiel4ce52322023-04-28 13:40:34 +0200994#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
995 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
996 PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) ? \
997 PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) : \
998 PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200999
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001000/** The default IV size for a cipher algorithm, in bytes.
1001 *
1002 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
1003 * the default IV length for the algorithm.
1004 *
1005 * This macro can be used to allocate a buffer of sufficient size to
1006 * store the IV output from #psa_cipher_generate_iv() when using
1007 * a multi-part cipher operation.
1008 *
1009 * See also #PSA_CIPHER_IV_MAX_SIZE.
1010 *
1011 * \warning This macro may evaluate its arguments multiple times or
1012 * zero times, so you should not pass arguments that contain
1013 * side effects.
1014 *
1015 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
1016 *
1017 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
1018 *
1019 * \return The default IV size for the specified key type and algorithm.
1020 * If the algorithm does not use an IV, return 0.
1021 * If the key type or cipher algorithm is not recognized,
1022 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001023 */
1024#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001025 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 ((alg) == PSA_ALG_CTR || \
1027 (alg) == PSA_ALG_CFB || \
1028 (alg) == PSA_ALG_OFB || \
1029 (alg) == PSA_ALG_XTS || \
1030 (alg) == PSA_ALG_CBC_NO_PADDING || \
1031 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001032 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
1034 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
1035 0)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001036
1037/** The maximum IV size for all supported cipher algorithms, in bytes.
1038 *
1039 * See also #PSA_CIPHER_IV_LENGTH().
1040 */
1041#define PSA_CIPHER_IV_MAX_SIZE 16
1042
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001043/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1044 *
1045 * If the size of the output buffer is at least this large, it is guaranteed
1046 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1047 * Depending on the algorithm, the actual size of the output might be smaller.
1048 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001049 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1050 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001051 * \warning This macro may evaluate its arguments multiple times or
1052 * zero times, so you should not pass arguments that contain
1053 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001054 *
1055 * \param key_type A symmetric key type that is compatible with algorithm
1056 * alg.
1057 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1058 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1059 * \param input_length Size of the input in bytes.
1060 *
1061 * \return A sufficient output size for the specified key type and
1062 * algorithm. If the key type or cipher algorithm is not
1063 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001064 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001065 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001066#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1067 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliottc22950c2021-07-08 16:50:01 +01001068 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1070 (input_length) + 1) + \
1071 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001072 (PSA_ALG_IS_CIPHER(alg) ? \
1073 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001075
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001076/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1077 * supported key types and cipher algorithms.
1078 *
1079 * If the size of the output buffer is at least this large, it is guaranteed
1080 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1081 *
1082 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1083 *
1084 * \param input_length Size of the input in bytes.
1085 *
1086 */
1087#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1088 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001089 (input_length) + 1) + \
1090 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001091
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001092/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
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 * Depending on the algorithm, the actual size of the output might be smaller.
1097 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001098 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001099 *
1100 * \param key_type A symmetric key type that is compatible with algorithm
1101 * alg.
1102 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1103 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1104 * \param input_length Size of the input in bytes.
1105 *
1106 * \return A sufficient output size for the specified key type and
1107 * algorithm. If the key type or cipher algorithm is not
1108 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001109 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001110 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001111#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1112 (PSA_ALG_IS_CIPHER(alg) && \
1113 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1114 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001115 0)
1116
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001117/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1118 * supported key types and cipher algorithms.
1119 *
1120 * If the size of the output buffer is at least this large, it is guaranteed
1121 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1122 *
1123 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1124 *
1125 * \param input_length Size of the input in bytes.
1126 */
1127#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1128 (input_length)
1129
1130/** A sufficient output buffer size for psa_cipher_update().
1131 *
1132 * If the size of the output buffer is at least this large, it is guaranteed
1133 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1134 * The actual size of the output might be smaller in any given call.
1135 *
1136 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1137 *
1138 * \param key_type A symmetric key type that is compatible with algorithm
1139 * alg.
1140 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1141 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1142 * \param input_length Size of the input in bytes.
1143 *
1144 * \return A sufficient output size for the specified key type and
1145 * algorithm. If the key type or cipher algorithm is not
1146 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001147 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001148#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1149 (PSA_ALG_IS_CIPHER(alg) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1151 (((alg) == PSA_ALG_CBC_PKCS7 || \
1152 (alg) == PSA_ALG_CBC_NO_PADDING || \
1153 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1154 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001155 input_length) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001157 0)
1158
1159/** A sufficient output buffer size for psa_cipher_update(), for any of the
1160 * supported key types and cipher algorithms.
1161 *
1162 * If the size of the output buffer is at least this large, it is guaranteed
1163 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1164 *
1165 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1166 *
1167 * \param input_length Size of the input in bytes.
1168 */
1169#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001170 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001171
1172/** A sufficient ciphertext buffer size for psa_cipher_finish().
1173 *
1174 * If the size of the ciphertext buffer is at least this large, it is
1175 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1176 * ciphertext buffer size. The actual size of the output might be smaller in
1177 * any given call.
1178 *
1179 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1180 *
1181 * \param key_type A symmetric key type that is compatible with algorithm
1182 * alg.
1183 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1184 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1185 * \return A sufficient output size for the specified key type and
1186 * algorithm. If the key type or cipher algorithm is not
1187 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001188 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001189#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1190 (PSA_ALG_IS_CIPHER(alg) ? \
1191 (alg == PSA_ALG_CBC_PKCS7 ? \
1192 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1193 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001194 0)
1195
1196/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1197 * supported key types and cipher algorithms.
1198 *
1199 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1200 */
1201#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1202 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1203
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001204#endif /* PSA_CRYPTO_SIZES_H */