The recovery key (v0.17.0) #21

Merged
mnovak merged 2 commits from feature/recovery-key into main 2026-11-12 23:09:32 +01:00
Owner

The recovery key: one long random key per two-factor vault, for the day the factors are gone. Design and format: docs/FACTORS.md, "The recovery key".

What

  • Vault::recovery_create(now) makes the key and returns it once (RecoveryKey: an id and 8 groups of 8 base32 characters with a check). It is in no file and no log. A new key replaces the old one.
  • Vault::recover(bytes, key, new_password, kdf, second, label, now) is the only call that takes it. Before it returns the vault it drops every factor, makes the new password and one new second factor the vault's two factors, and replaces the recovery key. No open or open_with accepts a recovery key.
  • Vault::recovery_remove, Vault::recovery_id, Vault::recovery_id_of(bytes).
  • Header: slot kind 5 (id, salt, nonce, wrapped vault key), at most one, never in a machine store. It is not a factor.
  • Security audit: recovery-created, recovery-removed, recovered, with the key's id.
  • Errors RecoveryKeyMistyped (what was typed is not a key), WrongRecoveryKey, NoRecoveryKey.
  • A vault without a recovery key is read and written exactly as before. A vault with one is malformed to a reader before 0.17.0.
  • Version 0.17.0.

Tests

  • Workspace: 251 passed, 0 failed (241 before): tests/recovery.rs 8, unit tests in recovery.rs 2.
  • The tests were written before the code but passed on its first run, so they were not seen red that way. Instead nine bugs were put back one at a time (old factors kept after a recovery, the check skipped, a key for a machine store, for a version-1 vault, two recovery slots accepted, a password as the second factor, no audit on remove, no audit on recover, a second key not replacing the first): each failed at least one test. That run was on the first commit; the second commit only changes how the key is grouped in print.

Self-review

  1. Accepted, documented: the vault key is not changed by a recovery, so a copy of the file from before still opens with the old factors and the old key. A recovery takes back the vault, not its copies.
  2. Accepted, documented: recover writes nothing. A caller that uses the returned vault without saving has changed nothing on disk; the desktop must save before it shows anything.
  3. Accepted: the id is in the header in the clear. It is 4 random bytes and says nothing about the key.
  4. Minor, stated: the check that the id in the typed key matches the slot's id is not covered by a test of its own: without it a wrong key is still refused by the wrap. It only makes the answer quicker.
  5. Minor, stated: a test's comment claimed to forge a key with the right id; it changes the last character, which the check catches. The comment now says so. No test builds a well-formed key with this vault's id and other bytes (the encoder is not public).
  6. Not done here: no limit on wrong tries in the core (a file is guessed offline anyway; the key is 256 bits). A growing wait after wrong keys belongs to the app.
  7. cargo fmt also reformatted one line in tests/factors.rs.

Not verified

  • No app uses this yet: the Recover screen comes in Custos.
  • custos-wasm builds with it (workspace tests), but the web vault has no way to recover.
The recovery key: one long random key per two-factor vault, for the day the factors are gone. Design and format: `docs/FACTORS.md`, "The recovery key". ## What - `Vault::recovery_create(now)` makes the key and returns it once (`RecoveryKey`: an id and 8 groups of 8 base32 characters with a check). It is in no file and no log. A new key replaces the old one. - `Vault::recover(bytes, key, new_password, kdf, second, label, now)` is the only call that takes it. Before it returns the vault it drops every factor, makes the new password and one new second factor the vault's two factors, and replaces the recovery key. No `open` or `open_with` accepts a recovery key. - `Vault::recovery_remove`, `Vault::recovery_id`, `Vault::recovery_id_of(bytes)`. - Header: slot kind 5 (id, salt, nonce, wrapped vault key), at most one, never in a machine store. It is not a factor. - Security audit: `recovery-created`, `recovery-removed`, `recovered`, with the key's id. - Errors `RecoveryKeyMistyped` (what was typed is not a key), `WrongRecoveryKey`, `NoRecoveryKey`. - A vault without a recovery key is read and written exactly as before. A vault with one is malformed to a reader before 0.17.0. - Version 0.17.0. ## Tests - Workspace: **251 passed, 0 failed** (241 before): `tests/recovery.rs` 8, unit tests in `recovery.rs` 2. - The tests were written before the code but passed on its first run, so they were not seen red that way. Instead nine bugs were put back one at a time (old factors kept after a recovery, the check skipped, a key for a machine store, for a version-1 vault, two recovery slots accepted, a password as the second factor, no audit on remove, no audit on recover, a second key not replacing the first): each failed at least one test. That run was on the first commit; the second commit only changes how the key is grouped in print. ## Self-review 1. **Accepted, documented:** the vault key is not changed by a recovery, so a copy of the file from before still opens with the old factors and the old key. A recovery takes back the vault, not its copies. 2. **Accepted, documented:** `recover` writes nothing. A caller that uses the returned vault without saving has changed nothing on disk; the desktop must save before it shows anything. 3. **Accepted:** the id is in the header in the clear. It is 4 random bytes and says nothing about the key. 4. **Minor, stated:** the check that the id in the typed key matches the slot's id is not covered by a test of its own: without it a wrong key is still refused by the wrap. It only makes the answer quicker. 5. **Minor, stated:** a test's comment claimed to forge a key with the right id; it changes the last character, which the check catches. The comment now says so. No test builds a well-formed key with this vault's id and other bytes (the encoder is not public). 6. **Not done here:** no limit on wrong tries in the core (a file is guessed offline anyway; the key is 256 bits). A growing wait after wrong keys belongs to the app. 7. `cargo fmt` also reformatted one line in `tests/factors.rs`. ## Not verified - No app uses this yet: the Recover screen comes in Custos. - `custos-wasm` builds with it (workspace tests), but the web vault has no way to recover.
mnovak added 2 commits 2026-11-12 23:09:30 +01:00
mnovak merged commit ff14c8902f into main 2026-11-12 23:09:32 +01:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mnovak/CustosPublic#21