CLAUDE.md gains a 'How this project is developed' section: strictly serial Dev (Opus 5) -> QA (Sonnet 5, code quality) -> lead inspection; commit along the way without requiring every commit to compile; the user is the tester. docs/loop.md, docs/topo.md and TODO.md are rewritten to a serial lane order with no test-writing items; the test targets are removed and tools/gates.sh is lint + layering grep + build. Co-Authored-By: Claude Fable 5.1 <[email protected]>
75 lines
3.3 KiB
Markdown
75 lines
3.3 KiB
Markdown
# 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
|