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