Skip to content

Commit

Permalink
fix cfg bugs when using ring feature
Browse files Browse the repository at this point in the history
  • Loading branch information
KJHJason committed Jun 27, 2024
1 parent d37f93d commit 647a2d4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
9 changes: 0 additions & 9 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions rust/src/hkdf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::algorithm::Algorithm;

#[cfg(feature = "rust_crypto")]
#[cfg(not(feature = "ring"))]
use hkdf::Hkdf;

#[cfg(feature = "ring")]
Expand All @@ -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);
Expand All @@ -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<u8> {
match self.algo {
Algorithm::SHA1 => hkdf_expand!(self, ikm, salt, info, sha1::Sha1),
Expand Down
18 changes: 9 additions & 9 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -226,25 +226,25 @@ 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<u8>,
#[cfg(feature = "rust_crypto")]
#[cfg(not(feature = "ring"))]
algo: Algorithm,
#[cfg(feature = "ring")]
expanded_key: hmac::Key,

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)
.expect("HMAC can take key of any size")
};
}

#[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);
Expand All @@ -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);
Expand Down Expand Up @@ -291,7 +291,7 @@ impl HmacSigner {
}

#[inline]
#[cfg(feature = "rust_crypto")]
#[cfg(not(feature = "ring"))]
fn sign_payload(&self, payload: &[u8]) -> Vec<u8> {
match self.algo {
Algorithm::SHA1 => hmac_sign!(self, payload, sha1::Sha1),
Expand All @@ -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),
Expand Down

0 comments on commit 647a2d4

Please sign in to comment.