diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d820dfbb..99625501 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -114,6 +114,8 @@ jobs: persist-credentials: false - name: Test run: cargo test --no-fail-fast + - name: Test with the boring backend + run: cargo test --no-fail-fast -p reqsign-core -p reqsign-aws-v4 --features reqsign-core/boring env: RUST_LOG: DEBUG RUST_BACKTRACE: full diff --git a/Cargo.toml b/Cargo.toml index 94ccb168..7816100f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ aes-gcm = "0.11" anyhow = "1" asyncband = { version = "0.8.0", features = ["mutex"] } base64 = { version = "0.23", default-features = false, features = ["std"] } +boring = "5" bytes = "1" criterion = { version = "0.8.1", features = ["async_tokio", "html_reports"] } dotenvy = "0.15" diff --git a/README.md b/README.md index 1e60a136..c25c3c18 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ let signer = aliyun::default_signer("mybucket"); ## Features - Pure Rust with minimal dependencies. +- Optional `boring` feature (on `reqsign` or `reqsign-core`) that computes the SHA and HMAC behind signing with BoringSSL instead of RustCrypto. - Tested against official SDKs and services. - Supported services - Aliyun OSS: `reqsign-aliyun-oss` diff --git a/core/Cargo.toml b/core/Cargo.toml index dc3a0428..dc606f3e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -29,6 +29,7 @@ rust-version.workspace = true [features] default = [] +boring = ["dep:boring"] jwt = ["dep:rsa", "dep:serde", "dep:serde_json"] [dependencies] @@ -46,6 +47,7 @@ sha1 = { workspace = true } sha2 = { workspace = true } # Optional dependencies +boring = { workspace = true, optional = true } rsa = { workspace = true, optional = true } serde = { workspace = true, optional = true } serde_json = { workspace = true, optional = true } diff --git a/core/src/hash.rs b/core/src/hash.rs index 7300d8d5..3e4662d8 100644 --- a/core/src/hash.rs +++ b/core/src/hash.rs @@ -16,16 +16,14 @@ // under the License. //! Hash related utils. +//! +//! SHA and HMAC use RustCrypto by default. With the `boring` feature they use +//! BoringSSL instead, through the [`boring`](https://docs.rs/boring) crate, for +//! applications that standardize their cryptography on BoringSSL. use crate::Error; use base64::Engine; use base64::prelude::BASE64_STANDARD; -use hmac::Hmac; -use hmac::KeyInit; -use hmac::Mac; -use sha1::Sha1; -use sha2::Digest; -use sha2::Sha256; /// Base64 encode pub fn base64_encode(content: &[u8]) -> String { @@ -44,7 +42,7 @@ pub fn base64_decode(content: &str) -> crate::Result> { /// Use this function instead of `hex::encode(sha1(content))` can reduce /// extra copy. pub fn hex_sha1(content: &[u8]) -> String { - hex::encode(Sha1::digest(content)) + hex::encode(backend::sha1(content)) } /// Hex encoded SHA256 hash. @@ -52,25 +50,17 @@ pub fn hex_sha1(content: &[u8]) -> String { /// Use this function instead of `hex::encode(sha256(content))` can reduce /// extra copy. pub fn hex_sha256(content: &[u8]) -> String { - hex::encode(Sha256::digest(content)) + hex::encode(backend::sha256(content)) } /// HMAC with SHA256 hash. pub fn hmac_sha256(key: &[u8], content: &[u8]) -> Vec { - // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length - let mut h = Hmac::::new_from_slice(key).unwrap(); - h.update(content); - - h.finalize().into_bytes().to_vec() + backend::hmac_sha256(key, content).to_vec() } /// Base64 encoded HMAC with SHA256 hash. pub fn base64_hmac_sha256(key: &[u8], content: &[u8]) -> String { - // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length - let mut h = Hmac::::new_from_slice(key).unwrap(); - h.update(content); - - base64_encode(&h.finalize().into_bytes()) + base64_encode(&backend::hmac_sha256(key, content)) } /// Hex encoded HMAC with SHA1 hash. @@ -78,11 +68,7 @@ pub fn base64_hmac_sha256(key: &[u8], content: &[u8]) -> String { /// Use this function instead of `hex::encode(hmac_sha1(key, content))` can /// reduce extra copy. pub fn hex_hmac_sha1(key: &[u8], content: &[u8]) -> String { - // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length - let mut h = Hmac::::new_from_slice(key).unwrap(); - h.update(content); - - hex::encode(h.finalize().into_bytes()) + hex::encode(backend::hmac_sha1(key, content)) } /// Hex encoded HMAC with SHA256 hash. @@ -90,18 +76,66 @@ pub fn hex_hmac_sha1(key: &[u8], content: &[u8]) -> String { /// Use this function instead of `hex::encode(hmac_sha256(key, content))` can /// reduce extra copy. pub fn hex_hmac_sha256(key: &[u8], content: &[u8]) -> String { - // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length - let mut h = Hmac::::new_from_slice(key).unwrap(); - h.update(content); - - hex::encode(h.finalize().into_bytes()) + hex::encode(backend::hmac_sha256(key, content)) } /// Base64 encoded HMAC with SHA1 hash. pub fn base64_hmac_sha1(key: &[u8], content: &[u8]) -> String { - // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length - let mut h = Hmac::::new_from_slice(key).unwrap(); - h.update(content); + base64_encode(&backend::hmac_sha1(key, content)) +} + +#[cfg(not(feature = "boring"))] +mod backend { + use hmac::Hmac; + use hmac::KeyInit; + use hmac::Mac; + use sha1::Sha1; + use sha2::Digest; + use sha2::Sha256; + + pub(super) fn sha1(content: &[u8]) -> [u8; 20] { + Sha1::digest(content).into() + } + + pub(super) fn sha256(content: &[u8]) -> [u8; 32] { + Sha256::digest(content).into() + } + + pub(super) fn hmac_sha1(key: &[u8], content: &[u8]) -> [u8; 20] { + // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length + let mut h = Hmac::::new_from_slice(key).unwrap(); + h.update(content); + h.finalize().into_bytes().into() + } + + pub(super) fn hmac_sha256(key: &[u8], content: &[u8]) -> [u8; 32] { + // SAFETY: HMAC's new_from_slice always returns Ok - it handles any key length + let mut h = Hmac::::new_from_slice(key).unwrap(); + h.update(content); + h.finalize().into_bytes().into() + } +} + +#[cfg(feature = "boring")] +mod backend { + use boring::hash::MessageDigest; + use boring::hash::hash; + + pub(super) fn sha1(content: &[u8]) -> [u8; 20] { + let digest = hash(MessageDigest::sha1(), content).expect("SHA-1 does not fail"); + digest[..].try_into().expect("SHA-1 digest is 20 bytes") + } + + pub(super) fn sha256(content: &[u8]) -> [u8; 32] { + let digest = hash(MessageDigest::sha256(), content).expect("SHA-256 does not fail"); + digest[..].try_into().expect("SHA-256 digest is 32 bytes") + } + + pub(super) fn hmac_sha1(key: &[u8], content: &[u8]) -> [u8; 20] { + boring::hash::hmac_sha1(key, content).expect("HMAC-SHA1 does not fail") + } - base64_encode(&h.finalize().into_bytes()) + pub(super) fn hmac_sha256(key: &[u8], content: &[u8]) -> [u8; 32] { + boring::hash::hmac_sha256(key, content).expect("HMAC-SHA256 does not fail") + } } diff --git a/reqsign/Cargo.toml b/reqsign/Cargo.toml index 9d71b1f0..58ba291b 100644 --- a/reqsign/Cargo.toml +++ b/reqsign/Cargo.toml @@ -62,6 +62,9 @@ default-context = [ "dep:reqsign-http-send-reqwest", ] +# Use BoringSSL for the SHA and HMAC in reqsign-core. +boring = ["reqsign-core/boring"] + # Service features aliyun = ["dep:reqsign-aliyun-oss"] aws = ["aws-v4"]