blob: 15cdb416a53d344046bec5e8296daf8dbbdb49d0 [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_MD5 ? 16 : \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
84 0)
85
Gilles Peskineaf3baab2018-06-27 22:55:52 +020086/** \def PSA_HASH_MAX_SIZE
87 *
88 * Maximum size of a hash.
89 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +010090 * This macro expands to a compile-time constant integer. This value
91 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +020092 */
Gilles Peskine3052f532018-09-17 14:13:26 +020093/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
94 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
95 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +020096#if defined(MBEDTLS_SHA512_C)
97#define PSA_HASH_MAX_SIZE 64
98#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
99#else
100#define PSA_HASH_MAX_SIZE 32
101#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
102#endif
103
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200104/** \def PSA_MAC_MAX_SIZE
105 *
106 * Maximum size of a MAC.
107 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100108 * This macro expands to a compile-time constant integer. This value
109 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200110 */
111/* All non-HMAC MACs have a maximum size that's smaller than the
112 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200113/* Note that the encoding of truncated MAC algorithms limits this value
114 * to 64 bytes.
115 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200116#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
117
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100118/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100119 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100120 * This macro can be used to allocate a buffer of sufficient size to store the
121 * tag output from psa_aead_finish().
122 *
123 * See also #PSA_AEAD_TAG_MAX_SIZE.
124 *
125 * \param key_type The type of the AEAD key.
126 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100127 * \param alg An AEAD algorithm
128 * (\c PSA_ALG_XXX value such that
129 * #PSA_ALG_IS_AEAD(\p alg) is true).
130 *
Bence Szépkútibd98df72021-04-27 04:37:18 +0200131 * \return The tag length for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100132 * If the AEAD algorithm does not have an identified
133 * tag that can be distinguished from the rest of
134 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100135 * If the key type or AEAD algorithm is not
136 * recognized, or the parameters are incompatible,
137 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100138 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100139#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200140 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200141 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100142 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100143
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200144/** The maximum tag size for all supported AEAD algorithms, in bytes.
145 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100146 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200147 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100148#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200149
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200150/* The maximum size of an RSA key on this implementation, in bits.
151 * This is a vendor-specific macro.
152 *
153 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
154 * whose parameters fit in a bignum is accepted. However large keys can
155 * induce a large memory usage and long computation times. Unlike other
156 * auxiliary macros in this file and in crypto.h, which reflect how the
157 * library is configured, this macro defines how the library is
158 * configured. This implementation refuses to import or generate an
159 * RSA key whose size is larger than the value defined here.
160 *
161 * Note that an implementation may set different size limits for different
162 * operations, and does not need to accept all key sizes up to the limit. */
163#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
164
165/* The maximum size of an ECC key on this implementation, in bits.
166 * This is a vendor-specific macro. */
167#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
168#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
169#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
170#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
171#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
172#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
173#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
174#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
175#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
176#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
177#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
178#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
179#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
180#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
181#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
182#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
183#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
184#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
185#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
186#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
187#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
188#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
189#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
190#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
191#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
192#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
193#else
194#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
195#endif
196
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100197/** This macro returns the maximum supported length of the PSK for the
198 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100199 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100200 *
201 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100202 *
203 * Quoting RFC 4279, Sect 5.3:
204 * TLS implementations supporting these ciphersuites MUST support
205 * arbitrary PSK identities up to 128 octets in length, and arbitrary
206 * PSKs up to 64 octets in length. Supporting longer identities and
207 * keys is RECOMMENDED.
208 *
209 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100210 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100211 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100212#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100213
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100214/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100215#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200216
Gilles Peskineacd4be32018-07-08 19:56:25 +0200217/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200218 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200219 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200220 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100221 * \warning This macro may evaluate its arguments multiple times or
222 * zero times, so you should not pass arguments that contain
223 * side effects.
224 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200225 * \param key_type The type of the MAC key.
226 * \param key_bits The size of the MAC key in bits.
227 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100228 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200229 *
230 * \return The MAC size for the specified algorithm with
231 * the specified key parameters.
232 * \return 0 if the MAC algorithm is not recognized.
233 * \return Either 0 or the correct size for a MAC algorithm that
234 * the implementation recognizes, but does not support.
235 * \return Unspecified if the key parameters are not consistent
236 * with the algorithm.
237 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100238#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
239 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
240 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
241 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200242 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200243
244/** The maximum size of the output of psa_aead_encrypt(), in bytes.
245 *
246 * If the size of the ciphertext buffer is at least this large, it is
247 * guaranteed that psa_aead_encrypt() will not fail due to an
248 * insufficient buffer size. Depending on the algorithm, the actual size of
249 * the ciphertext may be smaller.
250 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100251 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
252 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100253 * \warning This macro may evaluate its arguments multiple times or
254 * zero times, so you should not pass arguments that contain
255 * side effects.
256 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100257 * \param key_type A symmetric key type that is
258 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200259 * \param alg An AEAD algorithm
260 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100261 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200262 * \param plaintext_length Size of the plaintext in bytes.
263 *
264 * \return The AEAD ciphertext size for the specified
265 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100266 * If the key type or AEAD algorithm is not
267 * recognized, or the parameters are incompatible,
268 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200269 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100270#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200271 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200272 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200273 0)
274
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200275/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
276 * supported key types and AEAD algorithms.
277 *
278 * If the size of the ciphertext buffer is at least this large, it is guaranteed
279 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
280 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100281 * \note This macro returns a compile-time constant if its arguments are
282 * compile-time constants.
283 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100284 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
285 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200286 *
287 * \param plaintext_length Size of the plaintext in bytes.
288 *
289 * \return A sufficient output buffer size for any of the
290 * supported key types and AEAD algorithms.
291 *
292 */
293#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
294 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
295
296
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297/** The maximum size of the output of psa_aead_decrypt(), in bytes.
298 *
299 * If the size of the plaintext buffer is at least this large, it is
300 * guaranteed that psa_aead_decrypt() will not fail due to an
301 * insufficient buffer size. Depending on the algorithm, the actual size of
302 * the plaintext may be smaller.
303 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100304 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
305 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100306 * \warning This macro may evaluate its arguments multiple times or
307 * zero times, so you should not pass arguments that contain
308 * side effects.
309 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100310 * \param key_type A symmetric key type that is
311 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200312 * \param alg An AEAD algorithm
313 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100314 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200315 * \param ciphertext_length Size of the plaintext in bytes.
316 *
317 * \return The AEAD ciphertext size for the specified
318 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100319 * If the key type or AEAD algorithm is not
320 * recognized, or the parameters are incompatible,
321 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200322 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100323#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkúti1dda21c2021-04-21 11:09:50 +0200324 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
325 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
326 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200327 0)
328
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200329/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
330 * supported key types and AEAD algorithms.
331 *
332 * If the size of the plaintext buffer is at least this large, it is guaranteed
333 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
334 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100335 * \note This macro returns a compile-time constant if its arguments are
336 * compile-time constants.
337 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100338 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
339 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200340 *
341 * \param ciphertext_length Size of the ciphertext in bytes.
342 *
343 * \return A sufficient output buffer size for any of the
344 * supported key types and AEAD algorithms.
345 *
346 */
347#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
348 (ciphertext_length)
349
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100350/** The default nonce size for an AEAD algorithm, in bytes.
351 *
352 * This macro can be used to allocate a buffer of sufficient size to
353 * store the nonce output from #psa_aead_generate_nonce().
354 *
355 * See also #PSA_AEAD_NONCE_MAX_SIZE.
356 *
357 * \note This is not the maximum size of nonce supported as input to
358 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
359 * just the default size that is generated by #psa_aead_generate_nonce().
360 *
361 * \warning This macro may evaluate its arguments multiple times or
362 * zero times, so you should not pass arguments that contain
363 * side effects.
364 *
365 * \param key_type A symmetric key type that is compatible with
366 * algorithm \p alg.
367 *
368 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
369 * #PSA_ALG_IS_AEAD(\p alg) is true).
370 *
371 * \return The default nonce size for the specified key type and algorithm.
372 * If the key type or AEAD algorithm is not recognized,
373 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100374 */
375#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100376 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Bence Szépkútib639d432021-04-21 10:33:54 +0200377 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
378 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100379 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100380 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkútib639d432021-04-21 10:33:54 +0200381 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100382 0)
383
384/** The maximum default nonce size among all supported pairs of key types and
385 * AEAD algorithms, in bytes.
386 *
387 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
388 * may return.
389 *
390 * \note This is not the maximum size of nonce supported as input to
391 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
392 * just the largest size that may be generated by
393 * #psa_aead_generate_nonce().
394 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100395#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100396
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200397/** A sufficient output buffer size for psa_aead_update().
398 *
399 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200400 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200401 * insufficient buffer size. The actual size of the output may be smaller
402 * in any given call.
403 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100404 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
405 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100406 * \warning This macro may evaluate its arguments multiple times or
407 * zero times, so you should not pass arguments that contain
408 * side effects.
409 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100410 * \param key_type A symmetric key type that is
411 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200412 * \param alg An AEAD algorithm
413 * (\c PSA_ALG_XXX value such that
414 * #PSA_ALG_IS_AEAD(\p alg) is true).
415 * \param input_length Size of the input in bytes.
416 *
417 * \return A sufficient output buffer size for the specified
418 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100419 * If the key type or AEAD algorithm is not
420 * recognized, or the parameters are incompatible,
421 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200422 */
423/* For all the AEAD modes defined in this specification, it is possible
424 * to emit output without delay. However, hardware may not always be
425 * capable of this. So for modes based on a block cipher, allow the
426 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100427#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200428 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100429 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
430 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
431 (input_length) : \
432 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200433
434/** A sufficient output buffer size for psa_aead_update(), for any of the
435 * supported key types and AEAD algorithms.
436 *
437 * If the size of the output buffer is at least this large, it is guaranteed
438 * that psa_aead_update() will not fail due to an insufficient buffer size.
439 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100440 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200441 *
442 * \param input_length Size of the input in bytes.
443 */
444#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
445 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200446
447/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200448 *
449 * If the size of the ciphertext buffer is at least this large, it is
450 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200451 * insufficient ciphertext buffer size. The actual size of the output may
452 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200453 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100454 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
455 *
456 * \param key_type A symmetric key type that is
457 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200458 * \param alg An AEAD algorithm
459 * (\c PSA_ALG_XXX value such that
460 * #PSA_ALG_IS_AEAD(\p alg) is true).
461 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200462 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200463 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100464 * If the key type or AEAD algorithm is not
465 * recognized, or the parameters are incompatible,
466 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200467 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200468#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
469 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
470 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
471 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200472 0)
473
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200474/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
475 * supported key types and AEAD algorithms.
476 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100477 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200478 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200479#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200480
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200481/** A sufficient plaintext buffer size for psa_aead_verify().
482 *
483 * If the size of the plaintext buffer is at least this large, it is
484 * guaranteed that psa_aead_verify() will not fail due to an
485 * insufficient plaintext buffer size. The actual size of the output may
486 * be smaller in any given call.
487 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100488 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
489 *
490 * \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 *
496 * \return A sufficient plaintext buffer size for the
497 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100498 * If the key type or AEAD algorithm is not
499 * recognized, or the parameters are incompatible,
500 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200501 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200502#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
503 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
504 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
505 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200506 0)
507
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200508/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
509 * supported key types and AEAD algorithms.
510 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100511 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200512 */
513#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
514
Jaeden Amero7f042142019-02-07 10:44:38 +0000515#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
516 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100517 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100518 11 /*PKCS#1v1.5*/)
519
520/**
521 * \brief ECDSA signature size for a given curve bit size
522 *
523 * \param curve_bits Curve size in bits.
524 * \return Signature size in bytes.
525 *
526 * \note This macro returns a compile-time constant if its argument is one.
527 */
528#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
529 (PSA_BITS_TO_BYTES(curve_bits) * 2)
530
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100531/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200532 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200533 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200534 * of the specified type and size, with the specified algorithm.
535 * Note that the actual size of the signature may be smaller
536 * (some algorithms produce a variable-size signature).
537 *
538 * \warning This function may call its arguments multiple times or
539 * zero times, so you should not pass arguments that contain
540 * side effects.
541 *
542 * \param key_type An asymmetric key type (this may indifferently be a
543 * key pair type or a public key type).
544 * \param key_bits The size of the key in bits.
545 * \param alg The signature algorithm.
546 *
547 * \return If the parameters are valid and supported, return
548 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100549 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200550 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100551 * If the parameters are a valid combination that is not supported,
552 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200553 * If the parameters are not valid, the
554 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200555 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200557 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
558 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
559 ((void)alg, 0))
560
Gilles Peskine29755712019-11-08 15:49:40 +0100561#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
562 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
563
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100564/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100565 *
566 * Maximum size of an asymmetric signature.
567 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100568 * This macro expands to a compile-time constant integer. This value
569 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100570 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100571#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100572 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
573 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
574 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
575
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200576/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200577 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200578 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200579 * a key of the specified type and size, with the specified algorithm.
580 * Note that the actual size of the ciphertext may be smaller, depending
581 * on the algorithm.
582 *
583 * \warning This function may call its arguments multiple times or
584 * zero times, so you should not pass arguments that contain
585 * side effects.
586 *
587 * \param key_type An asymmetric key type (this may indifferently be a
588 * key pair type or a public key type).
589 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200590 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200591 *
592 * \return If the parameters are valid and supported, return
593 * a buffer size in bytes that guarantees that
594 * psa_asymmetric_encrypt() will not fail with
595 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100596 * If the parameters are a valid combination that is not supported,
597 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200598 * If the parameters are not valid, the
599 * return value is unspecified.
600 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200601#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
602 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
603 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
604 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200605
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200606/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
607 * supported asymmetric encryption.
608 *
609 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
610 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100611/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200612#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100613 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200614
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200615/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200616 *
Gilles Peskine76689602020-05-05 16:01:22 +0200617 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200618 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200619 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200620 * on the algorithm.
621 *
622 * \warning This function may call its arguments multiple times or
623 * zero times, so you should not pass arguments that contain
624 * side effects.
625 *
626 * \param key_type An asymmetric key type (this may indifferently be a
627 * key pair type or a public key type).
628 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200629 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200630 *
631 * \return If the parameters are valid and supported, return
632 * a buffer size in bytes that guarantees that
633 * psa_asymmetric_decrypt() will not fail with
634 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100635 * If the parameters are a valid combination that is not supported,
636 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200637 * If the parameters are not valid, the
638 * return value is unspecified.
639 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200640#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
641 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
642 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
643 0)
644
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200645/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
646 * supported asymmetric decryption.
647 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100648 * This macro assumes that RSA is the only supported asymmetric encryption.
649 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200650 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
651 */
652#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100653 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200654
Gilles Peskine1be949b2018-08-10 19:06:59 +0200655/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
656 * number of bits.
657 *
658 * This definition assumes that bits <= 2^19 - 9 so that the length field
659 * is at most 3 bytes. The length of the encoding is the length of the
660 * bit string padded to a whole number of bytes plus:
661 * - 1 type byte;
662 * - 1 to 3 length bytes;
663 * - 0 to 1 bytes of leading 0 due to the sign bit.
664 */
665#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
666 ((bits) / 8 + 5)
667
668/* Maximum size of the export encoding of an RSA public key.
669 * Assumes that the public exponent is less than 2^32.
670 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200671 * RSAPublicKey ::= SEQUENCE {
672 * modulus INTEGER, -- n
673 * publicExponent INTEGER } -- e
674 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000675 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200676 * - n : INTEGER;
677 * - 7 bytes for the public exponent.
678 */
679#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000680 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200681
682/* Maximum size of the export encoding of an RSA key pair.
683 * Assumes thatthe public exponent is less than 2^32 and that the size
684 * difference between the two primes is at most 1 bit.
685 *
686 * RSAPrivateKey ::= SEQUENCE {
687 * version Version, -- 0
688 * modulus INTEGER, -- N-bit
689 * publicExponent INTEGER, -- 32-bit
690 * privateExponent INTEGER, -- N-bit
691 * prime1 INTEGER, -- N/2-bit
692 * prime2 INTEGER, -- N/2-bit
693 * exponent1 INTEGER, -- N/2-bit
694 * exponent2 INTEGER, -- N/2-bit
695 * coefficient INTEGER, -- N/2-bit
696 * }
697 *
698 * - 4 bytes of SEQUENCE overhead;
699 * - 3 bytes of version;
700 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
701 * overapproximated as 9 half-size INTEGERS;
702 * - 7 bytes for the public exponent.
703 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200704#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200705 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
706
707/* Maximum size of the export encoding of a DSA public key.
708 *
709 * SubjectPublicKeyInfo ::= SEQUENCE {
710 * algorithm AlgorithmIdentifier,
711 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
712 * AlgorithmIdentifier ::= SEQUENCE {
713 * algorithm OBJECT IDENTIFIER,
714 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
715 * DSAPublicKey ::= INTEGER -- public key, Y
716 *
717 * - 3 * 4 bytes of SEQUENCE overhead;
718 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
719 * - 4 bytes of BIT STRING overhead;
720 * - 3 full-size INTEGERs (p, g, y);
721 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
722 */
723#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
724 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
725
726/* Maximum size of the export encoding of a DSA key pair.
727 *
728 * DSAPrivateKey ::= SEQUENCE {
729 * version Version, -- 0
730 * prime INTEGER, -- p
731 * subprime INTEGER, -- q
732 * generator INTEGER, -- g
733 * public INTEGER, -- y
734 * private INTEGER, -- x
735 * }
736 *
737 * - 4 bytes of SEQUENCE overhead;
738 * - 3 bytes of version;
739 * - 3 full-size INTEGERs (p, g, y);
740 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
741 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200742#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200743 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
744
745/* Maximum size of the export encoding of an ECC public key.
746 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000747 * The representation of an ECC public key is:
748 * - The byte 0x04;
749 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
750 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
751 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200752 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000753 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200754 */
755#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000756 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200757
758/* Maximum size of the export encoding of an ECC key pair.
759 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100760 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200761 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200762#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100763 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200764
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100765/** Sufficient output buffer size for psa_export_key() or
766 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200767 *
768 * This macro returns a compile-time constant if its arguments are
769 * compile-time constants.
770 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100771 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200772 * zero times, so you should not pass arguments that contain
773 * side effects.
774 *
775 * The following code illustrates how to allocate enough memory to export
776 * a key by querying the key type and size at runtime.
777 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200778 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200779 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200780 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200781 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200782 * psa_key_type_t key_type = psa_get_key_type(&attributes);
783 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100784 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200785 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200786 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200787 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200788 * size_t buffer_length;
789 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
790 * if (status != PSA_SUCCESS) handle_error(...);
791 * \endcode
792 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200793 * \param key_type A supported key type.
794 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200795 *
796 * \return If the parameters are valid and supported, return
797 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100798 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200799 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100800 * If the parameters are a valid combination that is not supported,
801 * return either a sensible size or 0.
802 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100804#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
805 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
806 (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 +0200807 (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 +0100808 (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 +0200809 (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 +0100810 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
811 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 +0200812 0)
813
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200814/** Sufficient output buffer size for psa_export_public_key().
815 *
816 * This macro returns a compile-time constant if its arguments are
817 * compile-time constants.
818 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100819 * \warning This macro may evaluate its arguments multiple times or
820 * zero times, so you should not pass arguments that contain
821 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200822 *
823 * The following code illustrates how to allocate enough memory to export
824 * a public key by querying the key type and size at runtime.
825 * \code{c}
826 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
827 * psa_status_t status;
828 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100829 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200830 * psa_key_type_t key_type = psa_get_key_type(&attributes);
831 * size_t key_bits = psa_get_key_bits(&attributes);
832 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
833 * psa_reset_key_attributes(&attributes);
834 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100835 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200836 * size_t buffer_length;
837 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100838 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200839 * \endcode
840 *
841 * \param key_type A public key or key pair key type.
842 * \param key_bits The size of the key in bits.
843 *
844 * \return If the parameters are valid and supported, return
845 * a buffer size in bytes that guarantees that
846 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100847 * #PSA_ERROR_BUFFER_TOO_SMALL.
848 * If the parameters are a valid combination that is not
849 * supported, return either a sensible size or 0.
850 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200851 * the return value is unspecified.
852 *
853 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100854 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200855 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
856 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
857 * \p key_bits).
858 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100859#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
860 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
861 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 +0200862 0)
863
864/** Sufficient buffer size for exporting any asymmetric key pair.
865 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100866 * This macro expands to a compile-time constant integer. This value is
867 * a sufficient buffer size when calling psa_export_key() to export any
868 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200869 *
870 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
871 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100872#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
873 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
874 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
875 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
876 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200877
878/** Sufficient buffer size for exporting any asymmetric public key.
879 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100880 * This macro expands to a compile-time constant integer. This value is
881 * a sufficient buffer size when calling psa_export_key() or
882 * psa_export_public_key() to export any asymmetric public key,
883 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200884 *
885 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
886 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100887#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
888 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
889 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
890 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
891 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200892
893/** Sufficient output buffer size for psa_raw_key_agreement().
894 *
895 * This macro returns a compile-time constant if its arguments are
896 * compile-time constants.
897 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100898 * \warning This macro may evaluate its arguments multiple times or
899 * zero times, so you should not pass arguments that contain
900 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200901 *
902 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
903 *
904 * \param key_type A supported key type.
905 * \param key_bits The size of the key in bits.
906 *
907 * \return If the parameters are valid and supported, return
908 * a buffer size in bytes that guarantees that
909 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100910 * #PSA_ERROR_BUFFER_TOO_SMALL.
911 * If the parameters are a valid combination that
912 * is not supported, return either a sensible size or 0.
913 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200914 * the return value is unspecified.
915 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100916/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200917#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
918 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100919 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200920 0)
921
922/** Maximum size of the output from psa_raw_key_agreement().
923 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100924 * This macro expands to a compile-time constant integer. This value is the
925 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200926 *
927 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
928 */
929#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100930 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200931
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100932/** The default IV size for a cipher algorithm, in bytes.
933 *
934 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
935 * the default IV length for the algorithm.
936 *
937 * This macro can be used to allocate a buffer of sufficient size to
938 * store the IV output from #psa_cipher_generate_iv() when using
939 * a multi-part cipher operation.
940 *
941 * See also #PSA_CIPHER_IV_MAX_SIZE.
942 *
943 * \warning This macro may evaluate its arguments multiple times or
944 * zero times, so you should not pass arguments that contain
945 * side effects.
946 *
947 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
948 *
949 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
950 *
951 * \return The default IV size for the specified key type and algorithm.
952 * If the algorithm does not use an IV, return 0.
953 * If the key type or cipher algorithm is not recognized,
954 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100955 */
956#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100957 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100958 ((alg) == PSA_ALG_CTR || \
959 (alg) == PSA_ALG_CFB || \
960 (alg) == PSA_ALG_OFB || \
961 (alg) == PSA_ALG_XTS || \
962 (alg) == PSA_ALG_CBC_NO_PADDING || \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100963 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100964 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúticbe39532020-12-08 00:01:31 +0100965 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100966 0)
967
968/** The maximum IV size for all supported cipher algorithms, in bytes.
969 *
970 * See also #PSA_CIPHER_IV_LENGTH().
971 */
972#define PSA_CIPHER_IV_MAX_SIZE 16
973
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200974/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
975 *
976 * If the size of the output buffer is at least this large, it is guaranteed
977 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
978 * Depending on the algorithm, the actual size of the output might be smaller.
979 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200980 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
981 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100982 * \warning This macro may evaluate its arguments multiple times or
983 * zero times, so you should not pass arguments that contain
984 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200985 *
986 * \param key_type A symmetric key type that is compatible with algorithm
987 * alg.
988 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
989 * #PSA_ALG_IS_CIPHER(\p alg) is true).
990 * \param input_length Size of the input in bytes.
991 *
992 * \return A sufficient output size for the specified key type and
993 * algorithm. If the key type or cipher algorithm is not
994 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100995 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200996 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100997#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
998 (alg == PSA_ALG_CBC_PKCS7 ? \
999 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arm9c3b5072021-03-10 15:57:44 +01001000 (input_length) + 1) + \
1001 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001002 (PSA_ALG_IS_CIPHER(alg) ? \
1003 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1004 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001005
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001006/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1007 * supported key types and cipher algorithms.
1008 *
1009 * If the size of the output buffer is at least this large, it is guaranteed
1010 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1011 *
1012 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1013 *
1014 * \param input_length Size of the input in bytes.
1015 *
1016 */
1017#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1018 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001019 (input_length) + 1) + \
1020 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001021
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001022/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1023 *
1024 * If the size of the output buffer is at least this large, it is guaranteed
1025 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1026 * Depending on the algorithm, the actual size of the output might be smaller.
1027 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001028 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001029 *
1030 * \param key_type A symmetric key type that is compatible with algorithm
1031 * alg.
1032 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1033 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1034 * \param input_length Size of the input in bytes.
1035 *
1036 * \return A sufficient output size for the specified key type and
1037 * algorithm. If the key type or cipher algorithm is not
1038 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001039 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001040 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001041#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1042 (PSA_ALG_IS_CIPHER(alg) && \
1043 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1044 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001045 0)
1046
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001047/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1048 * supported key types and cipher algorithms.
1049 *
1050 * If the size of the output buffer is at least this large, it is guaranteed
1051 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1052 *
1053 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1054 *
1055 * \param input_length Size of the input in bytes.
1056 */
1057#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1058 (input_length)
1059
1060/** A sufficient output buffer size for psa_cipher_update().
1061 *
1062 * If the size of the output buffer is at least this large, it is guaranteed
1063 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1064 * The actual size of the output might be smaller in any given call.
1065 *
1066 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1067 *
1068 * \param key_type A symmetric key type that is compatible with algorithm
1069 * alg.
1070 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1071 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1072 * \param input_length Size of the input in bytes.
1073 *
1074 * \return A sufficient output size for the specified key type and
1075 * algorithm. If the key type or cipher algorithm is not
1076 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001077 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001078#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1079 (PSA_ALG_IS_CIPHER(alg) ? \
1080 (((alg) == PSA_ALG_CBC_PKCS7 || \
1081 (alg) == PSA_ALG_CBC_NO_PADDING || \
1082 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1083 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1084 input_length) : \
1085 (input_length)) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001086 0)
1087
1088/** A sufficient output buffer size for psa_cipher_update(), for any of the
1089 * supported key types and cipher algorithms.
1090 *
1091 * If the size of the output buffer is at least this large, it is guaranteed
1092 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1093 *
1094 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1095 *
1096 * \param input_length Size of the input in bytes.
1097 */
1098#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001099 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001100
1101/** A sufficient ciphertext buffer size for psa_cipher_finish().
1102 *
1103 * If the size of the ciphertext buffer is at least this large, it is
1104 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1105 * ciphertext buffer size. The actual size of the output might be smaller in
1106 * any given call.
1107 *
1108 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1109 *
1110 * \param key_type A symmetric key type that is compatible with algorithm
1111 * alg.
1112 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1113 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1114 * \return A sufficient output size for the specified key type and
1115 * algorithm. If the key type or cipher algorithm is not
1116 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001117 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001118#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1119 (PSA_ALG_IS_CIPHER(alg) ? \
1120 (alg == PSA_ALG_CBC_PKCS7 ? \
1121 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1122 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001123 0)
1124
1125/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1126 * supported key types and cipher algorithms.
1127 *
1128 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1129 */
1130#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1131 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1132
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001133#endif /* PSA_CRYPTO_SIZES_H */