Skip to main content

Module rekey

Module rekey 

Source
Expand description

Changing the master password: a new salt, a new key, the same data.

The key is Argon2id over the password and vault.salt, so changing the password changes both files: vault.db is re-encrypted and vault.salt replaced. Two files cannot be swapped atomically, so the order is chosen so that a crash at any point leaves a vault that opens with either the old password or the new one, never neither:

  1. take a write lock on the old database (a second connection, since ATTACH cannot run inside a transaction), checkpoint it, and copy vault.db and vault.salt to *.pre-rekey (the way back);
  2. write the new salt as vault.salt.new;
  3. sqlcipher_export the data into vault.db.rekey-new under the new key;
  4. open that file with the new key and check that it is intact and holds the same rows;
  5. rename it over vault.db, then rename vault.salt.new over vault.salt.

A crash before step 5’s first rename leaves the old pair untouched (the leftovers are removed). A crash between the two renames leaves a new database beside the old salt and a vault.salt.new; recover_interrupted, which every unlock runs first, sees that the new password opens the database through vault.salt.new and finishes the rename. The *.pre-rekey copies stay until the next successful unlock (cleanup_after_unlock). Backups made before the change still open with the old password, which is the point of a backup, and the documentation says so.

Recovery packages are encrypted to an age recipient, not to the master password, so they are not affected.

Structs§

Report

Enums§

Stage
Where a rekey can be stopped, for the tests that prove each stop is survivable.

Constants§

MIN_PASSWORD_CHARS
The shortest new master password the command accepts, as in the desktop app.

Functions§

cleanup_after_unlock
Removes the *.pre-rekey copies. Called after an unlock has succeeded, which proves the new pair works; until then they are the way back.
recover_interrupted
Finishes or abandons a rekey that a crash interrupted. Cheap when there is nothing to do: one existence check. Returns whether it changed anything.
rekey
rekey_with without a stop.
rekey_with
Re-encrypts the vault under new_password. stop_after ends the run early with an error, which is what a crash at that point looks like; production callers pass None through rekey.