Mistake on this page?
Report an issue in GitHub or email us
TARGET_TFM/TARGET_TFM_LATEST/include/psa/crypto_sizes.h
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 /**
8  * \file psa/crypto_sizes.h
9  *
10  * \brief PSA cryptography module: Mbed TLS buffer size macros
11  *
12  * \note This file may not be included directly. Applications must
13  * include psa/crypto.h.
14  *
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
27  * implementation are in crypto.h.
28  */
29 
30 #ifndef PSA_CRYPTO_SIZES_H
31 #define PSA_CRYPTO_SIZES_H
32 
33 #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
34 #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
35 
36 #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
37  (((length) + (block_size) - 1) / (block_size) * (block_size))
38 
39 /** 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 
72 /** \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  */
80 /* 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. */
83 #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. */
96 /* Note that the encoding of truncated MAC algorithms limits this value
97  * to 64 bytes.
98  */
99 #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
100 
101 /** 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 
121 /* The maximum size of an RSA key on this implementation, in bits.
122  * This is a vendor-specific macro.
123  *
124  * Mbed TLS does not set a hard limit on the size of RSA keys: any key
125  * 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 #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
154 
155 /** The maximum size of a block cipher supported by the implementation. */
156 #define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
157 
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
165  * #PSA_ALG_IS_MAC(\p alg) is true).
166  *
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) \
176  ((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)) : \
178  PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
179  ((void)(key_type), (void)(key_bits), 0))
180 
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
190  * #PSA_ALG_IS_AEAD(\p alg) is true).
191  * \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  */
200 #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
201  (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
202  (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
203  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
214  * #PSA_ALG_IS_AEAD(\p alg) is true).
215  * \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  */
224 #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
225  (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
226  (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 : \
301  0)
302 
303 #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 
319 /** Sufficient signature buffer size for psa_sign_hash().
320  *
321  * This macro returns a sufficient buffer size for a signature using a key
322  * 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
337  * psa_sign_hash() will not fail with
338  * #PSA_ERROR_BUFFER_TOO_SMALL.
339  * If the parameters are a valid combination that is not supported
340  * by the implementation, this macro shall return either a
341  * sensible size or 0.
342  * If the parameters are not valid, the
343  * return value is unspecified.
344  */
345 #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
346  (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 
350 #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
354  *
355  * 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
369  * 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.
380  * \param alg The asymmetric encryption algorithm.
381  *
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
387  * by the implementation, this macro shall return either a
388  * 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 
397 /** Sufficient output buffer size for psa_asymmetric_decrypt().
398  *
399  * This macro returns a sufficient buffer size for a plaintext produced using
400  * a key of the specified type and size, with the specified algorithm.
401  * Note that the actual size of the plaintext may be smaller, depending
402  * 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.
411  * \param alg The asymmetric encryption algorithm.
412  *
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
418  * by the implementation, this macro shall return either a
419  * 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 
428 /* 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  */
477 #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
478  (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  */
515 #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
516  (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  */
535 #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
536  (PSA_BITS_TO_BYTES(key_bits))
537 
538 /** Sufficient output buffer size for psa_export_key() or psa_export_public_key().
539  *
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}
550  * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
551  * psa_status_t status;
552  * status = psa_get_key_attributes(key, &attributes);
553  * if (status != PSA_SUCCESS) handle_error(...);
554  * psa_key_type_t key_type = psa_get_key_type(&attributes);
555  * size_t key_bits = psa_get_key_bits(&attributes);
556  * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
557  * psa_reset_key_attributes(&attributes);
558  * uint8_t *buffer = malloc(buffer_size);
559  * if (buffer == NULL) handle_error(...);
560  * 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
566  * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR
567  * to convert a key pair type to the corresponding public key type.
568  * \code{c}
569  * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
570  * psa_status_t status;
571  * status = psa_get_key_attributes(key, &attributes);
572  * if (status != PSA_SUCCESS) handle_error(...);
573  * 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);
576  * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
577  * psa_reset_key_attributes(&attributes);
578  * uint8_t *buffer = malloc(buffer_size);
579  * if (buffer == NULL) handle_error(...);
580  * 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
590  * psa_sign_hash() will not fail with
591  * #PSA_ERROR_BUFFER_TOO_SMALL.
592  * If the parameters are a valid combination that is not supported
593  * by the implementation, this macro shall return either a
594  * 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) : \
600  (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
601  (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
602  (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
603  (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
604  PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
605  PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
606  0)
607 
608 #endif /* PSA_CRYPTO_SIZES_H */
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.