mbed TLS v3.1.0
crypto_sizes.h
Go to the documentation of this file.
1 
23 /*
24  * Copyright The Mbed TLS Contributors
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 
40 #ifndef PSA_CRYPTO_SIZES_H
41 #define PSA_CRYPTO_SIZES_H
42 
43 /* Include the Mbed TLS configuration file, the way Mbed TLS does it
44  * in each of its header files. */
45 #include "mbedtls/build_info.h"
46 
47 #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
48 #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
49 
50 #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
51  (((length) + (block_size) - 1) / (block_size) * (block_size))
52 
65 #define PSA_HASH_LENGTH(alg) \
66  ( \
67  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
68  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
69  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
70  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
71  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
72  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
73  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
74  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
75  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
76  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
77  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
78  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
79  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
80  0)
81 
97 #define PSA_HASH_BLOCK_LENGTH(alg) \
98  ( \
99  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
100  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
101  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
102  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
103  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
104  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
105  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
106  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
107  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
108  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
109  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
110  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
111  PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
112  0)
113 
121 /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
122  * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
123  * HMAC-SHA3-512. */
124 #if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
125 #define PSA_HASH_MAX_SIZE 64
126 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
127 #else
128 #define PSA_HASH_MAX_SIZE 32
129 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
130 #endif
131 
139 /* All non-HMAC MACs have a maximum size that's smaller than the
140  * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
141 /* Note that the encoding of truncated MAC algorithms limits this value
142  * to 64 bytes.
143  */
144 #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
145 
167 #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
168  (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
169  PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
170  ((void) (key_bits), 0))
171 
176 #define PSA_AEAD_TAG_MAX_SIZE 16
177 
178 /* The maximum size of an RSA key on this implementation, in bits.
179  * This is a vendor-specific macro.
180  *
181  * Mbed TLS does not set a hard limit on the size of RSA keys: any key
182  * whose parameters fit in a bignum is accepted. However large keys can
183  * induce a large memory usage and long computation times. Unlike other
184  * auxiliary macros in this file and in crypto.h, which reflect how the
185  * library is configured, this macro defines how the library is
186  * configured. This implementation refuses to import or generate an
187  * RSA key whose size is larger than the value defined here.
188  *
189  * Note that an implementation may set different size limits for different
190  * operations, and does not need to accept all key sizes up to the limit. */
191 #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
192 
193 /* The maximum size of an ECC key on this implementation, in bits.
194  * This is a vendor-specific macro. */
195 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
196 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
197 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
198 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
199 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
200 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
201 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
202 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
203 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
204 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
205 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
206 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
207 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
208 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
209 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
210 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
211 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
212 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
213 #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
214 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
215 #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
216 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
217 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
218 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
219 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
220 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
221 #else
222 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
223 #endif
224 
240 #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
241 
243 #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
244 
266 #define PSA_MAC_LENGTH(key_type, key_bits, alg) \
267  ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
268  PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
269  PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
270  ((void)(key_type), (void)(key_bits), 0))
271 
298 #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
299  (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
300  (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
301  0)
302 
321 #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
322  ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
323 
324 
351 #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
352  (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
353  (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
354  (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
355  0)
356 
375 #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
376  (ciphertext_length)
377 
403 #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
404  (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
405  MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
406  MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
407  0 : \
408  (key_type) == PSA_KEY_TYPE_CHACHA20 && \
409  MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
410  0)
411 
423 #define PSA_AEAD_NONCE_MAX_SIZE 13
424 
451 /* For all the AEAD modes defined in this specification, it is possible
452  * to emit output without delay. However, hardware may not always be
453  * capable of this. So for modes based on a block cipher, allow the
454  * implementation to delay the output until it has a full block. */
455 #define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
456  (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
457  PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
458  PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
459  (input_length) : \
460  0)
461 
472 #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
473  (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
474 
496 #define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
497  (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
498  PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
499  PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
500  0)
501 
507 #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
508 
530 #define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
531  (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
532  PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
533  PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
534  0)
535 
541 #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
542 
543 #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
544  (PSA_ALG_IS_RSA_OAEP(alg) ? \
545  2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
546  11 /*PKCS#1v1.5*/)
547 
556 #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
557  (PSA_BITS_TO_BYTES(curve_bits) * 2)
558 
584 #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
585  (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
586  PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
587  ((void)alg, 0))
588 
589 #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
590  PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
591 
599 #define PSA_SIGNATURE_MAX_SIZE \
600  (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
601  PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
602  PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
603 
629 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
630  (PSA_KEY_TYPE_IS_RSA(key_type) ? \
631  ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
632  0)
633 
639 /* This macro assumes that RSA is the only supported asymmetric encryption. */
640 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
641  (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
642 
668 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
669  (PSA_KEY_TYPE_IS_RSA(key_type) ? \
670  PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
671  0)
672 
680 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
681  (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
682 
683 /* Maximum size of the ASN.1 encoding of an INTEGER with the specified
684  * number of bits.
685  *
686  * This definition assumes that bits <= 2^19 - 9 so that the length field
687  * is at most 3 bytes. The length of the encoding is the length of the
688  * bit string padded to a whole number of bytes plus:
689  * - 1 type byte;
690  * - 1 to 3 length bytes;
691  * - 0 to 1 bytes of leading 0 due to the sign bit.
692  */
693 #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
694  ((bits) / 8 + 5)
695 
696 /* Maximum size of the export encoding of an RSA public key.
697  * Assumes that the public exponent is less than 2^32.
698  *
699  * RSAPublicKey ::= SEQUENCE {
700  * modulus INTEGER, -- n
701  * publicExponent INTEGER } -- e
702  *
703  * - 4 bytes of SEQUENCE overhead;
704  * - n : INTEGER;
705  * - 7 bytes for the public exponent.
706  */
707 #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
708  (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
709 
710 /* Maximum size of the export encoding of an RSA key pair.
711  * Assumes thatthe public exponent is less than 2^32 and that the size
712  * difference between the two primes is at most 1 bit.
713  *
714  * RSAPrivateKey ::= SEQUENCE {
715  * version Version, -- 0
716  * modulus INTEGER, -- N-bit
717  * publicExponent INTEGER, -- 32-bit
718  * privateExponent INTEGER, -- N-bit
719  * prime1 INTEGER, -- N/2-bit
720  * prime2 INTEGER, -- N/2-bit
721  * exponent1 INTEGER, -- N/2-bit
722  * exponent2 INTEGER, -- N/2-bit
723  * coefficient INTEGER, -- N/2-bit
724  * }
725  *
726  * - 4 bytes of SEQUENCE overhead;
727  * - 3 bytes of version;
728  * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
729  * overapproximated as 9 half-size INTEGERS;
730  * - 7 bytes for the public exponent.
731  */
732 #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
733  (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
734 
735 /* Maximum size of the export encoding of a DSA public key.
736  *
737  * SubjectPublicKeyInfo ::= SEQUENCE {
738  * algorithm AlgorithmIdentifier,
739  * subjectPublicKey BIT STRING } -- contains DSAPublicKey
740  * AlgorithmIdentifier ::= SEQUENCE {
741  * algorithm OBJECT IDENTIFIER,
742  * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
743  * DSAPublicKey ::= INTEGER -- public key, Y
744  *
745  * - 3 * 4 bytes of SEQUENCE overhead;
746  * - 1 + 1 + 7 bytes of algorithm (DSA OID);
747  * - 4 bytes of BIT STRING overhead;
748  * - 3 full-size INTEGERs (p, g, y);
749  * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
750  */
751 #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
752  (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
753 
754 /* Maximum size of the export encoding of a DSA key pair.
755  *
756  * DSAPrivateKey ::= SEQUENCE {
757  * version Version, -- 0
758  * prime INTEGER, -- p
759  * subprime INTEGER, -- q
760  * generator INTEGER, -- g
761  * public INTEGER, -- y
762  * private INTEGER, -- x
763  * }
764  *
765  * - 4 bytes of SEQUENCE overhead;
766  * - 3 bytes of version;
767  * - 3 full-size INTEGERs (p, g, y);
768  * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
769  */
770 #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
771  (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
772 
773 /* Maximum size of the export encoding of an ECC public key.
774  *
775  * The representation of an ECC public key is:
776  * - The byte 0x04;
777  * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
778  * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
779  * - where m is the bit size associated with the curve.
780  *
781  * - 1 byte + 2 * point size.
782  */
783 #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
784  (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
785 
786 /* Maximum size of the export encoding of an ECC key pair.
787  *
788  * An ECC key pair is represented by the secret value.
789  */
790 #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
791  (PSA_BITS_TO_BYTES(key_bits))
792 
832 #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
833  (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
834  (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
835  (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
836  (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
837  (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
838  PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
839  PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
840  0)
841 
887 #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
888  (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
889  PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
890  0)
891 
900 #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
901  (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
902  PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
903  PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
904  PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
905 
915 #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
916  (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
917  PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
918  PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
919  PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
920 
944 /* FFDH is not yet supported in PSA. */
945 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
946  (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
947  PSA_BITS_TO_BYTES(key_bits) : \
948  0)
949 
957 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
958  (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
959 
984 #define PSA_CIPHER_IV_LENGTH(key_type, alg) \
985  (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
986  ((alg) == PSA_ALG_CTR || \
987  (alg) == PSA_ALG_CFB || \
988  (alg) == PSA_ALG_OFB || \
989  (alg) == PSA_ALG_XTS || \
990  (alg) == PSA_ALG_CBC_NO_PADDING || \
991  (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
992  (key_type) == PSA_KEY_TYPE_CHACHA20 && \
993  (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
994  (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
995  0)
996 
1001 #define PSA_CIPHER_IV_MAX_SIZE 16
1002 
1026 #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1027  (alg == PSA_ALG_CBC_PKCS7 ? \
1028  (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1029  PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1030  (input_length) + 1) + \
1031  PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
1032  (PSA_ALG_IS_CIPHER(alg) ? \
1033  (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
1034  0))
1035 
1047 #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
1048  (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
1049  (input_length) + 1) + \
1050  PSA_CIPHER_IV_MAX_SIZE)
1051 
1071 #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
1072  (PSA_ALG_IS_CIPHER(alg) && \
1073  ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
1074  (input_length) : \
1075  0)
1076 
1087 #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
1088  (input_length)
1089 
1108 #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
1109  (PSA_ALG_IS_CIPHER(alg) ? \
1110  (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
1111  (((alg) == PSA_ALG_CBC_PKCS7 || \
1112  (alg) == PSA_ALG_CBC_NO_PADDING || \
1113  (alg) == PSA_ALG_ECB_NO_PADDING) ? \
1114  PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
1115  input_length) : \
1116  (input_length)) : 0) : \
1117  0)
1118 
1129 #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1130  (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
1131 
1149 #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
1150  (PSA_ALG_IS_CIPHER(alg) ? \
1151  (alg == PSA_ALG_CBC_PKCS7 ? \
1152  PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
1153  0) : \
1154  0)
1155 
1161 #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1162  (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
1163 
1164 #endif /* PSA_CRYPTO_SIZES_H */
Build-time configuration info.