Skip to content

PSA Crypto API results in larger binary size compared to direct mbedTLS API - seeking guidance on optimization #10254

@Ashish285

Description

@Ashish285

Summary

We've been evaluating the PSA Crypto API as a migration path from direct mbedTLS calls and noticed that for basic SHA-256 hashing, the PSA implementation produces approximately 17KB additional flash usage. We'd appreciate guidance on whether this is expected behavior and if there are optimization strategies we might be missing

System information

Mbed TLS version (number or commit id): 3.6.3
Operating system and version: ESP-IDF v5.5
Configuration (if not default, please attach mbedtls_config.h): Default
Compiler and options (if you used a pre-built binary, please indicate how you obtained it):
Additional environment information:

Expected behavior

We expect to see similar flash usage with the PSA APIs as the direct mbedTLS APIs.

Actual behavior

Size Analysis Results
Total flash size difference: +17,104 bytes
Text section difference: +15,960 bytes
Rodata section difference: +1,144 bytes

Current approach:

  • Using identical mbedTLS configuration macros for both builds
  • MBEDTLS_PSA_CRYPTO_CONFIG is not enabled (using default behavior)
  • Relying on automatic PSA symbol deduction from MBEDTLS_* symbols

Observations from symbol analysis:

The PSA build appears to include additional algorithms beyond SHA-256:

  • mbedtls_sha512_software_process: +3,216 bytes
  • esp_internal_sha1_parallel_engine_pr: +4,516 bytes
  • Various SHA-1, MD5, and SHA-512 related functions: ~600+ bytes total
  • Additional infrastructure: entropy/DRBG, key management, error translation (~2KB+)

Comparison with direct mbedTLS:

When using direct mbedTLS calls, the linker effectively performs dead code elimination. Even with multiple hash algorithms enabled in the configuration, only the functions actually used (SHA-256) appear in the final binary.

Questions

We're hoping you could help us understand:

  • Expected behavior: Is this size difference typical when migrating from direct mbedTLS to PSA API? We'd appreciate understanding if this represents the expected cost of PSA's standardized interface.

  • Configuration guidance: We noticed the documentation mentions PSA_WANT_ALG_* macros for fine-grained algorithm selection. Would you recommend:

    • Enabling MBEDTLS_PSA_CRYPTO_CONFIG and explicitly configuring PSA_WANT_ALG_SHA_256?
    • Is this the recommended approach for size-conscious applications?
  • Dead code elimination: Is there a way to achieve similar dead code elimination with PSA as we see with direct mbedTLS calls? Or does PSA's abstraction layer inherently prevent this?

  • Migration best practices: For applications migrating from direct mbedTLS to PSA, what would you recommend as the optimal configuration strategy to minimize size impact?

Steps to reproduce

Build a binary with the following code and check the binary size difference.

PSA Implementation:

psa_crypto_init();
psa_hash_operation_t hash_op = psa_hash_operation_init();
psa_hash_setup(&hash_op, PSA_ALG_SHA_256);
psa_hash_update(&hash_op, data, len);
psa_hash_finish(&hash_op, hash, sizeof(hash), &hash_length);

Direct mbedTLS Implementation:

mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
mbedtls_sha256_starts(&ctx, 0);
mbedtls_sha256_update(&ctx, data, len);
mbedtls_sha256_finish(&ctx, hash);
mbedtls_sha256_free(&ctx);

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions