Skip to content

fix: verify AES-GCM response tags and use the response counter in the AAD - #102

Merged
yoziru merged 1 commit into
mainfrom
fix/response-authentication
Oct 10, 2026
Merged

yoziru merged 1 commit into
mainfrom
fix/response-authentication

Conversation

@yoziru

@yoziru yoziru commented Oct 10, 2026

Copy link
Copy Markdown
Owner

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_response passed 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 the AES_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_response computes the tag and compares it with the received one using CryptoUtils::secure_memory_compare (constant-time); mismatch → ERROR_DECRYPT.
  • Response AD uses the counter the vehicle reports (AES_GCM_ResponseData.counter) via a counter override on construct_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.
  • Free the GCM context on the remaining early error path.

Tests

  • EncryptedResponseTest.TamperedResponseTagIsRejected / ResponseWithMismatchedCounterIsRejected — end-to-end through Vehicle; verified to fail without the verification (the command completed with the tampered response).
  • VcsecResponseDecryptRoundTrip signs with a response counter that differs from the peer's and asserts a mismatched counter is rejected.
  • ResponseDecryption replaced its tautological assertion: unauthenticated mock data must now be rejected.
  • 235/235 pass; clang-format clean.

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

  • ponytail-review: minimal — one comparison, one parameter, two call sites; no new abstraction.
  • test-audit: rejections asserted at the owning boundaries (Vehicle end-to-end, Peer round trip), both fail on pre-fix behavior; tautology removed rather than replaced with another.

… 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>
@yoziru
yoziru merged commit b135894 into main Oct 10, 2026
4 checks passed
@yoziru
yoziru deleted the fix/response-authentication branch October 10, 2026 11:35
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.

1 participant