diff --git a/CLAUDE.md b/CLAUDE.md index 53bd5ff..4a1d1c2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -7,12 +7,15 @@ The Windows app lives in the private `Custos` repo and depends on this one by gi ## Layout ``` crates/custos-vault file format, Argon2id + XChaCha20-Poly1305, atomic save, item model, SecretString -crates/custos-gen memorable password, nickname, random, PIN, strict-site generators + wordlists +crates/custos-gen memorable password, nickname, random, PIN, strict-site, secret-key and API-key generators +crates/custos-keys key pairs (Ed25519, ECDSA P-256, RSA) as OpenSSH and PKCS#8 PEM crates/custos-totp TOTP / HOTP (RFC 6238 / 4226 vectors are tests), otpauth:// parsing crates/custos-qr QR payload strings (Wi-Fi escaping); rendering is the app's job crates/custos-share private share links: XChaCha20-Poly1305 blob, key in the URL fragment, optional password crates/custos-wasm the browser build (web vault + share page): JSON `call` ABI, no wasm-bindgen; js/custos.mjs -docs/FORMAT.md the vault file format (versioned; change it before the code) +docs/FORMAT.md the vault file format, version 1 (versioned; change it before the code) +docs/FACTORS.md version 2: any two factors open the vault; the machine store +docs/KEYS.md secret keys, API keys and key pairs docs/SHARE.md the share blob and link format (versioned like FORMAT.md) docs/THREAT_MODEL.md what is and is not protected ``` @@ -33,8 +36,9 @@ On the dev machine set `CARGO_HOME=F:\C-Drive-Migration\future-caches\cargo` (C: - Commits: `[] : `, e.g. `[vault] Format: authenticate slot parameters`. - **No AI attribution in commits or PRs, ever**: no `Co-Authored-By`, no session links, no "Generated with". - No binaries in git. -- **No custom crypto.** Only RustCrypto / rust-random crates (`argon2`, `chacha20poly1305`, `zeroize`, - `rand` with `SysRng`). Randomness from the OS CSPRNG only. +- **No custom crypto.** Only RustCrypto / rust-random crates (`argon2`, `chacha20poly1305`, `hkdf`, `zeroize`, + `rand` with `SysRng`; for key pairs `ssh-key`, `p256`, `rsa` and `ed25519-dalek`, RustCrypto's Ed25519 + backend). Randomness from the OS CSPRNG only. - **Secrets never in logs, panics, error messages, `Debug`/`Display` output or test snapshots.** Secret-holding types print `[redacted]`; there is a test for each one. - Tests here are mandatory (it is public and holds the crypto). A format change updates `docs/FORMAT.md` diff --git a/Cargo.lock b/Cargo.lock index 4053f2c..e4c0948 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,8 +8,8 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" dependencies = [ - "crypto-common", - "inout", + "crypto-common 0.2.2", + "inout 0.2.5", ] [[package]] @@ -29,11 +29,23 @@ checksum = "134c52ddac6d63c576bef8168db10c83c49c26444ecbc68060fef078925a901c" dependencies = [ "base64ct", "blake2", - "cpufeatures", + "cpufeatures 0.3.1", "password-hash", "zeroize", ] +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + [[package]] name = "base64ct" version = "1.8.3" @@ -52,7 +64,16 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b5d4d889834ee8ecfc0f8426ad30faf7cdcb10f741a8e6d7224d95325479f6f" dependencies = [ - "digest", + "digest 0.11.3", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", ] [[package]] @@ -77,9 +98,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", - "cipher", - "cpufeatures", - "rand_core", + "cipher 0.5.2", + "cpufeatures 0.3.1", + "rand_core 0.10.1", ] [[package]] @@ -90,19 +111,29 @@ checksum = "9b89e1c441e926b9c82a8d023f6e1b7ae0adcfaa7d621814e4d60789bac751cb" dependencies = [ "aead", "chacha20", - "cipher", + "cipher 0.5.2", "poly1305", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.6", + "inout 0.1.4", +] + [[package]] name = "cipher" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" dependencies = [ - "block-buffer", - "crypto-common", - "inout", + "block-buffer 0.12.1", + "crypto-common 0.2.2", + "inout 0.2.5", ] [[package]] @@ -111,12 +142,27 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + [[package]] name = "const-oid" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.3.1" @@ -126,15 +172,37 @@ dependencies = [ "libc", ] +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "crypto-common" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" dependencies = [ - "getrandom", + "getrandom 0.4.3", "hybrid-array", - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -146,62 +214,103 @@ dependencies = [ "cmov", ] +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "custos-gen" -version = "0.3.0" +version = "0.4.0" dependencies = [ "custos-vault", - "rand", + "data-encoding", + "rand 0.10.3", "regex", "unicode-normalization", "zeroize", ] +[[package]] +name = "custos-keys" +version = "0.4.0" +dependencies = [ + "custos-vault", + "ed25519-dalek", + "p256", + "rand_core 0.6.4", + "rsa", + "ssh-key", + "thiserror", +] + [[package]] name = "custos-qr" -version = "0.3.0" +version = "0.4.0" [[package]] name = "custos-share" -version = "0.3.0" +version = "0.4.0" dependencies = [ "argon2", "chacha20poly1305", "custos-vault", "data-encoding", "hkdf", - "rand", + "rand 0.10.3", "serde", "serde_json", - "sha2", + "sha2 0.11.1", "thiserror", "zeroize", ] [[package]] name = "custos-totp" -version = "0.3.0" +version = "0.4.0" dependencies = [ "custos-vault", "data-encoding", - "hmac", + "hmac 0.13.0", "percent-encoding", "sha1", - "sha2", + "sha2 0.11.1", "zeroize", ] [[package]] name = "custos-vault" -version = "0.3.0" +version = "0.4.0" dependencies = [ "argon2", "chacha20poly1305", "hkdf", - "rand", + "rand 0.10.3", "serde", "serde_json", - "sha2", + "sha2 0.11.1", "tempfile", "thiserror", "zeroize", @@ -209,14 +318,14 @@ dependencies = [ [[package]] name = "custos-wasm" -version = "0.3.0" +version = "0.4.0" dependencies = [ "custos-qr", "custos-share", "custos-totp", "custos-vault", "data-encoding", - "getrandom", + "getrandom 0.4.3", "qrcode", "serde", "serde_json", @@ -229,18 +338,100 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid 0.9.6", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "const-oid 0.9.6", + "crypto-common 0.1.6", + "subtle", +] + [[package]] name = "digest" version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" dependencies = [ - "block-buffer", - "const-oid", - "crypto-common", + "block-buffer 0.12.1", + "const-oid 0.10.2", + "crypto-common 0.2.2", "ctutils", ] +[[package]] +name = "ecdsa" +version = "0.16.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +dependencies = [ + "der", + "digest 0.10.7", + "elliptic-curve", + "rfc6979", + "signature", + "spki", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" +dependencies = [ + "curve25519-dalek", + "ed25519", + "rand_core 0.6.4", + "serde", + "sha2 0.10.9", + "subtle", + "zeroize", +] + +[[package]] +name = "elliptic-curve" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +dependencies = [ + "base16ct", + "crypto-bigint", + "digest 0.10.7", + "ff", + "generic-array", + "group", + "pem-rfc7468", + "pkcs8", + "rand_core 0.6.4", + "sec1", + "subtle", + "zeroize", +] + [[package]] name = "errno" version = "0.3.14" @@ -257,6 +448,44 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "ff" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b50bfb653653f9ca9095b427bed08ab8d75a137839d9ad64eb11810d5b6393" +dependencies = [ + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "generic-array" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bb6743198531e02858aeaea5398fcc883e71851fcbcb5a2f773e2fb6cb1edf2" +dependencies = [ + "typenum", + "version_check", + "zeroize", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "getrandom" version = "0.4.3" @@ -266,7 +495,18 @@ dependencies = [ "cfg-if", "libc", "r-efi", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff", + "rand_core 0.6.4", + "subtle", ] [[package]] @@ -275,7 +515,16 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" dependencies = [ - "hmac", + "hmac 0.13.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", ] [[package]] @@ -284,7 +533,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" dependencies = [ - "digest", + "digest 0.11.3", ] [[package]] @@ -296,6 +545,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "inout" version = "0.2.5" @@ -311,12 +569,27 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +dependencies = [ + "spin", +] + [[package]] name = "libc" version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -329,12 +602,95 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "num-bigint-dig" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e661dda6640fad38e827a6d4a310ff4763082116fe217f279885c97f511bb0b7" +dependencies = [ + "lazy_static", + "libm", + "num-integer", + "num-iter", + "num-traits", + "rand 0.8.8", + "smallvec", + "zeroize", +] + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c92800bd69a1eac91786bcfe9da64a897eb72911b8dc3095decbd07429e8048b" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + [[package]] name = "once_cell" version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "p256" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", + "sha2 0.10.9", +] + +[[package]] +name = "p384" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe42f1670a52a47d448f14b6a5c61dd78fce51856e68edaa38f7ae3a46b8d6b6" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", + "sha2 0.10.9", +] + +[[package]] +name = "p521" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc9e2161f1f215afdfce23677034ae137bbd45016a880c2eb3ba8eb95f085b2" +dependencies = [ + "base16ct", + "ecdsa", + "elliptic-curve", + "primeorder", + "rand_core 0.6.4", + "sha2 0.10.9", +] + [[package]] name = "password-hash" version = "0.6.1" @@ -344,6 +700,15 @@ dependencies = [ "phc", ] +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -360,16 +725,55 @@ dependencies = [ "ctutils", ] +[[package]] +name = "pkcs1" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8ffb9f10fa047879315e6625af03c164b16962a5368d724ed16323b68ace47f" +dependencies = [ + "der", + "pkcs8", + "spki", +] + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + [[package]] name = "poly1305" version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c" dependencies = [ - "cpufeatures", + "cpufeatures 0.3.1", "universal-hash", ] +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "primeorder" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353e1ca18966c16d9deb1c69278edbc5f194139612772bd9537af60ac231e1e6" +dependencies = [ + "elliptic-curve", +] + [[package]] name = "proc-macro2" version = "1.0.107" @@ -400,6 +804,16 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "rand_chacha", + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.10.3" @@ -407,8 +821,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ "chacha20", - "getrandom", - "rand_core", + "getrandom 0.4.3", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", ] [[package]] @@ -446,6 +879,46 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac 0.12.1", + "subtle", +] + +[[package]] +name = "rsa" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8573f03f5883dcaebdfcf4725caa1ecb9c15b2ef50c43a07b816e06799bb12d" +dependencies = [ + "const-oid 0.9.6", + "digest 0.10.7", + "num-bigint-dig", + "num-integer", + "num-traits", + "pkcs1", + "pkcs8", + "rand_core 0.6.4", + "sha2 0.10.9", + "signature", + "spki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "1.1.6" @@ -459,6 +932,26 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct", + "der", + "generic-array", + "pkcs8", + "subtle", + "zeroize", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + [[package]] name = "serde" version = "1.0.229" @@ -509,8 +1002,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", ] [[package]] @@ -520,10 +1024,91 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.3.1", + "digest 0.11.3", ] +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest 0.10.7", + "rand_core 0.6.4", +] + +[[package]] +name = "smallvec" +version = "1.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" + +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "ssh-cipher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caac132742f0d33c3af65bfcde7f6aa8f62f0e991d80db99149eb9d44708784f" +dependencies = [ + "cipher 0.4.4", + "ssh-encoding", +] + +[[package]] +name = "ssh-encoding" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9242b9ef4108a78e8cd1a2c98e193ef372437f8c22be363075233321dd4a15" +dependencies = [ + "base64ct", + "pem-rfc7468", + "sha2 0.10.9", +] + +[[package]] +name = "ssh-key" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b86f5297f0f04d08cabaa0f6bff7cb6aec4d9c3b49d87990d63da9d9156a8c3" +dependencies = [ + "ed25519-dalek", + "num-bigint-dig", + "p256", + "p384", + "p521", + "rand_core 0.6.4", + "rsa", + "sec1", + "sha2 0.10.9", + "signature", + "ssh-cipher", + "ssh-encoding", + "subtle", + "zeroize", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "2.0.119" @@ -553,7 +1138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom", + "getrandom 0.4.3", "once_cell", "rustix", "windows-sys", @@ -612,10 +1197,22 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96" dependencies = [ - "crypto-common", + "crypto-common 0.2.2", "ctutils", ] +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "windows-link" version = "0.2.1" @@ -631,6 +1228,26 @@ dependencies = [ "windows-link", ] +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "zeroize" version = "1.9.1" diff --git a/Cargo.toml b/Cargo.toml index d3bb352..7518b53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/*"] [workspace.package] -version = "0.3.0" +version = "0.4.0" edition = "2024" license = "MPL-2.0" repository = "https://gitea.marek-novak.cz/mnovak/CustosPublic" diff --git a/crates/custos-gen/Cargo.toml b/crates/custos-gen/Cargo.toml index 72bab71..7e47695 100644 --- a/crates/custos-gen/Cargo.toml +++ b/crates/custos-gen/Cargo.toml @@ -11,6 +11,7 @@ custos-vault = { path = "../custos-vault" } rand.workspace = true zeroize.workspace = true unicode-normalization.workspace = true +data-encoding.workspace = true [dev-dependencies] regex = "1" diff --git a/crates/custos-gen/src/lib.rs b/crates/custos-gen/src/lib.rs index 9a2148a..a3938c7 100644 --- a/crates/custos-gen/src/lib.rs +++ b/crates/custos-gen/src/lib.rs @@ -514,3 +514,83 @@ pub fn pin(length: u8) -> Generated { let value: String = (0..len).map(|_| digit()).collect(); Generated { value: SecretString::new(value), entropy_bits: len as f64 * 10f64.log2() } } + +// ---------- keys and API keys (docs/KEYS.md) ---------- + +/// How a secret key is written out. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum KeyEncoding { + /// Lowercase hex. + Hex, + /// Base64url without padding. + Base64Url, + /// Standard base64 with padding. + Base64, +} + +/// `bytes` random bytes (16..=128), encoded: symmetric keys, HMAC and JWT secrets. +pub fn secret_key(bytes: usize, encoding: KeyEncoding) -> Result { + if !(16..=128).contains(&bytes) { + return Err(GenError("a secret key is 16 to 128 bytes".into())); + } + let mut raw = zeroize::Zeroizing::new(vec![0u8; bytes]); + rand::Rng::fill_bytes(&mut rng(), &mut raw); + let value = match encoding { + KeyEncoding::Hex => data_encoding::HEXLOWER.encode(&raw), + KeyEncoding::Base64Url => data_encoding::BASE64URL_NOPAD.encode(&raw), + KeyEncoding::Base64 => data_encoding::BASE64.encode(&raw), + }; + Ok(Generated { value: SecretString::new(value), entropy_bits: 8.0 * bytes as f64 }) +} + +const BASE62: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +const API_KEY_RANDOM: usize = 32; + +/// CRC-32 (IEEE 802.3, reflected, as zlib computes it): the API key checksum. Not a security feature. +fn crc32(data: &[u8]) -> u32 { + let mut crc = !0u32; + for &b in data { + crc ^= b as u32; + for _ in 0..8 { + crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 }; + } + } + !crc +} + +fn checksum6(body: &str) -> String { + let mut n = crc32(body.as_bytes()) as u64; + let mut out = [b'0'; 6]; + for c in out.iter_mut().rev() { + *c = BASE62[(n % 62) as usize]; + n /= 62; + } + String::from_utf8(out.to_vec()).expect("base62 is ASCII") +} + +/// `_` + 32 uniform base62 characters (190.5 bits) + a 6-character CRC-32 checksum. +/// The prefix is empty, or a lowercase letter and up to 15 lowercase letters or digits. +pub fn api_key(prefix: &str) -> Result { + let ok = prefix.is_empty() + || (prefix.len() <= 16 + && prefix.starts_with(|c: char| c.is_ascii_lowercase()) + && prefix.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())); + if !ok { + return Err(GenError("the prefix is a lowercase letter and up to 15 lowercase letters or digits".into())); + } + let mut body = if prefix.is_empty() { String::new() } else { format!("{prefix}_") }; + for _ in 0..API_KEY_RANDOM { + body.push(BASE62[rng().random_range(0..62)] as char); + } + let sum = checksum6(&body); + body.push_str(&sum); + Ok(Generated { value: SecretString::new(body), entropy_bits: API_KEY_RANDOM as f64 * 62f64.log2() }) +} + +/// Whether an API key's last 6 characters are the checksum of the rest (a typo, or not one of ours). +pub fn api_key_checksum_ok(key: &str) -> bool { + key.len() > 6 && key.is_char_boundary(key.len() - 6) && { + let (body, sum) = key.split_at(key.len() - 6); + checksum6(body) == sum + } +} diff --git a/crates/custos-keys/Cargo.toml b/crates/custos-keys/Cargo.toml new file mode 100644 index 0000000..f593dae --- /dev/null +++ b/crates/custos-keys/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "custos-keys" +description = "Custos key pairs: Ed25519, ECDSA P-256 and RSA, as OpenSSH and PKCS#8 PEM (docs/KEYS.md)" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +custos-vault = { path = "../custos-vault" } +thiserror.workspace = true +# RustCrypto's SSH key formats and the key types they build on (rand_core 0.6 generation API). +ssh-key = { version = "0.6", default-features = false, features = ["std", "ed25519", "p256", "rsa", "rand_core"] } +ed25519-dalek = { version = "2", features = ["pkcs8", "pem", "rand_core"] } +p256 = { version = "0.13", features = ["ecdsa", "pkcs8", "pem"] } +rsa = { version = "0.9", features = ["pem"] } +rand_core = { version = "0.6", features = ["getrandom"] } diff --git a/crates/custos-keys/src/lib.rs b/crates/custos-keys/src/lib.rs new file mode 100644 index 0000000..29666ac --- /dev/null +++ b/crates/custos-keys/src/lib.rs @@ -0,0 +1,142 @@ +//! Key pairs for the `key-pair` item type (docs/KEYS.md): Ed25519, ECDSA P-256 and RSA, written as an +//! OpenSSH public line, SPKI PEM, PKCS#8 PEM and an OpenSSH private key. RustCrypto crates only; the +//! randomness is the OS CSPRNG (`rand_core::OsRng`). + +use custos_vault::SecretString; +use ed25519_dalek::pkcs8::spki::der::pem::LineEnding; +use rand_core::OsRng; +use ssh_key::private::{EcdsaKeypair, Ed25519Keypair, KeypairData, RsaKeypair}; +use ssh_key::{HashAlg, PrivateKey}; + +#[derive(Debug, thiserror::Error)] +pub enum KeyError { + #[error("the key could not be generated or encoded")] + Encode, + #[error("not a valid key of this algorithm")] + Invalid, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Algorithm { + Ed25519, + EcdsaP256, + Rsa3072, + Rsa4096, +} + +impl Algorithm { + pub const ALL: [Algorithm; 4] = [Algorithm::Ed25519, Algorithm::EcdsaP256, Algorithm::Rsa3072, Algorithm::Rsa4096]; + + /// The value of the item's `algorithm` field. + pub fn name(self) -> &'static str { + match self { + Algorithm::Ed25519 => "ed25519", + Algorithm::EcdsaP256 => "ecdsa-p256", + Algorithm::Rsa3072 => "rsa-3072", + Algorithm::Rsa4096 => "rsa-4096", + } + } + + pub fn from_name(s: &str) -> Option { + Algorithm::ALL.into_iter().find(|a| a.name() == s) + } +} + +/// A generated key pair, ready to become a `key-pair` item. `Debug` never shows the private parts. +#[derive(Clone, Debug)] +pub struct KeyPair { + pub algorithm: String, + /// `ssh-ed25519 AAAA… comment`. + pub public_openssh: String, + /// SubjectPublicKeyInfo PEM. + pub public_pem: String, + /// PKCS#8 PEM, unencrypted (the vault is its encryption). + pub private_pem: SecretString, + /// OpenSSH private key, unencrypted. + pub private_openssh: SecretString, + /// `SHA256:…`, as `ssh-keygen -l` prints it. + pub fingerprint: String, +} + +fn enc(r: Result) -> Result { + r.map_err(|_| KeyError::Encode) +} + +/// A new key pair. RSA takes a few seconds (prime generation). +pub fn generate(alg: Algorithm, comment: &str) -> Result { + let (data, private_pem, public_pem) = match alg { + Algorithm::Ed25519 => { + use ed25519_dalek::pkcs8::{EncodePrivateKey, EncodePublicKey}; + let sk = ed25519_dalek::SigningKey::generate(&mut OsRng); + let private_pem = enc(sk.to_pkcs8_pem(LineEnding::LF))?.to_string(); + let public_pem = enc(sk.verifying_key().to_public_key_pem(LineEnding::LF))?; + (KeypairData::Ed25519(Ed25519Keypair::from(&sk)), private_pem, public_pem) + } + Algorithm::EcdsaP256 => { + use p256::pkcs8::{EncodePrivateKey, EncodePublicKey}; + let sk = p256::SecretKey::random(&mut OsRng); + let private_pem = enc(sk.to_pkcs8_pem(LineEnding::LF))?.to_string(); + let public_pem = enc(sk.public_key().to_public_key_pem(LineEnding::LF))?; + let public = sk.public_key(); + (KeypairData::Ecdsa(EcdsaKeypair::NistP256 { private: sk.into(), public: public.into() }), private_pem, public_pem) + } + Algorithm::Rsa3072 | Algorithm::Rsa4096 => { + use rsa::pkcs8::{EncodePrivateKey, EncodePublicKey}; + let bits = if alg == Algorithm::Rsa3072 { 3072 } else { 4096 }; + let sk = enc(rsa::RsaPrivateKey::new(&mut OsRng, bits))?; + let private_pem = enc(sk.to_pkcs8_pem(LineEnding::LF))?.to_string(); + let public_pem = enc(sk.to_public_key().to_public_key_pem(LineEnding::LF))?; + (KeypairData::Rsa(enc(RsaKeypair::try_from(sk))?), private_pem, public_pem) + } + }; + let private = enc(PrivateKey::new(data, comment))?; + let public = private.public_key(); + Ok(KeyPair { + algorithm: alg.name().into(), + public_openssh: enc(public.to_openssh())?, + public_pem, + private_pem: SecretString::new(private_pem), + private_openssh: SecretString::new(enc(private.to_openssh(ssh_key::LineEnding::LF))?.to_string()), + fingerprint: public.fingerprint(HashAlg::Sha256).to_string(), + }) +} + +/// Whether a PKCS#8 private key and an SPKI public key belong together (tests, and a check before +/// importing a key pair someone pasted). +pub fn pem_matches(alg: Algorithm, private_pem: &str, public_pem: &str) -> Result { + let derived = match alg { + Algorithm::Ed25519 => { + use ed25519_dalek::pkcs8::{DecodePrivateKey, EncodePublicKey}; + let sk = ed25519_dalek::SigningKey::from_pkcs8_pem(private_pem).map_err(|_| KeyError::Invalid)?; + enc(sk.verifying_key().to_public_key_pem(LineEnding::LF))? + } + Algorithm::EcdsaP256 => { + use p256::pkcs8::{DecodePrivateKey, EncodePublicKey}; + let sk = p256::SecretKey::from_pkcs8_pem(private_pem).map_err(|_| KeyError::Invalid)?; + enc(sk.public_key().to_public_key_pem(LineEnding::LF))? + } + Algorithm::Rsa3072 | Algorithm::Rsa4096 => { + use rsa::pkcs8::{DecodePrivateKey, EncodePublicKey}; + let sk = rsa::RsaPrivateKey::from_pkcs8_pem(private_pem).map_err(|_| KeyError::Invalid)?; + enc(sk.to_public_key().to_public_key_pem(LineEnding::LF))? + } + }; + Ok(derived.trim() == public_pem.trim()) +} + +/// An Ed25519 signature (64 bytes) of `message` with a PKCS#8 private key. +pub fn sign_ed25519(private_pem: &str, message: &[u8]) -> Result, KeyError> { + use ed25519_dalek::Signer; + use ed25519_dalek::pkcs8::DecodePrivateKey; + let sk = ed25519_dalek::SigningKey::from_pkcs8_pem(private_pem).map_err(|_| KeyError::Invalid)?; + Ok(sk.sign(message).to_bytes().to_vec()) +} + +/// Whether `signature` is a valid Ed25519 signature of `message` under an SPKI public key. +pub fn verify_ed25519(public_pem: &str, message: &[u8], signature: &[u8]) -> Result { + use ed25519_dalek::Verifier; + use ed25519_dalek::pkcs8::DecodePublicKey; + let vk = ed25519_dalek::VerifyingKey::from_public_key_pem(public_pem).map_err(|_| KeyError::Invalid)?; + let sig = ed25519_dalek::Signature::from_slice(signature).map_err(|_| KeyError::Invalid)?; + Ok(vk.verify(message, &sig).is_ok()) +} diff --git a/docs/THREAT_MODEL.md b/docs/THREAT_MODEL.md index a1529af..3699ad7 100644 --- a/docs/THREAT_MODEL.md +++ b/docs/THREAT_MODEL.md @@ -14,6 +14,7 @@ just as plainly, what it does not. | Attacker | Has | Gets | Why | |:--|:--|:--|:--| | Thief or sync server | The `.custos` file (any number of old versions) | Item count and sizes roughly (the ciphertext length), the KDF parameters. Nothing else. | XChaCha20-Poly1305 over the whole body; titles, URLs and usernames are inside it too. | +| Thief with one factor (v2) | The file plus the master password, or a copied key file, or a lost phone | Nothing | A version-2 vault opens only with two different factors (docs/FACTORS.md); one factor unwraps one factor key, never the vault key. | | Offline guesser | The file and a GPU farm | One Argon2id evaluation (64 MiB, 20 passes by default, ≈ 0.75 s on a desktop CPU) per guess | A 4-word memorable password with digits (~65 bits) is out of reach; a weak master password is not. **The master password is the whole defence here.** | | Tamperer | Write access to the file | Can destroy it, or roll it back to an older version they saw. Cannot change, reorder, add or remove items unnoticed. | The header is the body's AAD; each slot's parameters are its wrap's AAD. | | Share server operator | Every share blob, its id, size, expiry, view count, the uploader's login and the recipients' IP addresses | Nothing of the secret | The key is in the link's `#fragment`, which browsers never send; the server stores ciphertext only (docs/SHARE.md). | @@ -34,7 +35,11 @@ just as plainly, what it does not. desktop app), and by any clipboard manager that ignores the "exclude from history" marker. - **Auto-type** sends keystrokes to whatever window has focus. The app re-checks the target window before typing, but a window that steals focus at the wrong moment can receive them. -- **Device unlock (Windows Hello).** The blob on disk is only as strong as the device key behind it: anyone +- **A device factor (v2) on its own device.** The key file is bound to one Windows account by DPAPI; malware + running as that account can use it. That is one factor, and a v2 vault never opens with one. +- **The machine store** (the Claude Code store) opens with its device key alone, by design: anything running as + the user can open it. It must hold only what that program generated, never the user's own secrets. +- **Device unlock (Windows Hello, version 1 only).** The blob on disk is only as strong as the device key behind it: anyone who can make Windows Hello sign as you on that machine (your PIN, your face, or malware running while you approve a prompt) can unlock the vault there. It does not help an attacker who has only the vault file. - **Rollback.** Someone with write access to the synced file can replace it with an older version.