lock.rs

crates/veilvoice-crypto/src/lock.rs

veilvoice-crypto · 2384 lines · read the source here · or on GitHub

The application lock: an Argon2id password verifier with a rate limit.

What this is worth, stated before anything else

This is not a security boundary against someone holding the disk. It cannot be. A local application has nowhere to hide a secret from the machine it runs on: whoever can read this file can also delete it, and deleting it removes the lock. That is not a defect in the implementation, it is what a local lock is.

What it does buy is real and worth having: someone who picks up your unlocked computer, such as a flatmate, a colleague or a border officer with your session open, cannot open VeilVoice, see which files you have processed, or start a live scramble. That is the threat this defends against, and SCOPE says so in the words the user sees.

If the threat is an attacker with the disk, the answer is full-volume encryption (LUKS, BitLocker, FileVault) plus crate::container for the recordings themselves. Neither of those is replaced by this module.

Why a verifier and not a key

The lock stores Argon2id(domain ‖ password, salt), split by HKDF into a verifier and a tag key, and compares the verifier in constant time. It derives no key that encrypts anything, and that is still true of this module after roadmap item 86.

What changed is one level up. The desktop application can now be told to seal every recording it writes with the app-lock passphrase, and it does that through crate::container in the ordinary way, with a fresh salt per file. So the recordings do not depend on this file: delete the lock and they still open, given the passphrase. Nothing here holds a key to them and nothing here can.

The property that is given up by switching that on is not cryptographic, it is human, and it belongs in the interface rather than in a comment: one passphrase then opens the application and the archive together. The default is still two separate secrets, and docs/USER_GUIDE.md section 5.4 states the trade in the words a user reads.

The stored verifier is a password hash sitting on disk in the clear, and must be treated like one. Argon2id at the default cost (256 MiB, t=3, p=4) makes an offline attack expensive per guess, which matters for a decent passphrase and does not save a bad one.

The tag, and the one tamper claim this file can honestly make

Every record carries a 16-byte authentication tag over all the bytes before it, keyed by a value that exists only while a correct passphrase is in memory. The tag key is the second half of one Argon2id run, split from the verifier by HKDF, so publishing the verifier, which the file does by sitting on disk, says nothing about the tag key.

That buys exactly one thing, and it is worth naming precisely. Somebody who edits this file without knowing the passphrase cannot leave the edit looking authentic. Resetting the failure counter to zero, winding the last-failure timestamp back to escape a wait, or dropping the Argon2id cost so that a guess is cheap. All three are edits, and all three are caught at the next successful unlock, because that is the moment the tag key exists.

It buys nothing at all against the two attacks people expect it to stop. Deleting the file still removes the lock. Replacing the file wholesale with a lock the attacker created still lets the attacker in, because their own record is authentic under their passphrase. crate::vault answers the first with a second copy and answers the second not at all.

The tamper flag, once raised, is stored and is cleared only by an unlock that also proves the passphrase. So the report survives a restart, and the person who caused it cannot dismiss it.

Rate limiting

Wrong attempts are counted and the count is persisted, so killing the process does not hand an attacker a fresh budget. After three free attempts the wait doubles: 5 s, 10 s, 20 s … capped at fifteen minutes.

The counter is stored in the same unauthenticated file as the verifier, and the wait is measured against the system clock. Someone who can edit the file or move the clock defeats both. Again: casual access, not the disk.

Separate from the recording password

The password that unlocks the app and the password that encrypts recordings are two different passwords, on purpose, so that unlocking the app is not the same act as unsealing everything it has ever written. To make that structural rather than merely conventional, the verifier is derived over a domain- separated input, so the same passphrase used in both places still produces unrelated values.

In plain words

The lock on the application window.

It asks for a passphrase before VeilVoice will open, and it slows down after repeated wrong answers so that guessing is not worth trying.

It is not protection against somebody who has your disk. It stops the person who picks up your unlocked laptop, and that is genuinely worth having, but anybody who can read the files directly is not stopped by a program deciding whether to show you a window. Encrypting your recordings is what protects them; this protects the session.

WHAT THIS FILE CONTAINS

2384 lines defining 57 functions (38 public), 3 types and 16 constants. Everything below is read out of the source, so it cannot disagree with the code.

The types it owns.

  • struct AppLock line 191 · A password verifier plus its attempt history.
  • struct LockStore line 702 · An AppLock bound to a file, which is persisted after every attempt.
  • enum Backing line 718 · Where a LockStore keeps its record.

