Skip to content

Commit

Permalink
do not silently reinit after squeeze
Browse files Browse the repository at this point in the history
  • Loading branch information
initsecret committed Sep 2, 2024
1 parent 70496c9 commit 4ad9249
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions openssl/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ impl Hasher {
/// Feeds data into the hasher.
pub fn update(&mut self, data: &[u8]) -> Result<(), ErrorStack> {
match self.state {
#[cfg(ossl330)]
Squeeze => self.init()?,
Finalized => self.init()?,
_ => {}
}
Expand All @@ -301,9 +299,6 @@ impl Hasher {
/// The output will be as long as the buf.
#[cfg(ossl330)]
pub fn squeeze_xof(&mut self, buf: &mut [u8]) -> Result<(), ErrorStack> {
if self.state == Finalized {
self.init()?;
}
unsafe {
cvt(ffi::EVP_DigestSqueeze(
self.ctx,
Expand All @@ -318,8 +313,6 @@ impl Hasher {
/// Returns the hash of the data written and resets the non-XOF hasher.
pub fn finish(&mut self) -> Result<DigestBytes, ErrorStack> {
match self.state {
#[cfg(ossl330)]
Squeeze => self.init()?,
Finalized => self.init()?,
_ => {}
}
Expand Down Expand Up @@ -347,8 +340,6 @@ impl Hasher {
#[cfg(ossl111)]
pub fn finish_xof(&mut self, buf: &mut [u8]) -> Result<(), ErrorStack> {
match self.state {
#[cfg(ossl330)]
Squeeze => self.init()?,
Finalized => self.init()?,
_ => {}
}
Expand Down Expand Up @@ -595,9 +586,7 @@ mod tests {
let mut h = Hasher::new(digest).unwrap();
let mut buf = vec![0; digest.size()];
h.finish_xof(&mut buf).unwrap();
h.squeeze_xof(&mut buf).unwrap();
let null = hash(digest, &[]).unwrap();
assert_eq!(&*buf, &*null);
h.squeeze_xof(&mut buf).expect_err("squeezing after finalize should fail");
}

#[cfg(ossl330)]
Expand All @@ -608,10 +597,18 @@ mod tests {
let mut h = Hasher::new(digest).unwrap();
let mut buf = vec![0; digest.size()];
h.squeeze_xof(&mut buf).unwrap();
h.update(&data).unwrap();
h.update(&data).expect_err("updating after squeeze should fail");
}

#[cfg(ossl330)]
#[test]
fn test_squeeze_then_finalize() {
let digest = MessageDigest::shake_128();
let data = Vec::from_hex(MD5_TESTS[6].0).unwrap();
let mut h = Hasher::new(digest).unwrap();
let mut buf = vec![0; digest.size()];
h.squeeze_xof(&mut buf).unwrap();
let null = hash(digest, &data).unwrap();
assert_eq!(&*buf, &*null);
h.finish_xof(&mut buf).expect_err("finalize after squeeze should fail");
}

#[test]
Expand Down

0 comments on commit 4ad9249

Please sign in to comment.