From 647a2d415a2983cefed4f833e844728cec58c6c8 Mon Sep 17 00:00:00 2001 From: KJHJason Date: Thu, 27 Jun 2024 16:02:54 +0800 Subject: [PATCH] fix cfg bugs when using ring feature --- rust/Cargo.toml | 2 +- rust/README.md | 9 --------- rust/src/hkdf.rs | 6 +++--- rust/src/lib.rs | 18 +++++++++--------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index f65b116..67ee9e2 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hmac-serialiser" -version = "0.3.0" +version = "0.3.1" description = "HMAC Serialisers to cryptographically sign data like Python's ItsDangerous library but in rust." authors = [ "KJHJason ", diff --git a/rust/README.md b/rust/README.md index 85a7f19..d5c0e09 100644 --- a/rust/README.md +++ b/rust/README.md @@ -25,15 +25,6 @@ Additionally, the data serialisation and deserialisation uses the [serde](https: ## Sample Usage -Add this to your `Cargo.toml`: - -```toml -[dependencies] -hmac-serialiser = { version = "0.3.0", features = ["rust_crypto"] } -``` - -Note: `features = ["rust_crypto"]` is optional. If not specified, it will default to `rust_crypto`. - ```rust use hmac_serialiser::{Encoder, HmacSigner, KeyInfo, Payload, Algorithm}; use serde::{Serialize, Deserialize}; diff --git a/rust/src/hkdf.rs b/rust/src/hkdf.rs index d0e0015..90f7186 100644 --- a/rust/src/hkdf.rs +++ b/rust/src/hkdf.rs @@ -1,6 +1,6 @@ use crate::algorithm::Algorithm; -#[cfg(feature = "rust_crypto")] +#[cfg(not(feature = "ring"))] use hkdf::Hkdf; #[cfg(feature = "ring")] @@ -10,7 +10,7 @@ pub struct HkdfWrapper { algo: Algorithm, } -#[cfg(feature = "rust_crypto")] +#[cfg(not(feature = "ring"))] macro_rules! hkdf_expand { ($self:ident, $ikm:ident, $salt:ident, $info:ident, $D:ty) => {{ let hk = Hkdf::<$D>::new(Some($salt), $ikm); @@ -26,7 +26,7 @@ impl HkdfWrapper { Self { algo } } - #[cfg(feature = "rust_crypto")] + #[cfg(not(feature = "ring"))] pub fn expand(&self, ikm: &[u8], salt: &[u8], info: &[u8]) -> Vec { match self.algo { Algorithm::SHA1 => hkdf_expand!(self, ikm, salt, info, sha1::Sha1), diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 15eea43..babdb99 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -3,7 +3,7 @@ //! `hmac-serialiser` is a Rust library for generating and verifying HMAC signatures for secure data transmission. //! //! Regarding the cryptographic implementations, you can choose which implementations to use from via the `features` flag in the `Cargo.toml` file: -//! - `rust_crypto` +//! - `rust_crypto` (default) //! - the underlying [SHA1](https://crates.io/crates/sha1), [SHA2](https://crates.io/crates/sha2), [HMAC](https://crates.io/crates/hmac), and [HKDF](https://crates.io/crates/hkdf) implementations are by [RustCrypto](https://github.com/RustCrypto). //! - `ring` //! - The underlying SHA1, SHA2, HMAC, and HKDF implementations are from the [ring](https://crates.io/crates/ring) crate. @@ -119,7 +119,7 @@ use serde::{Deserialize, Serialize}; pub use algorithm::Algorithm; pub use errors::Error; -#[cfg(feature = "rust_crypto")] +#[cfg(not(feature = "ring"))] use hmac::Mac; #[cfg(feature = "ring")] @@ -226,9 +226,9 @@ impl Default for KeyInfo { /// The `HmacSigner` struct is used for signing and verifying the payload using HMAC signatures. #[derive(Debug, Clone)] pub struct HmacSigner { - #[cfg(feature = "rust_crypto")] + #[cfg(not(feature = "ring"))] expanded_key: Vec, - #[cfg(feature = "rust_crypto")] + #[cfg(not(feature = "ring"))] algo: Algorithm, #[cfg(feature = "ring")] expanded_key: hmac::Key, @@ -236,7 +236,7 @@ pub struct HmacSigner { encoder: general_purpose::GeneralPurpose, } -#[cfg(feature = "rust_crypto")] +#[cfg(not(feature = "ring"))] macro_rules! get_hmac { ($self:ident, $D:ty) => { hmac::Hmac::<$D>::new_from_slice(&$self.expanded_key) @@ -244,7 +244,7 @@ macro_rules! get_hmac { }; } -#[cfg(feature = "rust_crypto")] +#[cfg(not(feature = "ring"))] macro_rules! hmac_sign { ($self:ident, $payload:ident, $D:ty) => {{ let mut mac = get_hmac!($self, $D); @@ -253,7 +253,7 @@ macro_rules! hmac_sign { }}; } -#[cfg(feature = "rust_crypto")] +#[cfg(not(feature = "ring"))] macro_rules! hmac_verify { ($self:ident, $payload:ident, $signature:ident, $D:ty) => {{ let mut mac = get_hmac!($self, $D); @@ -291,7 +291,7 @@ impl HmacSigner { } #[inline] - #[cfg(feature = "rust_crypto")] + #[cfg(not(feature = "ring"))] fn sign_payload(&self, payload: &[u8]) -> Vec { match self.algo { Algorithm::SHA1 => hmac_sign!(self, payload, sha1::Sha1), @@ -302,7 +302,7 @@ impl HmacSigner { } #[inline] - #[cfg(feature = "rust_crypto")] + #[cfg(not(feature = "ring"))] fn verify(&self, payload: &[u8], signature: &[u8]) -> bool { match self.algo { Algorithm::SHA1 => hmac_verify!(self, payload, signature, sha1::Sha1),