Skip to content

feat: add the secure channel session in pure Dart - #134

Merged
Tichael merged 7 commits into
mainfrom
feat/secure-channel-session
Dec 5, 2026
Merged

Tichael merged 7 commits into
mainfrom
feat/secure-channel-session

Conversation

@Tichael

@Tichael Tichael commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Closes #129. Phase 1 of OpenCarControls/contracts#44: the app side of the secure channel (OpenCarControls/contracts#46), a transport-independent session the LAN (#130) and MQTT (#131) transports will plug into. Stacked on #133 (base feat/client-key-enrollment); retarget to main once that merges.

All in lib/secure/, pure Dart with no Flutter imports (ready for the background isolate in #132):

  • noise_ik.dart: Noise_IK_25519_ChaChaPoly_BLAKE2s initiator, synchronous on the cryptography package's Dart implementations, with HMAC/HKDF from the Noise spec. Refuses a low-order device key, as the firmware does.
  • secure_frames.dart: frame build/parse (malformed → dropped), the 64-counter replay window (updated only after a frame decrypts), and the 2-byte length framing for streams as a separate helper.
  • secure_session.dart:
    • SecureHandshake → SecureSession: seal(AppToDevice), sealKeepalive(), open(frame) → message, keepalive, or drop with a reason. Tracks idle time, lifetime and message counts.
    • SecureChannel over any SecureFramePipe: handshakes on first use, after expiry, on an unknown-session hint (keeping the old session meanwhile) and at 90 % of the lifetime or message limit (keeping the old session until the new one is up, still reading late frames on it). If the device doesn't answer within handshake_timeout_ms, it fails with SecureHandshakeTimeoutException, whose text tells the user to connect over Bluetooth.
  • The [secure_channel] constants are generated (SecureChannelConstantsBuilder, added in feat: enroll the app's client key once the phone is validated #133), not copied.
  • Docs: new "Secure channel session" section in docs/architecture.md.

One choice to flag: the 90 % re-handshake threshold (kSecureRehandshakeAt) is an app constant, not in the contract, since the device doesn't need it.

How it was tested

  • test/secure_channel_test.dart, against contracts/docs/secure-channel-vectors.json: init frame byte for byte, response, handshake hash, transport frames both ways (including the out-of-order counter and the keepalive), unknown-session frame, response under another device key.
  • Frames: malformed frames, the plaintext limit, stream framing split across chunks. Replay window: out-of-order, duplicates, outside the window.
  • SecureChannel against a fake device (test/support/fake_secure_device.dart, a small IK responder): messages both ways, a duplicated or replayed frame, a forged tag, timeouts (no answer, client key removed, wrong pinned device key), re-handshake after idle, before the lifetime (old session still read), after a device restart (unknown session), and after dispose.
  • dart format, flutter analyze --fatal-infos and the full flutter test pass.
  • Not run: an end-to-end check against the PC firmware. The firmware has the channel in core-interface (feat(secure): run the secure channel in core-interface firmware#125), but no transport carries it yet (LAN is firmware#120), so there's nothing to connect to. Locally, flutter test needed thermion_dart's native download skipped; that workaround is not committed.

Checklist

  • PR title uses conventional-commit style (feat:, fix:, docs:, chore:); CI validates it
  • dart format, flutter analyze --fatal-infos and flutter test pass
  • Docs (docs/ or README) updated if behaviour changed
  • No config.toml or credentials committed; generated files not hand-edited
  • If the wire format changed: the contracts PR is linked and the submodule bump is its own commit (in feat: enroll the app's client key once the phone is validated #133)

Safety

  • This change does not touch BLE pairing/bonding or how commands are sent to the vehicle
  • It does (explain below)

Nothing calls the new code yet.


Generated by Claude Code

Each install makes an X25519 client key, kept in flutter_secure_storage.
Over Bluetooth, once the device reports the phone validated, the app
enrolls the key with EnrollClientKey and pins the device identity per
vehicle. A different identity is never pinned silently: the dashboard
shows a banner to trust it or unpair. The paired phones screen shows
which phones have remote access set up.

Closes #128

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
Adds a builder for [secure_channel] in transport.toml, so the key ID
length and the client label limit are imported instead of copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
A Noise IK initiator on the cryptography package's Dart code, the
channel's frames, replay window and stream framing, and SecureChannel,
which keeps a session up over any frame pipe: it handshakes on first
use, after expiry, on an unknown session hint and ahead of the limits,
and reports a clear error when the device doesn't answer. Tested
against the contract's vectors and a fake device.

Closes #129

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
@Tichael Tichael self-assigned this Dec 1, 2026
@Tichael

Tichael commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator Author

Hardware test: WiCAN (ESP32-C3) + virtual-car

Tested with contracts#50, firmware#124, firmware#125 and mobile-app#133 (its base). Test 6 and the host checks cover this PR. Nothing calls this code yet, so it wasn't installed on the phone.

Setup

  • Board: WiCAN (ESP32-C3, 4 MB), platform virtual-car. Flash erased before stage A.
  • Stage A: firmware#124 765697e + mobile-app#133 7dd1ecb (release APK, app data cleared) + contracts#50 4ce23a0.
  • Stage B: firmware#125 16d5284, flashed over stage A without erasing. The phone kept the mobile-app#133 APK, because nothing in mobile-app#134 is called yet. mobile-app#134 0eb773e was tested off-device (test 6).
  • Phone: Pixel 9 Pro Fold (Android), one phone.
  • All four code PRs pin contracts 4ce23a0.

Tests

1. [Analysis] The identity is made on the ESP: pass

After the erase, the board created an identity. On boot it logged Device identity: key ID 9b4174fd65e9e12b71a9, and no none, the secure channel is off. The created line itself came on the first boot after flashing, before the logger was running.

2. [PR] Pairing, validation and key enrollment: pass

Fresh pairing of the Virtual car, then validation with the cluster code. After that, Paired phones showed "Remote access set up (…)". No identity banner.

Time (UTC) Event
17:52:52.073 Phone connected (pairing window open)
17:52:59.079 Link encrypted, MTU 251, bond stored
17:52:59.581 App subscribed to TX
17:53:25.264 Validation code shown; phone validated

3. [PR]/[Analysis] Reset with the bond kept: pass

The board was reset with espflash reset --after watchdog-reset. The key ID stayed the same, the client keys came back from flash, and the app still showed the label, with no banner.

Time (UTC) Event
17:55:36.209 Phone validations: restored from flash
17:55:36.213 Client keys: restored from flash
17:55:36.232 Device identity: key ID 9b4174fd65e9e12b71a9 (unchanged)
17:56:10.565 Phone reconnected (is_known=true) after the app's 30 s retry gap, encrypted, subscribed 1 s later

4. [Analysis] firmware#125 flashed over #124 without erasing: pass

  • The build was 1,348,960 bytes, 32.67% of the app partition, about the same as 3D hero: skin accent on the charge glow and cable #124.
  • No ESP transport calls the secure channel yet, so the linker leaves its code out. Its flash, RAM and handshake cost on the C3 still has to be measured once the LAN or MQTT transport lands.
  • On boot, the identity, client keys, validation and bond were all restored, with the same key ID.
  • The phone reconnected at 17:58:08. Live state and several lock/unlock commands over BLE worked, which goes through the new dispatch, routing and dedupe code.
  • No warnings, no TX dropped and no dropped chunks in the log.

5. [Analysis] New device identity: pass

I erased only the access flash range (0x3FC000–0x3FDFFF), which holds the approval, validations, client keys and identity, and kept the bond.

Time (UTC) Event
18:07:26.098 Device identity: created
18:07:26.105 Device identity: key ID ceade7dcc2033c4cbc38
18:07:59.208 Phone reconnected with its bond, encrypted
  • The app showed "The device has a new identity" with the new key ID, plus Unpair and Trust new identity.
  • The tester tapped Trust new identity while the phone wasn't validated yet. The app then asked to allow control again, which is expected: the CAN approval was erased too. After that it ran the normal validation.
  • After validating, Paired phones showed "Remote access set up (…)" again, and the banner was gone.

6. [Analysis] Live interop: the Dart SecureChannel (mobile-app#134) against firmware#125's channel, off-device: pass

A throwaway, uncommitted harness:

  • A temporary core-interface integration test serves on_frame, handle_secure_message and seal over localhost TCP, with the 2-byte length framing.
  • Its setup: random OS keys, one owner phone enrolled and validated, and a second phone enrolled but not validated.
  • A temporary flutter test ran the Dart SecureChannel against it.
Case Result
Owner: handshake, 51 commands (each echoed back on the same session), a keepalive 1 handshake. Every command reached BASIC_CMD_CHANNEL as Transport::Secure with the owner's ble: ID and an authorization. The keepalive was dropped silently
Device restart (sessions forgotten) while the client keeps its session The next frame got the unknown-session reply (type 5). The client handshook again, and the next command went through. The command sent during the restart is lost, as designed
Second phone, enrolled but not validated RejectedNotValidated from the dispatcher, sealed back on its session and decrypted by the client
Unknown client key / wrong pinned device key No answer. The client failed with SecureHandshakeTimeoutException

Host checks for the stacked PRs (CI only ran the title check, since their base isn't main)

  • firmware#125: cargo xtask test, cargo fmt --check, and cargo xtask clippy for pc and esp × virtual-car and egmp all pass. The only warning is a rustls deprecation in board-pc, which also appears on main.
  • mobile-app#134: build_runner, dart format (non-generated files), flutter analyze --fatal-infos and flutter test (557 tests) all pass.

Not tested

  • E-GMP in the car.
  • iOS.
  • A second phone: a stranger trying to enroll, or a key already enrolled by another phone.
  • Xtensa/S3 flash cost.
  • The channel over a real transport (LAN firmware#120, MQTT firmware#122) and its handshake time and memory on the C3.
  • Re-enrolling after a lost key on the device. The unit tests cover it, but it can't be triggered on this board without also changing the identity.

Base automatically changed from feat/client-key-enrollment to main December 5, 2026 18:28
…into feat/secure-channel-session

# Conflicts:
#	docs/architecture.md
@Tichael
Tichael merged commit 131481d into main Dec 5, 2026
4 checks passed
@Tichael
Tichael deleted the feat/secure-channel-session branch December 5, 2026 19:09
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.

Secure channel session in the app

2 participants