Repository navigation
Unix: try cached passwords only with the KDF they opened a volume with #1972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flatstik
wants to merge
3
commits into
veracrypt:master
Choose a base branch
from
flatstik:fix-1969-cache-kdf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #!/usr/bin/env python3 | ||
| """Regression test for issue #1969 on Linux, against the installed veracrypt. | ||
|
|
||
| Usage (as root, after installing a package): | ||
| python3 Tests/test_password_cache_kdf.py [--veracrypt PATH] | ||
|
|
||
| Creates three volumes with a PIM of 100: A (SHA-512) and C (SHA-256) share | ||
| a password, B (SHA-512) has another one. With password caching enabled, | ||
| mounts them as favorites in one run, typing only the passwords of A and B, | ||
| with KDF autodetection. Checked: | ||
|
|
||
| - B is mounted after its password is typed, and the cached password of A | ||
| that is tried on B first fails without trying Argon2: with that PIM, | ||
| Argon2 uses 1 GiB and 82 passes, so the run would take much longer than | ||
| mounting the volumes one by one; | ||
| - C is mounted with the cached password of A, without a prompt, although | ||
| that password was cached with another KDF. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| PASSWORD_A = "regression-cache-password-a" | ||
| PASSWORD_B = "regression-cache-password-b" | ||
| PIM = "100" | ||
| # Argon2 with PIM 100 takes far longer than this on any machine that can run | ||
| # it, while the PBKDF2 attempts for the volumes here take a fraction of it. | ||
| MARGIN = 15 | ||
|
|
||
|
|
||
| class Checks: | ||
| def __init__(self): | ||
| self.failures = 0 | ||
|
|
||
| def check(self, desc, cond, detail=""): | ||
| print(("ok " if cond else "FAIL ") + desc, flush=True) | ||
| if not cond: | ||
| self.failures += 1 | ||
| if detail: | ||
| print(" " + detail.strip().replace("\n", "\n ")) | ||
|
|
||
|
|
||
| def text(result): | ||
| return (result.stdout or b"").decode("utf-8", "replace") | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | ||
| parser.add_argument("--veracrypt", | ||
| default=shutil.which("veracrypt") or "veracrypt") | ||
| args = parser.parse_args() | ||
| if os.geteuid() != 0: | ||
| sys.exit("Run as root: the test mounts volumes") | ||
|
|
||
| work = Path(tempfile.mkdtemp(prefix="vc-regression-")) | ||
| config = work / "config" / "VeraCrypt" | ||
| config.mkdir(parents=True) | ||
| env = dict(os.environ, XDG_CONFIG_HOME=str(work / "config")) | ||
| (work / "random").write_bytes(os.urandom(4096)) | ||
| c = Checks() | ||
|
|
||
| def run(arguments, stdin=None): | ||
| return subprocess.run([args.veracrypt, "--text"] + arguments, | ||
| input=stdin, stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, cwd=work, env=env, | ||
| timeout=1800) | ||
|
|
||
| def mounted(): | ||
| """Paths of the mounted volumes; raises if they cannot be listed.""" | ||
| r = run(["--list"]) | ||
| if r.returncode != 0: | ||
| if "No volumes mounted" in text(r): | ||
| return set() | ||
| raise RuntimeError("veracrypt --list failed: " + text(r)) | ||
| return {line.split()[1] for line in text(r).splitlines() | ||
| if len(line.split()) >= 4 and line.split()[0].endswith(":")} | ||
|
|
||
| def dismount(paths): | ||
| """Dismounts only these volumes; returns whether none remains.""" | ||
| paths = {str(path) for path in paths} | ||
| for path in paths & mounted(): | ||
| run(["--non-interactive", "-d", path]) | ||
| return not paths & mounted() | ||
|
|
||
| mount_options = ["--pim=" + PIM, "--keyfiles=", "--protect-hidden=no", | ||
| "--filesystem=none", "--mount-options=nokernelcrypto"] | ||
| volumes = {name: work / ("%s.hc" % name) for name in ("a", "b", "c")} | ||
| try: | ||
| for name, password, hash in (("a", PASSWORD_A, "SHA-512"), | ||
| ("b", PASSWORD_B, "SHA-512"), | ||
| ("c", PASSWORD_A, "SHA-256")): | ||
| r = run(["--non-interactive", "--create", str(volumes[name]), | ||
| "--size=2M", "--volume-type=normal", "--encryption=AES", | ||
| "--hash=" + hash, "--filesystem=none", "-p", password, | ||
| "--pim=" + PIM, "--keyfiles=", | ||
| "--random-source=" + str(work / "random")]) | ||
| c.check("create %s.hc" % name, r.returncode == 0, text(r)) | ||
| if c.failures: | ||
| sys.exit("Cannot create the test volumes") | ||
|
|
||
| start = time.monotonic() | ||
| r = run(["--non-interactive", "-p", PASSWORD_A, str(volumes["a"])] | ||
| + mount_options) | ||
| single = time.monotonic() - start | ||
| c.check("mount a.hc with KDF autodetection", | ||
| r.returncode == 0 and dismount([volumes["a"]]), text(r)) | ||
|
|
||
| (config / "Configuration.xml").write_text( | ||
| '<?xml version="1.0" encoding="utf-8"?>\n<VeraCrypt>\n' | ||
| '\t<configuration>\n\t\t<config key="CachePasswords">1</config>\n' | ||
| '\t</configuration>\n</VeraCrypt>\n') | ||
| (config / "Favorite Volumes.xml").write_text( | ||
| '<?xml version="1.0" encoding="utf-8"?>\n<VeraCrypt>\n' | ||
| '\t<favorites>\n' + "".join( | ||
| '\t\t<volume mountpoint="" slotnumber="%d" readonly="0" ' | ||
| 'system="0">%s</volume>\n' % (slot, volumes[name]) | ||
| for slot, name in ((1, "a"), (2, "b"), (3, "c"))) | ||
| + '\t</favorites>\n</VeraCrypt>\n') | ||
|
|
||
| start = time.monotonic() | ||
| # --load-preferences enables the password cache of Configuration.xml | ||
| r = run(["--load-preferences", "--auto-mount=favorites"] | ||
| + mount_options, | ||
| stdin=("%s\n%s\n" % (PASSWORD_A, PASSWORD_B)).encode()) | ||
| favorites = time.monotonic() - start | ||
| listed = mounted() | ||
| c.check("all three favorites are mounted", | ||
| r.returncode == 0 | ||
| and all(str(volumes[name]) in listed for name in volumes), | ||
| text(r) + "\n" + str(listed)) | ||
| c.check("c.hc (another KDF) is mounted from the cache, without a " | ||
| "prompt", | ||
| text(r).count("Enter password") == 2, text(r)) | ||
| c.check("cached passwords that do not match are not tried with " | ||
| "Argon2 (%.1f s, one mount %.1f s)" % (favorites, single), | ||
| favorites < 3 * single + MARGIN) | ||
| finally: | ||
| # Only the volumes of this test: unrelated ones may be mounted | ||
| if not dismount(volumes.values()): | ||
| sys.exit("A test volume is still mounted; not removing %s" % work) | ||
| shutil.rmtree(work, ignore_errors=True) | ||
|
|
||
| if c.failures: | ||
| sys.exit("%d check(s) failed" % c.failures) | ||
| print("All checks passed") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.