From 01a4f68e204a05bbad06eea2d127c2e01a23a94d Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Tue, 27 Aug 2024 15:11:34 +0100 Subject: [PATCH] Make rust-tls work (through ring) --- Cargo.lock | 1 + crates/bencode/Cargo.toml | 4 ++ crates/librqbit/Cargo.toml | 6 +- crates/peer_binary_protocol/Cargo.toml | 1 - crates/sha1w/Cargo.toml | 10 +++- crates/sha1w/src/lib.rs | 76 ++++++++++++++++++++------ crates/upnp-serve/Cargo.toml | 6 +- 7 files changed, 77 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de6d2d3..59d0fce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1661,6 +1661,7 @@ name = "librqbit-sha1-wrapper" version = "3.0.0" dependencies = [ "crypto-hash", + "ring", ] [[package]] diff --git a/crates/bencode/Cargo.toml b/crates/bencode/Cargo.toml index b544d50..0e77460 100644 --- a/crates/bencode/Cargo.toml +++ b/crates/bencode/Cargo.toml @@ -9,6 +9,10 @@ repository = "https://github.com/ikatson/rqbit" readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["sha1-crypto-hash"] +sha1-crypto-hash = ["sha1w/sha1-crypto-hash"] +sha1-ring = ["sha1w/sha1-ring"] [dependencies] serde = { version = "1", features = ["derive"] } diff --git a/crates/librqbit/Cargo.toml b/crates/librqbit/Cargo.toml index 0fb0517..660843a 100644 --- a/crates/librqbit/Cargo.toml +++ b/crates/librqbit/Cargo.toml @@ -17,8 +17,8 @@ http-api = ["axum", "tower-http"] upnp-serve-adapter = ["upnp-serve"] webui = [] timed_existence = [] -default-tls = ["reqwest/default-tls"] -rust-tls = ["reqwest/rustls-tls"] +default-tls = ["reqwest/default-tls", "sha1w/sha1-crypto-hash"] +rust-tls = ["reqwest/rustls-tls", "sha1w/sha1-ring"] storage_middleware = ["lru"] storage_examples = [] tracing-subscriber-utils = ["tracing-subscriber"] @@ -35,7 +35,7 @@ tracker_comms = { path = "../tracker_comms", default-features = false, package = buffers = { path = "../buffers", package = "librqbit-buffers", version = "4.1" } librqbit-core = { path = "../librqbit_core", version = "4" } clone_to_owned = { path = "../clone_to_owned", package = "librqbit-clone-to-owned", version = "3" } -peer_binary_protocol = { path = "../peer_binary_protocol", package = "librqbit-peer-protocol", version = "4.1" } +peer_binary_protocol = { path = "../peer_binary_protocol", default-features = false, package = "librqbit-peer-protocol", version = "4.1" } sha1w = { path = "../sha1w", default-features = false, package = "librqbit-sha1-wrapper", version = "3.0.0" } dht = { path = "../dht", package = "librqbit-dht", version = "5.1.0" } librqbit-upnp = { path = "../upnp", version = "0.1.1" } diff --git a/crates/peer_binary_protocol/Cargo.toml b/crates/peer_binary_protocol/Cargo.toml index f84d24a..370fae9 100644 --- a/crates/peer_binary_protocol/Cargo.toml +++ b/crates/peer_binary_protocol/Cargo.toml @@ -9,7 +9,6 @@ repository = "https://github.com/ikatson/rqbit" readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] serde = { version = "1", features = ["derive"] } bincode = "1" diff --git a/crates/sha1w/Cargo.toml b/crates/sha1w/Cargo.toml index e72d9bc..dbf5329 100644 --- a/crates/sha1w/Cargo.toml +++ b/crates/sha1w/Cargo.toml @@ -8,7 +8,13 @@ documentation = "https://docs.rs/librqbit-sha1-wrapper" repository = "https://github.com/ikatson/rqbit" readme = "README.md" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html# + +[features] +default = ["sha1-crypto-hash"] +sha1-crypto-hash = ["crypto-hash"] +sha1-ring = ["ring"] [dependencies] -crypto-hash = "0.3" +crypto-hash = { version = "0.3", optional = true } +ring = { version = "0.17", optional = true } diff --git a/crates/sha1w/src/lib.rs b/crates/sha1w/src/lib.rs index 0414ede..1982a07 100644 --- a/crates/sha1w/src/lib.rs +++ b/crates/sha1w/src/lib.rs @@ -5,35 +5,75 @@ // openssl is 2-3x faster than rust's sha1. // system library is the best choice probably (it's the default anyway). -pub type Sha1 = Sha1System; - pub trait ISha1 { fn new() -> Self; fn update(&mut self, buf: &[u8]); fn finish(self) -> [u8; 20]; } -pub struct Sha1System { - inner: crypto_hash::Hasher, -} +#[cfg(feature = "sha1-crypto-hash")] +mod crypto_hash_impl { + use super::ISha1; -impl ISha1 for Sha1System { - fn new() -> Self { - Self { - inner: crypto_hash::Hasher::new(crypto_hash::Algorithm::SHA1), + pub struct Sha1CryptoHash { + inner: crypto_hash::Hasher, + } + + impl ISha1 for Sha1CryptoHash { + fn new() -> Self { + Self { + inner: crypto_hash::Hasher::new(crypto_hash::Algorithm::SHA1), + } + } + + fn update(&mut self, buf: &[u8]) { + use std::io::Write; + self.inner.write_all(buf).unwrap(); + } + + fn finish(mut self) -> [u8; 20] { + let result = self.inner.finish(); + debug_assert_eq!(result.len(), 20); + let mut result_arr = [0u8; 20]; + result_arr.copy_from_slice(&result); + result_arr } } +} - fn update(&mut self, buf: &[u8]) { - use std::io::Write; - self.inner.write_all(buf).unwrap(); +#[cfg(feature = "sha1-ring")] +mod ring_impl { + use super::ISha1; + + use ring::digest::{Context, SHA1_FOR_LEGACY_USE_ONLY as SHA1}; + + pub struct Sha1Ring { + ctx: Context, } - fn finish(mut self) -> [u8; 20] { - let result = self.inner.finish(); - debug_assert_eq!(result.len(), 20); - let mut result_arr = [0u8; 20]; - result_arr.copy_from_slice(&result); - result_arr + impl ISha1 for Sha1Ring { + fn new() -> Self { + Self { + ctx: Context::new(&SHA1), + } + } + + fn update(&mut self, buf: &[u8]) { + self.ctx.update(buf); + } + + fn finish(self) -> [u8; 20] { + let result = self.ctx.finish(); + debug_assert_eq!(result.as_ref().len(), 20); + let mut result_arr = [0u8; 20]; + result_arr.copy_from_slice(result.as_ref()); + result_arr + } } } + +#[cfg(feature = "sha1-crypto-hash")] +pub type Sha1 = crypto_hash_impl::Sha1CryptoHash; + +#[cfg(feature = "sha1-ring")] +pub type Sha1 = ring_impl::Sha1Ring; diff --git a/crates/upnp-serve/Cargo.toml b/crates/upnp-serve/Cargo.toml index 3c2601f..f9c0efb 100644 --- a/crates/upnp-serve/Cargo.toml +++ b/crates/upnp-serve/Cargo.toml @@ -13,10 +13,10 @@ quick-xml = "0.36.1" http = "1.1.0" httparse = "1.9.4" uuid = { version = "1.10.0", features = ["v4"] } -librqbit-upnp = { path = "../upnp" } +librqbit-upnp = { path = "../upnp", default-features = false } gethostname = "0.5.0" -librqbit-sha1-wrapper = { path = "../sha1w" } -librqbit-core = { path = "../librqbit_core" } +librqbit-sha1-wrapper = { path = "../sha1w", default-features = false } +librqbit-core = { path = "../librqbit_core", default-features = false } mime_guess = "2.0.5" url = "2.5.2" parking_lot = "0.12.3"