Skip to content

feat(system): add the device identity, client key enrollment and the secure channel spec - #50

Merged
Tichael merged 1 commit into
mainfrom
feat/secure-channel-keys
Dec 5, 2026
Merged

Tichael merged 1 commit into
mainfrom
feat/secure-channel-keys

Conversation

@Tichael

@Tichael Tichael commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Phase 1 of the epic #44 (keys and secure channel), contracts side. Closes #45 and #46 in one PR, since the firmware and the app need both to implement and test the channel.

#45, keys over BLE (system.proto, append-only):

  • DeviceIdentity { public_key, key_id }, reported in SystemState.device_identity (field 10, BLE only) and in SystemCommandResult.device_identity (field 5).
  • EnrollClientKeyCommand { public_key, kind, label } (SystemCommand field 14) and ClientKind (PHONE; HUB reserved and refused for now). BLE only, validated phone only, re-enrolling replaces the key; revocation is documented in the comments.
  • PairedPhone.remote_key_enrolled (4) and remote_key_label (5, so the phone list can show the label the issue mentions).
  • mqtt_client_id is not deprecated here, as the issue says (that comes with the relay).

#46, the channel spec: docs/secure-channel.md, a [secure_channel] section in transport.toml with its constants, docs/secure-channel-vectors.json, and the generator tools/secure_channel_vectors.py (Python noiseprotocol 0.3.1, run with python -I, deterministic output). README links the doc.

Choices the issue left open (please check)

  • Handshake payloads are empty in both messages. A message in message 1 would get neither forward secrecy nor replay protection, so messages only travel in transport frames. Cost: one round trip before the first command.
  • Unknown session: explicit frame type 5 (type + session ID, nothing else) answered to a transport frame for a session the device doesn't have. It is unauthenticated, so the client treats it as a hint and keeps its old session until a new handshake completes. It reveals nothing (session IDs are random) and is smaller than what triggered it.
  • Session limits: 4 sessions, 2 per client (LAN + relay), idle 300 s, lifetime 3600 s or 1,000,000 frames per direction, then the client re-handshakes (no in-session rekey). When full, the session idle the longest is replaced (the same client's first).
  • Replayed handshake inits: refused when their ephemeral key was seen recently for that client, so a replay can't take a slot.
  • Handshake timeout 10 s (LTE relay), label at most 32 UTF-8 bytes, empty plaintext = keepalive.
  • Prologue: opencar/secure/1 + 0x00 + the key ID in ASCII. Transport messages use empty associated data (the counter is the nonce, and each session has its own keys).

Checklist

  • PR title uses conventional-commit style
  • buf format, buf lint and buf breaking --against '.git#branch=main' pass
  • Append-only rules followed: new fields and messages appended, nothing renumbered or removed
  • BLE message size limit still respected (the identity adds 56 bytes to a system state update sent on its own)
  • Matching firmware and mobile-app changes exist or are linked: they follow (firmware#118/#119, mobile-app#128/#129) and will be linked here

Source of the data

The vectors come from the noiseprotocol reference implementation, with fixed static and ephemeral keys.


Generated by Claude Code

…secure channel spec

Adds the trust anchor and the contract of the end-to-end encrypted channel
(epic #44, phase 1):

- DeviceIdentity (X25519 public key and key ID) in SystemState and in the
  answer to the new EnrollClientKeyCommand, with ClientKind (HUB reserved).
- PairedPhone.remote_key_enrolled and remote_key_label.
- docs/secure-channel.md: Noise_IK_25519_ChaChaPoly_BLAKE2s, prologue, frame
  format, replay window, session rules, identity and authorization.
- [secure_channel] constants in transport.toml.
- docs/secure-channel-vectors.json and the script that generates them with
  the noiseprotocol reference implementation.

Closes #45
Closes #46

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
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

The latest Buf updates on your PR. Results from workflow Proto Check / check (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedDec 1, 2026, 1:21 AM

@Tichael

Tichael commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator Author

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

Tested together with firmware#124, mobile-app#133, firmware#125 and mobile-app#134. The schema and the test vectors are exercised by tests 1–3 and 5 (keys and enrollment over BLE) and test 6 (the channel, Dart ↔ Rust).

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

@Tichael
Tichael merged commit d825689 into main Dec 5, 2026
2 checks passed
@Tichael
Tichael deleted the feat/secure-channel-keys branch December 5, 2026 18:15
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 0.16.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add the device identity key and client key enrollment over BLE

2 participants