Skip to content

Commit

Permalink
remove hash state wrapper for sha2
Browse files Browse the repository at this point in the history
  • Loading branch information
keks committed Nov 5, 2024
1 parent eec025e commit 5e48543
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 70 deletions.
69 changes: 4 additions & 65 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,72 +259,11 @@ pub fn sha2_512(payload: &[u8]) -> Sha2_512Digest {

// Streaming API - This is the recommended one.
// For implementations based on hacl_rs (over hacl-c)
macro_rules! impl_streaming_hacl_rs {
($name:ident, $state:ty, $result:ty) => {
#[derive(Clone)]
pub struct $name {
state: $state,
}
impl $name {
/// Return the digest for the given input byte slice, in immediate mode.
pub fn hash(digest: &mut [u8], input: &[u8]) {
<$state>::hash(digest, input)
}

/// Initialize a new digest state.
pub fn new() -> Self {
Self {
state: <$state>::new(),
}
}

/// Add the `payload` to the digest.
pub fn update(&mut self, payload: &[u8]) {
self.state.update(payload);
}

/// Get the digest.
///
/// Note that the digest state can be continued to be used, to extend the
/// digest.
pub fn finish(&self, digest: &mut $result) {
self.state.finish(digest)
}

/// Reset the digest state.
pub fn reset(&mut self) {
self.state.reset()
}
}

impl Default for $name {
fn default() -> Self {
Self::new()
}
}
};
}
pub use crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha256 as Sha2_256;
pub use crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha256_224 as Sha2_224;
pub use crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha512 as Sha2_512;
pub use crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha512_384 as Sha2_384;

impl_streaming_hacl_rs!(
Sha2_224,
crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha256_224,
Sha2_224Digest
);
impl_streaming_hacl_rs!(
Sha2_256,
crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha256,
Sha2_256Digest
);
impl_streaming_hacl_rs!(
Sha2_384,
crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha512_384,
Sha2_384Digest
);
impl_streaming_hacl_rs!(
Sha2_512,
crate::hacl_rs::hash_sha2::HaclRs_Sha2_Sha512,
Sha2_512Digest
);
// SHAKE messages from SHA 3

#[cfg(simd256)]
Expand Down
6 changes: 3 additions & 3 deletions src/hacl_rs/hash_sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,8 +1264,8 @@ macro_rules! impl_hash {

impl $name {
/// Return the digest for the given input byte slice, in immediate mode.
pub fn hash(digest: &mut [u8], input: &[u8]) {
$hash(digest, input, input.len() as u32)
pub fn hash(digest: &mut [u8; $digest_size], payload: &[u8]) {
$hash(digest, payload, payload.len() as u32)
}

/// Initialize a new digest state for streaming use.
Expand All @@ -1282,7 +1282,7 @@ macro_rules! impl_hash {
///
/// Note that the digest state can be continued to be used, to extend the
/// digest.
pub fn finish(&self, digest: &mut [u8]) {
pub fn finish(&self, digest: &mut [u8; $digest_size]) {
$finish(self.state.as_ref(), digest);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn sha256_kat_streaming() {
digest.finish(&mut d);

let expected = "8683520e19e5b33db33c8fb90918c0c96fcdfd9a17c695ce0f0ea2eaa0c95956";
assert_eq!(hex::encode(&d), expected);
assert_eq!(hex::encode(d), expected);
}

// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
Expand All @@ -18,7 +18,7 @@ fn sha256_kat_oneshot() {
let d = libcrux::digest::sha2_256(b"libcrux sha2 256 tests");

let expected = "8683520e19e5b33db33c8fb90918c0c96fcdfd9a17c695ce0f0ea2eaa0c95956";
assert_eq!(hex::encode(&d), expected);
assert_eq!(hex::encode(d), expected);
}

#[test]
Expand Down

0 comments on commit 5e48543

Please sign in to comment.