blob: bb254d75721dfcfb3548884f6db2fdbda4b367a4 [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
Gilles Peskineaf3baab2018-06-27 22:55:52 +020088/** \def PSA_HASH_MAX_SIZE
89 *
90 * Maximum size of a hash.
91 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +010092 * This macro expands to a compile-time constant integer. This value
93 * is the maximum size of a hash in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +020094 */
Gilles Peskine3052f532018-09-17 14:13:26 +020095/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
96 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
97 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +020098#if defined(MBEDTLS_SHA512_C)
99#define PSA_HASH_MAX_SIZE 64
100#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
101#else
102#define PSA_HASH_MAX_SIZE 32
103#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
104#endif
105
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200106/** \def PSA_MAC_MAX_SIZE
107 *
108 * Maximum size of a MAC.
109 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100110 * This macro expands to a compile-time constant integer. This value
111 * is the maximum size of a MAC in bytes.
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200112 */
113/* All non-HMAC MACs have a maximum size that's smaller than the
114 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200115/* Note that the encoding of truncated MAC algorithms limits this value
116 * to 64 bytes.
117 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200118#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
119
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100120/** The length of a tag for an AEAD algorithm, in bytes.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100121 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100122 * This macro can be used to allocate a buffer of sufficient size to store the
123 * tag output from psa_aead_finish().
124 *
125 * See also #PSA_AEAD_TAG_MAX_SIZE.
126 *
127 * \param key_type The type of the AEAD key.
128 * \param key_bits The size of the AEAD key in bits.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100129 * \param alg An AEAD algorithm
130 * (\c PSA_ALG_XXX value such that
131 * #PSA_ALG_IS_AEAD(\p alg) is true).
132 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100133 * \return The tag size for the specified algorithm and key.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100134 * If the AEAD algorithm does not have an identified
135 * tag that can be distinguished from the rest of
136 * the ciphertext, return 0.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100137 * If the key type or AEAD algorithm is not
138 * recognized, or the parameters are incompatible,
139 * return 0.
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100140 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100141#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200142 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200143 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Bence Szépkúti0d8da392021-03-19 19:28:52 +0100144 ((void) (key_bits), 0))
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100145
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200146/** The maximum tag size for all supported AEAD algorithms, in bytes.
147 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100148 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200149 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100150#define PSA_AEAD_TAG_MAX_SIZE 16
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200151
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200152/* The maximum size of an RSA key on this implementation, in bits.
153 * This is a vendor-specific macro.
154 *
155 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
156 * whose parameters fit in a bignum is accepted. However large keys can
157 * induce a large memory usage and long computation times. Unlike other
158 * auxiliary macros in this file and in crypto.h, which reflect how the
159 * library is configured, this macro defines how the library is
160 * configured. This implementation refuses to import or generate an
161 * RSA key whose size is larger than the value defined here.
162 *
163 * Note that an implementation may set different size limits for different
164 * operations, and does not need to accept all key sizes up to the limit. */
165#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
166
167/* The maximum size of an ECC key on this implementation, in bits.
168 * This is a vendor-specific macro. */
169#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
170#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
171#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
172#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
173#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
174#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
175#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
176#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
177#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
178#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
179#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
180#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
181#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
182#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
183#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
184#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
185#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
186#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
187#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
188#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
189#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
190#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
191#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
192#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
193#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
194#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
195#else
196#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
197#endif
198
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100199/** This macro returns the maximum supported length of the PSK for the
200 * TLS-1.2 PSK-to-MS key derivation
Gilles Peskine364d12c2021-03-08 17:23:47 +0100201 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100202 *
203 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100204 *
205 * Quoting RFC 4279, Sect 5.3:
206 * TLS implementations supporting these ciphersuites MUST support
207 * arbitrary PSK identities up to 128 octets in length, and arbitrary
208 * PSKs up to 64 octets in length. Supporting longer identities and
209 * keys is RECOMMENDED.
210 *
211 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100212 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100213 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100214#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100215
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100216/** The maximum size of a block cipher. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100217#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200218
Gilles Peskineacd4be32018-07-08 19:56:25 +0200219/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200220 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200221 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200222 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100223 * \warning This macro may evaluate its arguments multiple times or
224 * zero times, so you should not pass arguments that contain
225 * side effects.
226 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200227 * \param key_type The type of the MAC key.
228 * \param key_bits The size of the MAC key in bits.
229 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100230 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200231 *
232 * \return The MAC size for the specified algorithm with
233 * the specified key parameters.
234 * \return 0 if the MAC algorithm is not recognized.
235 * \return Either 0 or the correct size for a MAC algorithm that
236 * the implementation recognizes, but does not support.
237 * \return Unspecified if the key parameters are not consistent
238 * with the algorithm.
239 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100240#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
241 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
242 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
243 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200244 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200245
246/** The maximum size of the output of psa_aead_encrypt(), in bytes.
247 *
248 * If the size of the ciphertext buffer is at least this large, it is
249 * guaranteed that psa_aead_encrypt() will not fail due to an
250 * insufficient buffer size. Depending on the algorithm, the actual size of
251 * the ciphertext may be smaller.
252 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100253 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
254 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100255 * \warning This macro may evaluate its arguments multiple times or
256 * zero times, so you should not pass arguments that contain
257 * side effects.
258 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100259 * \param key_type A symmetric key type that is
260 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200261 * \param alg An AEAD algorithm
262 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100263 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200264 * \param plaintext_length Size of the plaintext in bytes.
265 *
266 * \return The AEAD ciphertext size for the specified
267 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100268 * If the key type or AEAD algorithm is not
269 * recognized, or the parameters are incompatible,
270 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200271 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100272#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200273 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200274 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200275 0)
276
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200277/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
278 * supported key types and AEAD algorithms.
279 *
280 * If the size of the ciphertext buffer is at least this large, it is guaranteed
281 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
282 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100283 * \note This macro returns a compile-time constant if its arguments are
284 * compile-time constants.
285 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100286 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
287 * \p plaintext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200288 *
289 * \param plaintext_length Size of the plaintext in bytes.
290 *
291 * \return A sufficient output buffer size for any of the
292 * supported key types and AEAD algorithms.
293 *
294 */
295#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
296 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
297
298
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200299/** The maximum size of the output of psa_aead_decrypt(), in bytes.
300 *
301 * If the size of the plaintext buffer is at least this large, it is
302 * guaranteed that psa_aead_decrypt() will not fail due to an
303 * insufficient buffer size. Depending on the algorithm, the actual size of
304 * the plaintext may be smaller.
305 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100306 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
307 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100308 * \warning This macro may evaluate its arguments multiple times or
309 * zero times, so you should not pass arguments that contain
310 * side effects.
311 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100312 * \param key_type A symmetric key type that is
313 * compatible with algorithm \p alg.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200314 * \param alg An AEAD algorithm
315 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100316 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200317 * \param ciphertext_length Size of the plaintext in bytes.
318 *
319 * \return The AEAD ciphertext size for the specified
320 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100321 * If the key type or AEAD algorithm is not
322 * recognized, or the parameters are incompatible,
323 * return 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200324 */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100325#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200326 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200327 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200328 0)
329
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200330/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
331 * supported key types and AEAD algorithms.
332 *
333 * If the size of the plaintext buffer is at least this large, it is guaranteed
334 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
335 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100336 * \note This macro returns a compile-time constant if its arguments are
337 * compile-time constants.
338 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100339 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
340 * \p ciphertext_length).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200341 *
342 * \param ciphertext_length Size of the ciphertext in bytes.
343 *
344 * \return A sufficient output buffer size for any of the
345 * supported key types and AEAD algorithms.
346 *
347 */
348#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
349 (ciphertext_length)
350
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100351/** The default nonce size for an AEAD algorithm, in bytes.
352 *
353 * This macro can be used to allocate a buffer of sufficient size to
354 * store the nonce output from #psa_aead_generate_nonce().
355 *
356 * See also #PSA_AEAD_NONCE_MAX_SIZE.
357 *
358 * \note This is not the maximum size of nonce supported as input to
359 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
360 * just the default size that is generated by #psa_aead_generate_nonce().
361 *
362 * \warning This macro may evaluate its arguments multiple times or
363 * zero times, so you should not pass arguments that contain
364 * side effects.
365 *
366 * \param key_type A symmetric key type that is compatible with
367 * algorithm \p alg.
368 *
369 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
370 * #PSA_ALG_IS_AEAD(\p alg) is true).
371 *
372 * \return The default nonce size for the specified key type and algorithm.
373 * If the key type or AEAD algorithm is not recognized,
374 * or the parameters are incompatible, return 0.
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100375 */
376#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100377 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200378 PSA_ALG_AEAD_IS_BASE_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
379 PSA_ALG_AEAD_IS_BASE_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
Bence Szépkúti0153c942021-03-04 10:32:59 +0100380 0 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100381 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúti7e310092021-04-08 12:05:18 +0200382 PSA_ALG_AEAD_IS_BASE_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100383 0)
384
385/** The maximum default nonce size among all supported pairs of key types and
386 * AEAD algorithms, in bytes.
387 *
388 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
389 * may return.
390 *
391 * \note This is not the maximum size of nonce supported as input to
392 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
393 * just the largest size that may be generated by
394 * #psa_aead_generate_nonce().
395 */
Bence Szépkúti0153c942021-03-04 10:32:59 +0100396#define PSA_AEAD_NONCE_MAX_SIZE 13
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100397
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200398/** A sufficient output buffer size for psa_aead_update().
399 *
400 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200401 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200402 * insufficient buffer size. The actual size of the output may be smaller
403 * in any given call.
404 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100405 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
406 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100407 * \warning This macro may evaluate its arguments multiple times or
408 * zero times, so you should not pass arguments that contain
409 * side effects.
410 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100411 * \param key_type A symmetric key type that is
412 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200413 * \param alg An AEAD algorithm
414 * (\c PSA_ALG_XXX value such that
415 * #PSA_ALG_IS_AEAD(\p alg) is true).
416 * \param input_length Size of the input in bytes.
417 *
418 * \return A sufficient output buffer size for the specified
419 * algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100420 * If the key type or AEAD algorithm is not
421 * recognized, or the parameters are incompatible,
422 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200423 */
424/* For all the AEAD modes defined in this specification, it is possible
425 * to emit output without delay. However, hardware may not always be
426 * capable of this. So for modes based on a block cipher, allow the
427 * implementation to delay the output until it has a full block. */
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100428#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200429 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
Bence Szépkúti12116bc2021-03-11 15:59:24 +0100430 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
431 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
432 (input_length) : \
433 0)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200434
435/** A sufficient output buffer size for psa_aead_update(), for any of the
436 * supported key types and AEAD algorithms.
437 *
438 * If the size of the output buffer is at least this large, it is guaranteed
439 * that psa_aead_update() will not fail due to an insufficient buffer size.
440 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100441 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200442 *
443 * \param input_length Size of the input in bytes.
444 */
445#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
446 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200447
448/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200449 *
450 * If the size of the ciphertext buffer is at least this large, it is
451 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200452 * insufficient ciphertext buffer size. The actual size of the output may
453 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200454 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100455 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
456 *
457 * \param key_type A symmetric key type that is
458 compatible with algorithm \p alg.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200459 * \param alg An AEAD algorithm
460 * (\c PSA_ALG_XXX value such that
461 * #PSA_ALG_IS_AEAD(\p alg) is true).
462 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200463 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200464 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100465 * If the key type or AEAD algorithm is not
466 * recognized, or the parameters are incompatible,
467 * return 0.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200468 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200469#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
470 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
471 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
472 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200473 0)
474
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200475/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
476 * supported key types and AEAD algorithms.
477 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100478 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200479 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200480#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200481
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200482/** A sufficient plaintext buffer size for psa_aead_verify().
483 *
484 * If the size of the plaintext buffer is at least this large, it is
485 * guaranteed that psa_aead_verify() will not fail due to an
486 * insufficient plaintext buffer size. The actual size of the output may
487 * be smaller in any given call.
488 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100489 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
490 *
491 * \param key_type A symmetric key type that is
492 * compatible with algorithm \p alg.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200493 * \param alg An AEAD algorithm
494 * (\c PSA_ALG_XXX value such that
495 * #PSA_ALG_IS_AEAD(\p alg) is true).
496 *
497 * \return A sufficient plaintext buffer size for the
498 * specified algorithm.
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100499 * If the key type or AEAD algorithm is not
500 * recognized, or the parameters are incompatible,
501 * return 0.
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200502 */
Bence Szépkútif5a1fe92021-04-21 10:13:08 +0200503#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
504 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
505 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
506 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200507 0)
508
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200509/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
510 * supported key types and AEAD algorithms.
511 *
Bence Szépkútieb1a3012021-03-18 10:33:33 +0100512 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200513 */
514#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
515
Jaeden Amero7f042142019-02-07 10:44:38 +0000516#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
517 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100518 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100519 11 /*PKCS#1v1.5*/)
520
521/**
522 * \brief ECDSA signature size for a given curve bit size
523 *
524 * \param curve_bits Curve size in bits.
525 * \return Signature size in bytes.
526 *
527 * \note This macro returns a compile-time constant if its argument is one.
528 */
529#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
530 (PSA_BITS_TO_BYTES(curve_bits) * 2)
531
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100532/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200533 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200534 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200535 * of the specified type and size, with the specified algorithm.
536 * Note that the actual size of the signature may be smaller
537 * (some algorithms produce a variable-size signature).
538 *
539 * \warning This function may call its arguments multiple times or
540 * zero times, so you should not pass arguments that contain
541 * side effects.
542 *
543 * \param key_type An asymmetric key type (this may indifferently be a
544 * key pair type or a public key type).
545 * \param key_bits The size of the key in bits.
546 * \param alg The signature algorithm.
547 *
548 * \return If the parameters are valid and supported, return
549 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200551 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100552 * If the parameters are a valid combination that is not supported,
553 * return either a sensible size or 0.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200554 * If the parameters are not valid, the
555 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200556 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100557#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200558 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
559 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
560 ((void)alg, 0))
561
Gilles Peskine29755712019-11-08 15:49:40 +0100562#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
563 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
564
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100565/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100566 *
567 * Maximum size of an asymmetric signature.
568 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100569 * This macro expands to a compile-time constant integer. This value
570 * is the maximum size of a signature in bytes.
Gilles Peskine29755712019-11-08 15:49:40 +0100571 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100572#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100573 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
574 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
575 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
576
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200577/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200578 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200579 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200580 * a key of the specified type and size, with the specified algorithm.
581 * Note that the actual size of the ciphertext may be smaller, depending
582 * on the algorithm.
583 *
584 * \warning This function may call its arguments multiple times or
585 * zero times, so you should not pass arguments that contain
586 * side effects.
587 *
588 * \param key_type An asymmetric key type (this may indifferently be a
589 * key pair type or a public key type).
590 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200591 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200592 *
593 * \return If the parameters are valid and supported, return
594 * a buffer size in bytes that guarantees that
595 * psa_asymmetric_encrypt() will not fail with
596 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100597 * If the parameters are a valid combination that is not supported,
598 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200599 * If the parameters are not valid, the
600 * return value is unspecified.
601 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200602#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
603 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
604 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
605 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200606
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200607/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
608 * supported asymmetric encryption.
609 *
610 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
611 */
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100612/* This macro assumes that RSA is the only supported asymmetric encryption. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200613#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100614 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200615
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200616/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200617 *
Gilles Peskine76689602020-05-05 16:01:22 +0200618 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200619 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200620 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200621 * on the algorithm.
622 *
623 * \warning This function may call its arguments multiple times or
624 * zero times, so you should not pass arguments that contain
625 * side effects.
626 *
627 * \param key_type An asymmetric key type (this may indifferently be a
628 * key pair type or a public key type).
629 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200630 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200631 *
632 * \return If the parameters are valid and supported, return
633 * a buffer size in bytes that guarantees that
634 * psa_asymmetric_decrypt() will not fail with
635 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100636 * If the parameters are a valid combination that is not supported,
637 * return either a sensible size or 0.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200638 * If the parameters are not valid, the
639 * return value is unspecified.
640 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200641#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
642 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
643 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
644 0)
645
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200646/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
647 * supported asymmetric decryption.
648 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100649 * This macro assumes that RSA is the only supported asymmetric encryption.
650 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200651 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
652 */
653#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100654 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200655
Gilles Peskine1be949b2018-08-10 19:06:59 +0200656/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
657 * number of bits.
658 *
659 * This definition assumes that bits <= 2^19 - 9 so that the length field
660 * is at most 3 bytes. The length of the encoding is the length of the
661 * bit string padded to a whole number of bytes plus:
662 * - 1 type byte;
663 * - 1 to 3 length bytes;
664 * - 0 to 1 bytes of leading 0 due to the sign bit.
665 */
666#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
667 ((bits) / 8 + 5)
668
669/* Maximum size of the export encoding of an RSA public key.
670 * Assumes that the public exponent is less than 2^32.
671 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200672 * RSAPublicKey ::= SEQUENCE {
673 * modulus INTEGER, -- n
674 * publicExponent INTEGER } -- e
675 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000676 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200677 * - n : INTEGER;
678 * - 7 bytes for the public exponent.
679 */
680#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000681 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200682
683/* Maximum size of the export encoding of an RSA key pair.
684 * Assumes thatthe public exponent is less than 2^32 and that the size
685 * difference between the two primes is at most 1 bit.
686 *
687 * RSAPrivateKey ::= SEQUENCE {
688 * version Version, -- 0
689 * modulus INTEGER, -- N-bit
690 * publicExponent INTEGER, -- 32-bit
691 * privateExponent INTEGER, -- N-bit
692 * prime1 INTEGER, -- N/2-bit
693 * prime2 INTEGER, -- N/2-bit
694 * exponent1 INTEGER, -- N/2-bit
695 * exponent2 INTEGER, -- N/2-bit
696 * coefficient INTEGER, -- N/2-bit
697 * }
698 *
699 * - 4 bytes of SEQUENCE overhead;
700 * - 3 bytes of version;
701 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
702 * overapproximated as 9 half-size INTEGERS;
703 * - 7 bytes for the public exponent.
704 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200705#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200706 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
707
708/* Maximum size of the export encoding of a DSA public key.
709 *
710 * SubjectPublicKeyInfo ::= SEQUENCE {
711 * algorithm AlgorithmIdentifier,
712 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
713 * AlgorithmIdentifier ::= SEQUENCE {
714 * algorithm OBJECT IDENTIFIER,
715 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
716 * DSAPublicKey ::= INTEGER -- public key, Y
717 *
718 * - 3 * 4 bytes of SEQUENCE overhead;
719 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
720 * - 4 bytes of BIT STRING overhead;
721 * - 3 full-size INTEGERs (p, g, y);
722 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
723 */
724#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
725 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
726
727/* Maximum size of the export encoding of a DSA key pair.
728 *
729 * DSAPrivateKey ::= SEQUENCE {
730 * version Version, -- 0
731 * prime INTEGER, -- p
732 * subprime INTEGER, -- q
733 * generator INTEGER, -- g
734 * public INTEGER, -- y
735 * private INTEGER, -- x
736 * }
737 *
738 * - 4 bytes of SEQUENCE overhead;
739 * - 3 bytes of version;
740 * - 3 full-size INTEGERs (p, g, y);
741 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
742 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200743#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200744 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
745
746/* Maximum size of the export encoding of an ECC public key.
747 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000748 * The representation of an ECC public key is:
749 * - The byte 0x04;
750 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
751 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
752 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200753 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000754 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200755 */
756#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000757 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200758
759/* Maximum size of the export encoding of an ECC key pair.
760 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100761 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200762 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200763#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100764 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200765
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100766/** Sufficient output buffer size for psa_export_key() or
767 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200768 *
769 * This macro returns a compile-time constant if its arguments are
770 * compile-time constants.
771 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100772 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200773 * zero times, so you should not pass arguments that contain
774 * side effects.
775 *
776 * The following code illustrates how to allocate enough memory to export
777 * a key by querying the key type and size at runtime.
778 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200779 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200780 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200781 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200782 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200783 * psa_key_type_t key_type = psa_get_key_type(&attributes);
784 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100785 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200786 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200787 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200788 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200789 * size_t buffer_length;
790 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
791 * if (status != PSA_SUCCESS) handle_error(...);
792 * \endcode
793 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200794 * \param key_type A supported key type.
795 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200796 *
797 * \return If the parameters are valid and supported, return
798 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100799 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200800 * #PSA_ERROR_BUFFER_TOO_SMALL.
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100801 * If the parameters are a valid combination that is not supported,
802 * return either a sensible size or 0.
803 * If the parameters are not valid, the return value is unspecified.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200804 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100805#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
806 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
807 (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 +0200808 (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 +0100809 (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 +0200810 (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 +0100811 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
812 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 +0200813 0)
814
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200815/** Sufficient output buffer size for psa_export_public_key().
816 *
817 * This macro returns a compile-time constant if its arguments are
818 * compile-time constants.
819 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100820 * \warning This macro may evaluate its arguments multiple times or
821 * zero times, so you should not pass arguments that contain
822 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200823 *
824 * The following code illustrates how to allocate enough memory to export
825 * a public key by querying the key type and size at runtime.
826 * \code{c}
827 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
828 * psa_status_t status;
829 * status = psa_get_key_attributes(key, &attributes);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100830 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200831 * psa_key_type_t key_type = psa_get_key_type(&attributes);
832 * size_t key_bits = psa_get_key_bits(&attributes);
833 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
834 * psa_reset_key_attributes(&attributes);
835 * uint8_t *buffer = malloc(buffer_size);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100836 * if (buffer == NULL) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200837 * size_t buffer_length;
838 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100839 * if (status != PSA_SUCCESS) handle_error(...);
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200840 * \endcode
841 *
842 * \param key_type A public key or key pair key type.
843 * \param key_bits The size of the key in bits.
844 *
845 * \return If the parameters are valid and supported, return
846 * a buffer size in bytes that guarantees that
847 * psa_export_public_key() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100848 * #PSA_ERROR_BUFFER_TOO_SMALL.
849 * If the parameters are a valid combination that is not
850 * supported, return either a sensible size or 0.
851 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200852 * the return value is unspecified.
853 *
854 * If the parameters are valid and supported,
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100855 * return the same result as
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200856 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
857 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
858 * \p key_bits).
859 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100860#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
861 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
862 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 +0200863 0)
864
865/** Sufficient buffer size for exporting any asymmetric key pair.
866 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100867 * This macro expands to a compile-time constant integer. This value is
868 * a sufficient buffer size when calling psa_export_key() to export any
869 * asymmetric key pair, regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200870 *
871 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
872 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100873#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
874 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
875 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
876 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
877 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200878
879/** Sufficient buffer size for exporting any asymmetric public key.
880 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100881 * This macro expands to a compile-time constant integer. This value is
882 * a sufficient buffer size when calling psa_export_key() or
883 * psa_export_public_key() to export any asymmetric public key,
884 * regardless of the exact key type and key size.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200885 *
886 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
887 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100888#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
889 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
890 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
891 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
892 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200893
894/** Sufficient output buffer size for psa_raw_key_agreement().
895 *
896 * This macro returns a compile-time constant if its arguments are
897 * compile-time constants.
898 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100899 * \warning This macro may evaluate its arguments multiple times or
900 * zero times, so you should not pass arguments that contain
901 * side effects.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200902 *
903 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
904 *
905 * \param key_type A supported key type.
906 * \param key_bits The size of the key in bits.
907 *
908 * \return If the parameters are valid and supported, return
909 * a buffer size in bytes that guarantees that
910 * psa_raw_key_agreement() will not fail with
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100911 * #PSA_ERROR_BUFFER_TOO_SMALL.
912 * If the parameters are a valid combination that
913 * is not supported, return either a sensible size or 0.
914 * If the parameters are not valid,
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200915 * the return value is unspecified.
916 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100917/* FFDH is not yet supported in PSA. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200918#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
919 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100920 PSA_BITS_TO_BYTES(key_bits) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200921 0)
922
923/** Maximum size of the output from psa_raw_key_agreement().
924 *
gabor-mezei-armc6f24802021-02-15 15:56:29 +0100925 * This macro expands to a compile-time constant integer. This value is the
926 * maximum size of the output any raw key agreement algorithm, in bytes.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200927 *
928 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
929 */
930#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100931 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200932
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100933/** The default IV size for a cipher algorithm, in bytes.
934 *
935 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
936 * the default IV length for the algorithm.
937 *
938 * This macro can be used to allocate a buffer of sufficient size to
939 * store the IV output from #psa_cipher_generate_iv() when using
940 * a multi-part cipher operation.
941 *
942 * See also #PSA_CIPHER_IV_MAX_SIZE.
943 *
944 * \warning This macro may evaluate its arguments multiple times or
945 * zero times, so you should not pass arguments that contain
946 * side effects.
947 *
948 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
949 *
950 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
951 *
952 * \return The default IV size for the specified key type and algorithm.
953 * If the algorithm does not use an IV, return 0.
954 * If the key type or cipher algorithm is not recognized,
955 * or the parameters are incompatible, return 0.
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100956 */
957#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100958 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100959 ((alg) == PSA_ALG_CTR || \
960 (alg) == PSA_ALG_CFB || \
961 (alg) == PSA_ALG_OFB || \
962 (alg) == PSA_ALG_XTS || \
963 (alg) == PSA_ALG_CBC_NO_PADDING || \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100964 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100965 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúticbe39532020-12-08 00:01:31 +0100966 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100967 0)
968
969/** The maximum IV size for all supported cipher algorithms, in bytes.
970 *
971 * See also #PSA_CIPHER_IV_LENGTH().
972 */
973#define PSA_CIPHER_IV_MAX_SIZE 16
974
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200975/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
976 *
977 * If the size of the output buffer is at least this large, it is guaranteed
978 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
979 * Depending on the algorithm, the actual size of the output might be smaller.
980 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200981 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
982 *
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100983 * \warning This macro may evaluate its arguments multiple times or
984 * zero times, so you should not pass arguments that contain
985 * side effects.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200986 *
987 * \param key_type A symmetric key type that is compatible with algorithm
988 * alg.
989 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
990 * #PSA_ALG_IS_CIPHER(\p alg) is true).
991 * \param input_length Size of the input in bytes.
992 *
993 * \return A sufficient output size for the specified key type and
994 * algorithm. If the key type or cipher algorithm is not
995 * recognized, or the parameters are incompatible,
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100996 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200997 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +0100998#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
999 (alg == PSA_ALG_CBC_PKCS7 ? \
1000 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
gabor-mezei-arm9c3b5072021-03-10 15:57:44 +01001001 (input_length) + 1) + \
1002 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001003 (PSA_ALG_IS_CIPHER(alg) ? \
1004 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1005 0))
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001006
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001007/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1008 * supported key types and cipher algorithms.
1009 *
1010 * If the size of the output buffer is at least this large, it is guaranteed
1011 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1012 *
1013 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1014 *
1015 * \param input_length Size of the input in bytes.
1016 *
1017 */
1018#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1019 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
gabor-mezei-arm56991012021-03-10 16:43:14 +01001020 (input_length) + 1) + \
1021 PSA_CIPHER_IV_MAX_SIZE)
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001022
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001023/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1024 *
1025 * If the size of the output buffer is at least this large, it is guaranteed
1026 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1027 * Depending on the algorithm, the actual size of the output might be smaller.
1028 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001029 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001030 *
1031 * \param key_type A symmetric key type that is compatible with algorithm
1032 * alg.
1033 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1034 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1035 * \param input_length Size of the input in bytes.
1036 *
1037 * \return A sufficient output size for the specified key type and
1038 * algorithm. If the key type or cipher algorithm is not
1039 * recognized, or the parameters are incompatible,
gabor-mezei-armc6f24802021-02-15 15:56:29 +01001040 * return 0.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001041 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001042#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1043 (PSA_ALG_IS_CIPHER(alg) && \
1044 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1045 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001046 0)
1047
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001048/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1049 * supported key types and cipher algorithms.
1050 *
1051 * If the size of the output buffer is at least this large, it is guaranteed
1052 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1053 *
1054 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1055 *
1056 * \param input_length Size of the input in bytes.
1057 */
1058#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1059 (input_length)
1060
1061/** A sufficient output buffer size for psa_cipher_update().
1062 *
1063 * If the size of the output buffer is at least this large, it is guaranteed
1064 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1065 * The actual size of the output might be smaller in any given call.
1066 *
1067 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1068 *
1069 * \param key_type A symmetric key type that is compatible with algorithm
1070 * alg.
1071 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1072 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1073 * \param input_length Size of the input in bytes.
1074 *
1075 * \return A sufficient output size for the specified key type and
1076 * algorithm. If the key type or cipher algorithm is not
1077 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001078 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001079#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1080 (PSA_ALG_IS_CIPHER(alg) ? \
1081 (((alg) == PSA_ALG_CBC_PKCS7 || \
1082 (alg) == PSA_ALG_CBC_NO_PADDING || \
1083 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1084 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1085 input_length) : \
1086 (input_length)) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001087 0)
1088
1089/** A sufficient output buffer size for psa_cipher_update(), for any of the
1090 * supported key types and cipher algorithms.
1091 *
1092 * If the size of the output buffer is at least this large, it is guaranteed
1093 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1094 *
1095 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1096 *
1097 * \param input_length Size of the input in bytes.
1098 */
1099#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
gabor-mezei-arm286a36e2021-03-05 15:54:21 +01001100 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001101
1102/** A sufficient ciphertext buffer size for psa_cipher_finish().
1103 *
1104 * If the size of the ciphertext buffer is at least this large, it is
1105 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1106 * ciphertext buffer size. The actual size of the output might be smaller in
1107 * any given call.
1108 *
1109 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1110 *
1111 * \param key_type A symmetric key type that is compatible with algorithm
1112 * alg.
1113 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1114 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1115 * \return A sufficient output size for the specified key type and
1116 * algorithm. If the key type or cipher algorithm is not
1117 * recognized, or the parameters are incompatible, return 0.
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001118 */
gabor-mezei-arme86bdca2021-01-07 14:26:12 +01001119#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1120 (PSA_ALG_IS_CIPHER(alg) ? \
1121 (alg == PSA_ALG_CBC_PKCS7 ? \
1122 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1123 0) : \
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001124 0)
1125
1126/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1127 * supported key types and cipher algorithms.
1128 *
1129 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1130 */
1131#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1132 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1133
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001134#endif /* PSA_CRYPTO_SIZES_H */