[seal] Sealed boxes and key import: custos-seal (X25519 + XChaCha20-Poly1305, match code, at-rest sealing), custos_keys::import, Vault::factor_ids; v0.5.0

This commit is contained in:
2026-11-02 18:05:00 +01:00
parent 7893dd199c
commit 7a7a628fa5
16 changed files with 566 additions and 25 deletions
+6
View File
@@ -89,6 +89,12 @@ slot, then the body.
| Both unwrap, but their pair slot is missing or fails | `Corrupted` |
| A machine store: its one device factor unwraps, then the single slot | — |
## Which factors a file has
`Vault::factor_ids(bytes)` reads the header alone and returns each factor's id and type. No key is needed,
because ids and types are not secret. The app uses them to ask the TOTP service or a paired device for a
factor. Those services pass material through a mailbox they cannot read; see `docs/SEAL.md`.
## Body
`"format": 2`, and a `factors` list next to `items`:
+23
View File
@@ -36,3 +36,26 @@ Each gives:
- `fingerprint`: `SHA256:…`, exactly as `ssh-keygen -l` prints it.
The crates are RustCrypto's: `ssh-key`, `ed25519-dalek` (its Ed25519 backend), `p256`, `rsa`.
## Importing (`custos_keys::import`)
An existing private key becomes the same kind of item. Accepted formats:
| Input | Header | Algorithms |
|:--|:--|:--|
| OpenSSH private key | `BEGIN OPENSSH PRIVATE KEY` | Ed25519, ECDSA P-256, RSA |
| PKCS#8 | `BEGIN PRIVATE KEY` | Ed25519, ECDSA P-256, RSA |
| PKCS#1 | `BEGIN RSA PRIVATE KEY` | RSA |
| SEC1 | `BEGIN EC PRIVATE KEY` | ECDSA P-256 |
- **The public line, the PEMs and the fingerprint are derived from the private key**, never copied from the
input. A pasted public key therefore cannot disagree with its private key.
- **RSA:** 2048 to 4096 bits. An imported 2048-bit key is named `rsa-2048`; Custos never generates one.
- **A passphrase-protected key is refused** (`Encrypted`): OpenSSH with a cipher, `ENCRYPTED PRIVATE KEY`,
or PKCS#1 with `Proc-Type: 4,ENCRYPTED`. Remove the passphrase first (`ssh-keygen -p`); the vault is the
key's encryption.
- **Comment:** an OpenSSH key keeps its own comment; the other formats take the item's name.
- **Line ends:** CRLF and surrounding whitespace are accepted, as in a paste or a Windows file.
- **Errors never repeat the input.**
- **ssh-key 0.6.7 conversion bug:** its `TryFrom<&RsaKeypair> for rsa::RsaPrivateKey` uses `p` twice. Custos
rebuilds the key from `n, e, d, p, q` and validates it instead.
+56
View File
@@ -0,0 +1,56 @@
# Sealed boxes (`custos-seal`)
The two-factor services (`docs/FACTORS.md`, types 3 and 4) pass factor material through a mailbox on the
home server. The mailbox must never be able to read it. So the device that wants a secret makes a
**one-time X25519 key pair**, sends only the public half, and whoever releases the secret seals it to that
key. The same box carries a secret from the user's PC to a Claude Code cloud session.
## The box, version 1
```
sealed = "CSL1" ‖ E (32) ‖ nonce (24) ‖ XChaCha20-Poly1305(K, nonce, plaintext, aad)
E the sender's ephemeral X25519 public key (a fresh one per box)
R the recipient's one-time public key
K HKDF-SHA256(ikm = X25519(e, R), salt = E ‖ R, info = "custos seal v1" ‖ context)
aad "CSL1" ‖ E ‖ context
```
- **`context`** names the purpose, for example `approval:<factor id>:<request id>` or `cloud:<request id>`.
A box opens only with the context it was sealed for, so a box for one request does not work as another.
- **A low-order public key is refused** on both sides: the shared secret must be contributory.
- **Plaintext limit:** 32 KiB.
- **The recipient's secret half** lives only in memory, as `x25519_dalek::StaticSecret` with `zeroize`,
and is wiped on drop. Nothing writes it to disk.
## The match code
Six digits that the requesting screen and the approving screen both show:
```
match = u32_be(SHA-256("custos match v1" ‖ R)[0..4]) mod 1 000 000, zero-padded to 6 digits
```
If they differ, someone else's request is waiting: deny it. The code is not a secret. It only ties the
approval to the key the box is sealed for.
Test vector: R = `07` × 32 → `684361`.
## At rest
The factor service keeps each TOTP share sealed with its own key, which comes from a systemd credential
that is not in its state directory:
```
at rest = "CSR1" ‖ nonce (24) ‖ XChaCha20-Poly1305(HKDF-SHA256(key, salt = "", info = "custos rest v1" ‖ context),
nonce, plaintext, "CSR1" ‖ context)
```
What this protects: a leaked copy of `/var/lib/custos-factors` without the credential gives nothing.
What it does not protect: anyone who controls the running server. That is one factor, which is why a
factor is never enough alone.
## Crates
`x25519-dalek` 2 (dalek-cryptography, the X25519 sibling of the `ed25519-dalek` used for key pairs),
`chacha20poly1305`, `hkdf`, `sha2`, and `rand`'s `SysRng`.
+6
View File
@@ -58,6 +58,12 @@ just as plainly, what it does not.
revealed or copied value passes through JavaScript strings, which cannot be wiped. Lock (or close the tab)
when done.
- **The factor service holds one factor.** It releases the TOTP factor after a valid code, so whoever
controls the home server has that factor. It is one factor, never enough alone. Approval material and
cloud secrets pass through it only as sealed boxes (`docs/SEAL.md`), which it cannot open.
- **An approval is only as careful as the person approving.** Compare the six-digit match code on both
screens; a request you did not start is someone else's.
## Design rules that follow from this
- No custom cryptography: Argon2id (`argon2`), XChaCha20-Poly1305 (`chacha20poly1305`), the OS CSPRNG