What happens when it runs. These are the ways in: public, and nothing else in this file calls them, so they are what an outside caller reaches first.

  • AppLock::create line 223 · Create a lock for password.
    reaches derive_pair, derive_keys
  • AppLock::store_key line 255 · Derive the key that names and opens the obfuscated program folder.
    reaches derive_keys
  • AppLock::would_open line 336 · Whether password is this lock's, recording nothing.
    reaches derive_pair, derive_keys
  • AppLock::same_secret_as line 351 · Whether two records hold the same stored password.
  • AppLock::tampered line 363 · Whether a record has been found edited by somebody without the passphrase, at any point since this was last cleared.
  • AppLock::acknowledge line 371 · Clear the tamper report, after proving the passphrase.
    reaches verify, unix_now, verify_at, verify_keeping_at, cooldown_at, derive_pair, tag_matches, delay_secs, derive_keys, tag, body
  • AppLock::cooldown line 409 · Seconds still to wait before another attempt is accepted.
    reaches cooldown_at, unix_now, delay_secs
  • AppLock::failures line 429 · Consecutive failed attempts recorded so far.
  • AppLock::params line 434 · The Argon2id cost this lock was created with.
  • AppLock::retag line 472 · Draw a fresh nonce and re-tag the record under password.
    reaches derive_pair, retag_with, derive_keys, tag, body
  • AppLock::to_bytes line 509 · Serialise exactly as it appears on disk.
    reaches body
  • LockStore::open line 740 · Load the lock at path, or Ok(None) if no lock is configured there.
    reaches parse
  • LockStore::create line 762 · Create a lock at path, refusing to overwrite one already there.
    reaches derive_pair, derive_keys
  • LockStore::tampered line 811 · Whether the stored record has been found edited by somebody without the passphrase.
  • LockStore::would_open line 817 · Whether password is this lock's, recording nothing.
    reaches derive_pair, derive_keys
  • LockStore::params line 826 · The cost parameters this lock was made with.
  • LockStore::acknowledge line 838 · Clear the tamper report, after proving the passphrase, and persist that.
    reaches verify, unix_now, verify_at, verify_keeping_at, cooldown_at, derive_pair, tag_matches, delay_secs, derive_keys, tag, body
  • LockStore::report_tamper line 853 · Raise the tamper report from outside, and persist it if the passphrase allows.
  • LockStore::change_password line 864 · Replace the password, after proving the current one.
    reaches save, unlock, write_private, unlock_keeping
  • LockStore::remove line 899 · Remove the lock, after proving the password.
    reaches unlock, unlock_keeping, save, write_private
  • LockStore::cooldown line 908 · Seconds still to wait before another attempt is accepted.
    reaches cooldown_at, unix_now, delay_secs
  • LockStore::store_key line 917 · Derive the key that names and opens the obfuscated program folder.
    reaches derive_keys
  • LockStore::failures line 922 · Consecutive failed attempts recorded so far.
  • LockStore::path line 928 · Where this lock is stored.
  • LockStore::every_copy_current line 955 · Whether the last write reached every copy.
  • open_default line 972 · Open the lock at the default location, wherever this platform keeps it.
    reaches default_dir, open_in, default_path, read_legacy, choose_base, config_path, portable_dir, parse
  • kept_in line 991 · Whether a lock is kept under base, in the vault or as the old single file, found without opening it or writing anything.
  • create_default line 1058 · Create a lock at the default location, refusing to replace one already there.
    reaches create_in, default_dir, read_legacy, default_path, parse, choose_base, config_path, portable_dir
  • platform_dir line 1209 · The platform's own configuration directory, whether or not it is the one in use.
    reaches config_path
  • is_portable line 1228 · Whether this copy is keeping its state beside itself.
    reaches portable_dir

WHAT CALLS WHAT

delay_secs line 166 AppLock::create line 223 AppLock::store_key line 255 AppLock::verify line 265 AppLock::would_open line 336 AppLock::acknowledge line 371 AppLock::cooldown line 409 AppLock::retag line 472 AppLock::to_bytes line 509 AppLock::parse line 541 LockStore::open line 740 LockStore::create line 762 LockStore::unlock line 783 LockStore::would_open line 817 LockStore::acknowledge line 838 LockStore::change_password line 864 LockStore::remove line 899 LockStore::cooldown line 908 LockStore::store_key line 917 open_default line 972 open_in line 1002 create_default line 1058 entry: a way in: public, and nothing in this file calls it api: public, and also used inside this file dashed: a call that goes back up, or across a wrapped rank The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one. 22 of 49 functions are drawn; the diagram is bounded at 22 so it stays readable.

The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one. 22 of 49 functions are drawn; the diagram is bounded at 22 so it stays readable.

