fe65e1f899
This commit lands four follow-up items that were explicitly deferred in
TODO.md after the prior file-format change, plus a CLI/units cleanup
that fell out of reviewing them:
1. Manual STREAM nonce construction (drops `stream` cargo feature).
2. Atomic file output (`.tmp` + rename, with cleanup on failure).
3. Argon2id KDF + passphrase prompt + matching CLI flags.
4. Hardened secret handling: zeroize-on-drop, mlock'd buffers,
custom cross-platform tty reader (replaces `rpassword`).
Why
---
The prior version had three concrete weaknesses that were fine for
"early development" but unacceptable past that point:
* `--raw-key` was the only way to supply a key, exposing it in
`/proc/$pid/cmdline`. There was no passphrase mode at all.
* Crashes/aborts during encrypt could leave a half-written output
file in place of (or replacing) the user's target.
* Key material wasn't zeroed and could end up in swap or coredumps.
rpassword's reallocating String buffers also leaked stale heap
copies of typed passphrases that no `Zeroizing` wrapper could
reach after the fact.
(1) Manual STREAM nonces
------------------------
Replaces `aead::stream::EncryptorBE32` / `DecryptorBE32` with
explicit `make_nonce(prefix, counter, last)` and direct
`XChaCha20Poly1305::{encrypt,decrypt}_in_place` calls. The wire format
is unchanged (XChaCha20Poly1305 STREAM-BE32 = 19-byte prefix || 4-byte
big-endian counter || 1-byte last-block flag), so files written by the
previous version still decrypt. Counter overflow is now an explicit
`Format` error rather than a panic in the upstream stream wrapper.
This removes the `stream` cargo feature from `chacha20poly1305` and
prepares the encrypt path for parallelism: with explicit nonces we can
hand chunks to a worker pool keyed by counter without the stream
wrapper's stateful API getting in the way.
(2) Atomic file output
----------------------
New `utils::OutSink` writes to `<path>.tmp`, calls `sync_all()` on
`commit()`, and renames into place. If dropped without commit (panic,
crypto/IO error, ctrl-C), the temp file is unlinked so the existing
target is untouched. Stdout output is unaffected (no temp dance).
A new integration test (`atomic_output_no_stale_tmp_on_failure`)
verifies that a failed decrypt leaves neither the final output nor
the temp file behind.
(3) Argon2id + passphrase
-------------------------
New `KdfParams::Argon2id { salt, m_cost, t_cost, p_cost }` variant
encoded into the header (and authenticated as AAD), so tampering with
KDF params fails authentication on every chunk.
CLI surface (BREAKING):
* `--raw-key` is now optional; one of `--raw-key`, `--passphrase`,
`--passphrase-env <VAR>` is required.
* `--passphrase` prompts on the controlling terminal with echo off,
and asks for confirmation when encrypting.
* `--passphrase-env <VAR>` reads from a named env var; intended for
non-interactive use (scripts, tests). The env-table copy is a
known leak for that path.
* `--argon-memory <MiB>` (default 1024 = 1 GiB), `--argon-passes`
(default 2), `--argon-parallelism` (default 4). Names follow
argon2 RFC 9106 terminology; memory is MiB rather than KiB to
match how humans actually think about RAM. Defaults follow the
"Balanced" preset for 2026-era hardware (~1.5–4 s on a laptop).
The argon2 crate wants KiB internally, so the CLI value is
multiplied by 1024 with overflow-check.
(4) Secret hardening
--------------------
New `secrets` module provides:
* `SecretBytes32`: heap-allocated 32-byte buffer wrapped in
`Zeroizing<[u8; 32]>` and mlock'd via the `region` crate.
Field order ensures the lock guard drops *before* the buffer is
freed (otherwise munlock would target freed memory).
* `SecretVec`: fixed-capacity, mlock'd, zeroize-on-drop byte
buffer. `push()` rejects writes past the reserved capacity so
the underlying allocation never reallocates and moves — which
would invalidate the lock and leave a stale unzeroed copy on
the heap.
* `read_passphrase_tty()`: direct tty reader. On Unix, opens
`/dev/tty`, clears `ECHO` via `tcgetattr`/`tcsetattr` with an
RAII guard that restores termios on drop. On Windows, opens
`CONIN$`/`CONOUT$` and clears `ENABLE_ECHO_INPUT` via
`Get/SetConsoleMode`. Reads byte-by-byte into a pre-reserved
`SecretVec` (1024 bytes), so neither the Rust side nor the libc
side reallocates during read. This replaces `rpassword`, which
returned a `String` that grew by reallocation and left
unzeroed copies of typed passphrases on the heap.
`PartialEq` on `SecretVec` is constant-time-ish (length check +
xor-or accumulate) so the confirmation comparison doesn't early-out
on the first differing byte.
`disable_core_dumps()` calls `setrlimit(CORE, 0)` on Unix; on
Windows it's a no-op (WER/minidump suppression is a per-machine
policy and intentionally not done here).
`Cli`'s secret-bearing fields are moved out into local bindings at
the top of `run()` and the `Cli` is explicitly dropped, so they
don't sit in the parsed struct for the rest of the function.
`Cli.raw_key` is `Option<Zeroizing<String>>` so the field we own
zeroes itself on drop. Clap's own intermediate copies during
parsing are an accepted leak.
Threat model — what is and isn't covered
-----------------------------------------
Covered (best-effort):
* Secrets in coredumps → rlimit on Unix.
* Secrets paged to swap or hibernation → mlock on the AEAD key
and passphrase buffer.
* Half-written ciphertext on crash → atomic rename.
* Stale heap copies of typed passphrase → custom tty reader,
pre-reserved buffer.
* Stale stack/heap copies of the AEAD
key or passphrase post-process-exit → zeroize on drop.
Not covered (and not pretending to be):
* Live-process attackers with ptrace or `/proc/$pid/mem` access.
* The kernel's tty/line buffer.
* Clap's transient String allocations during arg parsing.
* The `environ` table copy of an env-var passphrase.
* Swap on systems without functioning mlock or with
`RLIMIT_MEMLOCK = 0`.
mlock is small (32 bytes + 1024 bytes — two pages at most on any
of the three target OSes), so it fits well under the typical
unprivileged `RLIMIT_MEMLOCK` of 64 KiB.
Portability
-----------
The whole binary targets Linux, macOS, and Windows 11 with the
same security properties where the OS supports them:
* `region` crate provides cross-platform mlock/munlock.
* `libc::tcgetattr`/`tcsetattr` covers Linux + macOS.
* `windows-sys` covers Console API.
* `rlimit` is gated to `cfg(unix)`.
The Windows tty path compiles in my head but is unverified on this
machine — there is no `x86_64-pc-windows-*` target installed and
no Windows runner. Treat that path as "best-effort, needs CI on
Windows" until exercised.
Files written by the previous v0.10 (Raw KDF, BE32 STREAM) are
still readable: the wire format is unchanged for that path.
Test plan
---------
Existing 17 integration tests pass unchanged. Two new tests:
* `roundtrip_passphrase_argon2id` — encrypts and decrypts via
`--passphrase-env` with cheap argon2 params (8 MiB / 1 pass) so
the test stays fast; also verifies that a wrong passphrase
fails.
* `atomic_output_no_stale_tmp_on_failure` — wrong-key decrypt
leaves neither the final file nor the `.tmp` in place.
Manual sanity (not automated): run with `--passphrase` on a
terminal and confirm echo is off and confirmation works.
Follow-ups (still in TODO.md)
-----------------------------
* Multi-threaded encrypt pipeline (now feasible — manual nonces).
* Length-committed mode + random-access decrypt fast path.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
439 lines
11 KiB
Rust
439 lines
11 KiB
Rust
// SPDX-License-Identifier: GPL-3.0-only
|
|
//
|
|
// Integration tests for the `fcry` binary.
|
|
//
|
|
// These exercise the CLI as a black box: encrypt then decrypt and check that
|
|
// plaintext bytes are preserved, plus a handful of failure cases (tampering,
|
|
// wrong key, truncation, bad magic).
|
|
|
|
use std::fs;
|
|
use std::io::Write;
|
|
use std::process::{Command, Stdio};
|
|
|
|
use assert_cmd::cargo::CommandCargoExt;
|
|
use tempfile::TempDir;
|
|
|
|
const KEY: &[u8; 32] = b"0123456789abcdef0123456789abcdef";
|
|
const KEY_STR: &str = "0123456789abcdef0123456789abcdef";
|
|
|
|
fn fcry() -> Command {
|
|
Command::cargo_bin("fcry").unwrap()
|
|
}
|
|
|
|
/// Deterministic pseudo-random plaintext of `n` bytes (xorshift, seedable).
|
|
/// We avoid `/dev/urandom` so tests are reproducible on failure.
|
|
fn pseudo_random(seed: u64, n: usize) -> Vec<u8> {
|
|
let mut s = seed.wrapping_add(0x9E3779B97F4A7C15);
|
|
let mut out = Vec::with_capacity(n);
|
|
while out.len() < n {
|
|
s ^= s << 13;
|
|
s ^= s >> 7;
|
|
s ^= s << 17;
|
|
out.extend_from_slice(&s.to_le_bytes());
|
|
}
|
|
out.truncate(n);
|
|
out
|
|
}
|
|
|
|
fn encrypt_file(plain: &std::path::Path, ct: &std::path::Path, chunk_size: Option<u32>) {
|
|
let mut cmd = fcry();
|
|
cmd.arg("-i")
|
|
.arg(plain)
|
|
.arg("-o")
|
|
.arg(ct)
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR);
|
|
if let Some(cs) = chunk_size {
|
|
cmd.arg("--chunk-size").arg(cs.to_string());
|
|
}
|
|
let out = cmd.output().unwrap();
|
|
assert!(
|
|
out.status.success(),
|
|
"encrypt failed: {}",
|
|
String::from_utf8_lossy(&out.stderr)
|
|
);
|
|
}
|
|
|
|
fn decrypt_file(ct: &std::path::Path, rt: &std::path::Path) {
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(ct)
|
|
.arg("-o")
|
|
.arg(rt)
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
out.status.success(),
|
|
"decrypt failed: {}",
|
|
String::from_utf8_lossy(&out.stderr)
|
|
);
|
|
}
|
|
|
|
fn roundtrip_with_size(plaintext_size: usize, chunk_size: Option<u32>) {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("plain.bin");
|
|
let ct = dir.path().join("ct.bin");
|
|
let rt = dir.path().join("rt.bin");
|
|
|
|
let data = pseudo_random(plaintext_size as u64, plaintext_size);
|
|
fs::write(&plain, &data).unwrap();
|
|
|
|
encrypt_file(&plain, &ct, chunk_size);
|
|
decrypt_file(&ct, &rt);
|
|
|
|
let got = fs::read(&rt).unwrap();
|
|
assert_eq!(got, data, "roundtrip mismatch at size {plaintext_size}");
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_empty() {
|
|
roundtrip_with_size(0, None);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_one_byte() {
|
|
roundtrip_with_size(1, None);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_smaller_than_chunk() {
|
|
roundtrip_with_size(100, None);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_exactly_one_chunk() {
|
|
roundtrip_with_size(1024 * 1024, None);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_just_over_one_chunk() {
|
|
roundtrip_with_size(1024 * 1024 + 1, None);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_multi_chunk() {
|
|
roundtrip_with_size(5 * 1024 * 1024 + 12345, None);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_custom_small_chunk_size() {
|
|
// forces many chunks for a small input
|
|
roundtrip_with_size(50_000, Some(4096));
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_chunk_size_one_byte() {
|
|
// pathological but should still work
|
|
roundtrip_with_size(257, Some(1));
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_pipe_stdin_stdout() {
|
|
let data = pseudo_random(42, 200_000);
|
|
|
|
let mut enc = fcry()
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.unwrap();
|
|
enc.stdin.as_mut().unwrap().write_all(&data).unwrap();
|
|
let enc_out = enc.wait_with_output().unwrap();
|
|
assert!(
|
|
enc_out.status.success(),
|
|
"pipe encrypt failed: {}",
|
|
String::from_utf8_lossy(&enc_out.stderr)
|
|
);
|
|
|
|
let mut dec = fcry()
|
|
.arg("-d")
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.unwrap();
|
|
dec.stdin
|
|
.as_mut()
|
|
.unwrap()
|
|
.write_all(&enc_out.stdout)
|
|
.unwrap();
|
|
let dec_out = dec.wait_with_output().unwrap();
|
|
assert!(
|
|
dec_out.status.success(),
|
|
"pipe decrypt failed: {}",
|
|
String::from_utf8_lossy(&dec_out.stderr)
|
|
);
|
|
|
|
assert_eq!(dec_out.stdout, data);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_wrong_key() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
fs::write(&plain, pseudo_random(1, 1000)).unwrap();
|
|
encrypt_file(&plain, &ct, None);
|
|
|
|
let wrong = "ffffffffffffffffffffffffffffffff";
|
|
assert_ne!(wrong.as_bytes(), KEY);
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(dir.path().join("rt.bin"))
|
|
.arg("--raw-key")
|
|
.arg(wrong)
|
|
.output()
|
|
.unwrap();
|
|
assert!(!out.status.success(), "decrypt with wrong key should fail");
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_tampered_header() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
fs::write(&plain, pseudo_random(2, 1000)).unwrap();
|
|
encrypt_file(&plain, &ct, None);
|
|
|
|
// Flip a byte in the chunk_size field of the header (offset 8: 4 magic + 4 fixed).
|
|
let mut bytes = fs::read(&ct).unwrap();
|
|
bytes[8] ^= 0xff;
|
|
fs::write(&ct, &bytes).unwrap();
|
|
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(dir.path().join("rt.bin"))
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
!out.status.success(),
|
|
"decrypt with tampered header should fail"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_tampered_ciphertext() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
fs::write(&plain, pseudo_random(3, 5000)).unwrap();
|
|
encrypt_file(&plain, &ct, None);
|
|
|
|
// Flip a byte well past the header (in the first ciphertext chunk).
|
|
let mut bytes = fs::read(&ct).unwrap();
|
|
let off = bytes.len() / 2;
|
|
bytes[off] ^= 0x01;
|
|
fs::write(&ct, &bytes).unwrap();
|
|
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(dir.path().join("rt.bin"))
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
!out.status.success(),
|
|
"decrypt of tampered ciphertext should fail"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_truncated_ciphertext() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
fs::write(&plain, pseudo_random(4, 3 * 1024 * 1024)).unwrap();
|
|
encrypt_file(&plain, &ct, None);
|
|
|
|
// Drop the trailing 16-byte tag of the last chunk (and then some).
|
|
let mut bytes = fs::read(&ct).unwrap();
|
|
bytes.truncate(bytes.len() - 32);
|
|
fs::write(&ct, &bytes).unwrap();
|
|
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(dir.path().join("rt.bin"))
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
!out.status.success(),
|
|
"decrypt of truncated ciphertext should fail"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_bad_magic() {
|
|
let dir = TempDir::new().unwrap();
|
|
let bogus = dir.path().join("bogus.bin");
|
|
fs::write(&bogus, b"NOPE\x01\x01\x00\x00\x00\x10\x00\x00\x00").unwrap();
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&bogus)
|
|
.arg("-o")
|
|
.arg(dir.path().join("rt.bin"))
|
|
.arg("--raw-key")
|
|
.arg(KEY_STR)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
!out.status.success(),
|
|
"decrypt of file with bad magic should fail"
|
|
);
|
|
assert!(
|
|
String::from_utf8_lossy(&out.stderr).contains("magic"),
|
|
"expected 'magic' in stderr, got: {}",
|
|
String::from_utf8_lossy(&out.stderr)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_short_raw_key() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
fs::write(&plain, b"hello").unwrap();
|
|
let out = fcry()
|
|
.arg("-i")
|
|
.arg(&plain)
|
|
.arg("-o")
|
|
.arg(dir.path().join("c.bin"))
|
|
.arg("--raw-key")
|
|
.arg("tooshort")
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
!out.status.success(),
|
|
"encrypt with short raw_key should fail"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn roundtrip_passphrase_argon2id() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
let rt = dir.path().join("r.bin");
|
|
let data = pseudo_random(7, 100_000);
|
|
fs::write(&plain, &data).unwrap();
|
|
|
|
// Use cheap argon2 params so the test stays fast.
|
|
let enc = fcry()
|
|
.arg("-i")
|
|
.arg(&plain)
|
|
.arg("-o")
|
|
.arg(&ct)
|
|
.arg("--passphrase-env")
|
|
.arg("FCRY_TEST_PW")
|
|
.arg("--argon-memory")
|
|
.arg("8")
|
|
.arg("--argon-passes")
|
|
.arg("1")
|
|
.env("FCRY_TEST_PW", "correct horse battery staple")
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
enc.status.success(),
|
|
"passphrase encrypt failed: {}",
|
|
String::from_utf8_lossy(&enc.stderr)
|
|
);
|
|
|
|
let dec = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(&rt)
|
|
.arg("--passphrase-env")
|
|
.arg("FCRY_TEST_PW")
|
|
.env("FCRY_TEST_PW", "correct horse battery staple")
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
dec.status.success(),
|
|
"passphrase decrypt failed: {}",
|
|
String::from_utf8_lossy(&dec.stderr)
|
|
);
|
|
assert_eq!(fs::read(&rt).unwrap(), data);
|
|
|
|
// Wrong passphrase must fail.
|
|
let bad = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(dir.path().join("bad.bin"))
|
|
.arg("--passphrase-env")
|
|
.arg("FCRY_TEST_PW")
|
|
.env("FCRY_TEST_PW", "wrong passphrase")
|
|
.output()
|
|
.unwrap();
|
|
assert!(!bad.status.success(), "wrong passphrase should fail");
|
|
}
|
|
|
|
#[test]
|
|
fn atomic_output_no_stale_tmp_on_failure() {
|
|
// A failed decrypt (wrong key) should not leave the output file behind.
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
let rt = dir.path().join("r.bin");
|
|
fs::write(&plain, b"hello world").unwrap();
|
|
encrypt_file(&plain, &ct, None);
|
|
|
|
let wrong = "ffffffffffffffffffffffffffffffff";
|
|
let out = fcry()
|
|
.arg("-d")
|
|
.arg("-i")
|
|
.arg(&ct)
|
|
.arg("-o")
|
|
.arg(&rt)
|
|
.arg("--raw-key")
|
|
.arg(wrong)
|
|
.output()
|
|
.unwrap();
|
|
assert!(!out.status.success());
|
|
assert!(!rt.exists(), "final output must not exist after failure");
|
|
let mut tmp = rt.clone();
|
|
tmp.set_file_name("r.bin.tmp");
|
|
assert!(!tmp.exists(), "temp file must be cleaned up");
|
|
}
|
|
|
|
#[test]
|
|
fn header_chunk_size_is_authoritative_on_decrypt() {
|
|
// Encrypt with a non-default chunk size; decrypt without specifying one.
|
|
// The decryptor must read chunk_size from the header.
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("p.bin");
|
|
let ct = dir.path().join("c.bin");
|
|
let rt = dir.path().join("r.bin");
|
|
let data = pseudo_random(5, 100_000);
|
|
fs::write(&plain, &data).unwrap();
|
|
encrypt_file(&plain, &ct, Some(7919)); // prime, deliberately weird
|
|
decrypt_file(&ct, &rt);
|
|
assert_eq!(fs::read(&rt).unwrap(), data);
|
|
}
|