blob: 8473d7eae01b6a6e89baddb45b5cd4c85c068f25 [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.
68 * An implementation may return either 0 or the correct size
69 * for a hash algorithm that it recognizes, but does not support.
70 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +010071#define PSA_HASH_LENGTH(alg) \
72 ( \
Gilles Peskinea7c26db2018-12-12 13:42:25 +010073 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
87 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
88 0)
89
Gilles Peskineaf3baab2018-06-27 22:55:52 +020090/** \def PSA_HASH_MAX_SIZE
91 *
92 * Maximum size of a hash.
93 *
94 * This macro must expand to a compile-time constant integer. This value
95 * should be the maximum size of a hash supported by the implementation,
96 * in bytes, and must be no smaller than this maximum.
97 */
Gilles Peskine3052f532018-09-17 14:13:26 +020098/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
99 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
100 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200101#if defined(MBEDTLS_SHA512_C)
102#define PSA_HASH_MAX_SIZE 64
103#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
104#else
105#define PSA_HASH_MAX_SIZE 32
106#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
107#endif
108
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200109/** \def PSA_MAC_MAX_SIZE
110 *
111 * Maximum size of a MAC.
112 *
113 * This macro must expand to a compile-time constant integer. This value
114 * should be the maximum size of a MAC supported by the implementation,
115 * in bytes, and must be no smaller than this maximum.
116 */
117/* All non-HMAC MACs have a maximum size that's smaller than the
118 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200119/* Note that the encoding of truncated MAC algorithms limits this value
120 * to 64 bytes.
121 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200122#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
123
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100124/** The tag size for an AEAD algorithm, in bytes.
125 *
126 * \param alg An AEAD algorithm
127 * (\c PSA_ALG_XXX value such that
128 * #PSA_ALG_IS_AEAD(\p alg) is true).
129 *
130 * \return The tag size for the specified algorithm.
131 * If the AEAD algorithm does not have an identified
132 * tag that can be distinguished from the rest of
133 * the ciphertext, return 0.
134 * If the AEAD algorithm is not recognized, return 0.
135 * An implementation may return either 0 or a
136 * correct size for an AEAD algorithm that it
137 * recognizes, but does not support.
138 */
139#define PSA_AEAD_TAG_LENGTH(alg) \
140 (PSA_ALG_IS_AEAD(alg) ? \
141 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
142 0)
143
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200144/** The maximum tag size for all supported AEAD algorithms, in bytes.
145 *
146 * See also #PSA_AEAD_TAG_LENGTH(\p alg).
147 */
148#define PSA_AEAD_TAG_MAX_SIZE \
149 (PSA_ALG_AEAD_TAG_LENGTH_MASK >> PSA_AEAD_TAG_LENGTH_OFFSET)
150
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200151/* The maximum size of an RSA key on this implementation, in bits.
152 * This is a vendor-specific macro.
153 *
154 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
155 * whose parameters fit in a bignum is accepted. However large keys can
156 * induce a large memory usage and long computation times. Unlike other
157 * auxiliary macros in this file and in crypto.h, which reflect how the
158 * library is configured, this macro defines how the library is
159 * configured. This implementation refuses to import or generate an
160 * RSA key whose size is larger than the value defined here.
161 *
162 * Note that an implementation may set different size limits for different
163 * operations, and does not need to accept all key sizes up to the limit. */
164#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
165
166/* The maximum size of an ECC key on this implementation, in bits.
167 * This is a vendor-specific macro. */
168#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
169#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
170#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
171#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
172#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
173#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
174#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
175#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
176#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
177#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
178#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
179#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
180#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
181#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
182#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
183#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
184#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
185#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
186#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
187#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
188#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
189#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
190#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
191#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
192#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
193#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
194#else
195#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
196#endif
197
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100198/** This macro returns the maximum supported length of the PSK for the
199 * TLS-1.2 PSK-to-MS key derivation
200 * (#PSA_ALG_TLS12_PSK_TO_MS(\p hash_alg)).
201 *
202 * The maximum supported length does not depend on the chosen hash algorithm.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100203 *
204 * Quoting RFC 4279, Sect 5.3:
205 * TLS implementations supporting these ciphersuites MUST support
206 * arbitrary PSK identities up to 128 octets in length, and arbitrary
207 * PSKs up to 64 octets in length. Supporting longer identities and
208 * keys is RECOMMENDED.
209 *
210 * Therefore, no implementation should define a value smaller than 64
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100211 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Hanno Becker8dbfca42018-10-12 11:56:55 +0100212 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100213#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Hanno Becker8dbfca42018-10-12 11:56:55 +0100214
Gilles Peskined911eb72018-08-14 15:18:45 +0200215/** The maximum size of a block cipher supported by the implementation. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100216#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200217
Gilles Peskineacd4be32018-07-08 19:56:25 +0200218/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200219 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200220 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200221 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100222 * \warning This macro may evaluate its arguments multiple times or
223 * zero times, so you should not pass arguments that contain
224 * side effects.
225 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200226 * \param key_type The type of the MAC key.
227 * \param key_bits The size of the MAC key in bits.
228 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100229 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200230 *
231 * \return The MAC size for the specified algorithm with
232 * the specified key parameters.
233 * \return 0 if the MAC algorithm is not recognized.
234 * \return Either 0 or the correct size for a MAC algorithm that
235 * the implementation recognizes, but does not support.
236 * \return Unspecified if the key parameters are not consistent
237 * with the algorithm.
238 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100239#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
240 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
241 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
242 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200243 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200244
245/** The maximum size of the output of psa_aead_encrypt(), in bytes.
246 *
247 * If the size of the ciphertext buffer is at least this large, it is
248 * guaranteed that psa_aead_encrypt() will not fail due to an
249 * insufficient buffer size. Depending on the algorithm, the actual size of
250 * the ciphertext may be smaller.
251 *
252 * \param alg An AEAD algorithm
253 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100254 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200255 * \param plaintext_length Size of the plaintext in bytes.
256 *
257 * \return The AEAD ciphertext size for the specified
258 * algorithm.
259 * If the AEAD algorithm is not recognized, return 0.
260 * An implementation may return either 0 or a
261 * correct size for an AEAD algorithm that it
262 * recognizes, but does not support.
263 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200264#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
265 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
266 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200267 0)
268
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200269/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
270 * supported key types and AEAD algorithms.
271 *
272 * If the size of the ciphertext buffer is at least this large, it is guaranteed
273 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
274 *
275 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, \p plaintext_length).
276 *
277 * \param plaintext_length Size of the plaintext in bytes.
278 *
279 * \return A sufficient output buffer size for any of the
280 * supported key types and AEAD algorithms.
281 *
282 */
283#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
284 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
285
286
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200287/** The maximum size of the output of psa_aead_decrypt(), in bytes.
288 *
289 * If the size of the plaintext buffer is at least this large, it is
290 * guaranteed that psa_aead_decrypt() will not fail due to an
291 * insufficient buffer size. Depending on the algorithm, the actual size of
292 * the plaintext may be smaller.
293 *
294 * \param alg An AEAD algorithm
295 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100296 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200297 * \param ciphertext_length Size of the plaintext in bytes.
298 *
299 * \return The AEAD ciphertext size for the specified
300 * algorithm.
301 * If the AEAD algorithm is not recognized, return 0.
302 * An implementation may return either 0 or a
303 * correct size for an AEAD algorithm that it
304 * recognizes, but does not support.
305 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200306#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
307 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
Gilles Peskine36d477d2019-05-14 16:09:22 +0200308 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200309 0)
310
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200311/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
312 * supported key types and AEAD algorithms.
313 *
314 * If the size of the plaintext buffer is at least this large, it is guaranteed
315 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
316 *
317 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, \p ciphertext_length).
318 *
319 * \param ciphertext_length Size of the ciphertext in bytes.
320 *
321 * \return A sufficient output buffer size for any of the
322 * supported key types and AEAD algorithms.
323 *
324 */
325#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
326 (ciphertext_length)
327
gabor-mezei-arma200ee62020-12-17 14:09:38 +0100328/** The default nonce size for an AEAD algorithm, in bytes.
329 *
330 * This macro can be used to allocate a buffer of sufficient size to
331 * store the nonce output from #psa_aead_generate_nonce().
332 *
333 * See also #PSA_AEAD_NONCE_MAX_SIZE.
334 *
335 * \note This is not the maximum size of nonce supported as input to
336 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
337 * just the default size that is generated by #psa_aead_generate_nonce().
338 *
339 * \warning This macro may evaluate its arguments multiple times or
340 * zero times, so you should not pass arguments that contain
341 * side effects.
342 *
343 * \param key_type A symmetric key type that is compatible with
344 * algorithm \p alg.
345 *
346 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
347 * #PSA_ALG_IS_AEAD(\p alg) is true).
348 *
349 * \return The default nonce size for the specified key type and algorithm.
350 * If the key type or AEAD algorithm is not recognized,
351 * or the parameters are incompatible, return 0.
352 * An implementation can return either 0 or a correct size for a key
353 * type and AEAD algorithm that it recognizes, but does not support.
354 */
355#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
356 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 && \
357 (PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_CCM || \
358 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_GCM) ? 12 : \
359 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
360 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \
361 0)
362
363/** The maximum default nonce size among all supported pairs of key types and
364 * AEAD algorithms, in bytes.
365 *
366 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
367 * may return.
368 *
369 * \note This is not the maximum size of nonce supported as input to
370 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
371 * just the largest size that may be generated by
372 * #psa_aead_generate_nonce().
373 */
374#define PSA_AEAD_NONCE_MAX_SIZE 12
375
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200376/** A sufficient output buffer size for psa_aead_update().
377 *
378 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200379 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200380 * insufficient buffer size. The actual size of the output may be smaller
381 * in any given call.
382 *
383 * \param alg An AEAD algorithm
384 * (\c PSA_ALG_XXX value such that
385 * #PSA_ALG_IS_AEAD(\p alg) is true).
386 * \param input_length Size of the input in bytes.
387 *
388 * \return A sufficient output buffer size for the specified
389 * algorithm.
390 * If the AEAD algorithm is not recognized, return 0.
391 * An implementation may return either 0 or a
392 * correct size for an AEAD algorithm that it
393 * recognizes, but does not support.
394 */
395/* For all the AEAD modes defined in this specification, it is possible
396 * to emit output without delay. However, hardware may not always be
397 * capable of this. So for modes based on a block cipher, allow the
398 * implementation to delay the output until it has a full block. */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200399#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
400 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
401 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
402 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)) : \
403 (input_length)) : \
404 0)
405
406/** A sufficient output buffer size for psa_aead_update(), for any of the
407 * supported key types and AEAD algorithms.
408 *
409 * If the size of the output buffer is at least this large, it is guaranteed
410 * that psa_aead_update() will not fail due to an insufficient buffer size.
411 *
412 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p alg, \p input_length).
413 *
414 * \param input_length Size of the input in bytes.
415 */
416#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
417 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200418
419/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200420 *
421 * If the size of the ciphertext buffer is at least this large, it is
422 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200423 * insufficient ciphertext buffer size. The actual size of the output may
424 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200425 *
426 * \param alg An AEAD algorithm
427 * (\c PSA_ALG_XXX value such that
428 * #PSA_ALG_IS_AEAD(\p alg) is true).
429 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200430 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200431 * specified algorithm.
432 * If the AEAD algorithm is not recognized, return 0.
433 * An implementation may return either 0 or a
434 * correct size for an AEAD algorithm that it
435 * recognizes, but does not support.
436 */
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200437#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
438 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100439 PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200440 0)
441
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200442/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
443 * supported key types and AEAD algorithms.
444 *
445 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p alg).
446 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200447#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
gabor-mezei-arm0687b2b2020-05-06 16:05:37 +0200448
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200449/** A sufficient plaintext buffer size for psa_aead_verify().
450 *
451 * If the size of the plaintext buffer is at least this large, it is
452 * guaranteed that psa_aead_verify() will not fail due to an
453 * insufficient plaintext buffer size. The actual size of the output may
454 * be smaller in any given call.
455 *
456 * \param alg An AEAD algorithm
457 * (\c PSA_ALG_XXX value such that
458 * #PSA_ALG_IS_AEAD(\p alg) is true).
459 *
460 * \return A sufficient plaintext buffer size for the
461 * specified algorithm.
462 * If the AEAD algorithm is not recognized, return 0.
463 * An implementation may return either 0 or a
464 * correct size for an AEAD algorithm that it
465 * recognizes, but does not support.
466 */
467#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
468 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100469 PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200470 0)
471
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200472/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
473 * supported key types and AEAD algorithms.
474 *
475 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p alg).
476 */
477#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
478
Jaeden Amero7f042142019-02-07 10:44:38 +0000479#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
480 (PSA_ALG_IS_RSA_OAEP(alg) ? \
gabor-mezei-armd25ea722021-01-21 12:20:08 +0100481 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100482 11 /*PKCS#1v1.5*/)
483
484/**
485 * \brief ECDSA signature size for a given curve bit size
486 *
487 * \param curve_bits Curve size in bits.
488 * \return Signature size in bytes.
489 *
490 * \note This macro returns a compile-time constant if its argument is one.
491 */
492#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
493 (PSA_BITS_TO_BYTES(curve_bits) * 2)
494
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100495/** Sufficient signature buffer size for psa_sign_hash().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200496 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200497 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200498 * of the specified type and size, with the specified algorithm.
499 * Note that the actual size of the signature may be smaller
500 * (some algorithms produce a variable-size signature).
501 *
502 * \warning This function may call its arguments multiple times or
503 * zero times, so you should not pass arguments that contain
504 * side effects.
505 *
506 * \param key_type An asymmetric key type (this may indifferently be a
507 * key pair type or a public key type).
508 * \param key_bits The size of the key in bits.
509 * \param alg The signature algorithm.
510 *
511 * \return If the parameters are valid and supported, return
512 * a buffer size in bytes that guarantees that
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100513 * psa_sign_hash() will not fail with
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200514 * #PSA_ERROR_BUFFER_TOO_SMALL.
515 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200516 * by the implementation, this macro shall return either a
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200517 * sensible size or 0.
518 * If the parameters are not valid, the
519 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200520 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100521#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200522 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
523 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
524 ((void)alg, 0))
525
Gilles Peskine29755712019-11-08 15:49:40 +0100526#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
527 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
528
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100529/** \def PSA_SIGNATURE_MAX_SIZE
Gilles Peskine29755712019-11-08 15:49:40 +0100530 *
531 * Maximum size of an asymmetric signature.
532 *
533 * This macro must expand to a compile-time constant integer. This value
534 * should be the maximum size of a signature supported by the implementation,
535 * in bytes, and must be no smaller than this maximum.
536 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100537#define PSA_SIGNATURE_MAX_SIZE \
Gilles Peskine29755712019-11-08 15:49:40 +0100538 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
539 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
540 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
541
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200542/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200543 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200544 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200545 * a key of the specified type and size, with the specified algorithm.
546 * Note that the actual size of the ciphertext may be smaller, depending
547 * on the algorithm.
548 *
549 * \warning This function may call its arguments multiple times or
550 * zero times, so you should not pass arguments that contain
551 * side effects.
552 *
553 * \param key_type An asymmetric key type (this may indifferently be a
554 * key pair type or a public key type).
555 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200556 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200557 *
558 * \return If the parameters are valid and supported, return
559 * a buffer size in bytes that guarantees that
560 * psa_asymmetric_encrypt() will not fail with
561 * #PSA_ERROR_BUFFER_TOO_SMALL.
562 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200563 * by the implementation, this macro shall return either a
Gilles Peskinedcd14942018-07-12 00:30:52 +0200564 * sensible size or 0.
565 * If the parameters are not valid, the
566 * return value is unspecified.
567 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200568#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
569 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
570 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
571 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200572
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200573/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
574 * supported asymmetric encryption.
575 *
576 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
577 */
578#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
579 (PSA_BITS_TO_BYTES(PSA_MAX_KEY_BITS))
580
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200581/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200582 *
Gilles Peskine76689602020-05-05 16:01:22 +0200583 * This macro returns a sufficient buffer size for a plaintext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200584 * a key of the specified type and size, with the specified algorithm.
Gilles Peskine76689602020-05-05 16:01:22 +0200585 * Note that the actual size of the plaintext may be smaller, depending
Gilles Peskinedcd14942018-07-12 00:30:52 +0200586 * on the algorithm.
587 *
588 * \warning This function may call its arguments multiple times or
589 * zero times, so you should not pass arguments that contain
590 * side effects.
591 *
592 * \param key_type An asymmetric key type (this may indifferently be a
593 * key pair type or a public key type).
594 * \param key_bits The size of the key in bits.
Gilles Peskine9ff8d1f2020-05-05 16:00:17 +0200595 * \param alg The asymmetric encryption algorithm.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200596 *
597 * \return If the parameters are valid and supported, return
598 * a buffer size in bytes that guarantees that
599 * psa_asymmetric_decrypt() will not fail with
600 * #PSA_ERROR_BUFFER_TOO_SMALL.
601 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200602 * by the implementation, this macro shall return either a
Gilles Peskinedcd14942018-07-12 00:30:52 +0200603 * sensible size or 0.
604 * If the parameters are not valid, the
605 * return value is unspecified.
606 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200607#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
608 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
609 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
610 0)
611
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200612/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
613 * supported asymmetric decryption.
614 *
615 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
616 */
617#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
618 (PSA_BITS_TO_BYTES(PSA_MAX_KEY_BITS))
619
Gilles Peskine1be949b2018-08-10 19:06:59 +0200620/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
621 * number of bits.
622 *
623 * This definition assumes that bits <= 2^19 - 9 so that the length field
624 * is at most 3 bytes. The length of the encoding is the length of the
625 * bit string padded to a whole number of bytes plus:
626 * - 1 type byte;
627 * - 1 to 3 length bytes;
628 * - 0 to 1 bytes of leading 0 due to the sign bit.
629 */
630#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
631 ((bits) / 8 + 5)
632
633/* Maximum size of the export encoding of an RSA public key.
634 * Assumes that the public exponent is less than 2^32.
635 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200636 * RSAPublicKey ::= SEQUENCE {
637 * modulus INTEGER, -- n
638 * publicExponent INTEGER } -- e
639 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000640 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200641 * - n : INTEGER;
642 * - 7 bytes for the public exponent.
643 */
644#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000645 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200646
647/* Maximum size of the export encoding of an RSA key pair.
648 * Assumes thatthe public exponent is less than 2^32 and that the size
649 * difference between the two primes is at most 1 bit.
650 *
651 * RSAPrivateKey ::= SEQUENCE {
652 * version Version, -- 0
653 * modulus INTEGER, -- N-bit
654 * publicExponent INTEGER, -- 32-bit
655 * privateExponent INTEGER, -- N-bit
656 * prime1 INTEGER, -- N/2-bit
657 * prime2 INTEGER, -- N/2-bit
658 * exponent1 INTEGER, -- N/2-bit
659 * exponent2 INTEGER, -- N/2-bit
660 * coefficient INTEGER, -- N/2-bit
661 * }
662 *
663 * - 4 bytes of SEQUENCE overhead;
664 * - 3 bytes of version;
665 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
666 * overapproximated as 9 half-size INTEGERS;
667 * - 7 bytes for the public exponent.
668 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200669#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200670 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
671
672/* Maximum size of the export encoding of a DSA public key.
673 *
674 * SubjectPublicKeyInfo ::= SEQUENCE {
675 * algorithm AlgorithmIdentifier,
676 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
677 * AlgorithmIdentifier ::= SEQUENCE {
678 * algorithm OBJECT IDENTIFIER,
679 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
680 * DSAPublicKey ::= INTEGER -- public key, Y
681 *
682 * - 3 * 4 bytes of SEQUENCE overhead;
683 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
684 * - 4 bytes of BIT STRING overhead;
685 * - 3 full-size INTEGERs (p, g, y);
686 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
687 */
688#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
689 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
690
691/* Maximum size of the export encoding of a DSA key pair.
692 *
693 * DSAPrivateKey ::= SEQUENCE {
694 * version Version, -- 0
695 * prime INTEGER, -- p
696 * subprime INTEGER, -- q
697 * generator INTEGER, -- g
698 * public INTEGER, -- y
699 * private INTEGER, -- x
700 * }
701 *
702 * - 4 bytes of SEQUENCE overhead;
703 * - 3 bytes of version;
704 * - 3 full-size INTEGERs (p, g, y);
705 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
706 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200707#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200708 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
709
710/* Maximum size of the export encoding of an ECC public key.
711 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000712 * The representation of an ECC public key is:
713 * - The byte 0x04;
714 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
715 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
716 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200717 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000718 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200719 */
720#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000721 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200722
723/* Maximum size of the export encoding of an ECC key pair.
724 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100725 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200726 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200727#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100728 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200729
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100730/** Sufficient output buffer size for psa_export_key() or
731 * psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200732 *
733 * This macro returns a compile-time constant if its arguments are
734 * compile-time constants.
735 *
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100736 * \warning This macro may evaluate its arguments multiple times or
Gilles Peskine1be949b2018-08-10 19:06:59 +0200737 * zero times, so you should not pass arguments that contain
738 * side effects.
739 *
740 * The following code illustrates how to allocate enough memory to export
741 * a key by querying the key type and size at runtime.
742 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200743 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200744 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200745 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200746 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200747 * psa_key_type_t key_type = psa_get_key_type(&attributes);
748 * size_t key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100749 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200750 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200751 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200752 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200753 * size_t buffer_length;
754 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
755 * if (status != PSA_SUCCESS) handle_error(...);
756 * \endcode
757 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200758 * \param key_type A supported key type.
759 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200760 *
761 * \return If the parameters are valid and supported, return
762 * a buffer size in bytes that guarantees that
gabor-mezei-armbdae9182021-01-28 14:33:10 +0100763 * psa_export_key() or psa_export_public_key() will not fail with
Gilles Peskine1be949b2018-08-10 19:06:59 +0200764 * #PSA_ERROR_BUFFER_TOO_SMALL.
765 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200766 * by the implementation, this macro shall return either a
Gilles Peskine1be949b2018-08-10 19:06:59 +0200767 * sensible size or 0.
768 * If the parameters are not valid, the
769 * return value is unspecified.
770 */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100771#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
772 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
773 (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 +0200774 (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 +0100775 (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 +0200776 (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 +0100777 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
778 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 +0200779 0)
780
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200781/** Sufficient output buffer size for psa_export_public_key().
782 *
783 * This macro returns a compile-time constant if its arguments are
784 * compile-time constants.
785 *
786 * \warning This function can evaluate its arguments multiple times or
787 * zero times. Providing arguments that have side effects will
788 * result in implementation-specific behavior, and is non-portable.
789 *
790 * The following code illustrates how to allocate enough memory to export
791 * a public key by querying the key type and size at runtime.
792 * \code{c}
793 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
794 * psa_status_t status;
795 * status = psa_get_key_attributes(key, &attributes);
796 * if (status != PSA_SUCCESS)
797 * handle_error(...);
798 * psa_key_type_t key_type = psa_get_key_type(&attributes);
799 * size_t key_bits = psa_get_key_bits(&attributes);
800 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
801 * psa_reset_key_attributes(&attributes);
802 * uint8_t *buffer = malloc(buffer_size);
803 * if (buffer == NULL)
804 * handle_error(...);
805 * size_t buffer_length;
806 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
807 * if (status != PSA_SUCCESS)
808 * handle_error(...);
809 * \endcode
810 *
811 * \param key_type A public key or key pair key type.
812 * \param key_bits The size of the key in bits.
813 *
814 * \return If the parameters are valid and supported, return
815 * a buffer size in bytes that guarantees that
816 * psa_export_public_key() will not fail with
817 * #PSA_ERROR_BUFFER_TOO_SMALL. If the parameters are
818 * a valid combination that is not supported by the
819 * implementation, this macro must return either
820 * a sensible size or 0. If the parameters are not valid,
821 * the return value is unspecified.
822 *
823 * If the parameters are valid and supported,
824 * it is recommended that this macro returns the same
825 * result as
826 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
827 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
828 * \p key_bits).
829 */
830#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
831 ((key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
832 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
833 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
834 0)
835
836/** Sufficient buffer size for exporting any asymmetric key pair.
837 *
838 * This macro must expand to a compile-time constant integer. This value must
839 * be a sufficient buffer size when calling psa_export_key() to export any
840 * asymmetric key pair that is supported by the implementation, regardless of
841 * the exact key type and key size.
842 *
843 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
844 */
845#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
846 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) > \
847 PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) ? \
848 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) > \
849 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) ? \
850 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) : \
851 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS)) : \
852 (PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) > \
853 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) ? \
854 PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS) : \
855 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_MAX_KEY_BITS)))
856
857/** Sufficient buffer size for exporting any asymmetric public key.
858 *
859 * This macro must expand to a compile-time constant integer. This value must
860 * be a sufficient buffer size when calling psa_export_key() or
861 * psa_export_public_key() to export any asymmetric public key that is
862 * supported by the implementation, regardless of the exact key type and key
863 * size.
864 *
865 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
866 */
867#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
868 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) > \
869 PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) ? \
870 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) > \
871 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) ? \
872 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) : \
873 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS)) : \
874 (PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) > \
875 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) ? \
876 PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS) : \
877 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_MAX_KEY_BITS)))
878
879/** Sufficient output buffer size for psa_raw_key_agreement().
880 *
881 * This macro returns a compile-time constant if its arguments are
882 * compile-time constants.
883 *
884 * \warning This function can evaluate its arguments multiple times or
885 * zero times. Providing arguments that have side effects will
886 * result in implementation-specific behavior, and is non-portable.
887 *
888 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
889 *
890 * \param key_type A supported key type.
891 * \param key_bits The size of the key in bits.
892 *
893 * \return If the parameters are valid and supported, return
894 * a buffer size in bytes that guarantees that
895 * psa_raw_key_agreement() will not fail with
896 * #PSA_ERROR_BUFFER_TOO_SMALL. If the parameters are
897 * a valid combination that is not supported by
898 * the implementation, this macro must return either
899 * a sensible size or 0. If the parameters are not valid,
900 * the return value is unspecified.
901 */
902#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
903 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
904 2 * PSA_BITS_TO_BYTES(key_bits) : \
905 0)
906
907/** Maximum size of the output from psa_raw_key_agreement().
908 *
909 * This macro must expand to a compile-time constant integer. It is recommended
910 * that this value is the maximum size of the output any raw key agreement
911 * algorithm supported by the implementation, in bytes. The value must not be
912 * smaller than this maximum.
913 *
914 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
915 */
916#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
917 (2 * PSA_BITS_TO_BYTES(key_bits))
918
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100919/** The default IV size for a cipher algorithm, in bytes.
920 *
921 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
922 * the default IV length for the algorithm.
923 *
924 * This macro can be used to allocate a buffer of sufficient size to
925 * store the IV output from #psa_cipher_generate_iv() when using
926 * a multi-part cipher operation.
927 *
928 * See also #PSA_CIPHER_IV_MAX_SIZE.
929 *
930 * \warning This macro may evaluate its arguments multiple times or
931 * zero times, so you should not pass arguments that contain
932 * side effects.
933 *
934 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
935 *
936 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
937 *
938 * \return The default IV size for the specified key type and algorithm.
939 * If the algorithm does not use an IV, return 0.
940 * If the key type or cipher algorithm is not recognized,
941 * or the parameters are incompatible, return 0.
942 * An implementation can return either 0 or a correct size for a key type
943 * and cipher algorithm that it recognizes, but does not support.
944 */
945#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100946 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100947 ((alg) == PSA_ALG_CTR || \
948 (alg) == PSA_ALG_CFB || \
949 (alg) == PSA_ALG_OFB || \
950 (alg) == PSA_ALG_XTS || \
951 (alg) == PSA_ALG_CBC_NO_PADDING || \
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100952 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100953 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Bence Szépkúticbe39532020-12-08 00:01:31 +0100954 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Bence Szépkúti423d3e72020-10-29 11:07:39 +0100955 0)
956
957/** The maximum IV size for all supported cipher algorithms, in bytes.
958 *
959 * See also #PSA_CIPHER_IV_LENGTH().
960 */
961#define PSA_CIPHER_IV_MAX_SIZE 16
962
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200963/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
964 *
965 * If the size of the output buffer is at least this large, it is guaranteed
966 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
967 * Depending on the algorithm, the actual size of the output might be smaller.
968 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200969 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
970 *
971 * \warning This function can evaluate its arguments multiple times or
972 * zero times. Providing arguments that have side effects will
973 * result in implementation-specific behavior, and is non-portable.
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200974 *
975 * \param key_type A symmetric key type that is compatible with algorithm
976 * alg.
977 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
978 * #PSA_ALG_IS_CIPHER(\p alg) is true).
979 * \param input_length Size of the input in bytes.
980 *
981 * \return A sufficient output size for the specified key type and
982 * algorithm. If the key type or cipher algorithm is not
983 * recognized, or the parameters are incompatible,
984 * return 0. An implementation can return either 0 or
985 * a correct size for a key type and cipher algorithm
986 * that it recognizes, but does not support.
987 */
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200988#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
989 (PSA_ALG_IS_CIPHER(alg) && \
990 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
991 (alg == PSA_ALG_CBC_PKCS7 ? \
992 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
993 (input_length) + PSA_CIPHER_IV_LENGTH(key_type, alg)) : \
994 (input_length) + PSA_CIPHER_IV_LENGTH(key_type, alg) ) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +0200995 0)
996
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +0200997/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
998 * supported key types and cipher algorithms.
999 *
1000 * If the size of the output buffer is at least this large, it is guaranteed
1001 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1002 *
1003 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1004 *
1005 * \param input_length Size of the input in bytes.
1006 *
1007 */
1008#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1009 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1010 (input_length) + PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE))
1011
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001012/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1013 *
1014 * If the size of the output buffer is at least this large, it is guaranteed
1015 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1016 * Depending on the algorithm, the actual size of the output might be smaller.
1017 *
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001018 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001019 *
1020 * \param key_type A symmetric key type that is compatible with algorithm
1021 * alg.
1022 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1023 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1024 * \param input_length Size of the input in bytes.
1025 *
1026 * \return A sufficient output size for the specified key type and
1027 * algorithm. If the key type or cipher algorithm is not
1028 * recognized, or the parameters are incompatible,
1029 * return 0. An implementation can return either 0 or
1030 * a correct size for a key type and cipher algorithm
1031 * that it recognizes, but does not support.
1032 */
gabor-mezei-armee6bb562020-06-17 10:11:11 +02001033#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1034 (PSA_ALG_IS_CIPHER(alg) && \
1035 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1036 (input_length) : \
gabor-mezei-arm8809fb62020-06-02 14:27:06 +02001037 0)
1038
gabor-mezei-armfbd9f1e2020-06-29 10:38:39 +02001039/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1040 * supported key types and cipher algorithms.
1041 *
1042 * If the size of the output buffer is at least this large, it is guaranteed
1043 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1044 *
1045 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1046 *
1047 * \param input_length Size of the input in bytes.
1048 */
1049#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1050 (input_length)
1051
1052/** A sufficient output buffer size for psa_cipher_update().
1053 *
1054 * If the size of the output buffer is at least this large, it is guaranteed
1055 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1056 * The actual size of the output might be smaller in any given call.
1057 *
1058 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1059 *
1060 * \param key_type A symmetric key type that is compatible with algorithm
1061 * alg.
1062 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1063 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1064 * \param input_length Size of the input in bytes.
1065 *
1066 * \return A sufficient output size for the specified key type and
1067 * algorithm. If the key type or cipher algorithm is not
1068 * recognized, or the parameters are incompatible, return 0.
1069 * An implementation can return either 0 or a correct size
1070 * for a key type and cipher algorithm that it recognizes,
1071 * but does not support.
1072 */
1073#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1074 (PSA_ALG_IS_CIPHER(alg) && \
1075 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1076 (input_length) + PSA_CIPHER_IV_LENGTH(key_type, alg) : \
1077 0)
1078
1079/** A sufficient output buffer size for psa_cipher_update(), for any of the
1080 * supported key types and cipher algorithms.
1081 *
1082 * If the size of the output buffer is at least this large, it is guaranteed
1083 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1084 *
1085 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1086 *
1087 * \param input_length Size of the input in bytes.
1088 */
1089#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1090 ((input_length) + PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1091
1092/** A sufficient ciphertext buffer size for psa_cipher_finish().
1093 *
1094 * If the size of the ciphertext buffer is at least this large, it is
1095 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1096 * ciphertext buffer size. The actual size of the output might be smaller in
1097 * any given call.
1098 *
1099 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1100 *
1101 * \param key_type A symmetric key type that is compatible with algorithm
1102 * alg.
1103 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1104 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1105 * \return A sufficient output size for the specified key type and
1106 * algorithm. If the key type or cipher algorithm is not
1107 * recognized, or the parameters are incompatible, return 0.
1108 * An implementation can return either 0 or a correct size
1109 * for a key type and cipher algorithm that it recognizes,
1110 * but does not support.
1111 */
1112#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1113 (PSA_ALG_IS_CIPHER(alg) && \
1114 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1115 (alg == PSA_ALG_CBC_PKCS7 ? \
1116 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1117 0) : \
1118 0)
1119
1120/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1121 * supported key types and cipher algorithms.
1122 *
1123 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1124 */
1125#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1126 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1127
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001128#endif /* PSA_CRYPTO_SIZES_H */