Skip to content

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

Open
Tichael wants to merge 2 commits into
mainfrom
feat/device-identity-key
Open

Tichael wants to merge 2 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
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