Skip to content

Harden device pairing - #16

Open
betoxf wants to merge 2 commits into
mainfrom
fix/harden-pairing
Open

betoxf wants to merge 2 commits into
mainfrom
fix/harden-pairing

Conversation

@betoxf

@betoxf betoxf commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

What changed

  • PAKE-based pairing. Each connection starts with a short preamble (MagicInp + protocol version), then a symmetric SPAKE2 exchange (RustCrypto spake2, Ed25519 group, identity magicinput/pake/v6) on the normalized pairing code. HKDF-SHA256 of the SPAKE2 key, salted with the transcript, is the Noise NNpsk0 pre-shared key, and the transcript is also the Noise prologue. Noise still carries all traffic. Config::psk() is removed.
  • No pairing-derived data in discovery. mDNS TXT now holds only id, n (name) and v (protocol version). Computers dial any MagicInput computer on the same version (up to 16 remembered), and pairing decides. Config::group() is removed.
  • Failed-attempt throttling. This applies per address in both directions: 4 attempts in a row, then a wait that doubles up to 10 s. It is forgotten after 10 quiet minutes and cleared on success or a local code change. Inbound attempts from addresses that never paired share a budget of 30 per minute, and at most 8 inbound connections can be pairing at once. Failures are logged at warn with the wait, without the code.
  • Unbiased code generation. random_string uses rejection sampling. The format is unchanged (8 characters, XXXX-XXXX).
  • Log. The pairing code is not written to the log; magicinput status shows it to the local user.
  • Status. nearby[].same_code is replaced by nearby[].compatible (same protocol version), because whether codes match is only known once connected. Next-step text and docs/AI_SETUP.md are updated to match.
  • Protocol version 6. A 0.6.2-or-older peer fails with "update MagicInput on both computers": in the handshake error, a once-per-computer warning from discovery, and magicinput status.
  • Docs. SECURITY.md, docs/ARCHITECTURE.md, README (privacy section) and a CHANGELOG Unreleased entry.

Compatibility / deploy notes

Both computers need this version; it does not connect to 0.6.2 or older. There are no config or migration changes. New dependencies: spake2 0.4 and hkdf 0.13; Cargo.lock is updated.

Validation

  • cargo test --locked (macOS, stable 1.99.1): 26 passed. The new tests cover:
    • right code pairs (codes differing only in case and separators)
    • wrong code is rejected on both sides
    • an old-format or other-version peer gets the update message
    • the mDNS TXT is identical for configs that differ only in code, with keys exactly id, n, v
    • throttle pacing and backoff cap, the overall budget, and pairing working again after failed attempts
    • every code character is equally likely, checked exhaustively over all byte values
  • cargo clippy --all-targets --locked: no new warnings (the same 4 pre-existing ones in platform/macos.rs, core.rs, lib.rs).
  • cargo build --locked and cargo build --release --locked on macOS: pass.
  • cargo check --target x86_64-pc-windows-gnu --locked --all-targets: passes with no warnings.
  • CI: macos-latest and windows-latest (msvc) both pass cargo test --locked and cargo build --release --locked.

Not tested

  • Real two-computer Mac↔Windows pairing, reconnecting, and AirDrop forwarding over the new handshake.
  • A live mix with a 0.6.2 computer. The rejection path is covered only by a unit test that sends the old opening message.
  • Local cargo check --target x86_64-pc-windows-msvc: it fails in ring's C build on this Mac (no MSVC toolchain), so that target is covered only by CI.

🤖 Generated with Claude Code

Pair with symmetric SPAKE2 on the pairing code and use an HKDF of its
key as the Noise pre-shared key, with the transcript as the Noise
prologue. Discovery advertises only the id, name and protocol version.
Pace failed pairing attempts per address with an overall budget and
draw codes evenly from the alphabet. Protocol version 6: both
computers need this version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@betoxf
betoxf marked this pull request as ready for review November 18, 2026 18:19
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant