Skip to content

How it works

TonyD365-Agent edited this page Nov 21, 2026 · 3 revisions

How it works

The full threat model is in docs/design.md. This page explains what happens to one message.

Pairing (QR code)

  1. The host makes an offer: its public identity (64 bytes), its address, a 128-bit random secret and an expiry, as lanpair://pair?..., shown as a QR code. Scanning it is the trust step: nobody on the network can change what is on the host's screen.
  2. The device sends {"id": <its public identity>, "env": <envelope>} to that address. The envelope is sealed to the host's key; its encrypted body holds the secret.
  3. The host checks the envelope's signature against the identity in id, all the usual envelope checks, and then the secret in constant time. One device per offer; expiry; locked after 5 wrong answers; replayed requests refused.
  4. The host stores the device and replies with a sealed confirmation. Both now treat each other as paired peers.

Keys

Every device has one 32-byte seed. From it lanpair derives:

  • an Ed25519 key pair, to sign what it sends;
  • an X25519 key pair (via HKDF-SHA256), so others can encrypt to it.

The public identity is both public keys, 64 bytes. The device id is the first 16 hex characters of SHA-256 of the signing key.

The envelope (version 2)

Sealer.seal(recipient, body):

  1. Serialises body as canonical JSON.
  2. Makes a fresh X25519 key pair for this message only, does a key exchange with the recipient's X25519 key, and derives a 256-bit key with HKDF-SHA256.
  3. Encrypts with ChaCha20-Poly1305. The header (v, from, to, ts, seq, eph) is the associated data, so it cannot be changed without breaking decryption.
  4. Signs the header and the ciphertext with Ed25519.
{"ct":"…","eph":"…","from":"4f1c0a9e7b2d1c33","seq":1763683200123456,
 "sig":"…","to":"a87e5512c0de9b41","ts":1763683200.12,"v":2}

seq starts from the current time in microseconds and only goes up, so it keeps increasing across restarts of the sender without storing anything.

What Opener.open checks, in order

# Check Rejected as
1 Size ≤ max_size (1 MiB), before any parsing envelope too large
2 Valid JSON, not too deeply nested, all fields present malformed envelope
3 Version 2, no extra fields unsupported envelope
4 Field types (strings, integer seq, finite number ts) malformed envelope
5 Sender is a paired peer unknown sender
6 Ed25519 signature bad signature
7 Addressed to this device not addressed to this device
8 ts within ±max_skew (30 s) of the local clock stale or future timestamp
9 With MemorySeqStore: sent after the receiver started (+30 s) sent before this receiver started
10 seq higher than the last one accepted from this peer replayed message
11 Decryption cannot decrypt

Only when all pass is the sequence number stored and (peer, body) returned. Expensive and risky work happens as late as possible: nothing unauthenticated is decrypted, and nothing from an unknown sender reaches signature verification.

Replay protection across restarts

  • Sender restarts: time-based sequence numbers keep increasing.
  • Receiver restarts with JsonFileSeqStore: it remembers the last accepted number per peer (written atomically on every message), so old messages stay rejected.
  • Receiver restarts with the default MemorySeqStore: it forgets, so it refuses everything stamped earlier than 30 s after it started. Anything recorded before the restart is therefore refused, at the cost of 30 s of deafness after each start.

Forward secrecy (1.3+)

Without it, someone who records your traffic and later steals a device's seed could decrypt everything that was sent to that device. Host and Device prevent that:

  1. Each node holds X25519 session keys in memory only, never on disk or in the key store.
  2. It tells each peer its current session public key inside signed, encrypted control messages: hello when it starts, paired when pairing, key when the key changes.
  3. Peers encrypt to that session key instead of the long-term key. The envelope format is the same; the receiver tries its session keys, then its long-term key.
  4. A new session key starts every 10 minutes; the previous one is kept for one more period and then deleted. Messages sealed to a deleted key can't be decrypted again, not even with the seed. If someone takes the running device, at most the last ~20 minutes are exposed.
  5. If a message arrives sealed to a key the receiver no longer has (it restarted, or missed a key message), the receiver replies with its new key and that message's sequence number, and the sender seals the message again to the new key. Nothing is lost or delivered twice.

Peers on lanpair 1.2 or older never announce session keys and keep getting the long-term key. With the building blocks, use Opener.add_session_key() and Sealer.seal(..., box_key=...).

What lanpair does not hide

Who talks to whom, when, and how much.

Clone this wiki locally