blob: b884defe0c30706722f009dad22309b143f2c1ca [file] [log] [blame]
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001/**
2 * \file psa/crypto_sizes.h
3 *
4 * \brief PSA cryptography module: Mbed TLS buffer size macros
5 *
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02009 * This file contains the definitions of macros that are useful to
10 * compute buffer sizes. The signatures and semantics of these macros
11 * are standardized, but the definitions are not, because they depend on
12 * the available algorithms and, in some cases, on permitted tolerances
13 * on buffer sizes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +020014 *
Gilles Peskine07c91f52018-06-28 18:02:53 +020015 * In implementations with isolation between the application and the
16 * cryptography module, implementers should take care to ensure that
17 * the definitions that are exposed to applications match what the
18 * module implements.
19 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +020020 * Macros that compute sizes whose values do not depend on the
21 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020022 */
23/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020024 * Copyright The Mbed TLS Contributors
Gilles Peskine0cad07c2018-06-27 19:49:02 +020025 * SPDX-License-Identifier: Apache-2.0
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020038 */
39
40#ifndef PSA_CRYPTO_SIZES_H
41#define PSA_CRYPTO_SIZES_H
42
Ronald Cronf6236f02023-01-26 16:22:25 +010043/*
44 * Include the build-time configuration information file. Here, we do not
45 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
46 * is basically just an alias to it. This is to ease the maintenance of the
47 * PSA cryptography repository which has a different build system and
48 * configuration.
49 */
50#include "psa/build_info.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020051
Gilles Peskinea7c26db2018-12-12 13:42:25 +010052#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
53#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Przemek Stekiel654bef02022-12-15 13:28:02 +010054#define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \
55 (c) : (b) : (a) <= (c) ? (c) : (a))
Gilles Peskinea7c26db2018-12-12 13:42:25 +010056
Gilles Peskine248010c2019-05-14 16:08:59 +020057#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
58 (((length) + (block_size) - 1) / (block_size) * (block_size))
59
Gilles Peskinea7c26db2018-12-12 13:42:25 +010060/** The size of the output of psa_hash_finish(), in bytes.
61 *
62 * This is also the hash size that psa_hash_verify() expects.
63 *
64 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
65 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
66 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
67 * hash algorithm).
68 *
69 * \return The hash size for the specified hash algorithm.
70 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010071 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010072#define PSA_HASH_LENGTH(alg) \
73 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010074 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
87 0)
88
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +020089/** The input block size of a hash algorithm, in bytes.
90 *
91 * Hash algorithms process their input data in blocks. Hash operations will
92 * retain any partial blocks until they have enough input to fill the block or
93 * until the operation is finished.
94 * This affects the output from psa_hash_suspend().
95 *
96 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
97 * PSA_ALG_IS_HASH(\p alg) is true).
98 *
99 * \return The block size in bytes for the specified hash algorithm.
100 * If the hash algorithm is not recognized, return 0.
101 * An implementation can return either 0 or the correct size for a
102 * hash algorithm that it recognizes, but does not support.
103 */
104#define PSA_HASH_BLOCK_LENGTH(alg) \
105 ( \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
109 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
110 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
111 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
112 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
113 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
114 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
115 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
116 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
117 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
118 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
119 0)
120
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200121/** \def PSA_HASH_MAX_SIZE
122 *
123 * Maximum size of a hash.
124 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100125 * This macro expands to a compile-time constant integer. This value
126 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200127 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200128/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
129 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
130 * HMAC-SHA3-512. */
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 Peskine0cad07c2018-06-27 19:49:02 +0200134#define PSA_HASH_MAX_SIZE 64
135#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200136#elif defined(PSA_WANT_ALG_SHA_384)
137#define PSA_HASH_MAX_SIZE 48
138#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
139#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200140#define PSA_HASH_MAX_SIZE 32
141#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200142#elif defined(PSA_WANT_ALG_SHA_224)
143#define PSA_HASH_MAX_SIZE 28
144#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
145#else /* SHA-1 or smaller */
146#define PSA_HASH_MAX_SIZE 20
147#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
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) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100188 ((void) (key_bits), 0))
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 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100194#define PSA_AEAD_TAG_MAX_SIZE 16
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. */
209#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
210
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. */
215#define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192
216
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 Peskineaf3baab2018-06-27 22:55:52 +0200220#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Valerio Setti271c12e2023-03-23 16:30:27 +0100221#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200222#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
Valerio Setti271c12e2023-03-23 16:30:27 +0100223#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200224#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
Valerio Setti271c12e2023-03-23 16:30:27 +0100225#elif defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200226#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Valerio Setti271c12e2023-03-23 16:30:27 +0100227#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200228#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
Valerio Setti271c12e2023-03-23 16:30:27 +0100229#elif defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200230#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100231#elif defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200232#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100233#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200234#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
Valerio Setti271c12e2023-03-23 16:30:27 +0100235#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200236#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
Valerio Setti271c12e2023-03-23 16:30:27 +0100237#elif defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200238#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Valerio Setti271c12e2023-03-23 16:30:27 +0100239#elif defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200240#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
Valerio Setti271c12e2023-03-23 16:30:27 +0100241#elif defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200242#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
Valerio Setti271c12e2023-03-23 16:30:27 +0100243#elif defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200244#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
245#else
246#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
247#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 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100264#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
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. */
268#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65
269
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. */
273#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32
274
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 */
277#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffff
278
Kusumit Ghoderao857cd4b2023-06-22 15:37:23 +0530279/* Output size of AES_CMAC_PRF_128 algorithm */
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +0530280#define PSA_AES_CMAC_PRF_128_OUTPUT_SIZE 16
Kusumit Ghoderao857cd4b2023-06-22 15:37:23 +0530281
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100282/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100283#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200284
Gilles Peskineacd4be32018-07-08 19:56:25 +0200285/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200286 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200287 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200288 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100289 * \warning This macro may evaluate its arguments multiple times or
290 * zero times, so you should not pass arguments that contain
291 * side effects.
292 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200293 * \param key_type The type of the MAC key.
294 * \param key_bits The size of the MAC key in bits.
295 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100296 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297 *
298 * \return The MAC size for the specified algorithm with
299 * the specified key parameters.
300 * \return 0 if the MAC algorithm is not recognized.
301 * \return Either 0 or the correct size for a MAC algorithm that
302 * the implementation recognizes, but does not support.
303 * \return Unspecified if the key parameters are not consistent
304 * with the algorithm.
305 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100306#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
307 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
308 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
309 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 ((void) (key_type), (void) (key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200311
312/** The maximum size of the output of psa_aead_encrypt(), in bytes.
313 *
314 * If the size of the ciphertext buffer is at least this large, it is
315 * guaranteed that psa_aead_encrypt() will not fail due to an
316 * insufficient buffer size. Depending on the algorithm, the actual size of
317 * the ciphertext may be smaller.
318 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100319 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
320 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100321 * \warning This macro may evaluate its arguments multiple times or
322 * zero times, so you should not pass arguments that contain
323 * side effects.
324 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100325 * \param key_type A symmetric key type that is
326 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200327 * \param alg An AEAD algorithm
328 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100329 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200330 * \param plaintext_length Size of the plaintext in bytes.
331 *
332 * \return The AEAD ciphertext size for the specified
333 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100334 * If the key type or AEAD algorithm is not
335 * recognized, or the parameters are incompatible,
336 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200337 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100338#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200339 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200340 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200341 0)
342
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200343/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
344 * supported key types and AEAD algorithms.
345 *
346 * If the size of the ciphertext buffer is at least this large, it is guaranteed
347 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
348 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100349 * \note This macro returns a compile-time constant if its arguments are
350 * compile-time constants.
351 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100352 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
353 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200354 *
355 * \param plaintext_length Size of the plaintext in bytes.
356 *
357 * \return A sufficient output buffer size for any of the
358 * supported key types and AEAD algorithms.
359 *
360 */
361#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
362 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
363
364
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200365/** The maximum size of the output of psa_aead_decrypt(), in bytes.
366 *
367 * If the size of the plaintext buffer is at least this large, it is
368 * guaranteed that psa_aead_decrypt() will not fail due to an
369 * insufficient buffer size. Depending on the algorithm, the actual size of
370 * the plaintext may be smaller.
371 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100372 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
373 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100374 * \warning This macro may evaluate its arguments multiple times or
375 * zero times, so you should not pass arguments that contain
376 * side effects.
377 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100378 * \param key_type A symmetric key type that is
379 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200380 * \param alg An AEAD algorithm
381 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100382 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200383 * \param ciphertext_length Size of the plaintext in bytes.
384 *
385 * \return The AEAD ciphertext size for the specified
386 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100387 * If the key type or AEAD algorithm is not
388 * recognized, or the parameters are incompatible,
389 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200390 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100391#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200392 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
394 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200395 0)
396
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200397/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
398 * supported key types and AEAD algorithms.
399 *
400 * If the size of the plaintext buffer is at least this large, it is guaranteed
401 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
402 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100403 * \note This macro returns a compile-time constant if its arguments are
404 * compile-time constants.
405 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100406 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
407 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200408 *
409 * \param ciphertext_length Size of the ciphertext in bytes.
410 *
411 * \return A sufficient output buffer size for any of the
412 * supported key types and AEAD algorithms.
413 *
414 */
415#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200417
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100418/** The default nonce size for an AEAD algorithm, in bytes.
419 *
420 * This macro can be used to allocate a buffer of sufficient size to
421 * store the nonce output from #psa_aead_generate_nonce().
422 *
423 * See also #PSA_AEAD_NONCE_MAX_SIZE.
424 *
425 * \note This is not the maximum size of nonce supported as input to
426 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
427 * just the default size that is generated by #psa_aead_generate_nonce().
428 *
429 * \warning This macro may evaluate its arguments multiple times or
430 * zero times, so you should not pass arguments that contain
431 * side effects.
432 *
433 * \param key_type A symmetric key type that is compatible with
434 * algorithm \p alg.
435 *
436 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
437 * #PSA_ALG_IS_AEAD(\p alg) is true).
438 *
439 * \return The default nonce size for the specified key type and algorithm.
440 * If the key type or AEAD algorithm is not recognized,
441 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100442 */
443#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100444 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
446 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
447 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100448 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100450 0)
451
452/** The maximum default nonce size among all supported pairs of key types and
453 * AEAD algorithms, in bytes.
454 *
455 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
456 * may return.
457 *
458 * \note This is not the maximum size of nonce supported as input to
459 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
460 * just the largest size that may be generated by
461 * #psa_aead_generate_nonce().
462 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100463#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100464
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200465/** A sufficient output buffer size for psa_aead_update().
466 *
467 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200468 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200469 * insufficient buffer size. The actual size of the output may be smaller
470 * in any given call.
471 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100472 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
473 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100474 * \warning This macro may evaluate its arguments multiple times or
475 * zero times, so you should not pass arguments that contain
476 * side effects.
477 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100478 * \param key_type A symmetric key type that is
479 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200480 * \param alg An AEAD algorithm
481 * (\c PSA_ALG_XXX value such that
482 * #PSA_ALG_IS_AEAD(\p alg) is true).
483 * \param input_length Size of the input in bytes.
484 *
485 * \return A sufficient output buffer size for the specified
486 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100487 * If the key type or AEAD algorithm is not
488 * recognized, or the parameters are incompatible,
489 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200490 */
491/* For all the AEAD modes defined in this specification, it is possible
492 * to emit output without delay. However, hardware may not always be
493 * capable of this. So for modes based on a block cipher, allow the
494 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100495#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200496 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
498 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
499 (input_length) : \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100500 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200501
502/** A sufficient output buffer size for psa_aead_update(), for any of the
503 * supported key types and AEAD algorithms.
504 *
505 * If the size of the output buffer is at least this large, it is guaranteed
506 * that psa_aead_update() will not fail due to an insufficient buffer size.
507 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100508 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200509 *
510 * \param input_length Size of the input in bytes.
511 */
512#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
513 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200514
515/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200516 *
517 * If the size of the ciphertext buffer is at least this large, it is
518 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200519 * insufficient ciphertext buffer size. The actual size of the output may
520 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200521 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100522 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
523 *
524 * \param key_type A symmetric key type that is
525 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200526 * \param alg An AEAD algorithm
527 * (\c PSA_ALG_XXX value such that
528 * #PSA_ALG_IS_AEAD(\p alg) is true).
529 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200530 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200531 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100532 * If the key type or AEAD algorithm is not
533 * recognized, or the parameters are incompatible,
534 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200535 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200536#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
537 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
539 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200540 0)
541
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200542/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
543 * supported key types and AEAD algorithms.
544 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100545 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200546 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200547#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200548
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200549/** A sufficient plaintext buffer size for psa_aead_verify().
550 *
551 * If the size of the plaintext buffer is at least this large, it is
552 * guaranteed that psa_aead_verify() will not fail due to an
553 * insufficient plaintext buffer size. The actual size of the output may
554 * be smaller in any given call.
555 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100556 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
557 *
558 * \param key_type A symmetric key type that is
559 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200560 * \param alg An AEAD algorithm
561 * (\c PSA_ALG_XXX value such that
562 * #PSA_ALG_IS_AEAD(\p alg) is true).
563 *
564 * \return A sufficient plaintext buffer size for the
565 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100566 * If the key type or AEAD algorithm is not
567 * recognized, or the parameters are incompatible,
568 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200569 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200570#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
571 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
573 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200574 0)
575
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200576/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
577 * supported key types and AEAD algorithms.
578 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100579 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200580 */
581#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
582
Jaeden Amero7f042142019-02-07 10:44:38 +0000583#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
584 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100585 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100586 11 /*PKCS#1v1.5*/)
587
588/**
589 * \brief ECDSA signature size for a given curve bit size
590 *
591 * \param curve_bits Curve size in bits.
592 * \return Signature size in bytes.
593 *
594 * \note This macro returns a compile-time constant if its argument is one.
595 */
596#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
597 (PSA_BITS_TO_BYTES(curve_bits) * 2)
598
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100599/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200600 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200601 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200602 * of the specified type and size, with the specified algorithm.
603 * Note that the actual size of the signature may be smaller
604 * (some algorithms produce a variable-size signature).
605 *
606 * \warning This function may call its arguments multiple times or
607 * zero times, so you should not pass arguments that contain
608 * side effects.
609 *
610 * \param key_type An asymmetric key type (this may indifferently be a
611 * key pair type or a public key type).
612 * \param key_bits The size of the key in bits.
613 * \param alg The signature algorithm.
614 *
615 * \return If the parameters are valid and supported, return
616 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100617 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200618 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100619 * If the parameters are a valid combination that is not supported,
620 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200621 * If the parameters are not valid, the
622 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200623 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100624#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200626 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 ((void) alg, 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200628
Gilles Peskine29755712019-11-08 15:49:40 +0100629#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
630 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
631
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100632/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100633 *
634 * Maximum size of an asymmetric signature.
635 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100636 * This macro expands to a compile-time constant integer. This value
637 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100638 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100639#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100640 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
641 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
642 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
643
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200644/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200645 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200646 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200647 * a key of the specified type and size, with the specified algorithm.
648 * Note that the actual size of the ciphertext may be smaller, depending
649 * on the algorithm.
650 *
651 * \warning This function may call its arguments multiple times or
652 * zero times, so you should not pass arguments that contain
653 * side effects.
654 *
655 * \param key_type An asymmetric key type (this may indifferently be a
656 * key pair type or a public key type).
657 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200658 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200659 *
660 * \return If the parameters are valid and supported, return
661 * a buffer size in bytes that guarantees that
662 * psa_asymmetric_encrypt() will not fail with
663 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100664 * If the parameters are a valid combination that is not supported,
665 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200666 * If the parameters are not valid, the
667 * return value is unspecified.
668 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200669#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
670 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200672 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200673
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200674/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
675 * supported asymmetric encryption.
676 *
677 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
678 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100679/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200680#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100681 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200682
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200683/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200684 *
Gilles Peskine76689602020-05-05 16:01:22 +0200685 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200686 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200687 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200688 * on the algorithm.
689 *
690 * \warning This function may call its arguments multiple times or
691 * zero times, so you should not pass arguments that contain
692 * side effects.
693 *
694 * \param key_type An asymmetric key type (this may indifferently be a
695 * key pair type or a public key type).
696 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200697 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200698 *
699 * \return If the parameters are valid and supported, return
700 * a buffer size in bytes that guarantees that
701 * psa_asymmetric_decrypt() will not fail with
702 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100703 * If the parameters are a valid combination that is not supported,
704 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200705 * If the parameters are not valid, the
706 * return value is unspecified.
707 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200708#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
709 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
710 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
711 0)
712
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200713/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
714 * supported asymmetric decryption.
715 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100716 * This macro assumes that RSA is the only supported asymmetric encryption.
717 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200718 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
719 */
720#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100721 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200722
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
724 * number of bits.
725 *
726 * This definition assumes that bits <= 2^19 - 9 so that the length field
727 * is at most 3 bytes. The length of the encoding is the length of the
728 * bit string padded to a whole number of bytes plus:
729 * - 1 type byte;
730 * - 1 to 3 length bytes;
731 * - 0 to 1 bytes of leading 0 due to the sign bit.
732 */
733#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
734 ((bits) / 8 + 5)
735
736/* Maximum size of the export encoding of an RSA public key.
737 * Assumes that the public exponent is less than 2^32.
738 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200739 * RSAPublicKey ::= SEQUENCE {
740 * modulus INTEGER, -- n
741 * publicExponent INTEGER } -- e
742 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000743 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200744 * - n : INTEGER;
745 * - 7 bytes for the public exponent.
746 */
747#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000748 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200749
750/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000751 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200752 * difference between the two primes is at most 1 bit.
753 *
754 * RSAPrivateKey ::= SEQUENCE {
755 * version Version, -- 0
756 * modulus INTEGER, -- N-bit
757 * publicExponent INTEGER, -- 32-bit
758 * privateExponent INTEGER, -- N-bit
759 * prime1 INTEGER, -- N/2-bit
760 * prime2 INTEGER, -- N/2-bit
761 * exponent1 INTEGER, -- N/2-bit
762 * exponent2 INTEGER, -- N/2-bit
763 * coefficient INTEGER, -- N/2-bit
764 * }
765 *
766 * - 4 bytes of SEQUENCE overhead;
767 * - 3 bytes of version;
768 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
769 * overapproximated as 9 half-size INTEGERS;
770 * - 7 bytes for the public exponent.
771 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200772#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200773 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
774
775/* Maximum size of the export encoding of a DSA public key.
776 *
777 * SubjectPublicKeyInfo ::= SEQUENCE {
778 * algorithm AlgorithmIdentifier,
779 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
780 * AlgorithmIdentifier ::= SEQUENCE {
781 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400782 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200783 * DSAPublicKey ::= INTEGER -- public key, Y
784 *
785 * - 3 * 4 bytes of SEQUENCE overhead;
786 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
787 * - 4 bytes of BIT STRING overhead;
788 * - 3 full-size INTEGERs (p, g, y);
789 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
790 */
791#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
792 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
793
794/* Maximum size of the export encoding of a DSA key pair.
795 *
796 * DSAPrivateKey ::= SEQUENCE {
797 * version Version, -- 0
798 * prime INTEGER, -- p
799 * subprime INTEGER, -- q
800 * generator INTEGER, -- g
801 * public INTEGER, -- y
802 * private INTEGER, -- x
803 * }
804 *
805 * - 4 bytes of SEQUENCE overhead;
806 * - 3 bytes of version;
807 * - 3 full-size INTEGERs (p, g, y);
808 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
809 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200810#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200811 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
812
813/* Maximum size of the export encoding of an ECC public key.
814 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000815 * The representation of an ECC public key is:
816 * - The byte 0x04;
817 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
818 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
819 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200820 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000821 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200822 */
823#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000824 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200825
826/* Maximum size of the export encoding of an ECC key pair.
827 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100828 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200829 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200830#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100831 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200832
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200833/* Maximum size of the export encoding of an DH key pair.
Przemek Stekieled23b612022-12-01 15:00:41 +0100834 *
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200835 * An DH key pair is represented by the secret value.
Przemek Stekieled23b612022-12-01 15:00:41 +0100836 */
837#define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \
838 (PSA_BITS_TO_BYTES(key_bits))
839
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200840/* Maximum size of the export encoding of an DH public key.
Przemek Stekieled23b612022-12-01 15:00:41 +0100841 */
842#define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \
843 (PSA_BITS_TO_BYTES(key_bits))
844
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100845/** Sufficient output buffer size for psa_export_key() or
846 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200847 *
848 * This macro returns a compile-time constant if its arguments are
849 * compile-time constants.
850 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100851 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200852 * zero times, so you should not pass arguments that contain
853 * side effects.
854 *
855 * The following code illustrates how to allocate enough memory to export
856 * a key by querying the key type and size at runtime.
857 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200858 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200859 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200860 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200861 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200862 * psa_key_type_t key_type = psa_get_key_type(&attributes);
863 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100864 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200865 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200866 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200867 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200868 * size_t buffer_length;
869 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
870 * if (status != PSA_SUCCESS) handle_error(...);
871 * \endcode
872 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200873 * \param key_type A supported key type.
874 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200875 *
876 * \return If the parameters are valid and supported, return
877 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100878 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200879 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100880 * If the parameters are a valid combination that is not supported,
881 * return either a sensible size or 0.
882 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200883 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100884#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
885 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100886 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100887 (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 +0200888 (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 +0100889 (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 +0200890 (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 +0100891 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
892 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200893 0)
894
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200895/** Sufficient output buffer size for psa_export_public_key().
896 *
897 * This macro returns a compile-time constant if its arguments are
898 * compile-time constants.
899 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100900 * \warning This macro may evaluate its arguments multiple times or
901 * zero times, so you should not pass arguments that contain
902 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200903 *
904 * The following code illustrates how to allocate enough memory to export
905 * a public key by querying the key type and size at runtime.
906 * \code{c}
907 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
908 * psa_status_t status;
909 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100910 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200911 * psa_key_type_t key_type = psa_get_key_type(&attributes);
912 * size_t key_bits = psa_get_key_bits(&attributes);
913 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
914 * psa_reset_key_attributes(&attributes);
915 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100916 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200917 * size_t buffer_length;
918 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100919 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200920 * \endcode
921 *
922 * \param key_type A public key or key pair key type.
923 * \param key_bits The size of the key in bits.
924 *
925 * \return If the parameters are valid and supported, return
926 * a buffer size in bytes that guarantees that
927 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100928 * #PSA_ERROR_BUFFER_TOO_SMALL.
929 * If the parameters are a valid combination that is not
930 * supported, return either a sensible size or 0.
931 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200932 * the return value is unspecified.
933 *
934 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100935 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200936 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
937 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
938 * \p key_bits).
939 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100940#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
941 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
942 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100943 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200944 0)
945
946/** Sufficient buffer size for exporting any asymmetric key pair.
947 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100948 * This macro expands to a compile-time constant integer. This value is
949 * a sufficient buffer size when calling psa_export_key() to export any
950 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200951 *
952 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
953 */
Przemek Stekiel5357a7a2023-04-27 11:22:36 +0200954#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
955 PSA_MAX_OF_THREE(PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS), \
956 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS), \
957 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200958
959/** Sufficient buffer size for exporting any asymmetric public key.
960 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100961 * This macro expands to a compile-time constant integer. This value is
962 * a sufficient buffer size when calling psa_export_key() or
963 * psa_export_public_key() to export any asymmetric public key,
964 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200965 *
966 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
967 */
Przemek Stekiel5357a7a2023-04-27 11:22:36 +0200968#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
969 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 +0100970 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS), \
971 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS))
972
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200973
974/** Sufficient output buffer size for psa_raw_key_agreement().
975 *
976 * This macro returns a compile-time constant if its arguments are
977 * compile-time constants.
978 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100979 * \warning This macro may evaluate its arguments multiple times or
980 * zero times, so you should not pass arguments that contain
981 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200982 *
983 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
984 *
985 * \param key_type A supported key type.
986 * \param key_bits The size of the key in bits.
987 *
988 * \return If the parameters are valid and supported, return
989 * a buffer size in bytes that guarantees that
990 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100991 * #PSA_ERROR_BUFFER_TOO_SMALL.
992 * If the parameters are a valid combination that
993 * is not supported, return either a sensible size or 0.
994 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200995 * the return value is unspecified.
996 */
997#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
Przemek Stekiel654bef02022-12-15 13:28:02 +0100998 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \
999 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001000
1001/** Maximum size of the output from psa_raw_key_agreement().
1002 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001003 * This macro expands to a compile-time constant integer. This value is the
1004 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001005 *
1006 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
1007 */
Przemek Stekiel4ce52322023-04-28 13:40:34 +02001008#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
1009 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
1010 PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) ? \
1011 PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) : \
1012 PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001013
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001014/** The default IV size for a cipher algorithm, in bytes.
1015 *
1016 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
1017 * the default IV length for the algorithm.
1018 *
1019 * This macro can be used to allocate a buffer of sufficient size to
1020 * store the IV output from #psa_cipher_generate_iv() when using
1021 * a multi-part cipher operation.
1022 *
1023 * See also #PSA_CIPHER_IV_MAX_SIZE.
1024 *
1025 * \warning This macro may evaluate its arguments multiple times or
1026 * zero times, so you should not pass arguments that contain
1027 * side effects.
1028 *
1029 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
1030 *
1031 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
1032 *
1033 * \return The default IV size for the specified key type and algorithm.
1034 * If the algorithm does not use an IV, return 0.
1035 * If the key type or cipher algorithm is not recognized,
1036 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001037 */
1038#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001039 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 ((alg) == PSA_ALG_CTR || \
1041 (alg) == PSA_ALG_CFB || \
1042 (alg) == PSA_ALG_OFB || \
1043 (alg) == PSA_ALG_XTS || \
1044 (alg) == PSA_ALG_CBC_NO_PADDING || \
1045 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001046 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
1048 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
1049 0)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001050
1051/** The maximum IV size for all supported cipher algorithms, in bytes.
1052 *
1053 * See also #PSA_CIPHER_IV_LENGTH().
1054 */
1055#define PSA_CIPHER_IV_MAX_SIZE 16
1056
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001057/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1058 *
1059 * If the size of the output buffer is at least this large, it is guaranteed
1060 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1061 * Depending on the algorithm, the actual size of the output might be smaller.
1062 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001063 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1064 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001065 * \warning This macro may evaluate its arguments multiple times or
1066 * zero times, so you should not pass arguments that contain
1067 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001068 *
1069 * \param key_type A symmetric key type that is compatible with algorithm
1070 * alg.
1071 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1072 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1073 * \param input_length Size of the input in bytes.
1074 *
1075 * \return A sufficient output size for the specified key type and
1076 * algorithm. If the key type or cipher algorithm is not
1077 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001078 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001079 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001080#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1081 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliottc22950c2021-07-08 16:50:01 +01001082 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1084 (input_length) + 1) + \
1085 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001086 (PSA_ALG_IS_CIPHER(alg) ? \
1087 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001089
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001090/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1091 * supported key types and cipher algorithms.
1092 *
1093 * If the size of the output buffer is at least this large, it is guaranteed
1094 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1095 *
1096 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1097 *
1098 * \param input_length Size of the input in bytes.
1099 *
1100 */
1101#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1102 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001103 (input_length) + 1) + \
1104 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001105
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001106/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1107 *
1108 * If the size of the output buffer is at least this large, it is guaranteed
1109 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1110 * Depending on the algorithm, the actual size of the output might be smaller.
1111 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001112 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001113 *
1114 * \param key_type A symmetric key type that is compatible with algorithm
1115 * alg.
1116 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1117 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1118 * \param input_length Size of the input in bytes.
1119 *
1120 * \return A sufficient output size for the specified key type and
1121 * algorithm. If the key type or cipher algorithm is not
1122 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001123 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001124 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001125#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1126 (PSA_ALG_IS_CIPHER(alg) && \
1127 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1128 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001129 0)
1130
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001131/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1132 * supported key types and cipher algorithms.
1133 *
1134 * If the size of the output buffer is at least this large, it is guaranteed
1135 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1136 *
1137 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1138 *
1139 * \param input_length Size of the input in bytes.
1140 */
1141#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1142 (input_length)
1143
1144/** A sufficient output buffer size for psa_cipher_update().
1145 *
1146 * If the size of the output buffer is at least this large, it is guaranteed
1147 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1148 * The actual size of the output might be smaller in any given call.
1149 *
1150 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1151 *
1152 * \param key_type A symmetric key type that is compatible with algorithm
1153 * alg.
1154 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1155 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1156 * \param input_length Size of the input in bytes.
1157 *
1158 * \return A sufficient output size for the specified key type and
1159 * algorithm. If the key type or cipher algorithm is not
1160 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001161 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001162#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1163 (PSA_ALG_IS_CIPHER(alg) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1165 (((alg) == PSA_ALG_CBC_PKCS7 || \
1166 (alg) == PSA_ALG_CBC_NO_PADDING || \
1167 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1168 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001169 input_length) : \
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001171 0)
1172
1173/** A sufficient output buffer size for psa_cipher_update(), for any of the
1174 * supported key types and cipher algorithms.
1175 *
1176 * If the size of the output buffer is at least this large, it is guaranteed
1177 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1178 *
1179 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1180 *
1181 * \param input_length Size of the input in bytes.
1182 */
1183#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001184 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001185
1186/** A sufficient ciphertext buffer size for psa_cipher_finish().
1187 *
1188 * If the size of the ciphertext buffer is at least this large, it is
1189 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1190 * ciphertext buffer size. The actual size of the output might be smaller in
1191 * any given call.
1192 *
1193 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1194 *
1195 * \param key_type A symmetric key type that is compatible with algorithm
1196 * alg.
1197 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1198 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1199 * \return A sufficient output size for the specified key type and
1200 * algorithm. If the key type or cipher algorithm is not
1201 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001202 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001203#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1204 (PSA_ALG_IS_CIPHER(alg) ? \
1205 (alg == PSA_ALG_CBC_PKCS7 ? \
1206 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1207 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001208 0)
1209
1210/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1211 * supported key types and cipher algorithms.
1212 *
1213 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1214 */
1215#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1216 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1217
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001218#endif /* PSA_CRYPTO_SIZES_H */