The same graph as Mermaid source
%%{init: {"theme":"base","themeVariables":{"background":"#1a1b26","primaryColor":"#1f2335","primaryTextColor":"#c0caf5","primaryBorderColor":"#7aa2f7","secondaryColor":"#16161e","tertiaryColor":"#16161e","lineColor":"#737aa2","textColor":"#c0caf5","mainBkg":"#1f2335","nodeBorder":"#7aa2f7","clusterBkg":"#16161e","clusterBorder":"#2f3549","fontFamily":"ui-monospace, SFMono-Regular, Consolas, monospace","fontSize":"14px"}}}%%
flowchart TD
    n_delay_secs["delay_secs<br/>line 166"]
    n_create(["AppLock::create<br/>line 223"])
    n_store_key(["AppLock::store_key<br/>line 255"])
    n_verify["AppLock::verify<br/>line 265"]
    n_would_open(["AppLock::would_open<br/>line 336"])
    n_acknowledge(["AppLock::acknowledge<br/>line 371"])
    n_cooldown(["AppLock::cooldown<br/>line 409"])
    n_retag(["AppLock::retag<br/>line 472"])
    n_to_bytes(["AppLock::to_bytes<br/>line 509"])
    n_parse["AppLock::parse<br/>line 541"]
    n_open(["LockStore::open<br/>line 740"])
    n_create(["LockStore::create<br/>line 762"])
    n_unlock["LockStore::unlock<br/>line 783"]
    n_would_open(["LockStore::would_open<br/>line 817"])
    n_acknowledge(["LockStore::acknowledge<br/>line 838"])
    n_change_password(["LockStore::change_password<br/>line 864"])
    n_remove(["LockStore::remove<br/>line 899"])
    n_cooldown(["LockStore::cooldown<br/>line 908"])
    n_store_key(["LockStore::store_key<br/>line 917"])
    n_open_default(["open_default<br/>line 972"])
    n_open_in["open_in<br/>line 1002"]
    n_create_default(["create_default<br/>line 1058"])
    n_acknowledge --> n_verify
    n_change_password --> n_unlock
    n_open --> n_parse
    n_open_default --> n_open_in
    n_remove --> n_unlock
    click n_delay_secs href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L166" "open the source"
    click n_create href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L223" "open the source"
    click n_store_key href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L255" "open the source"
    click n_verify href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L265" "open the source"
    click n_would_open href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L336" "open the source"
    click n_acknowledge href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L371" "open the source"
    click n_cooldown href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L409" "open the source"
    click n_retag href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L472" "open the source"
    click n_to_bytes href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L509" "open the source"
    click n_parse href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L541" "open the source"
    click n_open href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L740" "open the source"
    click n_create href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L762" "open the source"
    click n_unlock href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L783" "open the source"
    click n_would_open href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L817" "open the source"
    click n_acknowledge href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L838" "open the source"
    click n_change_password href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L864" "open the source"
    click n_remove href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L899" "open the source"
    click n_cooldown href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L908" "open the source"
    click n_store_key href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L917" "open the source"
    click n_open_default href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L972" "open the source"
    click n_open_in href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L1002" "open the source"
    click n_create_default href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/lock.rs#L1058" "open the source"
    classDef entry fill:#1f2335,stroke:#7aa2f7,color:#c0caf5
    class n_create,n_store_key,n_would_open,n_acknowledge,n_cooldown,n_retag,n_to_bytes,n_open,n_create,n_would_open,n_acknowledge,n_change_password,n_remove,n_cooldown,n_store_key,n_open_default,n_create_default entry
    classDef api fill:#1f2335,stroke:#7dcfff,color:#c0caf5
    class n_delay_secs,n_verify,n_parse,n_unlock,n_open_in api
    subgraph chart_key ["Key"]
        direction LR
        key_entry["entry: a way in: public, and nothing in this file calls it"]
        key_api["api: public, and also used inside this file"]
    end
    class key_entry entry
    class key_api api

This site loads no third-party script, so it cannot run Mermaid; the diagram above is the same nodes and edges drawn by the generator instead. GitHub renders the source below directly.

ITEMS

