Skip to content

feat(secure): add the device identity key and the client key registry - #124

Merged
Tichael merged 3 commits into
mainfrom
feat/device-identity-key
Dec 5, 2026
Merged

Tichael merged 3 commits into
mainfrom
feat/device-identity-key

Conversation

@Tichael

@Tichael Tichael commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Closes #118. Phase 1 of OpenCarControls/contracts#44: the device's identity key and the registry of client keys enrolled over BLE. Nothing uses the keys yet; the secure channel (#119) does. Needs OpenCarControls/contracts#50; the submodule bump is its own commit and points at that PR's branch until it merges (I'll re-pin to main then).

Device identity (core-interface/src/identity.rs)

  • X25519 static key, made once from the board's random source, refused (no identity, channel off) without one. Restored at boot with restore_device_identity(stored), which returns the bytes to persist when it made a new one.
  • Kept across bond resets and ClearPairedPhones. I did not add a "factory reset" that rotates it: erasing the flash does, and phones pair again then anyway.
  • Key ID = hex of the first 10 bytes of BLAKE2s-256(public key), checked against the contracts' test vectors. Reported in SystemState.device_identity with the access state, BLE only.
  • ESP: stored in the access range (key 2), made only after BleConnector::new, since the RNG is only a true random source while the radio runs. PC: [hardware.pc] device_identity_file.

Client key registry (core-interface/src/client_keys.rs)

  • EnrollClientKeyCommand handled in the dispatcher: BLE only (MQTT drops system commands), validated phone only (REJECTED_NOT_VALIDATED), answered with the device identity. Hub kind, bad key length, all-zero key, label > 32 bytes, and a key already enrolled by another client are refused (FAILED).
  • One key per phone under its ble: ID; re-enrolling replaces it. Dropped with the bond: forget_phone_validation (removal, pairing again), clear_paired_phones, and prune_phone_validations at boot (bond reset). Kept when the validation ends (another car approved).
  • Persisted with a format version (encode_client_keys / client_keys_to_store / restore_client_keys). ESP: access range key 3, its buffer raised to 768 bytes for 8 phones; a failed write rewrites every item of the range, identity first. PC: client_keys_file.
  • ListPairedPhones fills remote_key_enrolled and remote_key_label. client_for_key() is the lookup for the channel.

Docs: docs/ble.md (Device identity, Client keys, Persistence), docs/architecture.md, docs/testing.md, config.toml.example.

How it was tested

  • cargo xtask test: all host suites pass, including the new core-interface/tests/client_keys.rs (17 tests: identity made once, restored, absent without a random source, kept across clearing phones, key ID vs. the contract vectors; enrollment refused for bonded-not-validated phones and over MQTT, replaced on re-enroll, refusals, removed with the bond / pairing again / clear / prune, kept when the validation ends, store round-trip and malformed input).
  • cargo xtask build --board pc and cargo xtask clippy --board pc pass; cargo test -p board-esp passes.
  • Ran the PC firmware twice: the identity is created on the first boot and the same key ID comes back after the restart.
  • core-interface (with x25519-dalek and blake2) builds for riscv32imc-unknown-none-elf (the C3).
  • Not run: the ESP firmware builds (no +esp toolchain here), so board-esp with hardware (the store and transport changes) is only checked by CI. Not tested on hardware, and I couldn't measure the flash cost on the C3/S3. A client key surviving a restart on the PC was only checked through the encoding tests, not end to end.

Checklist

Safety

  • This change does not touch CAN TX, BLE pairing/encryption, or OTA
  • It does touch the BLE trust model: a new system command, accepted over BLE only and only from a validated phone (same rule as the CAN approval). No CAN TX path changes and no BLE pairing checks change.

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

Tichael commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator Author

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

Tested with contracts#50, mobile-app#133, and the stacked firmware#125 / mobile-app#134. Tests 1–3 and 5 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.

@Tichael
Tichael merged commit 26fa722 into main Dec 5, 2026
9 checks passed
@Tichael
Tichael deleted the feat/device-identity-key branch December 5, 2026 18:28
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.

Device identity key and client key registry

2 participants