Compare commits
7 Commits
9b4d2c40d3
...
853f59ca60
| Author | SHA1 | Date | |
|---|---|---|---|
| 853f59ca60 | |||
| e0c6d53a99 | |||
| 91b84459b0 | |||
| 6233c8244e | |||
| 89b5329f06 | |||
| c7d050ed65 | |||
| ed9b8a3b17 |
Generated
+531
@@ -0,0 +1,531 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aead"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-common",
|
||||||
|
"inout",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arcanum"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"arcanum",
|
||||||
|
"chacha20",
|
||||||
|
"chacha20poly1305",
|
||||||
|
"hex",
|
||||||
|
"hkdf",
|
||||||
|
"hmac",
|
||||||
|
"ml-kem",
|
||||||
|
"rand_core",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"sha2",
|
||||||
|
"sha3",
|
||||||
|
"shake",
|
||||||
|
"x-wing",
|
||||||
|
"x25519-dalek",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "block-buffer"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chacha20"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cipher",
|
||||||
|
"cpufeatures",
|
||||||
|
"rand_core",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chacha20poly1305"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b89e1c441e926b9c82a8d023f6e1b7ae0adcfaa7d621814e4d60789bac751cb"
|
||||||
|
dependencies = [
|
||||||
|
"aead",
|
||||||
|
"chacha20",
|
||||||
|
"cipher",
|
||||||
|
"poly1305",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[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",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cmov"
|
||||||
|
version = "0.5.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "const-oid"
|
||||||
|
version = "0.10.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crypto-common"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ctutils"
|
||||||
|
version = "0.4.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f"
|
||||||
|
dependencies = [
|
||||||
|
"cmov",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "curve25519-dalek"
|
||||||
|
version = "5.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5eed333089e2e1c1ac8c6c0398e5e2497b4c9926ca6d0365ed1e099afa5bc23"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"curve25519-dalek-derive",
|
||||||
|
"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 = "digest"
|
||||||
|
version = "0.11.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
|
||||||
|
dependencies = [
|
||||||
|
"block-buffer",
|
||||||
|
"const-oid",
|
||||||
|
"crypto-common",
|
||||||
|
"ctutils",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fiat-crypto"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hex"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hkdf"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018"
|
||||||
|
dependencies = [
|
||||||
|
"hmac",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hmac"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hybrid-array"
|
||||||
|
version = "0.4.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17"
|
||||||
|
dependencies = [
|
||||||
|
"ctutils",
|
||||||
|
"typenum",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inout"
|
||||||
|
version = "0.2.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "keccak"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kem"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "01737161ba802849cfd486b5bd209d38ba4943494c249a8126005170c7621edd"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-common",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.191"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ml-kem"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "84ca78b66f46050d49fa21693ae8adab2081d8101b65d8067316b6b70b64a0d1"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
"kem",
|
||||||
|
"module-lattice",
|
||||||
|
"rand_core",
|
||||||
|
"sha3",
|
||||||
|
"shake",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "module-lattice"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4a1e412c8ee83a1c678f427806219c49c96b2511b9f07497514f0e43fb26155"
|
||||||
|
dependencies = [
|
||||||
|
"ctutils",
|
||||||
|
"hybrid-array",
|
||||||
|
"num-traits",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "poly1305"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c"
|
||||||
|
dependencies = [
|
||||||
|
"cpufeatures",
|
||||||
|
"universal-hash",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.107"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc_version"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
|
||||||
|
dependencies = [
|
||||||
|
"semver",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[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"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 3.0.10",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.154"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
|
"zmij",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha3"
|
||||||
|
version = "0.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
"keccak",
|
||||||
|
"sponge-cursor",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shake"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09057cb2149ad4cbd2da1e26b351f9a4c354219421229c69c3063e6f61947c4a"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
"keccak",
|
||||||
|
"sponge-cursor",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sponge-cursor"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620"
|
||||||
|
dependencies = [
|
||||||
|
"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"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "3.0.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.20.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "universal-hash"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-common",
|
||||||
|
"ctutils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x-wing"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "01576e56599d01d75c5efcd5b30fa5de29e776f5a225e43fbf8c92c7302d4c8b"
|
||||||
|
dependencies = [
|
||||||
|
"kem",
|
||||||
|
"ml-kem",
|
||||||
|
"sha3",
|
||||||
|
"shake",
|
||||||
|
"x25519-dalek",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x25519-dalek"
|
||||||
|
version = "3.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6"
|
||||||
|
dependencies = [
|
||||||
|
"curve25519-dalek",
|
||||||
|
"rand_core",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zeroize"
|
||||||
|
version = "1.9.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85ccff8a4805aba1eaa4cd4afcd49f7557dd2cf5fd79ffc37f8959466effc5aa"
|
||||||
|
dependencies = [
|
||||||
|
"zeroize_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zeroize_derive"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.119",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zmij"
|
||||||
|
version = "1.0.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
[package]
|
||||||
|
name = "arcanum"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
rust-version = "1.88"
|
||||||
|
license = "CPAL-1.0"
|
||||||
|
description = "Hybrid post-quantum key exchange (X-Wing: ML-KEM-768 + X25519), key schedule, WireGuard PSK derivation and sealing for the Domi ecosystem"
|
||||||
|
repository = "https://gitea.marek-novak.cz/mnovak/arcanum"
|
||||||
|
readme = "README.md"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
# Primitives: every one from a vetted crate, none written here (docs/01_arcanum.md).
|
||||||
|
ml-kem = { version = "=0.3.4", default-features = false, features = ["zeroize"] }
|
||||||
|
x25519-dalek = { version = "=3.0.0", default-features = false, features = ["static_secrets", "zeroize"] }
|
||||||
|
sha3 = { version = "0.12", default-features = false, features = ["zeroize"] }
|
||||||
|
shake = { version = "0.1", default-features = false, features = ["zeroize"] }
|
||||||
|
sha2 = { version = "0.11", default-features = false, features = ["zeroize"] }
|
||||||
|
hkdf = { version = "0.13", default-features = false }
|
||||||
|
hmac = { version = "0.13", default-features = false, features = ["zeroize"] }
|
||||||
|
chacha20poly1305 = { version = "0.11", default-features = false, features = ["alloc", "zeroize"] }
|
||||||
|
rand_core = { version = "0.10", default-features = false }
|
||||||
|
zeroize = { version = "1.8", default-features = false, features = ["alloc", "zeroize_derive"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
# Deterministic entry points (`_derand`, `from_seed`) for the known-answer tests. Off by default;
|
||||||
|
# never enable it in a release build (tests/feature_hygiene.rs checks the normal dependency graph).
|
||||||
|
hazmat = []
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
# The tests enable `hazmat` on the crate itself; dev-dependencies never reach a normal build.
|
||||||
|
arcanum = { path = ".", features = ["hazmat"] }
|
||||||
|
# Reference implementation for the differential test only; never linked into the library.
|
||||||
|
x-wing = { version = "=0.1.1" }
|
||||||
|
chacha20 = { version = "0.10", default-features = false, features = ["rng"] }
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
hex = "0.4"
|
||||||
|
sha2 = "0.11"
|
||||||
|
|
||||||
|
# ML-KEM in a debug build is slow; the byte-flip tests run thousands of exchanges.
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = 2
|
||||||
|
[profile.test.package."*"]
|
||||||
|
opt-level = 2
|
||||||
@@ -12,6 +12,24 @@ sealing and Tabellarius are expected to adopt it later.
|
|||||||
- No I/O, no network, no clock. Randomness is passed in by the caller.
|
- No I/O, no network, no clock. Randomness is passed in by the caller.
|
||||||
- No `unsafe` code.
|
- No `unsafe` code.
|
||||||
|
|
||||||
|
## Use
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let init = arcanum::exchange::initiate(&my_secret, &peer_public, context, &mut rng)?;
|
||||||
|
// send init.ciphertext() to the peer, which calls exchange::respond and sends back ct_ri
|
||||||
|
let session = init.finish(&ct_ri)?;
|
||||||
|
let psk = session.wireguard_psk(arcanum::epoch_at(unix_seconds)); // one exchange per 120 s epoch
|
||||||
|
```
|
||||||
|
|
||||||
|
## Checks
|
||||||
|
|
||||||
|
```
|
||||||
|
cargo test # KATs, binding, epochs, malformed input, golden vectors, feature graph
|
||||||
|
cargo deny check
|
||||||
|
cargo audit
|
||||||
|
cargo +nightly fuzz run --release untrusted_input -- -max_total_time=600
|
||||||
|
```
|
||||||
|
|
||||||
**Not audited.** Nothing here is called secure before an external audit.
|
**Not audited.** Nothing here is called secure before an external audit.
|
||||||
|
|
||||||
The design, the risks and the threat model are in `docs/`.
|
The design, the risks and the threat model are in `docs/`.
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# cargo deny configuration for Arcanum (mandatory test 6). Run: cargo deny check
|
||||||
|
|
||||||
|
[graph]
|
||||||
|
all-features = true
|
||||||
|
|
||||||
|
[advisories]
|
||||||
|
# Every RustSec advisory, including unmaintained and unsound crates, is an error.
|
||||||
|
version = 2
|
||||||
|
yanked = "deny"
|
||||||
|
ignore = []
|
||||||
|
|
||||||
|
[licenses]
|
||||||
|
version = 2
|
||||||
|
# Licences of the dependency tree; each was checked against the crates' own LICENSE files.
|
||||||
|
allow = [
|
||||||
|
"Apache-2.0",
|
||||||
|
"MIT",
|
||||||
|
"BSD-3-Clause",
|
||||||
|
"Unicode-3.0",
|
||||||
|
]
|
||||||
|
confidence-threshold = 0.9
|
||||||
|
|
||||||
|
|
||||||
|
[licenses.private]
|
||||||
|
ignore = true
|
||||||
|
|
||||||
|
[bans]
|
||||||
|
multiple-versions = "deny"
|
||||||
|
wildcards = "deny"
|
||||||
|
highlight = "all"
|
||||||
|
|
||||||
|
[sources]
|
||||||
|
unknown-registry = "deny"
|
||||||
|
unknown-git = "deny"
|
||||||
|
allow-registry = ["https://github.com/rust-lang/crates.io-index"]
|
||||||
|
allow-git = []
|
||||||
@@ -0,0 +1,265 @@
|
|||||||
|
# Day 1 — Arcanum
|
||||||
|
|
||||||
|
Status: v0.1.0 candidate, **not audited**. Nothing here is called secure before an external audit.
|
||||||
|
|
||||||
|
## Why
|
||||||
|
|
||||||
|
Every Domi product that moves data between devices (Via first, then Canalis, Custos sealing and
|
||||||
|
Tabellarius) needs a key exchange that survives a future quantum computer. Traffic recorded today
|
||||||
|
can be decrypted later ("harvest now, decrypt later"), so the protection has to be in place before
|
||||||
|
such a computer exists. Arcanum is the one small, reviewable crate that provides it, so the
|
||||||
|
ecosystem audits one piece of code instead of five.
|
||||||
|
|
||||||
|
## What
|
||||||
|
|
||||||
|
| Piece | What it does | Standard |
|
||||||
|
|:--|:--|:--|
|
||||||
|
| `xwing` | Hybrid KEM: ML-KEM-768 + X25519, X-Wing exactly as drafted | draft-connolly-cfrg-xwing-kem-11, FIPS 203, RFC 7748 |
|
||||||
|
| `exchange` | A **mutual** exchange: each peer encapsulates to the other's public key; both get a `Session` bound to both secrets, the full transcript and a caller context | HKDF (RFC 5869) with SHA-256 |
|
||||||
|
| `Session::wireguard_psk(epoch)` | The 32-byte WireGuard pre-shared key for one rotation epoch | HKDF-Expand |
|
||||||
|
| `seal` / `open` | One-shot sealed box to a recipient's hybrid public key | X-Wing + HKDF + ChaCha20-Poly1305 (RFC 8439) |
|
||||||
|
|
||||||
|
Not in scope for Day 1: networking, WireGuard itself, signatures (ML-DSA comes later), migrating
|
||||||
|
other Domi apps.
|
||||||
|
|
||||||
|
## How
|
||||||
|
|
||||||
|
### Primitives (all from vetted crates, none written here)
|
||||||
|
|
||||||
|
| Primitive | Crate | Version locked | Notes |
|
||||||
|
|:--|:--|:--|:--|
|
||||||
|
| ML-KEM-768 | `ml-kem` (RustCrypto) | 0.3.4 | FIPS 203 final. README: "never been independently audited". |
|
||||||
|
| X25519 | `x25519-dalek` | 3.0.0 | with `curve25519-dalek` 5.0.0 |
|
||||||
|
| SHA3-256 | `sha3` (RustCrypto) | 0.12.0 | X-Wing combiner |
|
||||||
|
| SHAKE256 | `shake` (RustCrypto) | 0.1.0 | X-Wing key expansion |
|
||||||
|
| HKDF-SHA-256 | `hkdf` 0.13.0, `hmac` 0.13.0, `sha2` 0.11.1 | | key schedule |
|
||||||
|
| ChaCha20-Poly1305 | `chacha20poly1305` (RustCrypto) | 0.11.0 | sealing |
|
||||||
|
| Zeroisation | `zeroize` | 1.9.2 | every secret type is `ZeroizeOnDrop` |
|
||||||
|
|
||||||
|
Exact versions are pinned by `Cargo.lock`, which is committed. `ml-kem` and `x25519-dalek` are
|
||||||
|
pinned with `=` in `Cargo.toml` as well, so an upgrade is a deliberate edit that re-runs the KATs.
|
||||||
|
|
||||||
|
### The hybrid KEM: X-Wing, draft -11 (decision D-QA-1)
|
||||||
|
|
||||||
|
Facts checked on 2026-11-25:
|
||||||
|
|
||||||
|
- The current draft is **draft-connolly-cfrg-xwing-kem-11**, dated 2026-09-23, an individual
|
||||||
|
Internet-Draft (intended status Informational), not an RFC and not CFRG-adopted.
|
||||||
|
Source: <https://datatracker.ietf.org/doc/draft-connolly-cfrg-xwing-kem/>.
|
||||||
|
- Its change log lists only editorial changes since -06 (ASN.1 module, SHAKE output length stated
|
||||||
|
in bits, implementation list, PEM header). Source: Appendix G of
|
||||||
|
<https://www.ietf.org/archive/id/draft-connolly-cfrg-xwing-kem-11.txt>.
|
||||||
|
- Appendix C has three vectors and still carries the note "TODO: replace with test vectors that
|
||||||
|
re-use ML-KEM, X25519 values", so **later drafts may change the vectors**. Arcanum pins the vector
|
||||||
|
file by sha256 together with the draft number (`tests/vectors/SOURCES.md`); a changed file fails
|
||||||
|
the KAT, and moving to a new draft is a deliberate decision.
|
||||||
|
- RustCrypto's `x-wing` 0.1.1 (2026-10-01) describes itself as draft 06; its vectors are the same
|
||||||
|
three as draft -11 (identical after removing whitespace).
|
||||||
|
|
||||||
|
**Decision D-QA-1 (recommended by QA, adopted).** Mark's spec asks for a combiner that hashes both
|
||||||
|
shared secrets, both ciphertexts and both public keys, "X-Wing-style … use it if it fits". X-Wing's
|
||||||
|
combiner hashes `ss_M`, `ss_X`, `ct_X` and `pk_X` and deliberately leaves `ct_M` and `pk_M` to
|
||||||
|
ML-KEM's own binding (draft -11 §5.3). So:
|
||||||
|
|
||||||
|
1. **The KEM is X-Wing, unmodified**, so the draft's vectors pass byte for byte:
|
||||||
|
```
|
||||||
|
expand(sk) = SHAKE256(sk, 96 bytes) -> d = [0:32], z = [32:64], sk_X = [64:96]
|
||||||
|
(pk_M, dk_M) = ML-KEM-768.KeyGen_internal(d, z); pk_X = X25519(sk_X, 9)
|
||||||
|
Encaps(pk, eseed) : (ct_M, ss_M) = ML-KEM-768.Encaps_internal(pk_M, eseed[0:32])
|
||||||
|
ek_X = eseed[32:64]; ct_X = X25519(ek_X, 9); ss_X = X25519(ek_X, pk_X)
|
||||||
|
Combiner = SHA3-256(ss_M || ss_X || ct_X || pk_X || "\.//^\")
|
||||||
|
```
|
||||||
|
2. **Both public keys and both ciphertexts are bound in Arcanum's own key schedule**, where the
|
||||||
|
`domi/arcanum/v1/...` labels live (below). That meets the spec in full, and the spec's own
|
||||||
|
"use it if it fits" condition holds.
|
||||||
|
|
||||||
|
Arcanum composes X-Wing from `ml-kem`, `x25519-dalek`, `sha3` and `shake` rather than calling the
|
||||||
|
`x-wing` crate, because mandatory test 3 needs the combiner as a separate function and because
|
||||||
|
Arcanum wants typed errors for every malformed input. The composition is about twenty lines of
|
||||||
|
glue between vetted primitives. It is pinned by the draft's three vectors **and** by a differential
|
||||||
|
test against RustCrypto `x-wing` (a dev-dependency only) over random seeds and random ciphertexts.
|
||||||
|
|
||||||
|
**Stricter than the draft, on purpose.** Arcanum rejects an X25519 public key or ciphertext part
|
||||||
|
that is a low-order point (`was_contributory() == false`, a constant-time check in `x25519-dalek`).
|
||||||
|
An honest key or ciphertext is never such a point, so this changes no honest exchange and no
|
||||||
|
official vector; it removes the case where the classical half silently contributes a known value
|
||||||
|
(QA A-16, A-17). RustCrypto ships the same option as `DecapsulationKeyRejectNonContrib`.
|
||||||
|
|
||||||
|
**ML-KEM key check.** The draft requires the FIPS 203 §7.2 encapsulation key check.
|
||||||
|
`ml-kem`'s `EncapsulationKey::new` performs it; Arcanum maps its failure to
|
||||||
|
`Error::InvalidPublicKey`, and the NIST ACVP `encapsulationKeyCheck` vectors check that.
|
||||||
|
|
||||||
|
**X25519 clamping.** Five bits of the 32-byte X25519 ephemeral scalar (bits 0–2 of the first byte,
|
||||||
|
bits 6–7 of the last) are clamped by RFC 7748, so changing only those bits of `eseed[32:64]` does
|
||||||
|
not change the exchange. That is the standard's behaviour, not a binding gap; the byte-flip test
|
||||||
|
flips bit 4 of each byte.
|
||||||
|
|
||||||
|
### Is `ml-kem` stable enough?
|
||||||
|
|
||||||
|
- `ml-kem` 0.3.4 (2026-10-31) is the newest release; the 0.3 line has been out since 2026-04-28
|
||||||
|
(0.3.0, 0.3.2, 0.3.3, 0.3.4). Source: <https://crates.io/api/v1/crates/ml-kem>. It depends on
|
||||||
|
`rand_core` 0.10 (read from its `Cargo.toml`).
|
||||||
|
- It is still 0.x, so a minor bump may break the API. Its README says it implements FIPS 203
|
||||||
|
(final) and that it **"has never been independently audited"**. Source:
|
||||||
|
<https://github.com/RustCrypto/KEMs/blob/master/ml-kem/README.md>.
|
||||||
|
- **Correction to a QA fact (F10):** in 0.3.4, `EncapsulationKey::encapsulate_deterministic` is
|
||||||
|
public **whether or not** the `hazmat` feature is on; `hazmat` only un-hides it in the docs
|
||||||
|
(`#[cfg_attr(not(feature = "hazmat"), doc(hidden))]`, `src/encapsulation_key.rs`). So `ml-kem`'s
|
||||||
|
feature gives no protection; Arcanum's own `hazmat` feature (below) is what gates the
|
||||||
|
deterministic paths for Domi code.
|
||||||
|
- Judgement: stable enough for X-Wing in a household mesh where WireGuard's classical security
|
||||||
|
remains underneath (the PSK can only add security, see THREAT_MODEL.md), **not** stable enough to
|
||||||
|
be the only protection of anything. The version is pinned; an upgrade re-runs the KATs.
|
||||||
|
|
||||||
|
### `x25519-dalek`
|
||||||
|
|
||||||
|
`x25519-dalek` 3.0.0 (2026-07-06), the newest stable release, on `curve25519-dalek` 5.0.0, with
|
||||||
|
`SharedSecret::was_contributory()`. Source: <https://crates.io/api/v1/crates/x25519-dalek> and the
|
||||||
|
crate source. Used with `static_secrets` and `zeroize`.
|
||||||
|
|
||||||
|
### The exchange is mutual (decision on QA A-15)
|
||||||
|
|
||||||
|
A PSK from a **single** encapsulation authenticates only the receiver: anyone who knows the
|
||||||
|
responder's public key can encapsulate to it. Today WireGuard's static X25519 keys authenticate
|
||||||
|
the peers, but a quantum attacker can recover those from the public keys, and then a one-way PSK
|
||||||
|
is the only thing left — and the attacker could impersonate the initiator and choose it.
|
||||||
|
|
||||||
|
**Chosen: both directions.** The initiator encapsulates to the responder (`ct_IR`), the responder
|
||||||
|
encapsulates back to the initiator (`ct_RI`), and the session is derived from both secrets and the
|
||||||
|
whole transcript. Whoever lacks either secret key cannot compute the PSK. This is the design
|
||||||
|
Rosenpass uses for the same reason. API:
|
||||||
|
|
||||||
|
```
|
||||||
|
let init = exchange::initiate(&my_sk, &peer_pk, context, rng)?; // send init.ciphertext()
|
||||||
|
let (ct_ri, session) = exchange::respond(&my_sk, &peer_pk, &ct_ir, context, rng)?; // send ct_ri
|
||||||
|
let session = init.finish(&ct_ri)?;
|
||||||
|
```
|
||||||
|
|
||||||
|
What Day 3 must still do (recorded here and in THREAT_MODEL.md):
|
||||||
|
|
||||||
|
- **Authenticate the public keys.** Mutual KEM authentication is only as good as each side's copy
|
||||||
|
of the other's hybrid public key. Via distributes them signed with Tessera. If Tessera's
|
||||||
|
signatures are classical (Ed25519), a quantum attacker who can forge them can substitute keys.
|
||||||
|
That is the gap ML-DSA closes later.
|
||||||
|
- **Forward secrecy against key theft.** With static key pairs, someone who later steals both
|
||||||
|
secret keys and has recorded the exchanges can recompute every PSK. Day 3 should add an
|
||||||
|
ephemeral key pair per exchange (Rosenpass does), or rotate the key pairs.
|
||||||
|
- **One exchange per epoch**, every 120 s (below).
|
||||||
|
- **Replay and rollback protection** (QA A-13). Arcanum is pure and keeps no epoch state, so
|
||||||
|
refusing an old epoch or an old exchange is the caller's job. Arcanum does not wrap epochs: the
|
||||||
|
caller passes a `u64`, and `epoch_at` is a division (QA A-12 becomes a Day 3 test).
|
||||||
|
|
||||||
|
### Key schedule (labels `domi/arcanum/v1/...`)
|
||||||
|
|
||||||
|
```
|
||||||
|
exchange: th = SHA-256("domi/arcanum/v1/transcript/exchange"
|
||||||
|
|| pk_I (1216) || pk_R (1216) || ct_IR (1120) || ct_RI (1120)
|
||||||
|
|| u64be(len ctx) || ctx)
|
||||||
|
prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss_IR || ss_RI || th)
|
||||||
|
psk(epoch) = HKDF-Expand(prk, "domi/arcanum/v1/wireguard-psk" || u64be(epoch), 32)
|
||||||
|
|
||||||
|
seal: th = SHA-256("domi/arcanum/v1/transcript/seal" || pk (1216) || ct (1120)
|
||||||
|
|| u64be(len aad) || aad)
|
||||||
|
prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss || th)
|
||||||
|
key = HKDF-Expand(prk, "domi/arcanum/v1/seal/key", 32)
|
||||||
|
nonce = HKDF-Expand(prk, "domi/arcanum/v1/seal/nonce", 12)
|
||||||
|
```
|
||||||
|
|
||||||
|
- `pk_*` are whole hybrid public keys (`pk_M || pk_X`) and `ct_*` whole ciphertexts
|
||||||
|
(`ct_M || ct_X`), so both public keys and both ciphertexts of each direction are hashed.
|
||||||
|
- Roles are bound by position (initiator first), so a reflected exchange gives a different PSK
|
||||||
|
(QA A-14).
|
||||||
|
- Every field before the context is fixed-width and the context is length-prefixed, so the
|
||||||
|
encoding is injective (QA A-08). Labels are unique, versioned and prefix-free (QA A-07, a unit
|
||||||
|
test). The epoch is a full 64-bit big-endian value (QA A-11).
|
||||||
|
- `ctx` is the caller's binding (for Via: both Tessera device ids, the protocol name, the version).
|
||||||
|
- A v2 changes the labels and never reuses these. The v1 outputs are frozen by golden vectors
|
||||||
|
(`tests/golden_v1.rs`, QA A-10, A-38), so a "harmless" relabel fails loudly instead of making
|
||||||
|
every stored Custos blob unopenable.
|
||||||
|
|
||||||
|
### WireGuard PSK and rotation
|
||||||
|
|
||||||
|
- Rosenpass, the reference design, rotates every two minutes: `REKEY_AFTER_TIME_RESPONDER = 120 s`,
|
||||||
|
`REKEY_AFTER_TIME_INITIATOR = 130 s` (so the roles alternate), and `REJECT_AFTER_TIME = 180 s`.
|
||||||
|
Source: <https://github.com/rosenpass/rosenpass/blob/main/rosenpass/src/protocol/constants.rs>,
|
||||||
|
read 2026-11-25; the code comment cites WireGuard's own two-minute rekey. The Go port
|
||||||
|
(cunicu.li/go-rosenpass) uses the same 120 s / +10 s / 3 min.
|
||||||
|
- Arcanum takes no clock. It exports `RECOMMENDED_EPOCH_SECONDS = 120` and a pure helper
|
||||||
|
`epoch_at(unix_seconds) = unix_seconds / 120`; the caller passes the time.
|
||||||
|
- `Session::wireguard_psk(epoch)` is deterministic: both peers get the same PSK for the same epoch,
|
||||||
|
and successive epochs differ. **Forward secrecy between epochs needs a fresh exchange per
|
||||||
|
epoch.** Deriving several epochs from one session is a fallback when an exchange fails.
|
||||||
|
|
||||||
|
### Sealing
|
||||||
|
|
||||||
|
`sealed = 0x01 || ct (1120 B) || ChaCha20-Poly1305 ciphertext || tag (16 B)`, AEAD associated
|
||||||
|
data = the version byte, key and nonce from the schedule above. Each seal uses a fresh
|
||||||
|
encapsulation, so a key and nonce are never used twice. Overhead is 1137 bytes. **Anyone who knows
|
||||||
|
the recipient's public key can seal**: there is no sender authentication, and ChaCha20-Poly1305 is
|
||||||
|
not key-committing, so Custos must not read a successful `open` as proof of who sealed it
|
||||||
|
(QA A-27). The output buffer is allocated once at its final size, so no buffer that held
|
||||||
|
plaintext is reallocated and freed unwiped (QA A-29). This is a documented construction, not HPKE
|
||||||
|
(RFC 9180).
|
||||||
|
|
||||||
|
### Shape of the API
|
||||||
|
|
||||||
|
- `#![no_std]` with `alloc`, `#![forbid(unsafe_code)]`, no I/O, no clock, no OS RNG in the normal
|
||||||
|
dependency graph (checked by `tests/feature_hygiene.rs`).
|
||||||
|
- Randomness is a parameter: `&mut R where R: rand_core::CryptoRng` (rand_core 0.10).
|
||||||
|
- **The `hazmat` feature** exposes the deterministic entry points (`SecretKey::from_seed`,
|
||||||
|
`PublicKey::encapsulate_derand`, `exchange::{initiate,respond}_derand`, `seal::seal_derand`)
|
||||||
|
used by the known-answer tests. It is off by default. The tests turn it on through a
|
||||||
|
self-dev-dependency, which never reaches a normal build; `tests/feature_hygiene.rs` asserts that
|
||||||
|
neither `arcanum/hazmat` nor `ml-kem/hazmat` is in the normal-edge feature graph, and proves the
|
||||||
|
check can see the feature when it is on (QA A-34, A-35). Via and via-core must run the same check.
|
||||||
|
- Every secret type (`SecretKey`, `SharedSecret`, `Session`, `Initiation`, `WireGuardPsk`) is
|
||||||
|
zeroised on drop, is not `Copy`, has no `PartialEq`, and prints as `[redacted]` in `Debug`.
|
||||||
|
- Every parser returns `Result<_, arcanum::Error>`; errors carry only lengths and variants.
|
||||||
|
|
||||||
|
## Tests (written first)
|
||||||
|
|
||||||
|
| # | Mandatory test | File |
|
||||||
|
|:--|:--|:--|
|
||||||
|
| 1 | KATs: ML-KEM-768 (NIST ACVP), X25519 (RFC 7748), X-Wing (draft -11) + differential vs `x-wing` | `tests/kat_mlkem768.rs`, `tests/kat_x25519.rs`, `tests/kat_xwing.rs` |
|
||||||
|
| 2 | Both sides derive the same secret; any changed input byte changes it | `tests/exchange.rs` |
|
||||||
|
| 3 | A zeroed component secret changes the output (both halves bound) | `src/binding_tests.rs`, `tests/binding.rs` |
|
||||||
|
| 4 | PSK epochs differ and are reproducible | `tests/psk_epochs.rs` |
|
||||||
|
| 5 | Malformed input gives typed errors, never a panic; fuzz target | `tests/malformed.rs`, `fuzz/fuzz_targets/untrusted_input.rs` |
|
||||||
|
| 6 | `cargo deny`, `cargo audit` clean; no unsafe | `deny.toml`, `tests/no_unsafe.rs`, `#![forbid(unsafe_code)]` |
|
||||||
|
|
||||||
|
Also: `tests/golden_v1.rs` (frozen v1 outputs), `tests/feature_hygiene.rs` (no hazmat, no OS RNG
|
||||||
|
in the release graph). Vector provenance is in `tests/vectors/SOURCES.md`. QA's attack plan
|
||||||
|
(`F:\Akce\Via\qa\01_arcanum_attacks.md`) is mapped case by case in the PR's self-review.
|
||||||
|
|
||||||
|
## Risks
|
||||||
|
|
||||||
|
| Risk | Consequence | Mitigation |
|
||||||
|
|:--|:--|:--|
|
||||||
|
| A bug in our X-Wing glue | Peers agree on a weak secret, or interoperate with nobody | Official vectors + differential test vs RustCrypto `x-wing` |
|
||||||
|
| `ml-kem` or `x25519-dalek` bug, side channel | Weaker PQ or classical half | Hybrid: either half suffices; versions pinned; `cargo audit` |
|
||||||
|
| Reusing one session for many epochs | No forward secrecy between epochs | Documented; Day 3 runs a fresh exchange per epoch |
|
||||||
|
| Substituted public key (MITM) | Attacker learns the PSK | Mutual exchange; Via must authenticate both hybrid public keys (Tessera); PQ signatures later. See THREAT_MODEL.md |
|
||||||
|
| Static key theft later | Recorded exchanges recomputable | Day 3 adds ephemeral keys or rotates key pairs |
|
||||||
|
| Deterministic API reaching production | Predictable PSKs | `hazmat` off by default; feature-graph test; consumers repeat it |
|
||||||
|
| Secrets left in memory | Key recovery from a memory dump | `ZeroizeOnDrop` everywhere we own the bytes; residuals listed below |
|
||||||
|
| Draft changes before RFC | Interop break with other X-Wing users | Draft number and vector sha256 pinned; labels versioned |
|
||||||
|
| A relabel or re-encoding | Stored sealed blobs unopenable; peers disagree | Golden v1 vectors |
|
||||||
|
|
||||||
|
## Gaps (known, not hidden)
|
||||||
|
|
||||||
|
- **Not audited**, and neither are `ml-kem` nor the `x-wing` reference.
|
||||||
|
- **No post-quantum authentication of public keys.** The mutual exchange authenticates whoever
|
||||||
|
holds the secret keys matching the public keys each side was given; getting the right public
|
||||||
|
keys is the caller's job, and ML-DSA is out of scope.
|
||||||
|
- **No forward secrecy against later theft of static secret keys** within Arcanum itself.
|
||||||
|
- **Side channels** are inherited from the crates; Arcanum adds no constant-time analysis of its
|
||||||
|
own (QA A-32, a dudect run, is not done).
|
||||||
|
- **Zeroisation residuals:** HKDF/HMAC and SHA-3 internal states are wiped only as far as those
|
||||||
|
crates' `zeroize` features go; values moved by the compiler may leave copies on the stack;
|
||||||
|
`PublicKey` copies are not secret. QA A-29's allocator probe is not built.
|
||||||
|
- **Sealing is not HPKE**, and has no sender authentication.
|
||||||
|
- **X25519 RFC 7748 1,000,000-iteration vector** is not run (minutes in a debug build); the
|
||||||
|
1- and 1,000-iteration vectors are.
|
||||||
|
- **FIPS 203 itself** was not read from the NIST PDF; the ML-KEM facts rest on the ACVP vectors,
|
||||||
|
the draft's references and the `ml-kem` source.
|
||||||
|
- **Fuzzing** ran on Windows MSVC with libFuzzer for the time recorded in the PR, not continuously.
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# Arcanum threat model
|
||||||
|
|
||||||
|
Scope: the `arcanum` crate v0.1.0 — the X-Wing hybrid KEM, the mutual exchange, the key schedule, the WireGuard PSK
|
||||||
|
derivation and the sealing helper. **Not audited.**
|
||||||
|
|
||||||
|
## Assets
|
||||||
|
|
||||||
|
1. The responder's hybrid secret key (a 32-byte seed).
|
||||||
|
2. Each exchange's shared secret and the `Session` derived from it.
|
||||||
|
3. Each epoch's WireGuard pre-shared key.
|
||||||
|
4. The plaintext inside a sealed box.
|
||||||
|
|
||||||
|
## Adversaries and what Arcanum protects against
|
||||||
|
|
||||||
|
| Adversary | Can do | Protected? | Why |
|
||||||
|
|:--|:--|:--|:--|
|
||||||
|
| **Harvest now, decrypt later** | Records every control-channel message and WireGuard packet today; gets a large quantum computer later | **Yes**, the main goal | The PSK comes from X-Wing. Shor breaks X25519, but ML-KEM-768 is believed to resist quantum attack, and X-Wing's secret is secure if either half is. WireGuard mixes the PSK into its key schedule, so recorded traffic stays confidential. |
|
||||||
|
| Classical passive eavesdropper | Records traffic, no quantum computer | Yes | Both halves hold. |
|
||||||
|
| A future break of ML-KEM (cryptanalysis or an implementation bug) | Recovers `ss_M` | Yes, classically | X25519 still protects the secret (hybrid). |
|
||||||
|
| A break of X25519 | Recovers `ss_X` | Yes | ML-KEM still protects it. |
|
||||||
|
| Active attacker who modifies ciphertexts | Flips bytes in transit | Detected or harmless | Changed `ct_M` → ML-KEM implicit rejection gives an unrelated secret; changed `ct_X` → different `ss_X`; the transcript hash binds both. The peers' PSKs differ and WireGuard simply fails to handshake. Low-order X25519 values are rejected with a typed error. |
|
||||||
|
| Malformed or hostile input | Sends wrong lengths, invalid ML-KEM keys, random bytes | No panic, typed error | Tests 5 and the fuzz target. |
|
||||||
|
| **Impersonating one peer** (QA A-15) | Knows both public keys, sends its own encapsulation as if from the initiator | **Yes** | The exchange is mutual: the PSK needs both directions' secrets, and the responder's encapsulation goes to the real initiator's key. Without that secret key the attacker cannot compute the PSK, even after breaking WireGuard's X25519 keys. |
|
||||||
|
| Epoch confusion | Replays an old exchange for a new epoch | Harmless for secrecy | The epoch is in every PSK derivation; an attacker who cannot decapsulate cannot compute any epoch's PSK. Refusing to re-install an old PSK (rollback) is the caller's job: Arcanum keeps no state. |
|
||||||
|
|
||||||
|
## What Arcanum does NOT protect against
|
||||||
|
|
||||||
|
| Not protected | Why, and who has to handle it |
|
||||||
|
|:--|:--|
|
||||||
|
| **Endpoint compromise** | Malware on a device reads keys and plaintext from memory. Zeroisation narrows the window; it does not close it. Keys at rest belong in Custos or Tessera. |
|
||||||
|
| **Traffic analysis** | Who talks to whom, when and how much is visible on the wire. Padding and cover traffic are the Stealth transport's job (Day 7), and only partly. |
|
||||||
|
| **Substituted public keys** | The mutual exchange authenticates whoever holds the secret keys matching the public keys each side was *given*. An attacker who gets a peer to accept her public key in place of the real one learns that exchange's PSK. In Via, WireGuard's static keys still authenticate the tunnel classically, so this falls back to classical security, not to plaintext. Via must distribute both hybrid public keys signed with Tessera (Day 3). If those signatures are classical (Ed25519), a quantum attacker who can forge them can substitute keys; ML-DSA closes that later. |
|
||||||
|
| **Theft of static secret keys later** | With static key pairs, whoever later steals both secret keys and has recorded the exchanges can recompute their PSKs. Day 3 must add an ephemeral key pair per exchange (as Rosenpass does) or rotate key pairs. |
|
||||||
|
| **Bad randomness** | The caller supplies the RNG. A broken RNG breaks every scheme. The `_derand` functions exist only with the off-by-default `hazmat` feature, for known-answer tests; reusing their seeds is catastrophic. |
|
||||||
|
| **Side channels** | Timing and cache behaviour are those of `ml-kem`, `x25519-dalek`, `sha3` and `chacha20poly1305`. Arcanum adds only fixed-length, data-independent glue, but has not been measured. |
|
||||||
|
| **Reusing one session for many epochs** | Supported, but a leaked session reveals every epoch's PSK derived from it. Forward secrecy needs one exchange per epoch. |
|
||||||
|
| **Denial of service** | An attacker on the path can always drop packets. |
|
||||||
|
| **Legacy peers** | Classic WireGuard peers (routers, third-party clients) get no Arcanum PSK and are labelled "not post-quantum" by Via. |
|
||||||
|
|
||||||
|
## Trust assumptions
|
||||||
|
|
||||||
|
- ML-KEM-768 is IND-CCA2 secure against quantum adversaries (FIPS 203).
|
||||||
|
- X25519 is a secure Diffie–Hellman group against classical adversaries (RFC 7748).
|
||||||
|
- SHA3-256, SHAKE256 and HKDF-SHA-256 behave as their standards say; X-Wing's security argument
|
||||||
|
(draft-connolly-cfrg-xwing-kem-11) holds.
|
||||||
|
- The vetted crates implement those standards correctly; the known-answer tests check the parts
|
||||||
|
Arcanum uses, not everything.
|
||||||
|
- The caller authenticates the responder's public key and supplies a cryptographically secure RNG.
|
||||||
|
|
||||||
|
## Blast radius if Arcanum is wrong
|
||||||
|
|
||||||
|
Via uses Arcanum only as the WireGuard PSK. A total failure of Arcanum (for example, both peers
|
||||||
|
derive a predictable PSK) reduces a Via tunnel to plain WireGuard: still authenticated and encrypted
|
||||||
|
classically, but open to harvest-now-decrypt-later. It never reduces it below WireGuard. Sealing
|
||||||
|
(Custos, Tabellarius) is different: there Arcanum is the only layer, which is why those migrations
|
||||||
|
wait for the audit.
|
||||||
|
|
||||||
|
## Residual memory exposure
|
||||||
|
|
||||||
|
Moves in Rust copy stack bytes, and `zeroize` cannot reach those copies; hash and HMAC states are
|
||||||
|
wiped only as far as the crates' `zeroize` features go. A process memory dump taken while Arcanum
|
||||||
|
runs may contain key material. That is accepted for v1 and is one more reason keys at rest live in
|
||||||
|
Custos or Tessera, not in Arcanum's callers.
|
||||||
Generated
+501
@@ -0,0 +1,501 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aead"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-common",
|
||||||
|
"inout",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arbitrary"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arcanum"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"chacha20poly1305",
|
||||||
|
"hkdf",
|
||||||
|
"hmac",
|
||||||
|
"ml-kem",
|
||||||
|
"rand_core",
|
||||||
|
"sha2",
|
||||||
|
"sha3",
|
||||||
|
"shake",
|
||||||
|
"x25519-dalek",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arcanum-fuzz"
|
||||||
|
version = "0.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"arcanum",
|
||||||
|
"libfuzzer-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "block-buffer"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cc"
|
||||||
|
version = "1.11.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69"
|
||||||
|
dependencies = [
|
||||||
|
"find-msvc-tools",
|
||||||
|
"jobserver",
|
||||||
|
"libc",
|
||||||
|
"shlex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chacha20"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cipher",
|
||||||
|
"cpufeatures",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chacha20poly1305"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b89e1c441e926b9c82a8d023f6e1b7ae0adcfaa7d621814e4d60789bac751cb"
|
||||||
|
dependencies = [
|
||||||
|
"aead",
|
||||||
|
"chacha20",
|
||||||
|
"cipher",
|
||||||
|
"poly1305",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[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",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cmov"
|
||||||
|
version = "0.5.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crypto-common"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ctutils"
|
||||||
|
version = "0.4.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f"
|
||||||
|
dependencies = [
|
||||||
|
"cmov",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "curve25519-dalek"
|
||||||
|
version = "5.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5eed333089e2e1c1ac8c6c0398e5e2497b4c9926ca6d0365ed1e099afa5bc23"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"curve25519-dalek-derive",
|
||||||
|
"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",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "digest"
|
||||||
|
version = "0.11.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
|
||||||
|
dependencies = [
|
||||||
|
"block-buffer",
|
||||||
|
"crypto-common",
|
||||||
|
"ctutils",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fiat-crypto"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "find-msvc-tools"
|
||||||
|
version = "0.1.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"r-efi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hkdf"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018"
|
||||||
|
dependencies = [
|
||||||
|
"hmac",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hmac"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hybrid-array"
|
||||||
|
version = "0.4.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17"
|
||||||
|
dependencies = [
|
||||||
|
"ctutils",
|
||||||
|
"typenum",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inout"
|
||||||
|
version = "0.2.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jobserver"
|
||||||
|
version = "0.1.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "keccak"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kem"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "01737161ba802849cfd486b5bd209d38ba4943494c249a8126005170c7621edd"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-common",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.191"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libfuzzer-sys"
|
||||||
|
version = "0.4.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2"
|
||||||
|
dependencies = [
|
||||||
|
"arbitrary",
|
||||||
|
"cc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ml-kem"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "84ca78b66f46050d49fa21693ae8adab2081d8101b65d8067316b6b70b64a0d1"
|
||||||
|
dependencies = [
|
||||||
|
"hybrid-array",
|
||||||
|
"kem",
|
||||||
|
"module-lattice",
|
||||||
|
"rand_core",
|
||||||
|
"sha3",
|
||||||
|
"shake",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "module-lattice"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4a1e412c8ee83a1c678f427806219c49c96b2511b9f07497514f0e43fb26155"
|
||||||
|
dependencies = [
|
||||||
|
"ctutils",
|
||||||
|
"hybrid-array",
|
||||||
|
"num-traits",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "poly1305"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c"
|
||||||
|
dependencies = [
|
||||||
|
"cpufeatures",
|
||||||
|
"universal-hash",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.107"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "r-efi"
|
||||||
|
version = "6.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc_version"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
|
||||||
|
dependencies = [
|
||||||
|
"semver",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "semver"
|
||||||
|
version = "1.0.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha3"
|
||||||
|
version = "0.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
"keccak",
|
||||||
|
"sponge-cursor",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shake"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09057cb2149ad4cbd2da1e26b351f9a4c354219421229c69c3063e6f61947c4a"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
"keccak",
|
||||||
|
"sponge-cursor",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shlex"
|
||||||
|
version = "2.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sponge-cursor"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620"
|
||||||
|
dependencies = [
|
||||||
|
"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"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.20.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "universal-hash"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-common",
|
||||||
|
"ctutils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x25519-dalek"
|
||||||
|
version = "3.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6"
|
||||||
|
dependencies = [
|
||||||
|
"curve25519-dalek",
|
||||||
|
"rand_core",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zeroize"
|
||||||
|
version = "1.9.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85ccff8a4805aba1eaa4cd4afcd49f7557dd2cf5fd79ffc37f8959466effc5aa"
|
||||||
|
dependencies = [
|
||||||
|
"zeroize_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zeroize_derive"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
[package]
|
||||||
|
name = "arcanum-fuzz"
|
||||||
|
version = "0.0.0"
|
||||||
|
edition = "2024"
|
||||||
|
license = "CPAL-1.0"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[package.metadata]
|
||||||
|
cargo-fuzz = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libfuzzer-sys = "0.4"
|
||||||
|
arcanum = { path = "..", features = ["hazmat"] }
|
||||||
|
|
||||||
|
# Keep the fuzz crate out of the library's own dependency graph and checks.
|
||||||
|
[workspace]
|
||||||
|
members = ["."]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "untrusted_input"
|
||||||
|
path = "fuzz_targets/untrusted_input.rs"
|
||||||
|
test = false
|
||||||
|
doc = false
|
||||||
|
bench = false
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
//! Mandatory test 5, fuzzed: every entry point that takes bytes from the network must return a
|
||||||
|
//! typed result for any input, and never panic. Also QA A-43: a sealed box opens only if it is
|
||||||
|
//! exactly what `seal` produced.
|
||||||
|
//!
|
||||||
|
//! The first byte picks the mode. Modes 0-3 parse raw bytes. Modes 4-7 XOR the input into a valid
|
||||||
|
//! public key, ciphertext or sealed box, so the fuzzer reaches decapsulation and the AEAD instead
|
||||||
|
//! of stopping at the length checks.
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use arcanum::xwing::{Ciphertext, PublicKey, SecretKey};
|
||||||
|
use arcanum::{exchange, seal};
|
||||||
|
use libfuzzer_sys::fuzz_target;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
const ESEED_I: [u8; 64] = [0x24; 64];
|
||||||
|
const ESEED_R: [u8; 64] = [0x25; 64];
|
||||||
|
|
||||||
|
fn xor_into(base: &[u8], data: &[u8]) -> Vec<u8> {
|
||||||
|
let mut out = base.to_vec();
|
||||||
|
for (i, b) in data.iter().enumerate() {
|
||||||
|
if i < out.len() {
|
||||||
|
out[i] ^= b;
|
||||||
|
} else {
|
||||||
|
out.push(*b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fuzz_target!(|input: &[u8]| {
|
||||||
|
let Some((&mode, data)) = input.split_first() else { return };
|
||||||
|
// Keys are built once; expanding them on every run would dominate the fuzzing time.
|
||||||
|
static KEYS: OnceLock<(SecretKey, SecretKey)> = OnceLock::new();
|
||||||
|
let (me, peer) = KEYS.get_or_init(|| (SecretKey::from_seed([0x42; 32]), SecretKey::from_seed([0x43; 32])));
|
||||||
|
match mode % 8 {
|
||||||
|
0 => {
|
||||||
|
if let Ok(pk) = PublicKey::from_bytes(data) {
|
||||||
|
let _ = exchange::initiate_derand(me, &pk, b"fuzz", &ESEED_I);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
if let Ok(ct) = Ciphertext::from_bytes(data) {
|
||||||
|
let _ = exchange::respond_derand(me, peer.public_key(), &ct, b"fuzz", &ESEED_R);
|
||||||
|
if let Ok(init) = exchange::initiate_derand(me, peer.public_key(), b"fuzz", &ESEED_I) {
|
||||||
|
let _ = init.finish(&ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
let _ = seal::open(me, data, b"fuzz");
|
||||||
|
}
|
||||||
|
3 => {
|
||||||
|
if let Ok(k) = SecretKey::from_bytes(data) {
|
||||||
|
let _ = k.public_key().as_bytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
4 => {
|
||||||
|
let pk = xor_into(peer.public_key().as_bytes(), data);
|
||||||
|
if let Ok(pk) = PublicKey::from_bytes(&pk) {
|
||||||
|
if let Ok(init) = exchange::initiate_derand(me, &pk, b"fuzz", &ESEED_I) {
|
||||||
|
let _ = exchange::respond_derand(peer, me.public_key(), init.ciphertext(), b"fuzz", &ESEED_R);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
5 | 6 => {
|
||||||
|
// Tamper with ct_IR (mode 5) or ct_RI (mode 6) of an honest exchange.
|
||||||
|
let init = exchange::initiate_derand(me, peer.public_key(), b"fuzz", &ESEED_I).unwrap();
|
||||||
|
let honest_ir = init.ciphertext().clone();
|
||||||
|
let (honest_ri, responder) =
|
||||||
|
exchange::respond_derand(peer, me.public_key(), &honest_ir, b"fuzz", &ESEED_R).unwrap();
|
||||||
|
let honest_psk = *responder.wireguard_psk(0).as_bytes();
|
||||||
|
if mode % 8 == 5 {
|
||||||
|
let bad = xor_into(honest_ir.as_bytes(), data);
|
||||||
|
if let Ok(bad) = Ciphertext::from_bytes(&bad) {
|
||||||
|
if let Ok((_, s)) = exchange::respond_derand(peer, me.public_key(), &bad, b"fuzz", &ESEED_R) {
|
||||||
|
// Only an unchanged ciphertext may reproduce the honest PSK.
|
||||||
|
if bad != honest_ir {
|
||||||
|
assert_ne!(s.wireguard_psk(0).as_bytes(), &honest_psk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let bad = xor_into(honest_ri.as_bytes(), data);
|
||||||
|
if let Ok(bad) = Ciphertext::from_bytes(&bad) {
|
||||||
|
if let Ok(s) = init.finish(&bad) {
|
||||||
|
if bad != honest_ri {
|
||||||
|
assert_ne!(s.wireguard_psk(0).as_bytes(), &honest_psk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let sealed = seal::seal_derand(me.public_key(), b"fuzz plaintext", b"fuzz", &ESEED_I).unwrap();
|
||||||
|
let bad = xor_into(&sealed, data);
|
||||||
|
if let Ok(pt) = seal::open(me, &bad, b"fuzz") {
|
||||||
|
// Only the unchanged box may open.
|
||||||
|
assert_eq!(bad, sealed);
|
||||||
|
assert_eq!(pt.as_slice(), b"fuzz plaintext");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
max_width = 120
|
||||||
|
use_small_heuristics = "Max"
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
//! Mandatory test 3, at the combiner: replacing either component secret by zeros must change the
|
||||||
|
//! output, so both halves are bound. Also QA A-02 (the combiner is exactly the draft's SHA3-256)
|
||||||
|
//! and A-07 (labels are prefix-free). These are unit tests because the combiner and the schedule
|
||||||
|
//! are crate-private (exposing them would invite misuse).
|
||||||
|
|
||||||
|
use sha3::{Digest, Sha3_256};
|
||||||
|
|
||||||
|
use crate::schedule::{ALL_LABELS, Transcript, session_prk};
|
||||||
|
use crate::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SecretKey, SharedSecret};
|
||||||
|
use crate::xwing::{SHARED_SECRET_LEN, combiner};
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
|
const SS_M: [u8; 32] = [0xa1; 32];
|
||||||
|
const SS_X: [u8; 32] = [0xb2; 32];
|
||||||
|
const CT_X: [u8; 32] = [0xc3; 32];
|
||||||
|
const PK_X: [u8; 32] = [0xd4; 32];
|
||||||
|
const ZERO: [u8; SHARED_SECRET_LEN] = [0; SHARED_SECRET_LEN];
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zeroing_the_ml_kem_secret_changes_the_output() {
|
||||||
|
let honest = combiner(&SS_M, &SS_X, &CT_X, &PK_X);
|
||||||
|
assert_ne!(combiner(&ZERO, &SS_X, &CT_X, &PK_X), honest);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zeroing_the_x25519_secret_changes_the_output() {
|
||||||
|
let honest = combiner(&SS_M, &SS_X, &CT_X, &PK_X);
|
||||||
|
assert_ne!(combiner(&SS_M, &ZERO, &CT_X, &PK_X), honest);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zeroing_both_differs_from_zeroing_either() {
|
||||||
|
let only_m = combiner(&ZERO, &SS_X, &CT_X, &PK_X);
|
||||||
|
let only_x = combiner(&SS_M, &ZERO, &CT_X, &PK_X);
|
||||||
|
let both = combiner(&ZERO, &ZERO, &CT_X, &PK_X);
|
||||||
|
assert_ne!(only_m, only_x);
|
||||||
|
assert_ne!(both, only_m);
|
||||||
|
assert_ne!(both, only_x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Swapping the two secrets must not give the same output: the positions are bound, not just the set.
|
||||||
|
#[test]
|
||||||
|
fn the_two_halves_are_not_interchangeable() {
|
||||||
|
assert_ne!(combiner(&SS_M, &SS_X, &CT_X, &PK_X), combiner(&SS_X, &SS_M, &CT_X, &PK_X));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every single bit of either component secret reaches the output.
|
||||||
|
#[test]
|
||||||
|
fn every_bit_of_each_component_reaches_the_output() {
|
||||||
|
let honest = combiner(&SS_M, &SS_X, &CT_X, &PK_X);
|
||||||
|
for byte in 0..32 {
|
||||||
|
for bit in 0..8 {
|
||||||
|
let mut m = SS_M;
|
||||||
|
m[byte] ^= 1 << bit;
|
||||||
|
assert_ne!(combiner(&m, &SS_X, &CT_X, &PK_X), honest, "ss_M byte {byte} bit {bit}");
|
||||||
|
let mut x = SS_X;
|
||||||
|
x[byte] ^= 1 << bit;
|
||||||
|
assert_ne!(combiner(&SS_M, &x, &CT_X, &PK_X), honest, "ss_X byte {byte} bit {bit}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ss(b: [u8; 32]) -> SharedSecret {
|
||||||
|
SharedSecret(Zeroizing::new(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The binding survives the key schedule, through the real `session_prk`: a zeroed component of
|
||||||
|
/// the first direction's X-Wing secret, or a missing second direction, gives a different session.
|
||||||
|
/// The last case is QA A-15: an attacker who impersonates the initiator knows ss_IR (she made it)
|
||||||
|
/// and the whole transcript, but not ss_RI; every guess of hers that reuses known values fails.
|
||||||
|
#[test]
|
||||||
|
fn zeroed_components_and_a_missing_direction_give_a_different_session() {
|
||||||
|
let pk_i = SecretKey::from_bytes(&[1u8; 32]).unwrap().public_key().clone();
|
||||||
|
let pk_r = SecretKey::from_bytes(&[2u8; 32]).unwrap().public_key().clone();
|
||||||
|
let ct_ir = Ciphertext::from_bytes(&[3u8; CIPHERTEXT_LEN]).unwrap();
|
||||||
|
let ct_ri = Ciphertext::from_bytes(&[4u8; CIPHERTEXT_LEN]).unwrap();
|
||||||
|
let t = Transcript { initiator: &pk_i, responder: &pk_r, ct_ir: &ct_ir, ct_ri: &ct_ri, context: b"ctx" };
|
||||||
|
let ss_ir = combiner(&SS_M, &SS_X, &CT_X, &PK_X);
|
||||||
|
let ss_ri = combiner(&[0x01; 32], &[0x02; 32], &CT_X, &PK_X);
|
||||||
|
|
||||||
|
let honest = *session_prk(&ss(ss_ir), &ss(ss_ri), &t);
|
||||||
|
let no_m = *session_prk(&ss(combiner(&ZERO, &SS_X, &CT_X, &PK_X)), &ss(ss_ri), &t);
|
||||||
|
let no_x = *session_prk(&ss(combiner(&SS_M, &ZERO, &CT_X, &PK_X)), &ss(ss_ri), &t);
|
||||||
|
assert_ne!(honest, no_m);
|
||||||
|
assert_ne!(honest, no_x);
|
||||||
|
for guess in [ZERO, ss_ir, [0xff; 32]] {
|
||||||
|
assert_ne!(*session_prk(&ss(ss_ir), &ss(guess), &t), honest, "PSK computable without ss_RI");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-02: recomputed independently, the combiner is SHA3-256(ss_M || ss_X || ct_X || pk_X ||
|
||||||
|
/// 5c2e2f2f5e5c), label last, no length prefixes.
|
||||||
|
#[test]
|
||||||
|
fn the_combiner_is_the_drafts_sha3_256() {
|
||||||
|
let mut h = Sha3_256::new();
|
||||||
|
for part in [&SS_M[..], &SS_X, &CT_X, &PK_X, &[0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c]] {
|
||||||
|
h.update(part);
|
||||||
|
}
|
||||||
|
let want: [u8; 32] = h.finalize().into();
|
||||||
|
assert_eq!(combiner(&SS_M, &SS_X, &CT_X, &PK_X), want);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-07: every label is unique, ASCII, versioned, and none is a prefix of another.
|
||||||
|
#[test]
|
||||||
|
fn labels_are_versioned_and_prefix_free() {
|
||||||
|
for (i, a) in ALL_LABELS.iter().enumerate() {
|
||||||
|
assert!(a.is_ascii() && a.starts_with(b"domi/arcanum/v1/"));
|
||||||
|
for (j, b) in ALL_LABELS.iter().enumerate() {
|
||||||
|
if i != j {
|
||||||
|
assert!(!b.starts_with(a), "{} is a prefix of {}", a.escape_ascii(), b.escape_ascii());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-05: with both X-Wing secrets held fixed, every part of the transcript (both public keys,
|
||||||
|
/// both ciphertexts, the context and its length) still changes the session. This isolates the
|
||||||
|
/// key-schedule binding from what X-Wing itself hashes.
|
||||||
|
#[test]
|
||||||
|
fn the_schedule_binds_the_full_transcript() {
|
||||||
|
let ss_ir = SharedSecret(Zeroizing::new([7u8; 32]));
|
||||||
|
let ss_ri = SharedSecret(Zeroizing::new([8u8; 32]));
|
||||||
|
let pk_i = SecretKey::from_bytes(&[1u8; 32]).unwrap().public_key().clone();
|
||||||
|
let pk_r = SecretKey::from_bytes(&[2u8; 32]).unwrap().public_key().clone();
|
||||||
|
let ct_ir = Ciphertext::from_bytes(&[3u8; CIPHERTEXT_LEN]).unwrap();
|
||||||
|
let ct_ri = Ciphertext::from_bytes(&[4u8; CIPHERTEXT_LEN]).unwrap();
|
||||||
|
let prk = |i: &PublicKey, r: &PublicKey, a: &Ciphertext, b: &Ciphertext, ctx: &[u8]| {
|
||||||
|
*session_prk(&ss_ir, &ss_ri, &Transcript { initiator: i, responder: r, ct_ir: a, ct_ri: b, context: ctx })
|
||||||
|
};
|
||||||
|
let honest = prk(&pk_i, &pk_r, &ct_ir, &ct_ri, b"ctx");
|
||||||
|
|
||||||
|
let flip_pk = |pk: &PublicKey, n: usize| {
|
||||||
|
let mut b = *pk.as_bytes();
|
||||||
|
b[n] ^= 0x01;
|
||||||
|
PublicKey::from_bytes(&b).ok()
|
||||||
|
};
|
||||||
|
let flip_ct = |ct: &Ciphertext, n: usize| {
|
||||||
|
let mut b = *ct.as_bytes();
|
||||||
|
b[n] ^= 0x01;
|
||||||
|
Ciphertext::from_bytes(&b).unwrap()
|
||||||
|
};
|
||||||
|
let mut checked = 0;
|
||||||
|
for n in (0..PUBLIC_KEY_LEN).step_by(37).chain([0, 1183, 1184, PUBLIC_KEY_LEN - 1]) {
|
||||||
|
if let Some(p) = flip_pk(&pk_i, n) {
|
||||||
|
assert_ne!(prk(&p, &pk_r, &ct_ir, &ct_ri, b"ctx"), honest, "pk_I[{n}]");
|
||||||
|
checked += 1;
|
||||||
|
}
|
||||||
|
if let Some(p) = flip_pk(&pk_r, n) {
|
||||||
|
assert_ne!(prk(&pk_i, &p, &ct_ir, &ct_ri, b"ctx"), honest, "pk_R[{n}]");
|
||||||
|
checked += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(checked > 20, "the sampled public-key flips must mostly parse");
|
||||||
|
for n in (0..CIPHERTEXT_LEN).step_by(37).chain([0, 1087, 1088, CIPHERTEXT_LEN - 1]) {
|
||||||
|
assert_ne!(prk(&pk_i, &pk_r, &flip_ct(&ct_ir, n), &ct_ri, b"ctx"), honest, "ct_IR[{n}]");
|
||||||
|
assert_ne!(prk(&pk_i, &pk_r, &ct_ir, &flip_ct(&ct_ri, n), b"ctx"), honest, "ct_RI[{n}]");
|
||||||
|
}
|
||||||
|
assert_ne!(prk(&pk_r, &pk_i, &ct_ir, &ct_ri, b"ctx"), honest, "roles swapped");
|
||||||
|
assert_ne!(prk(&pk_i, &pk_r, &ct_ri, &ct_ir, b"ctx"), honest, "ciphertexts swapped");
|
||||||
|
assert_ne!(prk(&pk_i, &pk_r, &ct_ir, &ct_ri, b"ctX"), honest, "context");
|
||||||
|
assert_ne!(prk(&pk_i, &pk_r, &ct_ir, &ct_ri, b"ctx\0"), honest, "context length");
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
//! Typed errors. No variant carries secret material, so an error can always be logged.
|
||||||
|
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
|
/// Which input was malformed.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Input {
|
||||||
|
/// A hybrid public key (`pk_M || pk_X`).
|
||||||
|
PublicKey,
|
||||||
|
/// A hybrid secret key (the 32-byte seed).
|
||||||
|
SecretKey,
|
||||||
|
/// A hybrid ciphertext (`ct_M || ct_X`).
|
||||||
|
Ciphertext,
|
||||||
|
/// A sealed box.
|
||||||
|
SealedBox,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Input {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str(match self {
|
||||||
|
Input::PublicKey => "public key",
|
||||||
|
Input::SecretKey => "secret key",
|
||||||
|
Input::Ciphertext => "ciphertext",
|
||||||
|
Input::SealedBox => "sealed box",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Everything that can go wrong in Arcanum. Every parser returns one of these; none panics.
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Error {
|
||||||
|
/// The input has a fixed length and this is not it.
|
||||||
|
InvalidLength {
|
||||||
|
/// What was being parsed.
|
||||||
|
input: Input,
|
||||||
|
/// The required length in bytes.
|
||||||
|
expected: usize,
|
||||||
|
/// The length that was given.
|
||||||
|
actual: usize,
|
||||||
|
},
|
||||||
|
/// The input is shorter than the smallest valid value.
|
||||||
|
TooShort {
|
||||||
|
/// What was being parsed.
|
||||||
|
input: Input,
|
||||||
|
/// The minimum length in bytes.
|
||||||
|
minimum: usize,
|
||||||
|
/// The length that was given.
|
||||||
|
actual: usize,
|
||||||
|
},
|
||||||
|
/// The ML-KEM-768 part of a public key fails the FIPS 203 §7.2 encapsulation key check.
|
||||||
|
InvalidPublicKey,
|
||||||
|
/// An X25519 value is a low-order point, so the classical half would contribute nothing.
|
||||||
|
NonContributory,
|
||||||
|
/// A sealed box was made by a format version this build does not know.
|
||||||
|
UnsupportedVersion(u8),
|
||||||
|
/// A sealed box failed authentication: wrong key, wrong associated data, or tampered.
|
||||||
|
DecryptionFailed,
|
||||||
|
/// The plaintext is longer than ChaCha20-Poly1305 allows (about 256 GiB).
|
||||||
|
MessageTooLong,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::InvalidLength { input, expected, actual } => {
|
||||||
|
write!(f, "{input} has the wrong length: expected {expected} bytes, got {actual}")
|
||||||
|
}
|
||||||
|
Error::TooShort { input, minimum, actual } => {
|
||||||
|
write!(f, "{input} is too short: at least {minimum} bytes, got {actual}")
|
||||||
|
}
|
||||||
|
Error::InvalidPublicKey => f.write_str("public key fails the ML-KEM-768 encapsulation key check"),
|
||||||
|
Error::NonContributory => f.write_str("X25519 value is a low-order point"),
|
||||||
|
Error::UnsupportedVersion(v) => write!(f, "sealed box version {v} is not supported"),
|
||||||
|
Error::DecryptionFailed => f.write_str("sealed box could not be opened"),
|
||||||
|
Error::MessageTooLong => f.write_str("message is too long to seal"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::error::Error for Error {}
|
||||||
|
|
||||||
|
pub(crate) fn exact<const N: usize>(input: Input, bytes: &[u8]) -> Result<&[u8; N], Error> {
|
||||||
|
bytes.try_into().map_err(|_| Error::InvalidLength { input, expected: N, actual: bytes.len() })
|
||||||
|
}
|
||||||
+204
@@ -0,0 +1,204 @@
|
|||||||
|
//! A mutual exchange between two peers, and the WireGuard PSK it yields per epoch.
|
||||||
|
//!
|
||||||
|
//! Each peer has a hybrid key pair, and each knows the other's public key (authenticated by the
|
||||||
|
//! caller; for Via, signed with the device's Tessera key). The exchange runs both directions:
|
||||||
|
//!
|
||||||
|
//! ```text
|
||||||
|
//! initiator I responder R
|
||||||
|
//! (ct_IR, ss_IR) = Encaps(pk_R) ct_IR -> ss_IR = Decaps(sk_R, ct_IR)
|
||||||
|
//! (ct_RI, ss_RI) = Encaps(pk_I)
|
||||||
|
//! ss_RI = Decaps(sk_I, ct_RI) <- ct_RI
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! The session is derived from **both** secrets and the whole transcript, so a peer that cannot
|
||||||
|
//! decapsulate the other direction cannot compute it. A single encapsulation would authenticate
|
||||||
|
//! only the receiver: anyone who knows `pk_R` can encapsulate to it, so once a quantum computer
|
||||||
|
//! breaks WireGuard's static X25519 keys, an attacker could impersonate the initiator and choose
|
||||||
|
//! the responder's PSK (QA finding A-15). Rosenpass is mutual for the same reason.
|
||||||
|
//!
|
||||||
|
//! **Forward secrecy between epochs needs one exchange per epoch** (Rosenpass rotates every
|
||||||
|
//! 120 s), and forward secrecy against a later theft of a static key needs the key pairs to be
|
||||||
|
//! ephemeral, or an ephemeral exchange added on top (Day 3).
|
||||||
|
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
|
use rand_core::CryptoRng;
|
||||||
|
use zeroize::{ZeroizeOnDrop, Zeroizing};
|
||||||
|
|
||||||
|
use crate::error::Error;
|
||||||
|
use crate::schedule::{LABEL_WIREGUARD_PSK, Prk, Transcript, expand, session_prk};
|
||||||
|
use crate::xwing::{Ciphertext, ENCAPSULATION_SEED_LEN, PublicKey, SecretKey, SharedSecret};
|
||||||
|
|
||||||
|
/// Length of a WireGuard pre-shared key.
|
||||||
|
pub const WIREGUARD_PSK_LEN: usize = 32;
|
||||||
|
|
||||||
|
/// The keys both peers hold after one exchange. Zeroised on drop; never printed.
|
||||||
|
pub struct Session {
|
||||||
|
prk: Prk,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Session {
|
||||||
|
/// The WireGuard pre-shared key for `epoch` (see [`crate::epoch_at`]). Deterministic: both
|
||||||
|
/// peers get the same key for the same epoch, and different epochs give different keys.
|
||||||
|
#[must_use]
|
||||||
|
pub fn wireguard_psk(&self, epoch: u64) -> WireGuardPsk {
|
||||||
|
WireGuardPsk(expand(&self.prk, &[LABEL_WIREGUARD_PSK, &epoch.to_be_bytes()]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Session {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("Session([redacted])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZeroizeOnDrop for Session {}
|
||||||
|
|
||||||
|
/// A 32-byte WireGuard pre-shared key. Zeroised on drop; never printed.
|
||||||
|
pub struct WireGuardPsk(Zeroizing<[u8; WIREGUARD_PSK_LEN]>);
|
||||||
|
|
||||||
|
impl WireGuardPsk {
|
||||||
|
/// The raw key, to hand to WireGuard (`wg set ... preshared-key`).
|
||||||
|
#[must_use]
|
||||||
|
pub fn as_bytes(&self) -> &[u8; WIREGUARD_PSK_LEN] {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for WireGuardPsk {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("WireGuardPsk([redacted])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZeroizeOnDrop for WireGuardPsk {}
|
||||||
|
|
||||||
|
/// The initiator's state between sending its ciphertext and receiving the responder's.
|
||||||
|
/// Holds the first-direction secret; zeroised on drop.
|
||||||
|
pub struct Initiation<'a> {
|
||||||
|
me: &'a SecretKey,
|
||||||
|
peer: PublicKey,
|
||||||
|
ct_ir: Ciphertext,
|
||||||
|
ss_ir: SharedSecret,
|
||||||
|
context: Zeroizing<Vec<u8>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Initiation<'_> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("Initiation([redacted])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Initiation<'_> {
|
||||||
|
/// The ciphertext to send to the responder.
|
||||||
|
#[must_use]
|
||||||
|
pub fn ciphertext(&self) -> &Ciphertext {
|
||||||
|
&self.ct_ir
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finish with the responder's ciphertext.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if its X25519 part is a low-order point. A tampered ML-KEM part
|
||||||
|
/// is not reported; it yields a session that does not match the responder's, and the
|
||||||
|
/// WireGuard handshake then fails.
|
||||||
|
pub fn finish(self, ct_ri: &Ciphertext) -> Result<Session, Error> {
|
||||||
|
let ss_ri = self.me.decapsulate(ct_ri)?;
|
||||||
|
let t = Transcript {
|
||||||
|
initiator: self.me.public_key(),
|
||||||
|
responder: &self.peer,
|
||||||
|
ct_ir: &self.ct_ir,
|
||||||
|
ct_ri,
|
||||||
|
context: &self.context,
|
||||||
|
};
|
||||||
|
Ok(Session { prk: session_prk(&self.ss_ir, &ss_ri, &t) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start an exchange as the initiator: encapsulate to the responder and keep the state.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if the responder's X25519 key is a low-order point.
|
||||||
|
pub fn initiate<'a, R: CryptoRng + ?Sized>(
|
||||||
|
me: &'a SecretKey,
|
||||||
|
responder: &PublicKey,
|
||||||
|
context: &[u8],
|
||||||
|
rng: &mut R,
|
||||||
|
) -> Result<Initiation<'a>, Error> {
|
||||||
|
let mut eseed = Zeroizing::new([0u8; ENCAPSULATION_SEED_LEN]);
|
||||||
|
rng.fill_bytes(eseed.as_mut_slice());
|
||||||
|
initiate_inner(me, responder, context, &eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Answer an exchange as the responder: decapsulate the initiator's ciphertext, encapsulate back,
|
||||||
|
/// and return the ciphertext to send together with the finished session.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if the initiator's ciphertext or public key has a low-order X25519
|
||||||
|
/// part.
|
||||||
|
pub fn respond<R: CryptoRng + ?Sized>(
|
||||||
|
me: &SecretKey,
|
||||||
|
initiator: &PublicKey,
|
||||||
|
ct_ir: &Ciphertext,
|
||||||
|
context: &[u8],
|
||||||
|
rng: &mut R,
|
||||||
|
) -> Result<(Ciphertext, Session), Error> {
|
||||||
|
let mut eseed = Zeroizing::new([0u8; ENCAPSULATION_SEED_LEN]);
|
||||||
|
rng.fill_bytes(eseed.as_mut_slice());
|
||||||
|
respond_inner(me, initiator, ct_ir, context, &eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [`initiate`] with explicit randomness. **Hazmat** (feature `hazmat`), for known-answer tests:
|
||||||
|
/// reusing an `eseed` reuses the secret.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// As [`initiate`].
|
||||||
|
#[cfg(feature = "hazmat")]
|
||||||
|
pub fn initiate_derand<'a>(
|
||||||
|
me: &'a SecretKey,
|
||||||
|
responder: &PublicKey,
|
||||||
|
context: &[u8],
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<Initiation<'a>, Error> {
|
||||||
|
initiate_inner(me, responder, context, eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [`respond`] with explicit randomness. **Hazmat** (feature `hazmat`), for known-answer tests.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// As [`respond`].
|
||||||
|
#[cfg(feature = "hazmat")]
|
||||||
|
pub fn respond_derand(
|
||||||
|
me: &SecretKey,
|
||||||
|
initiator: &PublicKey,
|
||||||
|
ct_ir: &Ciphertext,
|
||||||
|
context: &[u8],
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<(Ciphertext, Session), Error> {
|
||||||
|
respond_inner(me, initiator, ct_ir, context, eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn initiate_inner<'a>(
|
||||||
|
me: &'a SecretKey,
|
||||||
|
responder: &PublicKey,
|
||||||
|
context: &[u8],
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<Initiation<'a>, Error> {
|
||||||
|
let (ct_ir, ss_ir) = responder.encapsulate_inner(eseed)?;
|
||||||
|
Ok(Initiation { me, peer: responder.clone(), ct_ir, ss_ir, context: Zeroizing::new(context.to_vec()) })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn respond_inner(
|
||||||
|
me: &SecretKey,
|
||||||
|
initiator: &PublicKey,
|
||||||
|
ct_ir: &Ciphertext,
|
||||||
|
context: &[u8],
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<(Ciphertext, Session), Error> {
|
||||||
|
let ss_ir = me.decapsulate(ct_ir)?;
|
||||||
|
let (ct_ri, ss_ri) = initiator.encapsulate_inner(eseed)?;
|
||||||
|
let t = Transcript { initiator, responder: me.public_key(), ct_ir, ct_ri: &ct_ri, context };
|
||||||
|
let session = Session { prk: session_prk(&ss_ir, &ss_ri, &t) };
|
||||||
|
Ok((ct_ri, session))
|
||||||
|
}
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
//! # Arcanum
|
||||||
|
//!
|
||||||
|
//! The post-quantum crypto core of the Domi ecosystem. **Not audited.**
|
||||||
|
//!
|
||||||
|
//! - [`xwing`]: the hybrid KEM ML-KEM-768 + X25519 (X-Wing, draft-connolly-cfrg-xwing-kem-11).
|
||||||
|
//! - [`exchange`]: a mutual exchange between two peers (each encapsulates to the other), bound to
|
||||||
|
//! the full transcript and a context, and the WireGuard pre-shared key it yields per epoch.
|
||||||
|
//! - [`seal`]: a one-shot sealed box to a recipient's hybrid public key.
|
||||||
|
//!
|
||||||
|
//! No primitive is implemented here; they come from `ml-kem`, `x25519-dalek`, `sha3`, `shake`,
|
||||||
|
//! `sha2`, `hkdf` and `chacha20poly1305`. No I/O, no clock: randomness and time are parameters.
|
||||||
|
//! Design, sources and risks: `docs/01_arcanum.md` and `docs/THREAT_MODEL.md`.
|
||||||
|
//!
|
||||||
|
//! The `hazmat` feature exposes the deterministic (`_derand`, `from_seed`) entry points used by
|
||||||
|
//! the known-answer tests. It is off by default and must never be enabled in a release build.
|
||||||
|
#![no_std]
|
||||||
|
#![forbid(unsafe_code)]
|
||||||
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
pub mod exchange;
|
||||||
|
mod schedule;
|
||||||
|
pub mod seal;
|
||||||
|
pub mod xwing;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod binding_tests;
|
||||||
|
|
||||||
|
pub use error::{Error, Input};
|
||||||
|
pub use exchange::{Session, WIREGUARD_PSK_LEN, WireGuardPsk};
|
||||||
|
|
||||||
|
/// The recommended rotation interval in seconds. Rosenpass's responder rekeys every 120 s
|
||||||
|
/// (`REKEY_AFTER_TIME_RESPONDER`), following WireGuard's own two-minute rekey.
|
||||||
|
pub const RECOMMENDED_EPOCH_SECONDS: u64 = 120;
|
||||||
|
|
||||||
|
/// The epoch number at `unix_seconds`, for [`Session::wireguard_psk`]. Arcanum has no clock; the
|
||||||
|
/// caller passes the time.
|
||||||
|
#[must_use]
|
||||||
|
pub const fn epoch_at(unix_seconds: u64) -> u64 {
|
||||||
|
unix_seconds / RECOMMENDED_EPOCH_SECONDS
|
||||||
|
}
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
//! The key schedule: HKDF-SHA-256 (RFC 5869) with versioned `domi/arcanum/v1/...` labels.
|
||||||
|
//!
|
||||||
|
//! X-Wing is used exactly as specified; its combiner binds `ct_X` and `pk_X` and leaves `ct_M` and
|
||||||
|
//! `pk_M` to ML-KEM. The roadmap's decided combiner binds both public keys and both ciphertexts,
|
||||||
|
//! so that is done here, in Arcanum's own derivation (QA decision D-QA-1(a)):
|
||||||
|
//!
|
||||||
|
//! ```text
|
||||||
|
//! exchange: th = SHA-256("domi/arcanum/v1/transcript/exchange"
|
||||||
|
//! || pk_I || pk_R || ct_IR || ct_RI || u64be(len ctx) || ctx)
|
||||||
|
//! prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss_IR || ss_RI || th)
|
||||||
|
//! seal: th = SHA-256("domi/arcanum/v1/transcript/seal" || pk || ct || u64be(len aad) || aad)
|
||||||
|
//! prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss || th)
|
||||||
|
//! keys: okm = HKDF-Expand(prk, label || fixed-width fields)
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! Every hashed field before the context is fixed-width and the context is length-prefixed, so
|
||||||
|
//! the encoding is injective. Roles are bound by position. A v2 schedule gets new labels; these
|
||||||
|
//! are never reused.
|
||||||
|
|
||||||
|
use hkdf::{Hkdf, HkdfExtract};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use zeroize::{Zeroize, Zeroizing};
|
||||||
|
|
||||||
|
use crate::xwing::{Ciphertext, PublicKey, SharedSecret};
|
||||||
|
|
||||||
|
pub(crate) const LABEL_TRANSCRIPT_EXCHANGE: &[u8] = b"domi/arcanum/v1/transcript/exchange";
|
||||||
|
pub(crate) const LABEL_TRANSCRIPT_SEAL: &[u8] = b"domi/arcanum/v1/transcript/seal";
|
||||||
|
pub(crate) const LABEL_EXTRACT: &[u8] = b"domi/arcanum/v1/extract";
|
||||||
|
pub(crate) const LABEL_WIREGUARD_PSK: &[u8] = b"domi/arcanum/v1/wireguard-psk";
|
||||||
|
pub(crate) const LABEL_SEAL_KEY: &[u8] = b"domi/arcanum/v1/seal/key";
|
||||||
|
pub(crate) const LABEL_SEAL_NONCE: &[u8] = b"domi/arcanum/v1/seal/nonce";
|
||||||
|
|
||||||
|
/// Every label, for the prefix-freeness test.
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) const ALL_LABELS: [&[u8]; 6] = [
|
||||||
|
LABEL_TRANSCRIPT_EXCHANGE,
|
||||||
|
LABEL_TRANSCRIPT_SEAL,
|
||||||
|
LABEL_EXTRACT,
|
||||||
|
LABEL_WIREGUARD_PSK,
|
||||||
|
LABEL_SEAL_KEY,
|
||||||
|
LABEL_SEAL_NONCE,
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Pseudo-random key of one exchange or one seal.
|
||||||
|
pub(crate) type Prk = Zeroizing<[u8; 32]>;
|
||||||
|
|
||||||
|
/// The transcript of one mutual exchange, in role order.
|
||||||
|
pub(crate) struct Transcript<'a> {
|
||||||
|
pub(crate) initiator: &'a PublicKey,
|
||||||
|
pub(crate) responder: &'a PublicKey,
|
||||||
|
pub(crate) ct_ir: &'a Ciphertext,
|
||||||
|
pub(crate) ct_ri: &'a Ciphertext,
|
||||||
|
pub(crate) context: &'a [u8],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transcript<'_> {
|
||||||
|
fn hash(&self) -> [u8; 32] {
|
||||||
|
let mut h = Sha256::new();
|
||||||
|
h.update(LABEL_TRANSCRIPT_EXCHANGE);
|
||||||
|
h.update(self.initiator.as_bytes());
|
||||||
|
h.update(self.responder.as_bytes());
|
||||||
|
h.update(self.ct_ir.as_bytes());
|
||||||
|
h.update(self.ct_ri.as_bytes());
|
||||||
|
h.update((self.context.len() as u64).to_be_bytes());
|
||||||
|
h.update(self.context);
|
||||||
|
h.finalize().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract(secrets: &[&[u8; 32]], th: &[u8; 32]) -> Prk {
|
||||||
|
let mut ext = HkdfExtract::<Sha256>::new(Some(LABEL_EXTRACT));
|
||||||
|
for s in secrets {
|
||||||
|
ext.input_ikm(s.as_slice());
|
||||||
|
}
|
||||||
|
ext.input_ikm(th);
|
||||||
|
let (mut prk, _) = ext.finalize();
|
||||||
|
let mut out = Zeroizing::new([0u8; 32]);
|
||||||
|
out.copy_from_slice(prk.as_slice());
|
||||||
|
prk.as_mut_slice().zeroize();
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The PRK of a mutual exchange: both directions' X-Wing secrets and the whole transcript.
|
||||||
|
pub(crate) fn session_prk(ss_ir: &SharedSecret, ss_ri: &SharedSecret, t: &Transcript<'_>) -> Prk {
|
||||||
|
extract(&[ss_ir.as_bytes(), ss_ri.as_bytes()], &t.hash())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The PRK of one sealed box: the X-Wing secret, the recipient key, the ciphertext and the aad.
|
||||||
|
pub(crate) fn seal_prk(ss: &SharedSecret, recipient: &PublicKey, ct: &Ciphertext, aad: &[u8]) -> Prk {
|
||||||
|
let mut h = Sha256::new();
|
||||||
|
h.update(LABEL_TRANSCRIPT_SEAL);
|
||||||
|
h.update(recipient.as_bytes());
|
||||||
|
h.update(ct.as_bytes());
|
||||||
|
h.update((aad.len() as u64).to_be_bytes());
|
||||||
|
h.update(aad);
|
||||||
|
let th: [u8; 32] = h.finalize().into();
|
||||||
|
extract(&[ss.as_bytes()], &th)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// HKDF-Expand to `N` bytes with the info parts concatenated.
|
||||||
|
pub(crate) fn expand<const N: usize>(prk: &Prk, info: &[&[u8]]) -> Zeroizing<[u8; N]> {
|
||||||
|
// Infallible: a 32-byte PRK is the SHA-256 output size, and N <= 32 is far below 255 * 32.
|
||||||
|
let hk = Hkdf::<Sha256>::from_prk(prk.as_slice()).expect("PRK is 32 bytes");
|
||||||
|
let mut out = Zeroizing::new([0u8; N]);
|
||||||
|
hk.expand_multi_info(info, out.as_mut_slice()).expect("N is at most 255 * 32");
|
||||||
|
out
|
||||||
|
}
|
||||||
+121
@@ -0,0 +1,121 @@
|
|||||||
|
//! One-shot sealed box to a recipient's hybrid public key, for Custos sealing and Tabellarius.
|
||||||
|
//!
|
||||||
|
//! ```text
|
||||||
|
//! sealed = 0x01 || ct (1120 B) || ChaCha20-Poly1305(key, nonce, plaintext, aad = 0x01) || tag
|
||||||
|
//! th = SHA-256("domi/arcanum/v1/transcript/seal" || pk || ct || u64be(len aad) || aad)
|
||||||
|
//! prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss || th)
|
||||||
|
//! key = HKDF-Expand(prk, "domi/arcanum/v1/seal/key", 32)
|
||||||
|
//! nonce = HKDF-Expand(prk, "domi/arcanum/v1/seal/nonce", 12)
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! Every seal runs a fresh encapsulation, so a key and nonce pair is never used twice.
|
||||||
|
//!
|
||||||
|
//! **Anyone who knows the recipient's public key can seal.** There is no sender authentication,
|
||||||
|
//! and ChaCha20-Poly1305 is not key-committing: a successful [`open`] says nothing about who
|
||||||
|
//! sealed. This is a documented construction, not HPKE (RFC 9180).
|
||||||
|
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
use chacha20poly1305::aead::AeadInOut;
|
||||||
|
use chacha20poly1305::{ChaCha20Poly1305, KeyInit, Nonce};
|
||||||
|
use rand_core::CryptoRng;
|
||||||
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
|
use crate::error::{Error, Input};
|
||||||
|
use crate::schedule::{LABEL_SEAL_KEY, LABEL_SEAL_NONCE, Prk, expand, seal_prk};
|
||||||
|
use crate::xwing::{CIPHERTEXT_LEN, Ciphertext, ENCAPSULATION_SEED_LEN, PublicKey, SecretKey};
|
||||||
|
|
||||||
|
/// The format version written as the first byte.
|
||||||
|
pub const VERSION: u8 = 1;
|
||||||
|
const TAG_LEN: usize = 16;
|
||||||
|
const HEADER_LEN: usize = 1 + CIPHERTEXT_LEN;
|
||||||
|
/// Bytes a sealed box adds to the plaintext: version, ciphertext and the Poly1305 tag.
|
||||||
|
pub const OVERHEAD: usize = HEADER_LEN + TAG_LEN;
|
||||||
|
|
||||||
|
fn cipher(prk: &Prk) -> (ChaCha20Poly1305, Nonce) {
|
||||||
|
let key = expand::<32>(prk, &[LABEL_SEAL_KEY]);
|
||||||
|
let nonce = expand::<12>(prk, &[LABEL_SEAL_NONCE]);
|
||||||
|
// Infallible: the key is exactly 32 bytes.
|
||||||
|
let aead = ChaCha20Poly1305::new_from_slice(key.as_slice()).expect("32-byte key");
|
||||||
|
(aead, Nonce::from(*nonce))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Seal `plaintext` to `recipient`, bound to `aad` (which is not encrypted and must be given again
|
||||||
|
/// to open).
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if the recipient's X25519 key is a low-order point.
|
||||||
|
pub fn seal<R: CryptoRng + ?Sized>(
|
||||||
|
recipient: &PublicKey,
|
||||||
|
plaintext: &[u8],
|
||||||
|
aad: &[u8],
|
||||||
|
rng: &mut R,
|
||||||
|
) -> Result<Vec<u8>, Error> {
|
||||||
|
let mut eseed = Zeroizing::new([0u8; ENCAPSULATION_SEED_LEN]);
|
||||||
|
rng.fill_bytes(eseed.as_mut_slice());
|
||||||
|
seal_inner(recipient, plaintext, aad, &eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [`seal`] with explicit randomness. **Hazmat** (feature `hazmat`), for tests: reusing an
|
||||||
|
/// `eseed` reuses key and nonce.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// As [`seal`].
|
||||||
|
#[cfg(feature = "hazmat")]
|
||||||
|
pub fn seal_derand(
|
||||||
|
recipient: &PublicKey,
|
||||||
|
plaintext: &[u8],
|
||||||
|
aad: &[u8],
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<Vec<u8>, Error> {
|
||||||
|
seal_inner(recipient, plaintext, aad, eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn seal_inner(
|
||||||
|
recipient: &PublicKey,
|
||||||
|
plaintext: &[u8],
|
||||||
|
aad: &[u8],
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<Vec<u8>, Error> {
|
||||||
|
let (ct, ss) = recipient.encapsulate_inner(eseed)?;
|
||||||
|
let (aead, nonce) = cipher(&seal_prk(&ss, recipient, &ct, aad));
|
||||||
|
|
||||||
|
// Allocated once at its final size, so no buffer that held plaintext is ever reallocated.
|
||||||
|
let mut out = Vec::with_capacity(plaintext.len() + OVERHEAD);
|
||||||
|
out.push(VERSION);
|
||||||
|
out.extend_from_slice(ct.as_bytes());
|
||||||
|
out.extend_from_slice(plaintext);
|
||||||
|
// ChaCha20-Poly1305 refuses only messages over 2^38 bytes; that is reported, not a panic.
|
||||||
|
let tag = aead
|
||||||
|
.encrypt_inout_detached(&nonce, &[VERSION], (&mut out[HEADER_LEN..]).into())
|
||||||
|
.map_err(|_| Error::MessageTooLong)?;
|
||||||
|
out.extend_from_slice(tag.as_slice());
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Open a sealed box with the recipient's secret key and the same `aad`.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::TooShort`], [`Error::UnsupportedVersion`], [`Error::NonContributory`], or
|
||||||
|
/// [`Error::DecryptionFailed`] for a wrong key, wrong `aad`, or any tampering.
|
||||||
|
pub fn open(recipient: &SecretKey, sealed: &[u8], aad: &[u8]) -> Result<Zeroizing<Vec<u8>>, Error> {
|
||||||
|
if sealed.len() < OVERHEAD {
|
||||||
|
return Err(Error::TooShort { input: Input::SealedBox, minimum: OVERHEAD, actual: sealed.len() });
|
||||||
|
}
|
||||||
|
let (header, rest) = sealed.split_at(HEADER_LEN);
|
||||||
|
let (&version, ct) = header.split_first().ok_or(Error::DecryptionFailed)?;
|
||||||
|
if version != VERSION {
|
||||||
|
return Err(Error::UnsupportedVersion(version));
|
||||||
|
}
|
||||||
|
let ct = Ciphertext::from_bytes(ct)?;
|
||||||
|
let ss = recipient.decapsulate(&ct)?;
|
||||||
|
let (aead, nonce) = cipher(&seal_prk(&ss, recipient.public_key(), &ct, aad));
|
||||||
|
|
||||||
|
let (body, tag) = rest.split_at(rest.len() - TAG_LEN);
|
||||||
|
let tag = chacha20poly1305::Tag::try_from(tag).map_err(|_| Error::DecryptionFailed)?;
|
||||||
|
// The tag is checked before anything is decrypted; on failure the buffer holds only ciphertext.
|
||||||
|
let mut plaintext = Zeroizing::new(body.to_vec());
|
||||||
|
aead.decrypt_inout_detached(&nonce, &[VERSION], plaintext.as_mut_slice().into(), &tag)
|
||||||
|
.map_err(|_| Error::DecryptionFailed)?;
|
||||||
|
Ok(plaintext)
|
||||||
|
}
|
||||||
+304
@@ -0,0 +1,304 @@
|
|||||||
|
//! X-Wing: the hybrid KEM ML-KEM-768 + X25519, as specified in draft-connolly-cfrg-xwing-kem-11.
|
||||||
|
//!
|
||||||
|
//! This module is glue between vetted primitives: ML-KEM-768 from `ml-kem` (FIPS 203), X25519 from
|
||||||
|
//! `x25519-dalek` (RFC 7748), SHAKE256 from `shake` and SHA3-256 from `sha3` (FIPS 202). It is pinned
|
||||||
|
//! by the draft's three test vectors and a differential test against RustCrypto's `x-wing` crate.
|
||||||
|
//!
|
||||||
|
//! One deliberate difference from the draft: a low-order X25519 public key or ciphertext part is
|
||||||
|
//! refused with [`Error::NonContributory`]. Honest keys and ciphertexts are never low-order, so no
|
||||||
|
//! honest exchange and no official vector is affected (docs/01_arcanum.md).
|
||||||
|
|
||||||
|
use core::{array, fmt};
|
||||||
|
|
||||||
|
use ml_kem::array::Array;
|
||||||
|
use ml_kem::{B32, Decapsulate, KeyExport, MlKem768, Seed};
|
||||||
|
use rand_core::CryptoRng;
|
||||||
|
use sha3::{Digest, Sha3_256};
|
||||||
|
use shake::{ExtendableOutput, Shake256, Update, XofReader};
|
||||||
|
use x25519_dalek::{PublicKey as X25519Public, StaticSecret};
|
||||||
|
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
||||||
|
|
||||||
|
use crate::error::{Error, Input, exact};
|
||||||
|
|
||||||
|
type MlKemEncapsulationKey = ml_kem::EncapsulationKey<MlKem768>;
|
||||||
|
type MlKemDecapsulationKey = ml_kem::DecapsulationKey<MlKem768>;
|
||||||
|
|
||||||
|
/// Length of a hybrid public key: ML-KEM-768 encapsulation key (1184) + X25519 public key (32).
|
||||||
|
pub const PUBLIC_KEY_LEN: usize = 1216;
|
||||||
|
/// Length of a hybrid secret key: a 32-byte seed, expanded with SHAKE256.
|
||||||
|
pub const SECRET_KEY_LEN: usize = 32;
|
||||||
|
/// Length of a hybrid ciphertext: ML-KEM-768 ciphertext (1088) + X25519 ephemeral public key (32).
|
||||||
|
pub const CIPHERTEXT_LEN: usize = 1120;
|
||||||
|
/// Length of the X-Wing shared secret.
|
||||||
|
pub const SHARED_SECRET_LEN: usize = 32;
|
||||||
|
/// Length of the encapsulation randomness: 32 bytes for ML-KEM, 32 for the X25519 ephemeral key.
|
||||||
|
pub const ENCAPSULATION_SEED_LEN: usize = 64;
|
||||||
|
|
||||||
|
const ML_KEM_PUBLIC_KEY_LEN: usize = 1184;
|
||||||
|
const ML_KEM_CIPHERTEXT_LEN: usize = 1088;
|
||||||
|
|
||||||
|
/// The X-Wing label, ASCII `\.//^\` (hex 5c2e2f2f5e5c), draft -11 §5.3.
|
||||||
|
const XWING_LABEL: &[u8; 6] = br"\.//^\";
|
||||||
|
|
||||||
|
/// X-Wing Combiner (draft -11 §5.3): SHA3-256(ss_M || ss_X || ct_X || pk_X || XWingLabel).
|
||||||
|
pub(crate) fn combiner(ss_m: &[u8; 32], ss_x: &[u8; 32], ct_x: &[u8; 32], pk_x: &[u8; 32]) -> [u8; SHARED_SECRET_LEN] {
|
||||||
|
let mut h = Sha3_256::new();
|
||||||
|
Digest::update(&mut h, ss_m);
|
||||||
|
Digest::update(&mut h, ss_x);
|
||||||
|
Digest::update(&mut h, ct_x);
|
||||||
|
Digest::update(&mut h, pk_x);
|
||||||
|
Digest::update(&mut h, XWING_LABEL);
|
||||||
|
h.finalize().into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The X-Wing shared secret. Zeroised on drop; never printed.
|
||||||
|
pub struct SharedSecret(pub(crate) Zeroizing<[u8; SHARED_SECRET_LEN]>);
|
||||||
|
|
||||||
|
impl SharedSecret {
|
||||||
|
/// The raw 32 bytes, for interoperating with other X-Wing users. Prefer [`crate::exchange`],
|
||||||
|
/// which binds the secret to the transcript and a context.
|
||||||
|
#[must_use]
|
||||||
|
pub fn as_bytes(&self) -> &[u8; SHARED_SECRET_LEN] {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for SharedSecret {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("SharedSecret([redacted])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ZeroizeOnDrop for SharedSecret {}
|
||||||
|
|
||||||
|
/// A hybrid public key (`pk_M || pk_X`), validated on parse.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct PublicKey {
|
||||||
|
bytes: [u8; PUBLIC_KEY_LEN],
|
||||||
|
pk_m: MlKemEncapsulationKey,
|
||||||
|
pk_x: X25519Public,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PublicKey {
|
||||||
|
/// Parse a hybrid public key received from somewhere else.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::InvalidLength`] if it is not 1216 bytes; [`Error::InvalidPublicKey`] if the ML-KEM
|
||||||
|
/// part fails the FIPS 203 §7.2 encapsulation key check.
|
||||||
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
|
let bytes: &[u8; PUBLIC_KEY_LEN] = exact(Input::PublicKey, bytes)?;
|
||||||
|
let m: [u8; ML_KEM_PUBLIC_KEY_LEN] = array::from_fn(|i| bytes[i]);
|
||||||
|
let pk_m = MlKemEncapsulationKey::new(&Array::from(m)).map_err(|_| Error::InvalidPublicKey)?;
|
||||||
|
let x: [u8; 32] = array::from_fn(|i| bytes[ML_KEM_PUBLIC_KEY_LEN + i]);
|
||||||
|
Ok(Self { bytes: *bytes, pk_m, pk_x: X25519Public::from(x) })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The 1216-byte encoding.
|
||||||
|
#[must_use]
|
||||||
|
pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LEN] {
|
||||||
|
&self.bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encapsulate a fresh shared secret to this key.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if the X25519 part of the key is a low-order point.
|
||||||
|
pub fn encapsulate<R: CryptoRng + ?Sized>(&self, rng: &mut R) -> Result<(Ciphertext, SharedSecret), Error> {
|
||||||
|
let mut eseed = Zeroizing::new([0u8; ENCAPSULATION_SEED_LEN]);
|
||||||
|
rng.fill_bytes(eseed.as_mut_slice());
|
||||||
|
self.encapsulate_inner(&eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// X-Wing `EncapsulateDerand` (draft -11 §5.4.1): the randomness is given explicitly.
|
||||||
|
///
|
||||||
|
/// **Hazmat** (feature `hazmat`). For known-answer tests. Using the same `eseed` twice gives
|
||||||
|
/// the same secret to anyone who sees both ciphertexts. Use [`PublicKey::encapsulate`].
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if the X25519 part of the key is a low-order point.
|
||||||
|
#[cfg(feature = "hazmat")]
|
||||||
|
pub fn encapsulate_derand(
|
||||||
|
&self,
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<(Ciphertext, SharedSecret), Error> {
|
||||||
|
self.encapsulate_inner(eseed)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn encapsulate_inner(
|
||||||
|
&self,
|
||||||
|
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||||
|
) -> Result<(Ciphertext, SharedSecret), Error> {
|
||||||
|
let mut m = B32::from(array::from_fn::<u8, 32, _>(|i| eseed[i]));
|
||||||
|
let (ct_m, mut ss_m) = self.pk_m.encapsulate_deterministic(&m);
|
||||||
|
m.as_mut_slice().zeroize();
|
||||||
|
|
||||||
|
let ek_x: Zeroizing<[u8; 32]> = Zeroizing::new(array::from_fn(|i| eseed[32 + i]));
|
||||||
|
let ek_x = StaticSecret::from(*ek_x);
|
||||||
|
let ct_x = X25519Public::from(&ek_x);
|
||||||
|
let ss_x = ek_x.diffie_hellman(&self.pk_x);
|
||||||
|
drop(ek_x);
|
||||||
|
|
||||||
|
let result = if ss_x.was_contributory() {
|
||||||
|
let ss_m_arr = as32(&ss_m);
|
||||||
|
Ok(combiner(&ss_m_arr, ss_x.as_bytes(), ct_x.as_bytes(), self.pk_x.as_bytes()))
|
||||||
|
} else {
|
||||||
|
Err(Error::NonContributory)
|
||||||
|
};
|
||||||
|
ss_m.as_mut_slice().zeroize();
|
||||||
|
let ss = result?;
|
||||||
|
|
||||||
|
let mut ct = [0u8; CIPHERTEXT_LEN];
|
||||||
|
ct[..ML_KEM_CIPHERTEXT_LEN].copy_from_slice(ct_m.as_slice());
|
||||||
|
ct[ML_KEM_CIPHERTEXT_LEN..].copy_from_slice(ct_x.as_bytes());
|
||||||
|
Ok((Ciphertext(ct), SharedSecret(Zeroizing::new(ss))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for PublicKey {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.bytes == other.bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for PublicKey {}
|
||||||
|
|
||||||
|
impl fmt::Debug for PublicKey {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("PublicKey").field("len", &PUBLIC_KEY_LEN).finish_non_exhaustive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A hybrid ciphertext (`ct_M || ct_X`). Any 1120 bytes parse; a tampered ML-KEM part is handled
|
||||||
|
/// by implicit rejection, a low-order X25519 part is refused at decapsulation.
|
||||||
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
|
pub struct Ciphertext([u8; CIPHERTEXT_LEN]);
|
||||||
|
|
||||||
|
impl Ciphertext {
|
||||||
|
/// Parse a ciphertext.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::InvalidLength`] if it is not 1120 bytes.
|
||||||
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
|
exact(Input::Ciphertext, bytes).map(|b| Self(*b))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The 1120-byte encoding.
|
||||||
|
#[must_use]
|
||||||
|
pub fn as_bytes(&self) -> &[u8; CIPHERTEXT_LEN] {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Ciphertext {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("Ciphertext").field("len", &CIPHERTEXT_LEN).finish_non_exhaustive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A hybrid secret key. Stored as its 32-byte seed plus the expanded keys; all zeroised on drop.
|
||||||
|
pub struct SecretKey {
|
||||||
|
seed: Zeroizing<[u8; SECRET_KEY_LEN]>,
|
||||||
|
dk_m: MlKemDecapsulationKey,
|
||||||
|
sk_x: StaticSecret,
|
||||||
|
public: PublicKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SecretKey {
|
||||||
|
/// Generate a new key pair from the caller's RNG.
|
||||||
|
pub fn generate<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
|
||||||
|
let mut seed = Zeroizing::new([0u8; SECRET_KEY_LEN]);
|
||||||
|
rng.fill_bytes(seed.as_mut_slice());
|
||||||
|
Self::expand(seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// X-Wing `GenerateKeyPairDerand` (draft -11 §5.2.1): the key from a given 32-byte seed.
|
||||||
|
/// **Hazmat** (feature `hazmat`), for tests: the caller's copy of `seed` is not wiped. Real
|
||||||
|
/// code uses [`SecretKey::generate`], or [`SecretKey::from_bytes`] on a buffer it zeroises.
|
||||||
|
#[cfg(feature = "hazmat")]
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_seed(seed: [u8; SECRET_KEY_LEN]) -> Self {
|
||||||
|
Self::expand(Zeroizing::new(seed))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a stored secret key (its 32-byte seed).
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::InvalidLength`] if it is not 32 bytes. Every 32-byte value is a valid key.
|
||||||
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
|
let seed: &[u8; SECRET_KEY_LEN] = exact(Input::SecretKey, bytes)?;
|
||||||
|
Ok(Self::expand(Zeroizing::new(*seed)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The 32-byte seed, for storing in Custos or Tessera. Handle with care.
|
||||||
|
#[must_use]
|
||||||
|
pub fn to_seed(&self) -> Zeroizing<[u8; SECRET_KEY_LEN]> {
|
||||||
|
Zeroizing::new(*self.seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The matching public key.
|
||||||
|
#[must_use]
|
||||||
|
pub fn public_key(&self) -> &PublicKey {
|
||||||
|
&self.public
|
||||||
|
}
|
||||||
|
|
||||||
|
/// X-Wing `Decapsulate` (draft -11 §5.5).
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`Error::NonContributory`] if the X25519 part of the ciphertext is a low-order point. A
|
||||||
|
/// tampered ML-KEM part is not an error: ML-KEM's implicit rejection returns an unrelated secret.
|
||||||
|
pub fn decapsulate(&self, ct: &Ciphertext) -> Result<SharedSecret, Error> {
|
||||||
|
let ct_x: [u8; 32] = array::from_fn(|i| ct.0[ML_KEM_CIPHERTEXT_LEN + i]);
|
||||||
|
let ss_x = self.sk_x.diffie_hellman(&X25519Public::from(ct_x));
|
||||||
|
if !ss_x.was_contributory() {
|
||||||
|
return Err(Error::NonContributory);
|
||||||
|
}
|
||||||
|
let ct_m: [u8; ML_KEM_CIPHERTEXT_LEN] = array::from_fn(|i| ct.0[i]);
|
||||||
|
let mut ss_m = self.dk_m.decapsulate(&Array::from(ct_m));
|
||||||
|
let ss_m_arr = as32(&ss_m);
|
||||||
|
let ss = combiner(&ss_m_arr, ss_x.as_bytes(), &ct_x, self.public.pk_x.as_bytes());
|
||||||
|
ss_m.as_mut_slice().zeroize();
|
||||||
|
Ok(SharedSecret(Zeroizing::new(ss)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `expandDecapsulationKey` (draft -11 §5.2): SHAKE256(seed) -> d || z || sk_X.
|
||||||
|
fn expand(seed: Zeroizing<[u8; SECRET_KEY_LEN]>) -> Self {
|
||||||
|
let mut expanded = Zeroizing::new([0u8; 96]);
|
||||||
|
let mut xof = Shake256::default();
|
||||||
|
Update::update(&mut xof, seed.as_slice());
|
||||||
|
xof.finalize_xof().read(expanded.as_mut_slice());
|
||||||
|
|
||||||
|
let mut ml_seed = Seed::default();
|
||||||
|
ml_seed.as_mut_slice().copy_from_slice(&expanded[..64]);
|
||||||
|
let dk_m = MlKemDecapsulationKey::from_seed(ml_seed);
|
||||||
|
ml_seed.as_mut_slice().zeroize();
|
||||||
|
|
||||||
|
let mut sk_x_bytes = Zeroizing::new([0u8; 32]);
|
||||||
|
sk_x_bytes.copy_from_slice(&expanded[64..]);
|
||||||
|
let sk_x = StaticSecret::from(*sk_x_bytes);
|
||||||
|
let pk_x = X25519Public::from(&sk_x);
|
||||||
|
|
||||||
|
let pk_m = dk_m.encapsulation_key().clone();
|
||||||
|
let mut bytes = [0u8; PUBLIC_KEY_LEN];
|
||||||
|
bytes[..ML_KEM_PUBLIC_KEY_LEN].copy_from_slice(pk_m.to_bytes().as_slice());
|
||||||
|
bytes[ML_KEM_PUBLIC_KEY_LEN..].copy_from_slice(pk_x.as_bytes());
|
||||||
|
|
||||||
|
Self { seed, dk_m, sk_x, public: PublicKey { bytes, pk_m, pk_x } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for SecretKey {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("SecretKey([redacted])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// `seed` is `Zeroizing`, and `ml-kem`'s and `x25519-dalek`'s key types wipe themselves on drop
|
||||||
|
// (their `zeroize` features are enabled in Cargo.toml).
|
||||||
|
impl ZeroizeOnDrop for SecretKey {}
|
||||||
|
|
||||||
|
/// Copy a 32-byte ML-KEM shared key into a stack array that is wiped on drop.
|
||||||
|
fn as32(k: &ml_kem::SharedKey) -> Zeroizing<[u8; 32]> {
|
||||||
|
let mut out = Zeroizing::new([0u8; 32]);
|
||||||
|
out.copy_from_slice(k.as_slice());
|
||||||
|
out
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
//! Mandatory test 3, through the public API: an attacker can force the X25519 half to zero by
|
||||||
|
//! sending a low-order point. Arcanum refuses that with a typed error instead of producing a
|
||||||
|
//! secret that only one half protects (QA A-16, A-17). The combiner-level half of test 3, which
|
||||||
|
//! zeroes each component directly, is `src/binding_tests.rs`. Also QA A-04 (raw ct_X is hashed).
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use arcanum::Error;
|
||||||
|
use arcanum::exchange;
|
||||||
|
use arcanum::xwing::{Ciphertext, PublicKey, SecretKey};
|
||||||
|
use common::rng;
|
||||||
|
|
||||||
|
/// Curve25519 u-coordinates of small order and non-canonical encodings of them (libsodium's
|
||||||
|
/// blocklist). Each is also used with bit 255 set, which X25519 ignores (RFC 7748 §5).
|
||||||
|
const LOW_ORDER: [&str; 7] = [
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
|
"0100000000000000000000000000000000000000000000000000000000000000",
|
||||||
|
"e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800",
|
||||||
|
"5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157",
|
||||||
|
"ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
||||||
|
"edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
||||||
|
"eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
||||||
|
];
|
||||||
|
|
||||||
|
fn low_order_points() -> Vec<[u8; 32]> {
|
||||||
|
LOW_ORDER
|
||||||
|
.iter()
|
||||||
|
.flat_map(|h| {
|
||||||
|
let p: [u8; 32] = hex::decode(h).unwrap().try_into().unwrap();
|
||||||
|
let mut high = p;
|
||||||
|
high[31] |= 0x80;
|
||||||
|
[p, high]
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Guards the fixture: if a byte above were mistyped, the point would not be low order.
|
||||||
|
#[test]
|
||||||
|
fn the_fixture_points_really_are_low_order() {
|
||||||
|
let s = x25519_dalek::StaticSecret::from([0x5a; 32]);
|
||||||
|
for p in low_order_points() {
|
||||||
|
assert!(!s.diffie_hellman(&p.into()).was_contributory(), "{}", hex::encode(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn low_order_x25519_ciphertext_is_refused() {
|
||||||
|
let i = SecretKey::from_seed([0x43; 32]);
|
||||||
|
let r = SecretKey::from_seed([0x44; 32]);
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||||
|
let (ct_ri, _) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), b"ctx", &[0x56; 64]).unwrap();
|
||||||
|
for point in low_order_points() {
|
||||||
|
let mut bad = *init.ciphertext().as_bytes();
|
||||||
|
bad[1088..].copy_from_slice(&point);
|
||||||
|
let bad = Ciphertext::from_bytes(&bad).unwrap();
|
||||||
|
assert_eq!(r.decapsulate(&bad).err(), Some(Error::NonContributory));
|
||||||
|
let resp = exchange::respond_derand(&r, i.public_key(), &bad, b"ctx", &[0x56; 64]);
|
||||||
|
assert_eq!(resp.err(), Some(Error::NonContributory));
|
||||||
|
|
||||||
|
let mut bad = *ct_ri.as_bytes();
|
||||||
|
bad[1088..].copy_from_slice(&point);
|
||||||
|
let again = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||||
|
assert_eq!(again.finish(&Ciphertext::from_bytes(&bad).unwrap()).err(), Some(Error::NonContributory));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn low_order_x25519_public_key_is_refused() {
|
||||||
|
let i = SecretKey::from_seed([0x43; 32]);
|
||||||
|
let r = SecretKey::from_seed([0x44; 32]);
|
||||||
|
for point in low_order_points() {
|
||||||
|
let mut bad = *r.public_key().as_bytes();
|
||||||
|
bad[1184..].copy_from_slice(&point);
|
||||||
|
let pk = PublicKey::from_bytes(&bad).expect("the ML-KEM part is still valid");
|
||||||
|
assert_eq!(pk.encapsulate_derand(&[0x55; 64]).err(), Some(Error::NonContributory));
|
||||||
|
assert_eq!(exchange::initiate_derand(&i, &pk, b"ctx", &[0x55; 64]).err(), Some(Error::NonContributory));
|
||||||
|
assert_eq!(arcanum::seal::seal(&pk, b"x", b"", &mut rng(1)).err(), Some(Error::NonContributory));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-04: X25519 ignores bit 255 of ct_X, so ct_X and ct_X ^ 0x80 give the same ss_X. The
|
||||||
|
/// output must still differ, because the combiner hashes the raw bytes.
|
||||||
|
#[test]
|
||||||
|
fn the_high_bit_of_ct_x_is_bound() {
|
||||||
|
let r = SecretKey::from_seed([0x44; 32]);
|
||||||
|
let (ct, ss) = r.public_key().encapsulate(&mut rng(4)).unwrap();
|
||||||
|
let mut b = *ct.as_bytes();
|
||||||
|
b[1119] ^= 0x80;
|
||||||
|
let out = r.decapsulate(&Ciphertext::from_bytes(&b).unwrap()).unwrap();
|
||||||
|
assert_ne!(out.as_bytes(), ss.as_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The honest exchange is not affected by the check.
|
||||||
|
#[test]
|
||||||
|
fn honest_points_pass_the_check() {
|
||||||
|
let i = SecretKey::from_seed([0x43; 32]);
|
||||||
|
let r = SecretKey::from_seed([0x44; 32]);
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||||
|
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), b"ctx", &[0x56; 64]).unwrap();
|
||||||
|
assert_eq!(init.finish(&ct_ri).unwrap().wireguard_psk(3).as_bytes(), rs.wireguard_psk(3).as_bytes());
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
//! Helpers shared by the integration tests. Test fixtures only: no real keys, no network.
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use chacha20::ChaCha20Rng;
|
||||||
|
use rand_core::SeedableRng;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
/// A deterministic RNG for tests. Never used outside tests.
|
||||||
|
pub fn rng(label: u8) -> ChaCha20Rng {
|
||||||
|
ChaCha20Rng::from_seed([label; 32])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hex(s: &str) -> Vec<u8> {
|
||||||
|
hex::decode(s).expect("vector hex")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arr<const N: usize>(s: &str) -> [u8; N] {
|
||||||
|
hex(s).try_into().expect("vector length")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fail loudly if a pinned vector file is not the one recorded in tests/vectors/SOURCES.md.
|
||||||
|
pub fn assert_sha256(bytes: &[u8], expected_hex: &str, name: &str) {
|
||||||
|
let got = hex::encode(Sha256::digest(bytes));
|
||||||
|
assert_eq!(got, expected_hex, "{name} is not the pinned file (see tests/vectors/SOURCES.md)");
|
||||||
|
}
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
//! Mandatory test 2: both sides of a hybrid exchange derive the same secret, and changing any
|
||||||
|
//! single input byte changes it. Also QA A-14 (roles bound) and A-15 (both directions needed).
|
||||||
|
//!
|
||||||
|
//! The exchange is mutual (each peer encapsulates to the other, docs/01_arcanum.md). Its inputs
|
||||||
|
//! are: both peers' seeds, both encapsulation seeds, both public keys (as the other side received
|
||||||
|
//! them), both ciphertexts (as the other side received them), and the context. Every byte of the
|
||||||
|
//! public keys, the ciphertexts, the seeds and the context is flipped in turn.
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use arcanum::exchange::{self, Session};
|
||||||
|
use arcanum::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SecretKey};
|
||||||
|
use common::rng;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
const SEED_I: [u8; 32] = [0x11; 32];
|
||||||
|
const SEED_R: [u8; 32] = [0x12; 32];
|
||||||
|
const ESEED_I: [u8; 64] = [0x21; 64];
|
||||||
|
const ESEED_R: [u8; 64] = [0x22; 64];
|
||||||
|
const CONTEXT: &[u8] = b"via/test: device-a <-> device-b";
|
||||||
|
|
||||||
|
fn psk(session: &Session) -> [u8; 32] {
|
||||||
|
*session.wireguard_psk(0).as_bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The honest exchange: (PSK, ct_IR, ct_RI).
|
||||||
|
fn honest() -> ([u8; 32], Ciphertext, Ciphertext) {
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &ESEED_I).unwrap();
|
||||||
|
let ct_ir = init.ciphertext().clone();
|
||||||
|
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), &ct_ir, CONTEXT, &ESEED_R).unwrap();
|
||||||
|
let is = init.finish(&ct_ri).unwrap();
|
||||||
|
assert_eq!(psk(&is), psk(&rs));
|
||||||
|
(psk(&rs), ct_ir, ct_ri)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn both_sides_derive_the_same_secret() {
|
||||||
|
let mut g = rng(1);
|
||||||
|
for _ in 0..32 {
|
||||||
|
let i = SecretKey::generate(&mut g);
|
||||||
|
let r = SecretKey::generate(&mut g);
|
||||||
|
let init = exchange::initiate(&i, r.public_key(), CONTEXT, &mut g).unwrap();
|
||||||
|
let (ct_ri, rs) = exchange::respond(&r, i.public_key(), init.ciphertext(), CONTEXT, &mut g).unwrap();
|
||||||
|
let is = init.finish(&ct_ri).unwrap();
|
||||||
|
for epoch in [0, 1, 7, u64::MAX] {
|
||||||
|
assert_eq!(is.wireguard_psk(epoch).as_bytes(), rs.wireguard_psk(epoch).as_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn both_sides_agree_after_keys_and_ciphertexts_go_through_bytes() {
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let r_pk = PublicKey::from_bytes(r.public_key().as_bytes()).unwrap();
|
||||||
|
let i_pk = PublicKey::from_bytes(i.public_key().as_bytes()).unwrap();
|
||||||
|
let init = exchange::initiate_derand(&i, &r_pk, CONTEXT, &ESEED_I).unwrap();
|
||||||
|
let ct_ir = Ciphertext::from_bytes(init.ciphertext().as_bytes()).unwrap();
|
||||||
|
let (ct_ri, rs) = exchange::respond_derand(&r, &i_pk, &ct_ir, CONTEXT, &ESEED_R).unwrap();
|
||||||
|
let ct_ri = Ciphertext::from_bytes(ct_ri.as_bytes()).unwrap();
|
||||||
|
assert_eq!(psk(&init.finish(&ct_ri).unwrap()), psk(&rs));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fresh_randomness_gives_a_fresh_secret() {
|
||||||
|
let mut g = rng(2);
|
||||||
|
let i = SecretKey::generate(&mut g);
|
||||||
|
let r = SecretKey::generate(&mut g);
|
||||||
|
let mut seen = HashSet::new();
|
||||||
|
for _ in 0..64 {
|
||||||
|
let init = exchange::initiate(&i, r.public_key(), CONTEXT, &mut g).unwrap();
|
||||||
|
let (_, rs) = exchange::respond(&r, i.public_key(), init.ciphertext(), CONTEXT, &mut g).unwrap();
|
||||||
|
assert!(seen.insert(psk(&rs)), "two exchanges produced the same secret");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Flip each byte of ct_IR as the responder receives it: the responder's secret changes.
|
||||||
|
#[test]
|
||||||
|
fn every_byte_of_the_initiators_ciphertext_changes_the_secret() {
|
||||||
|
let (honest_psk, ct_ir, _) = honest();
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let mut accepted = 0;
|
||||||
|
for n in 0..CIPHERTEXT_LEN {
|
||||||
|
let mut bad = *ct_ir.as_bytes();
|
||||||
|
bad[n] ^= 0x01;
|
||||||
|
let bad = Ciphertext::from_bytes(&bad).unwrap();
|
||||||
|
// A refusal (a low-order X25519 value) is also "not the honest secret".
|
||||||
|
if let Ok((_, s)) = exchange::respond_derand(&r, i.public_key(), &bad, CONTEXT, &ESEED_R) {
|
||||||
|
assert_ne!(psk(&s), honest_psk, "ct_IR byte {n} did not change the secret");
|
||||||
|
accepted += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(accepted > CIPHERTEXT_LEN - 32, "flipped ciphertexts must mostly decapsulate (implicit rejection)");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Flip each byte of ct_RI as the initiator receives it: the initiator's secret changes.
|
||||||
|
#[test]
|
||||||
|
fn every_byte_of_the_responders_ciphertext_changes_the_secret() {
|
||||||
|
let (honest_psk, _, ct_ri) = honest();
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let mut accepted = 0;
|
||||||
|
for n in 0..CIPHERTEXT_LEN {
|
||||||
|
let mut bad = *ct_ri.as_bytes();
|
||||||
|
bad[n] ^= 0x01;
|
||||||
|
let bad = Ciphertext::from_bytes(&bad).unwrap();
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &ESEED_I).unwrap();
|
||||||
|
if let Ok(s) = init.finish(&bad) {
|
||||||
|
assert_ne!(psk(&s), honest_psk, "ct_RI byte {n} did not change the secret");
|
||||||
|
accepted += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(accepted > CIPHERTEXT_LEN - 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Flip each byte of the responder's public key as the initiator received it, and of the
|
||||||
|
/// initiator's public key as the responder received it.
|
||||||
|
#[test]
|
||||||
|
fn every_public_key_byte_changes_the_secret() {
|
||||||
|
let (honest_psk, ct_ir, _) = honest();
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let mut used = 0;
|
||||||
|
for n in 0..PUBLIC_KEY_LEN {
|
||||||
|
// The initiator's view of the responder's key.
|
||||||
|
let mut bad = *r.public_key().as_bytes();
|
||||||
|
bad[n] ^= 0x01;
|
||||||
|
// A flipped ML-KEM coefficient can fail the FIPS 203 modulus check: refused, not accepted.
|
||||||
|
if let Ok(pk) = PublicKey::from_bytes(&bad) {
|
||||||
|
let init = exchange::initiate_derand(&i, &pk, CONTEXT, &ESEED_I).unwrap();
|
||||||
|
let (ct_ri, rs) =
|
||||||
|
exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &ESEED_R).unwrap();
|
||||||
|
let is = init.finish(&ct_ri).unwrap();
|
||||||
|
assert_ne!(psk(&is), honest_psk, "initiator: responder key byte {n}");
|
||||||
|
assert_ne!(psk(&is), psk(&rs), "initiator: responder key byte {n} still agrees");
|
||||||
|
used += 1;
|
||||||
|
}
|
||||||
|
// The responder's view of the initiator's key.
|
||||||
|
let mut bad = *i.public_key().as_bytes();
|
||||||
|
bad[n] ^= 0x01;
|
||||||
|
if let Ok(pk) = PublicKey::from_bytes(&bad) {
|
||||||
|
let (_, rs) = exchange::respond_derand(&r, &pk, &ct_ir, CONTEXT, &ESEED_R).unwrap();
|
||||||
|
assert_ne!(psk(&rs), honest_psk, "responder: initiator key byte {n}");
|
||||||
|
used += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(used > PUBLIC_KEY_LEN, "most flipped keys still parse; the test must exercise them");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn every_seed_byte_changes_the_secret() {
|
||||||
|
let (honest_psk, _, _) = honest();
|
||||||
|
let run = |si: [u8; 32], sr: [u8; 32], ei: [u8; 64], er: [u8; 64]| {
|
||||||
|
let i = SecretKey::from_seed(si);
|
||||||
|
let r = SecretKey::from_seed(sr);
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &ei).unwrap();
|
||||||
|
let (_, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &er).unwrap();
|
||||||
|
psk(&rs)
|
||||||
|
};
|
||||||
|
for n in 0..32 {
|
||||||
|
let mut s = SEED_I;
|
||||||
|
s[n] ^= 1;
|
||||||
|
assert_ne!(run(s, SEED_R, ESEED_I, ESEED_R), honest_psk, "initiator seed byte {n}");
|
||||||
|
let mut s = SEED_R;
|
||||||
|
s[n] ^= 1;
|
||||||
|
assert_ne!(run(SEED_I, s, ESEED_I, ESEED_R), honest_psk, "responder seed byte {n}");
|
||||||
|
}
|
||||||
|
// Bit 4 of each byte: X25519 clamps bits 0-2 of byte 32 and bits 6-7 of byte 63 of the
|
||||||
|
// ephemeral scalar (RFC 7748 §5), so flipping those bits is, by design, no change at all.
|
||||||
|
for n in 0..64 {
|
||||||
|
let mut e = ESEED_I;
|
||||||
|
e[n] ^= 0x10;
|
||||||
|
assert_ne!(run(SEED_I, SEED_R, e, ESEED_R), honest_psk, "initiator eseed byte {n}");
|
||||||
|
let mut e = ESEED_R;
|
||||||
|
e[n] ^= 0x10;
|
||||||
|
assert_ne!(run(SEED_I, SEED_R, ESEED_I, e), honest_psk, "responder eseed byte {n}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn every_context_byte_changes_the_secret() {
|
||||||
|
let (honest_psk, ct_ir, _) = honest();
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let resp = |ctx: &[u8]| psk(&exchange::respond_derand(&r, i.public_key(), &ct_ir, ctx, &ESEED_R).unwrap().1);
|
||||||
|
for n in 0..CONTEXT.len() {
|
||||||
|
let mut ctx = CONTEXT.to_vec();
|
||||||
|
ctx[n] ^= 0x01;
|
||||||
|
assert_ne!(resp(&ctx), honest_psk, "context byte {n}");
|
||||||
|
}
|
||||||
|
// Length is bound too: a prefix or an extension of the context is a different context.
|
||||||
|
for ctx in [&CONTEXT[..CONTEXT.len() - 1], b"", &[CONTEXT, b"\0"].concat()[..]] {
|
||||||
|
assert_ne!(resp(ctx), honest_psk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-15, through the public API: an attacker who plays the initiator without its secret key
|
||||||
|
/// can send ct_IR, but cannot finish: ct_RI is encapsulated to the real initiator. (The API cannot
|
||||||
|
/// express her full knowledge, so this alone would pass even with a one-direction schedule; the
|
||||||
|
/// test that fails then is `src/binding_tests.rs`, checked by reintroducing that bug.)
|
||||||
|
#[test]
|
||||||
|
fn an_impersonated_initiator_cannot_compute_the_psk() {
|
||||||
|
let i = SecretKey::from_seed(SEED_I); // the real initiator, whom the attacker impersonates
|
||||||
|
let r = SecretKey::from_seed(SEED_R);
|
||||||
|
let mallory = SecretKey::from_seed([0x66; 32]);
|
||||||
|
let mut g = rng(3);
|
||||||
|
|
||||||
|
// Mallory encapsulates to the responder (anyone can) and sends it as "from I".
|
||||||
|
let forged = exchange::initiate(&mallory, r.public_key(), CONTEXT, &mut g).unwrap();
|
||||||
|
let (ct_ri, responder_session) =
|
||||||
|
exchange::respond(&r, i.public_key(), forged.ciphertext(), CONTEXT, &mut g).unwrap();
|
||||||
|
// Her best effort: finish with her own key. ct_RI was made for I's key, not hers.
|
||||||
|
let mallory_session = forged.finish(&ct_ri).unwrap();
|
||||||
|
assert_ne!(psk(&mallory_session), psk(&responder_session));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-14: roles are bound by position. Running the exchange with the roles swapped (same two
|
||||||
|
/// keys, same randomness) gives a different PSK, so a reflected message cannot complete it.
|
||||||
|
#[test]
|
||||||
|
fn roles_are_bound() {
|
||||||
|
let a = SecretKey::from_seed(SEED_I);
|
||||||
|
let b = SecretKey::from_seed(SEED_R);
|
||||||
|
let ab = exchange::initiate_derand(&a, b.public_key(), CONTEXT, &ESEED_I).unwrap();
|
||||||
|
let (ct, ab_resp) = exchange::respond_derand(&b, a.public_key(), ab.ciphertext(), CONTEXT, &ESEED_R).unwrap();
|
||||||
|
let ab_init = ab.finish(&ct).unwrap();
|
||||||
|
let ba = exchange::initiate_derand(&b, a.public_key(), CONTEXT, &ESEED_I).unwrap();
|
||||||
|
let (ct, ba_resp) = exchange::respond_derand(&a, b.public_key(), ba.ciphertext(), CONTEXT, &ESEED_R).unwrap();
|
||||||
|
let ba_init = ba.finish(&ct).unwrap();
|
||||||
|
assert_eq!(psk(&ab_init), psk(&ab_resp));
|
||||||
|
assert_eq!(psk(&ba_init), psk(&ba_resp));
|
||||||
|
assert_ne!(psk(&ab_init), psk(&ba_init));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_wrong_secret_key_does_not_agree() {
|
||||||
|
let (honest_psk, ct_ir, _) = honest();
|
||||||
|
let i = SecretKey::from_seed(SEED_I);
|
||||||
|
let other = SecretKey::from_seed([0x33; 32]);
|
||||||
|
let (_, s) = exchange::respond_derand(&other, i.public_key(), &ct_ir, CONTEXT, &ESEED_R).unwrap();
|
||||||
|
assert_ne!(psk(&s), honest_psk);
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
//! QA A-35: no test-only feature may reach a normal (release) build through Cargo feature
|
||||||
|
//! unification. Checks the normal-edge graph a consumer of Arcanum gets with default features.
|
||||||
|
//!
|
||||||
|
//! `arcanum/hazmat` exposes the deterministic entry points. `ml-kem/hazmat` only un-hides docs in
|
||||||
|
//! 0.3.4 (its `encapsulate_deterministic` is public either way), but is kept out on principle.
|
||||||
|
//! Consumers (via-core, via) must run the same check in their own CI: a `[dependencies]` entry
|
||||||
|
//! there could still enable `arcanum/hazmat`.
|
||||||
|
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
fn tree(args: &[&str]) -> String {
|
||||||
|
let out = Command::new(env!("CARGO"))
|
||||||
|
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||||
|
.args(["tree", "--offline", "--locked", "-e", "features,normal", "--prefix", "none"])
|
||||||
|
.args(args)
|
||||||
|
.output()
|
||||||
|
.expect("run cargo tree");
|
||||||
|
assert!(out.status.success(), "cargo tree failed: {}", String::from_utf8_lossy(&out.stderr));
|
||||||
|
String::from_utf8(out.stdout).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_no_hazmat(t: &str, what: &str) {
|
||||||
|
for line in t.lines() {
|
||||||
|
assert!(!line.contains("feature \"hazmat\""), "{what}: hazmat reached a normal build: {line}\n{t}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn arcanum_default_features_do_not_include_hazmat() {
|
||||||
|
let t = tree(&["-i", "arcanum"]);
|
||||||
|
assert!(t.contains("arcanum feature \"default\""), "the tree must list arcanum's features:\n{t}");
|
||||||
|
assert_no_hazmat(&t, "arcanum");
|
||||||
|
// Prove the check can see the feature at all: asking for it must make it appear.
|
||||||
|
let with = tree(&["-i", "arcanum", "--features", "hazmat"]);
|
||||||
|
assert!(with.contains("arcanum feature \"hazmat\""), "the check is blind:\n{with}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ml_kem_hazmat_is_not_in_the_normal_graph() {
|
||||||
|
let t = tree(&["-i", "ml-kem"]);
|
||||||
|
assert!(t.contains("ml-kem feature \"zeroize\""), "the tree must list ml-kem's features:\n{t}");
|
||||||
|
assert_no_hazmat(&t, "ml-kem");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_os_rng_in_the_normal_graph() {
|
||||||
|
// Randomness is a parameter (QA A-36): nothing in the normal graph may pull an OS RNG.
|
||||||
|
let t = tree(&["-p", "arcanum"]);
|
||||||
|
assert!(t.contains("rand_core"), "the tree must include rand_core:\n{t}");
|
||||||
|
assert!(!t.contains("getrandom"), "getrandom reached a normal build:\n{t}");
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
//! QA A-10 and A-38: the v1 key schedule and the v1 sealed-box format are frozen. These values
|
||||||
|
//! were produced by this implementation (not by an outside source, so they prove stability, not
|
||||||
|
//! correctness; correctness is the KATs' job). If one fails, a label, an encoding or a field order
|
||||||
|
//! changed: every sealed blob Custos stored would stop opening and two peers on different builds
|
||||||
|
//! would silently disagree. Never regenerate these; a v2 adds new ones next to them.
|
||||||
|
|
||||||
|
use arcanum::exchange;
|
||||||
|
use arcanum::seal;
|
||||||
|
use arcanum::xwing::SecretKey;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
const CONTEXT: &[u8] = b"domi/arcanum golden v1";
|
||||||
|
|
||||||
|
fn golden_session() -> exchange::Session {
|
||||||
|
let i = SecretKey::from_seed([0xa0; 32]);
|
||||||
|
let r = SecretKey::from_seed([0xb0; 32]);
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &[0xc0; 64]).unwrap();
|
||||||
|
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &[0xd0; 64]).unwrap();
|
||||||
|
let is = init.finish(&ct_ri).unwrap();
|
||||||
|
assert_eq!(is.wireguard_psk(0).as_bytes(), rs.wireguard_psk(0).as_bytes());
|
||||||
|
rs
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn v1_wireguard_psks_are_frozen() {
|
||||||
|
let s = golden_session();
|
||||||
|
for (epoch, want) in [
|
||||||
|
(0u64, "9fcb41a416cbd9d678e559bebe907cf9e7e483b7d9f7ded6583c871017cbe73a"),
|
||||||
|
(1, "3db1a6ede8d5ccec1faaa420d5a69e1b03ad4592525294d57dd29ae6d3fa8d91"),
|
||||||
|
(1 << 32, "39d30c3a9180d03264cca955a7fbba06ebd6e90b40e409ada57611253f0c042b"),
|
||||||
|
(u64::MAX - 1, "1931400ca68353ecc85fe76a1e01e4cce5f4f99a51d309f776235ab459ea464e"),
|
||||||
|
] {
|
||||||
|
assert_eq!(hex::encode(s.wireguard_psk(epoch).as_bytes()), want, "epoch {epoch}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn v1_sealed_box_is_frozen_and_opens() {
|
||||||
|
let r = SecretKey::from_seed([0xb0; 32]);
|
||||||
|
let sealed = seal::seal_derand(r.public_key(), b"Built on Domi", b"golden aad", &[0xe0; 64]).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
hex::encode(Sha256::digest(&sealed)),
|
||||||
|
"4effac5a1d96cc34d663d5c9873dfb816552389becdfd415a2ca33f244a3018c",
|
||||||
|
"the v1 sealed format changed"
|
||||||
|
);
|
||||||
|
assert_eq!(seal::open(&r, &sealed, b"golden aad").unwrap().as_slice(), b"Built on Domi");
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
//! Mandatory test 1a: ML-KEM-768 known-answer tests from NIST ACVP (FIPS 203).
|
||||||
|
//!
|
||||||
|
//! Runs the vetted `ml-kem` crate, exactly as Arcanum uses it, against the official vectors.
|
||||||
|
//! Provenance and sha256 pins: tests/vectors/SOURCES.md.
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{assert_sha256, hex};
|
||||||
|
#[allow(deprecated)]
|
||||||
|
use ml_kem::ExpandedKeyEncoding;
|
||||||
|
use ml_kem::array::Array;
|
||||||
|
use ml_kem::{B32, Decapsulate, DecapsulationKey, EncapsulationKey, KeyExport, MlKem768, Seed};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
const KEYGEN: &[u8] = include_bytes!("vectors/acvp-ml-kem-keyGen-FIPS203-internalProjection.json");
|
||||||
|
const KEYGEN_SHA256: &str = "d7a62a2c3476957f56dd8d24f9004ea6776ccfe995ffe71a65bb9506dc9c7b1b";
|
||||||
|
const ENCDEC: &[u8] = include_bytes!("vectors/acvp-ml-kem-encapDecap-FIPS203-internalProjection.json");
|
||||||
|
const ENCDEC_SHA256: &str = "a556952ce869bb89c3a3196a701dad89647c193a34c86eafb61a9d710d5b810f";
|
||||||
|
|
||||||
|
/// The ML-KEM-768 test groups for one `function` ("" for keyGen, which has none).
|
||||||
|
fn groups(file: &[u8], function: &str) -> Vec<Value> {
|
||||||
|
let v: Value = serde_json::from_slice(file).expect("ACVP json");
|
||||||
|
v["testGroups"]
|
||||||
|
.as_array()
|
||||||
|
.expect("testGroups")
|
||||||
|
.iter()
|
||||||
|
.filter(|g| g["parameterSet"] == "ML-KEM-768")
|
||||||
|
.filter(|g| g["function"].as_str().unwrap_or("") == function)
|
||||||
|
.cloned()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tests_of(groups: &[Value]) -> Vec<Value> {
|
||||||
|
groups.iter().flat_map(|g| g["tests"].as_array().expect("tests").clone()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn field(t: &Value, name: &str) -> Vec<u8> {
|
||||||
|
hex(t[name].as_str().unwrap_or_else(|| panic!("field {name}")))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ek_from(bytes: &[u8]) -> Result<EncapsulationKey<MlKem768>, ml_kem::InvalidKey> {
|
||||||
|
let arr = Array::try_from(bytes).expect("ek length");
|
||||||
|
EncapsulationKey::<MlKem768>::new(&arr)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(deprecated)]
|
||||||
|
fn dk_from_expanded(bytes: &[u8]) -> Result<DecapsulationKey<MlKem768>, ml_kem::InvalidKey> {
|
||||||
|
let arr = Array::try_from(bytes).expect("dk length");
|
||||||
|
DecapsulationKey::<MlKem768>::from_expanded_bytes(&arr)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vector_files_are_the_pinned_ones() {
|
||||||
|
assert_sha256(KEYGEN, KEYGEN_SHA256, "ACVP keyGen");
|
||||||
|
assert_sha256(ENCDEC, ENCDEC_SHA256, "ACVP encapDecap");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[allow(deprecated)]
|
||||||
|
fn acvp_keygen_768() {
|
||||||
|
let tests = tests_of(&groups(KEYGEN, ""));
|
||||||
|
assert_eq!(tests.len(), 25, "ACVP keyGen ML-KEM-768 has 25 cases");
|
||||||
|
for t in &tests {
|
||||||
|
let mut seed = field(t, "d");
|
||||||
|
seed.extend(field(t, "z"));
|
||||||
|
let dk = DecapsulationKey::<MlKem768>::from_seed(Seed::try_from(seed.as_slice()).unwrap());
|
||||||
|
assert_eq!(dk.encapsulation_key().to_bytes().as_slice(), field(t, "ek").as_slice(), "tcId {}", t["tcId"]);
|
||||||
|
assert_eq!(dk.to_expanded_bytes().as_slice(), field(t, "dk").as_slice(), "tcId {}", t["tcId"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn acvp_encapsulation_768() {
|
||||||
|
let tests = tests_of(&groups(ENCDEC, "encapsulation"));
|
||||||
|
assert_eq!(tests.len(), 25);
|
||||||
|
for t in &tests {
|
||||||
|
let ek = ek_from(&field(t, "ek")).expect("valid ek");
|
||||||
|
let m = B32::try_from(field(t, "m").as_slice()).unwrap();
|
||||||
|
let (c, k) = ek.encapsulate_deterministic(&m);
|
||||||
|
assert_eq!(c.as_slice(), field(t, "c").as_slice(), "tcId {}", t["tcId"]);
|
||||||
|
assert_eq!(k.as_slice(), field(t, "k").as_slice(), "tcId {}", t["tcId"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Includes "modified ciphertext" cases: implicit rejection must return the vector's K-bar.
|
||||||
|
#[test]
|
||||||
|
fn acvp_decapsulation_768_including_implicit_rejection() {
|
||||||
|
let tests = tests_of(&groups(ENCDEC, "decapsulation"));
|
||||||
|
assert_eq!(tests.len(), 10);
|
||||||
|
let mut modified = 0;
|
||||||
|
for t in &tests {
|
||||||
|
let dk = dk_from_expanded(&field(t, "dk")).expect("valid dk");
|
||||||
|
let c = Array::try_from(field(t, "c").as_slice()).unwrap();
|
||||||
|
let k = dk.decapsulate(&c);
|
||||||
|
assert_eq!(k.as_slice(), field(t, "k").as_slice(), "tcId {}", t["tcId"]);
|
||||||
|
if t["reason"] == "modified ciphertext" {
|
||||||
|
modified += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(modified > 0, "the set must exercise implicit rejection");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// FIPS 203 §7.2 encapsulation key check: the modulus check must reject the bad keys.
|
||||||
|
#[test]
|
||||||
|
fn acvp_encapsulation_key_check_768() {
|
||||||
|
let tests = tests_of(&groups(ENCDEC, "encapsulationKeyCheck"));
|
||||||
|
assert_eq!(tests.len(), 10);
|
||||||
|
let mut rejected = 0;
|
||||||
|
for t in &tests {
|
||||||
|
let expected = t["testPassed"].as_bool().unwrap();
|
||||||
|
assert_eq!(ek_from(&field(t, "ek")).is_ok(), expected, "tcId {} ({})", t["tcId"], t["reason"]);
|
||||||
|
// Arcanum's own parser must agree: a hybrid key built on a bad ML-KEM key is refused.
|
||||||
|
let mut hybrid = field(t, "ek");
|
||||||
|
hybrid.extend([9u8; 32]);
|
||||||
|
assert_eq!(arcanum::xwing::PublicKey::from_bytes(&hybrid).is_ok(), expected, "tcId {}", t["tcId"]);
|
||||||
|
rejected += usize::from(!expected);
|
||||||
|
}
|
||||||
|
assert!(rejected > 0, "the set must contain keys that fail the check");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// FIPS 203 §7.3 decapsulation key check (hash of ek inside dk).
|
||||||
|
#[test]
|
||||||
|
fn acvp_decapsulation_key_check_768() {
|
||||||
|
let tests = tests_of(&groups(ENCDEC, "decapsulationKeyCheck"));
|
||||||
|
assert_eq!(tests.len(), 10);
|
||||||
|
for t in &tests {
|
||||||
|
let expected = t["testPassed"].as_bool().unwrap();
|
||||||
|
assert_eq!(dk_from_expanded(&field(t, "dk")).is_ok(), expected, "tcId {} ({})", t["tcId"], t["reason"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
//! Mandatory test 1b: X25519 known-answer tests from RFC 7748 (§5.2 and §6.1).
|
||||||
|
//!
|
||||||
|
//! Runs the vetted `x25519-dalek` crate, exactly as Arcanum uses it. The bytes are the RFC's; the
|
||||||
|
//! same values are in x25519-dalek 3.0.0's own tests (tests/vectors/SOURCES.md).
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::arr;
|
||||||
|
use x25519_dalek::{PublicKey, StaticSecret, X25519_BASEPOINT_BYTES, x25519};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rfc7748_5_2_vector_1() {
|
||||||
|
let k = arr("a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4");
|
||||||
|
let u = arr("e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c");
|
||||||
|
let out: [u8; 32] = arr("c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552");
|
||||||
|
assert_eq!(x25519(k, u), out);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rfc7748_5_2_vector_2() {
|
||||||
|
let k = arr("4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d");
|
||||||
|
let u = arr("e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493");
|
||||||
|
let out: [u8; 32] = arr("95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957");
|
||||||
|
assert_eq!(x25519(k, u), out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RFC 7748 §5.2 iteration: k = u = 9; each step u <- k, k <- X25519(k, u).
|
||||||
|
fn iterate(n: usize) -> [u8; 32] {
|
||||||
|
let mut k = X25519_BASEPOINT_BYTES;
|
||||||
|
let mut u = X25519_BASEPOINT_BYTES;
|
||||||
|
for _ in 0..n {
|
||||||
|
let next = x25519(k, u);
|
||||||
|
u = k;
|
||||||
|
k = next;
|
||||||
|
}
|
||||||
|
k
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rfc7748_5_2_iterated_once() {
|
||||||
|
let expected: [u8; 32] = arr("422c8e7a6227d7bca1350b3e2bb7279f7897b87bb6854b783c60e80311ae3079");
|
||||||
|
assert_eq!(iterate(1), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rfc7748_5_2_iterated_1000() {
|
||||||
|
let expected: [u8; 32] = arr("684cf59ba83309552800ef566f2f4d3c1c3887c49360e3875f2eb94d99532c51");
|
||||||
|
assert_eq!(iterate(1000), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RFC 7748 §6.1, through the same `StaticSecret` / `PublicKey` types Arcanum uses.
|
||||||
|
#[test]
|
||||||
|
fn rfc7748_6_1_diffie_hellman() {
|
||||||
|
let alice_sk: [u8; 32] = arr("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
||||||
|
let alice_pk: [u8; 32] = arr("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
|
||||||
|
let bob_sk: [u8; 32] = arr("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
||||||
|
let bob_pk: [u8; 32] = arr("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
||||||
|
let shared: [u8; 32] = arr("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
|
||||||
|
|
||||||
|
let a = StaticSecret::from(alice_sk);
|
||||||
|
let b = StaticSecret::from(bob_sk);
|
||||||
|
assert_eq!(PublicKey::from(&a).as_bytes(), &alice_pk);
|
||||||
|
assert_eq!(PublicKey::from(&b).as_bytes(), &bob_pk);
|
||||||
|
assert_eq!(a.diffie_hellman(&PublicKey::from(bob_pk)).as_bytes(), &shared);
|
||||||
|
assert_eq!(b.diffie_hellman(&PublicKey::from(alice_pk)).as_bytes(), &shared);
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
//! Mandatory test 1c: X-Wing known-answer tests (draft-connolly-cfrg-xwing-kem-11, Appendix C)
|
||||||
|
//! through Arcanum's own composition, plus a differential test against RustCrypto `x-wing` 0.1.1.
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use arcanum::xwing::{Ciphertext, PublicKey, SecretKey};
|
||||||
|
use common::{assert_sha256, hex, rng};
|
||||||
|
use rand_core::Rng;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use x_wing::{Decapsulate, Decapsulator, KeyExport};
|
||||||
|
|
||||||
|
const VECTORS: &[u8] = include_bytes!("vectors/xwing-test-vectors.json");
|
||||||
|
const VECTORS_SHA256: &str = "409efe197550b22985b4a0419418a0c5f2c2b193426c55bd998399ec8d3e614d";
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Vector {
|
||||||
|
seed: String,
|
||||||
|
sk: String,
|
||||||
|
pk: String,
|
||||||
|
eseed: String,
|
||||||
|
ct: String,
|
||||||
|
ss: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn vectors() -> Vec<Vector> {
|
||||||
|
assert_sha256(VECTORS, VECTORS_SHA256, "X-Wing vectors");
|
||||||
|
serde_json::from_slice(VECTORS).expect("X-Wing json")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn draft_11_vectors() {
|
||||||
|
let vs = vectors();
|
||||||
|
assert_eq!(vs.len(), 3, "draft -11 Appendix C has three vectors");
|
||||||
|
for (i, v) in vs.iter().enumerate() {
|
||||||
|
let seed: [u8; 32] = hex(&v.seed).try_into().unwrap();
|
||||||
|
let sk = SecretKey::from_seed(seed);
|
||||||
|
assert_eq!(sk.to_seed().as_slice(), hex(&v.sk).as_slice(), "vector {i}: sk");
|
||||||
|
assert_eq!(sk.public_key().as_bytes().as_slice(), hex(&v.pk).as_slice(), "vector {i}: pk");
|
||||||
|
|
||||||
|
let pk = PublicKey::from_bytes(&hex(&v.pk)).expect("vector pk parses");
|
||||||
|
let eseed: [u8; 64] = hex(&v.eseed).try_into().unwrap();
|
||||||
|
let (ct, ss) = pk.encapsulate_derand(&eseed).expect("vector encapsulates");
|
||||||
|
assert_eq!(ct.as_bytes().as_slice(), hex(&v.ct).as_slice(), "vector {i}: ct");
|
||||||
|
assert_eq!(ss.as_bytes().as_slice(), hex(&v.ss).as_slice(), "vector {i}: ss (encaps)");
|
||||||
|
|
||||||
|
let ct = Ciphertext::from_bytes(&hex(&v.ct)).expect("vector ct parses");
|
||||||
|
let ss = sk.decapsulate(&ct).expect("vector decapsulates");
|
||||||
|
assert_eq!(ss.as_bytes().as_slice(), hex(&v.ss).as_slice(), "vector {i}: ss (decaps)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Arcanum and the RustCrypto reference must agree on keys, ciphertexts and secrets for random
|
||||||
|
/// inputs, and on the implicit-rejection output for random (garbage) ML-KEM ciphertexts.
|
||||||
|
#[test]
|
||||||
|
fn differential_against_rustcrypto_x_wing() {
|
||||||
|
let mut r = rng(7);
|
||||||
|
for round in 0..64 {
|
||||||
|
let mut seed = [0u8; 32];
|
||||||
|
r.fill_bytes(&mut seed);
|
||||||
|
let mut eseed = [0u8; 64];
|
||||||
|
r.fill_bytes(&mut eseed);
|
||||||
|
|
||||||
|
let ours = SecretKey::from_seed(seed);
|
||||||
|
let theirs = x_wing::DecapsulationKey::from(seed);
|
||||||
|
let their_pk = theirs.encapsulation_key().to_bytes();
|
||||||
|
assert_eq!(ours.public_key().as_bytes().as_slice(), their_pk.as_slice(), "round {round}: pk");
|
||||||
|
|
||||||
|
let (our_ct, our_ss) = ours.public_key().encapsulate_derand(&eseed).unwrap();
|
||||||
|
let their_ek = x_wing::EncapsulationKey::try_from(their_pk.as_slice()).unwrap();
|
||||||
|
let (their_ct, their_ss) = their_ek.encapsulate_deterministic(&eseed.into());
|
||||||
|
assert_eq!(our_ct.as_bytes().as_slice(), their_ct.as_slice(), "round {round}: ct");
|
||||||
|
assert_eq!(our_ss.as_bytes().as_slice(), their_ss.as_slice(), "round {round}: ss");
|
||||||
|
|
||||||
|
// A ciphertext whose ML-KEM part is random bytes, with an honest X25519 part.
|
||||||
|
let mut garbage = *our_ct.as_bytes();
|
||||||
|
r.fill_bytes(&mut garbage[..1088]);
|
||||||
|
let ours_ss = ours.decapsulate(&Ciphertext::from_bytes(&garbage).unwrap()).unwrap();
|
||||||
|
let theirs_ss = theirs.decapsulate(&garbage.into());
|
||||||
|
assert_eq!(ours_ss.as_bytes().as_slice(), theirs_ss.as_slice(), "round {round}: implicit rejection");
|
||||||
|
assert_ne!(ours_ss.as_bytes(), our_ss.as_bytes(), "round {round}: garbage must not decapsulate");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
//! Mandatory test 5: malformed ciphertexts and keys of the wrong length are rejected with typed
|
||||||
|
//! errors, never a panic. The same entry points are fuzzed by fuzz/fuzz_targets/untrusted_input.rs.
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use arcanum::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SECRET_KEY_LEN, SecretKey};
|
||||||
|
use arcanum::{Error, Input, exchange, seal};
|
||||||
|
use common::rng;
|
||||||
|
use rand_core::Rng;
|
||||||
|
|
||||||
|
const LENGTHS: [usize; 12] = [0, 1, 31, 32, 33, 64, 1087, 1088, 1119, 1121, 1215, 1217];
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn public_key_of_the_wrong_length_is_a_typed_error() {
|
||||||
|
for len in LENGTHS.into_iter().chain([PUBLIC_KEY_LEN * 2]) {
|
||||||
|
let err = PublicKey::from_bytes(&vec![0u8; len]).unwrap_err();
|
||||||
|
assert_eq!(err, Error::InvalidLength { input: Input::PublicKey, expected: PUBLIC_KEY_LEN, actual: len });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ciphertext_of_the_wrong_length_is_a_typed_error() {
|
||||||
|
for len in LENGTHS.into_iter().filter(|&l| l != CIPHERTEXT_LEN).chain([CIPHERTEXT_LEN * 2]) {
|
||||||
|
let err = Ciphertext::from_bytes(&vec![0u8; len]).unwrap_err();
|
||||||
|
assert_eq!(err, Error::InvalidLength { input: Input::Ciphertext, expected: CIPHERTEXT_LEN, actual: len });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn secret_key_of_the_wrong_length_is_a_typed_error() {
|
||||||
|
for len in LENGTHS.into_iter().filter(|&l| l != SECRET_KEY_LEN) {
|
||||||
|
let err = SecretKey::from_bytes(&vec![0u8; len]).unwrap_err();
|
||||||
|
assert_eq!(err, Error::InvalidLength { input: Input::SecretKey, expected: SECRET_KEY_LEN, actual: len });
|
||||||
|
}
|
||||||
|
assert!(SecretKey::from_bytes(&[7u8; SECRET_KEY_LEN]).is_ok(), "any 32 bytes are a valid seed");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An ML-KEM key whose coefficients are not reduced mod q fails FIPS 203 §7.2: all-0xff bytes
|
||||||
|
/// encode 12-bit values of 4095 > q = 3329.
|
||||||
|
#[test]
|
||||||
|
fn ml_kem_key_failing_the_modulus_check_is_a_typed_error() {
|
||||||
|
let mut bad = [0xffu8; PUBLIC_KEY_LEN];
|
||||||
|
bad[1184..].copy_from_slice(&[9u8; 32]);
|
||||||
|
assert_eq!(PublicKey::from_bytes(&bad).unwrap_err(), Error::InvalidPublicKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn random_bytes_never_panic() {
|
||||||
|
let mut r = rng(9);
|
||||||
|
let sk = SecretKey::from_seed([0x01; 32]);
|
||||||
|
let peer = SecretKey::from_seed([0x02; 32]);
|
||||||
|
let (honest_ct, _) = peer.public_key().encapsulate(&mut r).unwrap();
|
||||||
|
for round in 0..512 {
|
||||||
|
let len = [PUBLIC_KEY_LEN, CIPHERTEXT_LEN, seal::OVERHEAD, seal::OVERHEAD + 40, 0, 17][round % 6];
|
||||||
|
let mut buf = vec![0u8; len];
|
||||||
|
r.fill_bytes(&mut buf);
|
||||||
|
if let Ok(pk) = PublicKey::from_bytes(&buf) {
|
||||||
|
let _ = exchange::initiate(&sk, &pk, b"ctx", &mut r);
|
||||||
|
let _ = exchange::respond(&sk, &pk, &honest_ct, b"ctx", &mut r);
|
||||||
|
}
|
||||||
|
if let Ok(ct) = Ciphertext::from_bytes(&buf) {
|
||||||
|
// Random ML-KEM ciphertexts decapsulate (implicit rejection) to some unrelated secret.
|
||||||
|
let _ = exchange::respond(&sk, peer.public_key(), &ct, b"ctx", &mut r);
|
||||||
|
let _ = exchange::initiate(&sk, peer.public_key(), b"ctx", &mut r).unwrap().finish(&ct);
|
||||||
|
}
|
||||||
|
assert!(seal::open(&sk, &buf, b"aad").is_err(), "random bytes must never open");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sealed_box_round_trips() {
|
||||||
|
let mut r = rng(10);
|
||||||
|
let sk = SecretKey::generate(&mut r);
|
||||||
|
for len in [0usize, 1, 15, 16, 17, 1000, 70_000] {
|
||||||
|
let msg: Vec<u8> = (0..len).map(|i| (i * 31 % 251) as u8).collect();
|
||||||
|
let sealed = seal::seal(sk.public_key(), &msg, b"custos/v1 item 42", &mut r).unwrap();
|
||||||
|
assert_eq!(sealed.len(), msg.len() + seal::OVERHEAD);
|
||||||
|
let opened = seal::open(&sk, &sealed, b"custos/v1 item 42").unwrap();
|
||||||
|
assert_eq!(opened.as_slice(), msg.as_slice());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sealed_box_too_short_is_a_typed_error() {
|
||||||
|
let sk = SecretKey::from_seed([0x02; 32]);
|
||||||
|
for len in [0, 1, seal::OVERHEAD - 1] {
|
||||||
|
let err = seal::open(&sk, &vec![1u8; len], b"").unwrap_err();
|
||||||
|
assert_eq!(err, Error::TooShort { input: Input::SealedBox, minimum: seal::OVERHEAD, actual: len });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sealed_box_with_an_unknown_version_is_a_typed_error() {
|
||||||
|
let mut r = rng(11);
|
||||||
|
let sk = SecretKey::generate(&mut r);
|
||||||
|
let mut sealed = seal::seal(sk.public_key(), b"hello", b"", &mut r).unwrap();
|
||||||
|
for v in [0u8, 2, 0xff] {
|
||||||
|
sealed[0] = v;
|
||||||
|
assert_eq!(seal::open(&sk, &sealed, b"").unwrap_err(), Error::UnsupportedVersion(v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn every_tampered_or_truncated_sealed_box_is_refused() {
|
||||||
|
let mut r = rng(12);
|
||||||
|
let sk = SecretKey::generate(&mut r);
|
||||||
|
let sealed = seal::seal(sk.public_key(), b"the household photo key", b"aad", &mut r).unwrap();
|
||||||
|
for i in 1..sealed.len() {
|
||||||
|
let mut bad = sealed.clone();
|
||||||
|
bad[i] ^= 0x80;
|
||||||
|
let err = seal::open(&sk, &bad, b"aad").unwrap_err();
|
||||||
|
assert!(matches!(err, Error::DecryptionFailed | Error::NonContributory), "byte {i}: {err:?}");
|
||||||
|
}
|
||||||
|
for len in seal::OVERHEAD..sealed.len() {
|
||||||
|
assert_eq!(seal::open(&sk, &sealed[..len], b"aad").unwrap_err(), Error::DecryptionFailed, "length {len}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sealed_box_needs_the_right_key_and_aad() {
|
||||||
|
let mut r = rng(13);
|
||||||
|
let sk = SecretKey::generate(&mut r);
|
||||||
|
let other = SecretKey::generate(&mut r);
|
||||||
|
let sealed = seal::seal(sk.public_key(), b"secret", b"aad", &mut r).unwrap();
|
||||||
|
assert_eq!(seal::open(&other, &sealed, b"aad").unwrap_err(), Error::DecryptionFailed);
|
||||||
|
assert_eq!(seal::open(&sk, &sealed, b"aaD").unwrap_err(), Error::DecryptionFailed);
|
||||||
|
assert_eq!(seal::open(&sk, &sealed, b"").unwrap_err(), Error::DecryptionFailed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn errors_display_without_secrets() {
|
||||||
|
let e = Error::InvalidLength { input: Input::Ciphertext, expected: 1120, actual: 3 };
|
||||||
|
assert_eq!(e.to_string(), "ciphertext has the wrong length: expected 1120 bytes, got 3");
|
||||||
|
let _: &dyn std::error::Error = &e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-20: the modulus check is not over-eager. The last 32 bytes of the ML-KEM key are the
|
||||||
|
/// seed rho, where any value is legal.
|
||||||
|
#[test]
|
||||||
|
fn ml_kem_key_check_accepts_any_rho() {
|
||||||
|
let sk = SecretKey::from_seed([0x03; 32]);
|
||||||
|
let mut ok = *sk.public_key().as_bytes();
|
||||||
|
ok[1183] ^= 1;
|
||||||
|
assert!(PublicKey::from_bytes(&ok).is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-21: the only stored private-key form is the 32-byte seed; an expanded ML-KEM key or an
|
||||||
|
/// X-Wing key plus anything else is refused by length.
|
||||||
|
#[test]
|
||||||
|
fn only_the_32_byte_seed_is_a_secret_key() {
|
||||||
|
for len in [64, 96, 2400, 2432] {
|
||||||
|
assert!(matches!(SecretKey::from_bytes(&vec![0u8; len]), Err(Error::InvalidLength { .. })), "{len}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-24: a one-byte extension and a splice (header of one box, body of another) are refused.
|
||||||
|
#[test]
|
||||||
|
fn sealed_box_extension_and_splice_are_refused() {
|
||||||
|
let mut r = rng(14);
|
||||||
|
let sk = SecretKey::generate(&mut r);
|
||||||
|
let b1 = seal::seal(sk.public_key(), b"one", b"", &mut r).unwrap();
|
||||||
|
let b2 = seal::seal(sk.public_key(), b"two", b"", &mut r).unwrap();
|
||||||
|
let mut ext = b1.clone();
|
||||||
|
ext.push(0);
|
||||||
|
assert_eq!(seal::open(&sk, &ext, b"").unwrap_err(), Error::DecryptionFailed);
|
||||||
|
let mut spliced = b1[..1121].to_vec();
|
||||||
|
spliced.extend_from_slice(&b2[1121..]);
|
||||||
|
assert_eq!(seal::open(&sk, &spliced, b"").unwrap_err(), Error::DecryptionFailed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-26: all randomness comes from the RNG parameter: the same seed gives the same box, a
|
||||||
|
/// different seed a different one.
|
||||||
|
#[test]
|
||||||
|
fn seal_randomness_comes_only_from_the_rng() {
|
||||||
|
let sk = SecretKey::from_seed([0x04; 32]);
|
||||||
|
let a = seal::seal(sk.public_key(), b"m", b"", &mut rng(1)).unwrap();
|
||||||
|
let b = seal::seal(sk.public_key(), b"m", b"", &mut rng(1)).unwrap();
|
||||||
|
let c = seal::seal(sk.public_key(), b"m", b"", &mut rng(2)).unwrap();
|
||||||
|
assert_eq!(a, b);
|
||||||
|
assert_ne!(a, c);
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
//! Mandatory test 6 (the part a test can check; `cargo deny` and `cargo audit` are run separately,
|
||||||
|
//! see docs/01_arcanum.md). The compiler enforces `#![forbid(unsafe_code)]`; this guardrail makes
|
||||||
|
//! sure the attribute itself is never removed and no source file opts back in.
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
fn rust_files(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
|
||||||
|
for entry in fs::read_dir(dir).unwrap() {
|
||||||
|
let path = entry.unwrap().path();
|
||||||
|
if path.is_dir() {
|
||||||
|
rust_files(&path, out);
|
||||||
|
} else if path.extension().is_some_and(|e| e == "rs") {
|
||||||
|
out.push(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_crate_forbids_unsafe_code() {
|
||||||
|
let lib = fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("src/lib.rs")).unwrap();
|
||||||
|
assert!(lib.lines().any(|l| l.trim() == "#![forbid(unsafe_code)]"), "src/lib.rs must forbid unsafe code");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_source_file_contains_unsafe() {
|
||||||
|
let mut files = Vec::new();
|
||||||
|
rust_files(&Path::new(env!("CARGO_MANIFEST_DIR")).join("src"), &mut files);
|
||||||
|
assert!(!files.is_empty());
|
||||||
|
for f in files {
|
||||||
|
let text = fs::read_to_string(&f).unwrap();
|
||||||
|
for (n, line) in text.lines().enumerate() {
|
||||||
|
let code = line.split("//").next().unwrap_or("");
|
||||||
|
let opts_in = code.contains("unsafe") && code.trim() != "#![forbid(unsafe_code)]";
|
||||||
|
assert!(!opts_in, "{}:{}: {line}", f.display(), n + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
//! Mandatory test 4: WireGuard PSK epochs. Successive epochs differ, the same epoch is reproducible
|
||||||
|
//! from the same inputs, and the epoch helper follows the 120-second Rosenpass-style interval.
|
||||||
|
|
||||||
|
use arcanum::exchange;
|
||||||
|
use arcanum::xwing::SecretKey;
|
||||||
|
use arcanum::{RECOMMENDED_EPOCH_SECONDS, WIREGUARD_PSK_LEN, epoch_at};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
const CONTEXT: &[u8] = b"via/test: epochs";
|
||||||
|
|
||||||
|
fn exchange_with(eseed_i: [u8; 64]) -> (exchange::Session, exchange::Session) {
|
||||||
|
let i = SecretKey::from_seed([0x01; 32]);
|
||||||
|
let r = SecretKey::from_seed([0x04; 32]);
|
||||||
|
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &eseed_i).unwrap();
|
||||||
|
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &[0x05; 64]).unwrap();
|
||||||
|
(init.finish(&ct_ri).unwrap(), rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sessions() -> (exchange::Session, exchange::Session) {
|
||||||
|
exchange_with([0x02; 64])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn successive_epochs_differ() {
|
||||||
|
let (a, _) = sessions();
|
||||||
|
let mut seen = HashSet::new();
|
||||||
|
for epoch in 0..2048u64 {
|
||||||
|
assert!(seen.insert(*a.wireguard_psk(epoch).as_bytes()), "epoch {epoch} repeats an earlier PSK");
|
||||||
|
}
|
||||||
|
// The ends of the range and neighbours that differ only in the high bytes.
|
||||||
|
for epoch in [u64::MAX, u64::MAX - 1, 1 << 32, (1 << 32) + 1, 1 << 63] {
|
||||||
|
assert!(seen.insert(*a.wireguard_psk(epoch).as_bytes()), "epoch {epoch} repeats");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_same_epoch_is_reproducible_from_the_same_inputs() {
|
||||||
|
let (a1, b1) = sessions();
|
||||||
|
let (a2, b2) = sessions();
|
||||||
|
for epoch in [0, 1, 719, 720, u64::MAX] {
|
||||||
|
let p = *a1.wireguard_psk(epoch).as_bytes();
|
||||||
|
assert_eq!(&p, a1.wireguard_psk(epoch).as_bytes(), "same session, asked twice");
|
||||||
|
assert_eq!(&p, a2.wireguard_psk(epoch).as_bytes(), "rebuilt from the same inputs");
|
||||||
|
assert_eq!(&p, b1.wireguard_psk(epoch).as_bytes(), "the other peer");
|
||||||
|
assert_eq!(&p, b2.wireguard_psk(epoch).as_bytes(), "the other peer, rebuilt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_different_exchange_gives_different_psks_for_the_same_epoch() {
|
||||||
|
let (a, _) = exchange_with([0x02; 64]);
|
||||||
|
let (c, _) = exchange_with([0x03; 64]);
|
||||||
|
for epoch in 0..16 {
|
||||||
|
assert_ne!(a.wireguard_psk(epoch).as_bytes(), c.wireguard_psk(epoch).as_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_psk_is_32_bytes_and_not_trivial() {
|
||||||
|
let (a, _) = sessions();
|
||||||
|
let p = a.wireguard_psk(0);
|
||||||
|
assert_eq!(WIREGUARD_PSK_LEN, 32);
|
||||||
|
assert_eq!(p.as_bytes().len(), 32);
|
||||||
|
assert_ne!(p.as_bytes(), &[0u8; 32]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_psk_never_prints() {
|
||||||
|
let (a, _) = sessions();
|
||||||
|
let p = a.wireguard_psk(0);
|
||||||
|
let shown = format!("{p:?} {a:?}");
|
||||||
|
assert!(shown.contains("redacted"), "{shown}");
|
||||||
|
let k = p.as_bytes();
|
||||||
|
// Neither a hex dump nor a derived `[1, 2, 3, ...]` array may appear.
|
||||||
|
assert!(!shown.contains(&hex::encode(&k[..4])), "{shown}");
|
||||||
|
assert!(!shown.contains(&format!("{}, {}, {}", k[0], k[1], k[2])), "{shown}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn epoch_helper_uses_two_minute_epochs() {
|
||||||
|
assert_eq!(RECOMMENDED_EPOCH_SECONDS, 120, "Rosenpass REKEY_AFTER_TIME_RESPONDER is 120 s");
|
||||||
|
assert_eq!(epoch_at(0), 0);
|
||||||
|
assert_eq!(epoch_at(119), 0);
|
||||||
|
assert_eq!(epoch_at(120), 1);
|
||||||
|
assert_eq!(epoch_at(239), 1);
|
||||||
|
assert_eq!(epoch_at(240), 2);
|
||||||
|
assert_eq!(epoch_at(1_764_000_000), 1_764_000_000 / 120);
|
||||||
|
assert_eq!(epoch_at(u64::MAX), u64::MAX / 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// QA A-11: the epoch is encoded at its full 64-bit width; nothing truncates it.
|
||||||
|
#[test]
|
||||||
|
fn the_epoch_is_not_truncated() {
|
||||||
|
let (a, _) = sessions();
|
||||||
|
let p = |e: u64| *a.wireguard_psk(e).as_bytes();
|
||||||
|
assert_ne!(p(0), p(1 << 32));
|
||||||
|
assert_ne!(p(1), p(1 << 8));
|
||||||
|
assert_ne!(p(u64::MAX), p(u64::MAX >> 1));
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# Known-answer vector sources
|
||||||
|
|
||||||
|
Every file here is kept byte-for-byte as published (`.gitattributes` marks the folder `-text`).
|
||||||
|
The tests check each file's sha256 before using it, so a changed file fails the test instead of
|
||||||
|
silently changing what is verified. Fetched 2026-11-25.
|
||||||
|
|
||||||
|
| File | sha256 | Source |
|
||||||
|
|:--|:--|:--|
|
||||||
|
| `acvp-ml-kem-keyGen-FIPS203-internalProjection.json` | `d7a62a2c3476957f56dd8d24f9004ea6776ccfe995ffe71a65bb9506dc9c7b1b` | NIST ACVP-Server, `gen-val/json-files/ML-KEM-keyGen-FIPS203/internalProjection.json` at commit `975de31eb83d87039ec88934fdc47d8c312b892d` (2026-08-12). Git blob `99973426c8dd03f227ace6d7ac917ba4443e2f27` matches the GitHub API listing. |
|
||||||
|
| `acvp-ml-kem-encapDecap-FIPS203-internalProjection.json` | `a556952ce869bb89c3a3196a701dad89647c193a34c86eafb61a9d710d5b810f` | NIST ACVP-Server, `gen-val/json-files/ML-KEM-encapDecap-FIPS203/internalProjection.json` at the same commit. Git blob `73692fe52abe2f937b7884baa46475c77c68a1d3` matches. |
|
||||||
|
| `xwing-test-vectors.json` | `409efe197550b22985b4a0419418a0c5f2c2b193426c55bd998399ec8d3e614d` | The X-Wing draft authors' repository, `dconnolly/draft-connolly-cfrg-xwing-kem`, `spec/test-vectors.json` at commit `984c2f7a93b8f8d8f8073ebb53f9f4ce50b5babd` (2026-09-23, the day draft -11 was posted). The same three vectors are in Appendix C of draft -11 (spot-checked: seed and ss of vectors 1–2, ss of vector 3), and, pretty-printed, in RustCrypto `x-wing` 0.1.1 `tests/test-vectors.json` (identical after removing whitespace). |
|
||||||
|
|
||||||
|
URLs:
|
||||||
|
|
||||||
|
- <https://github.com/usnistgov/ACVP-Server/tree/975de31eb83d87039ec88934fdc47d8c312b892d/gen-val/json-files>
|
||||||
|
- <https://github.com/dconnolly/draft-connolly-cfrg-xwing-kem/blob/984c2f7a93b8f8d8f8073ebb53f9f4ce50b5babd/spec/test-vectors.json>
|
||||||
|
- <https://www.ietf.org/archive/id/draft-connolly-cfrg-xwing-kem-11.txt>, Appendix C
|
||||||
|
|
||||||
|
Only the ML-KEM-768 groups of the ACVP files are used (the files also hold 512 and 1024).
|
||||||
|
|
||||||
|
## Vectors kept in test source
|
||||||
|
|
||||||
|
| Test | Source |
|
||||||
|
|:--|:--|
|
||||||
|
| X25519 scalar multiplication, two vectors (RFC 7748 §5.2) | RFC 7748, <https://www.rfc-editor.org/rfc/rfc7748.txt>; the same bytes are in `x25519-dalek` 3.0.0 `tests/x25519_tests.rs` |
|
||||||
|
| X25519 iterated, 1 and 1,000 iterations (RFC 7748 §5.2) | RFC 7748 §5.2 |
|
||||||
|
| X25519 Diffie–Hellman, Alice and Bob (RFC 7748 §6.1) | RFC 7748 §6.1; also in `x25519-dalek` 3.0.0 `tests/x25519_tests.rs` |
|
||||||
|
|
||||||
|
Not run: the RFC 7748 1,000,000-iteration vector (minutes in a debug build).
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user