Files
CustosPublic/docs/FACTORS.md

155 lines
8.1 KiB
Markdown

# 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.
**Taking over a machine-key vault** (core 0.13.0, Custos's hands-off vault): `Vault::take_over(password)` adds the
master password as a factor paired with the device key and removes the single slot, in one step. From then on it is
an ordinary version-2 vault: two factors open it, the device key alone does not, and factors are added as for any
vault (a phone, a TOTP backup). Only a machine-key vault is taken over, and only once; `opens_with_machine_key()`
says which it is. The take-over also ticks `no-auto-connect`, `no-unattended` and `card-signing` and writes
`taken-over` (FORMAT.md, "The security member").
## 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 | — |
## 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`.
## In the browser and on a phone
`custos-wasm` opens a version-2 vault with any two factors (`open_with`). A factor that comes from the
factor service or from another device arrives as a box sealed to a one-time key the module made
(`recipient`); the key's secret half never leaves WebAssembly memory and is used once.
**Pairing a phone** gives the vault two factors at once, both added by the PC (which writes the file):
1. The PC waits with a one-time key and shows its match code; the phone shows the same code.
2. On the phone, a passkey is created and evaluated with the WebAuthn PRF extension
(salt `SHA-256("custos passkey v1")`). Its 32-byte output is the **passkey** factor's material.
3. `phone_enrol` makes 32 random bytes, the phone's **approval** material, and seals both to the PC
(context `pair:<pairing id>`). The phone keeps the approval material only wrapped with the PRF output
(at-rest sealing, context `custos phone approval v1`), so stolen browser storage is not a factor.
4. The phone shows the box's sender code (`docs/SEAL.md`); the PC adds the factors only after you confirm it
shows the same code. It adds the approval factor **apart** from the passkey (`add_factor_apart`).
**Factors on one device never pair.** `add_factor_apart` writes no pair slot between the new factor and the
ones named, so those two open nothing together (`Error::SameDevice`) while each still pairs with every other
factor. A phone's passkey and its approval key, or a PC's device key and its approval key, are added apart.
`phone_approve` unwraps the approval material with a fresh PRF output (Face ID, Touch ID) and seals it to a
requesting device, for `approval:<factor id>:<request id>`. The material exists unwrapped only inside that call.
## 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.