blob: 1d5ed6c264387f9ab5c30b153eb5675224999677 [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 Peskine3c861642023-07-20 15:10:34 +020052#define PSA_BITS_TO_BYTES(bits) (((bits) + 7u) / 8u)
53#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8u)
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 Peskine3c861642023-07-20 15:10:34 +020074 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16u : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20u : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20u : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28u : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32u : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48u : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64u : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28u : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32u : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28u : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32u : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48u : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64u : \
87 0u)
Gilles Peskinea7c26db2018-12-12 13:42:25 +010088
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 ( \
Gilles Peskine3c861642023-07-20 15:10:34 +0200106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64u : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64u : \
108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64u : \
109 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64u : \
110 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64u : \
111 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128u : \
112 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128u : \
113 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128u : \
114 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128u : \
115 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144u : \
116 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136u : \
117 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104u : \
118 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72u : \
Gilles Peskine935ff232023-08-09 19:48:02 +0200119 0u)
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +0200120
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 */
Dave Rodgman5734bb92023-06-26 18:23:08 +0100128/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224,
Gilles Peskine3052f532018-09-17 14:13:26 +0200129 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
130 * HMAC-SHA3-512. */
Manuel Pégourié-Gonnardc9d98292023-05-24 12:28:38 +0200131/* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE,
132 * see the note on MBEDTLS_MD_MAX_SIZE for details. */
Dave Rodgman5734bb92023-06-26 18:23:08 +0100133#if defined(PSA_WANT_ALG_SHA3_224)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200134#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100135#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200136#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 136u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100137#elif defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine3c861642023-07-20 15:10:34 +0200138#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200139#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200140#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100141#elif defined(PSA_WANT_ALG_SHA3_384)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200142#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 104u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100143#elif defined(PSA_WANT_ALG_SHA3_512)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200144#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 72u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200145#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200146#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200147#elif defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200148#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200149#else /* SHA-1 or smaller */
Gilles Peskine3c861642023-07-20 15:10:34 +0200150#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200151#endif
152
Dave Rodgman5734bb92023-06-26 18:23:08 +0100153#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200154#define PSA_HASH_MAX_SIZE 64u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100155#elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200156#define PSA_HASH_MAX_SIZE 48u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100157#elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200158#define PSA_HASH_MAX_SIZE 32u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100159#elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200160#define PSA_HASH_MAX_SIZE 28u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100161#else /* SHA-1 or smaller */
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200162#define PSA_HASH_MAX_SIZE 20u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100163#endif
164
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200165/** \def PSA_MAC_MAX_SIZE
166 *
167 * Maximum size of a MAC.
168 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100169 * This macro expands to a compile-time constant integer. This value
170 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200171 */
172/* All non-HMAC MACs have a maximum size that's smaller than the
173 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200174/* Note that the encoding of truncated MAC algorithms limits this value
175 * to 64 bytes.
176 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200177#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
178
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100179/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100180 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100181 * This macro can be used to allocate a buffer of sufficient size to store the
182 * tag output from psa_aead_finish().
183 *
184 * See also #PSA_AEAD_TAG_MAX_SIZE.
185 *
186 * \param key_type The type of the AEAD key.
187 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100188 * \param alg An AEAD algorithm
189 * (\c PSA_ALG_XXX value such that
190 * #PSA_ALG_IS_AEAD(\p alg) is true).
191 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200192 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100193 * If the AEAD algorithm does not have an identified
194 * tag that can be distinguished from the rest of
195 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100196 * If the key type or AEAD algorithm is not
197 * recognized, or the parameters are incompatible,
198 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100199 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100200#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200201 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200202 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200203 ((void) (key_bits), 0u))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100204
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200205/** The maximum tag size for all supported AEAD algorithms, in bytes.
206 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100207 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200208 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200209#define PSA_AEAD_TAG_MAX_SIZE 16u
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200210
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200211/* The maximum size of an RSA key on this implementation, in bits.
212 * This is a vendor-specific macro.
213 *
214 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
215 * whose parameters fit in a bignum is accepted. However large keys can
216 * induce a large memory usage and long computation times. Unlike other
217 * auxiliary macros in this file and in crypto.h, which reflect how the
218 * library is configured, this macro defines how the library is
219 * configured. This implementation refuses to import or generate an
220 * RSA key whose size is larger than the value defined here.
221 *
222 * Note that an implementation may set different size limits for different
223 * operations, and does not need to accept all key sizes up to the limit. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200224#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200225
Waleed Elmelegyab570712023-07-05 16:40:58 +0000226/* The minimum size of an RSA key on this implementation, in bits.
227 * This is a vendor-specific macro.
228 *
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000229 * Limits RSA key generation to a minimum due to avoid accidental misuse.
Waleed Elmelegyab570712023-07-05 16:40:58 +0000230 * This value cannot be less than 128 bits.
231 */
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000232#if defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)
233#define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS MBEDTLS_RSA_GEN_KEY_MIN_BITS
Waleed Elmelegyab570712023-07-05 16:40:58 +0000234#else
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000235#define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS 1024
Waleed Elmelegyab570712023-07-05 16:40:58 +0000236#endif
237
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200238/* The maximum size of an DH key on this implementation, in bits.
Przemek Stekieled23b612022-12-01 15:00:41 +0100239 *
240 * Note that an implementation may set different size limits for different
241 * operations, and does not need to accept all key sizes up to the limit. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200242#define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u
Przemek Stekieled23b612022-12-01 15:00:41 +0100243
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200244/* The maximum size of an ECC key on this implementation, in bits.
245 * This is a vendor-specific macro. */
Valerio Setti271c12e2023-03-23 16:30:27 +0100246#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine3c861642023-07-20 15:10:34 +0200247#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u
Valerio Setti271c12e2023-03-23 16:30:27 +0100248#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine3c861642023-07-20 15:10:34 +0200249#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u
Valerio Setti271c12e2023-03-23 16:30:27 +0100250#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine3c861642023-07-20 15:10:34 +0200251#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u
Valerio Setti271c12e2023-03-23 16:30:27 +0100252#elif defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200253#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Valerio Setti271c12e2023-03-23 16:30:27 +0100254#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200255#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Valerio Setti271c12e2023-03-23 16:30:27 +0100256#elif defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200257#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100258#elif defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200259#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100260#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200261#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100262#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine3c861642023-07-20 15:10:34 +0200263#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u
Valerio Setti271c12e2023-03-23 16:30:27 +0100264#elif defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200265#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Valerio Setti271c12e2023-03-23 16:30:27 +0100266#elif defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200267#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Valerio Setti271c12e2023-03-23 16:30:27 +0100268#elif defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine3c861642023-07-20 15:10:34 +0200269#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Valerio Setti271c12e2023-03-23 16:30:27 +0100270#elif defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine3c861642023-07-20 15:10:34 +0200271#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200272#else
Gilles Peskine3c861642023-07-20 15:10:34 +0200273#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200274#endif
275
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100276/** This macro returns the maximum supported length of the PSK for the
277 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100278 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100279 *
280 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100281 *
282 * Quoting RFC 4279, Sect 5.3:
283 * TLS implementations supporting these ciphersuites MUST support
284 * arbitrary PSK identities up to 128 octets in length, and arbitrary
285 * PSKs up to 64 octets in length. Supporting longer identities and
286 * keys is RECOMMENDED.
287 *
288 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100289 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100290 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200291#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u
Hanno Becker8dbfca42018-10-12 11:56:55 +0100292
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400293/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
294 * which is expected to work with P-256 curve only. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200295#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400296
297/* The size of a serialized K.X coordinate to be used in
298 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
299 * curve. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200300#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400301
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530302/* The maximum number of iterations for PBKDF2 on this implementation, in bits.
303 * This is a vendor-specific macro. This can be configured if necessary */
Gilles Peskine3c861642023-07-20 15:10:34 +0200304#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530305
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100306/** The maximum size of a block cipher. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200307#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200308
Gilles Peskineacd4be32018-07-08 19:56:25 +0200309/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200310 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200311 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200312 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100313 * \warning This macro may evaluate its arguments multiple times or
314 * zero times, so you should not pass arguments that contain
315 * side effects.
316 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200317 * \param key_type The type of the MAC key.
318 * \param key_bits The size of the MAC key in bits.
319 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100320 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200321 *
322 * \return The MAC size for the specified algorithm with
323 * the specified key parameters.
324 * \return 0 if the MAC algorithm is not recognized.
325 * \return Either 0 or the correct size for a MAC algorithm that
326 * the implementation recognizes, but does not support.
327 * \return Unspecified if the key parameters are not consistent
328 * with the algorithm.
329 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100330#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
331 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
332 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
333 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200334 ((void) (key_type), (void) (key_bits), 0u))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200335
336/** The maximum size of the output of psa_aead_encrypt(), in bytes.
337 *
338 * If the size of the ciphertext buffer is at least this large, it is
339 * guaranteed that psa_aead_encrypt() will not fail due to an
340 * insufficient buffer size. Depending on the algorithm, the actual size of
341 * the ciphertext may be smaller.
342 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100343 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
344 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100345 * \warning This macro may evaluate its arguments multiple times or
346 * zero times, so you should not pass arguments that contain
347 * side effects.
348 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100349 * \param key_type A symmetric key type that is
350 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200351 * \param alg An AEAD algorithm
352 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100353 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200354 * \param plaintext_length Size of the plaintext in bytes.
355 *
356 * \return The AEAD ciphertext size for the specified
357 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100358 * If the key type or AEAD algorithm is not
359 * recognized, or the parameters are incompatible,
360 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200361 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100362#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200363 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200364 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200365 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200366
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200367/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
368 * supported key types and AEAD algorithms.
369 *
370 * If the size of the ciphertext buffer is at least this large, it is guaranteed
371 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
372 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100373 * \note This macro returns a compile-time constant if its arguments are
374 * compile-time constants.
375 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100376 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
377 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200378 *
379 * \param plaintext_length Size of the plaintext in bytes.
380 *
381 * \return A sufficient output buffer size for any of the
382 * supported key types and AEAD algorithms.
383 *
384 */
385#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
386 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
387
388
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200389/** The maximum size of the output of psa_aead_decrypt(), in bytes.
390 *
391 * If the size of the plaintext buffer is at least this large, it is
392 * guaranteed that psa_aead_decrypt() will not fail due to an
393 * insufficient buffer size. Depending on the algorithm, the actual size of
394 * the plaintext may be smaller.
395 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100396 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
397 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100398 * \warning This macro may evaluate its arguments multiple times or
399 * zero times, so you should not pass arguments that contain
400 * side effects.
401 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100402 * \param key_type A symmetric key type that is
403 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200404 * \param alg An AEAD algorithm
405 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100406 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200407 * \param ciphertext_length Size of the plaintext in bytes.
408 *
409 * \return The AEAD ciphertext size for the specified
410 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100411 * If the key type or AEAD algorithm is not
412 * recognized, or the parameters are incompatible,
413 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200414 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100415#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200416 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
418 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200419 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200420
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200421/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
422 * supported key types and AEAD algorithms.
423 *
424 * If the size of the plaintext buffer is at least this large, it is guaranteed
425 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
426 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100427 * \note This macro returns a compile-time constant if its arguments are
428 * compile-time constants.
429 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100430 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
431 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200432 *
433 * \param ciphertext_length Size of the ciphertext in bytes.
434 *
435 * \return A sufficient output buffer size for any of the
436 * supported key types and AEAD algorithms.
437 *
438 */
439#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200441
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100442/** The default nonce size for an AEAD algorithm, in bytes.
443 *
444 * This macro can be used to allocate a buffer of sufficient size to
445 * store the nonce output from #psa_aead_generate_nonce().
446 *
447 * See also #PSA_AEAD_NONCE_MAX_SIZE.
448 *
449 * \note This is not the maximum size of nonce supported as input to
450 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
451 * just the default size that is generated by #psa_aead_generate_nonce().
452 *
453 * \warning This macro may evaluate its arguments multiple times or
454 * zero times, so you should not pass arguments that contain
455 * side effects.
456 *
457 * \param key_type A symmetric key type that is compatible with
458 * algorithm \p alg.
459 *
460 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
461 * #PSA_ALG_IS_AEAD(\p alg) is true).
462 *
463 * \return The default nonce size for the specified key type and algorithm.
464 * If the key type or AEAD algorithm is not recognized,
465 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100466 */
467#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100468 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine3c861642023-07-20 15:10:34 +0200469 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \
470 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \
471 0u : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100472 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine3c861642023-07-20 15:10:34 +0200473 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \
474 0u)
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100475
476/** The maximum default nonce size among all supported pairs of key types and
477 * AEAD algorithms, in bytes.
478 *
479 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
480 * may return.
481 *
482 * \note This is not the maximum size of nonce supported as input to
483 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
484 * just the largest size that may be generated by
485 * #psa_aead_generate_nonce().
486 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200487#define PSA_AEAD_NONCE_MAX_SIZE 13u
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100488
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200489/** A sufficient output buffer size for psa_aead_update().
490 *
491 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200492 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200493 * insufficient buffer size. The actual size of the output may be smaller
494 * in any given call.
495 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100496 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
497 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100498 * \warning This macro may evaluate its arguments multiple times or
499 * zero times, so you should not pass arguments that contain
500 * side effects.
501 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100502 * \param key_type A symmetric key type that is
503 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200504 * \param alg An AEAD algorithm
505 * (\c PSA_ALG_XXX value such that
506 * #PSA_ALG_IS_AEAD(\p alg) is true).
507 * \param input_length Size of the input in bytes.
508 *
509 * \return A sufficient output buffer size for the specified
510 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100511 * If the key type or AEAD algorithm is not
512 * recognized, or the parameters are incompatible,
513 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200514 */
515/* For all the AEAD modes defined in this specification, it is possible
516 * to emit output without delay. However, hardware may not always be
517 * capable of this. So for modes based on a block cipher, allow the
518 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100519#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200520 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
522 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
523 (input_length) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200524 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200525
526/** A sufficient output buffer size for psa_aead_update(), for any of the
527 * supported key types and AEAD algorithms.
528 *
529 * If the size of the output buffer is at least this large, it is guaranteed
530 * that psa_aead_update() will not fail due to an insufficient buffer size.
531 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100532 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200533 *
534 * \param input_length Size of the input in bytes.
535 */
536#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
537 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200538
539/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200540 *
541 * If the size of the ciphertext buffer is at least this large, it is
542 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200543 * insufficient ciphertext buffer size. The actual size of the output may
544 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200545 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100546 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
547 *
548 * \param key_type A symmetric key type that is
549 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200550 * \param alg An AEAD algorithm
551 * (\c PSA_ALG_XXX value such that
552 * #PSA_ALG_IS_AEAD(\p alg) is true).
553 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200554 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200555 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100556 * If the key type or AEAD algorithm is not
557 * recognized, or the parameters are incompatible,
558 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200559 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200560#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
561 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
563 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200564 0u)
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200565
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200566/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
567 * supported key types and AEAD algorithms.
568 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100569 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200570 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200571#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200572
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200573/** A sufficient plaintext buffer size for psa_aead_verify().
574 *
575 * If the size of the plaintext buffer is at least this large, it is
576 * guaranteed that psa_aead_verify() will not fail due to an
577 * insufficient plaintext buffer size. The actual size of the output may
578 * be smaller in any given call.
579 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100580 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
581 *
582 * \param key_type A symmetric key type that is
583 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200584 * \param alg An AEAD algorithm
585 * (\c PSA_ALG_XXX value such that
586 * #PSA_ALG_IS_AEAD(\p alg) is true).
587 *
588 * \return A sufficient plaintext buffer size for the
589 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100590 * If the key type or AEAD algorithm is not
591 * recognized, or the parameters are incompatible,
592 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200593 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200594#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
595 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
597 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200598 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200599
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200600/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
601 * supported key types and AEAD algorithms.
602 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100603 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200604 */
605#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
606
Jaeden Amero7f042142019-02-07 10:44:38 +0000607#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
608 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Gilles Peskine935ff232023-08-09 19:48:02 +0200609 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200610 11u /*PKCS#1v1.5*/)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100611
612/**
613 * \brief ECDSA signature size for a given curve bit size
614 *
615 * \param curve_bits Curve size in bits.
616 * \return Signature size in bytes.
617 *
618 * \note This macro returns a compile-time constant if its argument is one.
619 */
620#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
Gilles Peskine935ff232023-08-09 19:48:02 +0200621 (PSA_BITS_TO_BYTES(curve_bits) * 2u)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100622
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100623/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200624 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200625 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200626 * of the specified type and size, with the specified algorithm.
627 * Note that the actual size of the signature may be smaller
628 * (some algorithms produce a variable-size signature).
629 *
630 * \warning This function may call its arguments multiple times or
631 * zero times, so you should not pass arguments that contain
632 * side effects.
633 *
634 * \param key_type An asymmetric key type (this may indifferently be a
635 * key pair type or a public key type).
636 * \param key_bits The size of the key in bits.
637 * \param alg The signature algorithm.
638 *
639 * \return If the parameters are valid and supported, return
640 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100641 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200642 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100643 * If the parameters are a valid combination that is not supported,
644 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200645 * If the parameters are not valid, the
646 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200647 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100648#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200650 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200651 ((void) alg, 0u))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200652
Gilles Peskine29755712019-11-08 15:49:40 +0100653#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
654 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
655
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100656/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100657 *
658 * Maximum size of an asymmetric signature.
659 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100660 * This macro expands to a compile-time constant integer. This value
661 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100662 */
Valerio Settic012a2d2023-07-28 09:34:44 +0200663#define PSA_SIGNATURE_MAX_SIZE 1
Valerio Settia83d9bf2023-07-27 18:15:20 +0200664
Valerio Setti43c5bf42023-07-31 11:06:50 +0200665#if (defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +0200666 (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE > PSA_SIGNATURE_MAX_SIZE)
667#undef PSA_SIGNATURE_MAX_SIZE
668#define PSA_SIGNATURE_MAX_SIZE PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE
669#endif
Valerio Setti43c5bf42023-07-31 11:06:50 +0200670#if (defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) || defined(PSA_WANT_ALG_RSA_PSS)) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +0200671 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_SIGNATURE_MAX_SIZE)
672#undef PSA_SIGNATURE_MAX_SIZE
673#define PSA_SIGNATURE_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)
674#endif
Gilles Peskine29755712019-11-08 15:49:40 +0100675
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200676/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200677 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200678 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200679 * a key of the specified type and size, with the specified algorithm.
680 * Note that the actual size of the ciphertext may be smaller, depending
681 * on the algorithm.
682 *
683 * \warning This function may call its arguments multiple times or
684 * zero times, so you should not pass arguments that contain
685 * side effects.
686 *
687 * \param key_type An asymmetric key type (this may indifferently be a
688 * key pair type or a public key type).
689 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200690 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200691 *
692 * \return If the parameters are valid and supported, return
693 * a buffer size in bytes that guarantees that
694 * psa_asymmetric_encrypt() will not fail with
695 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100696 * If the parameters are a valid combination that is not supported,
697 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200698 * If the parameters are not valid, the
699 * return value is unspecified.
700 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200701#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
702 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200704 0u)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200705
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200706/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
707 * supported asymmetric encryption.
708 *
709 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
710 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100711/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200712#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100713 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200714
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200715/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200716 *
Gilles Peskine76689602020-05-05 16:01:22 +0200717 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200718 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200719 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200720 * on the algorithm.
721 *
722 * \warning This function may call its arguments multiple times or
723 * zero times, so you should not pass arguments that contain
724 * side effects.
725 *
726 * \param key_type An asymmetric key type (this may indifferently be a
727 * key pair type or a public key type).
728 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200729 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200730 *
731 * \return If the parameters are valid and supported, return
732 * a buffer size in bytes that guarantees that
733 * psa_asymmetric_decrypt() will not fail with
734 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100735 * If the parameters are a valid combination that is not supported,
736 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200737 * If the parameters are not valid, the
738 * return value is unspecified.
739 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200740#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
741 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
742 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200743 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200744
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200745/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
746 * supported asymmetric decryption.
747 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100748 * This macro assumes that RSA is the only supported asymmetric encryption.
749 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200750 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
751 */
752#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100753 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200754
Gilles Peskine1be949b2018-08-10 19:06:59 +0200755/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
756 * number of bits.
757 *
758 * This definition assumes that bits <= 2^19 - 9 so that the length field
759 * is at most 3 bytes. The length of the encoding is the length of the
760 * bit string padded to a whole number of bytes plus:
761 * - 1 type byte;
762 * - 1 to 3 length bytes;
763 * - 0 to 1 bytes of leading 0 due to the sign bit.
764 */
765#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200766 ((bits) / 8u + 5u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200767
768/* Maximum size of the export encoding of an RSA public key.
769 * Assumes that the public exponent is less than 2^32.
770 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200771 * RSAPublicKey ::= SEQUENCE {
772 * modulus INTEGER, -- n
773 * publicExponent INTEGER } -- e
774 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000775 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200776 * - n : INTEGER;
777 * - 7 bytes for the public exponent.
778 */
779#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200780 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200781
782/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000783 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200784 * difference between the two primes is at most 1 bit.
785 *
786 * RSAPrivateKey ::= SEQUENCE {
787 * version Version, -- 0
788 * modulus INTEGER, -- N-bit
789 * publicExponent INTEGER, -- 32-bit
790 * privateExponent INTEGER, -- N-bit
791 * prime1 INTEGER, -- N/2-bit
792 * prime2 INTEGER, -- N/2-bit
793 * exponent1 INTEGER, -- N/2-bit
794 * exponent2 INTEGER, -- N/2-bit
795 * coefficient INTEGER, -- N/2-bit
796 * }
797 *
798 * - 4 bytes of SEQUENCE overhead;
799 * - 3 bytes of version;
800 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
801 * overapproximated as 9 half-size INTEGERS;
802 * - 7 bytes for the public exponent.
803 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200804#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200805 (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200806
807/* Maximum size of the export encoding of a DSA public key.
808 *
809 * SubjectPublicKeyInfo ::= SEQUENCE {
810 * algorithm AlgorithmIdentifier,
811 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
812 * AlgorithmIdentifier ::= SEQUENCE {
813 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400814 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200815 * DSAPublicKey ::= INTEGER -- public key, Y
816 *
817 * - 3 * 4 bytes of SEQUENCE overhead;
818 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
819 * - 4 bytes of BIT STRING overhead;
820 * - 3 full-size INTEGERs (p, g, y);
821 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
822 */
823#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200824 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200825
826/* Maximum size of the export encoding of a DSA key pair.
827 *
828 * DSAPrivateKey ::= SEQUENCE {
829 * version Version, -- 0
830 * prime INTEGER, -- p
831 * subprime INTEGER, -- q
832 * generator INTEGER, -- g
833 * public INTEGER, -- y
834 * private INTEGER, -- x
835 * }
836 *
837 * - 4 bytes of SEQUENCE overhead;
838 * - 3 bytes of version;
839 * - 3 full-size INTEGERs (p, g, y);
840 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
841 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200842#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200843 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200844
845/* Maximum size of the export encoding of an ECC public key.
846 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000847 * The representation of an ECC public key is:
848 * - The byte 0x04;
849 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
850 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
851 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200852 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000853 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200854 */
855#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200856 (2u * PSA_BITS_TO_BYTES(key_bits) + 1u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200857
858/* Maximum size of the export encoding of an ECC key pair.
859 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100860 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200861 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200862#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100863 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200864
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200865/* Maximum size of the export encoding of an DH key pair.
Przemek Stekieled23b612022-12-01 15:00:41 +0100866 *
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200867 * An DH key pair is represented by the secret value.
Przemek Stekieled23b612022-12-01 15:00:41 +0100868 */
869#define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \
870 (PSA_BITS_TO_BYTES(key_bits))
871
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200872/* Maximum size of the export encoding of an DH public key.
Przemek Stekieled23b612022-12-01 15:00:41 +0100873 */
874#define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \
875 (PSA_BITS_TO_BYTES(key_bits))
876
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100877/** Sufficient output buffer size for psa_export_key() or
878 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200879 *
880 * This macro returns a compile-time constant if its arguments are
881 * compile-time constants.
882 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100883 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200884 * zero times, so you should not pass arguments that contain
885 * side effects.
886 *
887 * The following code illustrates how to allocate enough memory to export
888 * a key by querying the key type and size at runtime.
889 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200890 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200891 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200892 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200893 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200894 * psa_key_type_t key_type = psa_get_key_type(&attributes);
895 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100896 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200897 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200898 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200899 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200900 * size_t buffer_length;
901 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
902 * if (status != PSA_SUCCESS) handle_error(...);
903 * \endcode
904 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200905 * \param key_type A supported key type.
906 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200907 *
908 * \return If the parameters are valid and supported, return
909 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100910 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200911 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100912 * If the parameters are a valid combination that is not supported,
913 * return either a sensible size or 0.
914 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200915 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100916#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
917 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100918 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100919 (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 +0200920 (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 +0100921 (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 +0200922 (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 +0100923 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
924 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200925 0u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200926
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200927/** Sufficient output buffer size for psa_export_public_key().
928 *
929 * This macro returns a compile-time constant if its arguments are
930 * compile-time constants.
931 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100932 * \warning This macro may evaluate its arguments multiple times or
933 * zero times, so you should not pass arguments that contain
934 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200935 *
936 * The following code illustrates how to allocate enough memory to export
937 * a public key by querying the key type and size at runtime.
938 * \code{c}
939 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
940 * psa_status_t status;
941 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100942 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200943 * psa_key_type_t key_type = psa_get_key_type(&attributes);
944 * size_t key_bits = psa_get_key_bits(&attributes);
945 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
946 * psa_reset_key_attributes(&attributes);
947 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100948 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200949 * size_t buffer_length;
950 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100951 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200952 * \endcode
953 *
954 * \param key_type A public key or key pair key type.
955 * \param key_bits The size of the key in bits.
956 *
957 * \return If the parameters are valid and supported, return
958 * a buffer size in bytes that guarantees that
959 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100960 * #PSA_ERROR_BUFFER_TOO_SMALL.
961 * If the parameters are a valid combination that is not
962 * supported, return either a sensible size or 0.
963 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200964 * the return value is unspecified.
965 *
966 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100967 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200968 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
969 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
970 * \p key_bits).
971 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100972#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
973 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
974 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100975 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200976 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200977
978/** Sufficient buffer size for exporting any asymmetric key pair.
979 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100980 * This macro expands to a compile-time constant integer. This value is
981 * a sufficient buffer size when calling psa_export_key() to export any
982 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200983 *
984 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
985 */
Valerio Settic012a2d2023-07-28 09:34:44 +0200986#define PSA_EXPORT_KEY_PAIR_MAX_SIZE 1
Valerio Settia83d9bf2023-07-27 18:15:20 +0200987
988#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) && \
989 (PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
990 PSA_EXPORT_KEY_PAIR_MAX_SIZE)
991#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
992#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
993 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
994#endif
995#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) && \
996 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
997 PSA_EXPORT_KEY_PAIR_MAX_SIZE)
998#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
999#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
1000 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
1001#endif
1002#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_BASIC) && \
1003 (PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \
1004 PSA_EXPORT_KEY_PAIR_MAX_SIZE)
1005#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
1006#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
Valerio Setti644e01d2023-07-28 09:31:51 +02001007 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
Valerio Settia83d9bf2023-07-27 18:15:20 +02001008#endif
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001009
1010/** Sufficient buffer size for exporting any asymmetric public key.
1011 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001012 * This macro expands to a compile-time constant integer. This value is
1013 * a sufficient buffer size when calling psa_export_key() or
1014 * psa_export_public_key() to export any asymmetric public key,
1015 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001016 *
1017 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
1018 */
Valerio Settic012a2d2023-07-28 09:34:44 +02001019#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1
Przemek Stekiel654bef02022-12-15 13:28:02 +01001020
Valerio Settia83d9bf2023-07-27 18:15:20 +02001021#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \
1022 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
1023 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1024#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
1025#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1026 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
1027#endif
1028#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) && \
1029 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
1030 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1031#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
1032#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1033 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
1034#endif
1035#if defined(PSA_WANT_KEY_TYPE_DH_PUBLIC_KEY) && \
1036 (PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \
1037 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1038#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
1039#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1040 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
1041#endif
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001042
1043/** Sufficient output buffer size for psa_raw_key_agreement().
1044 *
1045 * This macro returns a compile-time constant if its arguments are
1046 * compile-time constants.
1047 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001048 * \warning This macro may evaluate its arguments multiple times or
1049 * zero times, so you should not pass arguments that contain
1050 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001051 *
1052 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
1053 *
1054 * \param key_type A supported key type.
1055 * \param key_bits The size of the key in bits.
1056 *
1057 * \return If the parameters are valid and supported, return
1058 * a buffer size in bytes that guarantees that
1059 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001060 * #PSA_ERROR_BUFFER_TOO_SMALL.
1061 * If the parameters are a valid combination that
1062 * is not supported, return either a sensible size or 0.
1063 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001064 * the return value is unspecified.
1065 */
1066#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
Przemek Stekiel654bef02022-12-15 13:28:02 +01001067 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \
Gilles Peskine3c861642023-07-20 15:10:34 +02001068 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001069
1070/** Maximum size of the output from psa_raw_key_agreement().
1071 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001072 * This macro expands to a compile-time constant integer. This value is the
1073 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001074 *
1075 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
1076 */
Valerio Settic012a2d2023-07-28 09:34:44 +02001077#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1
Valerio Settia83d9bf2023-07-27 18:15:20 +02001078
Valerio Setti43c5bf42023-07-31 11:06:50 +02001079#if defined(PSA_WANT_ALG_ECDH) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +02001080 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE)
1081#undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE
1082#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)
1083#endif
Valerio Setti43c5bf42023-07-31 11:06:50 +02001084#if defined(PSA_WANT_ALG_FFDH) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +02001085 (PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE)
1086#undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE
1087#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS)
1088#endif
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001089
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001090/** The default IV size for a cipher algorithm, in bytes.
1091 *
1092 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
1093 * the default IV length for the algorithm.
1094 *
1095 * This macro can be used to allocate a buffer of sufficient size to
1096 * store the IV output from #psa_cipher_generate_iv() when using
1097 * a multi-part cipher operation.
1098 *
1099 * See also #PSA_CIPHER_IV_MAX_SIZE.
1100 *
1101 * \warning This macro may evaluate its arguments multiple times or
1102 * zero times, so you should not pass arguments that contain
1103 * side effects.
1104 *
1105 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
1106 *
1107 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
1108 *
1109 * \return The default IV size for the specified key type and algorithm.
1110 * If the algorithm does not use an IV, return 0.
1111 * If the key type or cipher algorithm is not recognized,
1112 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001113 */
1114#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001115 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 ((alg) == PSA_ALG_CTR || \
1117 (alg) == PSA_ALG_CFB || \
1118 (alg) == PSA_ALG_OFB || \
1119 (alg) == PSA_ALG_XTS || \
1120 (alg) == PSA_ALG_CBC_NO_PADDING || \
1121 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001122 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine3c861642023-07-20 15:10:34 +02001123 (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \
1124 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \
1125 0u)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001126
1127/** The maximum IV size for all supported cipher algorithms, in bytes.
1128 *
1129 * See also #PSA_CIPHER_IV_LENGTH().
1130 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001131#define PSA_CIPHER_IV_MAX_SIZE 16u
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001132
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001133/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1134 *
1135 * If the size of the output buffer is at least this large, it is guaranteed
1136 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1137 * Depending on the algorithm, the actual size of the output might be smaller.
1138 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001139 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1140 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001141 * \warning This macro may evaluate its arguments multiple times or
1142 * zero times, so you should not pass arguments that contain
1143 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001144 *
1145 * \param key_type A symmetric key type that is compatible with algorithm
1146 * alg.
1147 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1148 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1149 * \param input_length Size of the input in bytes.
1150 *
1151 * \return A sufficient output size for the specified key type and
1152 * algorithm. If the key type or cipher algorithm is not
1153 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001154 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001155 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001156#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1157 (alg == PSA_ALG_CBC_PKCS7 ? \
1158 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1159 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1160 (input_length) + 1u) + \
1161 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \
1162 (PSA_ALG_IS_CIPHER(alg) ? \
1163 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1164 0u))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001165
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001166/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1167 * supported key types and cipher algorithms.
1168 *
1169 * If the size of the output buffer is at least this large, it is guaranteed
1170 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1171 *
1172 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1173 *
1174 * \param input_length Size of the input in bytes.
1175 *
1176 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001177#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1178 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1179 (input_length) + 1u) + \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001180 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001181
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001182/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1183 *
1184 * If the size of the output buffer is at least this large, it is guaranteed
1185 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1186 * Depending on the algorithm, the actual size of the output might be smaller.
1187 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001188 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001189 *
1190 * \param key_type A symmetric key type that is compatible with algorithm
1191 * alg.
1192 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1193 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1194 * \param input_length Size of the input in bytes.
1195 *
1196 * \return A sufficient output size for the specified key type and
1197 * algorithm. If the key type or cipher algorithm is not
1198 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001199 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001200 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001201#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1202 (PSA_ALG_IS_CIPHER(alg) && \
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001203 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Gilles Peskine3c861642023-07-20 15:10:34 +02001204 (input_length) : \
1205 0u)
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001206
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001207/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1208 * supported key types and cipher algorithms.
1209 *
1210 * If the size of the output buffer is at least this large, it is guaranteed
1211 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1212 *
1213 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1214 *
1215 * \param input_length Size of the input in bytes.
1216 */
1217#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1218 (input_length)
1219
1220/** A sufficient output buffer size for psa_cipher_update().
1221 *
1222 * If the size of the output buffer is at least this large, it is guaranteed
1223 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1224 * The actual size of the output might be smaller in any given call.
1225 *
1226 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1227 *
1228 * \param key_type A symmetric key type that is compatible with algorithm
1229 * alg.
1230 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1231 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1232 * \param input_length Size of the input in bytes.
1233 *
1234 * \return A sufficient output size for the specified key type and
1235 * algorithm. If the key type or cipher algorithm is not
1236 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001237 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001238#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1239 (PSA_ALG_IS_CIPHER(alg) ? \
1240 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1241 (((alg) == PSA_ALG_CBC_PKCS7 || \
1242 (alg) == PSA_ALG_CBC_NO_PADDING || \
1243 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1244 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1245 input_length) : \
1246 (input_length)) : 0u) : \
1247 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001248
1249/** A sufficient output buffer size for psa_cipher_update(), for any of the
1250 * supported key types and cipher algorithms.
1251 *
1252 * If the size of the output buffer is at least this large, it is guaranteed
1253 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1254 *
1255 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1256 *
1257 * \param input_length Size of the input in bytes.
1258 */
1259#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001260 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001261
1262/** A sufficient ciphertext buffer size for psa_cipher_finish().
1263 *
1264 * If the size of the ciphertext buffer is at least this large, it is
1265 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1266 * ciphertext buffer size. The actual size of the output might be smaller in
1267 * any given call.
1268 *
1269 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1270 *
1271 * \param key_type A symmetric key type that is compatible with algorithm
1272 * alg.
1273 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1274 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1275 * \return A sufficient output size for the specified key type and
1276 * algorithm. If the key type or cipher algorithm is not
1277 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001278 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001279#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1280 (PSA_ALG_IS_CIPHER(alg) ? \
1281 (alg == PSA_ALG_CBC_PKCS7 ? \
1282 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +02001283 0u) : \
1284 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001285
1286/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1287 * supported key types and cipher algorithms.
1288 *
1289 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1290 */
1291#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1292 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1293
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001294#endif /* PSA_CRYPTO_SIZES_H */