Skip to content

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

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

Tichael merged 6 commits into
mainfrom
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 trust anchor of the secure channel (contracts#44, phase 1):

- core-interface: the device's X25519 identity, made once from the board's
  random source and restored at boot, reported with its key ID in
  SystemState over BLE; the client key registry, filled by
  EnrollClientKeyCommand from a validated phone over BLE only, dropped with
  the phone's bond, kept when its validation ends, persisted like the
  validations. ListPairedPhones reports remote_key_enrolled and the label.
- ESP: identity and client keys in the access range of the flash, the
  identity made once the BLE controller (and so the true RNG) is up.
- PC: device_identity_file and client_keys_file under [hardware.pc].

Closes #118

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
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
@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 (its base), mobile-app#133 and mobile-app#134. Tests 4 and 6, and the host checks, cover this PR.

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 feat(secure): add the device identity key and the client key registry #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/device-identity-key to main December 5, 2026 18:28
…to feat/secure-channel

# Conflicts:
#	Cargo.lock
#	core-interface/Cargo.toml
#	core-interface/src/client_keys.rs
#	core-interface/src/identity.rs
#	core-interface/src/lib.rs
#	docs/testing.md
@Tichael
Tichael merged commit 36fbfbc into main Dec 5, 2026
18 checks passed
@Tichael
Tichael deleted the feat/secure-channel 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 and per-client authorization in core-interface

2 participants