blob: 2f60e61d98cb7074db55ae25e8065d2ca5cd81a3 [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
Soby Mathew07ef6e42020-07-20 21:09:23 +0100179/* The maximum size of an ECC key on this implementation, in bits */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100180#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
181
Maulik Patel13b27cf2021-05-14 11:44:53 +0100182/** This macro returns the maximum supported length of the PSK for the
183 * TLS-1.2 PSK-to-MS key derivation
Summer Qin359167d2021-07-05 18:11:50 +0800184 * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100185 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100186 * The maximum supported length does not depend on the chosen hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100187 *
188 * Quoting RFC 4279, Sect 5.3:
189 * TLS implementations supporting these ciphersuites MUST support
190 * arbitrary PSK identities up to 128 octets in length, and arbitrary
191 * PSKs up to 64 octets in length. Supporting longer identities and
192 * keys is RECOMMENDED.
193 *
194 * Therefore, no implementation should define a value smaller than 64
Maulik Patel13b27cf2021-05-14 11:44:53 +0100195 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100196 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100197#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000198
Maulik Patel13b27cf2021-05-14 11:44:53 +0100199/** The maximum size of a block cipher. */
200#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
201
Antonio de Angelis377a1552018-11-22 17:02:40 +0000202/** The size of the output of psa_mac_sign_finish(), in bytes.
203 *
204 * This is also the MAC size that psa_mac_verify_finish() expects.
205 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100206 * \warning This macro may evaluate its arguments multiple times or
207 * zero times, so you should not pass arguments that contain
208 * side effects.
209 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000210 * \param key_type The type of the MAC key.
211 * \param key_bits The size of the MAC key in bits.
212 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100213 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000214 *
215 * \return The MAC size for the specified algorithm with
216 * the specified key parameters.
217 * \return 0 if the MAC algorithm is not recognized.
218 * \return Either 0 or the correct size for a MAC algorithm that
219 * the implementation recognizes, but does not support.
220 * \return Unspecified if the key parameters are not consistent
221 * with the algorithm.
222 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100223#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
224 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
225 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
226 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100227 ((void)(key_type), (void)(key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000228
229/** The maximum size of the output of psa_aead_encrypt(), in bytes.
230 *
231 * If the size of the ciphertext buffer is at least this large, it is
232 * guaranteed that psa_aead_encrypt() will not fail due to an
233 * insufficient buffer size. Depending on the algorithm, the actual size of
234 * the ciphertext may be smaller.
235 *
Summer Qin359167d2021-07-05 18:11:50 +0800236 * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
237 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100238 * \warning This macro may evaluate its arguments multiple times or
239 * zero times, so you should not pass arguments that contain
240 * side effects.
241 *
Summer Qin359167d2021-07-05 18:11:50 +0800242 * \param key_type A symmetric key type that is
243 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000244 * \param alg An AEAD algorithm
245 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100246 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000247 * \param plaintext_length Size of the plaintext in bytes.
248 *
249 * \return The AEAD ciphertext size for the specified
250 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800251 * If the key type or AEAD algorithm is not
252 * recognized, or the parameters are incompatible,
253 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000254 */
Summer Qin359167d2021-07-05 18:11:50 +0800255#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
256 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
257 (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000258 0)
259
Maulik Patel13b27cf2021-05-14 11:44:53 +0100260/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
261 * supported key types and AEAD algorithms.
262 *
263 * If the size of the ciphertext buffer is at least this large, it is guaranteed
264 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
265 *
266 * \note This macro returns a compile-time constant if its arguments are
267 * compile-time constants.
268 *
Summer Qin359167d2021-07-05 18:11:50 +0800269 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
270 * \p plaintext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100271 *
272 * \param plaintext_length Size of the plaintext in bytes.
273 *
274 * \return A sufficient output buffer size for any of the
275 * supported key types and AEAD algorithms.
276 *
277 */
278#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
279 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
280
281
Antonio de Angelis377a1552018-11-22 17:02:40 +0000282/** The maximum size of the output of psa_aead_decrypt(), in bytes.
283 *
284 * If the size of the plaintext buffer is at least this large, it is
285 * guaranteed that psa_aead_decrypt() will not fail due to an
286 * insufficient buffer size. Depending on the algorithm, the actual size of
287 * the plaintext may be smaller.
288 *
Summer Qin359167d2021-07-05 18:11:50 +0800289 * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
290 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100291 * \warning This macro may evaluate its arguments multiple times or
292 * zero times, so you should not pass arguments that contain
293 * side effects.
294 *
Summer Qin359167d2021-07-05 18:11:50 +0800295 * \param key_type A symmetric key type that is
296 * compatible with algorithm \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000297 * \param alg An AEAD algorithm
298 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100299 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000300 * \param ciphertext_length Size of the plaintext in bytes.
301 *
302 * \return The AEAD ciphertext size for the specified
303 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800304 * If the key type or AEAD algorithm is not
305 * recognized, or the parameters are incompatible,
306 * return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000307 */
Summer Qin359167d2021-07-05 18:11:50 +0800308#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
309 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
310 (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
311 (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100312 0)
313
Maulik Patel13b27cf2021-05-14 11:44:53 +0100314/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
315 * supported key types and AEAD algorithms.
316 *
317 * If the size of the plaintext buffer is at least this large, it is guaranteed
318 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
319 *
320 * \note This macro returns a compile-time constant if its arguments are
321 * compile-time constants.
322 *
Summer Qin359167d2021-07-05 18:11:50 +0800323 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
324 * \p ciphertext_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100325 *
326 * \param ciphertext_length Size of the ciphertext in bytes.
327 *
328 * \return A sufficient output buffer size for any of the
329 * supported key types and AEAD algorithms.
330 *
331 */
332#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
333 (ciphertext_length)
334
335/** The default nonce size for an AEAD algorithm, in bytes.
336 *
337 * This macro can be used to allocate a buffer of sufficient size to
338 * store the nonce output from #psa_aead_generate_nonce().
339 *
340 * See also #PSA_AEAD_NONCE_MAX_SIZE.
341 *
342 * \note This is not the maximum size of nonce supported as input to
343 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
344 * just the default size that is generated by #psa_aead_generate_nonce().
345 *
346 * \warning This macro may evaluate its arguments multiple times or
347 * zero times, so you should not pass arguments that contain
348 * side effects.
349 *
350 * \param key_type A symmetric key type that is compatible with
351 * algorithm \p alg.
352 *
353 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
354 * #PSA_ALG_IS_AEAD(\p alg) is true).
355 *
356 * \return The default nonce size for the specified key type and algorithm.
357 * If the key type or AEAD algorithm is not recognized,
358 * or the parameters are incompatible, return 0.
359 */
360#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
Summer Qin359167d2021-07-05 18:11:50 +0800361 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
362 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
363 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
364 0 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100365 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
Summer Qin359167d2021-07-05 18:11:50 +0800366 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100367 0)
368
369/** The maximum default nonce size among all supported pairs of key types and
370 * AEAD algorithms, in bytes.
371 *
372 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
373 * may return.
374 *
375 * \note This is not the maximum size of nonce supported as input to
376 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
377 * just the largest size that may be generated by
378 * #psa_aead_generate_nonce().
379 */
Summer Qin359167d2021-07-05 18:11:50 +0800380#define PSA_AEAD_NONCE_MAX_SIZE 13
Maulik Patel13b27cf2021-05-14 11:44:53 +0100381
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100382/** A sufficient output buffer size for psa_aead_update().
383 *
384 * If the size of the output buffer is at least this large, it is
385 * guaranteed that psa_aead_update() will not fail due to an
386 * insufficient buffer size. The actual size of the output may be smaller
387 * in any given call.
388 *
Summer Qin359167d2021-07-05 18:11:50 +0800389 * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
390 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100391 * \warning This macro may evaluate its arguments multiple times or
392 * zero times, so you should not pass arguments that contain
393 * side effects.
394 *
Summer Qin359167d2021-07-05 18:11:50 +0800395 * \param key_type A symmetric key type that is
396 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100397 * \param alg An AEAD algorithm
398 * (\c PSA_ALG_XXX value such that
399 * #PSA_ALG_IS_AEAD(\p alg) is true).
400 * \param input_length Size of the input in bytes.
401 *
402 * \return A sufficient output buffer size for the specified
403 * algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800404 * If the key type or AEAD algorithm is not
405 * recognized, or the parameters are incompatible,
406 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100407 */
408/* For all the AEAD modes defined in this specification, it is possible
409 * to emit output without delay. However, hardware may not always be
410 * capable of this. So for modes based on a block cipher, allow the
411 * implementation to delay the output until it has a full block. */
Summer Qin359167d2021-07-05 18:11:50 +0800412#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
413 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
414 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
415 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
416 (input_length) : \
417 0)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100418
Maulik Patel13b27cf2021-05-14 11:44:53 +0100419/** A sufficient output buffer size for psa_aead_update(), for any of the
420 * supported key types and AEAD algorithms.
421 *
422 * If the size of the output buffer is at least this large, it is guaranteed
423 * that psa_aead_update() will not fail due to an insufficient buffer size.
424 *
Summer Qin359167d2021-07-05 18:11:50 +0800425 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100426 *
427 * \param input_length Size of the input in bytes.
428 */
429#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
430 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
431
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100432/** A sufficient ciphertext buffer size for psa_aead_finish().
433 *
434 * If the size of the ciphertext buffer is at least this large, it is
435 * guaranteed that psa_aead_finish() will not fail due to an
436 * insufficient ciphertext buffer size. The actual size of the output may
437 * be smaller in any given call.
438 *
Summer Qin359167d2021-07-05 18:11:50 +0800439 * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
440 *
441 * \param key_type A symmetric key type that is
442 compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100443 * \param alg An AEAD algorithm
444 * (\c PSA_ALG_XXX value such that
445 * #PSA_ALG_IS_AEAD(\p alg) is true).
446 *
447 * \return A sufficient ciphertext buffer size for the
448 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800449 * If the key type or AEAD algorithm is not
450 * recognized, or the parameters are incompatible,
451 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100452 */
Summer Qin359167d2021-07-05 18:11:50 +0800453#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
454 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
455 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
456 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100457 0)
458
Maulik Patel13b27cf2021-05-14 11:44:53 +0100459/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
460 * supported key types and AEAD algorithms.
461 *
Summer Qin359167d2021-07-05 18:11:50 +0800462 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100463 */
464#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
465
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100466/** A sufficient plaintext buffer size for psa_aead_verify().
467 *
468 * If the size of the plaintext buffer is at least this large, it is
469 * guaranteed that psa_aead_verify() will not fail due to an
470 * insufficient plaintext buffer size. The actual size of the output may
471 * be smaller in any given call.
472 *
Summer Qin359167d2021-07-05 18:11:50 +0800473 * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
474 *
475 * \param key_type A symmetric key type that is
476 * compatible with algorithm \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100477 * \param alg An AEAD algorithm
478 * (\c PSA_ALG_XXX value such that
479 * #PSA_ALG_IS_AEAD(\p alg) is true).
480 *
481 * \return A sufficient plaintext buffer size for the
482 * specified algorithm.
Summer Qin359167d2021-07-05 18:11:50 +0800483 * If the key type or AEAD algorithm is not
484 * recognized, or the parameters are incompatible,
485 * return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100486 */
Summer Qin359167d2021-07-05 18:11:50 +0800487#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
488 (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
489 PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
490 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000491 0)
492
Maulik Patel13b27cf2021-05-14 11:44:53 +0100493/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
494 * supported key types and AEAD algorithms.
495 *
Summer Qin359167d2021-07-05 18:11:50 +0800496 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
Maulik Patel13b27cf2021-05-14 11:44:53 +0100497 */
498#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
499
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100500#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
501 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100502 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100503 11 /*PKCS#1v1.5*/)
504
505/**
506 * \brief ECDSA signature size for a given curve bit size
507 *
508 * \param curve_bits Curve size in bits.
509 * \return Signature size in bytes.
510 *
511 * \note This macro returns a compile-time constant if its argument is one.
512 */
513#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
514 (PSA_BITS_TO_BYTES(curve_bits) * 2)
515
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100516/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000517 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100518 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000519 * of the specified type and size, with the specified algorithm.
520 * Note that the actual size of the signature may be smaller
521 * (some algorithms produce a variable-size signature).
522 *
523 * \warning This function may call its arguments multiple times or
524 * zero times, so you should not pass arguments that contain
525 * side effects.
526 *
527 * \param key_type An asymmetric key type (this may indifferently be a
528 * key pair type or a public key type).
529 * \param key_bits The size of the key in bits.
530 * \param alg The signature algorithm.
531 *
532 * \return If the parameters are valid and supported, return
533 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100534 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000535 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100536 * If the parameters are a valid combination that is not supported,
537 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000538 * If the parameters are not valid, the
539 * return value is unspecified.
540 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100541#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000542 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
543 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
544 ((void)alg, 0))
545
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100546#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
547 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
548
549/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000550 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100551 * Maximum size of an asymmetric signature.
552 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100553 * This macro expands to a compile-time constant integer. This value
554 * is the maximum size of a signature in bytes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100555 */
556#define PSA_SIGNATURE_MAX_SIZE \
557 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
558 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
559 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
560
561/** Sufficient output buffer size for psa_asymmetric_encrypt().
562 *
563 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000564 * a key of the specified type and size, with the specified algorithm.
565 * Note that the actual size of the ciphertext may be smaller, depending
566 * on the algorithm.
567 *
568 * \warning This function may call its arguments multiple times or
569 * zero times, so you should not pass arguments that contain
570 * side effects.
571 *
572 * \param key_type An asymmetric key type (this may indifferently be a
573 * key pair type or a public key type).
574 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100575 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000576 *
577 * \return If the parameters are valid and supported, return
578 * a buffer size in bytes that guarantees that
579 * psa_asymmetric_encrypt() will not fail with
580 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100581 * If the parameters are a valid combination that is not supported,
582 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000583 * If the parameters are not valid, the
584 * return value is unspecified.
585 */
586#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
587 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
588 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
589 0)
590
Maulik Patel13b27cf2021-05-14 11:44:53 +0100591/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
592 * supported asymmetric encryption.
593 *
594 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
595 */
596/* This macro assumes that RSA is the only supported asymmetric encryption. */
597#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
598 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
599
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100600/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000601 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100602 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000603 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100604 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000605 * on the algorithm.
606 *
607 * \warning This function may call its arguments multiple times or
608 * zero times, so you should not pass arguments that contain
609 * side effects.
610 *
611 * \param key_type An asymmetric key type (this may indifferently be a
612 * key pair type or a public key type).
613 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100614 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000615 *
616 * \return If the parameters are valid and supported, return
617 * a buffer size in bytes that guarantees that
618 * psa_asymmetric_decrypt() will not fail with
619 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100620 * If the parameters are a valid combination that is not supported,
621 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000622 * If the parameters are not valid, the
623 * return value is unspecified.
624 */
625#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
626 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
627 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
628 0)
629
Maulik Patel13b27cf2021-05-14 11:44:53 +0100630/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
631 * supported asymmetric decryption.
632 *
633 * This macro assumes that RSA is the only supported asymmetric encryption.
634 *
635 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
636 */
637#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
638 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
639
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100640/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
641 * number of bits.
642 *
643 * This definition assumes that bits <= 2^19 - 9 so that the length field
644 * is at most 3 bytes. The length of the encoding is the length of the
645 * bit string padded to a whole number of bytes plus:
646 * - 1 type byte;
647 * - 1 to 3 length bytes;
648 * - 0 to 1 bytes of leading 0 due to the sign bit.
649 */
650#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
651 ((bits) / 8 + 5)
652
653/* Maximum size of the export encoding of an RSA public key.
654 * Assumes that the public exponent is less than 2^32.
655 *
656 * RSAPublicKey ::= SEQUENCE {
657 * modulus INTEGER, -- n
658 * publicExponent INTEGER } -- e
659 *
660 * - 4 bytes of SEQUENCE overhead;
661 * - n : INTEGER;
662 * - 7 bytes for the public exponent.
663 */
664#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
665 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
666
667/* Maximum size of the export encoding of an RSA key pair.
668 * Assumes thatthe public exponent is less than 2^32 and that the size
669 * difference between the two primes is at most 1 bit.
670 *
671 * RSAPrivateKey ::= SEQUENCE {
672 * version Version, -- 0
673 * modulus INTEGER, -- N-bit
674 * publicExponent INTEGER, -- 32-bit
675 * privateExponent INTEGER, -- N-bit
676 * prime1 INTEGER, -- N/2-bit
677 * prime2 INTEGER, -- N/2-bit
678 * exponent1 INTEGER, -- N/2-bit
679 * exponent2 INTEGER, -- N/2-bit
680 * coefficient INTEGER, -- N/2-bit
681 * }
682 *
683 * - 4 bytes of SEQUENCE overhead;
684 * - 3 bytes of version;
685 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
686 * overapproximated as 9 half-size INTEGERS;
687 * - 7 bytes for the public exponent.
688 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100689#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100690 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
691
692/* Maximum size of the export encoding of a DSA public key.
693 *
694 * SubjectPublicKeyInfo ::= SEQUENCE {
695 * algorithm AlgorithmIdentifier,
696 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
697 * AlgorithmIdentifier ::= SEQUENCE {
698 * algorithm OBJECT IDENTIFIER,
699 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
700 * DSAPublicKey ::= INTEGER -- public key, Y
701 *
702 * - 3 * 4 bytes of SEQUENCE overhead;
703 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
704 * - 4 bytes of BIT STRING overhead;
705 * - 3 full-size INTEGERs (p, g, y);
706 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
707 */
708#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
709 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
710
711/* Maximum size of the export encoding of a DSA key pair.
712 *
713 * DSAPrivateKey ::= SEQUENCE {
714 * version Version, -- 0
715 * prime INTEGER, -- p
716 * subprime INTEGER, -- q
717 * generator INTEGER, -- g
718 * public INTEGER, -- y
719 * private INTEGER, -- x
720 * }
721 *
722 * - 4 bytes of SEQUENCE overhead;
723 * - 3 bytes of version;
724 * - 3 full-size INTEGERs (p, g, y);
725 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
726 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100727#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100728 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
729
730/* Maximum size of the export encoding of an ECC public key.
731 *
732 * The representation of an ECC public key is:
733 * - The byte 0x04;
734 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
735 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
736 * - where m is the bit size associated with the curve.
737 *
738 * - 1 byte + 2 * point size.
739 */
740#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
741 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
742
743/* Maximum size of the export encoding of an ECC key pair.
744 *
745 * An ECC key pair is represented by the secret value.
746 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100747#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100748 (PSA_BITS_TO_BYTES(key_bits))
749
Maulik Patel13b27cf2021-05-14 11:44:53 +0100750/** Sufficient output buffer size for psa_export_key() or
751 * psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100752 *
753 * This macro returns a compile-time constant if its arguments are
754 * compile-time constants.
755 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100756 * \warning This macro may evaluate its arguments multiple times or
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100757 * zero times, so you should not pass arguments that contain
758 * side effects.
759 *
760 * The following code illustrates how to allocate enough memory to export
761 * a key by querying the key type and size at runtime.
762 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100763 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100764 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100765 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100766 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100767 * psa_key_type_t key_type = psa_get_key_type(&attributes);
768 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100769 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100770 * psa_reset_key_attributes(&attributes);
771 * uint8_t *buffer = malloc(buffer_size);
772 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100773 * size_t buffer_length;
774 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
775 * if (status != PSA_SUCCESS) handle_error(...);
776 * \endcode
777 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100778 * \param key_type A supported key type.
779 * \param key_bits The size of the key in bits.
780 *
781 * \return If the parameters are valid and supported, return
782 * a buffer size in bytes that guarantees that
783 * psa_export_key() or psa_export_public_key() will not fail with
784 * #PSA_ERROR_BUFFER_TOO_SMALL.
785 * If the parameters are a valid combination that is not supported,
786 * return either a sensible size or 0.
787 * If the parameters are not valid, the return value is unspecified.
788 */
789#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
790 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
791 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
792 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
793 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
794 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
795 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
796 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
797 0)
798
799/** Sufficient output buffer size for psa_export_public_key().
800 *
801 * This macro returns a compile-time constant if its arguments are
802 * compile-time constants.
803 *
804 * \warning This macro may evaluate its arguments multiple times or
805 * zero times, so you should not pass arguments that contain
806 * side effects.
807 *
808 * The following code illustrates how to allocate enough memory to export
809 * a public key by querying the key type and size at runtime.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100810 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100811 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100812 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100813 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100814 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100815 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100816 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100817 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100818 * psa_reset_key_attributes(&attributes);
819 * uint8_t *buffer = malloc(buffer_size);
820 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100821 * size_t buffer_length;
822 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
823 * if (status != PSA_SUCCESS) handle_error(...);
824 * \endcode
825 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100826 * \param key_type A public key or key pair key type.
827 * \param key_bits The size of the key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100828 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100829 * \return If the parameters are valid and supported, return
830 * a buffer size in bytes that guarantees that
831 * psa_export_public_key() will not fail with
832 * #PSA_ERROR_BUFFER_TOO_SMALL.
833 * If the parameters are a valid combination that is not
834 * supported, return either a sensible size or 0.
835 * If the parameters are not valid,
836 * the return value is unspecified.
837 *
838 * If the parameters are valid and supported,
839 * return the same result as
840 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
841 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
842 * \p key_bits).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100843 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100844#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
845 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
846 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100847 0)
848
Maulik Patel13b27cf2021-05-14 11:44:53 +0100849/** Sufficient buffer size for exporting any asymmetric key pair.
Maulik Patel28659c42021-01-06 14:09:22 +0000850 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100851 * This macro expands to a compile-time constant integer. This value is
852 * a sufficient buffer size when calling psa_export_key() to export any
853 * asymmetric key pair, regardless of the exact key type and key size.
Maulik Patel28659c42021-01-06 14:09:22 +0000854 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100855 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
856 */
857#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
858 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
859 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
860 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
861 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
862
863/** Sufficient buffer size for exporting any asymmetric public key.
Maulik Patel28659c42021-01-06 14:09:22 +0000864 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100865 * This macro expands to a compile-time constant integer. This value is
866 * a sufficient buffer size when calling psa_export_key() or
867 * psa_export_public_key() to export any asymmetric public key,
868 * regardless of the exact key type and key size.
869 *
870 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
871 */
872#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
873 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
874 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
875 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
876 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
877
878/** Sufficient output buffer size for psa_raw_key_agreement().
879 *
880 * This macro returns a compile-time constant if its arguments are
881 * compile-time constants.
Maulik Patel28659c42021-01-06 14:09:22 +0000882 *
883 * \warning This macro may evaluate its arguments multiple times or
884 * zero times, so you should not pass arguments that contain
885 * side effects.
886 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100887 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
Maulik Patel28659c42021-01-06 14:09:22 +0000888 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100889 * \param key_type A supported key type.
890 * \param key_bits The size of the key in bits.
Maulik Patel28659c42021-01-06 14:09:22 +0000891 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100892 * \return If the parameters are valid and supported, return
893 * a buffer size in bytes that guarantees that
894 * psa_raw_key_agreement() will not fail with
895 * #PSA_ERROR_BUFFER_TOO_SMALL.
896 * If the parameters are a valid combination that
897 * is not supported, return either a sensible size or 0.
898 * If the parameters are not valid,
899 * the return value is unspecified.
Maulik Patel28659c42021-01-06 14:09:22 +0000900 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100901/* FFDH is not yet supported in PSA. */
902#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
903 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
904 PSA_BITS_TO_BYTES(key_bits) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000905 0)
906
Maulik Patel13b27cf2021-05-14 11:44:53 +0100907/** Maximum size of the output from psa_raw_key_agreement().
Maulik Patel28659c42021-01-06 14:09:22 +0000908 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100909 * This macro expands to a compile-time constant integer. This value is the
910 * maximum size of the output any raw key agreement algorithm, in bytes.
Maulik Patel28659c42021-01-06 14:09:22 +0000911 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100912 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
Maulik Patel28659c42021-01-06 14:09:22 +0000913 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100914#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
915 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
Maulik Patel28659c42021-01-06 14:09:22 +0000916
917/** The default IV size for a cipher algorithm, in bytes.
918 *
919 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
920 * the default IV length for the algorithm.
921 *
922 * This macro can be used to allocate a buffer of sufficient size to
923 * store the IV output from #psa_cipher_generate_iv() when using
924 * a multi-part cipher operation.
925 *
926 * See also #PSA_CIPHER_IV_MAX_SIZE.
927 *
928 * \warning This macro may evaluate its arguments multiple times or
929 * zero times, so you should not pass arguments that contain
930 * side effects.
931 *
932 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
933 *
934 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
935 * #PSA_ALG_IS_CIPHER(\p alg) is true).
936 *
937 * \return The default IV size for the specified key type and algorithm.
938 * If the algorithm does not use an IV, return 0.
939 * If the key type or cipher algorithm is not recognized,
940 * or the parameters are incompatible, return 0.
Maulik Patel28659c42021-01-06 14:09:22 +0000941 */
942#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100943 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Maulik Patel28659c42021-01-06 14:09:22 +0000944 ((alg) == PSA_ALG_CTR || \
945 (alg) == PSA_ALG_CFB || \
946 (alg) == PSA_ALG_OFB || \
947 (alg) == PSA_ALG_XTS || \
948 (alg) == PSA_ALG_CBC_NO_PADDING || \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100949 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000950 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
951 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
Summer Qinf07cc312022-01-05 16:52:54 +0800952 (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
953 0)
Maulik Patel28659c42021-01-06 14:09:22 +0000954
955/** The maximum IV size for all supported cipher algorithms, in bytes.
956 *
957 * See also #PSA_CIPHER_IV_LENGTH().
958 */
959#define PSA_CIPHER_IV_MAX_SIZE 16
960
Maulik Patel13b27cf2021-05-14 11:44:53 +0100961/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
962 *
963 * If the size of the output buffer is at least this large, it is guaranteed
964 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
965 * Depending on the algorithm, the actual size of the output might be smaller.
966 *
967 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
968 *
969 * \warning This macro may evaluate its arguments multiple times or
970 * zero times, so you should not pass arguments that contain
971 * side effects.
972 *
973 * \param key_type A symmetric key type that is compatible with algorithm
974 * alg.
975 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
976 * #PSA_ALG_IS_CIPHER(\p alg) is true).
977 * \param input_length Size of the input in bytes.
978 *
979 * \return A sufficient output size for the specified key type and
980 * algorithm. If the key type or cipher algorithm is not
981 * recognized, or the parameters are incompatible,
982 * return 0.
983 */
984#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
985 (alg == PSA_ALG_CBC_PKCS7 ? \
Summer Qinf07cc312022-01-05 16:52:54 +0800986 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100987 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
988 (input_length) + 1) + \
Summer Qinf07cc312022-01-05 16:52:54 +0800989 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100990 (PSA_ALG_IS_CIPHER(alg) ? \
991 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
992 0))
993
994/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
995 * supported key types and cipher algorithms.
996 *
997 * If the size of the output buffer is at least this large, it is guaranteed
998 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
999 *
1000 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1001 *
1002 * \param input_length Size of the input in bytes.
1003 *
1004 */
1005#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1006 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1007 (input_length) + 1) + \
1008 PSA_CIPHER_IV_MAX_SIZE)
1009
1010/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
1011 *
1012 * If the size of the output buffer is at least this large, it is guaranteed
1013 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1014 * Depending on the algorithm, the actual size of the output might be smaller.
1015 *
1016 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
1017 *
1018 * \param key_type A symmetric key type that is compatible with algorithm
1019 * alg.
1020 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
1021 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1022 * \param input_length Size of the input in bytes.
1023 *
1024 * \return A sufficient output size for the specified key type and
1025 * algorithm. If the key type or cipher algorithm is not
1026 * recognized, or the parameters are incompatible,
1027 * return 0.
1028 */
1029#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1030 (PSA_ALG_IS_CIPHER(alg) && \
1031 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1032 (input_length) : \
1033 0)
1034
1035/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
1036 * supported key types and cipher algorithms.
1037 *
1038 * If the size of the output buffer is at least this large, it is guaranteed
1039 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
1040 *
1041 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1042 *
1043 * \param input_length Size of the input in bytes.
1044 */
1045#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1046 (input_length)
1047
1048/** A sufficient output buffer size for psa_cipher_update().
1049 *
1050 * If the size of the output buffer is at least this large, it is guaranteed
1051 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1052 * The actual size of the output might be smaller in any given call.
1053 *
1054 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
1055 *
1056 * \param key_type A symmetric key type that is compatible with algorithm
1057 * alg.
1058 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1059 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1060 * \param input_length Size of the input in bytes.
1061 *
1062 * \return A sufficient output size for the specified key type and
1063 * algorithm. If the key type or cipher algorithm is not
1064 * recognized, or the parameters are incompatible, return 0.
1065 */
1066#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1067 (PSA_ALG_IS_CIPHER(alg) ? \
Summer Qinf07cc312022-01-05 16:52:54 +08001068 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001069 (((alg) == PSA_ALG_CBC_PKCS7 || \
1070 (alg) == PSA_ALG_CBC_NO_PADDING || \
1071 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1072 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1073 input_length) : \
Summer Qinf07cc312022-01-05 16:52:54 +08001074 (input_length)) : 0) : \
Maulik Patel13b27cf2021-05-14 11:44:53 +01001075 0)
1076
1077/** A sufficient output buffer size for psa_cipher_update(), for any of the
1078 * supported key types and cipher algorithms.
1079 *
1080 * If the size of the output buffer is at least this large, it is guaranteed
1081 * that psa_cipher_update() will not fail due to an insufficient buffer size.
1082 *
1083 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1084 *
1085 * \param input_length Size of the input in bytes.
1086 */
1087#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1088 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1089
1090/** A sufficient ciphertext buffer size for psa_cipher_finish().
1091 *
1092 * If the size of the ciphertext buffer is at least this large, it is
1093 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1094 * ciphertext buffer size. The actual size of the output might be smaller in
1095 * any given call.
1096 *
1097 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1098 *
1099 * \param key_type A symmetric key type that is compatible with algorithm
1100 * alg.
1101 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1102 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1103 * \return A sufficient output size for the specified key type and
1104 * algorithm. If the key type or cipher algorithm is not
1105 * recognized, or the parameters are incompatible, return 0.
1106 */
1107#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1108 (PSA_ALG_IS_CIPHER(alg) ? \
1109 (alg == PSA_ALG_CBC_PKCS7 ? \
1110 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1111 0) : \
1112 0)
1113
1114/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1115 * supported key types and cipher algorithms.
1116 *
1117 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1118 */
1119#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1120 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1121
Antonio de Angelis377a1552018-11-22 17:02:40 +00001122#endif /* PSA_CRYPTO_SIZES_H */