blob: 169566ecece6405f7be6c35b5f26034a5a9da8f0 [file] [log] [blame]
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001/**
2 * \file psa/crypto_sizes.h
3 *
4 * \brief PSA cryptography module: Mbed TLS buffer size macros
5 *
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02009 * This file contains the definitions of macros that are useful to
10 * compute buffer sizes. The signatures and semantics of these macros
11 * are standardized, but the definitions are not, because they depend on
12 * the available algorithms and, in some cases, on permitted tolerances
13 * on buffer sizes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +020014 *
Gilles Peskine07c91f52018-06-28 18:02:53 +020015 * In implementations with isolation between the application and the
16 * cryptography module, implementers should take care to ensure that
17 * the definitions that are exposed to applications match what the
18 * module implements.
19 *
Gilles Peskine49cee6c2018-06-27 21:03:58 +020020 * Macros that compute sizes whose values do not depend on the
21 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020022 */
23/*
24 * Copyright (C) 2018, ARM Limited, All Rights Reserved
25 * SPDX-License-Identifier: Apache-2.0
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
38 *
39 * This file is part of mbed TLS (https://tls.mbed.org)
40 */
41
42#ifndef PSA_CRYPTO_SIZES_H
43#define PSA_CRYPTO_SIZES_H
44
45/* Include the Mbed TLS configuration file, the way Mbed TLS does it
46 * in each of its header files. */
47#if !defined(MBEDTLS_CONFIG_FILE)
48#include "../mbedtls/config.h"
49#else
50#include MBEDTLS_CONFIG_FILE
51#endif
52
Gilles Peskineaf3baab2018-06-27 22:55:52 +020053/** \def PSA_HASH_MAX_SIZE
54 *
55 * Maximum size of a hash.
56 *
57 * This macro must expand to a compile-time constant integer. This value
58 * should be the maximum size of a hash supported by the implementation,
59 * in bytes, and must be no smaller than this maximum.
60 */
Gilles Peskine3052f532018-09-17 14:13:26 +020061/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
62 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
63 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +020064#if defined(MBEDTLS_SHA512_C)
65#define PSA_HASH_MAX_SIZE 64
66#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
67#else
68#define PSA_HASH_MAX_SIZE 32
69#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
70#endif
71
Gilles Peskineaf3baab2018-06-27 22:55:52 +020072/** \def PSA_MAC_MAX_SIZE
73 *
74 * Maximum size of a MAC.
75 *
76 * This macro must expand to a compile-time constant integer. This value
77 * should be the maximum size of a MAC supported by the implementation,
78 * in bytes, and must be no smaller than this maximum.
79 */
80/* All non-HMAC MACs have a maximum size that's smaller than the
81 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
82#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
83
84/* The maximum size of an RSA key on this implementation, in bits.
85 * This is a vendor-specific macro.
86 *
87 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
88 * whose parameters fit in a bignum is accepted. However large keys can
89 * induce a large memory usage and long computation times. Unlike other
90 * auxiliary macros in this file and in crypto.h, which reflect how the
91 * library is configured, this macro defines how the library is
92 * configured. This implementation refuses to import or generate an
93 * RSA key whose size is larger than the value defined here.
94 *
95 * Note that an implementation may set different size limits for different
96 * operations, and does not need to accept all key sizes up to the limit. */
97#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
98
99/* The maximum size of an ECC key on this implementation, in bits.
100 * This is a vendor-specific macro. */
101#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
102#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
103#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
104#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
105#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
106#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
107#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
108#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
109#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
110#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
111#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
112#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
113#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
114#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
115#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
116#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
117#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
118#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
119#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
120#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
121#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
122#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
123#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
124#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
125#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
126#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
127#else
128#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
129#endif
130
131/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
132 *
133 * Maximum size of an asymmetric signature.
134 *
135 * This macro must expand to a compile-time constant integer. This value
136 * should be the maximum size of a MAC supported by the implementation,
137 * in bytes, and must be no smaller than this maximum.
138 */
139#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
140 PSA_BITS_TO_BYTES( \
141 PSA_VENDOR_RSA_MAX_KEY_BITS > PSA_VENDOR_ECC_MAX_CURVE_BITS ? \
142 PSA_VENDOR_RSA_MAX_KEY_BITS : \
143 PSA_VENDOR_ECC_MAX_CURVE_BITS \
144 )
145
Gilles Peskined911eb72018-08-14 15:18:45 +0200146/** The maximum size of a block cipher supported by the implementation. */
147#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200148
Gilles Peskineacd4be32018-07-08 19:56:25 +0200149/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200150 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200151 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200152 *
153 * \param key_type The type of the MAC key.
154 * \param key_bits The size of the MAC key in bits.
155 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
156 * #PSA_ALG_IS_MAC(alg) is true).
157 *
158 * \return The MAC size for the specified algorithm with
159 * the specified key parameters.
160 * \return 0 if the MAC algorithm is not recognized.
161 * \return Either 0 or the correct size for a MAC algorithm that
162 * the implementation recognizes, but does not support.
163 * \return Unspecified if the key parameters are not consistent
164 * with the algorithm.
165 */
166#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskined911eb72018-08-14 15:18:45 +0200167 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
168 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200169 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200170 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200171
172/** The maximum size of the output of psa_aead_encrypt(), in bytes.
173 *
174 * If the size of the ciphertext buffer is at least this large, it is
175 * guaranteed that psa_aead_encrypt() will not fail due to an
176 * insufficient buffer size. Depending on the algorithm, the actual size of
177 * the ciphertext may be smaller.
178 *
179 * \param alg An AEAD algorithm
180 * (\c PSA_ALG_XXX value such that
181 * #PSA_ALG_IS_AEAD(alg) is true).
182 * \param plaintext_length Size of the plaintext in bytes.
183 *
184 * \return The AEAD ciphertext size for the specified
185 * algorithm.
186 * If the AEAD algorithm is not recognized, return 0.
187 * An implementation may return either 0 or a
188 * correct size for an AEAD algorithm that it
189 * recognizes, but does not support.
190 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200191#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
192 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
193 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200194 0)
195
196/** The maximum size of the output of psa_aead_decrypt(), in bytes.
197 *
198 * If the size of the plaintext buffer is at least this large, it is
199 * guaranteed that psa_aead_decrypt() will not fail due to an
200 * insufficient buffer size. Depending on the algorithm, the actual size of
201 * the plaintext may be smaller.
202 *
203 * \param alg An AEAD algorithm
204 * (\c PSA_ALG_XXX value such that
205 * #PSA_ALG_IS_AEAD(alg) is true).
206 * \param ciphertext_length Size of the plaintext in bytes.
207 *
208 * \return The AEAD ciphertext size for the specified
209 * algorithm.
210 * If the AEAD algorithm is not recognized, return 0.
211 * An implementation may return either 0 or a
212 * correct size for an AEAD algorithm that it
213 * recognizes, but does not support.
214 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200215#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
216 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
217 (plaintext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200218 0)
219
220/** Safe signature buffer size for psa_asymmetric_sign().
221 *
222 * This macro returns a safe buffer size for a signature using a key
223 * of the specified type and size, with the specified algorithm.
224 * Note that the actual size of the signature may be smaller
225 * (some algorithms produce a variable-size signature).
226 *
227 * \warning This function may call its arguments multiple times or
228 * zero times, so you should not pass arguments that contain
229 * side effects.
230 *
231 * \param key_type An asymmetric key type (this may indifferently be a
232 * key pair type or a public key type).
233 * \param key_bits The size of the key in bits.
234 * \param alg The signature algorithm.
235 *
236 * \return If the parameters are valid and supported, return
237 * a buffer size in bytes that guarantees that
238 * psa_asymmetric_sign() will not fail with
239 * #PSA_ERROR_BUFFER_TOO_SMALL.
240 * If the parameters are a valid combination that is not supported
241 * by the implementation, this macro either shall return either a
242 * sensible size or 0.
243 * If the parameters are not valid, the
244 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200245 */
246#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
247 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
248 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
249 ((void)alg, 0))
250
Gilles Peskinedcd14942018-07-12 00:30:52 +0200251/** Safe output buffer size for psa_asymmetric_encrypt().
252 *
253 * This macro returns a safe buffer size for a ciphertext produced using
254 * a key of the specified type and size, with the specified algorithm.
255 * Note that the actual size of the ciphertext may be smaller, depending
256 * on the algorithm.
257 *
258 * \warning This function may call its arguments multiple times or
259 * zero times, so you should not pass arguments that contain
260 * side effects.
261 *
262 * \param key_type An asymmetric key type (this may indifferently be a
263 * key pair type or a public key type).
264 * \param key_bits The size of the key in bits.
265 * \param alg The signature algorithm.
266 *
267 * \return If the parameters are valid and supported, return
268 * a buffer size in bytes that guarantees that
269 * psa_asymmetric_encrypt() will not fail with
270 * #PSA_ERROR_BUFFER_TOO_SMALL.
271 * If the parameters are a valid combination that is not supported
272 * by the implementation, this macro either shall return either a
273 * sensible size or 0.
274 * If the parameters are not valid, the
275 * return value is unspecified.
276 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200277#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
278 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
279 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
280 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200281
282/** Safe output buffer size for psa_asymmetric_decrypt().
283 *
284 * This macro returns a safe buffer size for a ciphertext produced using
285 * a key of the specified type and size, with the specified algorithm.
286 * Note that the actual size of the ciphertext may be smaller, depending
287 * on the algorithm.
288 *
289 * \warning This function may call its arguments multiple times or
290 * zero times, so you should not pass arguments that contain
291 * side effects.
292 *
293 * \param key_type An asymmetric key type (this may indifferently be a
294 * key pair type or a public key type).
295 * \param key_bits The size of the key in bits.
296 * \param alg The signature algorithm.
297 *
298 * \return If the parameters are valid and supported, return
299 * a buffer size in bytes that guarantees that
300 * psa_asymmetric_decrypt() will not fail with
301 * #PSA_ERROR_BUFFER_TOO_SMALL.
302 * If the parameters are a valid combination that is not supported
303 * by the implementation, this macro either shall return either a
304 * sensible size or 0.
305 * If the parameters are not valid, the
306 * return value is unspecified.
307 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200308#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
309 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
310 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
311 0)
312
Gilles Peskine1be949b2018-08-10 19:06:59 +0200313/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
314 * number of bits.
315 *
316 * This definition assumes that bits <= 2^19 - 9 so that the length field
317 * is at most 3 bytes. The length of the encoding is the length of the
318 * bit string padded to a whole number of bytes plus:
319 * - 1 type byte;
320 * - 1 to 3 length bytes;
321 * - 0 to 1 bytes of leading 0 due to the sign bit.
322 */
323#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
324 ((bits) / 8 + 5)
325
326/* Maximum size of the export encoding of an RSA public key.
327 * Assumes that the public exponent is less than 2^32.
328 *
329 * SubjectPublicKeyInfo ::= SEQUENCE {
330 * algorithm AlgorithmIdentifier,
331 * subjectPublicKey BIT STRING } -- contains RSAPublicKey
332 * AlgorithmIdentifier ::= SEQUENCE {
333 * algorithm OBJECT IDENTIFIER,
334 * parameters NULL }
335 * RSAPublicKey ::= SEQUENCE {
336 * modulus INTEGER, -- n
337 * publicExponent INTEGER } -- e
338 *
339 * - 3 * 4 bytes of SEQUENCE overhead;
340 * - 1 + 1 + 9 bytes of algorithm (RSA OID);
341 * - 2 bytes of NULL;
342 * - 4 bytes of BIT STRING overhead;
343 * - n : INTEGER;
344 * - 7 bytes for the public exponent.
345 */
346#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
347 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 36)
348
349/* Maximum size of the export encoding of an RSA key pair.
350 * Assumes thatthe public exponent is less than 2^32 and that the size
351 * difference between the two primes is at most 1 bit.
352 *
353 * RSAPrivateKey ::= SEQUENCE {
354 * version Version, -- 0
355 * modulus INTEGER, -- N-bit
356 * publicExponent INTEGER, -- 32-bit
357 * privateExponent INTEGER, -- N-bit
358 * prime1 INTEGER, -- N/2-bit
359 * prime2 INTEGER, -- N/2-bit
360 * exponent1 INTEGER, -- N/2-bit
361 * exponent2 INTEGER, -- N/2-bit
362 * coefficient INTEGER, -- N/2-bit
363 * }
364 *
365 * - 4 bytes of SEQUENCE overhead;
366 * - 3 bytes of version;
367 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
368 * overapproximated as 9 half-size INTEGERS;
369 * - 7 bytes for the public exponent.
370 */
371#define PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) \
372 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
373
374/* Maximum size of the export encoding of a DSA public key.
375 *
376 * SubjectPublicKeyInfo ::= SEQUENCE {
377 * algorithm AlgorithmIdentifier,
378 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
379 * AlgorithmIdentifier ::= SEQUENCE {
380 * algorithm OBJECT IDENTIFIER,
381 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
382 * DSAPublicKey ::= INTEGER -- public key, Y
383 *
384 * - 3 * 4 bytes of SEQUENCE overhead;
385 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
386 * - 4 bytes of BIT STRING overhead;
387 * - 3 full-size INTEGERs (p, g, y);
388 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
389 */
390#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
391 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
392
393/* Maximum size of the export encoding of a DSA key pair.
394 *
395 * DSAPrivateKey ::= SEQUENCE {
396 * version Version, -- 0
397 * prime INTEGER, -- p
398 * subprime INTEGER, -- q
399 * generator INTEGER, -- g
400 * public INTEGER, -- y
401 * private INTEGER, -- x
402 * }
403 *
404 * - 4 bytes of SEQUENCE overhead;
405 * - 3 bytes of version;
406 * - 3 full-size INTEGERs (p, g, y);
407 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
408 */
409#define PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) \
410 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
411
412/* Maximum size of the export encoding of an ECC public key.
413 *
414 * SubjectPublicKeyInfo ::= SEQUENCE {
415 * algorithm AlgorithmIdentifier,
416 * subjectPublicKey BIT STRING } -- contains ECPoint
417 * AlgorithmIdentifier ::= SEQUENCE {
418 * algorithm OBJECT IDENTIFIER,
419 * parameters OBJECT IDENTIFIER } -- namedCurve
Gilles Peskinecb6adbb2018-08-11 01:18:12 +0200420 * ECPoint ::= ...
421 * -- first 8 bits: 0x04;
422 * -- then x_P as an n-bit string, big endian;
423 * -- then y_P as a n-bit string, big endian,
Gilles Peskine1be949b2018-08-10 19:06:59 +0200424 * -- where n is the order of the curve.
425 *
426 * - 2 * 4 bytes of SEQUENCE overhead;
427 * - 1 + 1 + 7 bytes of algorithm (id-ecPublicKey OID);
428 * - 1 + 1 + 12 bytes of namedCurve OID;
429 * - 4 bytes of BIT STRING overhead;
430 * - 1 byte + 2 * point size in ECPoint.
431 */
432#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
433 (2 * PSA_BITS_TO_BYTES(key_bits) + 36)
434
435/* Maximum size of the export encoding of an ECC key pair.
436 *
437 * ECPrivateKey ::= SEQUENCE {
438 * version INTEGER, -- must be 1
439 * privateKey OCTET STRING,
Gilles Peskinecb6adbb2018-08-11 01:18:12 +0200440 * -- `ceiling(log2(n)/8)`-byte string, big endian,
Gilles Peskine1be949b2018-08-10 19:06:59 +0200441 * -- where n is the order of the curve.
Gilles Peskinecb6adbb2018-08-11 01:18:12 +0200442 * parameters [0] IMPLICIT ECParameters {{ NamedCurve }},
443 * publicKey [1] IMPLICIT BIT STRING
Gilles Peskine1be949b2018-08-10 19:06:59 +0200444 * }
445 *
446 * - 4 bytes of SEQUENCE overhead;
447 * - 1 * point size in privateKey
448 * - 1 + 1 + 12 bytes of namedCurve OID;
449 * - 4 bytes of BIT STRING overhead;
450 * - public key as for #PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE.
451 */
452#define PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) \
453 (3 * PSA_BITS_TO_BYTES(key_bits) + 56)
454
455/** Safe output buffer size for psa_export_key() or psa_export_public_key().
456 *
457 * This macro returns a compile-time constant if its arguments are
458 * compile-time constants.
459 *
460 * \warning This function may call its arguments multiple times or
461 * zero times, so you should not pass arguments that contain
462 * side effects.
463 *
464 * The following code illustrates how to allocate enough memory to export
465 * a key by querying the key type and size at runtime.
466 * \code{c}
467 * psa_key_type_t key_type;
468 * size_t key_bits;
469 * psa_status_t status;
470 * status = psa_get_key_information(key, &key_type, &key_bits);
471 * if (status != PSA_SUCCESS) handle_error(...);
472 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
473 * unsigned char *buffer = malloc(buffer_size);
474 * if (buffer != NULL) handle_error(...);
475 * size_t buffer_length;
476 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
477 * if (status != PSA_SUCCESS) handle_error(...);
478 * \endcode
479 *
480 * For psa_export_public_key(), calculate the buffer size from the
481 * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR
482 * to convert a key pair type to the corresponding public key type.
483 * \code{c}
484 * psa_key_type_t key_type;
485 * size_t key_bits;
486 * psa_status_t status;
487 * status = psa_get_key_information(key, &key_type, &key_bits);
488 * if (status != PSA_SUCCESS) handle_error(...);
489 * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(key_type);
490 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
491 * unsigned char *buffer = malloc(buffer_size);
492 * if (buffer != NULL) handle_error(...);
493 * size_t buffer_length;
494 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
495 * if (status != PSA_SUCCESS) handle_error(...);
496 * \endcode
497 *
498 * \param key_type A supported key type.
499 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200500 *
501 * \return If the parameters are valid and supported, return
502 * a buffer size in bytes that guarantees that
503 * psa_asymmetric_sign() will not fail with
504 * #PSA_ERROR_BUFFER_TOO_SMALL.
505 * If the parameters are a valid combination that is not supported
506 * by the implementation, this macro either shall return either a
507 * sensible size or 0.
508 * If the parameters are not valid, the
509 * return value is unspecified.
510 */
511#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
512 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
513 (key_type) == PSA_KEY_TYPE_RSA_KEYPAIR ? PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) : \
514 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
515 (key_type) == PSA_KEY_TYPE_DSA_KEYPAIR ? PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) : \
516 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
517 PSA_KEY_TYPE_IS_ECC_KEYPAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) : \
518 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
519 0)
520
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200521#endif /* PSA_CRYPTO_SIZES_H */