blob: 4f67501b563248b9c78d11e9d3632fe0e1d12d9a [file] [log] [blame]
Antonio de Angelis377a1552018-11-22 17:02:40 +00001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, 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.
50 * An implementation may return either 0 or the correct size
51 * for a hash algorithm that it recognizes, but does not support.
52 */
53#define PSA_HASH_SIZE(alg) \
54 ( \
55 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
56 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
57 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
58 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
59 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
60 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
61 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
62 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
63 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
64 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
65 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
66 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
67 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
68 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
69 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
70 0)
71
Antonio de Angelis377a1552018-11-22 17:02:40 +000072/** \def PSA_HASH_MAX_SIZE
73 *
74 * Maximum size of a hash.
75 *
76 * This macro must expand to a compile-time constant integer. This value
77 * should be the maximum size of a hash supported by the implementation,
78 * in bytes, and must be no smaller than this maximum.
79 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010080/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
81 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
82 * HMAC-SHA3-512. */
Antonio de Angelis377a1552018-11-22 17:02:40 +000083#define PSA_HASH_MAX_SIZE 64
84#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
85
86/** \def PSA_MAC_MAX_SIZE
87 *
88 * Maximum size of a MAC.
89 *
90 * This macro must expand to a compile-time constant integer. This value
91 * should be the maximum size of a MAC supported by the implementation,
92 * in bytes, and must be no smaller than this maximum.
93 */
94/* All non-HMAC MACs have a maximum size that's smaller than the
95 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010096/* Note that the encoding of truncated MAC algorithms limits this value
97 * to 64 bytes.
98 */
Antonio de Angelis377a1552018-11-22 17:02:40 +000099#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
100
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100101/** The tag size for an AEAD algorithm, in bytes.
102 *
103 * \param alg An AEAD algorithm
104 * (\c PSA_ALG_XXX value such that
105 * #PSA_ALG_IS_AEAD(\p alg) is true).
106 *
107 * \return The tag size for the specified algorithm.
108 * If the AEAD algorithm does not have an identified
109 * tag that can be distinguished from the rest of
110 * the ciphertext, return 0.
111 * If the AEAD algorithm is not recognized, return 0.
112 * An implementation may return either 0 or a
113 * correct size for an AEAD algorithm that it
114 * recognizes, but does not support.
115 */
116#define PSA_AEAD_TAG_LENGTH(alg) \
117 (PSA_ALG_IS_AEAD(alg) ? \
118 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
119 0)
120
Antonio de Angelis377a1552018-11-22 17:02:40 +0000121/* The maximum size of an RSA key on this implementation, in bits.
122 * This is a vendor-specific macro.
123 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100124 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000125 * whose parameters fit in a bignum is accepted. However large keys can
126 * induce a large memory usage and long computation times. Unlike other
127 * auxiliary macros in this file and in crypto.h, which reflect how the
128 * library is configured, this macro defines how the library is
129 * configured. This implementation refuses to import or generate an
130 * RSA key whose size is larger than the value defined here.
131 *
132 * Note that an implementation may set different size limits for different
133 * operations, and does not need to accept all key sizes up to the limit. */
134#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
135
136/* The maximum size of an ECC key on this implementation, in bits.
137 * This is a vendor-specific macro. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100138#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
139
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100140/** Bit size associated with an elliptic curve.
141 *
142 * \param curve An elliptic curve (value of type #psa_ecc_curve_t).
143 *
144 * \return The size associated with \p curve, in bits.
145 * This may be 0 if the implementation does not support
146 * the specified curve.
147 */
148#define PSA_ECC_CURVE_BITS(curve) \
149 ((curve) == PSA_ECC_CURVE_SECT163K1 ? 163 : \
150 (curve) == PSA_ECC_CURVE_SECT163R1 ? 163 : \
151 (curve) == PSA_ECC_CURVE_SECT163R2 ? 163 : \
152 (curve) == PSA_ECC_CURVE_SECT193R1 ? 193 : \
153 (curve) == PSA_ECC_CURVE_SECT193R2 ? 193 : \
154 (curve) == PSA_ECC_CURVE_SECT233K1 ? 233 : \
155 (curve) == PSA_ECC_CURVE_SECT233R1 ? 233 : \
156 (curve) == PSA_ECC_CURVE_SECT239K1 ? 239 : \
157 (curve) == PSA_ECC_CURVE_SECT283K1 ? 283 : \
158 (curve) == PSA_ECC_CURVE_SECT283R1 ? 283 : \
159 (curve) == PSA_ECC_CURVE_SECT409K1 ? 409 : \
160 (curve) == PSA_ECC_CURVE_SECT409R1 ? 409 : \
161 (curve) == PSA_ECC_CURVE_SECT571K1 ? 571 : \
162 (curve) == PSA_ECC_CURVE_SECT571R1 ? 571 : \
163 (curve) == PSA_ECC_CURVE_SECP160K1 ? 160 : \
164 (curve) == PSA_ECC_CURVE_SECP160R1 ? 160 : \
165 (curve) == PSA_ECC_CURVE_SECP160R2 ? 160 : \
166 (curve) == PSA_ECC_CURVE_SECP192K1 ? 192 : \
167 (curve) == PSA_ECC_CURVE_SECP192R1 ? 192 : \
168 (curve) == PSA_ECC_CURVE_SECP224K1 ? 224 : \
169 (curve) == PSA_ECC_CURVE_SECP224R1 ? 224 : \
170 (curve) == PSA_ECC_CURVE_SECP256K1 ? 256 : \
171 (curve) == PSA_ECC_CURVE_SECP256R1 ? 256 : \
172 (curve) == PSA_ECC_CURVE_SECP384R1 ? 384 : \
173 (curve) == PSA_ECC_CURVE_SECP521R1 ? 521 : \
174 (curve) == PSA_ECC_CURVE_BRAINPOOL_P256R1 ? 256 : \
175 (curve) == PSA_ECC_CURVE_BRAINPOOL_P384R1 ? 384 : \
176 (curve) == PSA_ECC_CURVE_BRAINPOOL_P512R1 ? 512 : \
177 (curve) == PSA_ECC_CURVE_CURVE25519 ? 255 : \
178 (curve) == PSA_ECC_CURVE_CURVE448 ? 448 : \
179 0)
180
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100181/** \def PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN
182 *
183 * This macro returns the maximum length of the PSK supported
184 * by the TLS-1.2 PSK-to-MS key derivation.
185 *
186 * Quoting RFC 4279, Sect 5.3:
187 * TLS implementations supporting these ciphersuites MUST support
188 * arbitrary PSK identities up to 128 octets in length, and arbitrary
189 * PSKs up to 64 octets in length. Supporting longer identities and
190 * keys is RECOMMENDED.
191 *
192 * Therefore, no implementation should define a value smaller than 64
193 * for #PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN.
194 */
195#define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000196
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100197/** The maximum size of a block cipher supported by the implementation. */
198#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
Antonio de Angelis377a1552018-11-22 17:02:40 +0000199
200/** The size of the output of psa_mac_sign_finish(), in bytes.
201 *
202 * This is also the MAC size that psa_mac_verify_finish() expects.
203 *
204 * \param key_type The type of the MAC key.
205 * \param key_bits The size of the MAC key in bits.
206 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100207 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000208 *
209 * \return The MAC size for the specified algorithm with
210 * the specified key parameters.
211 * \return 0 if the MAC algorithm is not recognized.
212 * \return Either 0 or the correct size for a MAC algorithm that
213 * the implementation recognizes, but does not support.
214 * \return Unspecified if the key parameters are not consistent
215 * with the algorithm.
216 */
217#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100218 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
219 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000220 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100221 ((void)(key_type), (void)(key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000222
223/** The maximum size of the output of psa_aead_encrypt(), in bytes.
224 *
225 * If the size of the ciphertext buffer is at least this large, it is
226 * guaranteed that psa_aead_encrypt() will not fail due to an
227 * insufficient buffer size. Depending on the algorithm, the actual size of
228 * the ciphertext may be smaller.
229 *
230 * \param alg An AEAD algorithm
231 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100232 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000233 * \param plaintext_length Size of the plaintext in bytes.
234 *
235 * \return The AEAD ciphertext size for the specified
236 * algorithm.
237 * If the AEAD algorithm is not recognized, return 0.
238 * An implementation may return either 0 or a
239 * correct size for an AEAD algorithm that it
240 * recognizes, but does not support.
241 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100242#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
243 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
244 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000245 0)
246
247/** The maximum size of the output of psa_aead_decrypt(), in bytes.
248 *
249 * If the size of the plaintext buffer is at least this large, it is
250 * guaranteed that psa_aead_decrypt() will not fail due to an
251 * insufficient buffer size. Depending on the algorithm, the actual size of
252 * the plaintext may be smaller.
253 *
254 * \param alg An AEAD algorithm
255 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100256 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000257 * \param ciphertext_length Size of the plaintext in bytes.
258 *
259 * \return The AEAD ciphertext size for the specified
260 * algorithm.
261 * If the AEAD algorithm is not recognized, return 0.
262 * An implementation may return either 0 or a
263 * correct size for an AEAD algorithm that it
264 * recognizes, but does not support.
265 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100266#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
267 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100268 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
269 0)
270
271/** A sufficient output buffer size for psa_aead_update().
272 *
273 * If the size of the output buffer is at least this large, it is
274 * guaranteed that psa_aead_update() will not fail due to an
275 * insufficient buffer size. The actual size of the output may be smaller
276 * in any given call.
277 *
278 * \param alg An AEAD algorithm
279 * (\c PSA_ALG_XXX value such that
280 * #PSA_ALG_IS_AEAD(\p alg) is true).
281 * \param input_length Size of the input in bytes.
282 *
283 * \return A sufficient output buffer size for the specified
284 * algorithm.
285 * If the AEAD algorithm is not recognized, return 0.
286 * An implementation may return either 0 or a
287 * correct size for an AEAD algorithm that it
288 * recognizes, but does not support.
289 */
290/* For all the AEAD modes defined in this specification, it is possible
291 * to emit output without delay. However, hardware may not always be
292 * capable of this. So for modes based on a block cipher, allow the
293 * implementation to delay the output until it has a full block. */
294#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
295 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
296 PSA_ROUND_UP_TO_MULTIPLE(PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE, (input_length)) : \
297 (input_length))
298
299/** A sufficient ciphertext buffer size for psa_aead_finish().
300 *
301 * If the size of the ciphertext buffer is at least this large, it is
302 * guaranteed that psa_aead_finish() will not fail due to an
303 * insufficient ciphertext buffer size. The actual size of the output may
304 * be smaller in any given call.
305 *
306 * \param alg An AEAD algorithm
307 * (\c PSA_ALG_XXX value such that
308 * #PSA_ALG_IS_AEAD(\p alg) is true).
309 *
310 * \return A sufficient ciphertext buffer size for the
311 * specified algorithm.
312 * If the AEAD algorithm is not recognized, return 0.
313 * An implementation may return either 0 or a
314 * correct size for an AEAD algorithm that it
315 * recognizes, but does not support.
316 */
317#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
318 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
319 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
320 0)
321
322/** A sufficient plaintext buffer size for psa_aead_verify().
323 *
324 * If the size of the plaintext buffer is at least this large, it is
325 * guaranteed that psa_aead_verify() will not fail due to an
326 * insufficient plaintext buffer size. The actual size of the output may
327 * be smaller in any given call.
328 *
329 * \param alg An AEAD algorithm
330 * (\c PSA_ALG_XXX value such that
331 * #PSA_ALG_IS_AEAD(\p alg) is true).
332 *
333 * \return A sufficient plaintext buffer size for the
334 * specified algorithm.
335 * If the AEAD algorithm is not recognized, return 0.
336 * An implementation may return either 0 or a
337 * correct size for an AEAD algorithm that it
338 * recognizes, but does not support.
339 */
340#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
341 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
342 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000343 0)
344
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100345#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
346 (PSA_ALG_IS_RSA_OAEP(alg) ? \
347 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
348 11 /*PKCS#1v1.5*/)
349
350/**
351 * \brief ECDSA signature size for a given curve bit size
352 *
353 * \param curve_bits Curve size in bits.
354 * \return Signature size in bytes.
355 *
356 * \note This macro returns a compile-time constant if its argument is one.
357 */
358#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
359 (PSA_BITS_TO_BYTES(curve_bits) * 2)
360
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100361/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000362 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100363 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000364 * of the specified type and size, with the specified algorithm.
365 * Note that the actual size of the signature may be smaller
366 * (some algorithms produce a variable-size signature).
367 *
368 * \warning This function may call its arguments multiple times or
369 * zero times, so you should not pass arguments that contain
370 * side effects.
371 *
372 * \param key_type An asymmetric key type (this may indifferently be a
373 * key pair type or a public key type).
374 * \param key_bits The size of the key in bits.
375 * \param alg The signature algorithm.
376 *
377 * \return If the parameters are valid and supported, return
378 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100379 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000380 * #PSA_ERROR_BUFFER_TOO_SMALL.
381 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100382 * by the implementation, this macro shall return either a
Antonio de Angelis377a1552018-11-22 17:02:40 +0000383 * sensible size or 0.
384 * If the parameters are not valid, the
385 * return value is unspecified.
386 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100387#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000388 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
389 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
390 ((void)alg, 0))
391
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100392#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
393 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
394
395/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000396 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100397 * Maximum size of an asymmetric signature.
398 *
399 * This macro must expand to a compile-time constant integer. This value
400 * should be the maximum size of a signature supported by the implementation,
401 * in bytes, and must be no smaller than this maximum.
402 */
403#define PSA_SIGNATURE_MAX_SIZE \
404 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
405 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
406 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
407
408/** Sufficient output buffer size for psa_asymmetric_encrypt().
409 *
410 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000411 * a key of the specified type and size, with the specified algorithm.
412 * Note that the actual size of the ciphertext may be smaller, depending
413 * on the algorithm.
414 *
415 * \warning This function may call its arguments multiple times or
416 * zero times, so you should not pass arguments that contain
417 * side effects.
418 *
419 * \param key_type An asymmetric key type (this may indifferently be a
420 * key pair type or a public key type).
421 * \param key_bits The size of the key in bits.
422 * \param alg The signature algorithm.
423 *
424 * \return If the parameters are valid and supported, return
425 * a buffer size in bytes that guarantees that
426 * psa_asymmetric_encrypt() will not fail with
427 * #PSA_ERROR_BUFFER_TOO_SMALL.
428 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100429 * by the implementation, this macro shall return either a
Antonio de Angelis377a1552018-11-22 17:02:40 +0000430 * sensible size or 0.
431 * If the parameters are not valid, the
432 * return value is unspecified.
433 */
434#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
435 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
436 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
437 0)
438
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100439/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000440 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100441 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000442 * a key of the specified type and size, with the specified algorithm.
443 * Note that the actual size of the ciphertext may be smaller, depending
444 * on the algorithm.
445 *
446 * \warning This function may call its arguments multiple times or
447 * zero times, so you should not pass arguments that contain
448 * side effects.
449 *
450 * \param key_type An asymmetric key type (this may indifferently be a
451 * key pair type or a public key type).
452 * \param key_bits The size of the key in bits.
453 * \param alg The signature algorithm.
454 *
455 * \return If the parameters are valid and supported, return
456 * a buffer size in bytes that guarantees that
457 * psa_asymmetric_decrypt() will not fail with
458 * #PSA_ERROR_BUFFER_TOO_SMALL.
459 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100460 * by the implementation, this macro shall return either a
Antonio de Angelis377a1552018-11-22 17:02:40 +0000461 * sensible size or 0.
462 * If the parameters are not valid, the
463 * return value is unspecified.
464 */
465#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
466 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
467 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
468 0)
469
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100470/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
471 * number of bits.
472 *
473 * This definition assumes that bits <= 2^19 - 9 so that the length field
474 * is at most 3 bytes. The length of the encoding is the length of the
475 * bit string padded to a whole number of bytes plus:
476 * - 1 type byte;
477 * - 1 to 3 length bytes;
478 * - 0 to 1 bytes of leading 0 due to the sign bit.
479 */
480#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
481 ((bits) / 8 + 5)
482
483/* Maximum size of the export encoding of an RSA public key.
484 * Assumes that the public exponent is less than 2^32.
485 *
486 * RSAPublicKey ::= SEQUENCE {
487 * modulus INTEGER, -- n
488 * publicExponent INTEGER } -- e
489 *
490 * - 4 bytes of SEQUENCE overhead;
491 * - n : INTEGER;
492 * - 7 bytes for the public exponent.
493 */
494#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
495 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
496
497/* Maximum size of the export encoding of an RSA key pair.
498 * Assumes thatthe public exponent is less than 2^32 and that the size
499 * difference between the two primes is at most 1 bit.
500 *
501 * RSAPrivateKey ::= SEQUENCE {
502 * version Version, -- 0
503 * modulus INTEGER, -- N-bit
504 * publicExponent INTEGER, -- 32-bit
505 * privateExponent INTEGER, -- N-bit
506 * prime1 INTEGER, -- N/2-bit
507 * prime2 INTEGER, -- N/2-bit
508 * exponent1 INTEGER, -- N/2-bit
509 * exponent2 INTEGER, -- N/2-bit
510 * coefficient INTEGER, -- N/2-bit
511 * }
512 *
513 * - 4 bytes of SEQUENCE overhead;
514 * - 3 bytes of version;
515 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
516 * overapproximated as 9 half-size INTEGERS;
517 * - 7 bytes for the public exponent.
518 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100520 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
521
522/* Maximum size of the export encoding of a DSA public key.
523 *
524 * SubjectPublicKeyInfo ::= SEQUENCE {
525 * algorithm AlgorithmIdentifier,
526 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
527 * AlgorithmIdentifier ::= SEQUENCE {
528 * algorithm OBJECT IDENTIFIER,
529 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
530 * DSAPublicKey ::= INTEGER -- public key, Y
531 *
532 * - 3 * 4 bytes of SEQUENCE overhead;
533 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
534 * - 4 bytes of BIT STRING overhead;
535 * - 3 full-size INTEGERs (p, g, y);
536 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
537 */
538#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
539 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
540
541/* Maximum size of the export encoding of a DSA key pair.
542 *
543 * DSAPrivateKey ::= SEQUENCE {
544 * version Version, -- 0
545 * prime INTEGER, -- p
546 * subprime INTEGER, -- q
547 * generator INTEGER, -- g
548 * public INTEGER, -- y
549 * private INTEGER, -- x
550 * }
551 *
552 * - 4 bytes of SEQUENCE overhead;
553 * - 3 bytes of version;
554 * - 3 full-size INTEGERs (p, g, y);
555 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
556 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100557#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100558 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
559
560/* Maximum size of the export encoding of an ECC public key.
561 *
562 * The representation of an ECC public key is:
563 * - The byte 0x04;
564 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
565 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
566 * - where m is the bit size associated with the curve.
567 *
568 * - 1 byte + 2 * point size.
569 */
570#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
571 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
572
573/* Maximum size of the export encoding of an ECC key pair.
574 *
575 * An ECC key pair is represented by the secret value.
576 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100577#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100578 (PSA_BITS_TO_BYTES(key_bits))
579
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100580/** Sufficient output buffer size for psa_export_key() or psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100581 *
582 * This macro returns a compile-time constant if its arguments are
583 * compile-time constants.
584 *
585 * \warning This function may call its arguments multiple times or
586 * zero times, so you should not pass arguments that contain
587 * side effects.
588 *
589 * The following code illustrates how to allocate enough memory to export
590 * a key by querying the key type and size at runtime.
591 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100592 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100593 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100594 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100595 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100596 * psa_key_type_t key_type = psa_get_key_type(&attributes);
597 * size_t key_bits = psa_get_key_bits(&attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100598 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100599 * psa_reset_key_attributes(&attributes);
600 * uint8_t *buffer = malloc(buffer_size);
601 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100602 * size_t buffer_length;
603 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
604 * if (status != PSA_SUCCESS) handle_error(...);
605 * \endcode
606 *
607 * For psa_export_public_key(), calculate the buffer size from the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100608 * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100609 * to convert a key pair type to the corresponding public key type.
610 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100611 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100612 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100613 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100614 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100615 * psa_key_type_t key_type = psa_get_key_type(&attributes);
616 * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
617 * size_t key_bits = psa_get_key_bits(&attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100618 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100619 * psa_reset_key_attributes(&attributes);
620 * uint8_t *buffer = malloc(buffer_size);
621 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100622 * size_t buffer_length;
623 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
624 * if (status != PSA_SUCCESS) handle_error(...);
625 * \endcode
626 *
627 * \param key_type A supported key type.
628 * \param key_bits The size of the key in bits.
629 *
630 * \return If the parameters are valid and supported, return
631 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100632 * psa_sign_hash() will not fail with
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100633 * #PSA_ERROR_BUFFER_TOO_SMALL.
634 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100635 * by the implementation, this macro shall return either a
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100636 * sensible size or 0.
637 * If the parameters are not valid, the
638 * return value is unspecified.
639 */
640#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
641 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100642 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100643 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100644 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100645 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100646 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100647 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
648 0)
649
Antonio de Angelis377a1552018-11-22 17:02:40 +0000650#endif /* PSA_CRYPTO_SIZES_H */