40#ifndef PSA_CRYPTO_SIZES_H
41#define PSA_CRYPTO_SIZES_H
45#if !defined(MBEDTLS_CONFIG_FILE)
48#include MBEDTLS_CONFIG_FILE
51#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
52#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
54#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
55 (((length) + (block_size) - 1) / (block_size) * (block_size))
71#define PSA_HASH_SIZE(alg) \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
87 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
101#if defined(MBEDTLS_SHA512_C)
102#define PSA_HASH_MAX_SIZE 64
103#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
105#define PSA_HASH_MAX_SIZE 32
106#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
122#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
139#define PSA_AEAD_TAG_LENGTH(alg) \
140 (PSA_ALG_IS_AEAD(alg) ? \
141 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
157#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
161#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
162#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
163#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
164#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
165#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
166#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
167#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
168#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
169#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
170#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
171#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
172#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
173#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
174#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
175#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
176#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
177#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
178#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
179#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
180#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
181#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
182#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
183#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
184#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
185#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
186#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
188#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
205#define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128
208#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
227#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
228 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
229 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
230 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
231 ((void)(key_type), (void)(key_bits), 0))
252#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
253 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
254 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
276#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
277 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
278 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
304#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
305 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
306 PSA_ROUND_UP_TO_MULTIPLE(PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE, (input_length)) : \
327#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
328 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
329 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
350#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
351 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
352 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
355#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
356 (PSA_ALG_IS_RSA_OAEP(alg) ? \
357 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
368#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
369 (PSA_BITS_TO_BYTES(curve_bits) * 2)
397#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
398 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
399 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
402#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
403 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
413#define PSA_SIGNATURE_MAX_SIZE \
414 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
415 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
416 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
444#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
445 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
446 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
475#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
476 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
477 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
490#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
504#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
505 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
529#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
530 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
548#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
549 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
567#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
568 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
580#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
581 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
587#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
588 (PSA_BITS_TO_BYTES(key_bits))
650#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
651 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
652 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
653 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
654 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
655 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
656 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
657 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
685#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
686 (PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) == 16 && \
687 (PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_CCM || \
688 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_GCM) ? 12 : \
689 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
690 PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \
701#define PSA_AEAD_NONCE_MAX_SIZE 12
729#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
730 (PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) > 1 && \
731 ((alg) == PSA_ALG_CTR || \
732 (alg) == PSA_ALG_CFB || \
733 (alg) == PSA_ALG_OFB || \
734 (alg) == PSA_ALG_XTS || \
735 (alg) == PSA_ALG_CBC_NO_PADDING || \
736 (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
737 (key_type) == PSA_KEY_TYPE_CHACHA20 && \
738 (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
745#define PSA_CIPHER_IV_MAX_SIZE 16
Configuration options (set of defines)