blob: 4d13e412af621c4bcdae65999ca589c943f602b8 [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.
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
Soby Mathew07ef6e42020-07-20 21:09:23 +0100136/* The maximum size of an ECC key on this implementation, in bits */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100137#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
138
139/** \def PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN
140 *
141 * This macro returns the maximum length of the PSK supported
142 * by the TLS-1.2 PSK-to-MS key derivation.
143 *
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
151 * for #PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN.
152 */
153#define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128
Antonio de Angelis377a1552018-11-22 17:02:40 +0000154
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100155/** The maximum size of a block cipher supported by the implementation. */
156#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
Antonio de Angelis377a1552018-11-22 17:02:40 +0000157
158/** The size of the output of psa_mac_sign_finish(), in bytes.
159 *
160 * This is also the MAC size that psa_mac_verify_finish() expects.
161 *
162 * \param key_type The type of the MAC key.
163 * \param key_bits The size of the MAC key in bits.
164 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100165 * #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000166 *
167 * \return The MAC size for the specified algorithm with
168 * the specified key parameters.
169 * \return 0 if the MAC algorithm is not recognized.
170 * \return Either 0 or the correct size for a MAC algorithm that
171 * the implementation recognizes, but does not support.
172 * \return Unspecified if the key parameters are not consistent
173 * with the algorithm.
174 */
175#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100176 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
177 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000178 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100179 ((void)(key_type), (void)(key_bits), 0))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000180
181/** The maximum size of the output of psa_aead_encrypt(), in bytes.
182 *
183 * If the size of the ciphertext buffer is at least this large, it is
184 * guaranteed that psa_aead_encrypt() will not fail due to an
185 * insufficient buffer size. Depending on the algorithm, the actual size of
186 * the ciphertext may be smaller.
187 *
188 * \param alg An AEAD algorithm
189 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100190 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000191 * \param plaintext_length Size of the plaintext in bytes.
192 *
193 * \return The AEAD ciphertext size for the specified
194 * algorithm.
195 * If the AEAD algorithm is not recognized, return 0.
196 * An implementation may return either 0 or a
197 * correct size for an AEAD algorithm that it
198 * recognizes, but does not support.
199 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100200#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
201 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
202 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000203 0)
204
205/** The maximum size of the output of psa_aead_decrypt(), in bytes.
206 *
207 * If the size of the plaintext buffer is at least this large, it is
208 * guaranteed that psa_aead_decrypt() will not fail due to an
209 * insufficient buffer size. Depending on the algorithm, the actual size of
210 * the plaintext may be smaller.
211 *
212 * \param alg An AEAD algorithm
213 * (\c PSA_ALG_XXX value such that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100214 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000215 * \param ciphertext_length Size of the plaintext in bytes.
216 *
217 * \return The AEAD ciphertext size for the specified
218 * algorithm.
219 * If the AEAD algorithm is not recognized, return 0.
220 * An implementation may return either 0 or a
221 * correct size for an AEAD algorithm that it
222 * recognizes, but does not support.
223 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100224#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
225 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100226 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
227 0)
228
229/** A sufficient output buffer size for psa_aead_update().
230 *
231 * If the size of the output buffer is at least this large, it is
232 * guaranteed that psa_aead_update() will not fail due to an
233 * insufficient buffer size. The actual size of the output may be smaller
234 * in any given call.
235 *
236 * \param alg An AEAD algorithm
237 * (\c PSA_ALG_XXX value such that
238 * #PSA_ALG_IS_AEAD(\p alg) is true).
239 * \param input_length Size of the input in bytes.
240 *
241 * \return A sufficient output buffer size for the specified
242 * algorithm.
243 * If the AEAD algorithm is not recognized, return 0.
244 * An implementation may return either 0 or a
245 * correct size for an AEAD algorithm that it
246 * recognizes, but does not support.
247 */
248/* For all the AEAD modes defined in this specification, it is possible
249 * to emit output without delay. However, hardware may not always be
250 * capable of this. So for modes based on a block cipher, allow the
251 * implementation to delay the output until it has a full block. */
252#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
253 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
254 PSA_ROUND_UP_TO_MULTIPLE(PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE, (input_length)) : \
255 (input_length))
256
257/** A sufficient ciphertext buffer size for psa_aead_finish().
258 *
259 * If the size of the ciphertext buffer is at least this large, it is
260 * guaranteed that psa_aead_finish() will not fail due to an
261 * insufficient ciphertext buffer size. The actual size of the output may
262 * be smaller in any given call.
263 *
264 * \param alg An AEAD algorithm
265 * (\c PSA_ALG_XXX value such that
266 * #PSA_ALG_IS_AEAD(\p alg) is true).
267 *
268 * \return A sufficient ciphertext buffer size for the
269 * specified algorithm.
270 * If the AEAD algorithm is not recognized, return 0.
271 * An implementation may return either 0 or a
272 * correct size for an AEAD algorithm that it
273 * recognizes, but does not support.
274 */
275#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
276 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
277 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
278 0)
279
280/** A sufficient plaintext buffer size for psa_aead_verify().
281 *
282 * If the size of the plaintext buffer is at least this large, it is
283 * guaranteed that psa_aead_verify() will not fail due to an
284 * insufficient plaintext buffer size. The actual size of the output may
285 * be smaller in any given call.
286 *
287 * \param alg An AEAD algorithm
288 * (\c PSA_ALG_XXX value such that
289 * #PSA_ALG_IS_AEAD(\p alg) is true).
290 *
291 * \return A sufficient plaintext buffer size for the
292 * specified algorithm.
293 * If the AEAD algorithm is not recognized, return 0.
294 * An implementation may return either 0 or a
295 * correct size for an AEAD algorithm that it
296 * recognizes, but does not support.
297 */
298#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
299 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
300 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000301 0)
302
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100303#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
304 (PSA_ALG_IS_RSA_OAEP(alg) ? \
305 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
306 11 /*PKCS#1v1.5*/)
307
308/**
309 * \brief ECDSA signature size for a given curve bit size
310 *
311 * \param curve_bits Curve size in bits.
312 * \return Signature size in bytes.
313 *
314 * \note This macro returns a compile-time constant if its argument is one.
315 */
316#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
317 (PSA_BITS_TO_BYTES(curve_bits) * 2)
318
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100319/** Sufficient signature buffer size for psa_sign_hash().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000320 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100321 * This macro returns a sufficient buffer size for a signature using a key
Antonio de Angelis377a1552018-11-22 17:02:40 +0000322 * of the specified type and size, with the specified algorithm.
323 * Note that the actual size of the signature may be smaller
324 * (some algorithms produce a variable-size signature).
325 *
326 * \warning This function may call its arguments multiple times or
327 * zero times, so you should not pass arguments that contain
328 * side effects.
329 *
330 * \param key_type An asymmetric key type (this may indifferently be a
331 * key pair type or a public key type).
332 * \param key_bits The size of the key in bits.
333 * \param alg The signature algorithm.
334 *
335 * \return If the parameters are valid and supported, return
336 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100337 * psa_sign_hash() will not fail with
Antonio de Angelis377a1552018-11-22 17:02:40 +0000338 * #PSA_ERROR_BUFFER_TOO_SMALL.
339 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100340 * by the implementation, this macro shall return either a
Antonio de Angelis377a1552018-11-22 17:02:40 +0000341 * sensible size or 0.
342 * If the parameters are not valid, the
343 * return value is unspecified.
344 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100345#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000346 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
347 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
348 ((void)alg, 0))
349
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100350#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
351 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
352
353/** \def PSA_SIGNATURE_MAX_SIZE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000354 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100355 * Maximum size of an asymmetric signature.
356 *
357 * This macro must expand to a compile-time constant integer. This value
358 * should be the maximum size of a signature supported by the implementation,
359 * in bytes, and must be no smaller than this maximum.
360 */
361#define PSA_SIGNATURE_MAX_SIZE \
362 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
363 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
364 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
365
366/** Sufficient output buffer size for psa_asymmetric_encrypt().
367 *
368 * This macro returns a sufficient buffer size for a ciphertext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000369 * a key of the specified type and size, with the specified algorithm.
370 * Note that the actual size of the ciphertext may be smaller, depending
371 * on the algorithm.
372 *
373 * \warning This function may call its arguments multiple times or
374 * zero times, so you should not pass arguments that contain
375 * side effects.
376 *
377 * \param key_type An asymmetric key type (this may indifferently be a
378 * key pair type or a public key type).
379 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100380 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000381 *
382 * \return If the parameters are valid and supported, return
383 * a buffer size in bytes that guarantees that
384 * psa_asymmetric_encrypt() will not fail with
385 * #PSA_ERROR_BUFFER_TOO_SMALL.
386 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100387 * by the implementation, this macro shall return either a
Antonio de Angelis377a1552018-11-22 17:02:40 +0000388 * sensible size or 0.
389 * If the parameters are not valid, the
390 * return value is unspecified.
391 */
392#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
393 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
394 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
395 0)
396
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100397/** Sufficient output buffer size for psa_asymmetric_decrypt().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000398 *
Soby Mathew07ef6e42020-07-20 21:09:23 +0100399 * This macro returns a sufficient buffer size for a plaintext produced using
Antonio de Angelis377a1552018-11-22 17:02:40 +0000400 * a key of the specified type and size, with the specified algorithm.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100401 * Note that the actual size of the plaintext may be smaller, depending
Antonio de Angelis377a1552018-11-22 17:02:40 +0000402 * on the algorithm.
403 *
404 * \warning This function may call its arguments multiple times or
405 * zero times, so you should not pass arguments that contain
406 * side effects.
407 *
408 * \param key_type An asymmetric key type (this may indifferently be a
409 * key pair type or a public key type).
410 * \param key_bits The size of the key in bits.
Soby Mathew07ef6e42020-07-20 21:09:23 +0100411 * \param alg The asymmetric encryption algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000412 *
413 * \return If the parameters are valid and supported, return
414 * a buffer size in bytes that guarantees that
415 * psa_asymmetric_decrypt() will not fail with
416 * #PSA_ERROR_BUFFER_TOO_SMALL.
417 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100418 * by the implementation, this macro shall return either a
Antonio de Angelis377a1552018-11-22 17:02:40 +0000419 * sensible size or 0.
420 * If the parameters are not valid, the
421 * return value is unspecified.
422 */
423#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
424 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
425 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
426 0)
427
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100428/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
429 * number of bits.
430 *
431 * This definition assumes that bits <= 2^19 - 9 so that the length field
432 * is at most 3 bytes. The length of the encoding is the length of the
433 * bit string padded to a whole number of bytes plus:
434 * - 1 type byte;
435 * - 1 to 3 length bytes;
436 * - 0 to 1 bytes of leading 0 due to the sign bit.
437 */
438#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
439 ((bits) / 8 + 5)
440
441/* Maximum size of the export encoding of an RSA public key.
442 * Assumes that the public exponent is less than 2^32.
443 *
444 * RSAPublicKey ::= SEQUENCE {
445 * modulus INTEGER, -- n
446 * publicExponent INTEGER } -- e
447 *
448 * - 4 bytes of SEQUENCE overhead;
449 * - n : INTEGER;
450 * - 7 bytes for the public exponent.
451 */
452#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
453 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
454
455/* Maximum size of the export encoding of an RSA key pair.
456 * Assumes thatthe public exponent is less than 2^32 and that the size
457 * difference between the two primes is at most 1 bit.
458 *
459 * RSAPrivateKey ::= SEQUENCE {
460 * version Version, -- 0
461 * modulus INTEGER, -- N-bit
462 * publicExponent INTEGER, -- 32-bit
463 * privateExponent INTEGER, -- N-bit
464 * prime1 INTEGER, -- N/2-bit
465 * prime2 INTEGER, -- N/2-bit
466 * exponent1 INTEGER, -- N/2-bit
467 * exponent2 INTEGER, -- N/2-bit
468 * coefficient INTEGER, -- N/2-bit
469 * }
470 *
471 * - 4 bytes of SEQUENCE overhead;
472 * - 3 bytes of version;
473 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
474 * overapproximated as 9 half-size INTEGERS;
475 * - 7 bytes for the public exponent.
476 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100477#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100478 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
479
480/* Maximum size of the export encoding of a DSA public key.
481 *
482 * SubjectPublicKeyInfo ::= SEQUENCE {
483 * algorithm AlgorithmIdentifier,
484 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
485 * AlgorithmIdentifier ::= SEQUENCE {
486 * algorithm OBJECT IDENTIFIER,
487 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
488 * DSAPublicKey ::= INTEGER -- public key, Y
489 *
490 * - 3 * 4 bytes of SEQUENCE overhead;
491 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
492 * - 4 bytes of BIT STRING overhead;
493 * - 3 full-size INTEGERs (p, g, y);
494 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
495 */
496#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
497 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
498
499/* Maximum size of the export encoding of a DSA key pair.
500 *
501 * DSAPrivateKey ::= SEQUENCE {
502 * version Version, -- 0
503 * prime INTEGER, -- p
504 * subprime INTEGER, -- q
505 * generator INTEGER, -- g
506 * public INTEGER, -- y
507 * private INTEGER, -- x
508 * }
509 *
510 * - 4 bytes of SEQUENCE overhead;
511 * - 3 bytes of version;
512 * - 3 full-size INTEGERs (p, g, y);
513 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
514 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100515#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100516 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
517
518/* Maximum size of the export encoding of an ECC public key.
519 *
520 * The representation of an ECC public key is:
521 * - The byte 0x04;
522 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
523 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
524 * - where m is the bit size associated with the curve.
525 *
526 * - 1 byte + 2 * point size.
527 */
528#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
529 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
530
531/* Maximum size of the export encoding of an ECC key pair.
532 *
533 * An ECC key pair is represented by the secret value.
534 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100535#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100536 (PSA_BITS_TO_BYTES(key_bits))
537
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100538/** Sufficient output buffer size for psa_export_key() or psa_export_public_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100539 *
540 * This macro returns a compile-time constant if its arguments are
541 * compile-time constants.
542 *
543 * \warning This function may call its arguments multiple times or
544 * zero times, so you should not pass arguments that contain
545 * side effects.
546 *
547 * The following code illustrates how to allocate enough memory to export
548 * a key by querying the key type and size at runtime.
549 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100550 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100551 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100552 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100553 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100554 * psa_key_type_t key_type = psa_get_key_type(&attributes);
555 * size_t key_bits = psa_get_key_bits(&attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100556 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100557 * psa_reset_key_attributes(&attributes);
558 * uint8_t *buffer = malloc(buffer_size);
559 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100560 * size_t buffer_length;
561 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
562 * if (status != PSA_SUCCESS) handle_error(...);
563 * \endcode
564 *
565 * For psa_export_public_key(), calculate the buffer size from the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100566 * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100567 * to convert a key pair type to the corresponding public key type.
568 * \code{c}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100569 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100570 * psa_status_t status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100571 * status = psa_get_key_attributes(key, &attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100572 * if (status != PSA_SUCCESS) handle_error(...);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100573 * psa_key_type_t key_type = psa_get_key_type(&attributes);
574 * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
575 * size_t key_bits = psa_get_key_bits(&attributes);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100576 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100577 * psa_reset_key_attributes(&attributes);
578 * uint8_t *buffer = malloc(buffer_size);
579 * if (buffer == NULL) handle_error(...);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100580 * size_t buffer_length;
581 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
582 * if (status != PSA_SUCCESS) handle_error(...);
583 * \endcode
584 *
585 * \param key_type A supported key type.
586 * \param key_bits The size of the key in bits.
587 *
588 * \return If the parameters are valid and supported, return
589 * a buffer size in bytes that guarantees that
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100590 * psa_sign_hash() will not fail with
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100591 * #PSA_ERROR_BUFFER_TOO_SMALL.
592 * If the parameters are a valid combination that is not supported
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100593 * by the implementation, this macro shall return either a
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100594 * sensible size or 0.
595 * If the parameters are not valid, the
596 * return value is unspecified.
597 */
598#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
599 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100600 (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 +0100601 (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 +0100602 (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 +0100603 (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 +0100604 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 +0100605 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
606 0)
607
Maulik Patel28659c42021-01-06 14:09:22 +0000608/** The default nonce size for an AEAD algorithm, in bytes.
609 *
610 * This macro can be used to allocate a buffer of sufficient size to
611 * store the nonce output from #psa_aead_generate_nonce().
612 *
613 * See also #PSA_AEAD_NONCE_MAX_SIZE.
614 *
615 * \note This is not the maximum size of nonce supported as input to #psa_aead_set_nonce(),
616 * #psa_aead_encrypt() or #psa_aead_decrypt(), just the default size that is generated by
617 * #psa_aead_generate_nonce().
618 *
619 * \warning This macro may evaluate its arguments multiple times or
620 * zero times, so you should not pass arguments that contain
621 * side effects.
622 *
623 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
624 *
625 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
626 * #PSA_ALG_IS_AEAD(\p alg) is true).
627 *
628 * \return The default nonce size for the specified key type and algorithm.
629 * If the key type or AEAD algorithm is not recognized,
630 * or the parameters are incompatible, return 0.
631 * An implementation can return either 0 or a correct size for a key type
632 * and AEAD algorithm that it recognizes, but does not support.
633 */
634#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
635 (PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) == 16 && \
636 (PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_CCM || \
637 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_GCM) ? 12 : \
638 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
639 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \
640 0)
641
642/** The maximum default nonce size among all supported pairs of key types and
643 * AEAD algorithms, in bytes.
644 *
645 * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH() may return.
646 *
647 * \note This is not the maximum size of nonce supported as input to #psa_aead_set_nonce(),
648 * #psa_aead_encrypt() or #psa_aead_decrypt(), just the largest size that may be generated by
649 * #psa_aead_generate_nonce().
650 */
651#define PSA_AEAD_NONCE_MAX_SIZE 12
652
653/** The default IV size for a cipher algorithm, in bytes.
654 *
655 * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
656 * the default IV length for the algorithm.
657 *
658 * This macro can be used to allocate a buffer of sufficient size to
659 * store the IV output from #psa_cipher_generate_iv() when using
660 * a multi-part cipher operation.
661 *
662 * See also #PSA_CIPHER_IV_MAX_SIZE.
663 *
664 * \warning This macro may evaluate its arguments multiple times or
665 * zero times, so you should not pass arguments that contain
666 * side effects.
667 *
668 * \param key_type A symmetric key type that is compatible with algorithm \p alg.
669 *
670 * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
671 * #PSA_ALG_IS_CIPHER(\p alg) is true).
672 *
673 * \return The default IV size for the specified key type and algorithm.
674 * If the algorithm does not use an IV, return 0.
675 * If the key type or cipher algorithm is not recognized,
676 * or the parameters are incompatible, return 0.
677 * An implementation can return either 0 or a correct size for a key type
678 * and cipher algorithm that it recognizes, but does not support.
679 */
680#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
681 (PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) > 1 && \
682 ((alg) == PSA_ALG_CTR || \
683 (alg) == PSA_ALG_CFB || \
684 (alg) == PSA_ALG_OFB || \
685 (alg) == PSA_ALG_XTS || \
686 (alg) == PSA_ALG_CBC_NO_PADDING || \
687 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
688 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
689 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
690 0)
691
692/** The maximum IV size for all supported cipher algorithms, in bytes.
693 *
694 * See also #PSA_CIPHER_IV_LENGTH().
695 */
696#define PSA_CIPHER_IV_MAX_SIZE 16
697
Antonio de Angelis377a1552018-11-22 17:02:40 +0000698#endif /* PSA_CRYPTO_SIZES_H */