Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Lagergren <[email protected]>
  • Loading branch information
elagergren-spideroak committed Jan 24, 2025
1 parent 63c2b04 commit aa0f135
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 296 deletions.
33 changes: 33 additions & 0 deletions crates/acvp/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,36 @@ macro_rules! dprintln {
};
}
pub(crate) use dprintln;

macro_rules! ensure_eq {
($left:expr, $right:expr $(,)?) => {
match (&$left, &$right) {
(left_val, right_val) => {
if (*left_val != *right_val) {
::anyhow::bail!(r#"
left: {:?}
right: {:?}"#,
&*left_val,
&*right_val,
);
}
}
}
};
($left:expr, $right:expr, $($arg:tt)+) => {
match (&$left, &$right) {
(left_val, right_val) => {
if (*left_val != *right_val) {
let args = ::alloc::format!($($arg)+);
::anyhow::bail!(r#"{args}
left: {:?}
right: {:?}"#,
&*left_val,
&*right_val,
);
}
}
}
};
}
pub(crate) use ensure_eq;
6 changes: 4 additions & 2 deletions crates/acvp/src/vectors/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

use alloc::{format, string::String, vec::Vec};

use anyhow::{ensure, Context};
use anyhow::Context;
use serde::{Deserialize, Serialize};

use crate::util::ensure_eq;

