blob: e2ae5965d4f98f8084d40e9d6691c5b6c1227ced [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
43/* Include the Mbed TLS configuration file, the way Mbed TLS does it
44 * in each of its header files. */
45#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amerod58a00d2019-06-07 11:49:59 +010046#include "mbedtls/config.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020047#else
48#include MBEDTLS_CONFIG_FILE
49#endif
50
Gilles Peskinea7c26db2018-12-12 13:42:25 +010051#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
52#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
53
Gilles Peskine248010c2019-05-14 16:08:59 +020054#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
55 (((length) + (block_size) - 1) / (block_size) * (block_size))
56
Gilles Peskinea7c26db2018-12-12 13:42:25 +010057/** The size of the output of psa_hash_finish(), in bytes.
58 *
59 * This is also the hash size that psa_hash_verify() expects.
60 *
61 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
62 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
63 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
64 * hash algorithm).
65 *
66 * \return The hash size for the specified hash algorithm.
67 * If the hash algorithm is not recognized, return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +010068 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010069#define PSA_HASH_LENGTH(alg) \
70 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010071 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
86 0)
87
Mateusz Starzyk272a3d42021-08-26 13:28:46 +020088/** The input block size of a hash algorithm, in bytes.
89 *
90 * Hash algorithms process their input data in blocks. Hash operations will
91 * retain any partial blocks until they have enough input to fill the block or
92 * until the operation is finished.
93 * This affects the output from psa_hash_suspend().
94 *
95 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
96 * PSA_ALG_IS_HASH(\p alg) is true).
97 *
98 * \return The block size in bytes for the specified hash algorithm.
99 * If the hash algorithm is not recognized, return 0.
100 * An implementation can return either 0 or the correct size for a
101 * hash algorithm that it recognizes, but does not support.
102 */
103#define PSA_HASH_BLOCK_LENGTH(alg) \
104 ( \
105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 64 : \
107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
109 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
110 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
111 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
112 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
113 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
114 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
115 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
116 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
117 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
118 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
119 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
120 0)
121
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200122/** \def PSA_HASH_MAX_SIZE
123 *
124 * Maximum size of a hash.
125 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100126 * This macro expands to a compile-time constant integer. This value
127 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200128 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200129/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
130 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
131 * HMAC-SHA3-512. */
Ronald Cron0859fe22021-10-18 09:10:31 +0200132#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200133#define PSA_HASH_MAX_SIZE 64
134#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
135#else
136#define PSA_HASH_MAX_SIZE 32
137#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
138#endif
139
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200140/** \def PSA_MAC_MAX_SIZE
141 *
142 * Maximum size of a MAC.
143 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100144 * This macro expands to a compile-time constant integer. This value
145 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200146 */
147/* All non-HMAC MACs have a maximum size that's smaller than the
148 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200149/* Note that the encoding of truncated MAC algorithms limits this value
150 * to 64 bytes.
151 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200152#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
153
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100154/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100155 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100156 * This macro can be used to allocate a buffer of sufficient size to store the
157 * tag output from psa_aead_finish().
158 *
159 * See also #PSA_AEAD_TAG_MAX_SIZE.
160 *
161 * \param key_type The type of the AEAD key.
162 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100163 * \param alg An AEAD algorithm
164 * (\c PSA_ALG_XXX value such that
165 * #PSA_ALG_IS_AEAD(\p alg) is true).
166 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200167 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100168 * If the AEAD algorithm does not have an identified
169 * tag that can be distinguished from the rest of
170 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100171 * If the key type or AEAD algorithm is not
172 * recognized, or the parameters are incompatible,
173 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100174 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100175#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200176 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200177 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100178 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100179
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200180/** The maximum tag size for all supported AEAD algorithms, in bytes.
181 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100182 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200183 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100184#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200185
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200186/* The maximum size of an RSA key on this implementation, in bits.
187 * This is a vendor-specific macro.
188 *
189 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
190 * whose parameters fit in a bignum is accepted. However large keys can
191 * induce a large memory usage and long computation times. Unlike other
192 * auxiliary macros in this file and in crypto.h, which reflect how the
193 * library is configured, this macro defines how the library is
194 * configured. This implementation refuses to import or generate an
195 * RSA key whose size is larger than the value defined here.
196 *
197 * Note that an implementation may set different size limits for different
198 * operations, and does not need to accept all key sizes up to the limit. */
199#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
200
201/* The maximum size of an ECC key on this implementation, in bits.
202 * This is a vendor-specific macro. */
203#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
204#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
205#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
206#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
207#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
208#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
209#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
210#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
211#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
212#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
213#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
214#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
215#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
216#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
217#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
218#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
219#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
220#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
221#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
222#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
223#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
224#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
225#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
226#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
227#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
228#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
229#else
230#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
231#endif
232
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100233/** This macro returns the maximum supported length of the PSK for the
234 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100235 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100236 *
237 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100238 *
239 * Quoting RFC 4279, Sect 5.3:
240 * TLS implementations supporting these ciphersuites MUST support
241 * arbitrary PSK identities up to 128 octets in length, and arbitrary
242 * PSKs up to 64 octets in length. Supporting longer identities and
243 * keys is RECOMMENDED.
244 *
245 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100246 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100247 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100248#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100249
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100250/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100251#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200252
Gilles Peskineacd4be32018-07-08 19:56:25 +0200253/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200254 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200255 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200256 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100257 * \warning This macro may evaluate its arguments multiple times or
258 * zero times, so you should not pass arguments that contain
259 * side effects.
260 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200261 * \param key_type The type of the MAC key.
262 * \param key_bits The size of the MAC key in bits.
263 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100264 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200265 *
266 * \return The MAC size for the specified algorithm with
267 * the specified key parameters.
268 * \return 0 if the MAC algorithm is not recognized.
269 * \return Either 0 or the correct size for a MAC algorithm that
270 * the implementation recognizes, but does not support.
271 * \return Unspecified if the key parameters are not consistent
272 * with the algorithm.
273 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100274#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
275 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
276 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
277 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200278 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200279
280/** The maximum size of the output of psa_aead_encrypt(), in bytes.
281 *
282 * If the size of the ciphertext buffer is at least this large, it is
283 * guaranteed that psa_aead_encrypt() will not fail due to an
284 * insufficient buffer size. Depending on the algorithm, the actual size of
285 * the ciphertext may be smaller.
286 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100287 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
288 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +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 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100293 * \param key_type A symmetric key type that is
294 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200295 * \param alg An AEAD algorithm
296 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100297 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200298 * \param plaintext_length Size of the plaintext in bytes.
299 *
300 * \return The AEAD ciphertext size for the specified
301 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100302 * If the key type or AEAD algorithm is not
303 * recognized, or the parameters are incompatible,
304 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200305 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100306#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200307 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200308 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200309 0)
310
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200311/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
312 * supported key types and AEAD algorithms.
313 *
314 * If the size of the ciphertext buffer is at least this large, it is guaranteed
315 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
316 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100317 * \note This macro returns a compile-time constant if its arguments are
318 * compile-time constants.
319 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100320 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
321 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200322 *
323 * \param plaintext_length Size of the plaintext in bytes.
324 *
325 * \return A sufficient output buffer size for any of the
326 * supported key types and AEAD algorithms.
327 *
328 */
329#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
330 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
331
332
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200333/** The maximum size of the output of psa_aead_decrypt(), in bytes.
334 *
335 * If the size of the plaintext buffer is at least this large, it is
336 * guaranteed that psa_aead_decrypt() will not fail due to an
337 * insufficient buffer size. Depending on the algorithm, the actual size of
338 * the plaintext may be smaller.
339 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100340 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
341 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100342 * \warning This macro may evaluate its arguments multiple times or
343 * zero times, so you should not pass arguments that contain
344 * side effects.
345 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100346 * \param key_type A symmetric key type that is
347 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200348 * \param alg An AEAD algorithm
349 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100350 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200351 * \param ciphertext_length Size of the plaintext in bytes.
352 *
353 * \return The AEAD ciphertext size for the specified
354 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100355 * If the key type or AEAD algorithm is not
356 * recognized, or the parameters are incompatible,
357 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200358 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100359#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200360 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
361 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
362 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200363 0)
364
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200365/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
366 * supported key types and AEAD algorithms.
367 *
368 * If the size of the plaintext buffer is at least this large, it is guaranteed
369 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
370 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100371 * \note This macro returns a compile-time constant if its arguments are
372 * compile-time constants.
373 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100374 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
375 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200376 *
377 * \param ciphertext_length Size of the ciphertext in bytes.
378 *
379 * \return A sufficient output buffer size for any of the
380 * supported key types and AEAD algorithms.
381 *
382 */
383#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
384 (ciphertext_length)
385
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100386/** The default nonce size for an AEAD algorithm, in bytes.
387 *
388 * This macro can be used to allocate a buffer of sufficient size to
389 * store the nonce output from #psa_aead_generate_nonce().
390 *
391 * See also #PSA_AEAD_NONCE_MAX_SIZE.
392 *
393 * \note This is not the maximum size of nonce supported as input to
394 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
395 * just the default size that is generated by #psa_aead_generate_nonce().
396 *
397 * \warning This macro may evaluate its arguments multiple times or
398 * zero times, so you should not pass arguments that contain
399 * side effects.
400 *
401 * \param key_type A symmetric key type that is compatible with
402 * algorithm \p alg.
403 *
404 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
405 * #PSA_ALG_IS_AEAD(\p alg) is true).
406 *
407 * \return The default nonce size for the specified key type and algorithm.
408 * If the key type or AEAD algorithm is not recognized,
409 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100410 */
411#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100412 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Bence Szépkútib639d432021-04-21 10:33:54 +0200413 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
414 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100415 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100416 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkútib639d432021-04-21 10:33:54 +0200417 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100418 0)
419
420/** The maximum default nonce size among all supported pairs of key types and
421 * AEAD algorithms, in bytes.
422 *
423 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
424 * may return.
425 *
426 * \note This is not the maximum size of nonce supported as input to
427 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
428 * just the largest size that may be generated by
429 * #psa_aead_generate_nonce().
430 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100431#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100432
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200433/** A sufficient output buffer size for psa_aead_update().
434 *
435 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200436 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200437 * insufficient buffer size. The actual size of the output may be smaller
438 * in any given call.
439 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100440 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
441 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100442 * \warning This macro may evaluate its arguments multiple times or
443 * zero times, so you should not pass arguments that contain
444 * side effects.
445 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100446 * \param key_type A symmetric key type that is
447 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200448 * \param alg An AEAD algorithm
449 * (\c PSA_ALG_XXX value such that
450 * #PSA_ALG_IS_AEAD(\p alg) is true).
451 * \param input_length Size of the input in bytes.
452 *
453 * \return A sufficient output buffer size for the specified
454 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100455 * If the key type or AEAD algorithm is not
456 * recognized, or the parameters are incompatible,
457 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200458 */
459/* For all the AEAD modes defined in this specification, it is possible
460 * to emit output without delay. However, hardware may not always be
461 * capable of this. So for modes based on a block cipher, allow the
462 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100463#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200464 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100465 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
466 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
467 (input_length) : \
468 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200469
470/** A sufficient output buffer size for psa_aead_update(), for any of the
471 * supported key types and AEAD algorithms.
472 *
473 * If the size of the output buffer is at least this large, it is guaranteed
474 * that psa_aead_update() will not fail due to an insufficient buffer size.
475 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100476 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200477 *
478 * \param input_length Size of the input in bytes.
479 */
480#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
481 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200482
483/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200484 *
485 * If the size of the ciphertext buffer is at least this large, it is
486 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200487 * insufficient ciphertext buffer size. The actual size of the output may
488 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200489 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100490 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
491 *
492 * \param key_type A symmetric key type that is
493 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200494 * \param alg An AEAD algorithm
495 * (\c PSA_ALG_XXX value such that
496 * #PSA_ALG_IS_AEAD(\p alg) is true).
497 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200498 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200499 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100500 * If the key type or AEAD algorithm is not
501 * recognized, or the parameters are incompatible,
502 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200503 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200504#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
505 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
506 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
507 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200508 0)
509
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200510/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
511 * supported key types and AEAD algorithms.
512 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100513 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200514 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200515#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200516
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200517/** A sufficient plaintext buffer size for psa_aead_verify().
518 *
519 * If the size of the plaintext buffer is at least this large, it is
520 * guaranteed that psa_aead_verify() will not fail due to an
521 * insufficient plaintext buffer size. The actual size of the output may
522 * be smaller in any given call.
523 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100524 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
525 *
526 * \param key_type A symmetric key type that is
527 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200528 * \param alg An AEAD algorithm
529 * (\c PSA_ALG_XXX value such that
530 * #PSA_ALG_IS_AEAD(\p alg) is true).
531 *
532 * \return A sufficient plaintext buffer size for the
533 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100534 * If the key type or AEAD algorithm is not
535 * recognized, or the parameters are incompatible,
536 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200537 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200538#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
539 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
540 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
541 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200542 0)
543
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200544/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
545 * supported key types and AEAD algorithms.
546 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100547 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200548 */
549#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
550
Jaeden Amero7f042142019-02-07 10:44:38 +0000551#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
552 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100553 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100554 11 /*PKCS#1v1.5*/)
555
556/**
557 * \brief ECDSA signature size for a given curve bit size
558 *
559 * \param curve_bits Curve size in bits.
560 * \return Signature size in bytes.
561 *
562 * \note This macro returns a compile-time constant if its argument is one.
563 */
564#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
565 (PSA_BITS_TO_BYTES(curve_bits) * 2)
566
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100567/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200568 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200569 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200570 * of the specified type and size, with the specified algorithm.
571 * Note that the actual size of the signature may be smaller
572 * (some algorithms produce a variable-size signature).
573 *
574 * \warning This function may call its arguments multiple times or
575 * zero times, so you should not pass arguments that contain
576 * side effects.
577 *
578 * \param key_type An asymmetric key type (this may indifferently be a
579 * key pair type or a public key type).
580 * \param key_bits The size of the key in bits.
581 * \param alg The signature algorithm.
582 *
583 * \return If the parameters are valid and supported, return
584 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100585 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200586 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100587 * If the parameters are a valid combination that is not supported,
588 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200589 * If the parameters are not valid, the
590 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200591 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100592#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200593 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
594 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
595 ((void)alg, 0))
596
Gilles Peskine29755712019-11-08 15:49:40 +0100597#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
598 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
599
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100600/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100601 *
602 * Maximum size of an asymmetric signature.
603 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100604 * This macro expands to a compile-time constant integer. This value
605 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100606 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100607#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100608 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
609 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
610 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
611
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200612/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200613 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200614 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200615 * a key of the specified type and size, with the specified algorithm.
616 * Note that the actual size of the ciphertext may be smaller, depending
617 * on the algorithm.
618 *
619 * \warning This function may call its arguments multiple times or
620 * zero times, so you should not pass arguments that contain
621 * side effects.
622 *
623 * \param key_type An asymmetric key type (this may indifferently be a
624 * key pair type or a public key type).
625 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200626 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200627 *
628 * \return If the parameters are valid and supported, return
629 * a buffer size in bytes that guarantees that
630 * psa_asymmetric_encrypt() will not fail with
631 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100632 * If the parameters are a valid combination that is not supported,
633 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200634 * If the parameters are not valid, the
635 * return value is unspecified.
636 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200637#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
638 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
639 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
640 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200641
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200642/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
643 * supported asymmetric encryption.
644 *
645 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
646 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100647/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200648#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100649 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200650
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200651/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200652 *
Gilles Peskine76689602020-05-05 16:01:22 +0200653 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200654 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200655 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200656 * on the algorithm.
657 *
658 * \warning This function may call its arguments multiple times or
659 * zero times, so you should not pass arguments that contain
660 * side effects.
661 *
662 * \param key_type An asymmetric key type (this may indifferently be a
663 * key pair type or a public key type).
664 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200665 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200666 *
667 * \return If the parameters are valid and supported, return
668 * a buffer size in bytes that guarantees that
669 * psa_asymmetric_decrypt() will not fail with
670 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100671 * If the parameters are a valid combination that is not supported,
672 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200673 * If the parameters are not valid, the
674 * return value is unspecified.
675 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200676#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
677 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
678 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
679 0)
680
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200681/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
682 * supported asymmetric decryption.
683 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100684 * This macro assumes that RSA is the only supported asymmetric encryption.
685 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200686 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
687 */
688#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100689 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200690
Gilles Peskine1be949b2018-08-10 19:06:59 +0200691/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
692 * number of bits.
693 *
694 * This definition assumes that bits <= 2^19 - 9 so that the length field
695 * is at most 3 bytes. The length of the encoding is the length of the
696 * bit string padded to a whole number of bytes plus:
697 * - 1 type byte;
698 * - 1 to 3 length bytes;
699 * - 0 to 1 bytes of leading 0 due to the sign bit.
700 */
701#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
702 ((bits) / 8 + 5)
703
704/* Maximum size of the export encoding of an RSA public key.
705 * Assumes that the public exponent is less than 2^32.
706 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200707 * RSAPublicKey ::= SEQUENCE {
708 * modulus INTEGER, -- n
709 * publicExponent INTEGER } -- e
710 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000711 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200712 * - n : INTEGER;
713 * - 7 bytes for the public exponent.
714 */
715#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000716 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200717
718/* Maximum size of the export encoding of an RSA key pair.
719 * Assumes thatthe public exponent is less than 2^32 and that the size
720 * difference between the two primes is at most 1 bit.
721 *
722 * RSAPrivateKey ::= SEQUENCE {
723 * version Version, -- 0
724 * modulus INTEGER, -- N-bit
725 * publicExponent INTEGER, -- 32-bit
726 * privateExponent INTEGER, -- N-bit
727 * prime1 INTEGER, -- N/2-bit
728 * prime2 INTEGER, -- N/2-bit
729 * exponent1 INTEGER, -- N/2-bit
730 * exponent2 INTEGER, -- N/2-bit
731 * coefficient INTEGER, -- N/2-bit
732 * }
733 *
734 * - 4 bytes of SEQUENCE overhead;
735 * - 3 bytes of version;
736 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
737 * overapproximated as 9 half-size INTEGERS;
738 * - 7 bytes for the public exponent.
739 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200740#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200741 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
742
743/* Maximum size of the export encoding of a DSA public key.
744 *
745 * SubjectPublicKeyInfo ::= SEQUENCE {
746 * algorithm AlgorithmIdentifier,
747 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
748 * AlgorithmIdentifier ::= SEQUENCE {
749 * algorithm OBJECT IDENTIFIER,
750 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
751 * DSAPublicKey ::= INTEGER -- public key, Y
752 *
753 * - 3 * 4 bytes of SEQUENCE overhead;
754 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
755 * - 4 bytes of BIT STRING overhead;
756 * - 3 full-size INTEGERs (p, g, y);
757 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
758 */
759#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
760 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
761
762/* Maximum size of the export encoding of a DSA key pair.
763 *
764 * DSAPrivateKey ::= SEQUENCE {
765 * version Version, -- 0
766 * prime INTEGER, -- p
767 * subprime INTEGER, -- q
768 * generator INTEGER, -- g
769 * public INTEGER, -- y
770 * private INTEGER, -- x
771 * }
772 *
773 * - 4 bytes of SEQUENCE overhead;
774 * - 3 bytes of version;
775 * - 3 full-size INTEGERs (p, g, y);
776 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
777 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200778#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200779 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
780
781/* Maximum size of the export encoding of an ECC public key.
782 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000783 * The representation of an ECC public key is:
784 * - The byte 0x04;
785 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
786 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
787 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200788 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000789 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200790 */
791#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000792 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200793
794/* Maximum size of the export encoding of an ECC key pair.
795 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100796 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200798#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100799 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200800
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100801/** Sufficient output buffer size for psa_export_key() or
802 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 *
804 * This macro returns a compile-time constant if its arguments are
805 * compile-time constants.
806 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100807 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200808 * zero times, so you should not pass arguments that contain
809 * side effects.
810 *
811 * The following code illustrates how to allocate enough memory to export
812 * a key by querying the key type and size at runtime.
813 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200814 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200815 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200816 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200817 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200818 * psa_key_type_t key_type = psa_get_key_type(&attributes);
819 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100820 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200821 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200822 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200823 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200824 * size_t buffer_length;
825 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
826 * if (status != PSA_SUCCESS) handle_error(...);
827 * \endcode
828 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200829 * \param key_type A supported key type.
830 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200831 *
832 * \return If the parameters are valid and supported, return
833 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100834 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200835 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100836 * If the parameters are a valid combination that is not supported,
837 * return either a sensible size or 0.
838 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200839 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100840#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
841 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
842 (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 +0200843 (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 +0100844 (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 +0200845 (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 +0100846 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
847 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 +0200848 0)
849
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200850/** Sufficient output buffer size for psa_export_public_key().
851 *
852 * This macro returns a compile-time constant if its arguments are
853 * compile-time constants.
854 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100855 * \warning This macro may evaluate its arguments multiple times or
856 * zero times, so you should not pass arguments that contain
857 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200858 *
859 * The following code illustrates how to allocate enough memory to export
860 * a public key by querying the key type and size at runtime.
861 * \code{c}
862 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
863 * psa_status_t status;
864 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100865 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200866 * psa_key_type_t key_type = psa_get_key_type(&attributes);
867 * size_t key_bits = psa_get_key_bits(&attributes);
868 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
869 * psa_reset_key_attributes(&attributes);
870 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100871 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200872 * size_t buffer_length;
873 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100874 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200875 * \endcode
876 *
877 * \param key_type A public key or key pair key type.
878 * \param key_bits The size of the key in bits.
879 *
880 * \return If the parameters are valid and supported, return
881 * a buffer size in bytes that guarantees that
882 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100883 * #PSA_ERROR_BUFFER_TOO_SMALL.
884 * If the parameters are a valid combination that is not
885 * supported, return either a sensible size or 0.
886 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200887 * the return value is unspecified.
888 *
889 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100890 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200891 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
892 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
893 * \p key_bits).
894 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100895#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
896 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
897 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200898 0)
899
900/** Sufficient buffer size for exporting any asymmetric key pair.
901 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100902 * This macro expands to a compile-time constant integer. This value is
903 * a sufficient buffer size when calling psa_export_key() to export any
904 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200905 *
906 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
907 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100908#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
909 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
910 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
911 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
912 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200913
914/** Sufficient buffer size for exporting any asymmetric public key.
915 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100916 * This macro expands to a compile-time constant integer. This value is
917 * a sufficient buffer size when calling psa_export_key() or
918 * psa_export_public_key() to export any asymmetric public key,
919 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200920 *
921 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
922 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100923#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
924 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
925 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
926 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
927 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200928
929/** Sufficient output buffer size for psa_raw_key_agreement().
930 *
931 * This macro returns a compile-time constant if its arguments are
932 * compile-time constants.
933 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100934 * \warning This macro may evaluate its arguments multiple times or
935 * zero times, so you should not pass arguments that contain
936 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200937 *
938 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
939 *
940 * \param key_type A supported key type.
941 * \param key_bits The size of the key in bits.
942 *
943 * \return If the parameters are valid and supported, return
944 * a buffer size in bytes that guarantees that
945 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100946 * #PSA_ERROR_BUFFER_TOO_SMALL.
947 * If the parameters are a valid combination that
948 * is not supported, return either a sensible size or 0.
949 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200950 * the return value is unspecified.
951 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100952/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200953#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
954 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100955 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200956 0)
957
958/** Maximum size of the output from psa_raw_key_agreement().
959 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100960 * This macro expands to a compile-time constant integer. This value is the
961 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200962 *
963 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
964 */
965#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100966 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200967
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100968/** The default IV size for a cipher algorithm, in bytes.
969 *
970 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
971 * the default IV length for the algorithm.
972 *
973 * This macro can be used to allocate a buffer of sufficient size to
974 * store the IV output from #psa_cipher_generate_iv() when using
975 * a multi-part cipher operation.
976 *
977 * See also #PSA_CIPHER_IV_MAX_SIZE.
978 *
979 * \warning This macro may evaluate its arguments multiple times or
980 * zero times, so you should not pass arguments that contain
981 * side effects.
982 *
983 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
984 *
985 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
986 *
987 * \return The default IV size for the specified key type and algorithm.
988 * If the algorithm does not use an IV, return 0.
989 * If the key type or cipher algorithm is not recognized,
990 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100991 */
992#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100993 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100994 ((alg) == PSA_ALG_CTR || \
995 (alg) == PSA_ALG_CFB || \
996 (alg) == PSA_ALG_OFB || \
997 (alg) == PSA_ALG_XTS || \
998 (alg) == PSA_ALG_CBC_NO_PADDING || \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100999 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001000 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúticbe39532020-12-08 00:01:31 +01001001 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +01001002 0)
1003
1004/** The maximum IV size for all supported cipher algorithms, in bytes.
1005 *
1006 * See also #PSA_CIPHER_IV_LENGTH().
1007 */
1008#define PSA_CIPHER_IV_MAX_SIZE 16
1009
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001010/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
1011 *
1012 * If the size of the output buffer is at least this large, it is guaranteed
1013 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1014 * Depending on the algorithm, the actual size of the output might be smaller.
1015 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001016 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1017 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001018 * \warning This macro may evaluate its arguments multiple times or
1019 * zero times, so you should not pass arguments that contain
1020 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001021 *
1022 * \param key_type A symmetric key type that is compatible with algorithm
1023 * alg.
1024 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1025 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1026 * \param input_length Size of the input in bytes.
1027 *
1028 * \return A sufficient output size for the specified key type and
1029 * algorithm. If the key type or cipher algorithm is not
1030 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001031 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001032 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001033#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1034 (alg == PSA_ALG_CBC_PKCS7 ? \
Paul Elliott9bc96592021-07-08 16:50:01 +01001035 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001036 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arm9c3b5072021-03-10 15:57:44 +01001037 (input_length) + 1) + \
Paul Elliott7fe62cc2021-07-07 18:34:48 +01001038 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001039 (PSA_ALG_IS_CIPHER(alg) ? \
1040 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1041 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001042
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001043/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1044 * supported key types and cipher algorithms.
1045 *
1046 * If the size of the output buffer is at least this large, it is guaranteed
1047 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1048 *
1049 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1050 *
1051 * \param input_length Size of the input in bytes.
1052 *
1053 */
1054#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1055 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001056 (input_length) + 1) + \
1057 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001058
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001059/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1060 *
1061 * If the size of the output buffer is at least this large, it is guaranteed
1062 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1063 * Depending on the algorithm, the actual size of the output might be smaller.
1064 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001065 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001066 *
1067 * \param key_type A symmetric key type that is compatible with algorithm
1068 * alg.
1069 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1070 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1071 * \param input_length Size of the input in bytes.
1072 *
1073 * \return A sufficient output size for the specified key type and
1074 * algorithm. If the key type or cipher algorithm is not
1075 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001076 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001077 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001078#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1079 (PSA_ALG_IS_CIPHER(alg) && \
1080 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1081 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001082 0)
1083
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001084/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1085 * supported key types and cipher algorithms.
1086 *
1087 * If the size of the output buffer is at least this large, it is guaranteed
1088 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1089 *
1090 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1091 *
1092 * \param input_length Size of the input in bytes.
1093 */
1094#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1095 (input_length)
1096
1097/** A sufficient output buffer size for psa_cipher_update().
1098 *
1099 * If the size of the output buffer is at least this large, it is guaranteed
1100 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1101 * The actual size of the output might be smaller in any given call.
1102 *
1103 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1104 *
1105 * \param key_type A symmetric key type that is compatible with algorithm
1106 * alg.
1107 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1108 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1109 * \param input_length Size of the input in bytes.
1110 *
1111 * \return A sufficient output size for the specified key type and
1112 * algorithm. If the key type or cipher algorithm is not
1113 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001114 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001115#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1116 (PSA_ALG_IS_CIPHER(alg) ? \
Paul Elliottc183f202021-07-08 16:53:42 +01001117 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001118 (((alg) == PSA_ALG_CBC_PKCS7 || \
1119 (alg) == PSA_ALG_CBC_NO_PADDING || \
1120 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1121 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1122 input_length) : \
Paul Elliottc183f202021-07-08 16:53:42 +01001123 (input_length)) : 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001124 0)
1125
1126/** A sufficient output buffer size for psa_cipher_update(), for any of the
1127 * supported key types and cipher algorithms.
1128 *
1129 * If the size of the output buffer is at least this large, it is guaranteed
1130 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1131 *
1132 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1133 *
1134 * \param input_length Size of the input in bytes.
1135 */
1136#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001137 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001138
1139/** A sufficient ciphertext buffer size for psa_cipher_finish().
1140 *
1141 * If the size of the ciphertext buffer is at least this large, it is
1142 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1143 * ciphertext buffer size. The actual size of the output might be smaller in
1144 * any given call.
1145 *
1146 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1147 *
1148 * \param key_type A symmetric key type that is compatible with algorithm
1149 * alg.
1150 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1151 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1152 * \return A sufficient output size for the specified key type and
1153 * algorithm. If the key type or cipher algorithm is not
1154 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001155 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001156#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1157 (PSA_ALG_IS_CIPHER(alg) ? \
1158 (alg == PSA_ALG_CBC_PKCS7 ? \
1159 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1160 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001161 0)
1162
1163/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1164 * supported key types and cipher algorithms.
1165 *
1166 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1167 */
1168#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1169 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1170
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001171#endif /* PSA_CRYPTO_SIZES_H */