Skip to content

Commit 1915d62

Browse files
committed
Add support for XOF squeeze
1 parent fe8088e commit 1915d62

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

openssl/src/hash.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ unsafe impl Send for MessageDigest {}
199199
enum State {
200200
Reset,
201201
Updated,
202+
Squeeze,
202203
Finalized,
203204
}
204205

@@ -265,6 +266,7 @@ impl Hasher {
265266
Updated => {
266267
self.finish()?;
267268
}
269+
Squeeze => (),
268270
Finalized => (),
269271
}
270272
unsafe {
@@ -290,6 +292,21 @@ impl Hasher {
290292
Ok(())
291293
}
292294

295+
/// Squeezes buf out of the hasher.
296+
/// The output will be as long as the buf.
297+
#[cfg(ossl330)]
298+
pub fn squeeze_xof(&mut self, buf: &mut [u8]) -> Result<(), ErrorStack> {
299+
unsafe {
300+
cvt(ffi::EVP_DigestSqueeze(
301+
self.ctx,
302+
buf.as_mut_ptr(),
303+
buf.len(),
304+
))?;
305+
self.state = Squeeze;
306+
Ok(())
307+
}
308+
}
309+
293310
/// Returns the hash of the data written and resets the non-XOF hasher.
294311
pub fn finish(&mut self) -> Result<DigestBytes, ErrorStack> {
295312
if self.state == Finalized {
@@ -486,6 +503,21 @@ mod tests {
486503
assert_eq!(buf, expected);
487504
}
488505

506+
/// Squeezes the expected length by doing two squeezes.
507+
#[cfg(ossl330)]
508+
fn hash_xof_squeeze_test(hashtype: MessageDigest, hashtest: &(&str, &str)) {
509+
let data = Vec::from_hex(hashtest.0).unwrap();
510+
let mut h = Hasher::new(hashtype).unwrap();
511+
h.update(&data).unwrap();
512+
513+
let expected = Vec::from_hex(hashtest.1).unwrap();
514+
let mut buf = vec![0; expected.len()];
515+
assert!(expected.len() > 10);
516+
h.squeeze_xof(&mut buf[..10]).unwrap();
517+
h.squeeze_xof(&mut buf[10..]).unwrap();
518+
assert_eq!(buf, expected);
519+
}
520+
489521
fn hash_recycle_test(h: &mut Hasher, hashtest: &(&str, &str)) {
490522
h.write_all(&Vec::from_hex(hashtest.0).unwrap()).unwrap();
491523
let res = h.finish().unwrap();
@@ -715,6 +747,8 @@ mod tests {
715747

716748
for test in tests.iter() {
717749
hash_xof_test(MessageDigest::shake_128(), test);
750+
#[cfg(ossl330)]
751+
hash_xof_squeeze_test(MessageDigest::shake_128(), test);
718752
}
719753

720754
assert_eq!(MessageDigest::shake_128().block_size(), 168);
@@ -735,6 +769,8 @@ mod tests {
735769

736770
for test in tests.iter() {
737771
hash_xof_test(MessageDigest::shake_256(), test);
772+
#[cfg(ossl330)]
773+
hash_xof_squeeze_test(MessageDigest::shake_256(), test);
738774
}
739775

740776
assert_eq!(MessageDigest::shake_256().block_size(), 136);

0 commit comments

Comments
 (0)