super::define_tests! {
Sha2_256 => "sha2_256",
Sha2_512 => "sha2_512",
Expand Down Expand Up @@ -91,7 +93,7 @@ where
} in &tests
{
let got = f(key, msg).with_context(|| format!("#{tc_id}: `F` failed"))?;
ensure!(got.as_ref() == md, "#{tc_id}");
ensure_eq!(got.as_ref(), md, "#{tc_id}");
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions crates/acvp/src/vectors/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use alloc::{format, string::String, vec::Vec};
use core::fmt;

use anyhow::{ensure, Context};
use anyhow::Context;
use serde::{Deserialize, Serialize};

use crate::traits::Hash;
use crate::{traits::Hash, util::ensure_eq};

super::define_tests! {
Sha2_256 => "sha2_256",
Expand Down Expand Up @@ -194,12 +194,13 @@ pub fn test<H: Hash>(vectors: &TestVectors) -> anyhow::Result<()> {
len,
} in tests.iter()
{
ensure!(
len % 8 == 0,
ensure_eq!(
len % 8,
0,
"`len` must be padded to full bytes, got `{len}`"
);
let got = <H>::hash(msg);
ensure!(got.as_ref() == md, "#{tc_id}");
ensure_eq!(got.as_ref(), md, "#{tc_id}");
}
}
Tests::Mct(tests) => {
Expand All @@ -212,8 +213,9 @@ pub fn test<H: Hash>(vectors: &TestVectors) -> anyhow::Result<()> {
{
let mct = MctIter::<H>::new(msg, group.mct_version.is_alt());
for (j, (got, want)) in mct.zip(results_array).enumerate() {
ensure!(
got.as_ref() == want.md,
ensure_eq!(
got.as_ref(),
want.md,
"#{tc_id}: j={j} vers={}",
group.mct_version
);
Expand All @@ -232,7 +234,7 @@ pub fn test<H: Hash>(vectors: &TestVectors) -> anyhow::Result<()> {
let got = ldt
.run::<H>(&large_msg.content, total_bytes)
.with_context(|| format!("{tc_id}"))?;
ensure!(got.as_ref() == md, "#{tc_id}");
ensure_eq!(got.as_ref(), md, "#{tc_id}");
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions crates/acvp/src/vectors/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use alloc::{format, vec::Vec};
use core::fmt;

use anyhow::{ensure, Context};
use anyhow::Context;
use either::Either;
use serde::{Deserialize, Serialize};

use crate::traits::Hash;
use crate::{traits::Hash, util::ensure_eq};

super::define_tests! {
Sha3_256 => "sha3_256",
Expand Down Expand Up @@ -189,12 +189,13 @@ pub fn test<H: Hash>(vectors: &TestVectors) -> anyhow::Result<()> {
len,
} in tests.iter()
{
ensure!(
len % 8 == 0,
ensure_eq!(
len % 8,
0,
"`len` must be padded to full bytes, got `{len}`"
);
let got = <H>::hash(msg);
ensure!(got.as_ref() == md, "#{tc_id}");
ensure_eq!(got.as_ref(), md, "#{tc_id}");
}
}
Tests::Mct(tests) => {
Expand All @@ -210,8 +211,9 @@ pub fn test<H: Hash>(vectors: &TestVectors) -> anyhow::Result<()> {
MctVersion::Alternate => Either::Right(AltMctIter::<H>::new(msg)),
};
for (j, (got, want)) in mct.zip(results_array).enumerate() {
ensure!(
got.as_ref() == want.md,
ensure_eq!(
got.as_ref(),
want.md,
"#{tc_id}: j={j} vers={}",
group.mct_version
);
Expand All @@ -230,7 +232,7 @@ pub fn test<H: Hash>(vectors: &TestVectors) -> anyhow::Result<()> {
let got = ldt
.run::<H>(&large_msg.content, total_bytes)
.with_context(|| format!("{tc_id}"))?;
ensure!(got.as_ref() == md, "#{tc_id}");
ensure_eq!(got.as_ref(), md, "#{tc_id}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,5 @@ mod tests {
use super::*;
use crate::test_util::test_signer;

test_signer!(ed25519, Ed25519, EddsaTest::Ed25519);
test_signer!(mod ed25519, Ed25519, Ed25519);
}
11 changes: 8 additions & 3 deletions crates/crypto/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl<H: Hash + BlockSize> Hkdf<H> {
///
/// ```rust
/// use spideroak_crypto::{
/// block::BlockSize,
/// hash::{Block, Digest, Hash, HashId},
/// hkdf_impl,
/// typenum::U32,
Expand All @@ -161,6 +162,10 @@ impl<H: Hash + BlockSize> Hkdf<H> {
/// }
/// }
///
/// impl BlockSize for Sha256 {
/// type BlockSize = U32;
/// }
///
/// hkdf_impl!(HkdfSha256, "HMAC-SHA-256", Sha256);
/// ```
#[macro_export]
Expand Down Expand Up @@ -212,9 +217,9 @@ mod tests {
hkdf_impl!(HkdfSha384, "HKDF-SHA384", Sha384);
hkdf_impl!(HkdfSha512, "HKDF-SHA512", Sha512);

test_kdf!(hkdf_sha256, HkdfSha256, HkdfTest::HkdfSha256);
test_kdf!(hkdf_sha384, HkdfSha384, HkdfTest::HkdfSha384);
test_kdf!(hkdf_sha512, HkdfSha512, HkdfTest::HkdfSha512);
test_kdf!(mod hkdf_sha256, HkdfSha256, HKDF_SHA_256);
test_kdf!(mod hkdf_sha384, HkdfSha384, HKDF_SHA_384);
test_kdf!(mod hkdf_sha512, HkdfSha512, HKDF_SHA_512);
};
}

Expand Down
10 changes: 7 additions & 3 deletions crates/crypto/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ impl<'a, N: ArrayLength> TryFrom<&'a [u8]> for Tag<N> {
/// }
/// }
///
/// impl BlockSize for Sha256 {
/// type BlockSize = U32;
/// }
///
/// hmac_impl!(HmacSha256, "HMAC-SHA-256", Sha256);
/// ```
#[macro_export]
Expand Down Expand Up @@ -235,9 +239,9 @@ mod tests {
hmac_impl!(HmacSha384, "HMAC-SHA384", Sha384);
hmac_impl!(HmacSha512, "HMAC-SHA512", Sha512);

test_mac!(hmac_sha256, HmacSha256, MacTest::HmacSha256);
test_mac!(hmac_sha384, HmacSha384, MacTest::HmacSha384);
test_mac!(hmac_sha512, HmacSha512, MacTest::HmacSha512);
test_mac!(mod hmac_sha256, HmacSha256, HMAC_SHA_256);
test_mac!(mod hmac_sha384, HmacSha384, HMAC_SHA_384);
test_mac!(mod hmac_sha512, HmacSha512, HMAC_SHA_512);
};
}

Expand Down
67 changes: 31 additions & 36 deletions crates/crypto/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ macro_rules! hash_impl {
hash_impl!(Sha256, "SHA2-256");
hash_impl!(Sha384, "SHA2-384");
hash_impl!(Sha512, "SHA2-512");
hash_impl!(Sha512_256, "SHA2-512-256");

hkdf_impl!(HkdfSha256, "HKDF-SHA256", Sha256);
hkdf_impl!(HkdfSha384, "HKDF-SHA384", Sha384);
Expand Down Expand Up @@ -586,46 +587,39 @@ mod tests {
}
}

// mod ecdh_tests {
// use super::*;
// use crate::test_util::vectors::{test_ecdh, EcdhTest};

// #[test]
// fn test_ecdh_p256() {
// test_ecdh::<P256>(EcdhTest::EcdhSecp256r1Ecpoint);
// }
mod ecdh_tests {
use super::*;
use crate::test_util::test_ecdh;

// #[test]
// fn test_ecdh_p384() {
// test_ecdh::<P384>(EcdhTest::EcdhSecp384r1Ecpoint);
// }
// }
test_ecdh!(mod p256, P256, ECDH_secp256r1);
test_ecdh!(mod p384, P384, ECDH_secp384r1);
}

// mod ecdsa_tests {
// use super::*;
// use crate::test_util::test_signer;
mod ecdsa_tests {
use super::*;
use crate::test_util::test_signer;

// test_signer!(p256, P256, EcdsaTest::EcdsaSecp256r1Sha256);
// test_signer!(p384, P384, EcdsaTest::EcdsaSecp384r1Sha384);
// }
test_signer!(mod p256, P256, ECDSA_secp256r1_SHA_256);
test_signer!(mod p384, P384, ECDSA_secp384r1_SHA_384);
}

// mod hkdf_tests {
// use super::*;
// use crate::test_util::test_kdf;
mod hkdf_tests {
use super::*;
use crate::test_util::test_kdf;

// test_kdf!(test_hkdf_sha256, HkdfSha256, HkdfTest::HkdfSha256);
// test_kdf!(test_hkdf_sha384, HkdfSha384, HkdfTest::HkdfSha384);
// test_kdf!(test_hkdf_sha512, HkdfSha512, HkdfTest::HkdfSha512);
// }
test_kdf!(mod hkdf_sha256, HkdfSha256, HKDF_SHA_256);
test_kdf!(mod hkdf_sha384, HkdfSha384, HKDF_SHA_384);
test_kdf!(mod hkdf_sha512, HkdfSha512, HKDF_SHA_512);
}

// mod hmac_tests {
// use super::*;
// use crate::test_util::test_mac;
mod hmac_tests {
use super::*;
use crate::test_util::test_mac;

// test_mac!(test_hmac_sha256, HmacSha256, MacTest::HmacSha256);
// test_mac!(test_hmac_sha384, HmacSha384, MacTest::HmacSha384);
// test_mac!(test_hmac_sha512, HmacSha512, MacTest::HmacSha512);
// }
test_mac!(mod hmac_sha256, HmacSha256, HMAC_SHA_256);
test_mac!(mod hmac_sha384, HmacSha384, HMAC_SHA_384);
test_mac!(mod hmac_sha512, HmacSha512, HMAC_SHA_512);
}

// mod hpke_tests {
// use super::*;
Expand All @@ -651,8 +645,9 @@ mod tests {
use super::*;
use crate::test_util::test_hash;

test_hash!(sha2_256, Sha256, SHA2_256);
test_hash!(sha2_384, Sha384, SHA2_384);
test_hash!(sha2_512, Sha512, SHA2_512);
test_hash!(mod sha2_256, Sha256, SHA2_256);
test_hash!(mod sha2_384, Sha384, SHA2_384);
test_hash!(mod sha2_512, Sha512, SHA2_512);
test_hash!(mod sha2_512_256, Sha512_256, SHA2_512_256);
}
}
18 changes: 9 additions & 9 deletions crates/crypto/src/sha3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ impl_sha3!(Sha3_256, "SHA3-256");
impl_sha3!(Sha3_384, "SHA3-384");
impl_sha3!(Sha3_512, "SHA3-512");

// #[cfg(test)]
// mod tests {
// use super::*;
// use crate::test_util::test_hash;

// test_hash!(sha3_256, Sha3_256, HashTest::Sha3_256);
// test_hash!(sha3_384, Sha3_384);
// test_hash!(sha3_512, Sha3_512);
// }
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::test_hash;

test_hash!(mod sha3_256, Sha3_256, SHA3_256);
test_hash!(mod sha3_384, Sha3_384, SHA3_384);
test_hash!(mod sha3_512, Sha3_512, SHA3_512);
}
Loading

0 comments on commit aa0f135

Please sign in to comment.