blob: 0608d71d10e1892be21f88dc13986902ea6818e9 [file] [log] [blame]
Antonio de Angelis377a1552018-11-22 17:02:40 +00001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, 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_MD2 ? 16 : \
54 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
55 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
56 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
57 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
58 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
59 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
60 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
61 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
62 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
66 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
67 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
68 0)
69
Antonio de Angelis377a1552018-11-22 17:02:40 +000070/** \def PSA_HASH_MAX_SIZE
71 *
72 * Maximum size of a hash.
73 *
Maulik Patel13b27cf2021-05-14 11:44:53 +010074 * This macro expands to a compile-time constant integer. This value
75 * is the maximum size of a hash in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +000076 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010077/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
78 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
79 * HMAC-SHA3-512. */
Antonio de Angelis377a1552018-11-22 17:02:40 +000080#define PSA_HASH_MAX_SIZE 64
81#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
82
83/** \def PSA_MAC_MAX_SIZE
84 *
85 * Maximum size of a MAC.
86 *
Maulik Patel13b27cf2021-05-14 11:44:53 +010087 * This macro expands to a compile-time constant integer. This value
88 * is the maximum size of a MAC in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +000089 */
90/* All non-HMAC MACs have a maximum size that's smaller than the
91 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010092/* Note that the encoding of truncated MAC algorithms limits this value
93 * to 64 bytes.
94 */
Antonio de Angelis377a1552018-11-22 17:02:40 +000095#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
96
Jamie Fox0e54ebc2019-04-09 14:21:04 +010097/** The tag size for an AEAD algorithm, in bytes.
98 *
99 * \param alg An AEAD algorithm
100 * (\c PSA_ALG_XXX value such that
101 * #PSA_ALG_IS_AEAD(\p alg) is true).
102 *
103 * \return The tag size for the specified algorithm.
104 * If the AEAD algorithm does not have an identified
105 * tag that can be distinguished from the rest of
106 * the ciphertext, return 0.
107 * If the AEAD algorithm is not recognized, return 0.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100108 */
109#define PSA_AEAD_TAG_LENGTH(alg) \
110 (PSA_ALG_IS_AEAD(alg) ? \
111 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
112 0)
113
Maulik Patel13b27cf2021-05-14 11:44:53 +0100114/** The maximum tag size for all supported AEAD algorithms, in bytes.
115 *
116 * See also #PSA_AEAD_TAG_LENGTH(\p alg).
117 */
118#define PSA_AEAD_TAG_MAX_SIZE 16
119
Antonio de Angelis377a1552018-11-22 17:02:40 +0000120/* The maximum size of an RSA key on this implementation, in bits.
121 * This is a vendor-specific macro.
122 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100123 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000124 * whose parameters fit in a bignum is accepted. However large keys can
125 * induce a large memory usage and long computation times. Unlike other
126 * auxiliary macros in this file and in crypto.h, which reflect how the
127 * library is configured, this macro defines how the library is
128 * configured. This implementation refuses to import or generate an
129 * RSA key whose size is larger than the value defined here.
130 *
131 * Note that an implementation may set different size limits for different
132 * operations, and does not need to accept all key sizes up to the limit. */
133#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
134
Soby Mathew07ef6e42020-07-20 21:09:23 +0100135/* The maximum size of an ECC key on this implementation, in bits */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100136#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
137
Maulik Patel13b27cf2021-05-14 11:44:53 +0100138/** This macro returns the maximum supported length of the PSK for the
139 * TLS-1.2 PSK-to-MS key derivation
140 * (#PSA_ALG_TLS12_PSK_TO_MS(\p hash_alg)).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100141 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100142 * The maximum supported length does not depend on the chosen hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100143 *
144 * Quoting RFC 4279, Sect 5.3:
145 * TLS implementations supporting these ciphersuites MUST support
146 * arbitrary PSK identities up to 128 octets in length, and arbitrary
147 * PSKs up to 64 octets in length. Supporting longer identities and
148 * keys is RECOMMENDED.
149 *
150 * Therefore, no implementation should define a value smaller than 64
Maulik Patel13b27cf2021-05-14 11:44:53 +0100151 * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100152 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100153#define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000154
Maulik Patel13b27cf2021-05-14 11:44:53 +0100155/** The maximum size of a block cipher. */
156#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
157
Antonio de Angelis377a1552018-11-22 17:02:40 +0000158
159/** The size of the output of psa_mac_sign_finish(), in bytes.
160 *
161 * This is also the MAC size that psa_mac_verify_finish() expects.
162 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100163 * \warning This macro may evaluate its arguments multiple times or
164 * zero times, so you should not pass arguments that contain
165 * side effects.
166 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000167 * \param key_type The type of the MAC key.
168 * \param key_bits The size of the MAC key in bits.
169 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100170 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000171 *
172 * \return The MAC size for the specified algorithm with
173 * the specified key parameters.
174 * \return 0 if the MAC algorithm is not recognized.
175 * \return Either 0 or the correct size for a MAC algorithm that
176 * the implementation recognizes, but does not support.
177 * \return Unspecified if the key parameters are not consistent
178 * with the algorithm.
179 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100180#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
181 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
182 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
183 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100184 ((void)(key_type), (void)(key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000185
186/** The maximum size of the output of psa_aead_encrypt(), in bytes.
187 *
188 * If the size of the ciphertext buffer is at least this large, it is
189 * guaranteed that psa_aead_encrypt() will not fail due to an
190 * insufficient buffer size. Depending on the algorithm, the actual size of
191 * the ciphertext may be smaller.
192 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100193 * \warning This macro may evaluate its arguments multiple times or
194 * zero times, so you should not pass arguments that contain
195 * side effects.
196 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000197 * \param alg An AEAD algorithm
198 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100199 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000200 * \param plaintext_length Size of the plaintext in bytes.
201 *
202 * \return The AEAD ciphertext size for the specified
203 * algorithm.
204 * If the AEAD algorithm is not recognized, return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000205 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100206#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
207 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
208 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000209 0)
210
Maulik Patel13b27cf2021-05-14 11:44:53 +0100211/** A sufficient output buffer size for psa_aead_encrypt(), for any of the
212 * supported key types and AEAD algorithms.
213 *
214 * If the size of the ciphertext buffer is at least this large, it is guaranteed
215 * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
216 *
217 * \note This macro returns a compile-time constant if its arguments are
218 * compile-time constants.
219 *
220 * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, \p plaintext_length).
221 *
222 * \param plaintext_length Size of the plaintext in bytes.
223 *
224 * \return A sufficient output buffer size for any of the
225 * supported key types and AEAD algorithms.
226 *
227 */
228#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
229 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
230
231
Antonio de Angelis377a1552018-11-22 17:02:40 +0000232/** The maximum size of the output of psa_aead_decrypt(), in bytes.
233 *
234 * If the size of the plaintext buffer is at least this large, it is
235 * guaranteed that psa_aead_decrypt() will not fail due to an
236 * insufficient buffer size. Depending on the algorithm, the actual size of
237 * the plaintext may be smaller.
238 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100239 * \warning This macro may evaluate its arguments multiple times or
240 * zero times, so you should not pass arguments that contain
241 * side effects.
242 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000243 * \param alg An AEAD algorithm
244 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100245 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000246 * \param ciphertext_length Size of the plaintext in bytes.
247 *
248 * \return The AEAD ciphertext size for the specified
249 * algorithm.
250 * If the AEAD algorithm is not recognized, return 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000251 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100252#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
253 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100254 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
255 0)
256
Maulik Patel13b27cf2021-05-14 11:44:53 +0100257/** A sufficient output buffer size for psa_aead_decrypt(), for any of the
258 * supported key types and AEAD algorithms.
259 *
260 * If the size of the plaintext buffer is at least this large, it is guaranteed
261 * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
262 *
263 * \note This macro returns a compile-time constant if its arguments are
264 * compile-time constants.
265 *
266 * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, \p ciphertext_length).
267 *
268 * \param ciphertext_length Size of the ciphertext in bytes.
269 *
270 * \return A sufficient output buffer size for any of the
271 * supported key types and AEAD algorithms.
272 *
273 */
274#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
275 (ciphertext_length)
276
277/** The default nonce size for an AEAD algorithm, in bytes.
278 *
279 * This macro can be used to allocate a buffer of sufficient size to
280 * store the nonce output from #psa_aead_generate_nonce().
281 *
282 * See also #PSA_AEAD_NONCE_MAX_SIZE.
283 *
284 * \note This is not the maximum size of nonce supported as input to
285 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
286 * just the default size that is generated by #psa_aead_generate_nonce().
287 *
288 * \warning This macro may evaluate its arguments multiple times or
289 * zero times, so you should not pass arguments that contain
290 * side effects.
291 *
292 * \param key_type A symmetric key type that is compatible with
293 * algorithm \p alg.
294 *
295 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
296 * #PSA_ALG_IS_AEAD(\p alg) is true).
297 *
298 * \return The default nonce size for the specified key type and algorithm.
299 * If the key type or AEAD algorithm is not recognized,
300 * or the parameters are incompatible, return 0.
301 */
302#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
303 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 && \
304 (PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM || \
305 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_GCM) ? 12 : \
306 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
307 PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \
308 0)
309
310/** The maximum default nonce size among all supported pairs of key types and
311 * AEAD algorithms, in bytes.
312 *
313 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
314 * may return.
315 *
316 * \note This is not the maximum size of nonce supported as input to
317 * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
318 * just the largest size that may be generated by
319 * #psa_aead_generate_nonce().
320 */
321#define PSA_AEAD_NONCE_MAX_SIZE 12
322
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100323/** A sufficient output buffer size for psa_aead_update().
324 *
325 * If the size of the output buffer is at least this large, it is
326 * guaranteed that psa_aead_update() will not fail due to an
327 * insufficient buffer size. The actual size of the output may be smaller
328 * in any given call.
329 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100330 * \warning This macro may evaluate its arguments multiple times or
331 * zero times, so you should not pass arguments that contain
332 * side effects.
333 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100334 * \param alg An AEAD algorithm
335 * (\c PSA_ALG_XXX value such that
336 * #PSA_ALG_IS_AEAD(\p alg) is true).
337 * \param input_length Size of the input in bytes.
338 *
339 * \return A sufficient output buffer size for the specified
340 * algorithm.
341 * If the AEAD algorithm is not recognized, return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100342 */
343/* For all the AEAD modes defined in this specification, it is possible
344 * to emit output without delay. However, hardware may not always be
345 * capable of this. So for modes based on a block cipher, allow the
346 * implementation to delay the output until it has a full block. */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100347#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
348 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
349 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100350 (input_length))
351
Maulik Patel13b27cf2021-05-14 11:44:53 +0100352/** A sufficient output buffer size for psa_aead_update(), for any of the
353 * supported key types and AEAD algorithms.
354 *
355 * If the size of the output buffer is at least this large, it is guaranteed
356 * that psa_aead_update() will not fail due to an insufficient buffer size.
357 *
358 * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p alg, \p input_length).
359 *
360 * \param input_length Size of the input in bytes.
361 */
362#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
363 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
364
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100365/** A sufficient ciphertext buffer size for psa_aead_finish().
366 *
367 * If the size of the ciphertext buffer is at least this large, it is
368 * guaranteed that psa_aead_finish() will not fail due to an
369 * insufficient ciphertext buffer size. The actual size of the output may
370 * be smaller in any given call.
371 *
372 * \param alg An AEAD algorithm
373 * (\c PSA_ALG_XXX value such that
374 * #PSA_ALG_IS_AEAD(\p alg) is true).
375 *
376 * \return A sufficient ciphertext buffer size for the
377 * specified algorithm.
378 * If the AEAD algorithm is not recognized, return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100379 */
380#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
381 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100382 PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100383 0)
384
Maulik Patel13b27cf2021-05-14 11:44:53 +0100385/** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
386 * supported key types and AEAD algorithms.
387 *
388 * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p alg).
389 */
390#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
391
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100392/** A sufficient plaintext buffer size for psa_aead_verify().
393 *
394 * If the size of the plaintext buffer is at least this large, it is
395 * guaranteed that psa_aead_verify() will not fail due to an
396 * insufficient plaintext buffer size. The actual size of the output may
397 * be smaller in any given call.
398 *
399 * \param alg An AEAD algorithm
400 * (\c PSA_ALG_XXX value such that
401 * #PSA_ALG_IS_AEAD(\p alg) is true).
402 *
403 * \return A sufficient plaintext buffer size for the
404 * specified algorithm.
405 * If the AEAD algorithm is not recognized, return 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100406 */
407#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
408 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100409 PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000410 0)
411
Maulik Patel13b27cf2021-05-14 11:44:53 +0100412/** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
413 * supported key types and AEAD algorithms.
414 *
415 * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p alg).
416 */
417#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
418
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100419#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
420 (PSA_ALG_IS_RSA_OAEP(alg) ? \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100421 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100422 11 /*PKCS#1v1.5*/)
423
424/**
425 * \brief ECDSA signature size for a given curve bit size
426 *
427 * \param curve_bits Curve size in bits.
428 * \return Signature size in bytes.
429 *
430 * \note This macro returns a compile-time constant if its argument is one.
431 */
432#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
433 (PSA_BITS_TO_BYTES(curve_bits) * 2)
434
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100435/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000436 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100437 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000438 * of the specified type and size, with the specified algorithm.
439 * Note that the actual size of the signature may be smaller
440 * (some algorithms produce a variable-size signature).
441 *
442 * \warning This function may call its arguments multiple times or
443 * zero times, so you should not pass arguments that contain
444 * side effects.
445 *
446 * \param key_type An asymmetric key type (this may indifferently be a
447 * key pair type or a public key type).
448 * \param key_bits The size of the key in bits.
449 * \param alg The signature algorithm.
450 *
451 * \return If the parameters are valid and supported, return
452 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100453 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000454 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100455 * If the parameters are a valid combination that is not supported,
456 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000457 * If the parameters are not valid, the
458 * return value is unspecified.
459 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100460#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000461 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
462 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
463 ((void)alg, 0))
464
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100465#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
466 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
467
468/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000469 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100470 * Maximum size of an asymmetric signature.
471 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100472 * This macro expands to a compile-time constant integer. This value
473 * is the maximum size of a signature in bytes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100474 */
475#define PSA_SIGNATURE_MAX_SIZE \
476 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
477 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
478 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
479
480/** Sufficient output buffer size for psa_asymmetric_encrypt().
481 *
482 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000483 * a key of the specified type and size, with the specified algorithm.
484 * Note that the actual size of the ciphertext may be smaller, depending
485 * on the algorithm.
486 *
487 * \warning This function may call its arguments multiple times or
488 * zero times, so you should not pass arguments that contain
489 * side effects.
490 *
491 * \param key_type An asymmetric key type (this may indifferently be a
492 * key pair type or a public key type).
493 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100494 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000495 *
496 * \return If the parameters are valid and supported, return
497 * a buffer size in bytes that guarantees that
498 * psa_asymmetric_encrypt() will not fail with
499 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100500 * If the parameters are a valid combination that is not supported,
501 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000502 * If the parameters are not valid, the
503 * return value is unspecified.
504 */
505#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
506 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
507 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
508 0)
509
Maulik Patel13b27cf2021-05-14 11:44:53 +0100510/** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
511 * supported asymmetric encryption.
512 *
513 * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
514 */
515/* This macro assumes that RSA is the only supported asymmetric encryption. */
516#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
517 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
518
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000520 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100521 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000522 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100523 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000524 * on the algorithm.
525 *
526 * \warning This function may call its arguments multiple times or
527 * zero times, so you should not pass arguments that contain
528 * side effects.
529 *
530 * \param key_type An asymmetric key type (this may indifferently be a
531 * key pair type or a public key type).
532 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100533 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000534 *
535 * \return If the parameters are valid and supported, return
536 * a buffer size in bytes that guarantees that
537 * psa_asymmetric_decrypt() will not fail with
538 * #PSA_ERROR_BUFFER_TOO_SMALL.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100539 * If the parameters are a valid combination that is not supported,
540 * return either a sensible size or 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000541 * If the parameters are not valid, the
542 * return value is unspecified.
543 */
544#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
545 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
546 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
547 0)
548
Maulik Patel13b27cf2021-05-14 11:44:53 +0100549/** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
550 * supported asymmetric decryption.
551 *
552 * This macro assumes that RSA is the only supported asymmetric encryption.
553 *
554 * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
555 */
556#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
557 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
558
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100559/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
560 * number of bits.
561 *
562 * This definition assumes that bits <= 2^19 - 9 so that the length field
563 * is at most 3 bytes. The length of the encoding is the length of the
564 * bit string padded to a whole number of bytes plus:
565 * - 1 type byte;
566 * - 1 to 3 length bytes;
567 * - 0 to 1 bytes of leading 0 due to the sign bit.
568 */
569#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
570 ((bits) / 8 + 5)
571
572/* Maximum size of the export encoding of an RSA public key.
573 * Assumes that the public exponent is less than 2^32.
574 *
575 * RSAPublicKey ::= SEQUENCE {
576 * modulus INTEGER, -- n
577 * publicExponent INTEGER } -- e
578 *
579 * - 4 bytes of SEQUENCE overhead;
580 * - n : INTEGER;
581 * - 7 bytes for the public exponent.
582 */
583#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
584 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
585
586/* Maximum size of the export encoding of an RSA key pair.
587 * Assumes thatthe public exponent is less than 2^32 and that the size
588 * difference between the two primes is at most 1 bit.
589 *
590 * RSAPrivateKey ::= SEQUENCE {
591 * version Version, -- 0
592 * modulus INTEGER, -- N-bit
593 * publicExponent INTEGER, -- 32-bit
594 * privateExponent INTEGER, -- N-bit
595 * prime1 INTEGER, -- N/2-bit
596 * prime2 INTEGER, -- N/2-bit
597 * exponent1 INTEGER, -- N/2-bit
598 * exponent2 INTEGER, -- N/2-bit
599 * coefficient INTEGER, -- N/2-bit
600 * }
601 *
602 * - 4 bytes of SEQUENCE overhead;
603 * - 3 bytes of version;
604 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
605 * overapproximated as 9 half-size INTEGERS;
606 * - 7 bytes for the public exponent.
607 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100608#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100609 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
610
611/* Maximum size of the export encoding of a DSA public key.
612 *
613 * SubjectPublicKeyInfo ::= SEQUENCE {
614 * algorithm AlgorithmIdentifier,
615 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
616 * AlgorithmIdentifier ::= SEQUENCE {
617 * algorithm OBJECT IDENTIFIER,
618 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
619 * DSAPublicKey ::= INTEGER -- public key, Y
620 *
621 * - 3 * 4 bytes of SEQUENCE overhead;
622 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
623 * - 4 bytes of BIT STRING overhead;
624 * - 3 full-size INTEGERs (p, g, y);
625 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
626 */
627#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
628 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
629
630/* Maximum size of the export encoding of a DSA key pair.
631 *
632 * DSAPrivateKey ::= SEQUENCE {
633 * version Version, -- 0
634 * prime INTEGER, -- p
635 * subprime INTEGER, -- q
636 * generator INTEGER, -- g
637 * public INTEGER, -- y
638 * private INTEGER, -- x
639 * }
640 *
641 * - 4 bytes of SEQUENCE overhead;
642 * - 3 bytes of version;
643 * - 3 full-size INTEGERs (p, g, y);
644 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
645 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100646#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100647 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
648
649/* Maximum size of the export encoding of an ECC public key.
650 *
651 * The representation of an ECC public key is:
652 * - The byte 0x04;
653 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
654 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
655 * - where m is the bit size associated with the curve.
656 *
657 * - 1 byte + 2 * point size.
658 */
659#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
660 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
661
662/* Maximum size of the export encoding of an ECC key pair.
663 *
664 * An ECC key pair is represented by the secret value.
665 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100666#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100667 (PSA_BITS_TO_BYTES(key_bits))
668
Maulik Patel13b27cf2021-05-14 11:44:53 +0100669/** Sufficient output buffer size for psa_export_key() or
670 * psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100671 *
672 * This macro returns a compile-time constant if its arguments are
673 * compile-time constants.
674 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100675 * \warning This macro may evaluate its arguments multiple times or
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100676 * zero times, so you should not pass arguments that contain
677 * side effects.
678 *
679 * The following code illustrates how to allocate enough memory to export
680 * a key by querying the key type and size at runtime.
681 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100682 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100683 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100684 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100685 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100686 * psa_key_type_t key_type = psa_get_key_type(&attributes);
687 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100688 * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100689 * psa_reset_key_attributes(&attributes);
690 * uint8_t *buffer = malloc(buffer_size);
691 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100692 * size_t buffer_length;
693 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
694 * if (status != PSA_SUCCESS) handle_error(...);
695 * \endcode
696 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100697 * \param key_type A supported key type.
698 * \param key_bits The size of the key in bits.
699 *
700 * \return If the parameters are valid and supported, return
701 * a buffer size in bytes that guarantees that
702 * psa_export_key() or psa_export_public_key() will not fail with
703 * #PSA_ERROR_BUFFER_TOO_SMALL.
704 * If the parameters are a valid combination that is not supported,
705 * return either a sensible size or 0.
706 * If the parameters are not valid, the return value is unspecified.
707 */
708#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
709 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
710 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
711 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
712 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
713 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
714 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
715 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
716 0)
717
718/** Sufficient output buffer size for psa_export_public_key().
719 *
720 * This macro returns a compile-time constant if its arguments are
721 * compile-time constants.
722 *
723 * \warning This macro may evaluate its arguments multiple times or
724 * zero times, so you should not pass arguments that contain
725 * side effects.
726 *
727 * The following code illustrates how to allocate enough memory to export
728 * a public key by querying the key type and size at runtime.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100729 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100730 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100731 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100732 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100733 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100734 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100735 * size_t key_bits = psa_get_key_bits(&attributes);
Maulik Patel13b27cf2021-05-14 11:44:53 +0100736 * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100737 * psa_reset_key_attributes(&attributes);
738 * uint8_t *buffer = malloc(buffer_size);
739 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100740 * size_t buffer_length;
741 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
742 * if (status != PSA_SUCCESS) handle_error(...);
743 * \endcode
744 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100745 * \param key_type A public key or key pair key type.
746 * \param key_bits The size of the key in bits.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100747 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100748 * \return If the parameters are valid and supported, return
749 * a buffer size in bytes that guarantees that
750 * psa_export_public_key() will not fail with
751 * #PSA_ERROR_BUFFER_TOO_SMALL.
752 * If the parameters are a valid combination that is not
753 * supported, return either a sensible size or 0.
754 * If the parameters are not valid,
755 * the return value is unspecified.
756 *
757 * If the parameters are valid and supported,
758 * return the same result as
759 * #PSA_EXPORT_KEY_OUTPUT_SIZE(
760 * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
761 * \p key_bits).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100762 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100763#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
764 (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
765 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100766 0)
767
Maulik Patel13b27cf2021-05-14 11:44:53 +0100768/** Sufficient buffer size for exporting any asymmetric key pair.
Maulik Patel28659c42021-01-06 14:09:22 +0000769 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100770 * This macro expands to a compile-time constant integer. This value is
771 * a sufficient buffer size when calling psa_export_key() to export any
772 * asymmetric key pair, regardless of the exact key type and key size.
Maulik Patel28659c42021-01-06 14:09:22 +0000773 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100774 * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
775 */
776#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
777 (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
778 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
779 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
780 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
781
782/** Sufficient buffer size for exporting any asymmetric public key.
Maulik Patel28659c42021-01-06 14:09:22 +0000783 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100784 * This macro expands to a compile-time constant integer. This value is
785 * a sufficient buffer size when calling psa_export_key() or
786 * psa_export_public_key() to export any asymmetric public key,
787 * regardless of the exact key type and key size.
788 *
789 * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
790 */
791#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
792 (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
793 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
794 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
795 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
796
797/** Sufficient output buffer size for psa_raw_key_agreement().
798 *
799 * This macro returns a compile-time constant if its arguments are
800 * compile-time constants.
Maulik Patel28659c42021-01-06 14:09:22 +0000801 *
802 * \warning This macro may evaluate its arguments multiple times or
803 * zero times, so you should not pass arguments that contain
804 * side effects.
805 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100806 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
Maulik Patel28659c42021-01-06 14:09:22 +0000807 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100808 * \param key_type A supported key type.
809 * \param key_bits The size of the key in bits.
Maulik Patel28659c42021-01-06 14:09:22 +0000810 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100811 * \return If the parameters are valid and supported, return
812 * a buffer size in bytes that guarantees that
813 * psa_raw_key_agreement() will not fail with
814 * #PSA_ERROR_BUFFER_TOO_SMALL.
815 * If the parameters are a valid combination that
816 * is not supported, return either a sensible size or 0.
817 * If the parameters are not valid,
818 * the return value is unspecified.
Maulik Patel28659c42021-01-06 14:09:22 +0000819 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100820/* FFDH is not yet supported in PSA. */
821#define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
822 (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
823 PSA_BITS_TO_BYTES(key_bits) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000824 0)
825
Maulik Patel13b27cf2021-05-14 11:44:53 +0100826/** Maximum size of the output from psa_raw_key_agreement().
Maulik Patel28659c42021-01-06 14:09:22 +0000827 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100828 * This macro expands to a compile-time constant integer. This value is the
829 * maximum size of the output any raw key agreement algorithm, in bytes.
Maulik Patel28659c42021-01-06 14:09:22 +0000830 *
Maulik Patel13b27cf2021-05-14 11:44:53 +0100831 * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
Maulik Patel28659c42021-01-06 14:09:22 +0000832 */
Maulik Patel13b27cf2021-05-14 11:44:53 +0100833#define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
834 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
Maulik Patel28659c42021-01-06 14:09:22 +0000835
836/** The default IV size for a cipher algorithm, in bytes.
837 *
838 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
839 * the default IV length for the algorithm.
840 *
841 * This macro can be used to allocate a buffer of sufficient size to
842 * store the IV output from #psa_cipher_generate_iv() when using
843 * a multi-part cipher operation.
844 *
845 * See also #PSA_CIPHER_IV_MAX_SIZE.
846 *
847 * \warning This macro may evaluate its arguments multiple times or
848 * zero times, so you should not pass arguments that contain
849 * side effects.
850 *
851 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
852 *
853 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
854 * #PSA_ALG_IS_CIPHER(\p alg) is true).
855 *
856 * \return The default IV size for the specified key type and algorithm.
857 * If the algorithm does not use an IV, return 0.
858 * If the key type or cipher algorithm is not recognized,
859 * or the parameters are incompatible, return 0.
Maulik Patel28659c42021-01-06 14:09:22 +0000860 */
861#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100862 (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
Maulik Patel28659c42021-01-06 14:09:22 +0000863 ((alg) == PSA_ALG_CTR || \
864 (alg) == PSA_ALG_CFB || \
865 (alg) == PSA_ALG_OFB || \
866 (alg) == PSA_ALG_XTS || \
867 (alg) == PSA_ALG_CBC_NO_PADDING || \
Maulik Patel13b27cf2021-05-14 11:44:53 +0100868 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
Maulik Patel28659c42021-01-06 14:09:22 +0000869 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
870 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
871 0)
872
873/** The maximum IV size for all supported cipher algorithms, in bytes.
874 *
875 * See also #PSA_CIPHER_IV_LENGTH().
876 */
877#define PSA_CIPHER_IV_MAX_SIZE 16
878
Maulik Patel13b27cf2021-05-14 11:44:53 +0100879/** The maximum size of the output of psa_cipher_encrypt(), in bytes.
880 *
881 * If the size of the output buffer is at least this large, it is guaranteed
882 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
883 * Depending on the algorithm, the actual size of the output might be smaller.
884 *
885 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
886 *
887 * \warning This macro may evaluate its arguments multiple times or
888 * zero times, so you should not pass arguments that contain
889 * side effects.
890 *
891 * \param key_type A symmetric key type that is compatible with algorithm
892 * alg.
893 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
894 * #PSA_ALG_IS_CIPHER(\p alg) is true).
895 * \param input_length Size of the input in bytes.
896 *
897 * \return A sufficient output size for the specified key type and
898 * algorithm. If the key type or cipher algorithm is not
899 * recognized, or the parameters are incompatible,
900 * return 0.
901 */
902#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
903 (alg == PSA_ALG_CBC_PKCS7 ? \
904 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
905 (input_length) + 1) + \
906 PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
907 (PSA_ALG_IS_CIPHER(alg) ? \
908 (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
909 0))
910
911/** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
912 * supported key types and cipher algorithms.
913 *
914 * If the size of the output buffer is at least this large, it is guaranteed
915 * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
916 *
917 * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
918 *
919 * \param input_length Size of the input in bytes.
920 *
921 */
922#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
923 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
924 (input_length) + 1) + \
925 PSA_CIPHER_IV_MAX_SIZE)
926
927/** The maximum size of the output of psa_cipher_decrypt(), in bytes.
928 *
929 * If the size of the output buffer is at least this large, it is guaranteed
930 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
931 * Depending on the algorithm, the actual size of the output might be smaller.
932 *
933 * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
934 *
935 * \param key_type A symmetric key type that is compatible with algorithm
936 * alg.
937 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
938 * #PSA_ALG_IS_CIPHER(\p alg) is true).
939 * \param input_length Size of the input in bytes.
940 *
941 * \return A sufficient output size for the specified key type and
942 * algorithm. If the key type or cipher algorithm is not
943 * recognized, or the parameters are incompatible,
944 * return 0.
945 */
946#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
947 (PSA_ALG_IS_CIPHER(alg) && \
948 ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
949 (input_length) : \
950 0)
951
952/** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
953 * supported key types and cipher algorithms.
954 *
955 * If the size of the output buffer is at least this large, it is guaranteed
956 * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
957 *
958 * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
959 *
960 * \param input_length Size of the input in bytes.
961 */
962#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
963 (input_length)
964
965/** A sufficient output buffer size for psa_cipher_update().
966 *
967 * If the size of the output buffer is at least this large, it is guaranteed
968 * that psa_cipher_update() will not fail due to an insufficient buffer size.
969 * The actual size of the output might be smaller in any given call.
970 *
971 * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
972 *
973 * \param key_type A symmetric key type that is compatible with algorithm
974 * alg.
975 * \param alg A cipher algorithm (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, return 0.
982 */
983#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
984 (PSA_ALG_IS_CIPHER(alg) ? \
985 (((alg) == PSA_ALG_CBC_PKCS7 || \
986 (alg) == PSA_ALG_CBC_NO_PADDING || \
987 (alg) == PSA_ALG_ECB_NO_PADDING) ? \
988 PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
989 input_length) : \
990 (input_length)) : \
991 0)
992
993/** A sufficient output buffer size for psa_cipher_update(), for any of the
994 * supported key types and cipher algorithms.
995 *
996 * If the size of the output buffer is at least this large, it is guaranteed
997 * that psa_cipher_update() will not fail due to an insufficient buffer size.
998 *
999 * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
1000 *
1001 * \param input_length Size of the input in bytes.
1002 */
1003#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1004 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1005
1006/** A sufficient ciphertext buffer size for psa_cipher_finish().
1007 *
1008 * If the size of the ciphertext buffer is at least this large, it is
1009 * guaranteed that psa_cipher_finish() will not fail due to an insufficient
1010 * ciphertext buffer size. The actual size of the output might be smaller in
1011 * any given call.
1012 *
1013 * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
1014 *
1015 * \param key_type A symmetric key type that is compatible with algorithm
1016 * alg.
1017 * \param alg A cipher algorithm (PSA_ALG_XXX value such that
1018 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1019 * \return A sufficient output size for the specified key type and
1020 * algorithm. If the key type or cipher algorithm is not
1021 * recognized, or the parameters are incompatible, return 0.
1022 */
1023#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1024 (PSA_ALG_IS_CIPHER(alg) ? \
1025 (alg == PSA_ALG_CBC_PKCS7 ? \
1026 PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1027 0) : \
1028 0)
1029
1030/** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
1031 * supported key types and cipher algorithms.
1032 *
1033 * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
1034 */
1035#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1036 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1037
Antonio de Angelis377a1552018-11-22 17:02:40 +00001038#endif /* PSA_CRYPTO_SIZES_H */