Repository navigation
feat(secure): run the secure channel in core-interface - #125
Conversation
…racts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
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
Hardware test: WiCAN (ESP32-C3) + virtual-carTested 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
Tests1. [Analysis] The identity is made on the ESP: passAfter the erase, the board created an identity. On boot it logged 2. [PR] Pairing, validation and key enrollment: passFresh pairing of the Virtual car, then validation with the cluster code. After that, Paired phones showed "Remote access set up (…)". No identity banner.
3. [PR]/[Analysis] Reset with the bond kept: passThe board was reset with
4. [Analysis] firmware#125 flashed over #124 without erasing: pass
5. [Analysis] New device identity: passI erased only the access flash range (
6. [Analysis] Live interop: the Dart
|
| 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, andcargo xtask clippyfor pc and esp × virtual-car and egmp all pass. The only warning is arustlsdeprecation inboard-pc, which also appears onmain. - mobile-app#134:
build_runner,dart format(non-generated files),flutter analyze --fatal-infosandflutter 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.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkZEiWM4SEVB1xBTaoykRa
…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
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 branchfeat/device-identity-key); merge that first.noise.rs): I looked atclatterand implemented the pattern directly onx25519-dalek,chacha20poly1305andblake2instead. 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 passesdocs/secure-channel-vectors.jsonbyte 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.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 byclose_linkor 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]intransport.toml.process_secure_frame(link, bytes) -> Option<reply>,close_link(link),seal(session, &DeviceToApp) -> (link, frame), andSECURE_TX_CHANNELfor responses (never waited on, so a stalled transport can't stall the dispatcher).handle_secure_messagestamps the client'sble:ID assource_device_id, then basic and advanced commands go through the sameadmit_vehicle_commandas BLE (read-only, validated, controller lease) and get aCommandAuthorization. Responses route back throughTransport::Secure(SessionHandle). System commands are dropped. The plain-MQTTMQTT_COMMANDS_AUTHENTICATEDgate stays until Move the MQTT driver onto the secure channel and allow remote commands #122, as the issue says.ble:source); the comment and docs say so.SESSION_BYTES, in a static).sealbuilds 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, includingcore-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 withREJECTED_NOT_VALIDATEDsealed 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 innoise.rs.cargo xtask clippy --board pcandcargo fmt --checkpass;core-interfacebuilds forriscv32imc-unknown-none-elf.+esptoolchain 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
cargo fmt --all -- --check,cargo xtask clippy(pc) andcargo xtask testpass; ESP left to CIconfig.toml, credentials, build artifacts or.binfiles committedSafety
admit_vehicle_commandas BLE: read-only check, validated phone for the identified car, controller lease, thenCommandAuthorization::newfor that phone, exactly as over BLE, soallow_can_txstill checks the validated phone when the frames are sent. No new way to create aCommandAuthorization, andis_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