Skip to content

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

Open
Tichael wants to merge 1 commit into
feat/client-key-enrollmentfrom
feat/secure-channel-session
Open

Tichael wants to merge 1 commit into
feat/client-key-enrollmentfrom
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

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
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