blob: eb03c98d57971ccda4b2f86d64f4c23dcc434daf [file] [log] [blame]
Antonio de Angelis377a1552018-11-22 17:02:40 +00001/*
Summer Qinf07cc312022-01-05 16:52:54 +08002 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto_sizes.h
Antonio de Angelis377a1552018-11-22 17:02:40 +00009 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +010010 * \brief PSA cryptography module: Mbed TLS buffer size macros
Antonio de Angelis377a1552018-11-22 17:02:40 +000011 *
12 * \note This file may not be included directly. Applications must
Jamie Foxcc31d402019-01-28 17:13:52 +000013 * include psa/crypto.h.
Antonio de Angelis377a1552018-11-22 17:02:40 +000014 *
15 * This file contains the definitions of macros that are useful to
16 * compute buffer sizes. The signatures and semantics of these macros
17 * are standardized, but the definitions are not, because they depend on
18 * the available algorithms and, in some cases, on permitted tolerances
19 * on buffer sizes.
20 *
21 * In implementations with isolation between the application and the
22 * cryptography module, implementers should take care to ensure that
23 * the definitions that are exposed to applications match what the
24 * module implements.
25 *
26 * Macros that compute sizes whose values do not depend on the
Jamie Fox0e54ebc2019-04-09 14:21:04 +010027 * implementation are in crypto.h.
Antonio de Angelis377a1552018-11-22 17:02:40 +000028 */
29
30#ifndef PSA_CRYPTO_SIZES_H
31#define PSA_CRYPTO_SIZES_H
32
Jamie Fox0e54ebc2019-04-09 14:21:04 +010033#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
34#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
35
Antonio de Angelis04debbd2019-10-14 12:12:52 +010036#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
37 (((length) + (block_size) - 1) / (block_size) * (block_size))
38
Jamie Fox0e54ebc2019-04-09 14:21:04 +010039/** The size of the output of psa_hash_finish(), in bytes.
40 *
41 * This is also the hash size that psa_hash_verify() expects.
42 *
43 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
44 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
45 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
46 * hash algorithm).
47 *
48 * \return The hash size for the specified hash algorithm.
49 * If the hash algorithm is not recognized, return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +010050 */
Maulik Patel13b27cf2021-05-14 11:44:53 +010051#define PSA_HASH_LENGTH(alg) \
52 ( \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010053 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
54 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
55 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
56 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
57 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
58 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
59 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
60 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
61 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
62 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
66 0)
67
Summer Qinf07cc312022-01-05 16:52:54 +080068/** The input block size of a hash algorithm, in bytes.
69 *
70 * Hash algorithms process their input data in blocks. Hash operations will
71 * retain any partial blocks until they have enough input to fill the block or
72 * until the operation is finished.
73 * This affects the output from psa_hash_suspend().
74 *
75 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
76 * PSA_ALG_IS_HASH(\p alg) is true).
77 *
78 * \return The block size in bytes for the specified hash algorithm.
79 * If the hash algorithm is not recognized, return 0.
80 * An implementation can return either 0 or the correct size for a
81 * hash algorithm that it recognizes, but does not support.
82 */
83#define PSA_HASH_BLOCK_LENGTH(alg) \
84 ( \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
87 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
88 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
89 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
90 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
91 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
92 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
93 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
94 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
95 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
96 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
97 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
98 0)
99
Antonio de Angelis377a1552018-11-22 17:02:40 +0000100/** \def PSA_HASH_MAX_SIZE
101 *
102 * Maximum size of a hash.
103 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100104 * This macro expands to a compile-time constant integer. This value
105 * is the maximum size of a hash in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000106 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100107/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
108 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
109 * HMAC-SHA3-512. */
Summer Qinf07cc312022-01-05 16:52:54 +0800110#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000111#define PSA_HASH_MAX_SIZE 64
112#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
Summer Qin359167d2021-07-05 18:11:50 +0800113#else
114#define PSA_HASH_MAX_SIZE 32
115#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
116#endif
Antonio de Angelis377a1552018-11-22 17:02:40 +0000117
118/** \def PSA_MAC_MAX_SIZE
119 *
120 * Maximum size of a MAC.
121 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100122 * This macro expands to a compile-time constant integer. This value
123 * is the maximum size of a MAC in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000124 */
125/* All non-HMAC MACs have a maximum size that's smaller than the
126 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100127/* Note that the encoding of truncated MAC algorithms limits this value
128 * to 64 bytes.
129 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000130#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
131
Summer Qin359167d2021-07-05 18:11:50 +0800132/** The length of a tag for an AEAD algorithm, in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100133 *
Summer Qin359167d2021-07-05 18:11:50 +0800134 * This macro can be used to allocate a buffer of sufficient size to store the
135 * tag output from psa_aead_finish().
136 *
137 * See also #PSA_AEAD_TAG_MAX_SIZE.
138 *
139 * \param key_type The type of the AEAD key.
140 * \param key_bits The size of the AEAD key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100141 * \param alg An AEAD algorithm
142 * (\c PSA_ALG_XXX value such that
143 * #PSA_ALG_IS_AEAD(\p alg) is true).
144 *
Summer Qin359167d2021-07-05 18:11:50 +0800145 * \return The tag length for the specified algorithm and key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100146 * If the AEAD algorithm does not have an identified
147 * tag that can be distinguished from the rest of
148 * the ciphertext, return 0.
Summer Qin359167d2021-07-05 18:11:50 +0800149 * If the key type or AEAD algorithm is not
150 * recognized, or the parameters are incompatible,
151 * return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100152 */
Summer Qin359167d2021-07-05 18:11:50 +0800153#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
154 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
155 PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
156 ((void) (key_bits), 0))
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100157
Maulik Patel13b27cf2021-05-14 11:44:53 +0100158/** The maximum tag size for all supported AEAD algorithms, in bytes.
159 *
Summer Qin359167d2021-07-05 18:11:50 +0800160 * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100161 */
162#define PSA_AEAD_TAG_MAX_SIZE 16
163
Antonio de Angelis377a1552018-11-22 17:02:40 +0000164/* The maximum size of an RSA key on this implementation, in bits.
165 * This is a vendor-specific macro.
166 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100167 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000168 * whose parameters fit in a bignum is accepted. However large keys can
169 * induce a large memory usage and long computation times. Unlike other
170 * auxiliary macros in this file and in crypto.h, which reflect how the
171 * library is configured, this macro defines how the library is
172 * configured. This implementation refuses to import or generate an
173 * RSA key whose size is larger than the value defined here.
174 *
175 * Note that an implementation may set different size limits for different
176 * operations, and does not need to accept all key sizes up to the limit. */
177#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
178
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100179/* The maximum size of an ECC key on this implementation, in bits.
180 * This is a vendor-specific macro. */
181#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
182#if defined(MBEDTLS_PSA_CRYPTO_CONFIG_FILE)
183#include MBEDTLS_PSA_CRYPTO_CONFIG_FILE
184#else
185#include "psa/crypto_config.h"
186#endif
187#if defined(PSA_WANT_ECC_SECP_R1_521)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100188#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100189#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
190#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
191#elif defined(PSA_WANT_ECC_MONTGOMERY_448)
192#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
193#elif defined(PSA_WANT_ECC_SECP_R1_384)
194#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
195#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
196#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
197#elif defined(PSA_WANT_ECC_SECP_R1_256)
198#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
199#elif defined(PSA_WANT_ECC_SECP_K1_256)
200#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
201#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
202#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
203#elif defined(PSA_WANT_ECC_MONTGOMERY_255)
204#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
205#elif defined(PSA_WANT_ECC_SECP_R1_224)
206#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
207#elif defined(PSA_WANT_ECC_SECP_K1_224)
208#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
209#elif defined(PSA_WANT_ECC_SECP_R1_192)
210#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
211#elif defined(PSA_WANT_ECC_SECP_K1_192)
212#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
213#else
214#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
215#endif
216#else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
217#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
218#endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100219
Maulik Patel13b27cf2021-05-14 11:44:53 +0100220/** This macro returns the maximum supported length of the PSK for the
221 * TLS-1.2 PSK-to-MS key derivation
Summer Qin359167d2021-07-05 18:11:50 +0800222 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100223 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100224 * The maximum supported length does not depend on the chosen hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100225 *
226 * Quoting RFC 4279, Sect 5.3:
227 * TLS implementations supporting these ciphersuites MUST support
228 * arbitrary PSK identities up to 128 octets in length, and arbitrary
229 * PSKs up to 64 octets in length. Supporting longer identities and
230 * keys is RECOMMENDED.
231 *
232 * Therefore, no implementation should define a value smaller than 64
Maulik Patel13b27cf2021-05-14 11:44:53 +0100233 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100234 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100235#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000236
Maulik Patel13b27cf2021-05-14 11:44:53 +0100237/** The maximum size of a block cipher. */
238#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
239
Antonio de Angelis377a1552018-11-22 17:02:40 +0000240/** The size of the output of psa_mac_sign_finish(), in bytes.
241 *
242 * This is also the MAC size that psa_mac_verify_finish() expects.
243 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100244 * \warning This macro may evaluate its arguments multiple times or
245 * zero times, so you should not pass arguments that contain
246 * side effects.
247 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000248 * \param key_type The type of the MAC key.
249 * \param key_bits The size of the MAC key in bits.
250 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100251 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000252 *
253 * \return The MAC size for the specified algorithm with
254 * the specified key parameters.
255 * \return 0 if the MAC algorithm is not recognized.
256 * \return Either 0 or the correct size for a MAC algorithm that
257 * the implementation recognizes, but does not support.
258 * \return Unspecified if the key parameters are not consistent
259 * with the algorithm.
260 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100261#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
262 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
263 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
264 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100265 ((void)(key_type), (void)(key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000266
267/** The maximum size of the output of psa_aead_encrypt(), in bytes.
268 *
269 * If the size of the ciphertext buffer is at least this large, it is
270 * guaranteed that psa_aead_encrypt() will not fail due to an
271 * insufficient buffer size. Depending on the algorithm, the actual size of
272 * the ciphertext may be smaller.
273 *
Summer Qin359167d2021-07-05 18:11:50 +0800274 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
275 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100276 * \warning This macro may evaluate its arguments multiple times or
277 * zero times, so you should not pass arguments that contain
278 * side effects.
279 *
Summer Qin359167d2021-07-05 18:11:50 +0800280 * \param key_type A symmetric key type that is
281 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000282 * \param alg An AEAD algorithm
283 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100284 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000285 * \param plaintext_length Size of the plaintext in bytes.
286 *
287 * \return The AEAD ciphertext size for the specified
288 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800289 * If the key type or AEAD algorithm is not
290 * recognized, or the parameters are incompatible,
291 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000292 */
Summer Qin359167d2021-07-05 18:11:50 +0800293#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
294 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
295 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000296 0)
297
Maulik Patel13b27cf2021-05-14 11:44:53 +0100298/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
299 * supported key types and AEAD algorithms.
300 *
301 * If the size of the ciphertext buffer is at least this large, it is guaranteed
302 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
303 *
304 * \note This macro returns a compile-time constant if its arguments are
305 * compile-time constants.
306 *
Summer Qin359167d2021-07-05 18:11:50 +0800307 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
308 * \p plaintext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100309 *
310 * \param plaintext_length Size of the plaintext in bytes.
311 *
312 * \return A sufficient output buffer size for any of the
313 * supported key types and AEAD algorithms.
314 *
315 */
316#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
317 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
318
319
Antonio de Angelis377a1552018-11-22 17:02:40 +0000320/** The maximum size of the output of psa_aead_decrypt(), in bytes.
321 *
322 * If the size of the plaintext buffer is at least this large, it is
323 * guaranteed that psa_aead_decrypt() will not fail due to an
324 * insufficient buffer size. Depending on the algorithm, the actual size of
325 * the plaintext may be smaller.
326 *
Summer Qin359167d2021-07-05 18:11:50 +0800327 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
328 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100329 * \warning This macro may evaluate its arguments multiple times or
330 * zero times, so you should not pass arguments that contain
331 * side effects.
332 *
Summer Qin359167d2021-07-05 18:11:50 +0800333 * \param key_type A symmetric key type that is
334 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000335 * \param alg An AEAD algorithm
336 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100337 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000338 * \param ciphertext_length Size of the plaintext in bytes.
339 *
340 * \return The AEAD ciphertext size for the specified
341 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800342 * If the key type or AEAD algorithm is not
343 * recognized, or the parameters are incompatible,
344 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000345 */
Summer Qin359167d2021-07-05 18:11:50 +0800346#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
347 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
348 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
349 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100350 0)
351
Maulik Patel13b27cf2021-05-14 11:44:53 +0100352/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
353 * supported key types and AEAD algorithms.
354 *
355 * If the size of the plaintext buffer is at least this large, it is guaranteed
356 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
357 *
358 * \note This macro returns a compile-time constant if its arguments are
359 * compile-time constants.
360 *
Summer Qin359167d2021-07-05 18:11:50 +0800361 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
362 * \p ciphertext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100363 *
364 * \param ciphertext_length Size of the ciphertext in bytes.
365 *
366 * \return A sufficient output buffer size for any of the
367 * supported key types and AEAD algorithms.
368 *
369 */
370#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
371 (ciphertext_length)
372
373/** The default nonce size for an AEAD algorithm, in bytes.
374 *
375 * This macro can be used to allocate a buffer of sufficient size to
376 * store the nonce output from #psa_aead_generate_nonce().
377 *
378 * See also #PSA_AEAD_NONCE_MAX_SIZE.
379 *
380 * \note This is not the maximum size of nonce supported as input to
381 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
382 * just the default size that is generated by #psa_aead_generate_nonce().
383 *
384 * \warning This macro may evaluate its arguments multiple times or
385 * zero times, so you should not pass arguments that contain
386 * side effects.
387 *
388 * \param key_type A symmetric key type that is compatible with
389 * algorithm \p alg.
390 *
391 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
392 * #PSA_ALG_IS_AEAD(\p alg) is true).
393 *
394 * \return The default nonce size for the specified key type and algorithm.
395 * If the key type or AEAD algorithm is not recognized,
396 * or the parameters are incompatible, return 0.
397 */
398#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Summer Qin359167d2021-07-05 18:11:50 +0800399 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
400 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
401 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
402 0 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100403 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Summer Qin359167d2021-07-05 18:11:50 +0800404 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100405 0)
406
407/** The maximum default nonce size among all supported pairs of key types and
408 * AEAD algorithms, in bytes.
409 *
410 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
411 * may return.
412 *
413 * \note This is not the maximum size of nonce supported as input to
414 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
415 * just the largest size that may be generated by
416 * #psa_aead_generate_nonce().
417 */
Summer Qin359167d2021-07-05 18:11:50 +0800418#define PSA_AEAD_NONCE_MAX_SIZE 13
Maulik Patel13b27cf2021-05-14 11:44:53 +0100419
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100420/** A sufficient output buffer size for psa_aead_update().
421 *
422 * If the size of the output buffer is at least this large, it is
423 * guaranteed that psa_aead_update() will not fail due to an
424 * insufficient buffer size. The actual size of the output may be smaller
425 * in any given call.
426 *
Summer Qin359167d2021-07-05 18:11:50 +0800427 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
428 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100429 * \warning This macro may evaluate its arguments multiple times or
430 * zero times, so you should not pass arguments that contain
431 * side effects.
432 *
Summer Qin359167d2021-07-05 18:11:50 +0800433 * \param key_type A symmetric key type that is
434 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100435 * \param alg An AEAD algorithm
436 * (\c PSA_ALG_XXX value such that
437 * #PSA_ALG_IS_AEAD(\p alg) is true).
438 * \param input_length Size of the input in bytes.
439 *
440 * \return A sufficient output buffer size for the specified
441 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800442 * If the key type or AEAD algorithm is not
443 * recognized, or the parameters are incompatible,
444 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100445 */
446/* For all the AEAD modes defined in this specification, it is possible
447 * to emit output without delay. However, hardware may not always be
448 * capable of this. So for modes based on a block cipher, allow the
449 * implementation to delay the output until it has a full block. */
Summer Qin359167d2021-07-05 18:11:50 +0800450#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
451 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
452 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
453 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
454 (input_length) : \
455 0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100456
Maulik Patel13b27cf2021-05-14 11:44:53 +0100457/** A sufficient output buffer size for psa_aead_update(), for any of the
458 * supported key types and AEAD algorithms.
459 *
460 * If the size of the output buffer is at least this large, it is guaranteed
461 * that psa_aead_update() will not fail due to an insufficient buffer size.
462 *
Summer Qin359167d2021-07-05 18:11:50 +0800463 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100464 *
465 * \param input_length Size of the input in bytes.
466 */
467#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
468 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
469
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100470/** A sufficient ciphertext buffer size for psa_aead_finish().
471 *
472 * If the size of the ciphertext buffer is at least this large, it is
473 * guaranteed that psa_aead_finish() will not fail due to an
474 * insufficient ciphertext buffer size. The actual size of the output may
475 * be smaller in any given call.
476 *
Summer Qin359167d2021-07-05 18:11:50 +0800477 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
478 *
479 * \param key_type A symmetric key type that is
480 compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100481 * \param alg An AEAD algorithm
482 * (\c PSA_ALG_XXX value such that
483 * #PSA_ALG_IS_AEAD(\p alg) is true).
484 *
485 * \return A sufficient ciphertext buffer size for the
486 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800487 * If the key type or AEAD algorithm is not
488 * recognized, or the parameters are incompatible,
489 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100490 */
Summer Qin359167d2021-07-05 18:11:50 +0800491#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
492 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
493 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
494 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100495 0)
496
Maulik Patel13b27cf2021-05-14 11:44:53 +0100497/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
498 * supported key types and AEAD algorithms.
499 *
Summer Qin359167d2021-07-05 18:11:50 +0800500 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100501 */
502#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
503
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100504/** A sufficient plaintext buffer size for psa_aead_verify().
505 *
506 * If the size of the plaintext buffer is at least this large, it is
507 * guaranteed that psa_aead_verify() will not fail due to an
508 * insufficient plaintext buffer size. The actual size of the output may
509 * be smaller in any given call.
510 *
Summer Qin359167d2021-07-05 18:11:50 +0800511 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
512 *
513 * \param key_type A symmetric key type that is
514 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100515 * \param alg An AEAD algorithm
516 * (\c PSA_ALG_XXX value such that
517 * #PSA_ALG_IS_AEAD(\p alg) is true).
518 *
519 * \return A sufficient plaintext buffer size for the
520 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800521 * If the key type or AEAD algorithm is not
522 * recognized, or the parameters are incompatible,
523 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100524 */
Summer Qin359167d2021-07-05 18:11:50 +0800525#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
526 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
527 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
528 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000529 0)
530
Maulik Patel13b27cf2021-05-14 11:44:53 +0100531/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
532 * supported key types and AEAD algorithms.
533 *
Summer Qin359167d2021-07-05 18:11:50 +0800534 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100535 */
536#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
537
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100538#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
539 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100540 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100541 11 /*PKCS#1v1.5*/)
542
543/**
544 * \brief ECDSA signature size for a given curve bit size
545 *
546 * \param curve_bits Curve size in bits.
547 * \return Signature size in bytes.
548 *
549 * \note This macro returns a compile-time constant if its argument is one.
550 */
551#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
552 (PSA_BITS_TO_BYTES(curve_bits) * 2)
553
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100554/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000555 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100556 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000557 * of the specified type and size, with the specified algorithm.
558 * Note that the actual size of the signature may be smaller
559 * (some algorithms produce a variable-size signature).
560 *
561 * \warning This function may call its arguments multiple times or
562 * zero times, so you should not pass arguments that contain
563 * side effects.
564 *
565 * \param key_type An asymmetric key type (this may indifferently be a
566 * key pair type or a public key type).
567 * \param key_bits The size of the key in bits.
568 * \param alg The signature algorithm.
569 *
570 * \return If the parameters are valid and supported, return
571 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100572 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000573 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100574 * If the parameters are a valid combination that is not supported,
575 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000576 * If the parameters are not valid, the
577 * return value is unspecified.
578 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100579#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000580 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
581 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
582 ((void)alg, 0))
583
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100584#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
585 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
586
587/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000588 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100589 * Maximum size of an asymmetric signature.
590 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100591 * This macro expands to a compile-time constant integer. This value
592 * is the maximum size of a signature in bytes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100593 */
594#define PSA_SIGNATURE_MAX_SIZE \
595 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
596 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
597 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
598
599/** Sufficient output buffer size for psa_asymmetric_encrypt().
600 *
601 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000602 * a key of the specified type and size, with the specified algorithm.
603 * Note that the actual size of the ciphertext may be smaller, depending
604 * on the algorithm.
605 *
606 * \warning This function may call its arguments multiple times or
607 * zero times, so you should not pass arguments that contain
608 * side effects.
609 *
610 * \param key_type An asymmetric key type (this may indifferently be a
611 * key pair type or a public key type).
612 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100613 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000614 *
615 * \return If the parameters are valid and supported, return
616 * a buffer size in bytes that guarantees that
617 * psa_asymmetric_encrypt() will not fail with
618 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100619 * If the parameters are a valid combination that is not supported,
620 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000621 * If the parameters are not valid, the
622 * return value is unspecified.
623 */
624#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
625 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
626 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
627 0)
628
Maulik Patel13b27cf2021-05-14 11:44:53 +0100629/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
630 * supported asymmetric encryption.
631 *
632 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
633 */
634/* This macro assumes that RSA is the only supported asymmetric encryption. */
635#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
636 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
637
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100638/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000639 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100640 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000641 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100642 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000643 * on the algorithm.
644 *
645 * \warning This function may call its arguments multiple times or
646 * zero times, so you should not pass arguments that contain
647 * side effects.
648 *
649 * \param key_type An asymmetric key type (this may indifferently be a
650 * key pair type or a public key type).
651 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100652 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000653 *
654 * \return If the parameters are valid and supported, return
655 * a buffer size in bytes that guarantees that
656 * psa_asymmetric_decrypt() will not fail with
657 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100658 * If the parameters are a valid combination that is not supported,
659 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000660 * If the parameters are not valid, the
661 * return value is unspecified.
662 */
663#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
664 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
665 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
666 0)
667
Maulik Patel13b27cf2021-05-14 11:44:53 +0100668/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
669 * supported asymmetric decryption.
670 *
671 * This macro assumes that RSA is the only supported asymmetric encryption.
672 *
673 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
674 */
675#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
676 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
677
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100678/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
679 * number of bits.
680 *
681 * This definition assumes that bits <= 2^19 - 9 so that the length field
682 * is at most 3 bytes. The length of the encoding is the length of the
683 * bit string padded to a whole number of bytes plus:
684 * - 1 type byte;
685 * - 1 to 3 length bytes;
686 * - 0 to 1 bytes of leading 0 due to the sign bit.
687 */
688#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
689 ((bits) / 8 + 5)
690
691/* Maximum size of the export encoding of an RSA public key.
692 * Assumes that the public exponent is less than 2^32.
693 *
694 * RSAPublicKey ::= SEQUENCE {
695 * modulus INTEGER, -- n
696 * publicExponent INTEGER } -- e
697 *
698 * - 4 bytes of SEQUENCE overhead;
699 * - n : INTEGER;
700 * - 7 bytes for the public exponent.
701 */
702#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
703 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
704
705/* Maximum size of the export encoding of an RSA key pair.
706 * Assumes thatthe public exponent is less than 2^32 and that the size
707 * difference between the two primes is at most 1 bit.
708 *
709 * RSAPrivateKey ::= SEQUENCE {
710 * version Version, -- 0
711 * modulus INTEGER, -- N-bit
712 * publicExponent INTEGER, -- 32-bit
713 * privateExponent INTEGER, -- N-bit
714 * prime1 INTEGER, -- N/2-bit
715 * prime2 INTEGER, -- N/2-bit
716 * exponent1 INTEGER, -- N/2-bit
717 * exponent2 INTEGER, -- N/2-bit
718 * coefficient INTEGER, -- N/2-bit
719 * }
720 *
721 * - 4 bytes of SEQUENCE overhead;
722 * - 3 bytes of version;
723 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
724 * overapproximated as 9 half-size INTEGERS;
725 * - 7 bytes for the public exponent.
726 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100727#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100728 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
729
730/* Maximum size of the export encoding of a DSA public key.
731 *
732 * SubjectPublicKeyInfo ::= SEQUENCE {
733 * algorithm AlgorithmIdentifier,
734 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
735 * AlgorithmIdentifier ::= SEQUENCE {
736 * algorithm OBJECT IDENTIFIER,
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100737 * parameters Dss-Params } -- SEQUENCE of 3 INTEGERs
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100738 * DSAPublicKey ::= INTEGER -- public key, Y
739 *
740 * - 3 * 4 bytes of SEQUENCE overhead;
741 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
742 * - 4 bytes of BIT STRING overhead;
743 * - 3 full-size INTEGERs (p, g, y);
744 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
745 */
746#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
747 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
748
749/* Maximum size of the export encoding of a DSA key pair.
750 *
751 * DSAPrivateKey ::= SEQUENCE {
752 * version Version, -- 0
753 * prime INTEGER, -- p
754 * subprime INTEGER, -- q
755 * generator INTEGER, -- g
756 * public INTEGER, -- y
757 * private INTEGER, -- x
758 * }
759 *
760 * - 4 bytes of SEQUENCE overhead;
761 * - 3 bytes of version;
762 * - 3 full-size INTEGERs (p, g, y);
763 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
764 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100765#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100766 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
767
768/* Maximum size of the export encoding of an ECC public key.
769 *
770 * The representation of an ECC public key is:
771 * - The byte 0x04;
772 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
773 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
774 * - where m is the bit size associated with the curve.
775 *
776 * - 1 byte + 2 * point size.
777 */
778#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
779 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
780
781/* Maximum size of the export encoding of an ECC key pair.
782 *
783 * An ECC key pair is represented by the secret value.
784 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100785#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100786 (PSA_BITS_TO_BYTES(key_bits))
787
Maulik Patel13b27cf2021-05-14 11:44:53 +0100788/** Sufficient output buffer size for psa_export_key() or
789 * psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100790 *
791 * This macro returns a compile-time constant if its arguments are
792 * compile-time constants.
793 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100794 * \warning This macro may evaluate its arguments multiple times or
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100795 * zero times, so you should not pass arguments that contain
796 * side effects.
797 *
798 * The following code illustrates how to allocate enough memory to export
799 * a key by querying the key type and size at runtime.
800 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100801 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100802 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100803 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100804 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100805 * psa_key_type_t key_type = psa_get_key_type(&attributes);
806 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100807 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100808 * psa_reset_key_attributes(&attributes);
809 * uint8_t *buffer = malloc(buffer_size);
810 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100811 * size_t buffer_length;
812 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
813 * if (status != PSA_SUCCESS) handle_error(...);
814 * \endcode
815 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100816 * \param key_type A supported key type.
817 * \param key_bits The size of the key in bits.
818 *
819 * \return If the parameters are valid and supported, return
820 * a buffer size in bytes that guarantees that
821 * psa_export_key() or psa_export_public_key() will not fail with
822 * #PSA_ERROR_BUFFER_TOO_SMALL.
823 * If the parameters are a valid combination that is not supported,
824 * return either a sensible size or 0.
825 * If the parameters are not valid, the return value is unspecified.
826 */
827#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
828 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
829 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
830 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
831 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_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_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
834 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
835 0)
836
837/** Sufficient output buffer size for psa_export_public_key().
838 *
839 * This macro returns a compile-time constant if its arguments are
840 * compile-time constants.
841 *
842 * \warning This macro may evaluate its arguments multiple times or
843 * zero times, so you should not pass arguments that contain
844 * side effects.
845 *
846 * The following code illustrates how to allocate enough memory to export
847 * a public key by querying the key type and size at runtime.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100848 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100849 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100850 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100851 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100852 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100853 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100854 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100855 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100856 * psa_reset_key_attributes(&attributes);
857 * uint8_t *buffer = malloc(buffer_size);
858 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100859 * size_t buffer_length;
860 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
861 * if (status != PSA_SUCCESS) handle_error(...);
862 * \endcode
863 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100864 * \param key_type A public key or key pair key type.
865 * \param key_bits The size of the key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100866 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100867 * \return If the parameters are valid and supported, return
868 * a buffer size in bytes that guarantees that
869 * psa_export_public_key() will not fail with
870 * #PSA_ERROR_BUFFER_TOO_SMALL.
871 * If the parameters are a valid combination that is not
872 * supported, return either a sensible size or 0.
873 * If the parameters are not valid,
874 * the return value is unspecified.
875 *
876 * If the parameters are valid and supported,
877 * return the same result as
878 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
879 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
880 * \p key_bits).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100881 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100882#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
883 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
884 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100885 0)
886
Maulik Patel13b27cf2021-05-14 11:44:53 +0100887/** Sufficient buffer size for exporting any asymmetric key pair.
Maulik Patel28659c42021-01-06 14:09:22 +0000888 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100889 * This macro expands to a compile-time constant integer. This value is
890 * a sufficient buffer size when calling psa_export_key() to export any
891 * asymmetric key pair, regardless of the exact key type and key size.
Maulik Patel28659c42021-01-06 14:09:22 +0000892 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100893 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
894 */
895#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
896 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
897 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
898 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
899 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
900
901/** Sufficient buffer size for exporting any asymmetric public key.
Maulik Patel28659c42021-01-06 14:09:22 +0000902 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100903 * This macro expands to a compile-time constant integer. This value is
904 * a sufficient buffer size when calling psa_export_key() or
905 * psa_export_public_key() to export any asymmetric public key,
906 * regardless of the exact key type and key size.
907 *
908 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
909 */
910#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
911 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
912 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
913 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
914 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
915
916/** Sufficient output buffer size for psa_raw_key_agreement().
917 *
918 * This macro returns a compile-time constant if its arguments are
919 * compile-time constants.
Maulik Patel28659c42021-01-06 14:09:22 +0000920 *
921 * \warning This macro may evaluate its arguments multiple times or
922 * zero times, so you should not pass arguments that contain
923 * side effects.
924 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100925 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
Maulik Patel28659c42021-01-06 14:09:22 +0000926 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100927 * \param key_type A supported key type.
928 * \param key_bits The size of the key in bits.
Maulik Patel28659c42021-01-06 14:09:22 +0000929 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100930 * \return If the parameters are valid and supported, return
931 * a buffer size in bytes that guarantees that
932 * psa_raw_key_agreement() will not fail with
933 * #PSA_ERROR_BUFFER_TOO_SMALL.
934 * If the parameters are a valid combination that
935 * is not supported, return either a sensible size or 0.
936 * If the parameters are not valid,
937 * the return value is unspecified.
Maulik Patel28659c42021-01-06 14:09:22 +0000938 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100939/* FFDH is not yet supported in PSA. */
940#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
941 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
942 PSA_BITS_TO_BYTES(key_bits) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000943 0)
944
Maulik Patel13b27cf2021-05-14 11:44:53 +0100945/** Maximum size of the output from psa_raw_key_agreement().
Maulik Patel28659c42021-01-06 14:09:22 +0000946 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100947 * This macro expands to a compile-time constant integer. This value is the
948 * maximum size of the output any raw key agreement algorithm, in bytes.
Maulik Patel28659c42021-01-06 14:09:22 +0000949 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100950 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
Maulik Patel28659c42021-01-06 14:09:22 +0000951 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100952#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
953 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
Maulik Patel28659c42021-01-06 14:09:22 +0000954
955/** The default IV size for a cipher algorithm, in bytes.
956 *
957 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
958 * the default IV length for the algorithm.
959 *
960 * This macro can be used to allocate a buffer of sufficient size to
961 * store the IV output from #psa_cipher_generate_iv() when using
962 * a multi-part cipher operation.
963 *
964 * See also #PSA_CIPHER_IV_MAX_SIZE.
965 *
966 * \warning This macro may evaluate its arguments multiple times or
967 * zero times, so you should not pass arguments that contain
968 * side effects.
969 *
970 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
971 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100972 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
Maulik Patel28659c42021-01-06 14:09:22 +0000973 *
974 * \return The default IV size for the specified key type and algorithm.
975 * If the algorithm does not use an IV, return 0.
976 * If the key type or cipher algorithm is not recognized,
977 * or the parameters are incompatible, return 0.
Maulik Patel28659c42021-01-06 14:09:22 +0000978 */
979#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100980 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Maulik Patel28659c42021-01-06 14:09:22 +0000981 ((alg) == PSA_ALG_CTR || \
982 (alg) == PSA_ALG_CFB || \
983 (alg) == PSA_ALG_OFB || \
984 (alg) == PSA_ALG_XTS || \
985 (alg) == PSA_ALG_CBC_NO_PADDING || \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100986 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000987 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
988 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Summer Qinf07cc312022-01-05 16:52:54 +0800989 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
990 0)
Maulik Patel28659c42021-01-06 14:09:22 +0000991
992/** The maximum IV size for all supported cipher algorithms, in bytes.
993 *
994 * See also #PSA_CIPHER_IV_LENGTH().
995 */
996#define PSA_CIPHER_IV_MAX_SIZE 16
997
Maulik Patel13b27cf2021-05-14 11:44:53 +0100998/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
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 * Depending on the algorithm, the actual size of the output might be smaller.
1003 *
1004 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
1005 *
1006 * \warning This macro may evaluate its arguments multiple times or
1007 * zero times, so you should not pass arguments that contain
1008 * side effects.
1009 *
1010 * \param key_type A symmetric key type that is compatible with algorithm
1011 * alg.
1012 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1013 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1014 * \param input_length Size of the input in bytes.
1015 *
1016 * \return A sufficient output size for the specified key type and
1017 * algorithm. If the key type or cipher algorithm is not
1018 * recognized, or the parameters are incompatible,
1019 * return 0.
1020 */
1021#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1022 (alg == PSA_ALG_CBC_PKCS7 ? \
Summer Qinf07cc312022-01-05 16:52:54 +08001023 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001024 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1025 (input_length) + 1) + \
Summer Qinf07cc312022-01-05 16:52:54 +08001026 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001027 (PSA_ALG_IS_CIPHER(alg) ? \
1028 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1029 0))
1030
1031/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
1032 * supported key types and cipher algorithms.
1033 *
1034 * If the size of the output buffer is at least this large, it is guaranteed
1035 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
1036 *
1037 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1038 *
1039 * \param input_length Size of the input in bytes.
1040 *
1041 */
1042#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1043 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1044 (input_length) + 1) + \
1045 PSA_CIPHER_IV_MAX_SIZE)
1046
1047/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1048 *
1049 * If the size of the output buffer is at least this large, it is guaranteed
1050 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1051 * Depending on the algorithm, the actual size of the output might be smaller.
1052 *
1053 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
1054 *
1055 * \param key_type A symmetric key type that is compatible with algorithm
1056 * alg.
1057 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1058 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1059 * \param input_length Size of the input in bytes.
1060 *
1061 * \return A sufficient output size for the specified key type and
1062 * algorithm. If the key type or cipher algorithm is not
1063 * recognized, or the parameters are incompatible,
1064 * return 0.
1065 */
1066#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1067 (PSA_ALG_IS_CIPHER(alg) && \
1068 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1069 (input_length) : \
1070 0)
1071
1072/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1073 * supported key types and cipher algorithms.
1074 *
1075 * If the size of the output buffer is at least this large, it is guaranteed
1076 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1077 *
1078 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1079 *
1080 * \param input_length Size of the input in bytes.
1081 */
1082#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1083 (input_length)
1084
1085/** A sufficient output buffer size for psa_cipher_update().
1086 *
1087 * If the size of the output buffer is at least this large, it is guaranteed
1088 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1089 * The actual size of the output might be smaller in any given call.
1090 *
1091 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1092 *
1093 * \param key_type A symmetric key type that is compatible with algorithm
1094 * alg.
1095 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1096 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1097 * \param input_length Size of the input in bytes.
1098 *
1099 * \return A sufficient output size for the specified key type and
1100 * algorithm. If the key type or cipher algorithm is not
1101 * recognized, or the parameters are incompatible, return 0.
1102 */
1103#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1104 (PSA_ALG_IS_CIPHER(alg) ? \
Summer Qinf07cc312022-01-05 16:52:54 +08001105 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001106 (((alg) == PSA_ALG_CBC_PKCS7 || \
1107 (alg) == PSA_ALG_CBC_NO_PADDING || \
1108 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1109 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1110 input_length) : \
Summer Qinf07cc312022-01-05 16:52:54 +08001111 (input_length)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001112 0)
1113
1114/** A sufficient output buffer size for psa_cipher_update(), for any of the
1115 * supported key types and cipher algorithms.
1116 *
1117 * If the size of the output buffer is at least this large, it is guaranteed
1118 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1119 *
1120 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1121 *
1122 * \param input_length Size of the input in bytes.
1123 */
1124#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1125 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1126
1127/** A sufficient ciphertext buffer size for psa_cipher_finish().
1128 *
1129 * If the size of the ciphertext buffer is at least this large, it is
1130 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1131 * ciphertext buffer size. The actual size of the output might be smaller in
1132 * any given call.
1133 *
1134 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1135 *
1136 * \param key_type A symmetric key type that is compatible with algorithm
1137 * alg.
1138 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1139 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1140 * \return A sufficient output size for the specified key type and
1141 * algorithm. If the key type or cipher algorithm is not
1142 * recognized, or the parameters are incompatible, return 0.
1143 */
1144#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1145 (PSA_ALG_IS_CIPHER(alg) ? \
1146 (alg == PSA_ALG_CBC_PKCS7 ? \
1147 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1148 0) : \
1149 0)
1150
1151/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1152 * supported key types and cipher algorithms.
1153 *
1154 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1155 */
1156#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1157 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1158
Antonio de Angelis377a1552018-11-22 17:02:40 +00001159#endif /* PSA_CRYPTO_SIZES_H */