From 5ec7fa763575bae03a9c6eb6b50d38872d4fe1c0 Mon Sep 17 00:00:00 2001 From: Armin Sabouri Date: Thu, 8 Oct 2026 18:05:44 -0400 Subject: [PATCH] Add experimental supoprt for secp256k1 KEM Co-authored-by: Oladapo Oyindamola <111582215+0xZaddyy@users.noreply.github.com> using bitcoin ohttp crate and aliasing ohttp conditional on a feature flag. --- Cargo.lock | 499 +++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 5 + README.md | 12 +- justfile | 9 +- src/error.rs | 2 +- src/harness.rs | 8 +- src/lib.rs | 87 ++++++++- 7 files changed, 574 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f12f58c..1b7d9d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,26 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aead" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877" +dependencies = [ + "generic-array", + "rand_core 0.6.4", +] + +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array", +] + [[package]] name = "aead" version = "0.6.1" @@ -9,7 +29,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" dependencies = [ "crypto-common 0.2.2", - "inout", + "inout 0.2.2", +] + +[[package]] +name = "aes" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8" +dependencies = [ + "cfg-if", + "cipher 0.3.0", + "cpufeatures 0.2.17", + "opaque-debug", ] [[package]] @@ -18,23 +50,37 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35f0f96ce78e38c3dc6d8948aa8163d06385be74000f3c7a95bf1eef35d3ea32" dependencies = [ - "cipher", + "cipher 0.5.2", "cpubits", "cpufeatures 0.3.1", ] +[[package]] +name = "aes-gcm" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc3be92e19a7ef47457b8e6f90707e12b6ac5d20c6f3866584fa3be0787d839f" +dependencies = [ + "aead 0.4.3", + "aes 0.7.5", + "cipher 0.3.0", + "ctr 0.7.0", + "ghash 0.4.4", + "subtle", +] + [[package]] name = "aes-gcm" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f2b8006a0c83f52b62ba44a97b58bf76fe2f70a329e588f67f89691d93d498f" dependencies = [ - "aead", - "aes", - "cipher", - "ctr", + "aead 0.6.1", + "aes 0.9.3", + "cipher 0.5.2", + "ctr 0.10.1", "ctutils", - "ghash", + "ghash 0.6.0", ] [[package]] @@ -76,7 +122,49 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d305a54bcb99974213b4c78a486c34091e83c5d6d6572f7f4331c904ea9d127" dependencies = [ - "thiserror", + "thiserror 2.0.18", +] + +[[package]] +name = "bitcoin-hpke" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d37a54c486727c1d1ae9cc28dcf78b6e6ba20dcb88e8c892f1437d9ce215dc8c" +dependencies = [ + "aead 0.5.2", + "chacha20poly1305 0.10.1", + "digest 0.10.7", + "generic-array", + "hkdf 0.12.4", + "hmac 0.12.1", + "rand_core 0.6.4", + "secp256k1", + "sha2 0.10.9", + "subtle", + "zeroize", +] + +[[package]] +name = "bitcoin-ohttp" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a803a4b54e44635206b53329c78c0029d0c70926288ac2f07f4bb1267546cb" +dependencies = [ + "aead 0.4.3", + "aes-gcm 0.9.2", + "bitcoin-hpke", + "byteorder", + "chacha20poly1305 0.8.0", + "hex", + "hkdf 0.11.0", + "lazy_static", + "log", + "rand 0.8.8", + "serde", + "serde_derive", + "sha2 0.9.9", + "thiserror 1.0.69", + "toml 0.5.11", ] [[package]] @@ -89,6 +177,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -142,6 +239,29 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chacha20" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fee7ad89dc1128635074c268ee661f90c3f7e83d9fd12910608c36b47d6c3412" +dependencies = [ + "cfg-if", + "cipher 0.3.0", + "cpufeatures 0.1.5", + "zeroize", +] + +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher 0.4.4", + "cpufeatures 0.2.17", +] + [[package]] name = "chacha20" version = "0.10.2" @@ -149,21 +269,67 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", - "cipher", + "cipher 0.5.2", "cpufeatures 0.3.1", "rand_core 0.10.1", ] +[[package]] +name = "chacha20poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1580317203210c517b6d44794abfbe600698276db18127e37ad3e69bf5e848e5" +dependencies = [ + "aead 0.4.3", + "chacha20 0.7.1", + "cipher 0.3.0", + "poly1305 0.7.2", + "zeroize", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead 0.5.2", + "chacha20 0.9.1", + "cipher 0.4.4", + "poly1305 0.8.0", + "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", + "aead 0.6.1", + "chacha20 0.10.2", + "cipher 0.5.2", + "poly1305 0.9.1", +] + +[[package]] +name = "cipher" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout 0.1.4", + "zeroize", ] [[package]] @@ -174,7 +340,7 @@ checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" dependencies = [ "block-buffer 0.12.1", "crypto-common 0.2.2", - "inout", + "inout 0.2.2", ] [[package]] @@ -195,6 +361,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" +[[package]] +name = "cpufeatures" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66c99696f6c9dd7f35d486b9d04d7e6e202aa3e8c40d553f2fdf5e7e0c6a71ef" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -235,6 +410,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] @@ -249,13 +425,32 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "crypto-mac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25fab6889090c8133f3deb8f73ba3c65a7f456f66436fc012a1b1e272b1e103e" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "ctr" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a232f92a03f37dd7d7dd2adc67166c77e9cd88de5b019b9a9eecfaeaf7bfd481" +dependencies = [ + "cipher 0.3.0", +] + [[package]] name = "ctr" version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baaca1c4b237092596f64d571e9db6ce4109c4ef9742e27590f1709594461f21" dependencies = [ - "cipher", + "cipher 0.5.2", ] [[package]] @@ -310,6 +505,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + [[package]] name = "digest" version = "0.10.7" @@ -318,6 +522,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", "crypto-common 0.1.7", + "subtle", ] [[package]] @@ -356,7 +561,7 @@ dependencies = [ "digest 0.11.3", "ff", "group", - "hkdf", + "hkdf 0.13.0", "hybrid-array", "rand_core 0.10.1", "sec1", @@ -540,13 +745,23 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "ghash" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1583cc1656d7839fd3732b80cf4f38850336cdb9b8ded1cd399ca62958de3c99" +dependencies = [ + "opaque-debug", + "polyval 0.5.3", +] + [[package]] name = "ghash" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5" dependencies = [ - "polyval", + "polyval 0.7.3", ] [[package]] @@ -572,13 +787,51 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hkdf" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01706d578d5c281058480e673ae4086a9f4710d8df1ad80a5b03e39ece5f886b" +dependencies = [ + "digest 0.9.0", + "hmac 0.11.0", +] + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac 0.12.1", +] + [[package]] name = "hkdf" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" dependencies = [ - "hmac", + "hmac 0.13.0", +] + +[[package]] +name = "hmac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" +dependencies = [ + "crypto-mac", + "digest 0.9.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", ] [[package]] @@ -596,18 +849,18 @@ version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5324110b02044183df000f0bd7d2d7e61f1000631508627e77f63983b6fdffb" dependencies = [ - "aead", - "aes-gcm", - "chacha20poly1305", + "aead 0.6.1", + "aes-gcm 0.11.1", + "chacha20poly1305 0.11.0", "getrandom 0.4.3", - "hkdf", + "hkdf 0.13.0", "hybrid-array", "ml-kem", "p256", "p384", "p521", "rand_core 0.10.1", - "sha2", + "sha2 0.11.0", "sha3 0.12.0", "shake", "subtle", @@ -895,6 +1148,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "inout" version = "0.2.2" @@ -1041,22 +1303,22 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc53603e4bf77342fe8409611972078761604e4370330c4ee5abac49f079b464" dependencies = [ - "aead", - "aes-gcm", + "aead 0.6.1", + "aes-gcm 0.11.1", "byteorder", - "chacha20poly1305", + "chacha20poly1305 0.11.0", "futures", "hex", - "hkdf", + "hkdf 0.13.0", "hpke", "log", "pin-project", "rand 0.10.2", "serde", "serde_derive", - "sha2", - "thiserror", - "toml", + "sha2 0.11.0", + "thiserror 2.0.18", + "toml 0.9.12+spec-1.1.0", ] [[package]] @@ -1064,13 +1326,14 @@ name = "ohttp-client" version = "0.0.1" dependencies = [ "bhttp", + "bitcoin-ohttp", "bitreq", "getrandom 0.2.17", "getrandom 0.3.4", "getrandom 0.4.3", "ohttp", "ohttp-relay", - "thiserror", + "thiserror 2.0.18", "tokio", "url", "wasm-bindgen", @@ -1105,6 +1368,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "p256" version = "0.14.0" @@ -1172,6 +1441,28 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "poly1305" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "048aeb476be11a4b6ca432ca569e375810de9294ae78f4774e78ea98a9246ede" +dependencies = [ + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash 0.4.0", +] + +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash 0.5.1", +] + [[package]] name = "poly1305" version = "0.9.1" @@ -1179,7 +1470,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c" dependencies = [ "cpufeatures 0.3.1", - "universal-hash", + "universal-hash 0.6.1", +] + +[[package]] +name = "polyval" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash 0.4.0", ] [[package]] @@ -1190,7 +1493,7 @@ checksum = "f0fa31d631f2b2cb2a544d0aa321ce847a94764d701ca2becc411138b93d49cd" dependencies = [ "cpubits", "cpufeatures 0.3.1", - "universal-hash", + "universal-hash 0.6.1", ] [[package]] @@ -1257,13 +1560,24 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" dependencies = [ - "rand_chacha", + "rand_chacha 0.9.0", "rand_core 0.9.5", ] @@ -1273,11 +1587,21 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" dependencies = [ - "chacha20", + "chacha20 0.10.2", "getrandom 0.4.3", "rand_core 0.10.1", ] +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + [[package]] name = "rand_chacha" version = "0.9.0" @@ -1288,6 +1612,15 @@ dependencies = [ "rand_core 0.9.5", ] +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + [[package]] name = "rand_core" version = "0.9.5" @@ -1397,6 +1730,25 @@ dependencies = [ "zeroize", ] +[[package]] +name = "secp256k1" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" +dependencies = [ + "rand 0.8.8", + "secp256k1-sys", +] + +[[package]] +name = "secp256k1-sys" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" +dependencies = [ + "cc", +] + [[package]] name = "semver" version = "1.0.28" @@ -1452,6 +1804,30 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.9.0", + "opaque-debug", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + [[package]] name = "sha2" version = "0.11.0" @@ -1575,13 +1951,33 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + [[package]] name = "thiserror" version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.18", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1675,6 +2071,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "toml" version = "0.9.12+spec-1.1.0" @@ -1800,7 +2205,7 @@ dependencies = [ "log", "rand 0.9.5", "sha1", - "thiserror", + "thiserror 2.0.18", "utf-8", ] @@ -1827,6 +2232,26 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "universal-hash" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8326b2c654932e3e4f9196e69d08fdf7cfd718e1dc6f66b347e6024a0c961402" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + [[package]] name = "universal-hash" version = "0.6.1" diff --git a/Cargo.toml b/Cargo.toml index ee0976a..b9c1d5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,9 +30,14 @@ wasm = ["wasm_js", "dep:wasm-bindgen"] # for downstream integration tests, and build the `ohttp-test-harness` binary # used by the JS e2e. harness = ["bitreq", "dep:ohttp-relay", "dep:tokio"] +# Swap `ohttp` for `bitcoin-ohttp`, whose only KEM is DHKEM(secp256k1, +# HKDF-SHA256): gateways advertising any other KEM are rejected. Not additive, +# and not supported on wasm32. +secp256k1 = ["dep:bitcoin-ohttp"] [dependencies] bhttp = "0.7.2" +bitcoin-ohttp = { version = "0.6.0", optional = true } bitreq = { version = "0.3.7", features = ["async", "proxy"], optional = true } getrandom = { version = "0.3", features = ["std"] } # Feature-unification shim: pulls getrandom 0.2 with `js` so the transitive diff --git a/README.md b/README.md index 5b32b00..fc8f6cc 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,13 @@ The client uses whichever KEM a gateway advertises, so it works with every KEM [`ohttp`](https://crates.io/crates/ohttp) implements: DHKEM(X25519, HKDF-SHA256), DHKEM(P-256, HKDF-SHA256), and X-Wing (ML-KEM-768 + X25519). +The `secp256k1` feature swaps `ohttp` for +[`bitcoin-ohttp`](https://crates.io/crates/bitcoin-ohttp), whose only KEM is +DHKEM(secp256k1, HKDF-SHA256) with HKDF-SHA256 and ChaCha20Poly1305. With it +enabled, gateways that advertise only other KEMs are rejected. This holds for +every crate in the build that depends on `ohttp-client`, because Cargo unifies +features. It is not supported on wasm32. + ## Sans-IO by default By default the crate does **no network IO**. You encapsulate a request, send the @@ -75,7 +82,7 @@ WASI targets need no extra feature. Common tasks are wrapped in the [`justfile`](justfile): ```sh -just test # cargo test --all-features +just test # cargo test, with and without secp256k1 just check-wasm # verify the wasm32 build just build-wasm # build the web package into js/pkg/ just test-js # build wasm and run the JS e2e @@ -85,7 +92,8 @@ just check # full suite: fmt, clippy, tests, wasm, js e2e, audit Or directly with cargo: ```sh -cargo test --all-features +cargo test --features bitreq,harness,wasm # ohttp +cargo test --all-features # bitcoin-ohttp (secp256k1) cargo check --target wasm32-unknown-unknown --features wasm ``` diff --git a/justfile b/justfile index bbeafdb..92e838b 100644 --- a/justfile +++ b/justfile @@ -12,14 +12,19 @@ fmt: fmt-check: cargo fmt --all -- --check -# Lint with clippy, treating warnings as errors, across all features. +# Every feature except `secp256k1`, which swaps out the `ohttp` backend. +OHTTP_FEATURES := "bitreq,harness,wasm" + +# Lint with clippy, treating warnings as errors, with each OHTTP backend. lint: + cargo clippy --all-targets --features {{OHTTP_FEATURES}} -- -D warnings cargo clippy --all-targets --all-features -- -D warnings clippy: lint -# Run tests across all features. +# Run tests with each OHTTP backend. test: + cargo test --features {{OHTTP_FEATURES}} cargo test --all-features # Verify the crate (including wasm-bindgen exports) builds for browsers. diff --git a/src/error.rs b/src/error.rs index 524f363..6618070 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,7 @@ #[derive(Debug, thiserror::Error)] pub enum Error { #[error("ohttp: {0}")] - Ohttp(#[from] ohttp::Error), + Ohttp(#[from] crate::ohttp::Error), #[error("bhttp: {0}")] Bhttp(#[from] bhttp::Error), #[error("url: {0}")] diff --git a/src/harness.rs b/src/harness.rs index f78be7b..0b2e494 100644 --- a/src/harness.rs +++ b/src/harness.rs @@ -17,6 +17,8 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::thread::JoinHandle; +use crate::ohttp; + /// Gateway opt-in advertised to the relay's prober: ALPN-style list /// containing the one purpose `ohttp_relay` requires before it forwards. const ALLOWED_PURPOSES_BODY: &[u8] = b"\x00\x01\x2aTEST 454403bb-9f7b-4385-b31f-acd2dae20b7e"; @@ -56,9 +58,13 @@ impl TestHarness { // Gateway: serves its key config, opts in to relaying, and // decapsulates requests, forwarding them to the inner target. + #[cfg(not(feature = "secp256k1"))] + let kem = ohttp::hpke::Kem::X25519Sha256; + #[cfg(feature = "secp256k1")] + let kem = ohttp::hpke::Kem::K256Sha256; let key_config = crate::KeyConfig::new( 1, - ohttp::hpke::Kem::X25519Sha256, + kem, vec![ohttp::SymmetricSuite::new( ohttp::hpke::Kdf::HkdfSha256, ohttp::hpke::Aead::ChaCha20Poly1305, diff --git a/src/lib.rs b/src/lib.rs index ffaf119..7f82b8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,17 @@ use std::sync::Once; use bhttp::{Message, Mode}; +// The OHTTP implementation. Submodules must import this as `crate::ohttp`: +// a bare `ohttp::` path there names the `ohttp` crate regardless of features. +#[cfg(feature = "secp256k1")] +use bitcoin_ohttp as ohttp; +#[cfg(not(feature = "secp256k1"))] +#[allow(clippy::single_component_path_imports)] +use ohttp; + +#[cfg(all(feature = "secp256k1", target_arch = "wasm32"))] +compile_error!("the `secp256k1` feature does not support wasm32 targets"); + pub use ohttp::KeyConfig; pub use url::Url; @@ -91,12 +102,42 @@ pub const OHTTP_REQ_CONTENT_TYPE: &str = "message/ohttp-req"; /// capabilities; unusable ones are skipped. pub fn parse_key_config(bytes: &[u8]) -> Result { init(); - KeyConfig::decode_list(bytes)? + let configs = KeyConfig::decode_list(bytes)?; + #[cfg(feature = "secp256k1")] + let configs: Vec<_> = configs.iter().filter_map(retain_chacha20poly1305).collect(); + configs .into_iter() .find(has_usable_suite) .ok_or(Error::NoKeyConfig) } +/// Re-decode `config` with only its HKDF-SHA256/ChaCha20Poly1305 suite, or +/// `None` if it lacks one. +/// +/// `bitcoin-ohttp` keeps AES-128-GCM suites that it cannot encapsulate with, +/// and `ClientRequest::from_config` always uses the first suite. +#[cfg(feature = "secp256k1")] +fn retain_chacha20poly1305(config: &KeyConfig) -> Option { + use ohttp::hpke::{Aead, Kdf, Kem}; + + let suite = [ + u16::from(Kdf::HkdfSha256).to_be_bytes(), + u16::from(Aead::ChaCha20Poly1305).to_be_bytes(), + ] + .concat(); + // key_id | kem | pk | suites_len | suites + let encoded = config.encode().ok()?; + let header_len = 1 + 2 + Kem::K256Sha256.n_pk(); + let suites = encoded.get(header_len + 2..)?; + if !suites.chunks_exact(4).any(|s| s == suite) { + return None; + } + let mut retained = encoded[..header_len].to_vec(); + retained.extend_from_slice(&4u16.to_be_bytes()); + retained.extend_from_slice(&suite); + KeyConfig::decode(&retained).ok() +} + /// Whether `config` kept at least one symmetric suite this build supports. /// /// `KeyConfig::decode` strips unsupported KDF/AEAD pairs without rejecting a @@ -364,14 +405,18 @@ impl Response { #[cfg(test)] mod tests { use super::*; + use crate::ohttp; use ohttp::hpke::{Aead, Kdf, Kem}; use ohttp::SymmetricSuite; /// Every KEM `ohttp` implements, including the post-quantum X-Wing. - const SUPPORTED_KEMS: [Kem; 3] = [Kem::X25519Sha256, Kem::P256Sha256, Kem::XWing]; + #[cfg(not(feature = "secp256k1"))] + const SUPPORTED_KEMS: &[Kem] = &[Kem::X25519Sha256, Kem::P256Sha256, Kem::XWing]; + #[cfg(feature = "secp256k1")] + const SUPPORTED_KEMS: &[Kem] = &[Kem::K256Sha256]; fn test_key_config() -> KeyConfig { - key_config_with_kem(Kem::X25519Sha256) + key_config_with_kem(SUPPORTED_KEMS[0]) } fn key_config_with_kem(kem: Kem) -> KeyConfig { @@ -534,7 +579,7 @@ mod tests { #[test] fn parse_key_config_accepts_every_supported_kem() { - for kem in SUPPORTED_KEMS { + for &kem in SUPPORTED_KEMS { let config = key_config_with_kem(kem); let encoded = KeyConfig::encode_list(&[&config]).unwrap(); let parsed = parse_key_config(&encoded).unwrap(); @@ -544,7 +589,7 @@ mod tests { #[test] fn round_trips_with_every_supported_kem() { - for kem in SUPPORTED_KEMS { + for &kem in SUPPORTED_KEMS { let server = ohttp::Server::new(key_config_with_kem(kem)).unwrap(); let client = test_client(server.config().clone()); @@ -672,4 +717,36 @@ mod tests { }) if needed > 1 )); } + + #[cfg(feature = "secp256k1")] + #[test] + fn parse_key_config_skips_aes_128_gcm_for_secp256k1() { + let server = ohttp::Server::new(test_key_config()).unwrap(); + let chacha = server.config().encode().unwrap(); + let header = &chacha[..chacha.len() - 6]; + let with_suites = |aeads: &[Aead]| { + let mut config = header.to_vec(); + config.extend_from_slice(&(4 * aeads.len() as u16).to_be_bytes()); + for &aead in aeads { + config.extend_from_slice(&u16::from(Kdf::HkdfSha256).to_be_bytes()); + config.extend_from_slice(&u16::from(aead).to_be_bytes()); + } + config + }; + + // Alone, AES-128-GCM is unusable even though `bitcoin-ohttp` keeps it. + assert!(matches!( + parse_key_config(&encode_raw_list(&[with_suites(&[Aead::Aes128Gcm])])), + Err(Error::NoKeyConfig) + )); + + // Listed ahead of ChaCha20Poly1305, it is dropped rather than selected. + let both = with_suites(&[Aead::Aes128Gcm, Aead::ChaCha20Poly1305]); + let parsed = parse_key_config(&encode_raw_list(&[both])).unwrap(); + assert_eq!(parsed.encode().unwrap(), chacha); + let (req, _) = test_client(parsed) + .encapsulate("GET", "/", &[], &[], None) + .unwrap(); + assert!(server.decapsulate(&req.body).is_ok()); + } }