40 #ifndef PSA_CRYPTO_SIZES_H
41 #define PSA_CRYPTO_SIZES_H
47 #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
48 #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
50 #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
51 (((length) + (block_size) - 1) / (block_size) * (block_size))
65 #define PSA_HASH_LENGTH(alg) \
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 : \
97 #define PSA_HASH_BLOCK_LENGTH(alg) \
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 : \
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
128 #define PSA_HASH_MAX_SIZE 32
129 #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
144 #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
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))
176 #define PSA_AEAD_TAG_MAX_SIZE 16
191 #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
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
222 #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
240 #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
243 #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
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))
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) : \
321 #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
322 ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
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) : \
375 #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
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 : \
408 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
409 MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
423 #define PSA_AEAD_NONCE_MAX_SIZE 13
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)) : \
472 #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
473 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
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) : \
507 #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
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) : \
541 #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
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 : \
556 #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
557 (PSA_BITS_TO_BYTES(curve_bits) * 2)
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) : \
589 #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
590 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
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)
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)) : \
640 #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
641 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
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) : \
680 #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
681 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
693 #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
707 #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
708 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
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)
751 #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
752 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
770 #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
771 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
783 #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
784 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
790 #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
791 (PSA_BITS_TO_BYTES(key_bits))
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) : \
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) : \
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))
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))
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) : \
957 #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
958 (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
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 : \
1001 #define PSA_CIPHER_IV_MAX_SIZE 16
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)) : \
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)
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 ? \
1087 #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
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), \
1116 (input_length)) : 0) : \
1129 #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
1130 (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
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) : \
1161 #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
1162 (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
Build-time configuration info.