ItemLineDocumentation
SCOPE pub const114What the app lock protects against, and what it does not, in the words a front-end should show the user.
MAGIC pub const122Magic bytes at the start of a lock file.
FORMAT_VERSION pub const124Format version this build writes.
LOCK_LEN pub const126Exact size of a version 2 lock file, in bytes.
LOCK_LEN_V1 pub const128Exact size of a version 1 lock file, which this build still reads.
DOMAIN const132Domain separator, so the app-lock secret can never coincide with a key derived from the same passphrase anywhere else in this crate.
INFO_VERIFIER const134HKDF label for the half of the derivation that is written to disk.
INFO_TAG const136HKDF label for the half that is not, and that authenticates the record.
INFO_STORE const146HKDF label for the obfuscated store key.
TAG_LEN const148Length of the record tag.
BODY_LEN const151Length of the tagged part of a record, which is also the offset of the nonce that follows it.
FREE_ATTEMPTS const154Failed attempts allowed before the wait starts.
BASE_DELAY_SECS const156The first enforced wait, in seconds.
MAX_DELAY_SECS const160The longest the wait ever gets.
delay_secs pub fn166How long to refuse the next attempt after failures consecutive failures.
unix_now fn179Seconds since the Unix epoch, negative before it.
AppLock pub struct191A password verifier plus its attempt history.
AppLock::create pub fn223Create a lock for password.
AppLock::store_key pub fn255Derive the key that names and opens the obfuscated program folder.
AppLock::verify pub fn265Check password, recording the outcome.
AppLock::verify_at fn274Check a passphrase against this lock as of now.
AppLock::verify_keeping fn286As verify, handing back the tag key it derived.
AppLock::verify_keeping_at fn291As verify_at, handing back the tag key.
AppLock::would_open pub fn336Whether password is this lock's, recording nothing.
AppLock::same_secret_as pub fn351Whether two records hold the same stored password.
AppLock::tampered pub fn363Whether a record has been found edited by somebody without the passphrase, at any point since this was last cleared.
AppLock::acknowledge pub fn371Clear the tamper report, after proving the passphrase.
AppLock::tag_matches fn381Whether the file's tamper tag is the one this key computes.
AppLock::tag fn398The authentication tag over everything in the record before it.
AppLock::cooldown pub fn409Seconds still to wait before another attempt is accepted.
AppLock::cooldown_at fn416How long is left of the delay after the last failed attempt, if any.
AppLock::failures pub fn429Consecutive failed attempts recorded so far.
AppLock::params pub fn434The Argon2id cost this lock was created with.
AppLock::body fn453The bytes the tag covers.
AppLock::retag pub fn472Draw a fresh nonce and re-tag the record under password.
AppLock::retag_with fn479As retag, with a tag key already derived from the passphrase, so the caller pays for one Argon2id run rather than two.
AppLock::to_bytes pub fn509Serialise exactly as it appears on disk.
AppLock::parse pub fn541Parse a lock file, version 1 or version 2.
derive_pair fn659Derive the verifier and the tag key for password.
derive_keys fn674As derive_pair, and the store key with it.
LockStore pub struct702An AppLock bound to a file, which is persisted after every attempt.
Backing enum718Where a LockStore keeps its record.
Backing::primary fn726The path this lock is really kept at, whether it is a plain file or the real one among a vault's decoys.
LockStore::open pub fn740Load the lock at path, or Ok(None) if no lock is configured there.
LockStore::create pub fn762Create a lock at path, refusing to overwrite one already there.
LockStore::unlock pub fn783Check password and persist the outcome.
LockStore::unlock_keeping fn791As unlock, handing back the tag key the check derived.
LockStore::tampered pub fn811Whether the stored record has been found edited by somebody without the passphrase.
LockStore::would_open pub fn817Whether password is this lock's, recording nothing.
LockStore::params pub fn826The cost parameters this lock was made with.
LockStore::acknowledge pub fn838Clear the tamper report, after proving the passphrase, and persist that.
LockStore::report_tamper pub fn853Raise the tamper report from outside, and persist it if the passphrase allows.
LockStore::change_password pub fn864Replace the password, after proving the current one.
LockStore::remove pub fn899Remove the lock, after proving the password.
LockStore::cooldown pub fn908Seconds still to wait before another attempt is accepted.
LockStore::store_key pub fn917Derive the key that names and opens the obfuscated program folder.
LockStore::failures pub fn922Consecutive failed attempts recorded so far.
LockStore::path pub fn928Where this lock is stored.
LockStore::save fn936Write the record, and say whether every copy of it is now current.
LockStore::every_copy_current pub fn955Whether the last write reached every copy.
open_default pub fn972Open the lock at the default location, wherever this platform keeps it.
LEGACY_NAME pub(crate) const984The name the lock had before the vault: one file, under the obvious name.
kept_in pub fn991Whether a lock is kept under base, in the vault or as the old single file, found without opening it or writing anything.
open_in pub fn1002Open a vault-backed lock under base, adopting a pre-vault file if one is there.
read_legacy fn1048Read a pre-vault lock file, if one is there.
create_default pub fn1058Create a lock at the default location, refusing to replace one already there.
create_in pub fn1068Create a vault-backed lock under base.
write_private fn1094Write the lock file so it is owner-only from the moment it exists.
config_path fn1124Where the lock file lives, given a platform and an environment.
PORTABLE_DIR pub const1157The name of the folder that makes a copy of VeilVoice keep its state beside itself.
portable_dir fn1173Where a portable copy keeps its state, when it is one.
choose_base fn1192Which of the two locations a copy is using, given what is beside it and what the platform says.
default_dir pub fn1198The configuration directory the vault keeps its files in, if the environment says where one is.
platform_dir pub fn1209The platform's own configuration directory, whether or not it is the one in use.
is_portable pub fn1228Whether this copy is keeping its state beside itself.
default_path pub fn1251Where the lock file lives, if there is anywhere for it.