Skip to content

feat(secure): run the secure channel in core-interface - #125

Open
Tichael wants to merge 1 commit into
feat/device-identity-keyfrom
feat/secure-channel
Open

Tichael wants to merge 1 commit into
feat/device-identity-keyfrom
feat/secure-channel

Conversation

@Tichael

@Tichael Tichael commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Closes #119. The device side of the secure channel (OpenCarControls/contracts#46, spec in OpenCarControls/contracts#50) in core-interface, with no transport using it yet: the LAN (#120) and MQTT (#122) issues plug into it. Stacked on #124 (base branch feat/device-identity-key); merge that first.

  • Noise IK (noise.rs): I looked at clatter and implemented the pattern directly on x25519-dalek, chacha20poly1305 and blake2 instead. IK alone is short, and doing it directly gives the explicit per-frame nonces and the fixed ephemeral keys the vectors need without clatter's generic layers and RNG traits. It passes docs/secure-channel-vectors.json byte for byte on both sides (handshake frames, handshake hash, every transport frame), and a wrong prologue, another device key, a refused key or a tampered message fail. Low-order DH results are refused.
  • Frames and sessions (secure_channel.rs): frame parsing and building, the 64-wide replay window (updated only once a frame decrypts), empty plaintext = keepalive, a static table of 4 sessions (2 per client) keyed by session ID + ChannelLink, expiry on idle / lifetime / message count, ended by close_link or by the client's key leaving the registry (checked on every frame and seal), silent drop for unknown keys and recently seen ephemerals, an unknown session frame for transport frames of a session the device doesn't have. Constants are generated from [secure_channel] in transport.toml.
  • API for transports: process_secure_frame(link, bytes) -> Option<reply>, close_link(link), seal(session, &DeviceToApp) -> (link, frame), and SECURE_TX_CHANNEL for responses (never waited on, so a stalled transport can't stall the dispatcher).
  • Identity and authorization: handle_secure_message stamps the client's ble: ID as source_device_id, then basic and advanced commands go through the same admit_vehicle_command as BLE (read-only, validated, controller lease) and get a CommandAuthorization. Responses route back through Transport::Secure(SessionHandle). System commands are dropped. The plain-MQTT MQTT_COMMANDS_AUTHENTICATED gate stays until Move the MQTT driver onto the secure channel and allow remote commands #122, as the issue says.
  • De-duplication now works across BLE and the channel (same ble: source); the comment and docs say so.
  • Memory: a session's table entry is 192 bytes on the host (SESSION_BYTES, in a static). seal builds the frame in one buffer encrypted in place, and inbound frames decrypt into one buffer, so the heap cost of a message is about its own size (up to ~4 KB for the largest).

Docs: docs/architecture.md (new "Secure channel" section, channels table, dedupe), docs/ble.md, docs/testing.md.

How it was tested

  • cargo xtask test: all suites pass, including core-interface/tests/secure_channel.rs (22 tests: the device handshake against the vectors, unknown key unanswered, replayed init refused, malformed and tampered frames dropped, commands stamped and authorized like BLE, advanced commands allowed, a not-validated phone refused with REJECTED_NOT_VALIDATED sealed back to its session, read-only refused with the reason, system commands dropped, a command resent over BLE caught as a duplicate, sealing and per-frame counters, the replay window, keepalives, unknown session replies, idle and lifetime expiry, key removal and link close, session limits and replacement) and the vector tests in noise.rs.
  • cargo xtask clippy --board pc and cargo fmt --check pass; core-interface builds for riscv32imc-unknown-none-elf.
  • Not run: ESP builds (no +esp toolchain here) and anything on hardware. I couldn't measure the handshake's real stack and heap use or its time on the C3; the 4 X25519 operations of a handshake are the cost to watch there.

Checklist

Safety

  • This change does not touch CAN TX, BLE pairing/encryption, or OTA
  • It adds a second path into vehicle commands. It goes through the same admit_vehicle_command as BLE: read-only check, validated phone for the identified car, controller lease, then CommandAuthorization::new for that phone, exactly as over BLE, so allow_can_tx still checks the validated phone when the frames are sent. No new way to create a CommandAuthorization, and is_can_read_only() gating is unchanged. The phone identity comes from the Noise handshake (a key enrolled over BLE by that validated phone), never from the message.

Generated by Claude Code

The device side of the secure channel (contracts#44, phase 1), independent
of the transport that carries its frames:

- noise.rs: Noise_IK_25519_ChaChaPoly_BLAKE2s (responder, plus a test
  initiator), written on x25519-dalek, chacha20poly1305 and blake2; passes
  the contracts' test vectors byte for byte.
- secure_channel.rs: frames, the 64-wide replay window, a session table
  (4 sessions, 2 per client) with idle, lifetime and message-count expiry,
  sessions ended by a closed link or a removed key, silent drops for unknown
  keys and replayed handshakes, unknown session replies, seal() for
  outbound messages and SECURE_TX_CHANNEL for responses.
- dispatch: handle_secure_message stamps the client's ID and applies the
  same checks as BLE to basic and advanced commands; system commands are
  dropped. Transport::Secure routes responses back through the session.
- Constants from [secure_channel] in transport.toml.

Closes #119

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
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