Repository navigation
fix: verify AES-GCM response tags and use the response counter in the AAD - #102
Merged
Merged
Conversation
… AAD - 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Found while triaging #91: response authenticity was never verified.
mbedtls_gcm_finish()in decrypt mode only computes the tag — it never compares it (verified against the vendored mbedtls 3.6.6 source: it memcpys the computed tag into the caller's buffer and returns 0).Peer::decrypt_responsepassed a copy of the received tag as that output buffer, so every response was accepted regardless of tag, AAD or ciphertext tampering. Combined with the AAD being built from the peer's own request counter instead of theAES_GCM_ResponseData.counter, the shortcut went unnoticed: after a retry rebuilt the request (new counter), a response to the old request still "decrypted".Impact: a BLE-range attacker could flip ciphertext bits or replay/forge response structure; vehicle responses were never authenticated.
Changes
decrypt_responsecomputes the tag and compares it with the received one usingCryptoUtils::secure_memory_compare(constant-time); mismatch →ERROR_DECRYPT.AES_GCM_ResponseData.counter) via a counter override onconstruct_ad_buffer; both call sites (parse_payload_car_server_response, VCSEC path) pass it. This is protocol-correct and required — with verification active, the old request-counter shortcut breaks after retries.Tests
EncryptedResponseTest.TamperedResponseTagIsRejected/ResponseWithMismatchedCounterIsRejected— end-to-end throughVehicle; verified to fail without the verification (the command completed with the tampered response).VcsecResponseDecryptRoundTripsigns with a response counter that differs from the peer's and asserts a mismatched counter is rejected.ResponseDecryptionreplaced its tautological assertion: unauthenticated mock data must now be rejected.Credit
The counter-override design and parts of the test structure are adapted from #91 (David Coulson), who surfaced both bugs while porting to PSA —
psa_aead_decrypt()verifies, which is how the missing legacy check was found. This PR is the extracted, immediately-landable fix; #91's PSA port itself still needs a rebase on top of current main.Review