Skip to content

Mbedtls 4 support#2174

Open
petermm wants to merge 9 commits intoatomvm:release-0.7from
petermm:mbedtls-4-support
Open

Mbedtls 4 support#2174
petermm wants to merge 9 commits intoatomvm:release-0.7from
petermm:mbedtls-4-support

Conversation

@petermm
Copy link
Copy Markdown
Contributor

@petermm petermm commented Mar 9, 2026

Tested in other branch against esp-idf 6.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@petermm petermm force-pushed the mbedtls-4-support branch 3 times, most recently from 94c6a0c to cc891a3 Compare March 20, 2026 20:02
@petermm petermm changed the base branch from main to release-0.7 March 20, 2026 20:22
@petermm petermm force-pushed the mbedtls-4-support branch 3 times, most recently from 3a2edf1 to 9abc3fa Compare March 26, 2026 16:04
@petermm petermm changed the base branch from release-0.7 to main March 26, 2026 16:09
@petermm petermm changed the base branch from main to release-0.7 March 26, 2026 16:09
@petermm petermm force-pushed the mbedtls-4-support branch 4 times, most recently from 24daab8 to 936a5f7 Compare March 26, 2026 16:53
@petermm petermm changed the title Mbedtls 4 support (and esp-idf 6) Mbedtls 4 support Mar 26, 2026
@petermm petermm marked this pull request as ready for review March 26, 2026 19:26
@petermm petermm force-pushed the mbedtls-4-support branch 2 times, most recently from 10567e7 to 6c607cb Compare March 26, 2026 23:30
@petermm petermm marked this pull request as draft March 27, 2026 06:52
@petermm petermm force-pushed the mbedtls-4-support branch 3 times, most recently from d71b6e9 to 46cb4cb Compare March 27, 2026 12:17
@petermm petermm marked this pull request as ready for review March 27, 2026 14:09
@petermm petermm force-pushed the mbedtls-4-support branch from 46cb4cb to 5574dab Compare March 30, 2026 18:00
petermm and others added 7 commits March 31, 2026 10:45
Port otp_crypto to the PSA Crypto API used by mbedtls 4.x.

- Replace deprecated low-level mbedtls APIs with PSA equivalents for
  hash, HMAC, cipher, and AEAD operations
- Guard legacy mbedtls 2/3 code paths with version checks
- Update CMake to detect mbedtls 4 and set HAVE_PSA_CRYPTO
- Keep ESP32 JIT config outside mbedtls version guards

Signed-off-by: Peter M <petermm@gmail.com>
Avoid including mbedtls/pkcs5.h when building against mbedtls 4,
where that header is not available.

Keep the existing PKCS5-based pbkdf2_hmac implementation for
mbedtls 2/3, but switch the mbedtls 4 path to the PSA key
derivation API so crypto:pbkdf2_hmac/5 remains available.

Also reject zero iterations in PBKDF2 with a clear error message,
and update the feature/NIF guards so pbkdf2_hmac stays registered
on both legacy and mbedtls 4 builds.

Signed-off-by: Peter M <petermm@gmail.com>
Improve PSA crypto resource management and memory safety:

- Normalize do_psa_init() across all PSA-backed NIFs so every
  entry point initializes PSA consistently
- Destroy PSA key handles immediately after finalization instead
  of deferring to GC, reducing key material residency time
- Abort PSA operations and destroy keys on update failure to
  avoid dangling handles
- Replace free() with secure_free() for all scratch buffers that
  may contain sensitive data (plaintext, key material)

Signed-off-by: Peter M <petermm@gmail.com>
Declare the PSA output buffer size variables before any goto-based cleanup path can skip their initialization.

This fixes Clang -Wsometimes-uninitialized failures in crypto_one_time/4-5 and crypto_update/2 when cleanup frees scratch buffers after early exits.

Signed-off-by: Peter M <petermm@gmail.com>
Allocate at least 1 byte when the computed size is zero to avoid
undefined malloc(0) behaviour on embedded libc implementations that
may legally return NULL for zero-length allocations.

This aligns the one-shot cipher and handle_iodata paths with the
streaming cipher code that already guards against this case.

Signed-off-by: Peter M <petermm@gmail.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019d2a7c-9760-707a-b24e-f6f6475a8608
Co-authored-by: Amp <amp@ampcode.com>
- Reset key attributes after psa_import_key in one-shot cipher path
  to match all other PSA import sites
- Use secure_free for all crypto-adjacent buffers (sign/verify data,
  signature buffers, MAC data, AEAD AAD and combined buffers) to
  prevent sensitive data from lingering in freed memory
- Reject AEAD decryption without a tag early with a clear error
  instead of letting it fail deep in PSA
- Add finalized flag to MAC state so repeated mac_final/mac_update
  calls after finalization raise a clear error instead of a generic
  PSA failure
- Document that ssl:nif_conf_rng is a no-op on mbedtls 4.x where
PSA handles randomness internally

Signed-off-by: Peter M <petermm@gmail.com>
Signed-off-by: Peter M <petermm@gmail.com>
Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the mbedtls-4-support branch from 5574dab to dbee591 Compare March 31, 2026 08:48
@petermm petermm marked this pull request as draft March 31, 2026 10:36
@petermm petermm force-pushed the mbedtls-4-support branch from 1559fc0 to 260702e Compare March 31, 2026 12:59
Make the fetched MbedTLS version configurable and extend the wasm jobs to exercise both mbedtls3 and mbedtls4 builds while keeping release artifacts on the mbedtls3 path.

Update the Emscripten build to link against MbedTLS::mbedtls, guard the legacy entropy helpers to pre-4.x builds, and switch the PSA feature probes to compile-only checks so they work with fetched and custom MbedTLS targets.

Install the Python modules required by the MbedTLS 4 generators in the Emscripten CI container so the tf-psa-crypto build completes.

Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the mbedtls-4-support branch from 260702e to 7a79a64 Compare March 31, 2026 13:52
@github-advanced-security
Copy link
Copy Markdown

You are seeing this message because GitHub Code Scanning has recently been set up for this repository, or this pull request contains the workflow file for the Code Scanning tool.

What Enabling Code Scanning Means:

  • The 'Security' tab will display more code scanning analysis results (e.g., for the default branch).
  • Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results.
  • You will be able to see the analysis results for the pull request's branch on this overview once the scans have completed and the checks have passed.

For more information about GitHub Code Scanning, check out the documentation.

@petermm petermm marked this pull request as ready for review March 31, 2026 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants