brief: wire-core; consolidate the backlog into eight serial lanes

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5.1 committed 2026-09-16 00:02:11 -07:00
1 parent 5bb415c7bc
commit 9810ba441b
6 files changed
+295 -337

No files matched your search

-74
View File
@@ -1,74 +0,0 @@
# cipher-stream
## Goal
The two cryptographic primitives the wire needs, in `MWBCore` with
CommonCrypto and CryptoKit only: the magic-number hash of the security
key (`docs/protocol.md` §2.1) and the per-direction AES-256-CBC stream
with its cleartext salt+IV header and sacrificial first block (§2.2–2.3).
Streaming, allocation-light, and never able to leak key material into a
log. Wire compatibility with the Windows `Encryption.cs` is the whole
point; there is no "better" cipher choice to make here.
## Assumptions
- **CryptoKit cannot do CBC**, so AES goes through `CCCryptorCreate`
with `options = 0` (no padding, CBC) and `CCCryptorUpdate` per chunk.
Zero padding on the Windows side never emits bytes because every write
is a multiple of 16; we therefore do *no* padding at all and treat a
non-multiple-of-16 total as a stream error at close.
- **PBKDF2 goes through `CCKeyDerivationPBKDF`** with
`kCCPRFHmacAlgSHA512`, 100 000 rounds, 32 bytes. Password bytes are
UTF-8 of the key string — `Rfc2898DeriveBytes.Pbkdf2(string, …)` uses
UTF-8 (documented .NET behaviour).
- **The magic hash truncates each UTF-16 code unit to a byte**
(`(byte)st[i]`, `Encryption.cs:184`) and hashes a fixed 32-byte
buffer; keys longer than 32 characters contribute only their first 32.
The `<< 23` overlap is reproduced with `UInt32` wrapping arithmetic in
the same order as the C# expression.
- **Salt and IV are fresh per direction and never reused**
(`EncryptionTests.cs:59-68` documents the Windows expectation).
- **The sacrificial block is part of the cipher type's contract**: the
writer emits it right after the header, the reader discards the first
16 plaintext bytes. Callers never see it (topo.md handshake 2).
- A wrong key is not detectable here — CBC has no authentication. The
reader returns garbage plaintext and the framing layer's magic/checksum
rejects it, matching Windows (`InvalidKey` after >10 bad packets).
## Handshakes
### Promised (topo.md handshake 2, to control-channel and clipboard-bulk)
```swift
public enum MagicHash { public static func magicNumber(for key: String) -> UInt32 }
public struct CipherWriter {
public init(key: String, rng: RandomBytes = .system) throws // derives, builds header + sacrificial block
public mutating func header() -> Data // 32 bytes cleartext + 16 bytes ciphertext, once
public mutating func write(_ plaintext: Data) throws -> Data // ciphertext for the full blocks now available
}
public struct CipherReader {
public init(key: String)
public mutating func read(_ ciphertext: Data) throws -> Data // consumes header on first bytes; returns plaintext after the sacrificial block
}
```
`RandomBytes` is injectable so the lead can reproduce a stream by hand when checking against a Windows capture.
### Consumed
- Nothing from other lanes.
## Status / TODO
- [ ] `Sources/MWBCore/MagicHash.swift`: `magicNumber(for:)` per §2.1,
every step cited to `Encryption.cs:173-200`.
- [ ] `Sources/MWBCore/CipherStream.swift`: PBKDF2 key derivation,
`CipherWriter`, `CipherReader` (header, sacrificial block,
streaming CBC), cited to `Encryption.cs` and `Common.cs:1528-1558`.
- [ ] `docs/perf/README.md`: note the PBKDF2 cost per connection open.
## Work log
## QA log
## Review
-77
View File
@@ -1,77 +0,0 @@
# packet-codec
## Goal
The `DATA` packet and its framing as Swift value types in `MWBCore`,
byte-exact with `docs/protocol.md` §1: the 32/64-byte layout, the
`PackageType` numbering and big/small rule, the ASCII machine name, and
the magic + checksum framing that overwrites bytes 1–3. This is the
foundation every other lane reads bytes through; it has no I/O and no
crypto.
## Assumptions
- **Little-endian, natural alignment, no padding**, because the Windows
side marshals an explicit-layout struct (`DATA.cs:135-149`). All
offsets are taken from `docs/protocol.md` §1.1, not recomputed.
- **`Type` is one byte on the wire.** The Swift type stores `PackageType`
as `UInt8` at offset 0; `Framing.open` zeroes bytes 1–3 exactly as
`ProcessReceivedDataEx` does, so a decoded packet's bytes 1–3 are
always 0 and re-sealing is deterministic.
- **The checksum covers bytes 2..31 only, even on 64-byte packets.** We
reproduce this; the encryption layer, not the checksum, is what
protects the name half. Say so in a comment at the checksum loop.
- **Unknown type values decode as `.unknown(UInt8)`** rather than
throwing, so a newer Windows build cannot desynchronise our framing.
Size for an unknown value follows the big/small rule (`0x80` bit ⇒ 64,
else 32), which is what Windows would do.
- **Machine names are ASCII, ≤ 32, space-padded.** Non-ASCII input is an
error at construction time; on decode, bytes are trimmed of trailing
spaces and NULs (Windows trims spaces; NUL tolerance costs nothing).
- The magic number is a parameter of `Framing`, not computed here
(that is cipher-stream's `magicNumber(for:)`).
## Handshakes
### Promised (topo.md handshake 1, to session and every later consumer)
```swift
public enum PackageType: RawRepresentable { case hi, hello, … , unknown(UInt8); var rawValue: UInt8; var isBig: Bool }
public struct Packet {
public var type: PackageType; public var id: Int32; public var src: UInt32; public var des: UInt32
public var mouse: MouseData // X, Y, wheelDelta, flags at 16..31
public var keyboard: KeyboardData// dateTime at 16, vk at 24, flags at 28
public var nonce: (UInt32, UInt32, UInt32, UInt32)
public var postAction: UInt32
public var machineName: String? // nil on small packets
public var payload48: Data // bytes 16..63 for clipboard chunks
public init(type:) // zeroed buffer of the right size
public init?(bytes: Data) // 32 or 64 bytes, no framing check
public var bytes: Data // 32 or 64
}
public enum Framing {
public static func seal(_ p: Packet, magic: UInt32) -> Data
public static func open(_ bytes: Data, magic: UInt32) -> Packet // type == .invalid on bad framing
}
```
Exact spelling may change during the lane; the README is updated in the
same change and session codes against the README.
### Consumed
- Nothing. Foundation only.
## Status / TODO
- [ ] `Sources/MWBCore/Packet.swift`: `PackageType` (every value in
protocol §1.2, cited), `isBig`, `Packet` with the union views.
- [ ] `Sources/MWBCore/Framing.swift`: `seal` / `open` per §1.3, each
constant cited to `SocketStuff.cs`.
- [ ] `docs/protocol.md`: add any offset or rule learned while porting.
## Work log
## QA log
## Review
+104
View File
@@ -0,0 +1,104 @@
# wire-core
## Goal
The bytes of the protocol as Swift value types in `MWBCore`, byte-exact
with `docs/protocol.md` §1–§2: the 32/64-byte `DATA` packet and its
union views, the `PackageType` numbering and big/small rule, the ASCII
machine name, the magic + checksum framing that overwrites bytes 1–3,
the magic-number hash of the security key, PBKDF2 key derivation, and
the per-direction AES-256-CBC stream with its cleartext salt+IV header
and sacrificial first block. No I/O. Every later lane reads and writes
the wire through this code, and wire compatibility with the unmodified
Windows build is the whole point (CLAUDE.md invariant 1).
## Assumptions
- **Little-endian, natural alignment, no padding**, because the Windows
side marshals an explicit-layout struct (`DATA.cs:135-149`). Offsets
come from `docs/protocol.md` §1.1, not recomputed.
- **`Type` is one byte on the wire.** Stored as `UInt8` at offset 0;
`Framing.open` zeroes bytes 1–3 exactly as `ProcessReceivedDataEx`
does (`SocketStuff.cs:531-556`).
- **The checksum covers bytes 2..31 only, even on 64-byte packets**
(`SocketStuff.cs:510-513`). The cipher, not the checksum, protects
the name half. Say so in a comment at the loop.
- **Unknown type values decode as `.unknown(UInt8)`**, not an error, so
a newer Windows build cannot desynchronise framing; their size follows
the `0x80` rule.
- **Machine names are ASCII, ≤ 32, space-padded** (`Common.cs:397-405`,
`DATA.cs:75-94`); non-ASCII is a construction error; decode trims
trailing spaces and NULs.
- **CryptoKit cannot do CBC**, so AES is `CCCryptorCreate` with
`options = 0` (CBC, no padding) and `CCCryptorUpdate` per chunk. Zero
padding on Windows never emits bytes because every write is a multiple
of 16; we do no padding.
- **PBKDF2 is `CCKeyDerivationPBKDF`** with `kCCPRFHmacAlgSHA512`,
100 000 rounds, 32 bytes, password = UTF-8 of the key
(`Encryption.cs:107-115`).
- **The magic hash truncates each UTF-16 unit to a byte** and hashes a
fixed 32-byte buffer (`Encryption.cs:180-186`); the `<< 23` overlap
is reproduced with `UInt32` wrapping arithmetic in the C# order.
- **Salt and IV are fresh per direction**; the sacrificial block is
inside the cipher types' contract: the writer emits it after the
header, the reader discards the first 16 plaintext bytes
(`Common.cs:1528-1558`).
- **A wrong key is not detectable here** (no MAC). The reader returns
garbage and framing rejects it, matching Windows (`InvalidKey` after
more than 10 bad packets).
## Handshakes
### Promised (topo.md handshakes 1 and 2, to session-matrix, control-channel, clipboard)
```swift
public enum PackageType: RawRepresentable, Sendable { case hi, hello, …, unknown(UInt8); var rawValue: UInt8; var isBig: Bool }
public struct Packet: Sendable {
public var type: PackageType; public var id: Int32; public var src: UInt32; public var des: UInt32
public var mouse: MouseData // x, y, wheelDelta, flags at 16..31
public var keyboard: KeyboardData // dateTime at 16, vk at 24, flags at 28
public var nonce: (UInt32, UInt32, UInt32, UInt32)
public var postAction: UInt32
public var machineName: String? // nil on small packets
public var payload48: Data // bytes 16..63 for clipboard chunks
public init(type:) // zeroed buffer of the right size
public init?(bytes: Data) // 32 or 64 bytes, no framing check
public var bytes: Data // 32 or 64
}
public enum Framing {
public static func seal(_ p: Packet, magic: UInt32) -> Data
public static func open(_ bytes: Data, magic: UInt32) -> Packet // .invalid on bad framing
}
public enum MagicHash { public static func magicNumber(for key: String) -> UInt32 }
public enum SecurityKey { public static func isValid(_:) -> Bool; public static func random() -> String; public static func displayText(_:) -> String }
public struct CipherWriter { public init(key: String) throws; public mutating func header() -> Data; public mutating func write(_ plaintext: Data) throws -> Data }
public struct CipherReader { public init(key: String); public mutating func read(_ ciphertext: Data) throws -> Data }
```
Exact spelling may change during the lane; this README is updated in
the same change and later lanes code against it.
### Consumed
- Nothing. Foundation, CommonCrypto, CryptoKit only.
## Status / TODO
- [ ] `Sources/MWBCore/Packet.swift`: `PackageType` (every value §1.2,
cited), `isBig`, `Packet` with union views, machine name rules.
- [ ] `Sources/MWBCore/Framing.swift`: `seal` / `open` per §1.3, each
constant cited to `SocketStuff.cs`.
- [ ] `Sources/MWBCore/MagicHash.swift`: `magicNumber(for:)` §2.1 cited
to `Encryption.cs:173-200`; `SecurityKey` rules §2.4
(`Encryption.cs:207-242`, `:65-79`).
- [ ] `Sources/MWBCore/CipherStream.swift`: PBKDF2, `CipherWriter`,
`CipherReader` (header, sacrificial block, streaming CBC), cited to
`Encryption.cs` and `Common.cs:1528-1558`; the CommonCrypto wrapper
lives in this one file.
- [ ] `docs/protocol.md`: add anything learned while porting, cited.
## Work log
## QA log
## Review
+10 -13
View File
@@ -147,23 +147,20 @@ carry into the next brief.
## 6. Lane order
Strictly one at a time, in this order (from `docs/topo.md` §3):
Strictly one at a time, in this order (mirrors `TODO.md`):
| # | lane | status |
|---|---|---|
| 0 | bootstrap | merged 2026-09-15 (lead, solo) |
| 1 | packet-codec | brief written |
| 2 | cipher-stream | brief written |
| 3 | session | |
| 4 | matrix-edge | |
| 5 | control-channel | |
| 6 | event-tap | |
| 7 | screens-cursor | |
| 8 | input-inject | |
| 9 | menubar-app | — first user-testable milestone: two Macs, or Mac ↔ Windows, mouse and keyboard |
| 10 | clipboard-inband | |
| 11 | clipboard-bulk | |
| 12 | end-to-end polish | driven by the user's testing findings |
| 1 | wire-core | brief written |
| 2 | session-matrix | |
| 3 | control-channel | |
| 4 | input-capture | |
| 5 | input-inject | |
| 6 | app-shell | first user-testable build: menu bar + settings window + mouse/keyboard sharing |
| 7 | clipboard | |
| 8 | extras | screensaver blocking, fullscreen Easy Mouse rule, drawn cursor |
| 9 | polish | driven by the user's testing |
## 7. Escalations — things only the user can do
+45 -56
View File
@@ -53,24 +53,24 @@ Standing promises:
| file | lane | consumers |
|---|---|---|
| `Packet.swift`, `Framing.swift` | packet-codec | session, matrix-edge, clipboard-inband, control-channel, bulk |
| `MagicHash.swift`, `CipherStream.swift` | cipher-stream | session (magic), control-channel, clipboard-bulk |
| `Session.swift` | session | control-channel |
| `Matrix.swift`, `Edge.swift` | matrix-edge | mwb, event-tap (edge), input-inject (coords) |
| `KeyMap.swift` | event-tap | input-inject |
| `ClipboardCodec.swift` | clipboard-inband | clipboard-bulk (header format only) |
| `Settings.swift` | menubar-app | everyone via `mwb` |
| `Packet.swift`, `Framing.swift` | wire-core | session, session-matrix, clipboard, control-channel, bulk |
| `MagicHash.swift`, `CipherStream.swift` | wire-core | session-matrix (magic), control-channel, clipboard |
| `Session.swift` | session-matrix | control-channel |
| `Matrix.swift`, `Edge.swift` | session-matrix | mwb, input-capture (edge), input-inject (coords) |
| `KeyMap.swift` | input-capture | input-inject |
| `ClipboardCodec.swift` | clipboard | clipboard (header format only) |
| `Settings.swift` | app-shell | everyone via `mwb` |
`MWBMac` files owned at planning time:
| file | lane | note |
|---|---|---|
| `Permissions.swift` | bootstrap → **event-tap** | preflight shipped by bootstrap; event-tap adds `request…` and owns the file from wave 3 |
| `EventTap.swift` | event-tap | |
| `Permissions.swift` | bootstrap → **input-capture** | preflight shipped by bootstrap; input-capture adds `request…` and owns the file from wave 3 |
| `EventTap.swift` | input-capture | |
| `Injector.swift` | input-inject | defines the injected-event tag (handshake 5) |
| `Screens.swift`, `Cursor.swift` | screens-cursor | |
| `Pasteboard.swift` | clipboard-inband | |
| `StatusItem.swift` | menubar-app | |
| `Screens.swift`, `Cursor.swift` | input-capture | |
| `Pasteboard.swift` | clipboard | |
| `StatusItem.swift` | app-shell | |
`MWBNet/Placeholder.swift` is deleted by control-channel.
@@ -82,26 +82,25 @@ lead's; a lane needing a change there files a Handshake request.
### 2.1 Lane order (sequencing, `─▶`)
```
bootstrap ─▶ packet-codec ─▶ cipher-stream ─▶ session ─▶ matrix-edge
─▶ control-channel ─▶ event-tap ─▶ screens-cursor ─▶ input-inject
─▶ menubar-app (first user-testable build)
─▶ clipboard-inband ─▶ clipboard-bulk ─▶ end-to-end polish
bootstrap ─▶ wire-core ─▶ session-matrix ─▶ control-channel
─▶ input-capture ─▶ input-inject ─▶ app-shell (first user-testable build)
─▶ clipboard ─▶ extras ─▶ polish
```
### 2.2 Consumption edges (`──▶`)
| consumer | consumes | what |
|---|---|---|
| session | packet-codec | `Packet`, `PackageType`, `Framing` |
| session | cipher-stream | `magicNumber(for:)` only |
| matrix-edge | packet-codec | `Packet` (Matrix, NextMachine, Mouse encode) |
| control-channel | session, cipher-stream, packet-codec | everything above the socket |
| event-tap | matrix-edge | `Edge` decisions on captured moves |
| input-inject | event-tap (KeyMap), matrix-edge (de-normalization), screens-cursor (desktop bounds) | |
| screens-cursor | matrix-edge | rectangle definitions |
| clipboard-inband | packet-codec | 64-byte chunk packets |
| clipboard-bulk | cipher-stream, packet-codec, clipboard-inband | stream layout, `DATA`, header/text format |
| menubar-app | every `MWBCore` type, control-channel, event-tap, screens-cursor | wiring |
| session-matrix | wire-core | `Packet`, `PackageType`, `Framing` |
| session-matrix | wire-core | `magicNumber(for:)` only |
| session-matrix | wire-core | `Packet` (Matrix, NextMachine, Mouse encode) |
| control-channel | session, wire-core, wire-core | everything above the socket |
| input-capture | session-matrix | `Edge` decisions on captured moves |
| input-inject | input-capture (KeyMap), session-matrix (de-normalization), input-capture (desktop bounds) | |
| input-capture | session-matrix | rectangle definitions |
| clipboard | wire-core | 64-byte chunk packets |
| clipboard | wire-core, wire-core, clipboard | stream layout, `DATA`, header/text format |
| app-shell | every `MWBCore` type, control-channel, input-capture, input-capture | wiring |
| end-to-end | all | |
### 2.3 Handshakes (`◀──▶`) — all open at planning time
@@ -111,56 +110,46 @@ Numbered so lanes can cite them. With serial lanes a handshake is simply
list still matters because it names the APIs the lead checks at
inspection.
1. **packet-codec ◀──▶ session**: the `Packet` API shape (value type
with typed union views, `Data` in/out). packet-codec proposes in its
1. **wire-core ◀──▶ session**: the `Packet` API shape (value type
with typed union views, `Data` in/out). wire-core proposes in its
README; session consumes as published. Closed when session lands.
2. **cipher-stream ◀──▶ control-channel**: streaming `write/read`
2. **wire-core ◀──▶ control-channel**: streaming `write/read`
returning `Data` with internal partial-block buffering; header and
sacrificial block handled inside the cipher types, not by the
channel. Closed when control-channel lands.
3. **session ◀──▶ control-channel**: `Session.feed(_:) -> [Event]`,
`Session.outgoing() -> Data`, injected `Clock`. The channel never
inspects packet bytes.
4. **matrix-edge ◀──▶ event-tap**: `Edge.evaluate(point:rect:now:)`
4. **session-matrix ◀──▶ input-capture**: `Edge.evaluate(point:rect:now:)`
returns a `Switch?` decision. Debounce lives in `Edge` with the
injected clock; the tap only supplies points and timestamps.
5. **event-tap ◀──▶ input-inject**: the injected-event tag value
5. **input-capture ◀──▶ input-inject**: the injected-event tag value
(`eventSourceUserData`) is one constant defined in `MWBMac/Injector.swift`
and read by `EventTap.swift`.
6. **screens-cursor ◀──▶ input-inject**: `Screens.desktopBounds` and
6. **input-capture ◀──▶ input-inject**: `Screens.desktopBounds` and
`primaryBounds` in CG coordinates (top-left origin, points).
7. **clipboard-inband ◀──▶ clipboard-bulk**: the `"<len>*<name>"`
7. **clipboard ◀──▶ clipboard**: the `"<len>*<name>"`
header codec and the text tag/SEP codec live in `ClipboardCodec.swift`
and are reused by bulk unchanged.
8. **menubar-app ◀──▶ everyone**: `Settings` is the one struct passed
8. **app-shell ◀──▶ everyone**: `Settings` is the one struct passed
down; lanes read fields, never the file.
## 3. Lane order
Lanes run **one at a time** (CLAUDE.md, the user's standing
instruction), so this section is an order, not a parallelism proof. The
order follows the consumption edges in §2.2: nothing is started before
everything it consumes is merged.
Lanes run **one at a time** (CLAUDE.md), so this is an order, not a
parallelism proof. Nothing starts before everything it consumes is merged.
| # | lane | consumes (must be merged first) |
| # | lane | consumes |
|---|---|---|
| 1 | packet-codec | bootstrap |
| 2 | cipher-stream | bootstrap |
| 3 | session | packet-codec, cipher-stream |
| 4 | matrix-edge | packet-codec |
| 5 | control-channel | session, cipher-stream, packet-codec |
| 6 | event-tap | matrix-edge |
| 7 | screens-cursor | matrix-edge |
| 8 | input-inject | event-tap (KeyMap), matrix-edge, screens-cursor |
| 9 | menubar-app | all of the above — the first build the user can test |
| 10 | clipboard-inband | packet-codec, menubar-app (Settings) |
| 11 | clipboard-bulk | cipher-stream, packet-codec, clipboard-inband |
| 12 | end-to-end polish | everything; driven by the user's testing |
`interop-vectors` is no longer a lane: there are no test suites. If a
Windows machine becomes available, the lead verifies protocol facts by
hand against a capture and flips `[transcribed]` marks in
`docs/protocol.md`.
| 1 | wire-core | bootstrap |
| 2 | session-matrix | wire-core |
| 3 | control-channel | session-matrix, wire-core |
| 4 | input-capture | session-matrix (Edge, coordinates) |
| 5 | input-inject | input-capture (KeyMap, Screens, injected tag), session-matrix |
| 6 | app-shell | all of the above |
| 7 | clipboard | wire-core, control-channel, app-shell (Settings) |
| 8 | extras | app-shell, session-matrix |
| 9 | polish | everything |
`TODO.md`, `docs/protocol.md` and `docs/perf/README.md` are
shared-by-nature: append in your own section only.