blob: 836c28cc23f83df998926c5698f5e128e914aa62 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000025 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine0cad07c2018-06-27 19:49:02 +020026 */
27
28#ifndef PSA_CRYPTO_SIZES_H
29#define PSA_CRYPTO_SIZES_H
30
Ronald Cronf6236f02023-01-26 16:22:25 +010031/*
Ronald Cron7871cb12023-10-10 08:51:39 +020032 * Include the build-time configuration information header. Here, we do not
Ronald Cronf6236f02023-01-26 16:22:25 +010033 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
34 * is basically just an alias to it. This is to ease the maintenance of the
Ronald Cron070e8652023-10-09 10:25:45 +020035 * TF-PSA-Crypto repository which has a different build system and
Ronald Cronf6236f02023-01-26 16:22:25 +010036 * configuration.
37 */
38#include "psa/build_info.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020039
Gilles Peskine3c861642023-07-20 15:10:34 +020040#define PSA_BITS_TO_BYTES(bits) (((bits) + 7u) / 8u)
41#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8u)
Przemek Stekiel654bef02022-12-15 13:28:02 +010042#define PSA_MAX_OF_THREE(a, b, c) ((a) <= (b) ? (b) <= (c) ? \
43 (c) : (b) : (a) <= (c) ? (c) : (a))
Gilles Peskinea7c26db2018-12-12 13:42:25 +010044
Gilles Peskine248010c2019-05-14 16:08:59 +020045#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
46 (((length) + (block_size) - 1) / (block_size) * (block_size))
47
Gilles Peskinea7c26db2018-12-12 13:42:25 +010048/** The size of the output of psa_hash_finish(), in bytes.
49 *
50 * This is also the hash size that psa_hash_verify() expects.
51 *
52 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
53 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
54 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
55 * hash algorithm).
56 *
57 * \return The hash size for the specified hash algorithm.
58 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010059 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010060#define PSA_HASH_LENGTH(alg) \
61 ( \
Gilles Peskine3c861642023-07-20 15:10:34 +020062 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16u : \
63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20u : \
64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20u : \
65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28u : \
66 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32u : \
67 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48u : \
68 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64u : \
69 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28u : \
70 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32u : \
71 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28u : \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32u : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48u : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64u : \
75 0u)
Gilles Peskinea7c26db2018-12-12 13:42:25 +010076
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +020077/** The input block size of a hash algorithm, in bytes.
78 *
79 * Hash algorithms process their input data in blocks. Hash operations will
80 * retain any partial blocks until they have enough input to fill the block or
81 * until the operation is finished.
82 * This affects the output from psa_hash_suspend().
83 *
84 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
85 * PSA_ALG_IS_HASH(\p alg) is true).
86 *
87 * \return The block size in bytes for the specified hash algorithm.
88 * If the hash algorithm is not recognized, return 0.
89 * An implementation can return either 0 or the correct size for a
90 * hash algorithm that it recognizes, but does not support.
91 */
92#define PSA_HASH_BLOCK_LENGTH(alg) \
93 ( \
Gilles Peskine3c861642023-07-20 15:10:34 +020094 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64u : \
95 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64u : \
96 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64u : \
97 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64u : \
98 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64u : \
99 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128u : \
100 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128u : \
101 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128u : \
102 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128u : \
103 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144u : \
104 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136u : \
105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104u : \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72u : \
Gilles Peskine935ff232023-08-09 19:48:02 +0200107 0u)
Mateusz Starzyk7d262dd2021-08-26 13:28:46 +0200108
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200109/** \def PSA_HASH_MAX_SIZE
110 *
111 * Maximum size of a hash.
112 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100113 * This macro expands to a compile-time constant integer. This value
114 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200115 */
Dave Rodgman5734bb92023-06-26 18:23:08 +0100116/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-224,
Gilles Peskine3052f532018-09-17 14:13:26 +0200117 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
118 * HMAC-SHA3-512. */
Manuel Pégourié-Gonnardc9d98292023-05-24 12:28:38 +0200119/* Note: PSA_HASH_MAX_SIZE should be kept in sync with MBEDTLS_MD_MAX_SIZE,
120 * see the note on MBEDTLS_MD_MAX_SIZE for details. */
Dave Rodgman5734bb92023-06-26 18:23:08 +0100121#if defined(PSA_WANT_ALG_SHA3_224)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200122#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 144u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100123#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200124#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 136u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100125#elif defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine3c861642023-07-20 15:10:34 +0200126#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200127#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200128#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100129#elif defined(PSA_WANT_ALG_SHA3_384)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200130#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 104u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100131#elif defined(PSA_WANT_ALG_SHA3_512)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200132#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 72u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200133#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200134#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200135#elif defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200136#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200137#else /* SHA-1 or smaller */
Gilles Peskine3c861642023-07-20 15:10:34 +0200138#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64u
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200139#endif
140
Dave Rodgman5734bb92023-06-26 18:23:08 +0100141#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200142#define PSA_HASH_MAX_SIZE 64u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100143#elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200144#define PSA_HASH_MAX_SIZE 48u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100145#elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200146#define PSA_HASH_MAX_SIZE 32u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100147#elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224)
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200148#define PSA_HASH_MAX_SIZE 28u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100149#else /* SHA-1 or smaller */
Gilles Peskine03e9dea2023-08-30 18:32:57 +0200150#define PSA_HASH_MAX_SIZE 20u
Dave Rodgman5734bb92023-06-26 18:23:08 +0100151#endif
152
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200153/** \def PSA_MAC_MAX_SIZE
154 *
155 * Maximum size of a MAC.
156 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100157 * This macro expands to a compile-time constant integer. This value
158 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200159 */
160/* All non-HMAC MACs have a maximum size that's smaller than the
161 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200162/* Note that the encoding of truncated MAC algorithms limits this value
163 * to 64 bytes.
164 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200165#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
166
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100167/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100168 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100169 * This macro can be used to allocate a buffer of sufficient size to store the
170 * tag output from psa_aead_finish().
171 *
172 * See also #PSA_AEAD_TAG_MAX_SIZE.
173 *
174 * \param key_type The type of the AEAD key.
175 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100176 * \param alg An AEAD algorithm
177 * (\c PSA_ALG_XXX value such that
178 * #PSA_ALG_IS_AEAD(\p alg) is true).
179 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200180 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100181 * If the AEAD algorithm does not have an identified
182 * tag that can be distinguished from the rest of
183 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100184 * If the key type or AEAD algorithm is not
185 * recognized, or the parameters are incompatible,
186 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100187 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100188#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200189 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200190 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200191 ((void) (key_bits), 0u))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100192
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200193/** The maximum tag size for all supported AEAD algorithms, in bytes.
194 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100195 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200196 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200197#define PSA_AEAD_TAG_MAX_SIZE 16u
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200198
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200199/* The maximum size of an RSA key on this implementation, in bits.
200 * This is a vendor-specific macro.
201 *
202 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
203 * whose parameters fit in a bignum is accepted. However large keys can
204 * induce a large memory usage and long computation times. Unlike other
205 * auxiliary macros in this file and in crypto.h, which reflect how the
206 * library is configured, this macro defines how the library is
207 * configured. This implementation refuses to import or generate an
208 * RSA key whose size is larger than the value defined here.
209 *
210 * Note that an implementation may set different size limits for different
211 * operations, and does not need to accept all key sizes up to the limit. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200212#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200213
Waleed Elmelegyab570712023-07-05 16:40:58 +0000214/* The minimum size of an RSA key on this implementation, in bits.
215 * This is a vendor-specific macro.
216 *
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000217 * Limits RSA key generation to a minimum due to avoid accidental misuse.
Waleed Elmelegyab570712023-07-05 16:40:58 +0000218 * This value cannot be less than 128 bits.
219 */
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000220#if defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)
221#define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS MBEDTLS_RSA_GEN_KEY_MIN_BITS
Waleed Elmelegyab570712023-07-05 16:40:58 +0000222#else
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000223#define PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS 1024
Waleed Elmelegyab570712023-07-05 16:40:58 +0000224#endif
225
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200226/* The maximum size of an DH key on this implementation, in bits.
Przemek Stekieled23b612022-12-01 15:00:41 +0100227 *
228 * Note that an implementation may set different size limits for different
229 * operations, and does not need to accept all key sizes up to the limit. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200230#define PSA_VENDOR_FFDH_MAX_KEY_BITS 8192u
Przemek Stekieled23b612022-12-01 15:00:41 +0100231
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200232/* The maximum size of an ECC key on this implementation, in bits.
233 * This is a vendor-specific macro. */
Valerio Setti271c12e2023-03-23 16:30:27 +0100234#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine3c861642023-07-20 15:10:34 +0200235#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521u
Valerio Setti271c12e2023-03-23 16:30:27 +0100236#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine3c861642023-07-20 15:10:34 +0200237#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512u
Valerio Setti271c12e2023-03-23 16:30:27 +0100238#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine3c861642023-07-20 15:10:34 +0200239#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448u
Valerio Setti271c12e2023-03-23 16:30:27 +0100240#elif defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200241#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Valerio Setti271c12e2023-03-23 16:30:27 +0100242#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine3c861642023-07-20 15:10:34 +0200243#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384u
Valerio Setti271c12e2023-03-23 16:30:27 +0100244#elif defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200245#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100246#elif defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200247#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100248#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine3c861642023-07-20 15:10:34 +0200249#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256u
Valerio Setti271c12e2023-03-23 16:30:27 +0100250#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine3c861642023-07-20 15:10:34 +0200251#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255u
Valerio Setti271c12e2023-03-23 16:30:27 +0100252#elif defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200253#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Valerio Setti271c12e2023-03-23 16:30:27 +0100254#elif defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine3c861642023-07-20 15:10:34 +0200255#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224u
Valerio Setti271c12e2023-03-23 16:30:27 +0100256#elif defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine3c861642023-07-20 15:10:34 +0200257#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Valerio Setti271c12e2023-03-23 16:30:27 +0100258#elif defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine3c861642023-07-20 15:10:34 +0200259#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200260#else
Gilles Peskine3c861642023-07-20 15:10:34 +0200261#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0u
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200262#endif
263
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100264/** This macro returns the maximum supported length of the PSK for the
265 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100266 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100267 *
268 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100269 *
270 * Quoting RFC 4279, Sect 5.3:
271 * TLS implementations supporting these ciphersuites MUST support
272 * arbitrary PSK identities up to 128 octets in length, and arbitrary
273 * PSKs up to 64 octets in length. Supporting longer identities and
274 * keys is RECOMMENDED.
275 *
276 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100277 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100278 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200279#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128u
Hanno Becker8dbfca42018-10-12 11:56:55 +0100280
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400281/* The expected size of input passed to psa_tls12_ecjpake_to_pms_input,
282 * which is expected to work with P-256 curve only. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200283#define PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE 65u
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400284
285/* The size of a serialized K.X coordinate to be used in
286 * psa_tls12_ecjpake_to_pms_input. This function only accepts the P-256
287 * curve. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200288#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE 32u
Andrzej Kurek08d34b82022-07-29 10:00:16 -0400289
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530290/* The maximum number of iterations for PBKDF2 on this implementation, in bits.
291 * This is a vendor-specific macro. This can be configured if necessary */
Gilles Peskine3c861642023-07-20 15:10:34 +0200292#define PSA_VENDOR_PBKDF2_MAX_ITERATIONS 0xffffffffU
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +0530293
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100294/** The maximum size of a block cipher. */
Gilles Peskine3c861642023-07-20 15:10:34 +0200295#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16u
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200296
Gilles Peskineacd4be32018-07-08 19:56:25 +0200297/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200298 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200299 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200300 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100301 * \warning This macro may evaluate its arguments multiple times or
302 * zero times, so you should not pass arguments that contain
303 * side effects.
304 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200305 * \param key_type The type of the MAC key.
306 * \param key_bits The size of the MAC key in bits.
307 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100308 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200309 *
310 * \return The MAC size for the specified algorithm with
311 * the specified key parameters.
312 * \return 0 if the MAC algorithm is not recognized.
313 * \return Either 0 or the correct size for a MAC algorithm that
314 * the implementation recognizes, but does not support.
315 * \return Unspecified if the key parameters are not consistent
316 * with the algorithm.
317 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100318#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
319 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
320 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
321 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200322 ((void) (key_type), (void) (key_bits), 0u))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200323
324/** The maximum size of the output of psa_aead_encrypt(), in bytes.
325 *
326 * If the size of the ciphertext buffer is at least this large, it is
327 * guaranteed that psa_aead_encrypt() will not fail due to an
328 * insufficient buffer size. Depending on the algorithm, the actual size of
329 * the ciphertext may be smaller.
330 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100331 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
332 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100333 * \warning This macro may evaluate its arguments multiple times or
334 * zero times, so you should not pass arguments that contain
335 * side effects.
336 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100337 * \param key_type A symmetric key type that is
338 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200339 * \param alg An AEAD algorithm
340 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100341 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200342 * \param plaintext_length Size of the plaintext in bytes.
343 *
344 * \return The AEAD ciphertext size for the specified
345 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100346 * If the key type or AEAD algorithm is not
347 * recognized, or the parameters are incompatible,
348 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200349 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100350#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200351 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200352 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200353 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200354
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200355/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
356 * supported key types and AEAD algorithms.
357 *
358 * If the size of the ciphertext buffer is at least this large, it is guaranteed
359 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
360 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100361 * \note This macro returns a compile-time constant if its arguments are
362 * compile-time constants.
363 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100364 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
365 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200366 *
367 * \param plaintext_length Size of the plaintext in bytes.
368 *
369 * \return A sufficient output buffer size for any of the
370 * supported key types and AEAD algorithms.
371 *
372 */
373#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
374 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
375
376
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200377/** The maximum size of the output of psa_aead_decrypt(), in bytes.
378 *
379 * If the size of the plaintext buffer is at least this large, it is
380 * guaranteed that psa_aead_decrypt() will not fail due to an
381 * insufficient buffer size. Depending on the algorithm, the actual size of
382 * the plaintext may be smaller.
383 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100384 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
385 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100386 * \warning This macro may evaluate its arguments multiple times or
387 * zero times, so you should not pass arguments that contain
388 * side effects.
389 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100390 * \param key_type A symmetric key type that is
391 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200392 * \param alg An AEAD algorithm
393 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100394 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200395 * \param ciphertext_length Size of the plaintext in bytes.
396 *
397 * \return The AEAD ciphertext size for the specified
398 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100399 * If the key type or AEAD algorithm is not
400 * recognized, or the parameters are incompatible,
401 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200402 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100403#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200404 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
406 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200407 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200408
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200409/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
410 * supported key types and AEAD algorithms.
411 *
412 * If the size of the plaintext buffer is at least this large, it is guaranteed
413 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
414 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100415 * \note This macro returns a compile-time constant if its arguments are
416 * compile-time constants.
417 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100418 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
419 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200420 *
421 * \param ciphertext_length Size of the ciphertext in bytes.
422 *
423 * \return A sufficient output buffer size for any of the
424 * supported key types and AEAD algorithms.
425 *
426 */
427#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 (ciphertext_length)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200429
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100430/** The default nonce size for an AEAD algorithm, in bytes.
431 *
432 * This macro can be used to allocate a buffer of sufficient size to
433 * store the nonce output from #psa_aead_generate_nonce().
434 *
435 * See also #PSA_AEAD_NONCE_MAX_SIZE.
436 *
437 * \note This is not the maximum size of nonce supported as input to
438 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
439 * just the default size that is generated by #psa_aead_generate_nonce().
440 *
441 * \warning This macro may evaluate its arguments multiple times or
442 * zero times, so you should not pass arguments that contain
443 * side effects.
444 *
445 * \param key_type A symmetric key type that is compatible with
446 * algorithm \p alg.
447 *
448 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
449 * #PSA_ALG_IS_AEAD(\p alg) is true).
450 *
451 * \return The default nonce size for the specified key type and algorithm.
452 * If the key type or AEAD algorithm is not recognized,
453 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100454 */
455#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100456 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Gilles Peskine3c861642023-07-20 15:10:34 +0200457 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13u : \
458 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12u : \
459 0u : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100460 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine3c861642023-07-20 15:10:34 +0200461 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12u : \
462 0u)
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100463
464/** The maximum default nonce size among all supported pairs of key types and
465 * AEAD algorithms, in bytes.
466 *
467 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
468 * may return.
469 *
470 * \note This is not the maximum size of nonce supported as input to
471 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
472 * just the largest size that may be generated by
473 * #psa_aead_generate_nonce().
474 */
Gilles Peskine3c861642023-07-20 15:10:34 +0200475#define PSA_AEAD_NONCE_MAX_SIZE 13u
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100476
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200477/** A sufficient output buffer size for psa_aead_update().
478 *
479 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200480 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200481 * insufficient buffer size. The actual size of the output may be smaller
482 * in any given call.
483 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100484 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
485 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100486 * \warning This macro may evaluate its arguments multiple times or
487 * zero times, so you should not pass arguments that contain
488 * side effects.
489 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100490 * \param key_type A symmetric key type that is
491 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200492 * \param alg An AEAD algorithm
493 * (\c PSA_ALG_XXX value such that
494 * #PSA_ALG_IS_AEAD(\p alg) is true).
495 * \param input_length Size of the input in bytes.
496 *
497 * \return A sufficient output buffer size for the specified
498 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100499 * If the key type or AEAD algorithm is not
500 * recognized, or the parameters are incompatible,
501 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200502 */
503/* For all the AEAD modes defined in this specification, it is possible
504 * to emit output without delay. However, hardware may not always be
505 * capable of this. So for modes based on a block cipher, allow the
506 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100507#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200508 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
510 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
511 (input_length) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200512 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200513
514/** A sufficient output buffer size for psa_aead_update(), for any of the
515 * supported key types and AEAD algorithms.
516 *
517 * If the size of the output buffer is at least this large, it is guaranteed
518 * that psa_aead_update() will not fail due to an insufficient buffer size.
519 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100520 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200521 *
522 * \param input_length Size of the input in bytes.
523 */
524#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
525 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200526
527/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200528 *
529 * If the size of the ciphertext buffer is at least this large, it is
530 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200531 * insufficient ciphertext buffer size. The actual size of the output may
532 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200533 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100534 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
535 *
536 * \param key_type A symmetric key type that is
537 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200538 * \param alg An AEAD algorithm
539 * (\c PSA_ALG_XXX value such that
540 * #PSA_ALG_IS_AEAD(\p alg) is true).
541 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200542 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200543 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100544 * If the key type or AEAD algorithm is not
545 * recognized, or the parameters are incompatible,
546 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200547 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200548#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
549 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
551 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200552 0u)
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200553
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200554/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
555 * supported key types and AEAD algorithms.
556 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100557 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200558 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200559#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200560
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200561/** A sufficient plaintext buffer size for psa_aead_verify().
562 *
563 * If the size of the plaintext buffer is at least this large, it is
564 * guaranteed that psa_aead_verify() will not fail due to an
565 * insufficient plaintext buffer size. The actual size of the output may
566 * be smaller in any given call.
567 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100568 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
569 *
570 * \param key_type A symmetric key type that is
571 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200572 * \param alg An AEAD algorithm
573 * (\c PSA_ALG_XXX value such that
574 * #PSA_ALG_IS_AEAD(\p alg) is true).
575 *
576 * \return A sufficient plaintext buffer size for the
577 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100578 * If the key type or AEAD algorithm is not
579 * recognized, or the parameters are incompatible,
580 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200581 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200582#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
583 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
585 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200586 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200587
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200588/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
589 * supported key types and AEAD algorithms.
590 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100591 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200592 */
593#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
594
Jaeden Amero7f042142019-02-07 10:44:38 +0000595#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
596 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Gilles Peskine935ff232023-08-09 19:48:02 +0200597 2u * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1u : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200598 11u /*PKCS#1v1.5*/)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100599
600/**
601 * \brief ECDSA signature size for a given curve bit size
602 *
603 * \param curve_bits Curve size in bits.
604 * \return Signature size in bytes.
605 *
606 * \note This macro returns a compile-time constant if its argument is one.
607 */
608#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
Gilles Peskine935ff232023-08-09 19:48:02 +0200609 (PSA_BITS_TO_BYTES(curve_bits) * 2u)
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100610
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100611/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200612 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200613 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200614 * of the specified type and size, with the specified algorithm.
615 * Note that the actual size of the signature may be smaller
616 * (some algorithms produce a variable-size signature).
617 *
618 * \warning This function may call its arguments multiple times or
619 * zero times, so you should not pass arguments that contain
620 * side effects.
621 *
622 * \param key_type An asymmetric key type (this may indifferently be a
623 * key pair type or a public key type).
624 * \param key_bits The size of the key in bits.
625 * \param alg The signature algorithm.
626 *
627 * \return If the parameters are valid and supported, return
628 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100629 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200630 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100631 * If the parameters are a valid combination that is not supported,
632 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200633 * If the parameters are not valid, the
634 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200635 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100636#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200638 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200639 ((void) alg, 0u))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200640
Gilles Peskine29755712019-11-08 15:49:40 +0100641#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
642 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
643
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100644/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100645 *
646 * Maximum size of an asymmetric signature.
647 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100648 * This macro expands to a compile-time constant integer. This value
649 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100650 */
Valerio Settic012a2d2023-07-28 09:34:44 +0200651#define PSA_SIGNATURE_MAX_SIZE 1
Valerio Settia83d9bf2023-07-27 18:15:20 +0200652
Valerio Setti43c5bf42023-07-31 11:06:50 +0200653#if (defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +0200654 (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE > PSA_SIGNATURE_MAX_SIZE)
655#undef PSA_SIGNATURE_MAX_SIZE
656#define PSA_SIGNATURE_MAX_SIZE PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE
657#endif
Valerio Setti43c5bf42023-07-31 11:06:50 +0200658#if (defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN) || defined(PSA_WANT_ALG_RSA_PSS)) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +0200659 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_SIGNATURE_MAX_SIZE)
660#undef PSA_SIGNATURE_MAX_SIZE
661#define PSA_SIGNATURE_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)
662#endif
Gilles Peskine29755712019-11-08 15:49:40 +0100663
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200664/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200665 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200666 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200667 * a key of the specified type and size, with the specified algorithm.
668 * Note that the actual size of the ciphertext may be smaller, depending
669 * on the algorithm.
670 *
671 * \warning This function may call its arguments multiple times or
672 * zero times, so you should not pass arguments that contain
673 * side effects.
674 *
675 * \param key_type An asymmetric key type (this may indifferently be a
676 * key pair type or a public key type).
677 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200678 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200679 *
680 * \return If the parameters are valid and supported, return
681 * a buffer size in bytes that guarantees that
682 * psa_asymmetric_encrypt() will not fail with
683 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100684 * If the parameters are a valid combination that is not supported,
685 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200686 * If the parameters are not valid, the
687 * return value is unspecified.
688 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200689#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
690 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 ((void) alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200692 0u)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200693
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200694/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
695 * supported asymmetric encryption.
696 *
697 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
698 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100699/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200700#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100701 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200702
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200703/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200704 *
Gilles Peskine76689602020-05-05 16:01:22 +0200705 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200706 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200707 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200708 * on the algorithm.
709 *
710 * \warning This function may call its arguments multiple times or
711 * zero times, so you should not pass arguments that contain
712 * side effects.
713 *
714 * \param key_type An asymmetric key type (this may indifferently be a
715 * key pair type or a public key type).
716 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200717 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200718 *
719 * \return If the parameters are valid and supported, return
720 * a buffer size in bytes that guarantees that
721 * psa_asymmetric_decrypt() will not fail with
722 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100723 * If the parameters are a valid combination that is not supported,
724 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200725 * If the parameters are not valid, the
726 * return value is unspecified.
727 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200728#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
729 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
730 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200731 0u)
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200732
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200733/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
734 * supported asymmetric decryption.
735 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100736 * This macro assumes that RSA is the only supported asymmetric encryption.
737 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200738 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
739 */
740#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100741 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200742
Gilles Peskine1be949b2018-08-10 19:06:59 +0200743/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
744 * number of bits.
745 *
746 * This definition assumes that bits <= 2^19 - 9 so that the length field
747 * is at most 3 bytes. The length of the encoding is the length of the
748 * bit string padded to a whole number of bytes plus:
749 * - 1 type byte;
750 * - 1 to 3 length bytes;
751 * - 0 to 1 bytes of leading 0 due to the sign bit.
752 */
753#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200754 ((bits) / 8u + 5u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200755
756/* Maximum size of the export encoding of an RSA public key.
757 * Assumes that the public exponent is less than 2^32.
758 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200759 * RSAPublicKey ::= SEQUENCE {
760 * modulus INTEGER, -- n
761 * publicExponent INTEGER } -- e
762 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000763 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200764 * - n : INTEGER;
765 * - 7 bytes for the public exponent.
766 */
767#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200768 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200769
770/* Maximum size of the export encoding of an RSA key pair.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000771 * Assumes that the public exponent is less than 2^32 and that the size
Gilles Peskine1be949b2018-08-10 19:06:59 +0200772 * difference between the two primes is at most 1 bit.
773 *
774 * RSAPrivateKey ::= SEQUENCE {
775 * version Version, -- 0
776 * modulus INTEGER, -- N-bit
777 * publicExponent INTEGER, -- 32-bit
778 * privateExponent INTEGER, -- N-bit
779 * prime1 INTEGER, -- N/2-bit
780 * prime2 INTEGER, -- N/2-bit
781 * exponent1 INTEGER, -- N/2-bit
782 * exponent2 INTEGER, -- N/2-bit
783 * coefficient INTEGER, -- N/2-bit
784 * }
785 *
786 * - 4 bytes of SEQUENCE overhead;
787 * - 3 bytes of version;
788 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
789 * overapproximated as 9 half-size INTEGERS;
790 * - 7 bytes for the public exponent.
791 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200792#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200793 (9u * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2u + 1u) + 14u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200794
795/* Maximum size of the export encoding of a DSA public key.
796 *
797 * SubjectPublicKeyInfo ::= SEQUENCE {
798 * algorithm AlgorithmIdentifier,
799 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
800 * AlgorithmIdentifier ::= SEQUENCE {
801 * algorithm OBJECT IDENTIFIER,
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400802 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 * DSAPublicKey ::= INTEGER -- public key, Y
804 *
805 * - 3 * 4 bytes of SEQUENCE overhead;
806 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
807 * - 4 bytes of BIT STRING overhead;
808 * - 3 full-size INTEGERs (p, g, y);
809 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
810 */
811#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200812 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 59u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200813
814/* Maximum size of the export encoding of a DSA key pair.
815 *
816 * DSAPrivateKey ::= SEQUENCE {
817 * version Version, -- 0
818 * prime INTEGER, -- p
819 * subprime INTEGER, -- q
820 * generator INTEGER, -- g
821 * public INTEGER, -- y
822 * private INTEGER, -- x
823 * }
824 *
825 * - 4 bytes of SEQUENCE overhead;
826 * - 3 bytes of version;
827 * - 3 full-size INTEGERs (p, g, y);
828 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
829 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200830#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200831 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3u + 75u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200832
833/* Maximum size of the export encoding of an ECC public key.
834 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000835 * The representation of an ECC public key is:
836 * - The byte 0x04;
837 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
838 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
839 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200840 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000841 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200842 */
843#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Gilles Peskine3c861642023-07-20 15:10:34 +0200844 (2u * PSA_BITS_TO_BYTES(key_bits) + 1u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200845
846/* Maximum size of the export encoding of an ECC key pair.
847 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100848 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200849 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200850#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100851 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200852
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200853/* Maximum size of the export encoding of an DH key pair.
Przemek Stekieled23b612022-12-01 15:00:41 +0100854 *
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200855 * An DH key pair is represented by the secret value.
Przemek Stekieled23b612022-12-01 15:00:41 +0100856 */
857#define PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(key_bits) \
858 (PSA_BITS_TO_BYTES(key_bits))
859
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200860/* Maximum size of the export encoding of an DH public key.
Przemek Stekieled23b612022-12-01 15:00:41 +0100861 */
862#define PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(key_bits) \
863 (PSA_BITS_TO_BYTES(key_bits))
864
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100865/** Sufficient output buffer size for psa_export_key() or
866 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200867 *
868 * This macro returns a compile-time constant if its arguments are
869 * compile-time constants.
870 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100871 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200872 * zero times, so you should not pass arguments that contain
873 * side effects.
874 *
875 * The following code illustrates how to allocate enough memory to export
876 * a key by querying the key type and size at runtime.
877 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200878 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200879 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200880 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200881 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200882 * psa_key_type_t key_type = psa_get_key_type(&attributes);
883 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100884 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200885 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200886 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200887 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200888 * size_t buffer_length;
889 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
890 * if (status != PSA_SUCCESS) handle_error(...);
891 * \endcode
892 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200893 * \param key_type A supported key type.
894 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200895 *
896 * \return If the parameters are valid and supported, return
897 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100898 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200899 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100900 * If the parameters are a valid combination that is not supported,
901 * return either a sensible size or 0.
902 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200903 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100904#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
905 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100906 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100907 (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 +0200908 (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 +0100909 (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 +0200910 (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 +0100911 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
912 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 +0200913 0u)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200914
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200915/** Sufficient output buffer size for psa_export_public_key().
916 *
917 * This macro returns a compile-time constant if its arguments are
918 * compile-time constants.
919 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100920 * \warning This macro may evaluate its arguments multiple times or
921 * zero times, so you should not pass arguments that contain
922 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200923 *
924 * The following code illustrates how to allocate enough memory to export
925 * a public key by querying the key type and size at runtime.
926 * \code{c}
927 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
928 * psa_status_t status;
929 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100930 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200931 * psa_key_type_t key_type = psa_get_key_type(&attributes);
932 * size_t key_bits = psa_get_key_bits(&attributes);
933 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
934 * psa_reset_key_attributes(&attributes);
935 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100936 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200937 * size_t buffer_length;
938 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100939 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200940 * \endcode
941 *
942 * \param key_type A public key or key pair key type.
943 * \param key_bits The size of the key in bits.
944 *
945 * \return If the parameters are valid and supported, return
946 * a buffer size in bytes that guarantees that
947 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100948 * #PSA_ERROR_BUFFER_TOO_SMALL.
949 * If the parameters are a valid combination that is not
950 * supported, return either a sensible size or 0.
951 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200952 * the return value is unspecified.
953 *
954 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100955 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200956 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
957 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
958 * \p key_bits).
959 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100960#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
961 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
962 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Przemek Stekieled23b612022-12-01 15:00:41 +0100963 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Gilles Peskine3c861642023-07-20 15:10:34 +0200964 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200965
966/** Sufficient buffer size for exporting any asymmetric key pair.
967 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100968 * This macro expands to a compile-time constant integer. This value is
969 * a sufficient buffer size when calling psa_export_key() to export any
970 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200971 *
972 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
973 */
Valerio Settic012a2d2023-07-28 09:34:44 +0200974#define PSA_EXPORT_KEY_PAIR_MAX_SIZE 1
Valerio Settia83d9bf2023-07-27 18:15:20 +0200975
976#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) && \
977 (PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
978 PSA_EXPORT_KEY_PAIR_MAX_SIZE)
979#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
980#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
981 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
982#endif
983#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) && \
984 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
985 PSA_EXPORT_KEY_PAIR_MAX_SIZE)
986#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
987#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
988 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
989#endif
990#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_BASIC) && \
991 (PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \
992 PSA_EXPORT_KEY_PAIR_MAX_SIZE)
993#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
994#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
Valerio Setti644e01d2023-07-28 09:31:51 +0200995 PSA_KEY_EXPORT_FFDH_KEY_PAIR_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
Valerio Settia83d9bf2023-07-27 18:15:20 +0200996#endif
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200997
998/** Sufficient buffer size for exporting any asymmetric public key.
999 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001000 * This macro expands to a compile-time constant integer. This value is
1001 * a sufficient buffer size when calling psa_export_key() or
1002 * psa_export_public_key() to export any asymmetric public key,
1003 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001004 *
1005 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
1006 */
Valerio Settic012a2d2023-07-28 09:34:44 +02001007#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 1
Przemek Stekiel654bef02022-12-15 13:28:02 +01001008
Valerio Settia83d9bf2023-07-27 18:15:20 +02001009#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \
1010 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
1011 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1012#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
1013#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1014 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
1015#endif
1016#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) && \
1017 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
1018 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1019#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
1020#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1021 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
1022#endif
1023#if defined(PSA_WANT_KEY_TYPE_DH_PUBLIC_KEY) && \
1024 (PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) > \
1025 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1026#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
1027#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1028 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
1029#endif
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001030
1031/** Sufficient output buffer size for psa_raw_key_agreement().
1032 *
1033 * This macro returns a compile-time constant if its arguments are
1034 * compile-time constants.
1035 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001036 * \warning This macro may evaluate its arguments multiple times or
1037 * zero times, so you should not pass arguments that contain
1038 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001039 *
1040 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
1041 *
1042 * \param key_type A supported key type.
1043 * \param key_bits The size of the key in bits.
1044 *
1045 * \return If the parameters are valid and supported, return
1046 * a buffer size in bytes that guarantees that
1047 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001048 * #PSA_ERROR_BUFFER_TOO_SMALL.
1049 * If the parameters are a valid combination that
1050 * is not supported, return either a sensible size or 0.
1051 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001052 * the return value is unspecified.
1053 */
1054#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
Przemek Stekiel654bef02022-12-15 13:28:02 +01001055 ((PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) || \
Gilles Peskine3c861642023-07-20 15:10:34 +02001056 PSA_KEY_TYPE_IS_DH_KEY_PAIR(key_type)) ? PSA_BITS_TO_BYTES(key_bits) : 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001057
1058/** Maximum size of the output from psa_raw_key_agreement().
1059 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001060 * This macro expands to a compile-time constant integer. This value is the
1061 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001062 *
1063 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
1064 */
Valerio Settic012a2d2023-07-28 09:34:44 +02001065#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE 1
Valerio Settia83d9bf2023-07-27 18:15:20 +02001066
Valerio Setti43c5bf42023-07-31 11:06:50 +02001067#if defined(PSA_WANT_ALG_ECDH) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +02001068 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE)
1069#undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE
1070#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)
1071#endif
Valerio Setti43c5bf42023-07-31 11:06:50 +02001072#if defined(PSA_WANT_ALG_FFDH) && \
Valerio Settia83d9bf2023-07-27 18:15:20 +02001073 (PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS) > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE)
1074#undef PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE
1075#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE PSA_BITS_TO_BYTES(PSA_VENDOR_FFDH_MAX_KEY_BITS)
1076#endif
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001077
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001078/** The default IV size for a cipher algorithm, in bytes.
1079 *
1080 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
1081 * the default IV length for the algorithm.
1082 *
1083 * This macro can be used to allocate a buffer of sufficient size to
1084 * store the IV output from #psa_cipher_generate_iv() when using
1085 * a multi-part cipher operation.
1086 *
1087 * See also #PSA_CIPHER_IV_MAX_SIZE.
1088 *
1089 * \warning This macro may evaluate its arguments multiple times or
1090 * zero times, so you should not pass arguments that contain
1091 * side effects.
1092 *
1093 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
1094 *
1095 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
1096 *
1097 * \return The default IV size for the specified key type and algorithm.
1098 * If the algorithm does not use an IV, return 0.
1099 * If the key type or cipher algorithm is not recognized,
1100 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001101 */
1102#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001103 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 ((alg) == PSA_ALG_CTR || \
1105 (alg) == PSA_ALG_CFB || \
1106 (alg) == PSA_ALG_OFB || \
1107 (alg) == PSA_ALG_XTS || \
1108 (alg) == PSA_ALG_CBC_NO_PADDING || \
1109 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001110 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Gilles Peskine3c861642023-07-20 15:10:34 +02001111 (alg) == PSA_ALG_STREAM_CIPHER ? 12u : \
1112 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13u : \
1113 0u)
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001114
1115/** The maximum IV size for all supported cipher algorithms, in bytes.
1116 *
1117 * See also #PSA_CIPHER_IV_LENGTH().
1118 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001119#define PSA_CIPHER_IV_MAX_SIZE 16u
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001120
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001121/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1122 *
1123 * If the size of the output buffer is at least this large, it is guaranteed
1124 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1125 * Depending on the algorithm, the actual size of the output might be smaller.
1126 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001127 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1128 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001129 * \warning This macro may evaluate its arguments multiple times or
1130 * zero times, so you should not pass arguments that contain
1131 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001132 *
1133 * \param key_type A symmetric key type that is compatible with algorithm
1134 * alg.
1135 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1136 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1137 * \param input_length Size of the input in bytes.
1138 *
1139 * \return A sufficient output size for the specified key type and
1140 * algorithm. If the key type or cipher algorithm is not
1141 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001142 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001143 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001144#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1145 (alg == PSA_ALG_CBC_PKCS7 ? \
1146 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1147 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1148 (input_length) + 1u) + \
1149 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0u) : \
1150 (PSA_ALG_IS_CIPHER(alg) ? \
1151 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1152 0u))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001153
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001154/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1155 * supported key types and cipher algorithms.
1156 *
1157 * If the size of the output buffer is at least this large, it is guaranteed
1158 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1159 *
1160 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1161 *
1162 * \param input_length Size of the input in bytes.
1163 *
1164 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001165#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1166 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1167 (input_length) + 1u) + \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001168 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001169
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001170/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1171 *
1172 * If the size of the output buffer is at least this large, it is guaranteed
1173 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1174 * Depending on the algorithm, the actual size of the output might be smaller.
1175 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001176 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001177 *
1178 * \param key_type A symmetric key type that is compatible with algorithm
1179 * alg.
1180 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1181 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1182 * \param input_length Size of the input in bytes.
1183 *
1184 * \return A sufficient output size for the specified key type and
1185 * algorithm. If the key type or cipher algorithm is not
1186 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001187 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001188 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001189#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1190 (PSA_ALG_IS_CIPHER(alg) && \
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001191 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
Gilles Peskine3c861642023-07-20 15:10:34 +02001192 (input_length) : \
1193 0u)
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001194
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001195/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1196 * supported key types and cipher algorithms.
1197 *
1198 * If the size of the output buffer is at least this large, it is guaranteed
1199 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1200 *
1201 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1202 *
1203 * \param input_length Size of the input in bytes.
1204 */
1205#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1206 (input_length)
1207
1208/** A sufficient output buffer size for psa_cipher_update().
1209 *
1210 * If the size of the output buffer is at least this large, it is guaranteed
1211 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1212 * The actual size of the output might be smaller in any given call.
1213 *
1214 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1215 *
1216 * \param key_type A symmetric key type that is compatible with algorithm
1217 * alg.
1218 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1219 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1220 * \param input_length Size of the input in bytes.
1221 *
1222 * \return A sufficient output size for the specified key type and
1223 * algorithm. If the key type or cipher algorithm is not
1224 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001225 */
Gilles Peskine3c861642023-07-20 15:10:34 +02001226#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1227 (PSA_ALG_IS_CIPHER(alg) ? \
1228 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1229 (((alg) == PSA_ALG_CBC_PKCS7 || \
1230 (alg) == PSA_ALG_CBC_NO_PADDING || \
1231 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1232 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1233 input_length) : \
1234 (input_length)) : 0u) : \
1235 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001236
1237/** A sufficient output buffer size for psa_cipher_update(), for any of the
1238 * supported key types and cipher algorithms.
1239 *
1240 * If the size of the output buffer is at least this large, it is guaranteed
1241 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1242 *
1243 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1244 *
1245 * \param input_length Size of the input in bytes.
1246 */
1247#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001248 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001249
1250/** A sufficient ciphertext buffer size for psa_cipher_finish().
1251 *
1252 * If the size of the ciphertext buffer is at least this large, it is
1253 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1254 * ciphertext buffer size. The actual size of the output might be smaller in
1255 * any given call.
1256 *
1257 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1258 *
1259 * \param key_type A symmetric key type that is compatible with algorithm
1260 * alg.
1261 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1262 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1263 * \return A sufficient output size for the specified key type and
1264 * algorithm. If the key type or cipher algorithm is not
1265 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001266 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001267#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1268 (PSA_ALG_IS_CIPHER(alg) ? \
1269 (alg == PSA_ALG_CBC_PKCS7 ? \
1270 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine3c861642023-07-20 15:10:34 +02001271 0u) : \
1272 0u)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001273
1274/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1275 * supported key types and cipher algorithms.
1276 *
1277 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1278 */
1279#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1280 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1281
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001282#endif /* PSA_CRYPTO_SIZES_H */