Files
CustosPublic/docs/FACTORS.md
T

204 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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.
- At most one recovery slot (kind 5, core 0.17.0), of exactly 108 bytes, and none in a machine store.
- 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 | — |
## The recovery key (core 0.17.0)
A two-factor vault can have **one recovery key**: 32 random bytes that open the vault alone, for the day the
factors are gone (a forgotten password and a dead PC, a lost phone and a reinstalled Windows). It is the one
deliberate exception to "never one secret", and it is hedged accordingly:
- **It is made on request and returned once** (`Vault::recovery_create`). It is in no file and no log. The app
shows or prints it and keeps nothing. Making a new one replaces the old one.
- **It is never an everyday unlock.** `Vault::recover` is the only call that takes it. Before it returns the vault
it drops **every** factor, makes the new master password and one new second factor the vault's two factors, and
replaces the recovery key (the new one is returned, once). Whoever held the old password, an old PC key or a
phone holds nothing of the vault from the next save on. No `open` or `open_with` accepts a recovery key, as a
password or as material.
- **It is audited** in the security member: `recovery-created`, `recovery-removed`, `recovered`, each with the
key's id (never the key).
- A version-1 vault and a machine-key vault have none (`NeedTwoFactors`, `FactorRejected`).
- `recover` writes nothing. Until the caller saves, the file is as it was and the old factors still open it.
- The vault key stays the same across a recovery: a **copy of the file from before** still opens with the old
factors and the old recovery key. A recovery takes back the vault, not its old copies.
**As printed:** `id (4 bytes) ‖ key (32 bytes) ‖ check (4 bytes)` in base32 without padding, 64 characters in 16
groups of 4 (`A–Z`, `2–7`). The check is the first 4 bytes of `SHA-256("custos recovery key v1" ‖ id ‖ key)`.
A reader ignores case, spaces and hyphens; any other character, another length or a failed check is
`RecoveryKeyMistyped`, told apart from `WrongRecoveryKey` (a well-formed key that is not this vault's current one)
before the vault is touched. The **id** (8 hex characters) is not secret: it is in the header, shown in Settings
and printed next to the key, so the paper can be matched to the vault.
```
kind 5, recovery slot (at most one; never together with a single slot)
4 id
32 salt
24 wrap nonce
48 wrapped vault key KEK = HKDF-SHA256(key, salt, info "custos recovery v2")
AAD = magic ‖ version ‖ kind ‖ id ‖ salt ‖ nonce
```
It is not a factor: it has no factor slot, no pair slots and no entry in the body's `factors`, it does not count
toward the two factors or the limit of 8, and `factor_ids` does not list it. `Vault::recovery_id_of(bytes)` reads
its id from the header.
**Older readers.** A reader before core 0.17.0 knows slot kinds 2 to 4 and treats kind 5 as malformed: a vault
with a recovery key does not open in an older Custos ("the vault file is malformed"), and is not rewritten by
one. Removing the key (`recovery_remove`) makes the file readable by older readers again, apart from the three
audit actions, which they refuse like any unknown action (docs/FORMAT.md, "The security member").
**What it costs.** The paper is a complete way in. It is 256 bits, so it is not guessed; it is stolen, copied or
photographed. Keep it away from the PC and from the other vault's paper. Losing it loses nothing: make a new one.
## 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.