diff --git a/docs/FACTORS.md b/docs/FACTORS.md new file mode 100644 index 0000000..aef7ba2 --- /dev/null +++ b/docs/FACTORS.md @@ -0,0 +1,118 @@ +# Two factors: vault format version 2 + +A version-2 vault never opens with one secret. It opens with **any two of its factors**: the master +password, a key kept by one device, a share released by a TOTP check, a secret held by another device that +it releases after you approve, a passkey's PRF output. Each pair of factors wraps the vault key. + +Version 1 (`docs/FORMAT.md`) stays readable. A v1 vault becomes v2 when a second factor is added; from then +on, Custos writes it as v2 only. + +A **machine store** (the Claude Code store) is the one exception: a separate file whose vault key is +wrapped by one device key alone, so a program can open it unattended. It never holds the user's secrets. + +## Keys + +``` +factor material ──KDF──► factor KEK ──unwraps──► factor key F (32 random bytes, one per factor) + +F_a ‖ F_b ──HKDF-SHA256(salt = id_a ‖ id_b, info "custos pair v2")──► pair KEK ──unwraps──► vault key +``` + +- Every factor has a random 16-byte **id** and a random 32-byte **factor key** `F`. The factor's material + (the password, a device key, a released share…) only unwraps `F`. +- A **pair slot** for every pair of factors wraps the vault key with a key derived from both factor keys. + Two factors of the same vault always have a pair slot; one factor never unwraps the vault key by itself. +- The factor keys are also stored **inside the encrypted body**. An unlocked vault can therefore add a + factor (only the new factor's material is needed) or drop one, without the other factors present. + Changing the password rewraps the password factor's `F`; the pair slots stay as they are. + +Factor KEKs: + +| Type | Code | Material | KEK | +|:--|:--|:--|:--| +| password | 1 | the master password | Argon2id(password, salt, m, t, p), limits as in v1 | +| device | 2 | 32..=64 bytes the device keeps (a key file protected by Windows DPAPI, a key in the phone's storage) | HKDF-SHA256(material, salt, "custos factor v2") | +| totp | 3 | 32 bytes the TOTP service releases after a valid code | same | +| approval | 4 | 32 bytes another paired device sends after you approve there | same | +| passkey | 5 | a WebAuthn PRF output (32 bytes) | same | + +The type only tells the app which material to ask for; the cryptography is the same for 2..5. + +## Layout + +The v1 layout with `version = 2`, up to 64 slots and these slot kinds (`docs/FORMAT.md`, "Layout"): + +``` +kind 2, factor slot +16 factor id +1 type 1..=5 (table above) +13 kdf type 1 only: kdf(1) = 1 (Argon2id), m_cost, t_cost, p_cost (u32 each) +32 salt +24 wrap nonce +48 wrapped F 32-byte factor key + 16-byte tag +AAD = magic ‖ version ‖ kind ‖ everything in the payload before the wrapped F + +kind 3, pair slot +16 id a the smaller of the two ids (bytewise) +16 id b +24 wrap nonce +48 wrapped vault key +AAD = magic ‖ version ‖ kind ‖ id a ‖ id b ‖ nonce + +kind 4, single slot (machine stores only) +16 factor id +24 wrap nonce +48 wrapped vault key KEK = HKDF-SHA256(F, salt = id, info "custos single v2") +AAD = magic ‖ version ‖ kind ‖ id ‖ nonce +``` + +Kind 1 (the v1 password slot) does not occur in a v2 file; a reader treats one as malformed. The body AAD +is the whole header, as in v1. + +### Limits a reader enforces + +- 1 ..= 64 slots; at most 8 factor slots; a factor type outside 1..=5 is malformed. +- A pair slot whose two ids are equal, or not in order, or name a factor that has no factor slot, is malformed. +- Argon2 limits as in v1, checked before any work. +- Material for types 2..5: 32 ..= 64 bytes, else `FactorRejected` before any work. + +### Opening + +The app passes two factors (a password, or a type and its material). For each one, the reader tries the +factor slots of that type; the first that unwraps gives `(id, F)`. With two distinct ids it opens their pair +slot, then the body. + +| Situation | Error | +|:--|:--| +| Fewer than two different factors given, or two of the same factor | `NeedTwoFactors` | +| A given factor unwraps no factor slot of its type | `FactorRejected` (a wrong password says so: `WrongPassword`) | +| Both unwrap, but their pair slot is missing or fails | `Corrupted` | +| A machine store: its one device factor unwraps, then the single slot | — | + +## Body + +`"format": 2`, and a `factors` list next to `items`: + +```json +"factors": [ + { "id": "9f2c…(32 hex)", "type": "password", "label": "Master password", "created": 1793600000000, "key": "…(64 hex)" }, + { "id": "41aa…", "type": "device", "label": "MARK-PC", "created": 1793600000000, "key": "…" } +] +``` + +`key` is the factor key `F`. It is inside the body's encryption like every secret in the vault. + +### The key-pair item type + +`key-pair` (new in body format 2): `algorithm` (`ed25519`, `ecdsa-p256`, `rsa-3072`, `rsa-4096`), +`public-key` (OpenSSH line), `public-pem` (SPKI PEM), `private-key`* (PKCS#8 PEM), `private-openssh`* (OpenSSH +private key), `fingerprint` (SHA256:… as `ssh-keygen -l` prints it), `comment`. + +## What it protects + +- The vault file plus one factor opens nothing: a stolen password, a copied key file, a compromised TOTP + service or one lost phone each give one factor key, never the vault key. +- A device factor is only as private as the device: Windows DPAPI binds the key file to the Windows account + on that machine, so a copy of the file does not work elsewhere, but malware running as that account can + use it. That is one factor, which is why it is never enough alone. +- Losing a factor is not losing the vault: any other two open it, and the lost one can then be removed. diff --git a/docs/FORMAT.md b/docs/FORMAT.md index 1dc905d..f9818f2 100644 --- a/docs/FORMAT.md +++ b/docs/FORMAT.md @@ -1,5 +1,7 @@ # The `.custos` vault file, format version 1 +Version 2 (two factors, the machine store) is in `docs/FACTORS.md`; it reuses this layout. + One file holds one vault. Everything after the header is one XChaCha20-Poly1305 ciphertext. The header is not secret, but it is authenticated: changing any byte of it makes the body fail to open. All integers are little-endian. The code is `crates/custos-vault/src/format.rs`.