blob: 0c80dabc3212daba01b379d390b88d0eaee62d0a [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 */
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. */
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. */
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200133#if defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine3c861642023-07-20 15:10:34 +0200134#define PSA_HASH_MAX_SIZE 64u
135#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200136#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200137#define PSA_HASH_MAX_SIZE 48u
138#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200139#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200140#define PSA_HASH_MAX_SIZE 32u
141#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200142#elif defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200143#define PSA_HASH_MAX_SIZE 28u
144#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200145#else /* SHA-1 or smaller */
Gilles Peskine3c861642023-07-20 15:10:34 +0200146#define PSA_HASH_MAX_SIZE 20u
147#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200148#endif
149
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200150/** \def PSA_MAC_MAX_SIZE
151 *
152 * Maximum size of a MAC.
153 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100154 * This macro expands to a compile-time constant integer. This value
155 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200156 */
157/* All non-HMAC MACs have a maximum size that's smaller than the
158 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200159/* Note that the encoding of truncated MAC algorithms limits this value
160 * to 64 bytes.
161 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200162#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
163
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100164/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100165 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100166 * This macro can be used to allocate a buffer of sufficient size to store the
167 * tag output from psa_aead_finish().
168 *
169 * See also #PSA_AEAD_TAG_MAX_SIZE.
170 *
171 * \param key_type The type of the AEAD key.
172 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100173 * \param alg An AEAD algorithm
174 * (\c PSA_ALG_XXX value such that
175 * #PSA_ALG_IS_AEAD(\p alg) is true).
176 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200177 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100178 * If the AEAD algorithm does not have an identified
179 * tag that can be distinguished from the rest of
180 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100181 * If the key type or AEAD algorithm is not
182 * recognized, or the parameters are incompatible,
183 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100184 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100185#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200186 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200187 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200188 ((void) (key_bits), 0u))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100189
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200190/** The maximum tag size for all supported AEAD algorithms, in bytes.
191 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100192 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200193 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200194#define PSA_AEAD_TAG_MAX_SIZE 16u
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200195
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200196/* The maximum size of an RSA key on this implementation, in bits.
197 * This is a vendor-specific macro.
198 *
199 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
200 * whose parameters fit in a bignum is accepted. However large keys can
201 * induce a large memory usage and long computation times. Unlike other
202 * auxiliary macros in this file and in crypto.h, which reflect how the
203 * library is configured, this macro defines how the library is
204 * configured. This implementation refuses to import or generate an
205 * RSA key whose size is larger than the value defined here.
206 *
207 * Note that an implementation may set different size limits for different
208 * operations, and does not need to accept all key sizes up to the limit. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200209#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200210
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200211/* The maximum size of an DH key on this implementation, in bits.
Przemek Stekieled23b612022-12-01 15:00:41 +0100212 *
213 * Note that an implementation may set different size limits for different
214 * operations, and does not need to accept all key sizes up to the limit. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200215#define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u
Przemek Stekieled23b612022-12-01 15:00:41 +0100216
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200217/* The maximum size of an ECC key on this implementation, in bits.
218 * This is a vendor-specific macro. */
Valerio Setti271c12e2023-03-23 16:30:27 +0100219#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine3c861642023-07-20 15:10:34 +0200220#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u
Valerio Setti271c12e2023-03-23 16:30:27 +0100221#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine3c861642023-07-20 15:10:34 +0200222#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u
Valerio Setti271c12e2023-03-23 16:30:27 +0100223#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine3c861642023-07-20 15:10:34 +0200224#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u
Valerio Setti271c12e2023-03-23 16:30:27 +0100225#elif defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200226#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Valerio Setti271c12e2023-03-23 16:30:27 +0100227#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200228#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Valerio Setti271c12e2023-03-23 16:30:27 +0100229#elif defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200230#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100231#elif defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200232#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100233#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200234#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100235#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine3c861642023-07-20 15:10:34 +0200236#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u
Valerio Setti271c12e2023-03-23 16:30:27 +0100237#elif defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200238#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Valerio Setti271c12e2023-03-23 16:30:27 +0100239#elif defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200240#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Valerio Setti271c12e2023-03-23 16:30:27 +0100241#elif defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine3c861642023-07-20 15:10:34 +0200242#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Valerio Setti271c12e2023-03-23 16:30:27 +0100243#elif defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine3c861642023-07-20 15:10:34 +0200244#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200245#else
Gilles Peskine3c861642023-07-20 15:10:34 +0200246#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200247#endif
248
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100249/** This macro returns the maximum supported length of the PSK for the
250 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100251 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100252 *
253 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100254 *
255 * Quoting RFC 4279, Sect 5.3:
256 * TLS implementations supporting these ciphersuites MUST support
257 * arbitrary PSK identities up to 128 octets in length, and arbitrary
258 * PSKs up to 64 octets in length. Supporting longer identities and
259 * keys is RECOMMENDED.
260 *
261 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100262 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100263 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200264#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u
Hanno Becker8dbfca42018-10-12 11:56:55 +0100265
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400266/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
267 * which is expected to work with P-256 curve only. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200268#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400269
270/* The size of a serialized K.X coordinate to be used in
271 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
272 * curve. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200273#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400274
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530275/* The maximum number of iterations for PBKDF2 on this implementation, in bits.
276 * This is a vendor-specific macro. This can be configured if necessary */
Gilles Peskine3c861642023-07-20 15:10:34 +0200277#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530278
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100279/** The maximum size of a block cipher. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200280#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200281
Gilles Peskineacd4be32018-07-08 19:56:25 +0200282/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200283 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200284 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200285 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100286 * \warning This macro may evaluate its arguments multiple times or
287 * zero times, so you should not pass arguments that contain
288 * side effects.
289 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200290 * \param key_type The type of the MAC key.
291 * \param key_bits The size of the MAC key in bits.
292 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100293 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200294 *
295 * \return The MAC size for the specified algorithm with
296 * the specified key parameters.
297 * \return 0 if the MAC algorithm is not recognized.
298 * \return Either 0 or the correct size for a MAC algorithm that
299 * the implementation recognizes, but does not support.
300 * \return Unspecified if the key parameters are not consistent
301 * with the algorithm.
302 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100303#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
304 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
305 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
306 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200307 ((void) (key_type), (void) (key_bits), 0u))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200308
309/** The maximum size of the output of psa_aead_encrypt(), in bytes.
310 *
311 * If the size of the ciphertext buffer is at least this large, it is
312 * guaranteed that psa_aead_encrypt() will not fail due to an
313 * insufficient buffer size. Depending on the algorithm, the actual size of
314 * the ciphertext may be smaller.
315 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100316 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
317 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100318 * \warning This macro may evaluate its arguments multiple times or
319 * zero times, so you should not pass arguments that contain
320 * side effects.
321 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100322 * \param key_type A symmetric key type that is
323 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200324 * \param alg An AEAD algorithm
325 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100326 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200327 * \param plaintext_length Size of the plaintext in bytes.
328 *
329 * \return The AEAD ciphertext size for the specified
330 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100331 * If the key type or AEAD algorithm is not
332 * recognized, or the parameters are incompatible,
333 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200334 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100335#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200336 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200337 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200338 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200339
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200340/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
341 * supported key types and AEAD algorithms.
342 *
343 * If the size of the ciphertext buffer is at least this large, it is guaranteed
344 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
345 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100346 * \note This macro returns a compile-time constant if its arguments are
347 * compile-time constants.
348 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100349 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
350 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200351 *
352 * \param plaintext_length Size of the plaintext in bytes.
353 *
354 * \return A sufficient output buffer size for any of the
355 * supported key types and AEAD algorithms.
356 *
357 */
358#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
359 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
360
361
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200362/** The maximum size of the output of psa_aead_decrypt(), in bytes.
363 *
364 * If the size of the plaintext buffer is at least this large, it is
365 * guaranteed that psa_aead_decrypt() will not fail due to an
366 * insufficient buffer size. Depending on the algorithm, the actual size of
367 * the plaintext may be smaller.
368 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100369 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
370 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100371 * \warning This macro may evaluate its arguments multiple times or
372 * zero times, so you should not pass arguments that contain
373 * side effects.
374 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100375 * \param key_type A symmetric key type that is
376 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200377 * \param alg An AEAD algorithm
378 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100379 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200380 * \param ciphertext_length Size of the plaintext in bytes.
381 *
382 * \return The AEAD ciphertext size for the specified
383 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100384 * If the key type or AEAD algorithm is not
385 * recognized, or the parameters are incompatible,
386 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200387 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100388#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200389 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
391 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200392 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200393
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200394/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
395 * supported key types and AEAD algorithms.
396 *
397 * If the size of the plaintext buffer is at least this large, it is guaranteed
398 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
399 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100400 * \note This macro returns a compile-time constant if its arguments are
401 * compile-time constants.
402 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100403 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
404 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200405 *
406 * \param ciphertext_length Size of the ciphertext in bytes.
407 *
408 * \return A sufficient output buffer size for any of the
409 * supported key types and AEAD algorithms.
410 *
411 */
412#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200414
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100415/** The default nonce size for an AEAD algorithm, in bytes.
416 *
417 * This macro can be used to allocate a buffer of sufficient size to
418 * store the nonce output from #psa_aead_generate_nonce().
419 *
420 * See also #PSA_AEAD_NONCE_MAX_SIZE.
421 *
422 * \note This is not the maximum size of nonce supported as input to
423 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
424 * just the default size that is generated by #psa_aead_generate_nonce().
425 *
426 * \warning This macro may evaluate its arguments multiple times or
427 * zero times, so you should not pass arguments that contain
428 * side effects.
429 *
430 * \param key_type A symmetric key type that is compatible with
431 * algorithm \p alg.
432 *
433 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
434 * #PSA_ALG_IS_AEAD(\p alg) is true).
435 *
436 * \return The default nonce size for the specified key type and algorithm.
437 * If the key type or AEAD algorithm is not recognized,
438 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100439 */
440#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100441 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine3c861642023-07-20 15:10:34 +0200442 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \
443 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \
444 0u : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100445 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine3c861642023-07-20 15:10:34 +0200446 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \
447 0u)
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100448
449/** The maximum default nonce size among all supported pairs of key types and
450 * AEAD algorithms, in bytes.
451 *
452 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
453 * may return.
454 *
455 * \note This is not the maximum size of nonce supported as input to
456 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
457 * just the largest size that may be generated by
458 * #psa_aead_generate_nonce().
459 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200460#define PSA_AEAD_NONCE_MAX_SIZE 13u
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100461
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200462/** A sufficient output buffer size for psa_aead_update().
463 *
464 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200465 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200466 * insufficient buffer size. The actual size of the output may be smaller
467 * in any given call.
468 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100469 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
470 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100471 * \warning This macro may evaluate its arguments multiple times or
472 * zero times, so you should not pass arguments that contain
473 * side effects.
474 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100475 * \param key_type A symmetric key type that is
476 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200477 * \param alg An AEAD algorithm
478 * (\c PSA_ALG_XXX value such that
479 * #PSA_ALG_IS_AEAD(\p alg) is true).
480 * \param input_length Size of the input in bytes.
481 *
482 * \return A sufficient output buffer size for the specified
483 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100484 * If the key type or AEAD algorithm is not
485 * recognized, or the parameters are incompatible,
486 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200487 */
488/* For all the AEAD modes defined in this specification, it is possible
489 * to emit output without delay. However, hardware may not always be
490 * capable of this. So for modes based on a block cipher, allow the
491 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100492#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200493 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
495 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
496 (input_length) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200497 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200498
499/** A sufficient output buffer size for psa_aead_update(), for any of the
500 * supported key types and AEAD algorithms.
501 *
502 * If the size of the output buffer is at least this large, it is guaranteed
503 * that psa_aead_update() will not fail due to an insufficient buffer size.
504 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100505 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200506 *
507 * \param input_length Size of the input in bytes.
508 */
509#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
510 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200511
512/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200513 *
514 * If the size of the ciphertext buffer is at least this large, it is
515 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200516 * insufficient ciphertext buffer size. The actual size of the output may
517 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200518 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100519 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
520 *
521 * \param key_type A symmetric key type that is
522 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200523 * \param alg An AEAD algorithm
524 * (\c PSA_ALG_XXX value such that
525 * #PSA_ALG_IS_AEAD(\p alg) is true).
526 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200527 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200528 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100529 * If the key type or AEAD algorithm is not
530 * recognized, or the parameters are incompatible,
531 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200532 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200533#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
534 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
536 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200537 0u)
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200538
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200539/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
540 * supported key types and AEAD algorithms.
541 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100542 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200543 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200544#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200545
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200546/** A sufficient plaintext buffer size for psa_aead_verify().
547 *
548 * If the size of the plaintext buffer is at least this large, it is
549 * guaranteed that psa_aead_verify() will not fail due to an
550 * insufficient plaintext buffer size. The actual size of the output may
551 * be smaller in any given call.
552 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100553 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
554 *
555 * \param key_type A symmetric key type that is
556 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200557 * \param alg An AEAD algorithm
558 * (\c PSA_ALG_XXX value such that
559 * #PSA_ALG_IS_AEAD(\p alg) is true).
560 *
561 * \return A sufficient plaintext buffer size for the
562 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100563 * If the key type or AEAD algorithm is not
564 * recognized, or the parameters are incompatible,
565 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200566 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200567#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
568 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
570 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200571 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200572
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200573/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
574 * supported key types and AEAD algorithms.
575 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100576 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200577 */
578#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
579
Jaeden Amero7f042142019-02-07 10:44:38 +0000580#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
581 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Gilles Peskine935ff232023-08-09 19:48:02 +0200582 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200583 11u /*PKCS#1v1.5*/)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100584
585/**
586 * \brief ECDSA signature size for a given curve bit size
587 *
588 * \param curve_bits Curve size in bits.
589 * \return Signature size in bytes.
590 *
591 * \note This macro returns a compile-time constant if its argument is one.
592 */
593#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
Gilles Peskine935ff232023-08-09 19:48:02 +0200594 (PSA_BITS_TO_BYTES(curve_bits) * 2u)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100595
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100596/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200597 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200598 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200599 * of the specified type and size, with the specified algorithm.
600 * Note that the actual size of the signature may be smaller
601 * (some algorithms produce a variable-size signature).
602 *
603 * \warning This function may call its arguments multiple times or
604 * zero times, so you should not pass arguments that contain
605 * side effects.
606 *
607 * \param key_type An asymmetric key type (this may indifferently be a
608 * key pair type or a public key type).
609 * \param key_bits The size of the key in bits.
610 * \param alg The signature algorithm.
611 *
612 * \return If the parameters are valid and supported, return
613 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100614 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200615 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100616 * If the parameters are a valid combination that is not supported,
617 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200618 * If the parameters are not valid, the
619 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200620 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100621#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200623 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200624 ((void) alg, 0u))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200625
Gilles Peskine29755712019-11-08 15:49:40 +0100626#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
627 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
628
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100629/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100630 *
631 * Maximum size of an asymmetric signature.
632 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100633 * This macro expands to a compile-time constant integer. This value
634 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100635 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100636#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100637 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
638 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
639 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
640
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200641/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200642 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200643 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 * a key of the specified type and size, with the specified algorithm.
645 * Note that the actual size of the ciphertext may be smaller, depending
646 * on the algorithm.
647 *
648 * \warning This function may call its arguments multiple times or
649 * zero times, so you should not pass arguments that contain
650 * side effects.
651 *
652 * \param key_type An asymmetric key type (this may indifferently be a
653 * key pair type or a public key type).
654 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200655 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200656 *
657 * \return If the parameters are valid and supported, return
658 * a buffer size in bytes that guarantees that
659 * psa_asymmetric_encrypt() will not fail with
660 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100661 * If the parameters are a valid combination that is not supported,
662 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200663 * If the parameters are not valid, the
664 * return value is unspecified.
665 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200666#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
667 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200669 0u)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200670
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200671/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
672 * supported asymmetric encryption.
673 *
674 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
675 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100676/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200677#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100678 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200679
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200680/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200681 *
Gilles Peskine76689602020-05-05 16:01:22 +0200682 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200683 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200684 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200685 * on the algorithm.
686 *
687 * \warning This function may call its arguments multiple times or
688 * zero times, so you should not pass arguments that contain
689 * side effects.
690 *
691 * \param key_type An asymmetric key type (this may indifferently be a
692 * key pair type or a public key type).
693 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200694 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200695 *
696 * \return If the parameters are valid and supported, return
697 * a buffer size in bytes that guarantees that
698 * psa_asymmetric_decrypt() will not fail with
699 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100700 * If the parameters are a valid combination that is not supported,
701 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200702 * If the parameters are not valid, the
703 * return value is unspecified.
704 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200705#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
706 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
707 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200708 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200709
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200710/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
711 * supported asymmetric decryption.
712 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100713 * This macro assumes that RSA is the only supported asymmetric encryption.
714 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200715 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
716 */
717#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100718 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200719
Gilles Peskine1be949b2018-08-10 19:06:59 +0200720/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
721 * number of bits.
722 *
723 * This definition assumes that bits <= 2^19 - 9 so that the length field
724 * is at most 3 bytes. The length of the encoding is the length of the
725 * bit string padded to a whole number of bytes plus:
726 * - 1 type byte;
727 * - 1 to 3 length bytes;
728 * - 0 to 1 bytes of leading 0 due to the sign bit.
729 */
730#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200731 ((bits) / 8u + 5u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200732
733/* Maximum size of the export encoding of an RSA public key.
734 * Assumes that the public exponent is less than 2^32.
735 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200736 * RSAPublicKey ::= SEQUENCE {
737 * modulus INTEGER, -- n
738 * publicExponent INTEGER } -- e
739 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000740 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200741 * - n : INTEGER;
742 * - 7 bytes for the public exponent.
743 */
744#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200745 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200746
747/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000748 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200749 * difference between the two primes is at most 1 bit.
750 *
751 * RSAPrivateKey ::= SEQUENCE {
752 * version Version, -- 0
753 * modulus INTEGER, -- N-bit
754 * publicExponent INTEGER, -- 32-bit
755 * privateExponent INTEGER, -- N-bit
756 * prime1 INTEGER, -- N/2-bit
757 * prime2 INTEGER, -- N/2-bit
758 * exponent1 INTEGER, -- N/2-bit
759 * exponent2 INTEGER, -- N/2-bit
760 * coefficient INTEGER, -- N/2-bit
761 * }
762 *
763 * - 4 bytes of SEQUENCE overhead;
764 * - 3 bytes of version;
765 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
766 * overapproximated as 9 half-size INTEGERS;
767 * - 7 bytes for the public exponent.
768 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200769#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200770 (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200771
772/* Maximum size of the export encoding of a DSA public key.
773 *
774 * SubjectPublicKeyInfo ::= SEQUENCE {
775 * algorithm AlgorithmIdentifier,
776 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
777 * AlgorithmIdentifier ::= SEQUENCE {
778 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400779 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200780 * DSAPublicKey ::= INTEGER -- public key, Y
781 *
782 * - 3 * 4 bytes of SEQUENCE overhead;
783 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
784 * - 4 bytes of BIT STRING overhead;
785 * - 3 full-size INTEGERs (p, g, y);
786 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
787 */
788#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200789 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200790
791/* Maximum size of the export encoding of a DSA key pair.
792 *
793 * DSAPrivateKey ::= SEQUENCE {
794 * version Version, -- 0
795 * prime INTEGER, -- p
796 * subprime INTEGER, -- q
797 * generator INTEGER, -- g
798 * public INTEGER, -- y
799 * private INTEGER, -- x
800 * }
801 *
802 * - 4 bytes of SEQUENCE overhead;
803 * - 3 bytes of version;
804 * - 3 full-size INTEGERs (p, g, y);
805 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
806 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200807#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200808 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809
810/* Maximum size of the export encoding of an ECC public key.
811 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000812 * The representation of an ECC public key is:
813 * - The byte 0x04;
814 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
815 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
816 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200817 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000818 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200819 */
820#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200821 (2u * PSA_BITS_TO_BYTES(key_bits) + 1u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200822
823/* Maximum size of the export encoding of an ECC key pair.
824 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100825 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200826 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200827#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100828 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200829
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200830/* Maximum size of the export encoding of an DH key pair.
Przemek Stekieled23b612022-12-01 15:00:41 +0100831 *
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200832 * An DH key pair is represented by the secret value.
Przemek Stekieled23b612022-12-01 15:00:41 +0100833 */
834#define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \
835 (PSA_BITS_TO_BYTES(key_bits))
836
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200837/* Maximum size of the export encoding of an DH public key.
Przemek Stekieled23b612022-12-01 15:00:41 +0100838 */
839#define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \
840 (PSA_BITS_TO_BYTES(key_bits))
841
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100842/** Sufficient output buffer size for psa_export_key() or
843 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200844 *
845 * This macro returns a compile-time constant if its arguments are
846 * compile-time constants.
847 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100848 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200849 * zero times, so you should not pass arguments that contain
850 * side effects.
851 *
852 * The following code illustrates how to allocate enough memory to export
853 * a key by querying the key type and size at runtime.
854 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200855 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200856 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200857 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200858 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200859 * psa_key_type_t key_type = psa_get_key_type(&attributes);
860 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100861 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200862 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200863 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200864 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200865 * size_t buffer_length;
866 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
867 * if (status != PSA_SUCCESS) handle_error(...);
868 * \endcode
869 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200870 * \param key_type A supported key type.
871 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200872 *
873 * \return If the parameters are valid and supported, return
874 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100875 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200876 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100877 * If the parameters are a valid combination that is not supported,
878 * return either a sensible size or 0.
879 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200880 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100881#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
882 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100883 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100884 (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 +0200885 (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 +0100886 (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 +0200887 (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 +0100888 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
889 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 +0200890 0u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200891
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200892/** Sufficient output buffer size for psa_export_public_key().
893 *
894 * This macro returns a compile-time constant if its arguments are
895 * compile-time constants.
896 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100897 * \warning This macro may evaluate its arguments multiple times or
898 * zero times, so you should not pass arguments that contain
899 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200900 *
901 * The following code illustrates how to allocate enough memory to export
902 * a public key by querying the key type and size at runtime.
903 * \code{c}
904 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
905 * psa_status_t status;
906 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100907 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200908 * psa_key_type_t key_type = psa_get_key_type(&attributes);
909 * size_t key_bits = psa_get_key_bits(&attributes);
910 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
911 * psa_reset_key_attributes(&attributes);
912 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100913 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200914 * size_t buffer_length;
915 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100916 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200917 * \endcode
918 *
919 * \param key_type A public key or key pair key type.
920 * \param key_bits The size of the key in bits.
921 *
922 * \return If the parameters are valid and supported, return
923 * a buffer size in bytes that guarantees that
924 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100925 * #PSA_ERROR_BUFFER_TOO_SMALL.
926 * If the parameters are a valid combination that is not
927 * supported, return either a sensible size or 0.
928 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200929 * the return value is unspecified.
930 *
931 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100932 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200933 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
934 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
935 * \p key_bits).
936 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100937#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
938 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
939 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100940 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200941 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200942
943/** Sufficient buffer size for exporting any asymmetric key pair.
944 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100945 * This macro expands to a compile-time constant integer. This value is
946 * a sufficient buffer size when calling psa_export_key() to export any
947 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200948 *
949 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
950 */
Przemek Stekiel5357a7a2023-04-27 11:22:36 +0200951#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
952 PSA_MAX_OF_THREE(PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS), \
953 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS), \
954 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200955
956/** Sufficient buffer size for exporting any asymmetric public key.
957 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100958 * This macro expands to a compile-time constant integer. This value is
959 * a sufficient buffer size when calling psa_export_key() or
960 * psa_export_public_key() to export any asymmetric public key,
961 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200962 *
963 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
964 */
Przemek Stekiel5357a7a2023-04-27 11:22:36 +0200965#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
966 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 +0100967 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS), \
968 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS))
969
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200970
971/** Sufficient output buffer size for psa_raw_key_agreement().
972 *
973 * This macro returns a compile-time constant if its arguments are
974 * compile-time constants.
975 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100976 * \warning This macro may evaluate its arguments multiple times or
977 * zero times, so you should not pass arguments that contain
978 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200979 *
980 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
981 *
982 * \param key_type A supported key type.
983 * \param key_bits The size of the key in bits.
984 *
985 * \return If the parameters are valid and supported, return
986 * a buffer size in bytes that guarantees that
987 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100988 * #PSA_ERROR_BUFFER_TOO_SMALL.
989 * If the parameters are a valid combination that
990 * is not supported, return either a sensible size or 0.
991 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200992 * the return value is unspecified.
993 */
994#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
Przemek Stekiel654bef02022-12-15 13:28:02 +0100995 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \
Gilles Peskine3c861642023-07-20 15:10:34 +0200996 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200997
998/** Maximum size of the output from psa_raw_key_agreement().
999 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001000 * This macro expands to a compile-time constant integer. This value is the
1001 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001002 *
1003 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
1004 */
Przemek Stekiel4ce52322023-04-28 13:40:34 +02001005#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
1006 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
1007 PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) ? \
1008 PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) : \
1009 PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001010
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001011/** The default IV size for a cipher algorithm, in bytes.
1012 *
1013 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
1014 * the default IV length for the algorithm.
1015 *
1016 * This macro can be used to allocate a buffer of sufficient size to
1017 * store the IV output from #psa_cipher_generate_iv() when using
1018 * a multi-part cipher operation.
1019 *
1020 * See also #PSA_CIPHER_IV_MAX_SIZE.
1021 *
1022 * \warning This macro may evaluate its arguments multiple times or
1023 * zero times, so you should not pass arguments that contain
1024 * side effects.
1025 *
1026 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
1027 *
1028 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
1029 *
1030 * \return The default IV size for the specified key type and algorithm.
1031 * If the algorithm does not use an IV, return 0.
1032 * If the key type or cipher algorithm is not recognized,
1033 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001034 */
1035#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001036 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 ((alg) == PSA_ALG_CTR || \
1038 (alg) == PSA_ALG_CFB || \
1039 (alg) == PSA_ALG_OFB || \
1040 (alg) == PSA_ALG_XTS || \
1041 (alg) == PSA_ALG_CBC_NO_PADDING || \
1042 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001043 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine3c861642023-07-20 15:10:34 +02001044 (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \
1045 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \
1046 0u)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001047
1048/** The maximum IV size for all supported cipher algorithms, in bytes.
1049 *
1050 * See also #PSA_CIPHER_IV_LENGTH().
1051 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001052#define PSA_CIPHER_IV_MAX_SIZE 16u
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001053
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001054/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1055 *
1056 * If the size of the output buffer is at least this large, it is guaranteed
1057 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1058 * Depending on the algorithm, the actual size of the output might be smaller.
1059 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001060 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1061 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001062 * \warning This macro may evaluate its arguments multiple times or
1063 * zero times, so you should not pass arguments that contain
1064 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001065 *
1066 * \param key_type A symmetric key type that is compatible with algorithm
1067 * alg.
1068 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1069 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1070 * \param input_length Size of the input in bytes.
1071 *
1072 * \return A sufficient output size for the specified key type and
1073 * algorithm. If the key type or cipher algorithm is not
1074 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001075 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001076 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001077#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1078 (alg == PSA_ALG_CBC_PKCS7 ? \
1079 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1080 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1081 (input_length) + 1u) + \
1082 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \
1083 (PSA_ALG_IS_CIPHER(alg) ? \
1084 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1085 0u))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001086
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001087/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1088 * supported key types and cipher algorithms.
1089 *
1090 * If the size of the output buffer is at least this large, it is guaranteed
1091 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1092 *
1093 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1094 *
1095 * \param input_length Size of the input in bytes.
1096 *
1097 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001098#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1099 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1100 (input_length) + 1u) + \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001101 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001102
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001103/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1104 *
1105 * If the size of the output buffer is at least this large, it is guaranteed
1106 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1107 * Depending on the algorithm, the actual size of the output might be smaller.
1108 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001109 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001110 *
1111 * \param key_type A symmetric key type that is compatible with algorithm
1112 * alg.
1113 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1114 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1115 * \param input_length Size of the input in bytes.
1116 *
1117 * \return A sufficient output size for the specified key type and
1118 * algorithm. If the key type or cipher algorithm is not
1119 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001120 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001121 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001122#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1123 (PSA_ALG_IS_CIPHER(alg) && \
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001124 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Gilles Peskine3c861642023-07-20 15:10:34 +02001125 (input_length) : \
1126 0u)
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001127
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001128/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1129 * supported key types and cipher algorithms.
1130 *
1131 * If the size of the output buffer is at least this large, it is guaranteed
1132 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1133 *
1134 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1135 *
1136 * \param input_length Size of the input in bytes.
1137 */
1138#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1139 (input_length)
1140
1141/** A sufficient output buffer size for psa_cipher_update().
1142 *
1143 * If the size of the output buffer is at least this large, it is guaranteed
1144 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1145 * The actual size of the output might be smaller in any given call.
1146 *
1147 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1148 *
1149 * \param key_type A symmetric key type that is compatible with algorithm
1150 * alg.
1151 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1152 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1153 * \param input_length Size of the input in bytes.
1154 *
1155 * \return A sufficient output size for the specified key type and
1156 * algorithm. If the key type or cipher algorithm is not
1157 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001158 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001159#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1160 (PSA_ALG_IS_CIPHER(alg) ? \
1161 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1162 (((alg) == PSA_ALG_CBC_PKCS7 || \
1163 (alg) == PSA_ALG_CBC_NO_PADDING || \
1164 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1165 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1166 input_length) : \
1167 (input_length)) : 0u) : \
1168 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001169
1170/** A sufficient output buffer size for psa_cipher_update(), for any of the
1171 * supported key types and cipher algorithms.
1172 *
1173 * If the size of the output buffer is at least this large, it is guaranteed
1174 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1175 *
1176 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1177 *
1178 * \param input_length Size of the input in bytes.
1179 */
1180#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001181 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001182
1183/** A sufficient ciphertext buffer size for psa_cipher_finish().
1184 *
1185 * If the size of the ciphertext buffer is at least this large, it is
1186 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1187 * ciphertext buffer size. The actual size of the output might be smaller in
1188 * any given call.
1189 *
1190 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1191 *
1192 * \param key_type A symmetric key type that is compatible with algorithm
1193 * alg.
1194 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1195 * #PSA_ALG_IS_CIPHER(\p alg) is true).
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, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001199 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001200#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1201 (PSA_ALG_IS_CIPHER(alg) ? \
1202 (alg == PSA_ALG_CBC_PKCS7 ? \
1203 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +02001204 0u) : \
1205 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001206
1207/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1208 * supported key types and cipher algorithms.
1209 *
1210 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1211 */
1212#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1213 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1214
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001215#endif /* PSA_CRYPTO_SIZES_H */