Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d79e96c498
|
|||
|
99705afa9e
|
|||
|
3f53c221c8
|
|||
|
725d33939e
|
|||
|
81ac1475ad
|
|||
|
d7b0127d20
|
|||
|
67b412a1a5
|
|||
|
6898297973
|
|||
|
45571c98fe
|
|||
|
acd2712ade
|
|||
|
ea2e43fe3d
|
|||
|
2c101abdbd
|
|||
|
91b459657e
|
|||
|
53bb927a87
|
|||
|
75afadb1ec
|
|||
|
f72f9034f3
|
Generated
+145
-21
@@ -76,15 +76,27 @@ checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"base64ct",
|
"base64ct",
|
||||||
"blake2",
|
"blake2",
|
||||||
"cpufeatures",
|
"cpufeatures 0.2.17",
|
||||||
"password-hash",
|
"password-hash",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "assert_cmd"
|
name = "arrayref"
|
||||||
version = "2.2.1"
|
version = "0.3.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "39bae1d3fa576f7c6519514180a72559268dd7d1fe104070956cb687bc6673bd"
|
checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arrayvec"
|
||||||
|
version = "0.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "assert_cmd"
|
||||||
|
version = "2.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2aa3a22042e45de04255c7bf3626e239f450200fd0493c1e382263544b20aea6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstyle",
|
"anstyle",
|
||||||
"bstr",
|
"bstr",
|
||||||
@@ -103,9 +115,9 @@ checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.11.1"
|
version = "2.13.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
|
checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "blake2"
|
name = "blake2"
|
||||||
@@ -116,6 +128,20 @@ dependencies = [
|
|||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "blake3"
|
||||||
|
version = "1.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0aa83c34e62843d924f905e0f5c866eb1dd6545fc4d719e803d9ba6030371fce"
|
||||||
|
dependencies = [
|
||||||
|
"arrayref",
|
||||||
|
"arrayvec",
|
||||||
|
"cc",
|
||||||
|
"cfg-if",
|
||||||
|
"constant_time_eq",
|
||||||
|
"cpufeatures 0.3.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "block-buffer"
|
name = "block-buffer"
|
||||||
version = "0.10.4"
|
version = "0.10.4"
|
||||||
@@ -136,6 +162,16 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cc"
|
||||||
|
version = "1.2.63"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "556e016178bb5662a08681bbe0f00f8e17631781a4dfc8c45e466e4b185ec27f"
|
||||||
|
dependencies = [
|
||||||
|
"find-msvc-tools",
|
||||||
|
"shlex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.4"
|
version = "1.0.4"
|
||||||
@@ -150,7 +186,7 @@ checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"cipher",
|
"cipher",
|
||||||
"cpufeatures",
|
"cpufeatures 0.2.17",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -223,6 +259,12 @@ version = "1.0.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "constant_time_eq"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpufeatures"
|
name = "cpufeatures"
|
||||||
version = "0.2.17"
|
version = "0.2.17"
|
||||||
@@ -232,6 +274,30 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-channel"
|
||||||
|
version = "0.5.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crypto-common"
|
name = "crypto-common"
|
||||||
version = "0.1.7"
|
version = "0.1.7"
|
||||||
@@ -284,21 +350,31 @@ checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fcry"
|
name = "fcry"
|
||||||
version = "0.10.0"
|
version = "0.11.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argon2",
|
"argon2",
|
||||||
"assert_cmd",
|
"assert_cmd",
|
||||||
|
"blake3",
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"clap",
|
"clap",
|
||||||
|
"crossbeam-channel",
|
||||||
"getrandom 0.4.2",
|
"getrandom 0.4.2",
|
||||||
"libc",
|
"libc",
|
||||||
"rlimit",
|
"rlimit",
|
||||||
|
"same-file",
|
||||||
"secrets",
|
"secrets",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
|
"unicode-normalization",
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "find-msvc-tools"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "foldhash"
|
name = "foldhash"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@@ -350,9 +426,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.17.0"
|
version = "0.17.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51"
|
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
@@ -373,7 +449,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
|
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown 0.17.0",
|
"hashbrown 0.17.1",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_core",
|
"serde_core",
|
||||||
]
|
]
|
||||||
@@ -419,15 +495,15 @@ checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.29"
|
version = "0.4.32"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
checksum = "953f07c43838f8e6f9758cab68bf5bed85465e7587ebe0b823f1bcd81978ad3a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.8.0"
|
version = "2.8.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
@@ -480,7 +556,7 @@ version = "0.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf"
|
checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cpufeatures",
|
"cpufeatures 0.2.17",
|
||||||
"opaque-debug",
|
"opaque-debug",
|
||||||
"universal-hash",
|
"universal-hash",
|
||||||
]
|
]
|
||||||
@@ -583,6 +659,15 @@ dependencies = [
|
|||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "same-file"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "secrets"
|
name = "secrets"
|
||||||
version = "1.3.0"
|
version = "1.3.0"
|
||||||
@@ -632,9 +717,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.149"
|
version = "1.0.150"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
|
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"memchr",
|
"memchr",
|
||||||
@@ -643,6 +728,12 @@ dependencies = [
|
|||||||
"zmij",
|
"zmij",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shlex"
|
||||||
|
version = "2.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
@@ -686,10 +777,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683"
|
checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typenum"
|
name = "tinyvec"
|
||||||
version = "1.20.0"
|
version = "1.11.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "40ce102ab67701b8526c123c1bab5cbe42d7040ccfd0f64af1a385808d2f43de"
|
checksum = "3e61e67053d25a4e82c844e8424039d9745781b3fc4f32b8d55ed50f5f667ef3"
|
||||||
|
dependencies = [
|
||||||
|
"tinyvec_macros",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tinyvec_macros"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.20.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
@@ -697,6 +803,15 @@ version = "1.0.24"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-normalization"
|
||||||
|
version = "0.1.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8"
|
||||||
|
dependencies = [
|
||||||
|
"tinyvec",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-xid"
|
name = "unicode-xid"
|
||||||
version = "0.2.6"
|
version = "0.2.6"
|
||||||
@@ -814,6 +929,15 @@ version = "0.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|||||||
+22
-16
@@ -1,36 +1,42 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["ddidderr <ddidderr@paul.network>"]
|
|
||||||
edition = "2024"
|
|
||||||
name = "fcry"
|
name = "fcry"
|
||||||
version = "0.10.0"
|
version = "0.11.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
argon2 = "0.5"
|
argon2 = "0.5"
|
||||||
chacha20poly1305 = "0.10"
|
chacha20poly1305 = "0.10"
|
||||||
clap = {version = "4", features = ["derive"]}
|
clap = { version = "4", features = ["derive"] }
|
||||||
getrandom = {version = "0.4"}
|
crossbeam-channel = "0.5"
|
||||||
protected-secrets = {package = "secrets", version = "1.3"}
|
getrandom = { version = "0.4" }
|
||||||
zeroize = {version = "1", features = ["derive"]}
|
blake3 = "1"
|
||||||
|
protected-secrets = { package = "secrets", version = "1.3" }
|
||||||
|
same-file = "1"
|
||||||
|
unicode-normalization = "0.1"
|
||||||
|
zeroize = { version = "1", features = ["derive"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
assert_cmd = "2"
|
||||||
|
tempfile = "3"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
rlimit = "0.11"
|
rlimit = "0.11"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
windows-sys = {version = "0.61", features = [
|
windows-sys = {
|
||||||
"Win32_System_Console",
|
version = "0.61",
|
||||||
|
features = [
|
||||||
"Win32_Foundation",
|
"Win32_Foundation",
|
||||||
"Win32_Storage_FileSystem",
|
|
||||||
"Win32_Security",
|
"Win32_Security",
|
||||||
]}
|
"Win32_Storage_FileSystem",
|
||||||
|
"Win32_System_Console",
|
||||||
[dev-dependencies]
|
]
|
||||||
assert_cmd = "2"
|
}
|
||||||
tempfile = "3"
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = false
|
|
||||||
debug = false
|
debug = false
|
||||||
strip = true
|
strip = true
|
||||||
|
lto = true
|
||||||
panic = "unwind"
|
panic = "unwind"
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
I read every file, traced the STREAM construction, the parallel pipeline's ordering/cancellation logic, and the I/O paths, and cross-checked the existing `REVIEW_SECURITY_GPT5.5.md`. Verdict first: **the cryptographic core is correct.** The problems are in input validation and file handling, and two of them can destroy user data without any attacker involved.
|
||||||
|
|
||||||
|
## What's cryptographically sound (verified, not assumed)
|
||||||
|
|
||||||
|
The construction is the STREAM online-AE scheme (Hoang–Reyhanitabar–Rogaway–Vizár), the same shape age and Tink use: XChaCha20-Poly1305, 19-byte random prefix + 4-byte BE counter + last-block byte = 24-byte nonce, full header as AAD on every chunk. I checked the failure modes individually:
|
||||||
|
|
||||||
|
- **Truncation** at a chunk boundary: the new final chunk was encrypted with `last=false`, decryptor computes the nonce with `last=true` → tag failure. Mid-chunk truncation and appended garbage also fail (a trailing partial read flips the real last chunk to `Normal`, wrong nonce again).
|
||||||
|
- **Reorder/duplicate/drop** of interior chunks: counter is in the nonce → fails.
|
||||||
|
- **Cross-file chunk splicing** under the same key: per-file random 19-byte prefix plus per-file header AAD → fails.
|
||||||
|
- **Header tampering** (chunk_size, KDF params, version, flags, length): header is the AAD → every chunk fails. The v1 regression test shows you understood the encode-stability trap; `version` is preserved as-read, so no downgrade is possible.
|
||||||
|
- **Empty plaintext** is authenticated via an explicit empty last chunk; a ciphertext that is only a header errors instead of silently producing an empty file.
|
||||||
|
- **Counter overflow** is checked (`bump_counter`), nonce prefix collision across files is 2⁻¹⁵² territory, `getrandom` failure propagates instead of falling back.
|
||||||
|
- **Parallel pipeline**: I traced the permit/reorder/cancel logic. The writer only writes in counter order, a failed chunk leaves `pending` non-empty → error, and `commit()` is only reachable after all three thread groups joined with no error. There is no path where a tampered chunk ends up in a committed file output.
|
||||||
|
|
||||||
|
The README's worry that it "could theoretically be not effective at all" is unfounded for the construction itself.
|
||||||
|
|
||||||
|
## New findings (not in the GPT-5.5 review)
|
||||||
|
|
||||||
|
**1. High — `--chunk-size 0` with stdin silently discards all data.**
|
||||||
|
There is no encrypt-side validation of `chunk_size`. With `0`, `AheadReader` gets capacity 0, `read_until_full` on an empty slice returns `Ok(0)` immediately (BufReader fills its internal buffer, then copies 0 bytes), so the very first `read_ahead` returns `Empty`. `encrypt` then writes a single authenticated empty last chunk and **commits successfully**. For a regular file input, the `plaintext_length` cross-check saves you (`committed != 0` → error before commit). For `cat backup.tar | fcry --chunk-size 0 ...` there is no committed length, so you get a valid-looking output file that encrypts zero bytes — and since `Header::read` rejects `chunk_size == 0`, the file is additionally undecryptable. If the user deletes the source afterwards, the data is gone. Fix: validate `chunk_size` in `run()` (reject 0, set a sane max), independent of the header-side check.
|
||||||
|
|
||||||
|
**2. High — `OutSink` can destroy the input file before reading it.**
|
||||||
|
`fcry -d -i backup.fcry.tmp -o backup.fcry` computes `tmp_path = backup.fcry.tmp` and calls `File::create` on it — which truncates your encrypted input to zero bytes before any read happens beyond what `BufReader` already buffered. The run then fails with "truncated ciphertext", but the original file is already destroyed. This is the sharpest consequence of the predictable `<name>.tmp` scheme; the GPT review only covered the symlink and permissions angle (both of which I confirm: `File::create` follows symlinks, and the decrypted plaintext gets umask-default 0644). One fix kills all three: `O_CREAT|O_EXCL` with a random suffix (`tempfile::NamedTempFile::new_in(parent)` + `persist`), mode 0600 for decrypt output, and optionally refuse to clobber an existing `final_path` without `--force`.
|
||||||
|
|
||||||
|
**3. Medium/Low — no key commitment.**
|
||||||
|
ChaCha20-Poly1305 is not key-committing: a ciphertext chunk can be constructed that authenticates under multiple keys (invisible-salamander / partitioning-oracle class). Practical exploitability here is low — it requires a victim decrypting attacker-supplied files and leaking success/failure, and each candidate key costs a full Argon2 run — but fixing it is nearly free and buys you something users feel daily: right now a wrong passphrase burns 1 GiB / seconds of Argon2 and then fails with the exact same `aead::Error` as a corrupted file. Derive a key-check value from the *stretched* key (e.g. first 16 bytes of BLAKE2b(key, "fcry-kcv") in the header, or encrypt a fixed zero block as chunk −1) and you get both key commitment in practice and a clean "wrong passphrase" vs. "corrupt file" distinction. Deriving it from the Argon2 output means it does not cheapen offline guessing — an attacker with the file can already test guesses against chunk 0's tag at identical cost.
|
||||||
|
|
||||||
|
**4. Low — passphrase byte-encoding is not portable.**
|
||||||
|
The Unix path reads UTF-8 bytes from `/dev/tty`. The Windows path reads bytes via `ReadFile` on `CONIN$`, which yields the console's ANSI/OEM codepage — for any non-ASCII passphrase those byte sequences differ, and Argon2 hashes bytes. A file encrypted on Linux with `pässwört` can be undecryptable on Windows and vice versa. There's also no Unicode normalization (NFC vs. NFD — macOS input methods differ from Linux). Either read UTF-16 via `ReadConsoleW` and convert to UTF-8 + normalize to NFC on all platforms, or document ASCII-only passphrases.
|
||||||
|
|
||||||
|
**5. Low — unchecked `u64` multiply in `decrypt_range`.**
|
||||||
|
`chunk_offset = header_len + i * cipher_chunk`: with a forged header committing `plaintext_length` near 2⁶⁴ and a small `chunk_size`, `i` can approach 2³²−1 while `cipher_chunk` is 2³²+15, and the product wraps in release (your release profile has default `overflow-checks = false`). The result is a seek to a wrong offset and a guaranteed tag failure, so it's not exploitable — but it's pre-authentication arithmetic on attacker bytes and should be `checked_mul`/`checked_add` like the rest of that function already is.
|
||||||
|
|
||||||
|
## GPT-5.5 findings I confirm, with sharpening
|
||||||
|
|
||||||
|
- **Pre-auth resource consumption from header fields** — real, and worse in the parallel path than stated: the pipeline reader allocates a fresh `vec![0u8; chunk_sz]` per job with up to `4 × threads` chunks in flight, so a forged `chunk_size = u32::MAX` on a 16-core box attempts ~64 × 4 GiB before any tag is checked. Argon2 `m_cost` from the header can demand up to 4 TiB; the argon2 crate's block allocation will abort the process on OOM. Cap both at parse time (e.g. chunk_size ≤ 256 MiB, m_cost ≤ some GiB ceiling with an override flag).
|
||||||
|
- **Stdout streams authenticated-but-possibly-truncated prefixes** — correct, and inherent to chunked streaming AE; every released byte is authentic, but a downstream consumer can act on a verified prefix before the truncation error lands. Document it prominently; a `--buffer-verify` mode is the only real alternative.
|
||||||
|
- **`--length 0` range decrypt succeeds with zero authentication** (doesn't even prove the key) and range success ≠ whole-file integrity — both correct as written.
|
||||||
|
- **No floors on passphrase/KDF choices** — confirmed; empty passphrase plus `--argon-memory 1 --argon-passes 1` is accepted (the argon2 crate clamps only at m_cost ≥ 8·p_cost).
|
||||||
|
- **`--raw-key` design** — confirmed; beyond the `/proc/*/cmdline` leak, the keyspace is restricted to valid UTF-8 of exactly 32 bytes, which both blocks legitimate random keys (~⅓ of random 32-byte strings aren't UTF-8, and clap's `String` parsing rejects them) and invites typing a 32-char password with zero stretching. Hex/base64 from a file or fd is the right replacement.
|
||||||
|
- **Plaintext chunk buffers unprotected** — confirmed (plain `Vec<u8>` through channels, no zeroize/mlock). I'd rank this lower than the review does: the plaintext's home is the disk anyway; the meaningful residual exposure is swap, and core dumps are already disabled on Unix.
|
||||||
|
|
||||||
|
Two more one-liners: `commit()` doesn't fsync the parent directory after the rename, so a crash right after a successful run can lose the rename (durability, not confidentiality); and `plaintext_length` sits in cleartext in the header, which leaks nothing beyond what the unpadded ciphertext length already reveals — but if you ever care about size metadata, that's where a padding scheme (à la covert padding / PADMÉ) would slot in.
|
||||||
|
|
||||||
|
If you want, I can write the patch set for the top items — chunk-size validation, the `OutSink` rework with `create_new` + random suffix + 0600, and header-side caps are all small, self-contained diffs.
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
# Findings
|
||||||
|
|
||||||
|
- High: attacker-controlled headers can cause large pre-auth resource use. chunk_size and Argon2 params are read before any AEAD tag is verified, then used for allocation/KDF work. A malicious file can request huge buffers or Argon2 memory/time. See
|
||||||
|
src/header.rs:107, src/crypto.rs:54, src/crypto.rs:189. Add strict max caps before allocation/KDF.
|
||||||
|
- High: output temp file handling is unsafe in hostile directories. Output uses predictable <name>.tmp plus File::create, which follows symlinks and truncates existing files. Decrypted plaintext may also be created with permissive umask-derived
|
||||||
|
permissions. See src/utils.rs:71. Use randomized create_new temp files, 0600 permissions for plaintext, and safe persist semantics.
|
||||||
|
- Medium: stdout decrypt streams verified chunks before whole-file success. File output is protected by temp-file commit, but stdout cannot roll back. A truncated valid-prefix ciphertext can emit authentic but incomplete plaintext before the final
|
||||||
|
error. See src/crypto.rs:217 and src/pipeline.rs:348. Document this sharply or add a “verify before stdout” mode.
|
||||||
|
- Medium: random-access decrypt authenticates only requested chunks. That is expected for range reads, but users may mistake success for whole-file integrity. --length 0 succeeds without authenticating any chunk, so it does not prove the key or file is
|
||||||
|
valid. See src/crypto.rs:307.
|
||||||
|
- Medium: weak passphrase choices are accepted. Empty/very short passphrases and very low Argon2 settings are possible. Defaults are strong, but user-supplied weak parameters are stored and honored. See src/main.rs:55, src/secrets.rs:172. Enforce
|
||||||
|
floors or require an explicit insecure flag.
|
||||||
|
- Medium: raw-key UX invites misuse. --raw-key is a UTF-8 command-line string, visible in process listings, and not truly arbitrary 32 raw bytes. It is documented as dangerous, but still a first-class CLI path. See src/main.rs:37. Prefer hex/base64
|
||||||
|
from file/stdin/fd, or make raw key testing-only.
|
||||||
|
- Medium: plaintext buffers are not zeroized. Keys/passphrases get protected handling, but plaintext/ciphertext chunk buffers are ordinary Vec<u8> and may leave sensitive plaintext in heap memory after decrypt/encrypt. See src/crypto.rs:127, src/
|
||||||
|
crypto.rs:210, src/pipeline.rs:48.
|
||||||
|
- Medium/Low: unchecked arithmetic and unbounded CLI knobs remain. chunk_size + TAG_LEN, random-access offsets, huge -j values, and release overflow behavior deserve explicit checked math and caps. See src/crypto.rs:190, src/pipeline.rs:61,
|
||||||
|
Cargo.toml:32.
|
||||||
|
- Low: platform hardening is partial. Unix core dumps are disabled, which is good, but Windows crash dumps are not addressed, and non-secret plaintext buffers are not protected from swap/dumps. See src/main.rs:141.
|
||||||
|
- Low: custom crypto framing needs a written threat model and test vectors. The construction is reasonable, but custom file formats are easy to misuse over time. README still says the tool is early and not thoroughly tested, and links a missing TODO.
|
||||||
|
See README.md:6.
|
||||||
|
|
||||||
|
Good Signs
|
||||||
|
|
||||||
|
The core AEAD choice is solid: XChaCha20-Poly1305 with random nonce prefix, per-chunk counters, full header as AAD, and an authenticated final chunk. The code also commits plaintext length for regular files, rejects unknown header flags, uses Argon2id
|
||||||
|
for passphrases, disables Unix core dumps, and avoids committing partial file outputs on failure.
|
||||||
|
|
||||||
|
Post-Quantum
|
||||||
|
|
||||||
|
For the current symmetric-only design, there is no RSA/ECC public-key crypto to replace. A 256-bit symmetric key is generally the right shape for post-quantum resistance; Grover-style search still leaves roughly 128-bit security. The weak link is
|
||||||
|
password entropy, not the AEAD.
|
||||||
|
|
||||||
|
If you add recipient/public-key encryption or signatures, use standardized PQC: NIST finalized FIPS 203 ML-KEM, FIPS 204 ML-DSA, and FIPS 205 SLH-DSA in 2024, and recommends migration planning now. Sources: NIST FIPS announcement
|
||||||
|
(<https://www.nist.gov/news-events/news/2024/08/announcing-approval-three-federal-information-processing-standards-fips>), NIST PQC project (<https://www.nist.gov/programs-projects/post-quantum-cryptography>).
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
**Deferred to follow-up commits** (in order):
|
|
||||||
1. Multi-threaded pipeline (worker pool + ordered writer)
|
|
||||||
2. Length-committed mode + random-access decrypt fast path for files
|
|
||||||
+482
-37
@@ -1,21 +1,29 @@
|
|||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
use chacha20poly1305::{KeyInit, XChaCha20Poly1305, XNonce, aead::AeadInPlace};
|
use chacha20poly1305::{KeyInit, XChaCha20Poly1305, XNonce, aead::AeadInPlace};
|
||||||
use std::io::Write;
|
use std::fs::File;
|
||||||
|
use std::io::{BufReader, Read, Seek, SeekFrom, Write};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
use crate::header::{AlgId, Header, KdfParams, NONCE_PREFIX_LEN, TAG_LEN};
|
use crate::header::{
|
||||||
|
AlgId, FLAG_KEY_COMMITTED, FLAG_LENGTH_COMMITTED, Header, KdfParams, NONCE_PREFIX_LEN, TAG_LEN,
|
||||||
|
VERSION_CURRENT,
|
||||||
|
};
|
||||||
|
use crate::pipeline;
|
||||||
|
use crate::policy;
|
||||||
use crate::reader::{AheadReader, ReadInfoChunk};
|
use crate::reader::{AheadReader, ReadInfoChunk};
|
||||||
use crate::secrets::{SecretBytes32, SecretVec};
|
use crate::secrets::{SecretBytes32, SecretVec};
|
||||||
use crate::utils::*;
|
use crate::utils::*;
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
/// XChaCha20Poly1305 nonce: 24 bytes total. STREAM splits the trailing 5 bytes
|
/// XChaCha20Poly1305 nonce: 24 bytes total. STREAM splits the trailing 5 bytes
|
||||||
/// into a 4-byte big-endian counter and a 1-byte "last block" flag.
|
/// into a 4-byte big-endian counter and a 1-byte "last block" flag.
|
||||||
const NONCE_LEN: usize = 24;
|
pub(crate) const NONCE_LEN: usize = 24;
|
||||||
const COUNTER_LEN: usize = 4;
|
pub(crate) const COUNTER_LEN: usize = 4;
|
||||||
const _: () = assert!(NONCE_PREFIX_LEN + COUNTER_LEN + 1 == NONCE_LEN);
|
const _: () = assert!(NONCE_PREFIX_LEN + COUNTER_LEN + 1 == NONCE_LEN);
|
||||||
|
|
||||||
fn make_nonce(prefix: &[u8; NONCE_PREFIX_LEN], counter: u32, last: bool) -> XNonce {
|
pub(crate) fn make_nonce(prefix: &[u8; NONCE_PREFIX_LEN], counter: u32, last: bool) -> XNonce {
|
||||||
let mut n = [0u8; NONCE_LEN];
|
let mut n = [0u8; NONCE_LEN];
|
||||||
n[..NONCE_PREFIX_LEN].copy_from_slice(prefix);
|
n[..NONCE_PREFIX_LEN].copy_from_slice(prefix);
|
||||||
n[NONCE_PREFIX_LEN..NONCE_PREFIX_LEN + COUNTER_LEN].copy_from_slice(&counter.to_be_bytes());
|
n[NONCE_PREFIX_LEN..NONCE_PREFIX_LEN + COUNTER_LEN].copy_from_slice(&counter.to_be_bytes());
|
||||||
@@ -35,7 +43,7 @@ pub fn derive_key(
|
|||||||
match kdf {
|
match kdf {
|
||||||
KdfParams::Raw => {
|
KdfParams::Raw => {
|
||||||
let raw =
|
let raw =
|
||||||
raw_key.ok_or_else(|| FcryError::Format("raw kdf requires --raw-key".into()))?;
|
raw_key.ok_or_else(|| FcryError::Format("raw kdf requires --key-file".into()))?;
|
||||||
raw.with_array(|raw| out.with_mut_array(|out| out.copy_from_slice(raw)));
|
raw.with_array(|raw| out.with_mut_array(|out| out.copy_from_slice(raw)));
|
||||||
}
|
}
|
||||||
KdfParams::Argon2id {
|
KdfParams::Argon2id {
|
||||||
@@ -55,53 +63,139 @@ pub fn derive_key(
|
|||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build the AEAD cipher from the protected key. The cipher holds an
|
||||||
|
/// unprotected copy of the key while alive; `chacha20poly1305` zeroizes that
|
||||||
|
/// copy on drop. Wrapping in `Arc` lets us share it across worker threads.
|
||||||
|
fn build_aead(key: &SecretBytes32) -> Arc<XChaCha20Poly1305> {
|
||||||
|
Arc::new(key.with_array(|key| XChaCha20Poly1305::new(key.into())))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute_key_commitment(key: &SecretBytes32, header: &Header) -> [u8; 32] {
|
||||||
|
key.with_array(|key| {
|
||||||
|
let mut hasher = blake3::Hasher::new_keyed(key);
|
||||||
|
hasher.update(b"fcry-kcv-v3");
|
||||||
|
hasher.update(&[0]);
|
||||||
|
hasher.update(&header.commitment_input_encoding());
|
||||||
|
*hasher.finalize().as_bytes()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_key_commitment(header: &Header, key: &SecretBytes32) -> Result<(), FcryError> {
|
||||||
|
let Some(expected) = header.key_commitment else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let actual = compute_key_commitment(key, header);
|
||||||
|
let mut diff = 0u8;
|
||||||
|
for (a, b) in actual.iter().zip(expected.iter()) {
|
||||||
|
diff |= a ^ b;
|
||||||
|
}
|
||||||
|
if diff == 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(FcryError::WrongKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bump the per-chunk counter; surface a domain error on overflow rather than
|
||||||
|
/// panicking on debug or wrapping in release.
|
||||||
|
pub(crate) fn bump_counter(counter: u32) -> Result<u32, FcryError> {
|
||||||
|
counter
|
||||||
|
.checked_add(1)
|
||||||
|
.ok_or_else(|| FcryError::Format("STREAM counter overflow (input too large)".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn encrypt<S: AsRef<str>>(
|
pub fn encrypt<S: AsRef<str>>(
|
||||||
input_file: Option<S>,
|
input_file: Option<S>,
|
||||||
output_file: Option<S>,
|
output_file: Option<S>,
|
||||||
key: &SecretBytes32,
|
key: &SecretBytes32,
|
||||||
chunk_size: u32,
|
chunk_size: u32,
|
||||||
kdf: KdfParams,
|
kdf: KdfParams,
|
||||||
|
threads: usize,
|
||||||
) -> Result<(), FcryError> {
|
) -> Result<(), FcryError> {
|
||||||
let chunk_sz = chunk_size as usize;
|
encrypt_with_output_options(
|
||||||
let mut f_plain = AheadReader::from(open_input(input_file)?, chunk_sz);
|
input_file,
|
||||||
let mut f_encrypted = OutSink::open(output_file)?;
|
output_file,
|
||||||
|
key,
|
||||||
|
chunk_size,
|
||||||
|
kdf,
|
||||||
|
threads,
|
||||||
|
&OutSinkOptions::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encrypt_with_output_options<S: AsRef<str>>(
|
||||||
|
input_file: Option<S>,
|
||||||
|
output_file: Option<S>,
|
||||||
|
key: &SecretBytes32,
|
||||||
|
chunk_size: u32,
|
||||||
|
kdf: KdfParams,
|
||||||
|
threads: usize,
|
||||||
|
output_options: &OutSinkOptions,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
let chunk_sz = policy::validate_chunk_size(chunk_size)?;
|
||||||
|
let input = open_input(input_file)?;
|
||||||
|
let plaintext_length = input.length;
|
||||||
|
let mut f_plain = AheadReader::from(input.reader, chunk_sz);
|
||||||
|
let mut f_encrypted = OutSink::open_with_options(output_file, output_options)?;
|
||||||
|
|
||||||
let mut nonce_prefix = [0u8; NONCE_PREFIX_LEN];
|
let mut nonce_prefix = [0u8; NONCE_PREFIX_LEN];
|
||||||
getrandom::fill(&mut nonce_prefix)?;
|
getrandom::fill(&mut nonce_prefix)?;
|
||||||
|
|
||||||
let header = Header {
|
let flags = if plaintext_length.is_some() {
|
||||||
|
FLAG_LENGTH_COMMITTED
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
} | FLAG_KEY_COMMITTED;
|
||||||
|
let mut header = Header {
|
||||||
|
version: VERSION_CURRENT,
|
||||||
alg: AlgId::XChaCha20Poly1305,
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
flags: 0,
|
flags,
|
||||||
chunk_size,
|
chunk_size,
|
||||||
kdf,
|
kdf,
|
||||||
nonce_prefix,
|
nonce_prefix,
|
||||||
|
plaintext_length,
|
||||||
|
key_commitment: None,
|
||||||
};
|
};
|
||||||
let aad = header.encode();
|
header.key_commitment = Some(compute_key_commitment(key, &header));
|
||||||
|
let aad = Arc::new(header.encode());
|
||||||
f_encrypted.write_all(&aad)?;
|
f_encrypted.write_all(&aad)?;
|
||||||
|
|
||||||
// The AEAD keeps its own unprotected key copy while the loop runs.
|
let aead = build_aead(key);
|
||||||
// chacha20poly1305 zeroizes that copy on drop.
|
|
||||||
let aead = key.with_array(|key| XChaCha20Poly1305::new(key.into()));
|
|
||||||
|
|
||||||
let mut buf = vec![0u8; chunk_sz];
|
if threads > 1 {
|
||||||
|
return pipeline::encrypt_parallel(
|
||||||
|
f_plain,
|
||||||
|
f_encrypted,
|
||||||
|
aead,
|
||||||
|
aad,
|
||||||
|
nonce_prefix,
|
||||||
|
chunk_sz,
|
||||||
|
threads,
|
||||||
|
plaintext_length,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = Zeroizing::new(vec![0u8; chunk_sz]);
|
||||||
let mut counter: u32 = 0;
|
let mut counter: u32 = 0;
|
||||||
|
let mut bytes_seen: u64 = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match f_plain.read_ahead(&mut buf)? {
|
match f_plain.read_ahead(&mut buf)? {
|
||||||
ReadInfoChunk::Normal(_) => {
|
ReadInfoChunk::Normal(_) => {
|
||||||
let nonce = make_nonce(&nonce_prefix, counter, false);
|
let nonce = make_nonce(&nonce_prefix, counter, false);
|
||||||
aead.encrypt_in_place(&nonce, &aad, &mut buf)?;
|
aead.encrypt_in_place(&nonce, &aad, &mut *buf)?;
|
||||||
f_encrypted.write_all(&buf)?;
|
f_encrypted.write_all(&buf)?;
|
||||||
buf.truncate(chunk_sz);
|
buf.truncate(chunk_sz);
|
||||||
counter = counter.checked_add(1).ok_or_else(|| {
|
bytes_seen = policy::checked_count_add(bytes_seen, chunk_sz, "bytes read")?;
|
||||||
FcryError::Format("STREAM counter overflow (input too large)".into())
|
counter = bump_counter(counter)?;
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
ReadInfoChunk::Last(n) => {
|
ReadInfoChunk::Last(n) => {
|
||||||
buf.truncate(n);
|
buf.truncate(n);
|
||||||
let nonce = make_nonce(&nonce_prefix, counter, true);
|
let nonce = make_nonce(&nonce_prefix, counter, true);
|
||||||
aead.encrypt_in_place(&nonce, &aad, &mut buf)?;
|
aead.encrypt_in_place(&nonce, &aad, &mut *buf)?;
|
||||||
f_encrypted.write_all(&buf)?;
|
f_encrypted.write_all(&buf)?;
|
||||||
|
bytes_seen = policy::checked_count_add(bytes_seen, n, "bytes read")?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ReadInfoChunk::Empty => {
|
ReadInfoChunk::Empty => {
|
||||||
@@ -109,58 +203,125 @@ pub fn encrypt<S: AsRef<str>>(
|
|||||||
// authenticates the (empty) stream rather than silently producing nothing.
|
// authenticates the (empty) stream rather than silently producing nothing.
|
||||||
buf.clear();
|
buf.clear();
|
||||||
let nonce = make_nonce(&nonce_prefix, counter, true);
|
let nonce = make_nonce(&nonce_prefix, counter, true);
|
||||||
aead.encrypt_in_place(&nonce, &aad, &mut buf)?;
|
aead.encrypt_in_place(&nonce, &aad, &mut *buf)?;
|
||||||
f_encrypted.write_all(&buf)?;
|
f_encrypted.write_all(&buf)?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(committed) = plaintext_length
|
||||||
|
&& committed != bytes_seen
|
||||||
|
{
|
||||||
|
// Defense in depth: the input changed between stat and EOF. The
|
||||||
|
// committed length is part of the AEAD AAD, so any decrypter would
|
||||||
|
// also surface this, but we prefer to fail before publishing the file.
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"input length changed during encryption: committed {committed}, read {bytes_seen}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
f_encrypted.commit()?;
|
f_encrypted.commit()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn decrypt<S: AsRef<str>>(
|
pub fn decrypt<S: AsRef<str>>(
|
||||||
input_file: Option<S>,
|
input_file: Option<S>,
|
||||||
output_file: Option<S>,
|
output_file: Option<S>,
|
||||||
raw_key: Option<&SecretBytes32>,
|
raw_key: Option<&SecretBytes32>,
|
||||||
passphrase: Option<&SecretVec>,
|
passphrase: Option<&SecretVec>,
|
||||||
|
threads: usize,
|
||||||
) -> Result<(), FcryError> {
|
) -> Result<(), FcryError> {
|
||||||
let mut reader = open_input(input_file)?;
|
decrypt_with_argon_cap(
|
||||||
let header = Header::read(&mut reader)?;
|
input_file,
|
||||||
let aad = header.encode();
|
output_file,
|
||||||
|
raw_key,
|
||||||
|
passphrase,
|
||||||
|
threads,
|
||||||
|
policy::default_argon_decrypt_cap_mib(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn decrypt_with_argon_cap<S: AsRef<str>>(
|
||||||
|
input_file: Option<S>,
|
||||||
|
output_file: Option<S>,
|
||||||
|
raw_key: Option<&SecretBytes32>,
|
||||||
|
passphrase: Option<&SecretVec>,
|
||||||
|
threads: usize,
|
||||||
|
max_argon_memory_mib: u32,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
decrypt_with_output_options(
|
||||||
|
input_file,
|
||||||
|
output_file,
|
||||||
|
raw_key,
|
||||||
|
passphrase,
|
||||||
|
threads,
|
||||||
|
max_argon_memory_mib,
|
||||||
|
&OutSinkOptions::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decrypt_with_output_options<S: AsRef<str>>(
|
||||||
|
input_file: Option<S>,
|
||||||
|
output_file: Option<S>,
|
||||||
|
raw_key: Option<&SecretBytes32>,
|
||||||
|
passphrase: Option<&SecretVec>,
|
||||||
|
threads: usize,
|
||||||
|
max_argon_memory_mib: u32,
|
||||||
|
output_options: &OutSinkOptions,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
let mut reader = open_input(input_file)?.reader;
|
||||||
|
let header = Header::read_with_argon_cap(&mut reader, max_argon_memory_mib)?;
|
||||||
|
let aad = Arc::new(header.encode());
|
||||||
|
|
||||||
let key = derive_key(&header.kdf, raw_key, passphrase)?;
|
let key = derive_key(&header.kdf, raw_key, passphrase)?;
|
||||||
|
verify_key_commitment(&header, &key)?;
|
||||||
|
|
||||||
let chunk_sz = header.chunk_size as usize;
|
let chunk_sz = policy::validate_chunk_size(header.chunk_size)?;
|
||||||
let cipher_chunk = chunk_sz + TAG_LEN;
|
let cipher_chunk = policy::cipher_chunk_len(chunk_sz)?;
|
||||||
|
|
||||||
let mut f_encrypted = AheadReader::from(reader, cipher_chunk);
|
let mut f_encrypted = AheadReader::from(reader, cipher_chunk);
|
||||||
let mut f_plain = OutSink::open(output_file)?;
|
let mut f_plain = OutSink::open_with_options(output_file, output_options)?;
|
||||||
|
|
||||||
// The AEAD keeps its own unprotected key copy while the loop runs.
|
let aead = build_aead(&key);
|
||||||
// chacha20poly1305 zeroizes that copy on drop.
|
|
||||||
let aead = key.with_array(|key| XChaCha20Poly1305::new(key.into()));
|
|
||||||
|
|
||||||
let mut buf = vec![0u8; cipher_chunk];
|
if threads > 1 {
|
||||||
|
return pipeline::decrypt_parallel(
|
||||||
|
f_encrypted,
|
||||||
|
f_plain,
|
||||||
|
aead,
|
||||||
|
aad,
|
||||||
|
header.nonce_prefix,
|
||||||
|
cipher_chunk,
|
||||||
|
threads,
|
||||||
|
header.plaintext_length,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = Zeroizing::new(vec![0u8; cipher_chunk]);
|
||||||
let mut counter: u32 = 0;
|
let mut counter: u32 = 0;
|
||||||
|
let mut bytes_written: u64 = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match f_encrypted.read_ahead(&mut buf)? {
|
match f_encrypted.read_ahead(&mut buf)? {
|
||||||
ReadInfoChunk::Normal(_) => {
|
ReadInfoChunk::Normal(_) => {
|
||||||
let nonce = make_nonce(&header.nonce_prefix, counter, false);
|
let nonce = make_nonce(&header.nonce_prefix, counter, false);
|
||||||
aead.decrypt_in_place(&nonce, &aad, &mut buf)?;
|
aead.decrypt_in_place(&nonce, &aad, &mut *buf)?;
|
||||||
f_plain.write_all(&buf)?;
|
f_plain.write_all(&buf)?;
|
||||||
|
bytes_written =
|
||||||
|
policy::checked_count_add(bytes_written, buf.len(), "bytes written")?;
|
||||||
buf.resize(cipher_chunk, 0);
|
buf.resize(cipher_chunk, 0);
|
||||||
counter = counter
|
counter = bump_counter(counter)?;
|
||||||
.checked_add(1)
|
|
||||||
.ok_or_else(|| FcryError::Format("STREAM counter overflow".into()))?;
|
|
||||||
}
|
}
|
||||||
ReadInfoChunk::Last(n) => {
|
ReadInfoChunk::Last(n) => {
|
||||||
buf.truncate(n);
|
buf.truncate(n);
|
||||||
let nonce = make_nonce(&header.nonce_prefix, counter, true);
|
let nonce = make_nonce(&header.nonce_prefix, counter, true);
|
||||||
aead.decrypt_in_place(&nonce, &aad, &mut buf)?;
|
aead.decrypt_in_place(&nonce, &aad, &mut *buf)?;
|
||||||
f_plain.write_all(&buf)?;
|
f_plain.write_all(&buf)?;
|
||||||
|
bytes_written =
|
||||||
|
policy::checked_count_add(bytes_written, buf.len(), "bytes written")?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ReadInfoChunk::Empty => {
|
ReadInfoChunk::Empty => {
|
||||||
@@ -171,6 +332,290 @@ pub fn decrypt<S: AsRef<str>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(committed) = header.plaintext_length
|
||||||
|
&& committed != bytes_written
|
||||||
|
{
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"decrypted length {bytes_written} disagrees with committed {committed}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
f_plain.commit()?;
|
f_plain.commit()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Random-access decrypt of a byte range. Requires a seekable input file
|
||||||
|
/// whose header has `FLAG_LENGTH_COMMITTED` set, so we know exactly where
|
||||||
|
/// each ciphertext chunk lives and which chunk is the last (its nonce uses
|
||||||
|
/// the STREAM last-block flag).
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn decrypt_range<S: AsRef<str>>(
|
||||||
|
input_file: &str,
|
||||||
|
output_file: Option<S>,
|
||||||
|
raw_key: Option<&SecretBytes32>,
|
||||||
|
passphrase: Option<&SecretVec>,
|
||||||
|
offset: u64,
|
||||||
|
length: u64,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
decrypt_range_with_argon_cap(
|
||||||
|
input_file,
|
||||||
|
output_file,
|
||||||
|
raw_key,
|
||||||
|
passphrase,
|
||||||
|
offset,
|
||||||
|
length,
|
||||||
|
policy::default_argon_decrypt_cap_mib(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn decrypt_range_with_argon_cap<S: AsRef<str>>(
|
||||||
|
input_file: &str,
|
||||||
|
output_file: Option<S>,
|
||||||
|
raw_key: Option<&SecretBytes32>,
|
||||||
|
passphrase: Option<&SecretVec>,
|
||||||
|
offset: u64,
|
||||||
|
length: u64,
|
||||||
|
max_argon_memory_mib: u32,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
decrypt_range_with_output_options(
|
||||||
|
input_file,
|
||||||
|
output_file,
|
||||||
|
raw_key,
|
||||||
|
passphrase,
|
||||||
|
offset,
|
||||||
|
length,
|
||||||
|
max_argon_memory_mib,
|
||||||
|
&OutSinkOptions::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn decrypt_range_with_output_options<S: AsRef<str>>(
|
||||||
|
input_file: &str,
|
||||||
|
output_file: Option<S>,
|
||||||
|
raw_key: Option<&SecretBytes32>,
|
||||||
|
passphrase: Option<&SecretVec>,
|
||||||
|
offset: u64,
|
||||||
|
length: u64,
|
||||||
|
max_argon_memory_mib: u32,
|
||||||
|
output_options: &OutSinkOptions,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
if length == 0 {
|
||||||
|
return Err(FcryError::Format("--length 0 is not allowed".into()));
|
||||||
|
}
|
||||||
|
let file = File::open(input_file)?;
|
||||||
|
let mut reader = BufReader::new(file);
|
||||||
|
let header = Header::read_with_argon_cap(&mut reader, max_argon_memory_mib)?;
|
||||||
|
let aad = header.encode();
|
||||||
|
let header_len = aad.len() as u64;
|
||||||
|
|
||||||
|
let total = header.plaintext_length.ok_or_else(|| {
|
||||||
|
FcryError::Format(
|
||||||
|
"random-access decrypt requires a length-committed header (encrypt from a regular file)".into(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let end = offset
|
||||||
|
.checked_add(length)
|
||||||
|
.ok_or_else(|| FcryError::Format("offset + length overflows u64".into()))?;
|
||||||
|
if end > total {
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"range [{offset}, {end}) exceeds plaintext length {total}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = derive_key(&header.kdf, raw_key, passphrase)?;
|
||||||
|
verify_key_commitment(&header, &key)?;
|
||||||
|
let aead = build_aead(&key);
|
||||||
|
|
||||||
|
let chunk_sz_usize = policy::validate_chunk_size(header.chunk_size)?;
|
||||||
|
let cipher_chunk_usize = policy::cipher_chunk_len(chunk_sz_usize)?;
|
||||||
|
let chunk_sz = chunk_sz_usize as u64;
|
||||||
|
let cipher_chunk = cipher_chunk_usize as u64;
|
||||||
|
|
||||||
|
// Layout invariants:
|
||||||
|
// n_chunks = ceil(total / chunk_sz), but always ≥ 1 (the empty file
|
||||||
|
// still authenticates a single empty "last" chunk).
|
||||||
|
// last_idx = n_chunks - 1
|
||||||
|
// last_pt = total - last_idx * chunk_sz (in [0, chunk_sz])
|
||||||
|
let (n_chunks, last_pt) = if total == 0 {
|
||||||
|
(1u64, 0u64)
|
||||||
|
} else {
|
||||||
|
let n = total.div_ceil(chunk_sz);
|
||||||
|
let before_last = policy::checked_mul_u64(n - 1, chunk_sz, "last chunk offset")?;
|
||||||
|
let last = total
|
||||||
|
.checked_sub(before_last)
|
||||||
|
.ok_or_else(|| FcryError::Format("last chunk length underflow".into()))?;
|
||||||
|
(n, last)
|
||||||
|
};
|
||||||
|
let last_idx = n_chunks - 1;
|
||||||
|
|
||||||
|
let mut out = OutSink::open_with_options(output_file, output_options)?;
|
||||||
|
|
||||||
|
let start_chunk = offset / chunk_sz;
|
||||||
|
let end_chunk = (end - 1) / chunk_sz;
|
||||||
|
|
||||||
|
// Reusable buffer sized to a full chunk + tag.
|
||||||
|
let mut buf = Zeroizing::new(Vec::with_capacity(cipher_chunk_usize));
|
||||||
|
|
||||||
|
let mut file = reader.into_inner();
|
||||||
|
|
||||||
|
for i in start_chunk..=end_chunk {
|
||||||
|
let i_u32 =
|
||||||
|
u32::try_from(i).map_err(|_| FcryError::Format("chunk index exceeds u32".into()))?;
|
||||||
|
let is_last = i == last_idx;
|
||||||
|
let cipher_len = if is_last {
|
||||||
|
last_pt + TAG_LEN as u64
|
||||||
|
} else {
|
||||||
|
cipher_chunk
|
||||||
|
};
|
||||||
|
let cipher_len_usz =
|
||||||
|
usize::try_from(cipher_len).map_err(|_| FcryError::Format("chunk too big".into()))?;
|
||||||
|
|
||||||
|
let chunk_offset = policy::checked_add_u64(
|
||||||
|
header_len,
|
||||||
|
policy::checked_mul_u64(i, cipher_chunk, "ciphertext chunk offset")?,
|
||||||
|
"ciphertext chunk offset",
|
||||||
|
)?;
|
||||||
|
file.seek(SeekFrom::Start(chunk_offset))?;
|
||||||
|
buf.clear();
|
||||||
|
buf.resize(cipher_len_usz, 0);
|
||||||
|
file.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
let nonce = make_nonce(&header.nonce_prefix, i_u32, is_last);
|
||||||
|
aead.decrypt_in_place(&nonce, &aad, &mut *buf)?;
|
||||||
|
|
||||||
|
// `buf` is now plaintext for this chunk. Compute the chunk's plaintext
|
||||||
|
// window in absolute bytes and intersect with the requested range.
|
||||||
|
let chunk_start = policy::checked_mul_u64(i, chunk_sz, "plaintext chunk offset")?;
|
||||||
|
let chunk_end = policy::checked_count_add(chunk_start, buf.len(), "plaintext chunk end")?;
|
||||||
|
let lo = offset.max(chunk_start) - chunk_start;
|
||||||
|
let hi = end.min(chunk_end) - chunk_start;
|
||||||
|
out.write_all(&buf[lo as usize..hi as usize])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
out.commit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
//! Regression tests for cross-version compatibility. The on-disk header
|
||||||
|
//! is part of the AEAD AAD, so any byte that ends up in `Header::encode()`
|
||||||
|
//! must match the bytes that were authenticated when the file was
|
||||||
|
//! written. The v1 test below catches the regression where `encode()`
|
||||||
|
//! used to hard-code the current version on output.
|
||||||
|
use super::*;
|
||||||
|
use crate::header::{Header, KdfParams, NONCE_PREFIX_LEN};
|
||||||
|
use std::fs;
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
|
fn write_v1_ciphertext(path: &std::path::Path, key: &SecretBytes32, plaintext: &[u8]) {
|
||||||
|
// Build a v1 header by hand: same wire format as v2 with flags=0,
|
||||||
|
// but with version byte = 1.
|
||||||
|
let nonce_prefix = [0x42u8; NONCE_PREFIX_LEN];
|
||||||
|
let header = Header {
|
||||||
|
version: 1,
|
||||||
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
|
flags: 0,
|
||||||
|
chunk_size: 64,
|
||||||
|
kdf: KdfParams::Raw,
|
||||||
|
nonce_prefix,
|
||||||
|
plaintext_length: None,
|
||||||
|
key_commitment: None,
|
||||||
|
};
|
||||||
|
let aad = header.encode();
|
||||||
|
// First byte after MAGIC is the version — verify our fixture really
|
||||||
|
// is v1 (so this test fails open if encode() ever reverts).
|
||||||
|
assert_eq!(aad[4], 1);
|
||||||
|
|
||||||
|
let chunk_sz = header.chunk_size as usize;
|
||||||
|
let aead = build_aead(key);
|
||||||
|
|
||||||
|
let mut out = Vec::new();
|
||||||
|
out.extend_from_slice(&aad);
|
||||||
|
|
||||||
|
let mut counter: u32 = 0;
|
||||||
|
let mut pos = 0;
|
||||||
|
while pos < plaintext.len() {
|
||||||
|
let end = (pos + chunk_sz).min(plaintext.len());
|
||||||
|
let last = end == plaintext.len() && (end - pos) < chunk_sz;
|
||||||
|
let mut buf = plaintext[pos..end].to_vec();
|
||||||
|
let nonce = make_nonce(&nonce_prefix, counter, last);
|
||||||
|
aead.encrypt_in_place(&nonce, &aad, &mut buf).unwrap();
|
||||||
|
out.extend_from_slice(&buf);
|
||||||
|
pos = end;
|
||||||
|
if last {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// If we hit a chunk-boundary EOF we still need a trailing "last"
|
||||||
|
// empty chunk to authenticate end-of-stream.
|
||||||
|
counter = bump_counter(counter).unwrap();
|
||||||
|
if pos == plaintext.len() {
|
||||||
|
let mut empty = Vec::new();
|
||||||
|
let nonce = make_nonce(&nonce_prefix, counter, true);
|
||||||
|
aead.encrypt_in_place(&nonce, &aad, &mut empty).unwrap();
|
||||||
|
out.extend_from_slice(&empty);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Empty plaintext: emit a single empty "last" chunk.
|
||||||
|
if plaintext.is_empty() {
|
||||||
|
let mut empty = Vec::new();
|
||||||
|
let nonce = make_nonce(&nonce_prefix, 0, true);
|
||||||
|
aead.encrypt_in_place(&nonce, &aad, &mut empty).unwrap();
|
||||||
|
out.extend_from_slice(&empty);
|
||||||
|
}
|
||||||
|
fs::write(path, &out).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn decrypts_v1_ciphertext() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let ct = dir.path().join("v1.bin");
|
||||||
|
let rt = dir.path().join("rt.bin");
|
||||||
|
|
||||||
|
let mut key = SecretBytes32::zeroed();
|
||||||
|
key.with_mut_array(|k| k.copy_from_slice(b"0123456789abcdef0123456789abcdef"));
|
||||||
|
|
||||||
|
// Multi-chunk plaintext (chunk_size = 64 in the fixture).
|
||||||
|
let plain: Vec<u8> = (0..200u8).collect();
|
||||||
|
write_v1_ciphertext(&ct, &key, &plain);
|
||||||
|
|
||||||
|
decrypt(
|
||||||
|
Some(ct.to_str().unwrap()),
|
||||||
|
Some(rt.to_str().unwrap()),
|
||||||
|
Some(&key),
|
||||||
|
None,
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
.expect("v1 decrypt should succeed");
|
||||||
|
let got = fs::read(&rt).unwrap();
|
||||||
|
assert_eq!(got, plain);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn decrypts_v1_ciphertext_parallel() {
|
||||||
|
// Same fixture, but exercising the multi-threaded pipeline.
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let ct = dir.path().join("v1.bin");
|
||||||
|
let rt = dir.path().join("rt.bin");
|
||||||
|
|
||||||
|
let mut key = SecretBytes32::zeroed();
|
||||||
|
key.with_mut_array(|k| k.copy_from_slice(b"0123456789abcdef0123456789abcdef"));
|
||||||
|
|
||||||
|
let plain: Vec<u8> = (0..200u8).collect();
|
||||||
|
write_v1_ciphertext(&ct, &key, &plain);
|
||||||
|
|
||||||
|
decrypt(
|
||||||
|
Some(ct.to_str().unwrap()),
|
||||||
|
Some(rt.to_str().unwrap()),
|
||||||
|
Some(&key),
|
||||||
|
None,
|
||||||
|
4,
|
||||||
|
)
|
||||||
|
.expect("v1 parallel decrypt should succeed");
|
||||||
|
assert_eq!(fs::read(&rt).unwrap(), plain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub enum FcryError {
|
|||||||
Format(String),
|
Format(String),
|
||||||
Kdf(String),
|
Kdf(String),
|
||||||
Passphrase(String),
|
Passphrase(String),
|
||||||
|
WrongKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for FcryError {
|
impl From<io::Error> for FcryError {
|
||||||
|
|||||||
+215
-22
@@ -4,34 +4,55 @@
|
|||||||
//!
|
//!
|
||||||
//! Layout:
|
//! Layout:
|
||||||
//! ```text
|
//! ```text
|
||||||
//! magic "fcry" 4 bytes
|
//! magic "fcry" 4 bytes
|
||||||
//! version u8 1
|
//! version u8 1
|
||||||
//! alg_id u8 1
|
//! alg_id u8 1
|
||||||
//! flags u8 1
|
//! flags u8 1
|
||||||
//! reserved u8 1 (must be 0)
|
//! reserved u8 1 (must be 0)
|
||||||
//! chunk_size u32 LE 4 (plaintext bytes per chunk)
|
//! chunk_size u32 LE 4 (plaintext bytes per chunk)
|
||||||
//! kdf_id u8 1
|
//! kdf_id u8 1
|
||||||
//! kdf_params variable (depends on kdf_id)
|
//! kdf_params variable (depends on kdf_id)
|
||||||
//! nonce_prefix [u8; 19] 19 (STREAM nonce prefix)
|
//! nonce_prefix [u8; 19] 19 (STREAM nonce prefix)
|
||||||
|
//! plaintext_length u64 LE 8 (only if version >= 2 and flags & 0x01)
|
||||||
|
//! key_commitment [u8; 32] 32 (only if version >= 3 and flags & 0x02)
|
||||||
//! --- end of header ---
|
//! --- end of header ---
|
||||||
//! chunk[0..N] each chunk_size + 16 bytes,
|
//! chunk[0..N] each chunk_size + 16 bytes,
|
||||||
//! last may be shorter
|
//! last may be shorter
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! The full encoded header is fed as AAD to every chunk, so any tampering
|
//! The full encoded header is fed as AAD to every chunk, so any tampering
|
||||||
//! with chunk_size, nonce_prefix, kdf params, etc. causes authentication
|
//! with chunk_size, nonce_prefix, kdf params, plaintext_length, etc. causes
|
||||||
//! failure on every chunk.
|
//! authentication failure on every chunk.
|
||||||
|
//!
|
||||||
|
//! Versions:
|
||||||
|
//! * v1 — no length committed, no flag bits used.
|
||||||
|
//! * v2 — adds `FLAG_LENGTH_COMMITTED` (bit 0); when set, the total plaintext
|
||||||
|
//! length is appended after `nonce_prefix`. This enables random-access
|
||||||
|
//! decryption without scanning predecessors.
|
||||||
|
//! * v3 — adds `FLAG_KEY_COMMITTED` (bit 1) and an authenticated key
|
||||||
|
//! commitment for fast wrong-key detection before chunk processing.
|
||||||
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
use crate::error::FcryError;
|
use crate::error::FcryError;
|
||||||
|
use crate::policy;
|
||||||
|
|
||||||
const MAGIC: [u8; 4] = *b"fcry";
|
const MAGIC: [u8; 4] = *b"fcry";
|
||||||
const VERSION: u8 = 1;
|
pub const VERSION_CURRENT: u8 = 3;
|
||||||
|
const VERSION_MIN: u8 = 1;
|
||||||
|
|
||||||
pub const NONCE_PREFIX_LEN: usize = 19;
|
pub const NONCE_PREFIX_LEN: usize = 19;
|
||||||
pub const TAG_LEN: usize = 16;
|
pub const TAG_LEN: usize = 16;
|
||||||
|
|
||||||
|
/// Set in `flags` when the header carries an authenticated `plaintext_length`
|
||||||
|
/// field. Required for random-access decryption.
|
||||||
|
pub const FLAG_LENGTH_COMMITTED: u8 = 0x01;
|
||||||
|
pub const FLAG_KEY_COMMITTED: u8 = 0x02;
|
||||||
|
|
||||||
|
/// Mask of all flag bits this build understands. Unknown bits → reject.
|
||||||
|
const FLAG_KNOWN_MASK: u8 = FLAG_LENGTH_COMMITTED | FLAG_KEY_COMMITTED;
|
||||||
|
pub const KEY_COMMITMENT_LEN: usize = 32;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum AlgId {
|
pub enum AlgId {
|
||||||
@@ -116,18 +137,26 @@ impl KdfParams {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
|
/// On-disk format version. Set to `VERSION_CURRENT` for new encrypts;
|
||||||
|
/// preserved as-read for decrypt so the AAD recomputed on decode matches
|
||||||
|
/// the bytes that were authenticated when the file was written.
|
||||||
|
pub version: u8,
|
||||||
pub alg: AlgId,
|
pub alg: AlgId,
|
||||||
pub flags: u8,
|
pub flags: u8,
|
||||||
pub chunk_size: u32,
|
pub chunk_size: u32,
|
||||||
pub kdf: KdfParams,
|
pub kdf: KdfParams,
|
||||||
pub nonce_prefix: [u8; NONCE_PREFIX_LEN],
|
pub nonce_prefix: [u8; NONCE_PREFIX_LEN],
|
||||||
|
/// Total plaintext byte count. `Some` iff `flags & FLAG_LENGTH_COMMITTED`.
|
||||||
|
pub plaintext_length: Option<u64>,
|
||||||
|
/// v3 key commitment. `Some` iff `flags & FLAG_KEY_COMMITTED`.
|
||||||
|
pub key_commitment: Option<[u8; KEY_COMMITMENT_LEN]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Header {
|
impl Header {
|
||||||
pub fn encode(&self) -> Vec<u8> {
|
fn encode_without_commitment(&self) -> Vec<u8> {
|
||||||
let mut out = Vec::with_capacity(64);
|
let mut out = Vec::with_capacity(104);
|
||||||
out.extend_from_slice(&MAGIC);
|
out.extend_from_slice(&MAGIC);
|
||||||
out.push(VERSION);
|
out.push(self.version);
|
||||||
out.push(self.alg as u8);
|
out.push(self.alg as u8);
|
||||||
out.push(self.flags);
|
out.push(self.flags);
|
||||||
out.push(0); // reserved
|
out.push(0); // reserved
|
||||||
@@ -135,10 +164,39 @@ impl Header {
|
|||||||
out.push(self.kdf.id());
|
out.push(self.kdf.id());
|
||||||
self.kdf.write_into(&mut out);
|
self.kdf.write_into(&mut out);
|
||||||
out.extend_from_slice(&self.nonce_prefix);
|
out.extend_from_slice(&self.nonce_prefix);
|
||||||
|
if (self.flags & FLAG_LENGTH_COMMITTED) != 0 {
|
||||||
|
let len = self
|
||||||
|
.plaintext_length
|
||||||
|
.expect("FLAG_LENGTH_COMMITTED set but plaintext_length is None");
|
||||||
|
out.extend_from_slice(&len.to_le_bytes());
|
||||||
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn encode(&self) -> Vec<u8> {
|
||||||
|
let mut out = self.encode_without_commitment();
|
||||||
|
if (self.flags & FLAG_KEY_COMMITTED) != 0 {
|
||||||
|
let commitment = self
|
||||||
|
.key_commitment
|
||||||
|
.expect("FLAG_KEY_COMMITTED set but key_commitment is None");
|
||||||
|
out.extend_from_slice(&commitment);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn commitment_input_encoding(&self) -> Vec<u8> {
|
||||||
|
self.encode_without_commitment()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn read(r: &mut impl Read) -> Result<Self, FcryError> {
|
pub fn read(r: &mut impl Read) -> Result<Self, FcryError> {
|
||||||
|
Self::read_with_argon_cap(r, policy::default_argon_decrypt_cap_mib())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_with_argon_cap(
|
||||||
|
r: &mut impl Read,
|
||||||
|
max_argon_memory_mib: u32,
|
||||||
|
) -> Result<Self, FcryError> {
|
||||||
let mut magic = [0u8; 4];
|
let mut magic = [0u8; 4];
|
||||||
r.read_exact(&mut magic)?;
|
r.read_exact(&mut magic)?;
|
||||||
if magic != MAGIC {
|
if magic != MAGIC {
|
||||||
@@ -148,34 +206,68 @@ impl Header {
|
|||||||
let mut fixed = [0u8; 4];
|
let mut fixed = [0u8; 4];
|
||||||
r.read_exact(&mut fixed)?;
|
r.read_exact(&mut fixed)?;
|
||||||
let [version, alg_id, flags, reserved] = fixed;
|
let [version, alg_id, flags, reserved] = fixed;
|
||||||
if version != VERSION {
|
if !(VERSION_MIN..=VERSION_CURRENT).contains(&version) {
|
||||||
return Err(FcryError::Format(format!("unsupported version: {version}")));
|
return Err(FcryError::Format(format!("unsupported version: {version}")));
|
||||||
}
|
}
|
||||||
if reserved != 0 {
|
if reserved != 0 {
|
||||||
return Err(FcryError::Format("reserved byte must be zero".into()));
|
return Err(FcryError::Format("reserved byte must be zero".into()));
|
||||||
}
|
}
|
||||||
|
if (flags & !FLAG_KNOWN_MASK) != 0 {
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"unknown flag bits: 0x{flags:02x}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if version < 2 && flags != 0 {
|
||||||
|
return Err(FcryError::Format("v1 header must have flags == 0".into()));
|
||||||
|
}
|
||||||
|
if version < 3 && (flags & FLAG_KEY_COMMITTED) != 0 {
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"key commitment flag requires v3 header".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if version >= 3 && (flags & FLAG_KEY_COMMITTED) == 0 {
|
||||||
|
return Err(FcryError::Format("v3 header must commit the key".into()));
|
||||||
|
}
|
||||||
let alg = AlgId::from_u8(alg_id)?;
|
let alg = AlgId::from_u8(alg_id)?;
|
||||||
|
|
||||||
let mut chunk_size_bytes = [0u8; 4];
|
let mut chunk_size_bytes = [0u8; 4];
|
||||||
r.read_exact(&mut chunk_size_bytes)?;
|
r.read_exact(&mut chunk_size_bytes)?;
|
||||||
let chunk_size = u32::from_le_bytes(chunk_size_bytes);
|
let chunk_size = u32::from_le_bytes(chunk_size_bytes);
|
||||||
if chunk_size == 0 {
|
policy::validate_chunk_size(chunk_size)?;
|
||||||
return Err(FcryError::Format("chunk_size must be > 0".into()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut kdf_id = [0u8; 1];
|
let mut kdf_id = [0u8; 1];
|
||||||
r.read_exact(&mut kdf_id)?;
|
r.read_exact(&mut kdf_id)?;
|
||||||
let kdf = KdfParams::read_from(kdf_id[0], r)?;
|
let kdf = KdfParams::read_from(kdf_id[0], r)?;
|
||||||
|
policy::validate_header_kdf(&kdf, max_argon_memory_mib)?;
|
||||||
|
|
||||||
let mut nonce_prefix = [0u8; NONCE_PREFIX_LEN];
|
let mut nonce_prefix = [0u8; NONCE_PREFIX_LEN];
|
||||||
r.read_exact(&mut nonce_prefix)?;
|
r.read_exact(&mut nonce_prefix)?;
|
||||||
|
|
||||||
|
let plaintext_length = if (flags & FLAG_LENGTH_COMMITTED) != 0 {
|
||||||
|
let mut b = [0u8; 8];
|
||||||
|
r.read_exact(&mut b)?;
|
||||||
|
Some(u64::from_le_bytes(b))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let key_commitment = if (flags & FLAG_KEY_COMMITTED) != 0 {
|
||||||
|
let mut b = [0u8; KEY_COMMITMENT_LEN];
|
||||||
|
r.read_exact(&mut b)?;
|
||||||
|
Some(b)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
version,
|
||||||
alg,
|
alg,
|
||||||
flags,
|
flags,
|
||||||
chunk_size,
|
chunk_size,
|
||||||
kdf,
|
kdf,
|
||||||
nonce_prefix,
|
nonce_prefix,
|
||||||
|
plaintext_length,
|
||||||
|
key_commitment,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,30 +280,87 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn roundtrip() {
|
fn roundtrip() {
|
||||||
let h = Header {
|
let h = Header {
|
||||||
|
version: VERSION_CURRENT,
|
||||||
alg: AlgId::XChaCha20Poly1305,
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
flags: 0,
|
flags: FLAG_KEY_COMMITTED,
|
||||||
chunk_size: 1024 * 1024,
|
chunk_size: 1024 * 1024,
|
||||||
kdf: KdfParams::Raw,
|
kdf: KdfParams::Raw,
|
||||||
nonce_prefix: [7u8; NONCE_PREFIX_LEN],
|
nonce_prefix: [7u8; NONCE_PREFIX_LEN],
|
||||||
|
plaintext_length: None,
|
||||||
|
key_commitment: Some([1u8; KEY_COMMITMENT_LEN]),
|
||||||
};
|
};
|
||||||
let bytes = h.encode();
|
let bytes = h.encode();
|
||||||
let mut cur = Cursor::new(&bytes);
|
let mut cur = Cursor::new(&bytes);
|
||||||
let parsed = Header::read(&mut cur).unwrap();
|
let parsed = Header::read(&mut cur).unwrap();
|
||||||
|
assert_eq!(parsed.version, h.version);
|
||||||
assert_eq!(parsed.alg, h.alg);
|
assert_eq!(parsed.alg, h.alg);
|
||||||
assert_eq!(parsed.flags, h.flags);
|
assert_eq!(parsed.flags, h.flags);
|
||||||
assert_eq!(parsed.chunk_size, h.chunk_size);
|
assert_eq!(parsed.chunk_size, h.chunk_size);
|
||||||
assert_eq!(parsed.nonce_prefix, h.nonce_prefix);
|
assert_eq!(parsed.nonce_prefix, h.nonce_prefix);
|
||||||
|
assert_eq!(parsed.plaintext_length, None);
|
||||||
|
assert_eq!(parsed.key_commitment, h.key_commitment);
|
||||||
assert_eq!(cur.position() as usize, bytes.len());
|
assert_eq!(cur.position() as usize, bytes.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn roundtrip_length_committed() {
|
||||||
|
let h = Header {
|
||||||
|
version: VERSION_CURRENT,
|
||||||
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
|
flags: FLAG_LENGTH_COMMITTED | FLAG_KEY_COMMITTED,
|
||||||
|
chunk_size: 65536,
|
||||||
|
kdf: KdfParams::Raw,
|
||||||
|
nonce_prefix: [9u8; NONCE_PREFIX_LEN],
|
||||||
|
plaintext_length: Some(123_456_789),
|
||||||
|
key_commitment: Some([2u8; KEY_COMMITMENT_LEN]),
|
||||||
|
};
|
||||||
|
let bytes = h.encode();
|
||||||
|
let mut cur = Cursor::new(&bytes);
|
||||||
|
let parsed = Header::read(&mut cur).unwrap();
|
||||||
|
assert_eq!(parsed.flags, FLAG_LENGTH_COMMITTED | FLAG_KEY_COMMITTED);
|
||||||
|
assert_eq!(parsed.plaintext_length, Some(123_456_789));
|
||||||
|
assert_eq!(parsed.key_commitment, h.key_commitment);
|
||||||
|
assert_eq!(cur.position() as usize, bytes.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn v3_encoding_layout_stable() {
|
||||||
|
let h = Header {
|
||||||
|
version: VERSION_CURRENT,
|
||||||
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
|
flags: FLAG_LENGTH_COMMITTED | FLAG_KEY_COMMITTED,
|
||||||
|
chunk_size: 0x0102_0304,
|
||||||
|
kdf: KdfParams::Raw,
|
||||||
|
nonce_prefix: [0x55u8; NONCE_PREFIX_LEN],
|
||||||
|
plaintext_length: Some(0x0807_0605_0403_0201),
|
||||||
|
key_commitment: Some([0xaau8; KEY_COMMITMENT_LEN]),
|
||||||
|
};
|
||||||
|
let commitment_input = h.commitment_input_encoding();
|
||||||
|
assert_eq!(commitment_input.len(), 40);
|
||||||
|
assert_eq!(&commitment_input[..4], b"fcry");
|
||||||
|
assert_eq!(commitment_input[4], 3);
|
||||||
|
assert_eq!(
|
||||||
|
&commitment_input[32..40],
|
||||||
|
&0x0807_0605_0403_0201u64.to_le_bytes()
|
||||||
|
);
|
||||||
|
|
||||||
|
let aad = h.encode();
|
||||||
|
assert_eq!(aad.len(), 72);
|
||||||
|
assert_eq!(&aad[..40], &commitment_input);
|
||||||
|
assert_eq!(&aad[40..], &[0xaau8; KEY_COMMITMENT_LEN]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rejects_bad_magic() {
|
fn rejects_bad_magic() {
|
||||||
let mut bytes = Header {
|
let mut bytes = Header {
|
||||||
|
version: VERSION_CURRENT,
|
||||||
alg: AlgId::XChaCha20Poly1305,
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
chunk_size: 4096,
|
chunk_size: 4096,
|
||||||
kdf: KdfParams::Raw,
|
kdf: KdfParams::Raw,
|
||||||
nonce_prefix: [0u8; NONCE_PREFIX_LEN],
|
nonce_prefix: [0u8; NONCE_PREFIX_LEN],
|
||||||
|
plaintext_length: None,
|
||||||
|
key_commitment: Some([3u8; KEY_COMMITMENT_LEN]),
|
||||||
}
|
}
|
||||||
.encode();
|
.encode();
|
||||||
bytes[0] ^= 1;
|
bytes[0] ^= 1;
|
||||||
@@ -220,4 +369,48 @@ mod tests {
|
|||||||
Err(FcryError::Format(_))
|
Err(FcryError::Format(_))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejects_unknown_flag_bits() {
|
||||||
|
let mut bytes = Header {
|
||||||
|
version: VERSION_CURRENT,
|
||||||
|
alg: AlgId::XChaCha20Poly1305,
|
||||||
|
flags: 0,
|
||||||
|
chunk_size: 4096,
|
||||||
|
kdf: KdfParams::Raw,
|
||||||
|
nonce_prefix: [0u8; NONCE_PREFIX_LEN],
|
||||||
|
plaintext_length: None,
|
||||||
|
key_commitment: Some([4u8; KEY_COMMITMENT_LEN]),
|
||||||
|
}
|
||||||
|
.encode();
|
||||||
|
// flags byte is at offset 6 (4 magic + version + alg)
|
||||||
|
bytes[6] = 0x80;
|
||||||
|
assert!(matches!(
|
||||||
|
Header::read(&mut Cursor::new(&bytes)),
|
||||||
|
Err(FcryError::Format(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reads_v1_header() {
|
||||||
|
// hand-crafted v1 header (raw kdf, no length field)
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
bytes.extend_from_slice(b"fcry");
|
||||||
|
bytes.push(1); // version
|
||||||
|
bytes.push(1); // alg
|
||||||
|
bytes.push(0); // flags
|
||||||
|
bytes.push(0); // reserved
|
||||||
|
bytes.extend_from_slice(&1024u32.to_le_bytes());
|
||||||
|
bytes.push(0); // kdf id raw
|
||||||
|
bytes.extend_from_slice(&[3u8; NONCE_PREFIX_LEN]);
|
||||||
|
let parsed = Header::read(&mut Cursor::new(&bytes)).unwrap();
|
||||||
|
assert_eq!(parsed.version, 1);
|
||||||
|
assert_eq!(parsed.flags, 0);
|
||||||
|
assert_eq!(parsed.chunk_size, 1024);
|
||||||
|
assert_eq!(parsed.plaintext_length, None);
|
||||||
|
assert_eq!(parsed.key_commitment, None);
|
||||||
|
// Re-encoding must reproduce the original v1 bytes exactly so the
|
||||||
|
// recomputed AAD matches what the file was authenticated with.
|
||||||
|
assert_eq!(parsed.encode(), bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+225
-32
@@ -3,6 +3,8 @@
|
|||||||
mod crypto;
|
mod crypto;
|
||||||
mod error;
|
mod error;
|
||||||
mod header;
|
mod header;
|
||||||
|
mod pipeline;
|
||||||
|
mod policy;
|
||||||
mod reader;
|
mod reader;
|
||||||
mod secrets;
|
mod secrets;
|
||||||
mod utils;
|
mod utils;
|
||||||
@@ -11,9 +13,13 @@ use crypto::*;
|
|||||||
use error::FcryError;
|
use error::FcryError;
|
||||||
use header::{ARGON2_SALT_LEN, KdfParams};
|
use header::{ARGON2_SALT_LEN, KdfParams};
|
||||||
use secrets::{SecretBytes32, SecretVec, read_passphrase_tty};
|
use secrets::{SecretBytes32, SecretVec, read_passphrase_tty};
|
||||||
use utils::DEFAULT_CHUNK_SIZE;
|
use utils::{DEFAULT_CHUNK_SIZE, OutSinkOptions};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use unicode_normalization::UnicodeNormalization;
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
/// fcry - [f]ile[cry]pt: A file en-/decryption tool for easy use
|
/// fcry - [f]ile[cry]pt: A file en-/decryption tool for easy use
|
||||||
@@ -33,10 +39,9 @@ struct Cli {
|
|||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
output_file: Option<String>,
|
output_file: Option<String>,
|
||||||
|
|
||||||
/// The raw bytes of the crypto key. Has to be exactly 32 bytes.
|
/// Read the raw 32-byte crypto key from a file.
|
||||||
/// *** DANGEROUS: visible in process listings (ps/proc). Testing only. ***
|
#[clap(short = 'k', long, conflicts_with_all = ["passphrase", "passphrase_env"])]
|
||||||
#[clap(short, long, conflicts_with_all = ["passphrase", "passphrase_env"])]
|
key_file: Option<PathBuf>,
|
||||||
raw_key: Option<Zeroizing<String>>,
|
|
||||||
|
|
||||||
/// Read passphrase interactively (terminal). Implies argon2id KDF on encrypt.
|
/// Read passphrase interactively (terminal). Implies argon2id KDF on encrypt.
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
@@ -52,37 +57,135 @@ struct Cli {
|
|||||||
chunk_size: u32,
|
chunk_size: u32,
|
||||||
|
|
||||||
/// Argon2id memory in MiB (encryption only). Default: 1024 (= 1 GiB).
|
/// Argon2id memory in MiB (encryption only). Default: 1024 (= 1 GiB).
|
||||||
#[clap(long, default_value_t = 1024)]
|
#[clap(long, default_value_t = policy::DEFAULT_ARGON_MEMORY_MIB)]
|
||||||
argon_memory: u32,
|
argon_memory: u32,
|
||||||
|
|
||||||
/// Argon2id passes / iterations (encryption only).
|
/// Argon2id passes / iterations (encryption only).
|
||||||
#[clap(long, default_value_t = 2)]
|
#[clap(long, default_value_t = policy::MIN_ARGON_PASSES)]
|
||||||
argon_passes: u32,
|
argon_passes: u32,
|
||||||
|
|
||||||
/// Argon2id parallelism / lanes (encryption only).
|
/// Argon2id parallelism / lanes (encryption only).
|
||||||
#[clap(long, default_value_t = 4)]
|
#[clap(long, default_value_t = policy::DEFAULT_ARGON_PARALLELISM)]
|
||||||
argon_parallelism: u32,
|
argon_parallelism: u32,
|
||||||
|
|
||||||
|
/// Permit intentionally weak passphrase/KDF parameters for tests or legacy interop.
|
||||||
|
#[clap(long)]
|
||||||
|
allow_weak_kdf: bool,
|
||||||
|
|
||||||
|
/// Maximum Argon2id memory accepted while decrypting, in MiB.
|
||||||
|
/// Overrides the dynamic default. Raising it can OOM constrained machines.
|
||||||
|
#[clap(long)]
|
||||||
|
max_argon_memory_mib: Option<u32>,
|
||||||
|
|
||||||
|
/// Number of worker threads for AEAD work. Defaults to the number of
|
||||||
|
/// available CPUs. Set to 1 for fully serial encrypt/decrypt.
|
||||||
|
#[clap(short = 'j', long, value_parser = clap::value_parser!(u32).range(1..))]
|
||||||
|
threads: Option<u32>,
|
||||||
|
|
||||||
|
/// Replace an existing different output file after encryption/decryption succeeds.
|
||||||
|
#[clap(long)]
|
||||||
|
force: bool,
|
||||||
|
|
||||||
|
/// Directory for private temporary files.
|
||||||
|
#[clap(long)]
|
||||||
|
temp_dir: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// For decrypt-to-stdout, verify the whole plaintext in a private temp file before emitting it.
|
||||||
|
#[clap(long, requires = "decrypt")]
|
||||||
|
buffer_verify: bool,
|
||||||
|
|
||||||
|
/// Random-access decrypt: byte offset of the slice to read.
|
||||||
|
/// Requires `--decrypt`, an `--input-file` whose header has the
|
||||||
|
/// length-committed flag set, and `--length`.
|
||||||
|
#[clap(
|
||||||
|
long,
|
||||||
|
requires = "length",
|
||||||
|
requires = "decrypt",
|
||||||
|
requires = "input_file"
|
||||||
|
)]
|
||||||
|
offset: Option<u64>,
|
||||||
|
|
||||||
|
/// Random-access decrypt: byte length of the slice to read.
|
||||||
|
/// Requires `--decrypt`, `--input-file`, and `--offset`.
|
||||||
|
#[clap(
|
||||||
|
long,
|
||||||
|
requires = "offset",
|
||||||
|
requires = "decrypt",
|
||||||
|
requires = "input_file"
|
||||||
|
)]
|
||||||
|
length: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_raw_key(s: &str) -> Result<SecretBytes32, FcryError> {
|
fn read_key_file(path: &Path) -> Result<SecretBytes32, FcryError> {
|
||||||
let raw = s.as_bytes();
|
warn_if_key_file_world_readable(path);
|
||||||
if raw.len() != 32 {
|
let mut file = File::open(path)?;
|
||||||
|
let mut buf = Zeroizing::new([0u8; 33]);
|
||||||
|
let mut n = 0usize;
|
||||||
|
while n < buf.len() {
|
||||||
|
match file.read(&mut buf[n..]) {
|
||||||
|
Ok(0) => break,
|
||||||
|
Ok(read) => n += read,
|
||||||
|
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if n < 32 {
|
||||||
return Err(FcryError::Format(format!(
|
return Err(FcryError::Format(format!(
|
||||||
"raw_key must be exactly 32 bytes, got {}",
|
"key file {} is too short: expected exactly 32 bytes, got {n}",
|
||||||
raw.len()
|
path.display()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if n > 32 {
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"key file {} is too long: expected exactly 32 bytes; possible trailing newline",
|
||||||
|
path.display()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
let mut extra = Zeroizing::new([0u8; 1]);
|
||||||
|
if file.read(&mut *extra)? != 0 {
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"key file {} is too long: expected exactly 32 bytes; possible trailing newline",
|
||||||
|
path.display()
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
let mut key = SecretBytes32::zeroed();
|
let mut key = SecretBytes32::zeroed();
|
||||||
key.with_mut_array(|key| key.copy_from_slice(raw));
|
key.with_mut_array(|key| key.copy_from_slice(&buf[..32]));
|
||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn warn_if_key_file_world_readable(path: &Path) {
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
if let Ok(meta) = std::fs::metadata(path) {
|
||||||
|
let mode = meta.permissions().mode();
|
||||||
|
if (mode & 0o077) != 0 {
|
||||||
|
eprintln!(
|
||||||
|
"Warning: key file {} is group/world accessible; consider chmod 600",
|
||||||
|
path.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
fn warn_if_key_file_world_readable(_path: &Path) {}
|
||||||
|
|
||||||
/// Source of a passphrase: either the terminal or a named env var.
|
/// Source of a passphrase: either the terminal or a named env var.
|
||||||
enum PassphraseSource {
|
enum PassphraseSource {
|
||||||
Tty,
|
Tty,
|
||||||
EnvVar(String),
|
EnvVar(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn normalize_passphrase(pw: SecretVec) -> Result<SecretVec, FcryError> {
|
||||||
|
let normalized = pw.with_slice(|bytes| {
|
||||||
|
let s = std::str::from_utf8(bytes).map_err(|_| {
|
||||||
|
FcryError::Passphrase("passphrase must be valid UTF-8 after normalization".into())
|
||||||
|
})?;
|
||||||
|
Ok::<Zeroizing<String>, FcryError>(Zeroizing::new(s.nfc().collect::<String>()))
|
||||||
|
})?;
|
||||||
|
Ok(SecretVec::from_vec(normalized.as_bytes().to_vec()))
|
||||||
|
}
|
||||||
|
|
||||||
fn read_passphrase(src: &PassphraseSource, confirm: bool) -> Result<SecretVec, FcryError> {
|
fn read_passphrase(src: &PassphraseSource, confirm: bool) -> Result<SecretVec, FcryError> {
|
||||||
match src {
|
match src {
|
||||||
PassphraseSource::EnvVar(var) => {
|
PassphraseSource::EnvVar(var) => {
|
||||||
@@ -90,17 +193,22 @@ fn read_passphrase(src: &PassphraseSource, confirm: bool) -> Result<SecretVec, F
|
|||||||
// protected storage. The source Vec is zeroed after the copy.
|
// protected storage. The source Vec is zeroed after the copy.
|
||||||
// Note: a copy still exists in the process `environ` table; that is
|
// Note: a copy still exists in the process `environ` table; that is
|
||||||
// a known and accepted leak for the env-var path.
|
// a known and accepted leak for the env-var path.
|
||||||
let v = std::env::var(var).map_err(|_| {
|
let v = Zeroizing::new(std::env::var(var).map_err(|_| {
|
||||||
FcryError::Passphrase(format!("environment variable {var} not set or not unicode"))
|
FcryError::Passphrase(format!("environment variable {var} not set or not unicode"))
|
||||||
})?;
|
})?);
|
||||||
Ok(SecretVec::from_vec(v.into_bytes()))
|
let normalized = Zeroizing::new(v.as_str().nfc().collect::<String>());
|
||||||
|
Ok(SecretVec::from_vec(normalized.as_bytes().to_vec()))
|
||||||
}
|
}
|
||||||
PassphraseSource::Tty => {
|
PassphraseSource::Tty => {
|
||||||
let pw = read_passphrase_tty("Passphrase: ")
|
let pw = normalize_passphrase(
|
||||||
.map_err(|e| FcryError::Passphrase(e.to_string()))?;
|
read_passphrase_tty("Passphrase: ")
|
||||||
|
.map_err(|e| FcryError::Passphrase(e.to_string()))?,
|
||||||
|
)?;
|
||||||
if confirm {
|
if confirm {
|
||||||
let pw2 = read_passphrase_tty("Confirm passphrase: ")
|
let pw2 = normalize_passphrase(
|
||||||
.map_err(|e| FcryError::Passphrase(e.to_string()))?;
|
read_passphrase_tty("Confirm passphrase: ")
|
||||||
|
.map_err(|e| FcryError::Passphrase(e.to_string()))?,
|
||||||
|
)?;
|
||||||
if pw != pw2 {
|
if pw != pw2 {
|
||||||
return Err(FcryError::Passphrase("passphrases do not match".into()));
|
return Err(FcryError::Passphrase("passphrases do not match".into()));
|
||||||
}
|
}
|
||||||
@@ -128,7 +236,7 @@ fn disable_core_dumps() {
|
|||||||
fn run(mut cli: Cli) -> Result<(), FcryError> {
|
fn run(mut cli: Cli) -> Result<(), FcryError> {
|
||||||
// Move the secret-bearing fields out of `Cli` immediately so they don't
|
// Move the secret-bearing fields out of `Cli` immediately so they don't
|
||||||
// sit in the parsed struct for the rest of the function.
|
// sit in the parsed struct for the rest of the function.
|
||||||
let raw_key_str: Option<Zeroizing<String>> = cli.raw_key.take();
|
let key_file: Option<PathBuf> = cli.key_file.take();
|
||||||
let pw_src: Option<PassphraseSource> = if cli.passphrase {
|
let pw_src: Option<PassphraseSource> = if cli.passphrase {
|
||||||
Some(PassphraseSource::Tty)
|
Some(PassphraseSource::Tty)
|
||||||
} else {
|
} else {
|
||||||
@@ -142,31 +250,107 @@ fn run(mut cli: Cli) -> Result<(), FcryError> {
|
|||||||
let argon_memory = cli.argon_memory;
|
let argon_memory = cli.argon_memory;
|
||||||
let argon_passes = cli.argon_passes;
|
let argon_passes = cli.argon_passes;
|
||||||
let argon_parallelism = cli.argon_parallelism;
|
let argon_parallelism = cli.argon_parallelism;
|
||||||
|
let allow_weak_kdf = cli.allow_weak_kdf;
|
||||||
|
let argon_cap = policy::resolve_argon_decrypt_cap(cli.max_argon_memory_mib)?;
|
||||||
|
if argon_cap.overridden && argon_cap.effective_mib > argon_cap.default_mib {
|
||||||
|
eprintln!(
|
||||||
|
"Warning: --max-argon-memory-mib raises the Argon2 decrypt trust ceiling from {} MiB to {} MiB; this can OOM constrained machines",
|
||||||
|
argon_cap.default_mib, argon_cap.effective_mib
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let (threads, thread_warning) = policy::normalize_worker_threads(cli.threads);
|
||||||
|
if let Some(requested) = thread_warning {
|
||||||
|
eprintln!(
|
||||||
|
"Warning: requested {requested} worker threads; capped at {}",
|
||||||
|
policy::MAX_WORKER_THREADS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let force = cli.force;
|
||||||
|
let temp_dir = cli.temp_dir.take();
|
||||||
|
let buffer_verify = cli.buffer_verify;
|
||||||
|
let offset = cli.offset;
|
||||||
|
let length = cli.length;
|
||||||
drop(cli);
|
drop(cli);
|
||||||
|
|
||||||
if pw_src.is_none() && raw_key_str.is_none() {
|
if pw_src.is_none() && key_file.is_none() {
|
||||||
return Err(FcryError::Format(
|
return Err(FcryError::Format(
|
||||||
"must provide one of --raw-key, --passphrase, --passphrase-env".into(),
|
"must provide one of --key-file, --passphrase, --passphrase-env".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if buffer_verify && !decrypt_mode {
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"--buffer-verify is only valid for decrypt".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if buffer_verify && output.is_some() {
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"--buffer-verify is only meaningful when decrypting to stdout".into(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let output_options = OutSinkOptions {
|
||||||
|
force,
|
||||||
|
input_file: input.as_ref().map(PathBuf::from),
|
||||||
|
temp_dir,
|
||||||
|
buffer_verify_stdout: buffer_verify,
|
||||||
|
};
|
||||||
|
|
||||||
if decrypt_mode {
|
if decrypt_mode {
|
||||||
let raw_key = match raw_key_str.as_deref() {
|
let raw_key = match key_file.as_deref() {
|
||||||
Some(s) => Some(parse_raw_key(s)?),
|
Some(path) => Some(read_key_file(path)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
let pw = match &pw_src {
|
let pw = match &pw_src {
|
||||||
Some(src) => Some(read_passphrase(src, false)?),
|
Some(src) => Some(read_passphrase(src, false)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
decrypt(input, output, raw_key.as_ref(), pw.as_ref())?;
|
match (offset, length) {
|
||||||
|
(Some(o), Some(l)) => {
|
||||||
|
// clap's `requires` makes this unreachable, but keep the
|
||||||
|
// dynamic check so the failure mode is a clean error.
|
||||||
|
let path = input.as_deref().ok_or_else(|| {
|
||||||
|
FcryError::Format(
|
||||||
|
"--offset/--length require --input-file (random-access needs a seekable file)".into(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
decrypt_range_with_output_options(
|
||||||
|
path,
|
||||||
|
output,
|
||||||
|
raw_key.as_ref(),
|
||||||
|
pw.as_ref(),
|
||||||
|
o,
|
||||||
|
l,
|
||||||
|
argon_cap.effective_mib,
|
||||||
|
&output_options,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
(None, None) => {
|
||||||
|
decrypt_with_output_options(
|
||||||
|
input,
|
||||||
|
output,
|
||||||
|
raw_key.as_ref(),
|
||||||
|
pw.as_ref(),
|
||||||
|
threads,
|
||||||
|
argon_cap.effective_mib,
|
||||||
|
&output_options,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"--offset and --length must be supplied together".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let (key, kdf) = if let Some(src) = &pw_src {
|
let (key, kdf) = if let Some(src) = &pw_src {
|
||||||
let mut salt = [0u8; ARGON2_SALT_LEN];
|
let mut salt = [0u8; ARGON2_SALT_LEN];
|
||||||
getrandom::fill(&mut salt)?;
|
getrandom::fill(&mut salt)?;
|
||||||
let m_cost_kib = argon_memory.checked_mul(1024).ok_or_else(|| {
|
let m_cost_kib = policy::validate_new_argon_params(
|
||||||
FcryError::Format("argon-memory too large (overflow when converting to KiB)".into())
|
argon_memory,
|
||||||
})?;
|
argon_passes,
|
||||||
|
argon_parallelism,
|
||||||
|
allow_weak_kdf,
|
||||||
|
)?;
|
||||||
let kdf = KdfParams::Argon2id {
|
let kdf = KdfParams::Argon2id {
|
||||||
salt,
|
salt,
|
||||||
m_cost: m_cost_kib,
|
m_cost: m_cost_kib,
|
||||||
@@ -174,13 +358,22 @@ fn run(mut cli: Cli) -> Result<(), FcryError> {
|
|||||||
p_cost: argon_parallelism,
|
p_cost: argon_parallelism,
|
||||||
};
|
};
|
||||||
let pw = read_passphrase(src, true)?;
|
let pw = read_passphrase(src, true)?;
|
||||||
|
policy::validate_new_passphrase(&pw, allow_weak_kdf)?;
|
||||||
let key = derive_key(&kdf, None, Some(&pw))?;
|
let key = derive_key(&kdf, None, Some(&pw))?;
|
||||||
(key, kdf)
|
(key, kdf)
|
||||||
} else {
|
} else {
|
||||||
let key = parse_raw_key(raw_key_str.as_deref().unwrap())?;
|
let key = read_key_file(key_file.as_deref().unwrap())?;
|
||||||
(key, KdfParams::Raw)
|
(key, KdfParams::Raw)
|
||||||
};
|
};
|
||||||
encrypt(input, output, &key, chunk_size, kdf)?;
|
encrypt_with_output_options(
|
||||||
|
input,
|
||||||
|
output,
|
||||||
|
&key,
|
||||||
|
chunk_size,
|
||||||
|
kdf,
|
||||||
|
threads,
|
||||||
|
&output_options,
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+375
@@ -0,0 +1,375 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
//! Multi-threaded encrypt/decrypt pipeline.
|
||||||
|
//!
|
||||||
|
//! Topology:
|
||||||
|
//!
|
||||||
|
//! ```text
|
||||||
|
//! reader thread → jobs (bounded MPMC) → N AEAD workers →
|
||||||
|
//! → results (bounded MPMC) → writer thread
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! The reader is sequential (one input handle, lookahead detects last chunk),
|
||||||
|
//! workers parallelize the AEAD step (independent per chunk), and the writer
|
||||||
|
//! reorders results by counter before writing them to `OutSink`.
|
||||||
|
//!
|
||||||
|
//! Bounded memory: a permit channel caps the total number of in-flight chunks
|
||||||
|
//! (queued jobs + in-progress at workers + pending in the writer's reorder
|
||||||
|
//! buffer). The reader acquires a permit before sending each job; the writer
|
||||||
|
//! releases a permit after flushing the chunk in order. A slow or stuck worker
|
||||||
|
//! therefore stalls the reader rather than letting the writer's reorder buffer
|
||||||
|
//! grow without bound.
|
||||||
|
//!
|
||||||
|
//! Fail-fast: a shared `cancel` flag lets workers signal an authentication or
|
||||||
|
//! AEAD error to the reader. The reader checks it each iteration and exits
|
||||||
|
//! early, so a tampered chunk doesn't waste full-file I/O on top of the
|
||||||
|
//! detection.
|
||||||
|
//!
|
||||||
|
//! Peak memory ≈ chunk_size × (in_flight_cap + 2). For 1 MiB chunks and 8
|
||||||
|
//! cores (cap = 32) that's ~34 MiB. Adjust `in_flight_capacity` if you need
|
||||||
|
//! a different memory/throughput tradeoff.
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::thread::{self, JoinHandle};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use chacha20poly1305::{XChaCha20Poly1305, aead::AeadInPlace};
|
||||||
|
use crossbeam_channel::{Receiver, RecvTimeoutError, Sender, bounded};
|
||||||
|
|
||||||
|
use crate::crypto::{bump_counter, make_nonce};
|
||||||
|
use crate::error::FcryError;
|
||||||
|
use crate::header::NONCE_PREFIX_LEN;
|
||||||
|
use crate::policy;
|
||||||
|
use crate::reader::{AheadReader, ReadInfoChunk};
|
||||||
|
use crate::utils::OutSink;
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
|
struct Job {
|
||||||
|
counter: u32,
|
||||||
|
last: bool,
|
||||||
|
buf: Zeroizing<Vec<u8>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Done {
|
||||||
|
counter: u32,
|
||||||
|
buf: Zeroizing<Vec<u8>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Job-channel capacity: small multiples of worker count, enough to keep
|
||||||
|
/// workers fed without unbounded memory.
|
||||||
|
fn channel_capacity(threads: usize, in_flight: usize) -> usize {
|
||||||
|
policy::pipeline_channel_capacity(threads, in_flight)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Total in-flight chunk cap (jobs queued + at workers + in writer's reorder
|
||||||
|
/// buffer). Permit count; bounded above the job-channel capacity to absorb
|
||||||
|
/// reordering without blocking workers unnecessarily.
|
||||||
|
fn in_flight_capacity(threads: usize, chunk_len: usize) -> usize {
|
||||||
|
policy::pipeline_in_flight_capacity(threads, chunk_len)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub(crate) fn encrypt_parallel(
|
||||||
|
input: AheadReader,
|
||||||
|
output: OutSink,
|
||||||
|
aead: Arc<XChaCha20Poly1305>,
|
||||||
|
aad: Arc<Vec<u8>>,
|
||||||
|
nonce_prefix: [u8; NONCE_PREFIX_LEN],
|
||||||
|
chunk_sz: usize,
|
||||||
|
threads: usize,
|
||||||
|
expected_length: Option<u64>,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
let (sink, bytes_seen) = run_pipeline(
|
||||||
|
input,
|
||||||
|
output,
|
||||||
|
aead,
|
||||||
|
aad,
|
||||||
|
nonce_prefix,
|
||||||
|
chunk_sz,
|
||||||
|
threads,
|
||||||
|
true,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if let Some(committed) = expected_length
|
||||||
|
&& committed != bytes_seen
|
||||||
|
{
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"input length changed during encryption: committed {committed}, read {bytes_seen}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
sink.commit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub(crate) fn decrypt_parallel(
|
||||||
|
input: AheadReader,
|
||||||
|
output: OutSink,
|
||||||
|
aead: Arc<XChaCha20Poly1305>,
|
||||||
|
aad: Arc<Vec<u8>>,
|
||||||
|
nonce_prefix: [u8; NONCE_PREFIX_LEN],
|
||||||
|
cipher_chunk: usize,
|
||||||
|
threads: usize,
|
||||||
|
expected_length: Option<u64>,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
let (sink, written) = run_pipeline(
|
||||||
|
input,
|
||||||
|
output,
|
||||||
|
aead,
|
||||||
|
aad,
|
||||||
|
nonce_prefix,
|
||||||
|
cipher_chunk,
|
||||||
|
threads,
|
||||||
|
false,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if let Some(committed) = expected_length
|
||||||
|
&& committed != written
|
||||||
|
{
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"decrypted length {written} disagrees with committed {committed}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
sink.commit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drives the reader/worker/writer pipeline. `is_encrypt = true` performs
|
||||||
|
/// `encrypt_in_place` and tracks bytes-read; `false` performs
|
||||||
|
/// `decrypt_in_place` and tracks bytes-written. The single shared topology
|
||||||
|
/// keeps backpressure, reorder, and fail-fast logic in one place.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn run_pipeline(
|
||||||
|
mut input: AheadReader,
|
||||||
|
output: OutSink,
|
||||||
|
aead: Arc<XChaCha20Poly1305>,
|
||||||
|
aad: Arc<Vec<u8>>,
|
||||||
|
nonce_prefix: [u8; NONCE_PREFIX_LEN],
|
||||||
|
chunk_sz: usize,
|
||||||
|
threads: usize,
|
||||||
|
is_encrypt: bool,
|
||||||
|
) -> Result<(OutSink, u64), FcryError> {
|
||||||
|
let in_flight = in_flight_capacity(threads, chunk_sz);
|
||||||
|
let cap = channel_capacity(threads, in_flight);
|
||||||
|
let (jobs_tx, jobs_rx) = bounded::<Job>(cap);
|
||||||
|
let (done_tx, done_rx) = bounded::<Done>(cap);
|
||||||
|
|
||||||
|
// Pre-fill the permit channel. Each permit represents one in-flight chunk
|
||||||
|
// slot. The reader consumes a permit before sending a job; the writer
|
||||||
|
// returns a permit after flushing in order.
|
||||||
|
let (permit_tx, permit_rx) = bounded::<()>(in_flight);
|
||||||
|
for _ in 0..in_flight {
|
||||||
|
permit_tx
|
||||||
|
.send(())
|
||||||
|
.expect("pre-fill of permit channel cannot fail");
|
||||||
|
}
|
||||||
|
|
||||||
|
let cancel = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
|
// Reader thread: dispatches jobs in counter order and tracks bytes read
|
||||||
|
// (used for the encrypt-side length cross-check). On decrypt the count is
|
||||||
|
// ignored — the writer's count is authoritative there.
|
||||||
|
let reader_handle: JoinHandle<Result<u64, FcryError>> = {
|
||||||
|
let cancel = cancel.clone();
|
||||||
|
thread::spawn(move || {
|
||||||
|
let mut counter: u32 = 0;
|
||||||
|
let mut bytes_seen: u64 = 0;
|
||||||
|
loop {
|
||||||
|
// Acquire an in-flight slot. We recv with a short timeout so
|
||||||
|
// a worker error (which sets `cancel`) is observed even if
|
||||||
|
// the rest of the pipeline has quiesced and is no longer
|
||||||
|
// releasing permits — this avoids a 3-way deadlock between
|
||||||
|
// reader, idle workers, and a stalled writer.
|
||||||
|
loop {
|
||||||
|
if cancel.load(Ordering::Acquire) {
|
||||||
|
return Ok(bytes_seen);
|
||||||
|
}
|
||||||
|
match permit_rx.recv_timeout(Duration::from_millis(50)) {
|
||||||
|
Ok(()) => break,
|
||||||
|
Err(RecvTimeoutError::Timeout) => continue,
|
||||||
|
Err(RecvTimeoutError::Disconnected) => return Ok(bytes_seen),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut buf = Zeroizing::new(vec![0u8; chunk_sz]);
|
||||||
|
match input.read_ahead(&mut buf)? {
|
||||||
|
ReadInfoChunk::Normal(_) => {
|
||||||
|
if jobs_tx
|
||||||
|
.send(Job {
|
||||||
|
counter,
|
||||||
|
last: false,
|
||||||
|
buf,
|
||||||
|
})
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
return Ok(bytes_seen);
|
||||||
|
}
|
||||||
|
bytes_seen = policy::checked_count_add(bytes_seen, chunk_sz, "bytes read")?;
|
||||||
|
counter = bump_counter(counter)?;
|
||||||
|
}
|
||||||
|
ReadInfoChunk::Last(n) => {
|
||||||
|
buf.truncate(n);
|
||||||
|
let _ = jobs_tx.send(Job {
|
||||||
|
counter,
|
||||||
|
last: true,
|
||||||
|
buf,
|
||||||
|
});
|
||||||
|
bytes_seen = policy::checked_count_add(bytes_seen, n, "bytes read")?;
|
||||||
|
return Ok(bytes_seen);
|
||||||
|
}
|
||||||
|
ReadInfoChunk::Empty => {
|
||||||
|
if is_encrypt {
|
||||||
|
buf.clear();
|
||||||
|
let _ = jobs_tx.send(Job {
|
||||||
|
counter,
|
||||||
|
last: true,
|
||||||
|
buf,
|
||||||
|
});
|
||||||
|
return Ok(bytes_seen);
|
||||||
|
}
|
||||||
|
// On decrypt an unexpected EOF means the ciphertext is
|
||||||
|
// truncated. Surface it as an error so the writer
|
||||||
|
// doesn't commit a partial output.
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"truncated ciphertext: missing final chunk".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
// Worker threads: AEAD encrypt/decrypt in place, ship to writer. On error
|
||||||
|
// we set the cancel flag so the reader exits early, and drop the senders
|
||||||
|
// so the writer drains and exits.
|
||||||
|
let mut worker_handles: Vec<JoinHandle<Result<(), FcryError>>> = Vec::with_capacity(threads);
|
||||||
|
for _ in 0..threads {
|
||||||
|
let jobs_rx = jobs_rx.clone();
|
||||||
|
let done_tx = done_tx.clone();
|
||||||
|
let aead = aead.clone();
|
||||||
|
let aad = aad.clone();
|
||||||
|
let cancel = cancel.clone();
|
||||||
|
worker_handles.push(thread::spawn(move || {
|
||||||
|
for mut job in jobs_rx.iter() {
|
||||||
|
if cancel.load(Ordering::Acquire) {
|
||||||
|
// Drain remaining queued jobs without doing AEAD work.
|
||||||
|
// Returning Ok here keeps the previously-set error from
|
||||||
|
// being clobbered by a fresh "ok" status.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let nonce = make_nonce(&nonce_prefix, job.counter, job.last);
|
||||||
|
let res = if is_encrypt {
|
||||||
|
aead.encrypt_in_place(&nonce, aad.as_slice(), &mut *job.buf)
|
||||||
|
} else {
|
||||||
|
aead.decrypt_in_place(&nonce, aad.as_slice(), &mut *job.buf)
|
||||||
|
};
|
||||||
|
if let Err(e) = res {
|
||||||
|
cancel.store(true, Ordering::Release);
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
if done_tx
|
||||||
|
.send(Done {
|
||||||
|
counter: job.counter,
|
||||||
|
buf: job.buf,
|
||||||
|
})
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
drop(jobs_rx);
|
||||||
|
drop(done_tx);
|
||||||
|
|
||||||
|
// Writer thread: ordered writeback. Returns the `OutSink` ownership back
|
||||||
|
// without committing; the caller commits only after every other thread
|
||||||
|
// has joined cleanly so a failure anywhere drops the sink and unlinks the
|
||||||
|
// temp file. Releases one permit per chunk flushed so the reader can make
|
||||||
|
// forward progress in lockstep with the actual disk write.
|
||||||
|
let writer_handle: JoinHandle<Result<(OutSink, u64), FcryError>> =
|
||||||
|
thread::spawn(move || ordered_writer(done_rx, output, permit_tx));
|
||||||
|
|
||||||
|
let reader_res = reader_handle.join().expect("reader thread panicked");
|
||||||
|
let mut first_err: Option<FcryError> = None;
|
||||||
|
let bytes_seen = match reader_res {
|
||||||
|
Ok(n) => Some(n),
|
||||||
|
Err(e) => {
|
||||||
|
cancel.store(true, Ordering::Release);
|
||||||
|
first_err.get_or_insert(e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for h in worker_handles {
|
||||||
|
if let Err(e) = h.join().expect("worker thread panicked")
|
||||||
|
&& first_err.is_none()
|
||||||
|
{
|
||||||
|
first_err = Some(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let writer_res = writer_handle.join().expect("writer thread panicked");
|
||||||
|
let written = match writer_res {
|
||||||
|
Ok((sink, n)) => Some((sink, n)),
|
||||||
|
Err(e) => {
|
||||||
|
if first_err.is_none() {
|
||||||
|
first_err = Some(e);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(e) = first_err {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (sink, n) = written.expect("no error but no sink");
|
||||||
|
let count = if is_encrypt {
|
||||||
|
bytes_seen.expect("no error but no reader count")
|
||||||
|
} else {
|
||||||
|
n
|
||||||
|
};
|
||||||
|
Ok((sink, count))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drain `done_rx` in counter order, writing each chunk to `output` and
|
||||||
|
/// returning a permit to `permit_tx` after every flush so the reader is held
|
||||||
|
/// in lockstep with disk writes (bounded reorder buffer).
|
||||||
|
fn ordered_writer(
|
||||||
|
done_rx: Receiver<Done>,
|
||||||
|
mut output: OutSink,
|
||||||
|
permit_tx: Sender<()>,
|
||||||
|
) -> Result<(OutSink, u64), FcryError> {
|
||||||
|
let mut next: u32 = 0;
|
||||||
|
let mut pending: BTreeMap<u32, Zeroizing<Vec<u8>>> = BTreeMap::new();
|
||||||
|
let mut total: u64 = 0;
|
||||||
|
for done in done_rx.iter() {
|
||||||
|
pending.insert(done.counter, done.buf);
|
||||||
|
while let Some(buf) = pending.remove(&next) {
|
||||||
|
output.write_all(&buf)?;
|
||||||
|
total = policy::checked_count_add(total, buf.len(), "bytes written")?;
|
||||||
|
// `bump_counter` rejects overflow upstream; a wrap here would be
|
||||||
|
// a real bug, so use plain addition and let it panic in debug.
|
||||||
|
next += 1;
|
||||||
|
// Release one in-flight slot. If the reader is gone the channel
|
||||||
|
// is closed; we don't care about the send result.
|
||||||
|
let _ = permit_tx.send(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !pending.is_empty() {
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"internal: ordered writer left chunks unflushed".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok((output, total))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile-time check that the job type is Send+Sync (channel sends across
|
||||||
|
// threads). Kept as a footgun for future struct edits.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn _assert_send_sync<T: Send + Sync>() {}
|
||||||
|
const _: fn() = || _assert_send_sync::<Sender<Job>>();
|
||||||
+299
@@ -0,0 +1,299 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
//! Central resource and format policy.
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use crate::error::FcryError;
|
||||||
|
use crate::header::{KdfParams, TAG_LEN};
|
||||||
|
use crate::secrets::SecretVec;
|
||||||
|
|
||||||
|
pub const MAX_CHUNK_SIZE: u32 = 64 * 1024 * 1024;
|
||||||
|
pub const DEFAULT_CHUNK_SIZE: u32 = 1024 * 1024;
|
||||||
|
|
||||||
|
pub const DEFAULT_ARGON_MEMORY_MIB: u32 = 1024;
|
||||||
|
pub const MIN_ARGON_MEMORY_MIB: u32 = 64;
|
||||||
|
pub const DEFAULT_ARGON_DECRYPT_CAP_MIB: u32 = 4096;
|
||||||
|
pub const MIN_ARGON_PASSES: u32 = 2;
|
||||||
|
pub const MAX_ARGON_PASSES: u32 = 64;
|
||||||
|
pub const DEFAULT_ARGON_PARALLELISM: u32 = 4;
|
||||||
|
pub const MAX_ARGON_PARALLELISM: u32 = 64;
|
||||||
|
pub const MIN_PASSPHRASE_BYTES: usize = 12;
|
||||||
|
|
||||||
|
pub const MAX_WORKER_THREADS: usize = 256;
|
||||||
|
pub const PIPELINE_IN_FLIGHT_BYTES: usize = 128 * 1024 * 1024;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct ArgonDecryptCap {
|
||||||
|
pub default_mib: u32,
|
||||||
|
pub effective_mib: u32,
|
||||||
|
pub overridden: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn architecture_argon_cap_mib() -> u32 {
|
||||||
|
let usize_cap_mib = usize::MAX / 1024 / 1024;
|
||||||
|
let argon_m_cost_cap_mib = (u32::MAX / 1024) as usize;
|
||||||
|
usize_cap_mib
|
||||||
|
.min(argon_m_cost_cap_mib)
|
||||||
|
.min(u32::MAX as usize) as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
fn available_memory_mib() -> Option<u32> {
|
||||||
|
let meminfo = fs::read_to_string("/proc/meminfo").ok()?;
|
||||||
|
for line in meminfo.lines() {
|
||||||
|
let Some(rest) = line.strip_prefix("MemAvailable:") else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let kib = rest.split_whitespace().next()?.parse::<u64>().ok()?;
|
||||||
|
return u32::try_from(kib / 1024).ok();
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
fn available_memory_mib() -> Option<u32> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_argon_decrypt_cap_mib() -> u32 {
|
||||||
|
let mut cap = DEFAULT_ARGON_DECRYPT_CAP_MIB.min(architecture_argon_cap_mib());
|
||||||
|
if let Some(available) = available_memory_mib() {
|
||||||
|
cap = cap.min(available);
|
||||||
|
}
|
||||||
|
cap.max(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolve_argon_decrypt_cap(override_mib: Option<u32>) -> Result<ArgonDecryptCap, FcryError> {
|
||||||
|
let default_mib = default_argon_decrypt_cap_mib();
|
||||||
|
let Some(effective_mib) = override_mib else {
|
||||||
|
return Ok(ArgonDecryptCap {
|
||||||
|
default_mib,
|
||||||
|
effective_mib: default_mib,
|
||||||
|
overridden: false,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
if effective_mib == 0 {
|
||||||
|
return Err(FcryError::Format(
|
||||||
|
"--max-argon-memory-mib must be at least 1".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let arch = architecture_argon_cap_mib();
|
||||||
|
if effective_mib > arch {
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"--max-argon-memory-mib {effective_mib} exceeds this build's supported cap {arch}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(ArgonDecryptCap {
|
||||||
|
default_mib,
|
||||||
|
effective_mib,
|
||||||
|
overridden: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mib_to_kib(mib: u32, name: &str) -> Result<u32, FcryError> {
|
||||||
|
mib.checked_mul(1024).ok_or_else(|| {
|
||||||
|
FcryError::Format(format!("{name} too large (overflow converting MiB to KiB)"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_chunk_size(chunk_size: u32) -> Result<usize, FcryError> {
|
||||||
|
if chunk_size == 0 {
|
||||||
|
return Err(FcryError::Format("chunk_size must be > 0".into()));
|
||||||
|
}
|
||||||
|
if chunk_size > MAX_CHUNK_SIZE {
|
||||||
|
return Err(FcryError::Format(format!(
|
||||||
|
"chunk_size {chunk_size} exceeds maximum {MAX_CHUNK_SIZE}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
usize::try_from(chunk_size)
|
||||||
|
.map_err(|_| FcryError::Format("chunk_size does not fit in usize".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cipher_chunk_len(plain_chunk_len: usize) -> Result<usize, FcryError> {
|
||||||
|
plain_chunk_len
|
||||||
|
.checked_add(TAG_LEN)
|
||||||
|
.ok_or_else(|| FcryError::Format("cipher chunk length overflow".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_new_argon_params(
|
||||||
|
memory_mib: u32,
|
||||||
|
passes: u32,
|
||||||
|
parallelism: u32,
|
||||||
|
allow_weak_kdf: bool,
|
||||||
|
) -> Result<u32, FcryError> {
|
||||||
|
if !allow_weak_kdf && memory_mib < MIN_ARGON_MEMORY_MIB {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-memory must be at least {MIN_ARGON_MEMORY_MIB} MiB for new encryption (use --allow-weak-kdf only for tests/legacy interop)"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if !allow_weak_kdf && passes < MIN_ARGON_PASSES {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-passes must be at least {MIN_ARGON_PASSES} for new encryption (use --allow-weak-kdf only for tests/legacy interop)"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
validate_argon_common(memory_mib, passes, parallelism, "new encryption")?;
|
||||||
|
mib_to_kib(memory_mib, "argon-memory")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_new_passphrase(pw: &SecretVec, allow_weak_kdf: bool) -> Result<(), FcryError> {
|
||||||
|
let len = pw.len();
|
||||||
|
if len == 0 {
|
||||||
|
return Err(FcryError::Passphrase("passphrase must not be empty".into()));
|
||||||
|
}
|
||||||
|
if !allow_weak_kdf && len < MIN_PASSPHRASE_BYTES {
|
||||||
|
return Err(FcryError::Passphrase(format!(
|
||||||
|
"passphrase must be at least {MIN_PASSPHRASE_BYTES} UTF-8 bytes for new encryption (use --allow-weak-kdf only for tests/legacy interop)"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_header_kdf(kdf: &KdfParams, max_argon_memory_mib: u32) -> Result<(), FcryError> {
|
||||||
|
match kdf {
|
||||||
|
KdfParams::Raw => Ok(()),
|
||||||
|
KdfParams::Argon2id {
|
||||||
|
m_cost,
|
||||||
|
t_cost,
|
||||||
|
p_cost,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let cap_kib = mib_to_kib(max_argon_memory_mib, "max argon memory")?;
|
||||||
|
if *m_cost == 0 {
|
||||||
|
return Err(FcryError::Format("argon2id memory cost must be > 0".into()));
|
||||||
|
}
|
||||||
|
if *t_cost == 0 {
|
||||||
|
return Err(FcryError::Format("argon2id passes must be > 0".into()));
|
||||||
|
}
|
||||||
|
if *p_cost == 0 {
|
||||||
|
return Err(FcryError::Format("argon2id parallelism must be > 0".into()));
|
||||||
|
}
|
||||||
|
if *m_cost > cap_kib {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon2id memory cost {} KiB exceeds configured decrypt cap {} MiB",
|
||||||
|
*m_cost, max_argon_memory_mib
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if *t_cost > MAX_ARGON_PASSES {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon2id passes {} exceeds maximum {}",
|
||||||
|
*t_cost, MAX_ARGON_PASSES
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if *p_cost > MAX_ARGON_PARALLELISM {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon2id parallelism {} exceeds maximum {}",
|
||||||
|
*p_cost, MAX_ARGON_PARALLELISM
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_argon_common(
|
||||||
|
memory_mib: u32,
|
||||||
|
passes: u32,
|
||||||
|
parallelism: u32,
|
||||||
|
context: &str,
|
||||||
|
) -> Result<(), FcryError> {
|
||||||
|
if memory_mib == 0 {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-memory must be > 0 for {context}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if passes == 0 {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-passes must be > 0 for {context}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if passes > MAX_ARGON_PASSES {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-passes {passes} exceeds maximum {MAX_ARGON_PASSES}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if parallelism == 0 {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-parallelism must be > 0 for {context}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if parallelism > MAX_ARGON_PARALLELISM {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-parallelism {parallelism} exceeds maximum {MAX_ARGON_PARALLELISM}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if memory_mib > architecture_argon_cap_mib() {
|
||||||
|
return Err(FcryError::Kdf(format!(
|
||||||
|
"argon-memory {memory_mib} exceeds this build's supported cap {}",
|
||||||
|
architecture_argon_cap_mib()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn normalize_worker_threads(requested: Option<u32>) -> (usize, Option<u32>) {
|
||||||
|
let requested = requested.map(|n| n as usize).unwrap_or_else(|| {
|
||||||
|
std::thread::available_parallelism()
|
||||||
|
.map(|n| n.get())
|
||||||
|
.unwrap_or(1)
|
||||||
|
});
|
||||||
|
let capped = requested.clamp(1, MAX_WORKER_THREADS);
|
||||||
|
let warning = (requested > MAX_WORKER_THREADS).then_some(requested as u32);
|
||||||
|
(capped, warning)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pipeline_in_flight_capacity(threads: usize, chunk_len: usize) -> usize {
|
||||||
|
let chunk_len = chunk_len.max(1);
|
||||||
|
let thread_cap = threads.saturating_mul(4).max(1);
|
||||||
|
let byte_cap = (PIPELINE_IN_FLIGHT_BYTES / chunk_len).max(1);
|
||||||
|
thread_cap.min(byte_cap)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pipeline_channel_capacity(threads: usize, in_flight: usize) -> usize {
|
||||||
|
threads.saturating_mul(2).max(1).min(in_flight.max(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn checked_add_u64(a: u64, b: u64, what: &str) -> Result<u64, FcryError> {
|
||||||
|
a.checked_add(b)
|
||||||
|
.ok_or_else(|| FcryError::Format(format!("{what} overflow")))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn checked_mul_u64(a: u64, b: u64, what: &str) -> Result<u64, FcryError> {
|
||||||
|
a.checked_mul(b)
|
||||||
|
.ok_or_else(|| FcryError::Format(format!("{what} overflow")))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn checked_count_add(total: u64, delta: usize, what: &str) -> Result<u64, FcryError> {
|
||||||
|
checked_add_u64(total, delta as u64, what)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn chunk_size_bounds() {
|
||||||
|
assert!(validate_chunk_size(1).is_ok());
|
||||||
|
assert!(validate_chunk_size(MAX_CHUNK_SIZE).is_ok());
|
||||||
|
assert!(validate_chunk_size(0).is_err());
|
||||||
|
assert!(validate_chunk_size(MAX_CHUNK_SIZE + 1).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn argon_cap_override_replaces_default() {
|
||||||
|
let down = resolve_argon_decrypt_cap(Some(1)).unwrap();
|
||||||
|
assert_eq!(down.effective_mib, 1);
|
||||||
|
assert!(down.overridden);
|
||||||
|
let default = resolve_argon_decrypt_cap(None).unwrap();
|
||||||
|
assert_eq!(default.effective_mib, default.default_mib);
|
||||||
|
assert!(!default.overridden);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pipeline_capacity_has_one_chunk_minimum() {
|
||||||
|
assert_eq!(
|
||||||
|
pipeline_in_flight_capacity(4, PIPELINE_IN_FLIGHT_BYTES * 2),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
-6
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{BufRead, Read};
|
use std::io::{BufRead, Read};
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
pub enum ReadInfoChunk {
|
pub enum ReadInfoChunk {
|
||||||
Normal(#[allow(dead_code)] usize),
|
Normal(#[allow(dead_code)] usize),
|
||||||
@@ -10,17 +11,17 @@ pub enum ReadInfoChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct AheadReader {
|
pub struct AheadReader {
|
||||||
inner: Box<dyn BufRead>,
|
inner: Box<dyn BufRead + Send>,
|
||||||
buf: Vec<u8>,
|
buf: Zeroizing<Vec<u8>>,
|
||||||
bufsz: usize,
|
bufsz: usize,
|
||||||
capacity: usize,
|
capacity: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AheadReader {
|
impl AheadReader {
|
||||||
pub fn from(reader: Box<dyn BufRead>, capacity: usize) -> Self {
|
pub fn from(reader: Box<dyn BufRead + Send>, capacity: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: reader,
|
inner: reader,
|
||||||
buf: vec![0; capacity],
|
buf: Zeroizing::new(vec![0; capacity]),
|
||||||
bufsz: 0,
|
bufsz: 0,
|
||||||
capacity,
|
capacity,
|
||||||
}
|
}
|
||||||
@@ -61,7 +62,7 @@ impl AheadReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2nd read directly into our internal buf
|
// 2nd read directly into our internal buf
|
||||||
let mut tmp = vec![0u8; self.capacity];
|
let mut tmp = Zeroizing::new(vec![0u8; self.capacity]);
|
||||||
let n2 = self.read_until_full(&mut tmp)?;
|
let n2 = self.read_until_full(&mut tmp)?;
|
||||||
self.buf = tmp;
|
self.buf = tmp;
|
||||||
self.bufsz = n2;
|
self.bufsz = n2;
|
||||||
@@ -78,7 +79,7 @@ impl AheadReader {
|
|||||||
let userbuf_sz = self.bufsz;
|
let userbuf_sz = self.bufsz;
|
||||||
|
|
||||||
// 2nd read directly into our internal buf
|
// 2nd read directly into our internal buf
|
||||||
let mut tmp = vec![0u8; self.capacity];
|
let mut tmp = Zeroizing::new(vec![0u8; self.capacity]);
|
||||||
let n2 = self.read_until_full(&mut tmp)?;
|
let n2 = self.read_until_full(&mut tmp)?;
|
||||||
self.buf = tmp;
|
self.buf = tmp;
|
||||||
self.bufsz = n2;
|
self.bufsz = n2;
|
||||||
|
|||||||
+56
-14
@@ -95,6 +95,10 @@ impl SecretVec {
|
|||||||
let inner = self.inner.borrow();
|
let inner = self.inner.borrow();
|
||||||
f(&inner[..self.len])
|
f(&inner[..self.len])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.len
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for SecretVec {
|
impl PartialEq for SecretVec {
|
||||||
@@ -192,13 +196,15 @@ mod imp {
|
|||||||
mod imp {
|
mod imp {
|
||||||
use super::{MAX_PASSPHRASE_LEN, SecretVec};
|
use super::{MAX_PASSPHRASE_LEN, SecretVec};
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Write};
|
||||||
use std::os::windows::io::AsRawHandle;
|
use std::os::windows::io::AsRawHandle;
|
||||||
|
use std::ptr;
|
||||||
use windows_sys::Win32::Foundation::HANDLE;
|
use windows_sys::Win32::Foundation::HANDLE;
|
||||||
use windows_sys::Win32::System::Console::{
|
use windows_sys::Win32::System::Console::{
|
||||||
ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, GetConsoleMode,
|
ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, GetConsoleMode, ReadConsoleW,
|
||||||
SetConsoleMode,
|
SetConsoleMode,
|
||||||
};
|
};
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
struct ConsoleModeGuard {
|
struct ConsoleModeGuard {
|
||||||
handle: HANDLE,
|
handle: HANDLE,
|
||||||
@@ -214,7 +220,7 @@ mod imp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_passphrase_tty(prompt: &str) -> io::Result<SecretVec> {
|
pub fn read_passphrase_tty(prompt: &str) -> io::Result<SecretVec> {
|
||||||
let mut tty_in = OpenOptions::new().read(true).write(true).open("CONIN$")?;
|
let tty_in = OpenOptions::new().read(true).write(true).open("CONIN$")?;
|
||||||
let mut tty_out = OpenOptions::new().write(true).open("CONOUT$")?;
|
let mut tty_out = OpenOptions::new().write(true).open("CONOUT$")?;
|
||||||
|
|
||||||
let h_in = tty_in.as_raw_handle() as HANDLE;
|
let h_in = tty_in.as_raw_handle() as HANDLE;
|
||||||
@@ -236,18 +242,38 @@ mod imp {
|
|||||||
write!(tty_out, "{prompt}")?;
|
write!(tty_out, "{prompt}")?;
|
||||||
tty_out.flush()?;
|
tty_out.flush()?;
|
||||||
|
|
||||||
let mut buf = SecretVec::with_capacity(MAX_PASSPHRASE_LEN);
|
let mut wide = Zeroizing::new(Vec::<u16>::with_capacity(MAX_PASSPHRASE_LEN));
|
||||||
let mut byte = [0u8; 1];
|
|
||||||
loop {
|
loop {
|
||||||
match tty_in.read(&mut byte) {
|
let mut unit = 0u16;
|
||||||
Ok(0) => break,
|
let mut read = 0u32;
|
||||||
Ok(_) => match byte[0] {
|
let ok = unsafe {
|
||||||
b'\n' => break,
|
ReadConsoleW(
|
||||||
b'\r' => continue,
|
h_in,
|
||||||
b => buf.push(b)?,
|
(&mut unit as *mut u16).cast(),
|
||||||
},
|
1,
|
||||||
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
|
&mut read,
|
||||||
Err(e) => return Err(e),
|
ptr::null_mut(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if ok == 0 {
|
||||||
|
return Err(io::Error::last_os_error());
|
||||||
|
}
|
||||||
|
if read == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const LF: u16 = b'\n' as u16;
|
||||||
|
const CR: u16 = b'\r' as u16;
|
||||||
|
match unit {
|
||||||
|
LF | CR => break,
|
||||||
|
u => {
|
||||||
|
if wide.len() >= MAX_PASSPHRASE_LEN {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"secret buffer full",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
wide.push(u);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,6 +281,22 @@ mod imp {
|
|||||||
let _ = writeln!(tty_out);
|
let _ = writeln!(tty_out);
|
||||||
let _ = tty_out.flush();
|
let _ = tty_out.flush();
|
||||||
|
|
||||||
|
let utf8 = Zeroizing::new(String::from_utf16(&wide).map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
"console passphrase is not valid UTF-16",
|
||||||
|
)
|
||||||
|
})?);
|
||||||
|
if utf8.len() > MAX_PASSPHRASE_LEN {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"secret buffer full",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let mut buf = SecretVec::with_capacity(MAX_PASSPHRASE_LEN);
|
||||||
|
for b in utf8.as_bytes() {
|
||||||
|
buf.push(*b)?;
|
||||||
|
}
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+240
-65
@@ -1,79 +1,271 @@
|
|||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File, OpenOptions};
|
||||||
use std::io::{self, BufRead, BufReader, Write};
|
use std::io::{self, BufRead, BufReader, Seek, SeekFrom, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use crate::policy;
|
||||||
|
|
||||||
/// Default plaintext chunk size: 1 MiB.
|
/// Default plaintext chunk size: 1 MiB.
|
||||||
///
|
///
|
||||||
/// Stored in the header per file, so callers may override via CLI without
|
/// Stored in the header per file, so callers may override via CLI without
|
||||||
/// breaking older files (the decryptor reads the size from the header).
|
/// breaking older files (the decryptor reads the size from the header).
|
||||||
pub const DEFAULT_CHUNK_SIZE: u32 = 1024 * 1024;
|
pub const DEFAULT_CHUNK_SIZE: u32 = policy::DEFAULT_CHUNK_SIZE;
|
||||||
|
|
||||||
pub(crate) fn open_input<S: AsRef<str>>(input_file: Option<S>) -> io::Result<Box<dyn BufRead>> {
|
/// Opened input.
|
||||||
Ok(match input_file {
|
///
|
||||||
Some(f) => Box::new(BufReader::new(File::open(f.as_ref())?)),
|
/// `length` is `Some(n)` only when the source is a regular file (we stat the
|
||||||
None => Box::new(io::stdin().lock()),
|
/// open FD to avoid TOCTOU). For stdin, FIFOs, sockets, char devices, etc.
|
||||||
})
|
/// it is `None` — those paths cannot commit a length in the header.
|
||||||
|
pub(crate) struct Input {
|
||||||
|
pub reader: Box<dyn BufRead + Send>,
|
||||||
|
pub length: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn open_input<S: AsRef<str>>(input_file: Option<S>) -> io::Result<Input> {
|
||||||
|
match input_file {
|
||||||
|
Some(f) => {
|
||||||
|
let file = File::open(f.as_ref())?;
|
||||||
|
// Stat the open FD (not the path) so we can't be raced between
|
||||||
|
// stat and open.
|
||||||
|
let length = file
|
||||||
|
.metadata()
|
||||||
|
.ok()
|
||||||
|
.filter(|m| m.is_file())
|
||||||
|
.map(|m| m.len());
|
||||||
|
Ok(Input {
|
||||||
|
reader: Box::new(BufReader::new(file)),
|
||||||
|
length,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
None => Ok(Input {
|
||||||
|
// `Stdin` is `Send` (unlike `StdinLock`), so wrap it in a
|
||||||
|
// `BufReader` and box for cross-thread use in the parallel pipeline.
|
||||||
|
reader: Box::new(BufReader::new(io::stdin())),
|
||||||
|
length: None,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub struct OutSinkOptions {
|
||||||
|
pub force: bool,
|
||||||
|
pub input_file: Option<PathBuf>,
|
||||||
|
pub temp_dir: Option<PathBuf>,
|
||||||
|
pub buffer_verify_stdout: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct SecureTempFile {
|
||||||
|
path: PathBuf,
|
||||||
|
file: Option<File>,
|
||||||
|
remove_on_drop: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SecureTempFile {
|
||||||
|
fn create(dir: &Path, prefix: &str) -> io::Result<Self> {
|
||||||
|
fs::create_dir_all(dir)?;
|
||||||
|
for _ in 0..128 {
|
||||||
|
let mut rand = [0u8; 16];
|
||||||
|
getrandom::fill(&mut rand).map_err(io::Error::other)?;
|
||||||
|
let name = format!("{prefix}.{}.tmp", hex(&rand));
|
||||||
|
let path = dir.join(name);
|
||||||
|
let mut opts = OpenOptions::new();
|
||||||
|
opts.read(true).write(true).create_new(true);
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
opts.mode(0o600);
|
||||||
|
}
|
||||||
|
match opts.open(&path) {
|
||||||
|
Ok(file) => {
|
||||||
|
return Ok(Self {
|
||||||
|
path,
|
||||||
|
file: Some(file),
|
||||||
|
remove_on_drop: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(io::Error::new(
|
||||||
|
io::ErrorKind::AlreadyExists,
|
||||||
|
"could not create a unique temporary file after 128 attempts",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_mut(&mut self) -> &mut File {
|
||||||
|
self.file
|
||||||
|
.as_mut()
|
||||||
|
.expect("temporary file handle taken before commit")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sync_file(&mut self) -> io::Result<()> {
|
||||||
|
let file = self.file_mut();
|
||||||
|
file.flush()?;
|
||||||
|
file.sync_all()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn persist(mut self, final_path: &Path) -> io::Result<()> {
|
||||||
|
self.sync_file()?;
|
||||||
|
self.file.take();
|
||||||
|
#[cfg(windows)]
|
||||||
|
if final_path.exists() {
|
||||||
|
fs::remove_file(final_path)?;
|
||||||
|
}
|
||||||
|
fs::rename(&self.path, final_path)?;
|
||||||
|
self.remove_on_drop = false;
|
||||||
|
best_effort_fsync_parent(final_path);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn copy_to_stdout(mut self) -> io::Result<()> {
|
||||||
|
self.sync_file()?;
|
||||||
|
let mut file = self
|
||||||
|
.file
|
||||||
|
.take()
|
||||||
|
.expect("temporary file handle taken before stdout commit");
|
||||||
|
file.seek(SeekFrom::Start(0))?;
|
||||||
|
let mut stdout = io::stdout();
|
||||||
|
io::copy(&mut file, &mut stdout)?;
|
||||||
|
stdout.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for SecureTempFile {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.file.take();
|
||||||
|
if self.remove_on_drop {
|
||||||
|
let _ = fs::remove_file(&self.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hex(bytes: &[u8]) -> String {
|
||||||
|
const HEX: &[u8; 16] = b"0123456789abcdef";
|
||||||
|
let mut out = String::with_capacity(bytes.len() * 2);
|
||||||
|
for b in bytes {
|
||||||
|
out.push(HEX[(b >> 4) as usize] as char);
|
||||||
|
out.push(HEX[(b & 0x0f) as usize] as char);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn best_effort_fsync_parent(path: &Path) {
|
||||||
|
let Some(parent) = path.parent() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if let Ok(dir) = File::open(parent) {
|
||||||
|
let _ = dir.sync_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn temp_dir_for_target(final_path: &Path, explicit: Option<&Path>) -> PathBuf {
|
||||||
|
if let Some(dir) = explicit {
|
||||||
|
return dir.to_path_buf();
|
||||||
|
}
|
||||||
|
final_path
|
||||||
|
.parent()
|
||||||
|
.filter(|p| !p.as_os_str().is_empty())
|
||||||
|
.map(Path::to_path_buf)
|
||||||
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn temp_dir_for_stdout(explicit: Option<&Path>) -> PathBuf {
|
||||||
|
explicit
|
||||||
|
.map(Path::to_path_buf)
|
||||||
|
.unwrap_or_else(std::env::temp_dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_name_prefix(path: &Path) -> String {
|
||||||
|
path.file_name()
|
||||||
|
.and_then(|name| name.to_str())
|
||||||
|
.filter(|name| !name.is_empty())
|
||||||
|
.unwrap_or("fcry")
|
||||||
|
.to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn output_aliases_input(output: &Path, input: Option<&Path>) -> io::Result<bool> {
|
||||||
|
let Some(input) = input else {
|
||||||
|
return Ok(false);
|
||||||
|
};
|
||||||
|
match same_file::is_same_file(input, output) {
|
||||||
|
Ok(same) => Ok(same),
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Output sink that supports atomic file replacement.
|
/// Output sink that supports atomic file replacement.
|
||||||
///
|
///
|
||||||
/// For file outputs: bytes are written to `<path>.tmp`. On `commit()`, the
|
/// For file outputs: bytes are written to a private, randomly named temp file.
|
||||||
/// temp file is renamed into place. If dropped without commit (panic, error,
|
/// On `commit()`, the temp file is fsynced and renamed into place. If dropped
|
||||||
/// process exit), the temp file is deleted so a partial/garbage file does
|
/// without commit (panic, error, process exit), the temp file is deleted so a
|
||||||
/// not replace any existing target.
|
/// partial/garbage file does not replace any existing target.
|
||||||
///
|
///
|
||||||
/// For stdout: behaves as a passthrough; `commit()` is a no-op.
|
/// For stdout: behaves as a passthrough; `commit()` is a no-op.
|
||||||
pub enum OutSink {
|
pub enum OutSink {
|
||||||
Stdout(io::Stdout),
|
Stdout(io::Stdout),
|
||||||
|
BufferVerify {
|
||||||
|
temp: SecureTempFile,
|
||||||
|
},
|
||||||
File {
|
File {
|
||||||
tmp_path: PathBuf,
|
|
||||||
final_path: PathBuf,
|
final_path: PathBuf,
|
||||||
file: Option<File>,
|
temp: SecureTempFile,
|
||||||
committed: bool,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutSink {
|
impl OutSink {
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn open<S: AsRef<str>>(output_file: Option<S>) -> io::Result<Self> {
|
pub fn open<S: AsRef<str>>(output_file: Option<S>) -> io::Result<Self> {
|
||||||
|
Self::open_with_options(output_file, &OutSinkOptions::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open_with_options<S: AsRef<str>>(
|
||||||
|
output_file: Option<S>,
|
||||||
|
options: &OutSinkOptions,
|
||||||
|
) -> io::Result<Self> {
|
||||||
match output_file {
|
match output_file {
|
||||||
|
None if options.buffer_verify_stdout => {
|
||||||
|
let dir = temp_dir_for_stdout(options.temp_dir.as_deref());
|
||||||
|
Ok(Self::BufferVerify {
|
||||||
|
temp: SecureTempFile::create(&dir, "fcry-buffer")?,
|
||||||
|
})
|
||||||
|
}
|
||||||
None => Ok(Self::Stdout(io::stdout())),
|
None => Ok(Self::Stdout(io::stdout())),
|
||||||
Some(f) => {
|
Some(f) => {
|
||||||
let final_path = PathBuf::from(f.as_ref());
|
let final_path = PathBuf::from(f.as_ref());
|
||||||
let mut tmp_path = final_path.clone();
|
if final_path.exists()
|
||||||
let name = tmp_path
|
&& !options.force
|
||||||
.file_name()
|
&& !output_aliases_input(&final_path, options.input_file.as_deref())?
|
||||||
.map(|n| n.to_os_string())
|
{
|
||||||
.unwrap_or_default();
|
return Err(io::Error::new(
|
||||||
let mut tmp_name = name;
|
io::ErrorKind::AlreadyExists,
|
||||||
tmp_name.push(".tmp");
|
format!(
|
||||||
tmp_path.set_file_name(tmp_name);
|
"output file {} already exists (use --force to replace it)",
|
||||||
let file = File::create(&tmp_path)?;
|
final_path.display()
|
||||||
Ok(Self::File {
|
),
|
||||||
tmp_path,
|
));
|
||||||
final_path,
|
}
|
||||||
file: Some(file),
|
let dir = temp_dir_for_target(&final_path, options.temp_dir.as_deref());
|
||||||
committed: false,
|
let prefix = file_name_prefix(&final_path);
|
||||||
})
|
let temp = SecureTempFile::create(&dir, &prefix)?;
|
||||||
|
Ok(Self::File { final_path, temp })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn commit(mut self) -> io::Result<()> {
|
pub fn commit(mut self) -> io::Result<()> {
|
||||||
if let Self::File {
|
match &mut self {
|
||||||
tmp_path,
|
Self::Stdout(s) => s.flush()?,
|
||||||
final_path,
|
Self::BufferVerify { .. } => {}
|
||||||
file,
|
Self::File { .. } => {}
|
||||||
committed,
|
}
|
||||||
} = &mut self
|
match self {
|
||||||
{
|
Self::Stdout(_) => {}
|
||||||
if let Some(mut f) = file.take() {
|
Self::BufferVerify { temp } => temp.copy_to_stdout()?,
|
||||||
f.flush()?;
|
Self::File { final_path, temp } => temp.persist(&final_path)?,
|
||||||
f.sync_all()?;
|
|
||||||
}
|
|
||||||
fs::rename(&*tmp_path, &*final_path)?;
|
|
||||||
*committed = true;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -83,33 +275,16 @@ impl Write for OutSink {
|
|||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
match self {
|
match self {
|
||||||
Self::Stdout(s) => s.write(buf),
|
Self::Stdout(s) => s.write(buf),
|
||||||
Self::File { file, .. } => file.as_mut().expect("file taken before commit").write(buf),
|
Self::BufferVerify { temp } => temp.file_mut().write(buf),
|
||||||
|
Self::File { temp, .. } => temp.file_mut().write(buf),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
fn flush(&mut self) -> io::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::Stdout(s) => s.flush(),
|
Self::Stdout(s) => s.flush(),
|
||||||
Self::File { file, .. } => match file.as_mut() {
|
Self::BufferVerify { temp } => temp.file_mut().flush(),
|
||||||
Some(f) => f.flush(),
|
Self::File { temp, .. } => temp.file_mut().flush(),
|
||||||
None => Ok(()),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for OutSink {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if let Self::File {
|
|
||||||
tmp_path,
|
|
||||||
committed,
|
|
||||||
file,
|
|
||||||
..
|
|
||||||
} = self
|
|
||||||
&& !*committed
|
|
||||||
{
|
|
||||||
file.take(); // close the file before unlink
|
|
||||||
let _ = fs::remove_file(tmp_path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+840
-28
@@ -7,19 +7,28 @@
|
|||||||
// wrong key, truncation, bad magic).
|
// wrong key, truncation, bad magic).
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::{ErrorKind, Write};
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
use assert_cmd::cargo::CommandCargoExt;
|
use assert_cmd::cargo::CommandCargoExt;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
const KEY: &[u8; 32] = b"0123456789abcdef0123456789abcdef";
|
const KEY: &[u8; 32] = b"0123456789abcdef0123456789abcdef";
|
||||||
const KEY_STR: &str = "0123456789abcdef0123456789abcdef";
|
|
||||||
|
|
||||||
fn fcry() -> Command {
|
fn fcry() -> Command {
|
||||||
Command::cargo_bin("fcry").unwrap()
|
Command::cargo_bin("fcry").unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_key_file(dir: &std::path::Path) -> std::path::PathBuf {
|
||||||
|
let key = dir.join("key.bin");
|
||||||
|
fs::write(&key, KEY).unwrap();
|
||||||
|
key
|
||||||
|
}
|
||||||
|
|
||||||
|
fn key_file_near(path: &std::path::Path) -> std::path::PathBuf {
|
||||||
|
write_key_file(path.parent().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
/// Deterministic pseudo-random plaintext of `n` bytes (xorshift, seedable).
|
/// Deterministic pseudo-random plaintext of `n` bytes (xorshift, seedable).
|
||||||
/// We avoid `/dev/urandom` so tests are reproducible on failure.
|
/// We avoid `/dev/urandom` so tests are reproducible on failure.
|
||||||
fn pseudo_random(seed: u64, n: usize) -> Vec<u8> {
|
fn pseudo_random(seed: u64, n: usize) -> Vec<u8> {
|
||||||
@@ -37,12 +46,13 @@ fn pseudo_random(seed: u64, n: usize) -> Vec<u8> {
|
|||||||
|
|
||||||
fn encrypt_file(plain: &std::path::Path, ct: &std::path::Path, chunk_size: Option<u32>) {
|
fn encrypt_file(plain: &std::path::Path, ct: &std::path::Path, chunk_size: Option<u32>) {
|
||||||
let mut cmd = fcry();
|
let mut cmd = fcry();
|
||||||
|
let key = key_file_near(ct);
|
||||||
cmd.arg("-i")
|
cmd.arg("-i")
|
||||||
.arg(plain)
|
.arg(plain)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(ct)
|
.arg(ct)
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR);
|
.arg(key);
|
||||||
if let Some(cs) = chunk_size {
|
if let Some(cs) = chunk_size {
|
||||||
cmd.arg("--chunk-size").arg(cs.to_string());
|
cmd.arg("--chunk-size").arg(cs.to_string());
|
||||||
}
|
}
|
||||||
@@ -55,14 +65,15 @@ fn encrypt_file(plain: &std::path::Path, ct: &std::path::Path, chunk_size: Optio
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn decrypt_file(ct: &std::path::Path, rt: &std::path::Path) {
|
fn decrypt_file(ct: &std::path::Path, rt: &std::path::Path) {
|
||||||
|
let key = key_file_near(ct);
|
||||||
let out = fcry()
|
let out = fcry()
|
||||||
.arg("-d")
|
.arg("-d")
|
||||||
.arg("-i")
|
.arg("-i")
|
||||||
.arg(ct)
|
.arg(ct)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(rt)
|
.arg(rt)
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(key)
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
@@ -133,10 +144,12 @@ fn roundtrip_chunk_size_one_byte() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn roundtrip_pipe_stdin_stdout() {
|
fn roundtrip_pipe_stdin_stdout() {
|
||||||
let data = pseudo_random(42, 200_000);
|
let data = pseudo_random(42, 200_000);
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let key = write_key_file(dir.path());
|
||||||
|
|
||||||
let mut enc = fcry()
|
let mut enc = fcry()
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(&key)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
@@ -152,8 +165,8 @@ fn roundtrip_pipe_stdin_stdout() {
|
|||||||
|
|
||||||
let mut dec = fcry()
|
let mut dec = fcry()
|
||||||
.arg("-d")
|
.arg("-d")
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(&key)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
@@ -182,19 +195,24 @@ fn rejects_wrong_key() {
|
|||||||
fs::write(&plain, pseudo_random(1, 1000)).unwrap();
|
fs::write(&plain, pseudo_random(1, 1000)).unwrap();
|
||||||
encrypt_file(&plain, &ct, None);
|
encrypt_file(&plain, &ct, None);
|
||||||
|
|
||||||
let wrong = "ffffffffffffffffffffffffffffffff";
|
let wrong = dir.path().join("wrong.key");
|
||||||
assert_ne!(wrong.as_bytes(), KEY);
|
fs::write(&wrong, b"ffffffffffffffffffffffffffffffff").unwrap();
|
||||||
let out = fcry()
|
let out = fcry()
|
||||||
.arg("-d")
|
.arg("-d")
|
||||||
.arg("-i")
|
.arg("-i")
|
||||||
.arg(&ct)
|
.arg(&ct)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(dir.path().join("rt.bin"))
|
.arg(dir.path().join("rt.bin"))
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(wrong)
|
.arg(wrong)
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(!out.status.success(), "decrypt with wrong key should fail");
|
assert!(!out.status.success(), "decrypt with wrong key should fail");
|
||||||
|
assert!(
|
||||||
|
String::from_utf8_lossy(&out.stderr).contains("WrongKey"),
|
||||||
|
"expected distinct WrongKey error, got {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -216,8 +234,8 @@ fn rejects_tampered_header() {
|
|||||||
.arg(&ct)
|
.arg(&ct)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(dir.path().join("rt.bin"))
|
.arg(dir.path().join("rt.bin"))
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(key_file_near(&ct))
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
@@ -246,8 +264,8 @@ fn rejects_tampered_ciphertext() {
|
|||||||
.arg(&ct)
|
.arg(&ct)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(dir.path().join("rt.bin"))
|
.arg(dir.path().join("rt.bin"))
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(key_file_near(&ct))
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
@@ -275,8 +293,8 @@ fn rejects_truncated_ciphertext() {
|
|||||||
.arg(&ct)
|
.arg(&ct)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(dir.path().join("rt.bin"))
|
.arg(dir.path().join("rt.bin"))
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(key_file_near(&ct))
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
@@ -296,8 +314,8 @@ fn rejects_bad_magic() {
|
|||||||
.arg(&bogus)
|
.arg(&bogus)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(dir.path().join("rt.bin"))
|
.arg(dir.path().join("rt.bin"))
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(KEY_STR)
|
.arg(write_key_file(dir.path()))
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
@@ -312,25 +330,145 @@ fn rejects_bad_magic() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rejects_short_raw_key() {
|
fn rejects_short_key_file() {
|
||||||
let dir = TempDir::new().unwrap();
|
let dir = TempDir::new().unwrap();
|
||||||
let plain = dir.path().join("p.bin");
|
let plain = dir.path().join("p.bin");
|
||||||
|
let key = dir.path().join("short.key");
|
||||||
fs::write(&plain, b"hello").unwrap();
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
fs::write(&key, b"tooshort").unwrap();
|
||||||
let out = fcry()
|
let out = fcry()
|
||||||
.arg("-i")
|
.arg("-i")
|
||||||
.arg(&plain)
|
.arg(&plain)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(dir.path().join("c.bin"))
|
.arg(dir.path().join("c.bin"))
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg("tooshort")
|
.arg(&key)
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
!out.status.success(),
|
!out.status.success(),
|
||||||
"encrypt with short raw_key should fail"
|
"encrypt with short key file should fail"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejects_long_key_file_and_trailing_newline() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let key = dir.path().join("long.key");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
fs::write(&key, b"0123456789abcdef0123456789abcdef\n").unwrap();
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(dir.path().join("c.bin"))
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!out.status.success(), "long key file should fail");
|
||||||
|
assert!(
|
||||||
|
String::from_utf8_lossy(&out.stderr).contains("too long"),
|
||||||
|
"expected too-long error, got {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn non_utf8_key_file_roundtrips() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let key = dir.path().join("key.bin");
|
||||||
|
let plain = dir.path().join("plain.bin");
|
||||||
|
let ct = dir.path().join("ct.bin");
|
||||||
|
let rt = dir.path().join("rt.bin");
|
||||||
|
let key_bytes: Vec<u8> = (0..32u8).map(|b| b ^ 0x80).collect();
|
||||||
|
let data = pseudo_random(31, 8192);
|
||||||
|
fs::write(&key, key_bytes).unwrap();
|
||||||
|
fs::write(&plain, &data).unwrap();
|
||||||
|
|
||||||
|
let enc = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
enc.status.success(),
|
||||||
|
"non-UTF-8 key encrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&enc.stderr)
|
||||||
|
);
|
||||||
|
let dec = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&rt)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
dec.status.success(),
|
||||||
|
"non-UTF-8 key decrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&dec.stderr)
|
||||||
|
);
|
||||||
|
assert_eq!(fs::read(&rt).unwrap(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[test]
|
||||||
|
fn split_fifo_key_file_read_roundtrips() {
|
||||||
|
use std::ffi::CString;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let fifo = dir.path().join("key.fifo");
|
||||||
|
let fifo_c = CString::new(fifo.as_os_str().as_bytes()).unwrap();
|
||||||
|
let rc = unsafe { libc::mkfifo(fifo_c.as_ptr(), 0o600) };
|
||||||
|
assert_eq!(rc, 0, "mkfifo failed: {}", std::io::Error::last_os_error());
|
||||||
|
|
||||||
|
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(33, 8192);
|
||||||
|
fs::write(&plain, &data).unwrap();
|
||||||
|
|
||||||
|
let fifo_writer = fifo.clone();
|
||||||
|
let writer = thread::spawn(move || {
|
||||||
|
let mut file = OpenOptions::new().write(true).open(&fifo_writer).unwrap();
|
||||||
|
file.write_all(&KEY[..8]).unwrap();
|
||||||
|
file.flush().unwrap();
|
||||||
|
thread::sleep(Duration::from_millis(50));
|
||||||
|
file.write_all(&KEY[8..]).unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
let enc = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&fifo)
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
writer.join().unwrap();
|
||||||
|
assert!(
|
||||||
|
enc.status.success(),
|
||||||
|
"split FIFO key encrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&enc.stderr)
|
||||||
|
);
|
||||||
|
|
||||||
|
decrypt_file(&ct, &rt);
|
||||||
|
assert_eq!(fs::read(&rt).unwrap(), data);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn roundtrip_passphrase_argon2id() {
|
fn roundtrip_passphrase_argon2id() {
|
||||||
let dir = TempDir::new().unwrap();
|
let dir = TempDir::new().unwrap();
|
||||||
@@ -352,6 +490,7 @@ fn roundtrip_passphrase_argon2id() {
|
|||||||
.arg("8")
|
.arg("8")
|
||||||
.arg("--argon-passes")
|
.arg("--argon-passes")
|
||||||
.arg("1")
|
.arg("1")
|
||||||
|
.arg("--allow-weak-kdf")
|
||||||
.env("FCRY_TEST_PW", "correct horse battery staple")
|
.env("FCRY_TEST_PW", "correct horse battery staple")
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -394,6 +533,70 @@ fn roundtrip_passphrase_argon2id() {
|
|||||||
assert!(!bad.status.success(), "wrong passphrase should fail");
|
assert!(!bad.status.success(), "wrong passphrase should fail");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn weak_passphrase_kdf_rejected_without_override() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
let enc = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(dir.path().join("c.bin"))
|
||||||
|
.arg("--passphrase-env")
|
||||||
|
.arg("FCRY_TEST_PW")
|
||||||
|
.arg("--argon-memory")
|
||||||
|
.arg("8")
|
||||||
|
.arg("--argon-passes")
|
||||||
|
.arg("1")
|
||||||
|
.env("FCRY_TEST_PW", "short")
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!enc.status.success(), "weak KDF/passphrase should fail");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn decrypt_argon_memory_cap_rejects_hostile_header() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
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")
|
||||||
|
.arg("--allow-weak-kdf")
|
||||||
|
.env("FCRY_TEST_PW", "correct horse battery staple")
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(enc.status.success());
|
||||||
|
|
||||||
|
let dec = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("--passphrase-env")
|
||||||
|
.arg("FCRY_TEST_PW")
|
||||||
|
.arg("--max-argon-memory-mib")
|
||||||
|
.arg("1")
|
||||||
|
.env("FCRY_TEST_PW", "correct horse battery staple")
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!dec.status.success(), "low decrypt cap should reject file");
|
||||||
|
assert!(
|
||||||
|
String::from_utf8_lossy(&dec.stderr).contains("decrypt cap"),
|
||||||
|
"expected cap error, got {}",
|
||||||
|
String::from_utf8_lossy(&dec.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn atomic_output_no_stale_tmp_on_failure() {
|
fn atomic_output_no_stale_tmp_on_failure() {
|
||||||
// A failed decrypt (wrong key) should not leave the output file behind.
|
// A failed decrypt (wrong key) should not leave the output file behind.
|
||||||
@@ -404,15 +607,16 @@ fn atomic_output_no_stale_tmp_on_failure() {
|
|||||||
fs::write(&plain, b"hello world").unwrap();
|
fs::write(&plain, b"hello world").unwrap();
|
||||||
encrypt_file(&plain, &ct, None);
|
encrypt_file(&plain, &ct, None);
|
||||||
|
|
||||||
let wrong = "ffffffffffffffffffffffffffffffff";
|
let wrong = dir.path().join("wrong.key");
|
||||||
|
fs::write(&wrong, b"ffffffffffffffffffffffffffffffff").unwrap();
|
||||||
let out = fcry()
|
let out = fcry()
|
||||||
.arg("-d")
|
.arg("-d")
|
||||||
.arg("-i")
|
.arg("-i")
|
||||||
.arg(&ct)
|
.arg(&ct)
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(&rt)
|
.arg(&rt)
|
||||||
.arg("--raw-key")
|
.arg("--key-file")
|
||||||
.arg(wrong)
|
.arg(&wrong)
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(!out.status.success());
|
assert!(!out.status.success());
|
||||||
@@ -422,6 +626,614 @@ fn atomic_output_no_stale_tmp_on_failure() {
|
|||||||
assert!(!tmp.exists(), "temp file must be cleaned up");
|
assert!(!tmp.exists(), "temp file must be cleaned up");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn existing_output_refuses_without_force() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
fs::write(&ct, b"existing").unwrap();
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!out.status.success(), "existing output should refuse");
|
||||||
|
assert_eq!(fs::read(&ct).unwrap(), b"existing");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn force_replaces_only_after_success() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
fs::write(&ct, b"existing").unwrap();
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("--force")
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
out.status.success(),
|
||||||
|
"force encrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
assert_ne!(fs::read(&ct).unwrap(), b"existing");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn in_place_replacement_roundtrips() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("data.bin");
|
||||||
|
let original = pseudo_random(41, 50_000);
|
||||||
|
fs::write(&path, &original).unwrap();
|
||||||
|
|
||||||
|
let enc = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&path)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&path)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
enc.status.success(),
|
||||||
|
"in-place encrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&enc.stderr)
|
||||||
|
);
|
||||||
|
assert_ne!(fs::read(&path).unwrap(), original);
|
||||||
|
|
||||||
|
let dec = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&path)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&path)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
dec.status.success(),
|
||||||
|
"in-place decrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&dec.stderr)
|
||||||
|
);
|
||||||
|
assert_eq!(fs::read(&path).unwrap(), original);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn old_predictable_temp_name_input_is_not_truncated() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let input = dir.path().join("out.bin.tmp");
|
||||||
|
let output = dir.path().join("out.bin");
|
||||||
|
let original = pseudo_random(42, 1024);
|
||||||
|
fs::write(&input, &original).unwrap();
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&input)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&output)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
out.status.success(),
|
||||||
|
"encrypt failed: {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
assert_eq!(fs::read(&input).unwrap(), original);
|
||||||
|
assert!(output.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[test]
|
||||||
|
fn output_file_mode_is_0600() {
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
encrypt_file(&plain, &ct, None);
|
||||||
|
let mode = fs::metadata(&ct).unwrap().permissions().mode() & 0o777;
|
||||||
|
assert_eq!(mode, 0o600);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Multi-threaded pipeline + length-committed + random-access tests
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn encrypt_file_threads(
|
||||||
|
plain: &std::path::Path,
|
||||||
|
ct: &std::path::Path,
|
||||||
|
chunk_size: Option<u32>,
|
||||||
|
threads: usize,
|
||||||
|
) {
|
||||||
|
let mut cmd = fcry();
|
||||||
|
let key = key_file_near(ct);
|
||||||
|
cmd.arg("-i")
|
||||||
|
.arg(plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(ct)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(key)
|
||||||
|
.arg("-j")
|
||||||
|
.arg(threads.to_string());
|
||||||
|
if let Some(cs) = chunk_size {
|
||||||
|
cmd.arg("--chunk-size").arg(cs.to_string());
|
||||||
|
}
|
||||||
|
let out = cmd.output().unwrap();
|
||||||
|
assert!(
|
||||||
|
out.status.success(),
|
||||||
|
"encrypt -j{threads} failed: {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrypt_file_threads(ct: &std::path::Path, rt: &std::path::Path, threads: usize) {
|
||||||
|
let key = key_file_near(ct);
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(ct)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(rt)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(key)
|
||||||
|
.arg("-j")
|
||||||
|
.arg(threads.to_string())
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
out.status.success(),
|
||||||
|
"decrypt -j{threads} failed: {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn roundtrip_multi_threaded() {
|
||||||
|
// Multi-chunk input. Encrypt+decrypt with -j 4 must round-trip.
|
||||||
|
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(11, 5 * 1024 * 1024 + 12345);
|
||||||
|
fs::write(&plain, &data).unwrap();
|
||||||
|
|
||||||
|
encrypt_file_threads(&plain, &ct, Some(64 * 1024), 4);
|
||||||
|
decrypt_file_threads(&ct, &rt, 4);
|
||||||
|
assert_eq!(fs::read(&rt).unwrap(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parallel_and_serial_outputs_round_trip() {
|
||||||
|
// Encrypt with -j 4 and decrypt serially (and vice-versa); both directions
|
||||||
|
// must yield the original plaintext.
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let data = pseudo_random(13, 256 * 1024 + 17);
|
||||||
|
fs::write(&plain, &data).unwrap();
|
||||||
|
|
||||||
|
let ct_par = dir.path().join("c_par.bin");
|
||||||
|
let ct_ser = dir.path().join("c_ser.bin");
|
||||||
|
encrypt_file_threads(&plain, &ct_par, Some(8192), 4);
|
||||||
|
encrypt_file_threads(&plain, &ct_ser, Some(8192), 1);
|
||||||
|
|
||||||
|
let rt1 = dir.path().join("r1.bin");
|
||||||
|
let rt2 = dir.path().join("r2.bin");
|
||||||
|
// par-encrypted, serial-decrypted
|
||||||
|
decrypt_file_threads(&ct_par, &rt1, 1);
|
||||||
|
// serial-encrypted, par-decrypted
|
||||||
|
decrypt_file_threads(&ct_ser, &rt2, 4);
|
||||||
|
assert_eq!(fs::read(&rt1).unwrap(), data);
|
||||||
|
assert_eq!(fs::read(&rt2).unwrap(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn roundtrip_pipe_multi_threaded() {
|
||||||
|
// stdin/stdout mode with -j 4: length flag must NOT be set (no committed
|
||||||
|
// length when we don't know the input size), but encrypt/decrypt must still
|
||||||
|
// round-trip cleanly across the pipeline.
|
||||||
|
let data = pseudo_random(14, 200_000);
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let key = write_key_file(dir.path());
|
||||||
|
|
||||||
|
let mut enc = fcry()
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.arg("-j")
|
||||||
|
.arg("4")
|
||||||
|
.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 -j4 failed: {}",
|
||||||
|
String::from_utf8_lossy(&enc_out.stderr)
|
||||||
|
);
|
||||||
|
|
||||||
|
// flags byte at offset 6 must not set length commitment for stdin input.
|
||||||
|
assert_eq!(
|
||||||
|
enc_out.stdout[6] & 0x01,
|
||||||
|
0,
|
||||||
|
"stdin-encrypted file unexpectedly committed length"
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut dec = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.arg("-j")
|
||||||
|
.arg("4")
|
||||||
|
.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 -j4 failed: {}",
|
||||||
|
String::from_utf8_lossy(&dec_out.stderr)
|
||||||
|
);
|
||||||
|
assert_eq!(dec_out.stdout, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stdin_chunk_size_zero_fails_but_empty_valid_chunk_succeeds() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let key = write_key_file(dir.path());
|
||||||
|
let mut bad = fcry()
|
||||||
|
.arg("--chunk-size")
|
||||||
|
.arg("0")
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.unwrap();
|
||||||
|
// Invalid options can make the child exit before it drains stdin.
|
||||||
|
if let Err(err) = bad.stdin.as_mut().unwrap().write_all(b"x") {
|
||||||
|
assert_eq!(
|
||||||
|
err.kind(),
|
||||||
|
ErrorKind::BrokenPipe,
|
||||||
|
"unexpected stdin write error for failing chunk-size 0 process: {err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let bad_out = bad.wait_with_output().unwrap();
|
||||||
|
assert!(!bad_out.status.success(), "chunk-size 0 should fail");
|
||||||
|
|
||||||
|
let mut good = fcry()
|
||||||
|
.arg("--chunk-size")
|
||||||
|
.arg("1")
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.unwrap();
|
||||||
|
drop(good.stdin.take());
|
||||||
|
let good_out = good.wait_with_output().unwrap();
|
||||||
|
assert!(
|
||||||
|
good_out.status.success(),
|
||||||
|
"empty stdin with valid chunk should succeed: {}",
|
||||||
|
String::from_utf8_lossy(&good_out.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn huge_thread_count_is_bounded() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
fs::write(&plain, b"hello").unwrap();
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&plain)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.arg("-j")
|
||||||
|
.arg("1000000")
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
out.status.success(),
|
||||||
|
"huge -j should be capped, got {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
assert!(String::from_utf8_lossy(&out.stderr).contains("capped"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn forged_huge_chunk_header_fails_before_allocation() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let forged = dir.path().join("forged.bin");
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
bytes.extend_from_slice(b"fcry");
|
||||||
|
bytes.push(3); // version
|
||||||
|
bytes.push(1); // alg
|
||||||
|
bytes.push(0x02); // key commitment flag
|
||||||
|
bytes.push(0); // reserved
|
||||||
|
bytes.extend_from_slice(&u32::MAX.to_le_bytes());
|
||||||
|
fs::write(&forged, bytes).unwrap();
|
||||||
|
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&forged)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!out.status.success(), "huge chunk header should fail");
|
||||||
|
assert!(
|
||||||
|
String::from_utf8_lossy(&out.stderr).contains("chunk_size"),
|
||||||
|
"expected chunk_size error, got {}",
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_input_commits_length() {
|
||||||
|
// Encrypting from a regular file must auto-set FLAG_LENGTH_COMMITTED (bit 0
|
||||||
|
// of the flags byte at offset 6) and embed the length.
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let plain = dir.path().join("p.bin");
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
let data = pseudo_random(15, 50_000);
|
||||||
|
fs::write(&plain, &data).unwrap();
|
||||||
|
encrypt_file(&plain, &ct, Some(4096));
|
||||||
|
|
||||||
|
let bytes = fs::read(&ct).unwrap();
|
||||||
|
// Magic(4) + version(1) + alg(1) + flags(1) = byte 6
|
||||||
|
assert_eq!(bytes[4], 3, "version should be 3");
|
||||||
|
assert_eq!(bytes[6] & 0x01, 0x01, "length-committed flag should be set");
|
||||||
|
assert_eq!(bytes[6] & 0x02, 0x02, "key-committed flag should be set");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn v3_downgrade_or_commitment_stripping_fails_authentication() {
|
||||||
|
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, pseudo_random(51, 1000)).unwrap();
|
||||||
|
encrypt_file(&plain, &ct, None);
|
||||||
|
|
||||||
|
let mut bytes = fs::read(&ct).unwrap();
|
||||||
|
bytes[4] = 2;
|
||||||
|
bytes[6] &= !0x02;
|
||||||
|
fs::write(&ct, bytes).unwrap();
|
||||||
|
|
||||||
|
let out = fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(&ct)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&rt)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(key_file_near(&ct))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
!out.status.success(),
|
||||||
|
"downgraded/stripped v3 header must fail authentication"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encrypt_random_access_fixture(
|
||||||
|
dir: &std::path::Path,
|
||||||
|
data: &[u8],
|
||||||
|
chunk_size: u32,
|
||||||
|
) -> std::path::PathBuf {
|
||||||
|
let plain = dir.join("p.bin");
|
||||||
|
let ct = dir.join("c.bin");
|
||||||
|
fs::write(&plain, data).unwrap();
|
||||||
|
encrypt_file(&plain, &ct, Some(chunk_size));
|
||||||
|
ct
|
||||||
|
}
|
||||||
|
|
||||||
|
fn random_access_decrypt(
|
||||||
|
ct: &std::path::Path,
|
||||||
|
out: &std::path::Path,
|
||||||
|
offset: u64,
|
||||||
|
length: u64,
|
||||||
|
) -> std::process::Output {
|
||||||
|
fcry()
|
||||||
|
.arg("-d")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(ct)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(out)
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(key_file_near(ct))
|
||||||
|
.arg("--offset")
|
||||||
|
.arg(offset.to_string())
|
||||||
|
.arg("--length")
|
||||||
|
.arg(length.to_string())
|
||||||
|
.output()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn random_access_decrypt_slices() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let chunk = 4096u32;
|
||||||
|
let total = 5 * 1024 * 1024 + 12345;
|
||||||
|
let data = pseudo_random(16, total);
|
||||||
|
let ct = encrypt_random_access_fixture(dir.path(), &data, chunk);
|
||||||
|
|
||||||
|
// (offset, length) cases:
|
||||||
|
// - chunk-aligned start, mid-chunk end
|
||||||
|
// - mid-chunk start crossing several chunks
|
||||||
|
// - last partial chunk
|
||||||
|
// - last byte
|
||||||
|
// - entire file
|
||||||
|
let cases: &[(u64, u64)] = &[
|
||||||
|
(0, 1),
|
||||||
|
(chunk as u64, 7),
|
||||||
|
(chunk as u64 - 5, 100),
|
||||||
|
(10, chunk as u64 * 3 + 17),
|
||||||
|
(total as u64 - 1, 1),
|
||||||
|
(total as u64 - 100, 100),
|
||||||
|
(0, total as u64),
|
||||||
|
];
|
||||||
|
for (i, (offset, length)) in cases.iter().copied().enumerate() {
|
||||||
|
let out = dir.path().join(format!("slice_{i}.bin"));
|
||||||
|
let r = random_access_decrypt(&ct, &out, offset, length);
|
||||||
|
assert!(
|
||||||
|
r.status.success(),
|
||||||
|
"slice {i} ({offset}, {length}) failed: {}",
|
||||||
|
String::from_utf8_lossy(&r.stderr)
|
||||||
|
);
|
||||||
|
let got = fs::read(&out).unwrap();
|
||||||
|
let expected = &data[offset as usize..(offset + length) as usize];
|
||||||
|
assert_eq!(got, expected, "slice {i} mismatch");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn random_access_rejects_out_of_range() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let data = pseudo_random(17, 1000);
|
||||||
|
let ct = encrypt_random_access_fixture(dir.path(), &data, 256);
|
||||||
|
let out = dir.path().join("oob.bin");
|
||||||
|
let r = random_access_decrypt(&ct, &out, 900, 1000); // 900+1000 > 1000
|
||||||
|
assert!(!r.status.success(), "out-of-range slice should fail");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn random_access_rejects_stdin_encrypted() {
|
||||||
|
// Encrypt via stdin → no length committed → random access must refuse.
|
||||||
|
let data = pseudo_random(18, 2000);
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let ct = dir.path().join("c.bin");
|
||||||
|
let key = write_key_file(dir.path());
|
||||||
|
|
||||||
|
let mut enc = fcry()
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(&key)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&ct)
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.unwrap();
|
||||||
|
enc.stdin.as_mut().unwrap().write_all(&data).unwrap();
|
||||||
|
assert!(enc.wait().unwrap().success());
|
||||||
|
|
||||||
|
let out = dir.path().join("slice.bin");
|
||||||
|
let r = random_access_decrypt(&ct, &out, 0, 100);
|
||||||
|
assert!(
|
||||||
|
!r.status.success(),
|
||||||
|
"random access on stdin-encrypted file should fail"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn random_access_rejects_zero_length() {
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let data = pseudo_random(19, 1000);
|
||||||
|
let ct = encrypt_random_access_fixture(dir.path(), &data, 256);
|
||||||
|
let out = dir.path().join("empty.bin");
|
||||||
|
let r = random_access_decrypt(&ct, &out, 500, 0);
|
||||||
|
assert!(!r.status.success(), "zero-length slice should fail");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn random_access_tampered_length_fails() {
|
||||||
|
// Flip a byte inside the committed plaintext_length field. The header is
|
||||||
|
// AAD for every chunk, so the AEAD must reject decryption.
|
||||||
|
let dir = TempDir::new().unwrap();
|
||||||
|
let data = pseudo_random(20, 4000);
|
||||||
|
let ct = encrypt_random_access_fixture(dir.path(), &data, 1024);
|
||||||
|
let mut bytes = fs::read(&ct).unwrap();
|
||||||
|
// For raw-kdf header: magic(4)+ver(1)+alg(1)+flags(1)+rsv(1)+chunksize(4)+kdf_id(1)+nonce_prefix(19) = 32
|
||||||
|
// plaintext_length is at offset 32..40.
|
||||||
|
bytes[34] ^= 0xff;
|
||||||
|
fs::write(&ct, &bytes).unwrap();
|
||||||
|
let out = dir.path().join("bad.bin");
|
||||||
|
let r = random_access_decrypt(&ct, &out, 0, 100);
|
||||||
|
assert!(
|
||||||
|
!r.status.success(),
|
||||||
|
"tampered plaintext_length must fail authentication"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn buffer_verify_stdout_emits_nothing_on_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(61, 3 * 1024 * 1024)).unwrap();
|
||||||
|
encrypt_file(&plain, &ct, Some(64 * 1024));
|
||||||
|
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("--buffer-verify")
|
||||||
|
.arg("--key-file")
|
||||||
|
.arg(key_file_near(&ct))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!out.status.success(), "truncated decrypt should fail");
|
||||||
|
assert!(
|
||||||
|
out.stdout.is_empty(),
|
||||||
|
"buffer-verify must suppress partial stdout"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejects_zero_threads() {
|
||||||
|
// -j 0 is almost certainly a user mistake. Clap should reject it before
|
||||||
|
// we ever reach the pipeline.
|
||||||
|
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("--key-file")
|
||||||
|
.arg(write_key_file(dir.path()))
|
||||||
|
.arg("-j")
|
||||||
|
.arg("0")
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(!out.status.success(), "-j 0 should be rejected");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn header_chunk_size_is_authoritative_on_decrypt() {
|
fn header_chunk_size_is_authoritative_on_decrypt() {
|
||||||
// Encrypt with a non-default chunk size; decrypt without specifying one.
|
// Encrypt with a non-default chunk size; decrypt without specifying one.
|
||||||
|
|||||||
Reference in New Issue
Block a user