[vault] The recovery key: one per two-factor vault, returned once, taken only by Vault::recover, which replaces every factor and the key (slot kind 5, three audit actions); v0.17.0

This commit is contained in:
2026-11-12 13:24:00 +01:00
parent 61b98ee9b0
commit 8d5d08afcb
9 changed files with 616 additions and 14 deletions
+49
View File
@@ -79,6 +79,7 @@ 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.
@@ -96,6 +97,54 @@ 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 | — |
## 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,