diff --git a/design/README.md b/design/README.md index d3840c7..e586f8f 100644 --- a/design/README.md +++ b/design/README.md @@ -17,6 +17,10 @@ each time. - [`native-mirror.md`](native-mirror.md) — *implemented in #217*: one planner shared by every layout, a small transport, and every `local` and plain `sftp` destination written without rclone. +- [`native-crypt.md`](native-crypt.md) — *proposed*: age encryption through + a per-destination data key that an agent key or a required recovery kit + opens, rclone crypt removed, packed as the one content layout, and seekable + packs. ## How to use this folder diff --git a/design/native-crypt.md b/design/native-crypt.md new file mode 100644 index 0000000..2a67c92 --- /dev/null +++ b/design/native-crypt.md @@ -0,0 +1,769 @@ +# Native encryption: age, one content layout, and seekable packs + +*Proposed.* This is step 3 of replacing rclone with squirrel's own code. +Peer sync dropped rclone in #210, #211 made the mirror's evidence honest, and +[native-mirror.md](native-mirror.md) (#217) moved every `local` and plain +`sftp` destination onto squirrel's transport. What still needs rclone is +encryption, and the s3, b2 and gcs backends. This design removes rclone's crypt +overlay and encrypts natively on `local` and `sftp`. The S3 transport follows +it on its own branch; until it lands, encrypted s3, b2 and gcs destinations are +refused. + +## Summary + +- **One content layout.** The content-addressed layout is retired: it was the + packed layout with every file stored as an object, which `pack_threshold = 0` + still gives. +- **Encryption belongs to the packed layout.** A `crypt` block is accepted on + a packed destination and refused on a mirror. An + encrypted mirror needs a tool to read any file, yet its tree discloses every + path, size and modification time. It also can't gate offload. +- **Squirrel encrypts every artifact itself, in the + [age](https://c2sp.org/age) format** (`age-encryption.org/v1`), to hybrid + post-quantum recipients (ML-KEM-768 with X25519). Recovery without squirrel + needs the stock `age` command, zstd, tar and a BLAKE3 tool. +- **Two keys you hold, two keys the destination holds.** The *agent identity* + lives on the machine that pushes; the *recovery kit* is printed once and kept + offline. Either one opens `.squirrel-keys`, an encrypted file on the + destination holding its random *data identity* and *naming key*. Every other + file is encrypted to the data identity. +- **Changing a key rewrites one small file.** `destination rekey` re-encrypts + `.squirrel-keys` to the new keys and starts a new data generation for new + files. No artifact is rewritten or uploaded again, and every file, old and + new, opens with the new keys. +- **Encrypted destinations are `local` and `sftp`, written natively.** rclone + crypt is gone, and rclone never handles an encrypted destination. Encrypted + s3, b2 and gcs destinations are refused at config load until the S3 transport + lands. +- **Packs become seekable.** A pack is written as a run of zstd frames of about + 1 MiB. The placement map records each member's frame, so restoring one file + reads one frame instead of the whole pack. Stock `zstd -d | tar -x` still + reads a pack end to end. +- **Objects are padded** with Padmé before encryption, so a large file's size + shows only to within a few percent. It costs about 1% of storage on a + media-heavy archive. + +## Why not rclone's format + +rclone crypt did the job, but squirrel inherited its limits: + +- **Truncation goes unnoticed.** The format has no end-of-file marker, so a + file cut at a 64 KiB block boundary decrypts cleanly as a shorter file. + Objects are saved by squirrel's BLAKE3 check on restore; manifest segments, + placement maps and snapshots only by parsing. +- **One key for everything, derived from a password** with fixed scrypt + parameters. Anyone holding the ciphertext can guess passwords offline, and a + password change means re-encrypting every file. +- **One implementation.** The format is specified by rclone's documentation and + read by rclone. +- **Source modification times leak.** rclone carries each file's mtime onto + the stored copy. + +age fixes each of these: + +- It has a frozen, published specification, test vectors, several independent + implementations, and a command-line tool in most distributions. +- Its payload is ChaCha20-Poly1305 in 64 KiB chunks, with a fresh key per file + and a flagged last chunk, so truncation fails authentication. +- ChaCha20 is fast on NAS processors without AES instructions. +- It encrypts each file to any number of recipients. +- Since v1.3 it has post-quantum recipients. Backups are the data a + "record now, decrypt later" attacker wants. + +The key layout in section 2 fixes the password: every key is random, and +changing one rewrites a single small file. + +Considered and rejected: libsodium's secretstream in a format of squirrel's own +(better context binding, but squirrel would own a format and its recovery tool), +Tink streaming encryption (keyset machinery built for services, no general +decrypt command), OpenPGP (GnuPG and RFC 9580 modes don't interoperate, large +attack surface), and staying byte-compatible with rclone crypt (the weaknesses +above). Kopia bundles content addressing, packing and encryption the same way +this design does; squirrel keeps what kopia doesn't offer: recovery with stock +tools, and BLAKE3 evidence that gates offload. + +## Scope + +**In scope:** + +- retiring the content-addressed layout; +- the `crypt` block's new shape, and its refusal on mirrors, kopia, s3, b2 and + gcs; +- the encryption layer in the transport's artifact store; +- keys: generation, the recovery kit, `.squirrel-keys` and its copies, + recipient pinning, and rekeying; +- secrets in config: every secret field takes `{ file = … }` or + `{ env = … }`, never a literal; +- the names derived from the naming key; +- evidence, verify, restore and recover on encrypted destinations; +- the recovery drill, and a current drill as a condition for offload credit; +- Padmé padding of objects; +- seekable packs, encrypted or not, with ranged reads on `local` and `sftp`; +- the recovery procedure without squirrel. + +**Out of scope:** + +- **The S3 transport.** It follows on its own branch. Until it lands, config + load refuses `crypt` on s3, b2 and gcs, and rclone-backed packed destinations + keep streaming packs end to end. +- **Mirrors, kopia, and peer sync.** An encrypted mirror is refused; kopia + encrypts its own repository; peer sync runs between machines that hold the + plaintext anyway. + +In the [reference setup](reference-setup.md), `cloudbox` becomes an encrypted +packed destination written natively over sftp and verified by its server's +`sha256sum`. `s3archive` can't be encrypted until the S3 transport lands. + +## What changes for the operator + +- **The `crypt` block names keys, not passwords:** + + ```toml + [destinations.offsite.crypt] + identity = { file = "~/.squirrel/keys/agent-20260929T101500Z" } # written by keygen + recovery = "age1pq1…" # public; its private half is the recovery kit + ``` + + `password`, `password2` and `obscured` are gone. Several destinations may + name the same keys; each still gets its own data identity and naming key. +- **`squirrel destination keygen`** creates both keys. It writes the agent + identity to a key file, shows the recovery kit, asks for it back to prove it + was stored, and prints the block above. `--agent` creates a new agent + identity only, and `--recovery` a new kit only, for replacing one key and + keeping the other. +- **No secret is written into the config file.** Every secret field (the + `crypt` identity, sftp and kopia passwords, bucket credentials, agent and + peer tokens) takes `{ file = … }` or `{ env = … }`, and a literal is refused + at config load (section 8). +- **An encrypted destination gates offload only while its drill is + current.** From the first push until the first drill, and whenever the + monthly drill is overdue, it earns no offload credit (section 5). +- **Encrypted mirrors are refused** at config load, with a message naming the + packed layout. +- **Encrypted s3, b2 and gcs destinations are refused** at config load, with a + message naming the S3 transport they wait for. +- **`layout = "content-addressed"` is refused** at config load, naming + `layout = "packed"` with `pack_threshold = 0` as the way to keep an object + per file. +- **`local` accepts `crypt`,** so an encrypted USB disk works. +- **Nothing old is read.** There is no reader for rclone's crypt format or for + single-frame packs; nothing uses them yet. +- **Recovery without squirrel** needs age v1.3 or newer (older clients need the + `age-plugin-pq` compatibility plugin), zstd, tar, and BLAKE3 (`b3sum`, or + Python's `blake3` package). The procedure is in + [formats](../docs/src/content/docs/reference/formats.md). +- **An encrypted destination discloses less:** no source modification times, + and no path on any layout. Object sizes show only to within a Padmé bucket. + Counts and timing stay visible (section 1). + +## 1. What an encrypted destination holds + +Every file squirrel writes under an encrypted root is an age file. Names are +fixed, keyed, or carry only a run id: + +``` +/ + .squirrel-keys # to agent + kit: naming key, data identities, recipients + objects/ # to the data identity: the content, padded + packs/ # to the data identity: zstd frames of a tar + packs/map- # to the data identity: placement map (JSONL) + /.squirrel-keys # a byte-identical copy of the root's + /.squirrel-volume # to the data identity: volume marker + /index/run- # to the data identity: manifest segment (JSONL) + /.squirrel-index/index-