Repository navigation
feat: port crypto to the PSA API for Mbed TLS 4 / ESP-IDF 6 - #91
davidcoulson wants to merge 2 commits into
Conversation
|
Thanks for this — triaging the open PRs surfaced that your second commit is a critical fix, so I've extracted it into #102 and it will land on its own:
After #102 lands, #91 needs a rebase onto |
… AAD (#102) - Peer::decrypt_response now compares the computed GCM tag with the received one (constant-time). mbedtls_gcm_finish only computes the tag in decrypt mode, so response authenticity was never actually checked. - The response AD is built with the counter the vehicle sends in AES_GCM_ResponseData instead of the peer's own request counter, which differs after a retry rebuilds the request. decrypt_response takes the response counter and construct_ad_buffer accepts a counter override. - Tests: tampered tag and mismatched counter are rejected end to end; the VCSEC round trip signs with a differing counter and asserts rejection on mismatch; the unauthenticated mock decryption is now asserted to fail instead of being accepted. The counter-override approach and test structure are adapted from PR #91 by David Coulson, which found this while porting to PSA. Co-authored-by: David Coulson <23066302+davidcoulson@users.noreply.github.com>
yoziru
left a comment
There was a problem hiding this comment.
Followed up on the earlier comment: #102 is on main (v6.3.0), so rebasing is now the only blocker for CI.
-
Rebase onto main and drop
e398afa. The red test is the stale copy ofResponseDecryption: at this head it assertsdecrypt_result == OK || decrypt_result != OK(tests/test_protocol_handshake.cpp:219), so it cannot fail. Main's version assertsE_ERROR_DECRYPTfor that invalid vector and should survive the conflict. I checked both files at this head and on main. -
The 4.x claim is not exercised anywhere. The host build fetches
mbedtls-3.6.6-idf(CMakeLists.txt:147) and CI has no other Mbed TLS leg, so the#ifdef TF_PSA_CRYPTO_VERSION_MAJORbranch insrc/crypto_context.cpp:22is never compiled. Either add a 4.x matrix entry or scope the PR/release note to "3.6+, 4.x hardware-verified only". Otherwise this will rot on the next bump.
Separate, pre-existing: Peer::generate_nonce() returns void and encrypt() continues after a failed RNG (src/peer.cpp:116-125, 501). Not introduced here, but this PR swaps the RNG backend, so a follow-up is worth it.
The PSA translation itself looks sound: the new tests compare against the real legacy implementation (not mocks) and cover tag/AAD/ciphertext tampering and PEM bytes. I did not run the suite; CI is the evidence for 3.6.
ESP-IDF 6 ships Mbed TLS 4 (TF-PSA-Crypto), which removed the legacy ECDH, ECP, GCM, SHA-1 and CTR_DRBG APIs this library used, so it no longer compiled. Use PSA Crypto instead, which Mbed TLS 3.6 (ESP-IDF 5.3+) and 4.x both provide: - P-256 key pair is a volatile PSA key; ECDH via psa_raw_key_agreement - AES-128-GCM, SHA-1, SHA-256, HMAC-SHA256 and random via PSA - private key storage is unchanged: the same SEC1 PEM is read and written through mbedtls_pk, so keys saved by earlier releases still load and newly saved keys are byte-identical - mbedtls_pk_parse_key lost its RNG arguments in 4.x; gated on TF_PSA_CRYPTO_VERSION_MAJOR - GCM error paths no longer leak the context Minimum ESP-IDF is now 5.3 (first with Mbed TLS 3.6). Tests: new test_psa_crypto checks the PSA paths against the protocol spec vectors and against the legacy 3.6 APIs (GCM ciphertext and tag, HMAC, SHA-256, PEM bytes), plus tamper rejection, key generation, save/reload, move semantics and curve validation. Host suite 270/270. Built with ESPHome for esp32c5 on ESP-IDF 5.5.1 and 6.1.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The TF_PSA_CRYPTO_VERSION_MAJOR branch in crypto_context.cpp was only ever compiled on hardware. The host build now takes the Mbed TLS repository and tag as cache variables, links tfpsacrypto when that target exists (4.x moved the crypto library out of mbedcrypto), and CI adds a test leg against upstream Mbed TLS v4.1.1. The tests that used the legacy mbedtls_gcm/sha256/md APIs as a reference implementation could not build on 4.x, where those headers are private. They now check the PSA output against known answers computed with the 3.6 implementation, and the PEM compatibility checks call mbedtls_pk_parse_key with the right arity for each major. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
e398afa to
d78fe04
Compare
|
Both addressed in the push.
Agreed on |
yoziru
left a comment
There was a problem hiding this comment.
Both earlier points check out: rebased onto main, and the 4.x leg runs the suite. Two new blockers before merge:
build4/is committed: 118 files / 27 MB of build output and binaries. Remove it and ignore the directory.- The ECDH path logs the session key at VERBOSE (inline); it leaks key material and fails the Python bindings leg here.
Ran the host suite at head: 332/332 on 3.6.6; the bindings test fails as above. 4.x is CI-only here.
| break; | ||
| } | ||
| LOG_DEBUG("Tesla ECDH completed successfully"); | ||
| LOG_VERBOSE("Session key: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", |
There was a problem hiding this comment.
Remove this log: it writes the derived session key to the log callback. test_raw_message_and_log_callbacks in bindings/python/tests/test_offline_flow.py asserts it never appears and fails at this head. main removed the same line in v6.0.0.
|
|
||
| EXPECT_STREQ(reinterpret_cast<char *>(pem_out), reinterpret_cast<char *>(legacy_pem)); | ||
|
|
||
| uint8_t public_key[65]; |
There was a problem hiding this comment.
This compares PSA output to the same Mbed TLS version, so it passes even if 4.x changes the PEM format. A fixed 3.6 golden (with the trailing newline) would prove the downgrade path.
|
Follow-up from the correctness pass, since the first review is already posted. The PSA port makes key storage process-global. The standalone build leaves Two smaller items: the GCM helpers import the key before constructing the Also worth a release note: removing the |
Summary
ESP-IDF 6 ships Mbed TLS 4 (TF-PSA-Crypto), which removed the legacy ECDH/ECP, GCM, SHA-1 and CTR_DRBG APIs this library uses, so it no longer builds there (
mbedtls/ecdh.h: No such file or directory). This moves the crypto onto the PSA Crypto API, which Mbed TLS 3.6 (ESP-IDF 5.3+) and 4.x both provide, so one source tree builds on both.psa_raw_key_agreement(same X-coordinate output as before), and the session key is stillSHA1(shared)[:16].CryptoUtilshelpers (aes_gcm_encrypt/aes_gcm_decrypt,sha256_hash).Peer::encrypt/decrypt_responseandget_vin_advertisement_nameuse them.EC PRIVATE KEYPEM is read and written throughmbedtls_pk(mbedtls_pk_get_psa_attributes/mbedtls_pk_import_into_psa/mbedtls_pk_copy_from_psa). Keys saved by earlier releases still load, and newly written PEM is byte-identical to what 3.x wrote (there's a test for this), so upgrading or downgrading does not lose pairing.mbedtls_pk_parse_keylost its RNG arguments in 4.x. That is the only#ifdef, keyed onTF_PSA_CRYPTO_VERSION_MAJOR.mbedtls_gcm_context.CryptoContextgetters returning rawmbedtls_*_contextpointers are removed. Nothing in the library or tests used them.idf_component.yml: the minimum ESP-IDF is now 5.3, the first release with Mbed TLS 3.6 (the PSA import/export helpers above).Second commit: Mbed TLS 4.x in CI
The
TF_PSA_CRYPTO_VERSION_MAJORbranch was previously only compiled on hardware. The host build now takesTESLABLE_MBEDTLS_REPO/TESLABLE_MBEDTLS_TAGas cache variables and linkstfpsacryptowhen that target exists, and CI adds a "Build and Test (Mbed TLS 4.x)" leg against upstream v4.1.1. The tests that compared against the legacymbedtls_gcm/sha256/mdAPIs (private headers in 4.x) now check known answers computed with the 3.6 implementation instead.Verification
test_psa_crypto.cpp. It checks:mbedtls_pkon both majors🤖 Generated with Claude Code