Skip to content

Commit

Permalink
Allow reusing an Md context object
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-fortanix committed Apr 16, 2020
1 parent 86f252e commit bf19d76
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion mbedtls/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ impl Md {
Ok(())
}

pub fn finish(mut self, out: &mut [u8]) -> Result<usize> {
pub fn finish(&mut self, out: &mut [u8]) -> Result<usize> {
unsafe {
let olen = (*self.inner.md_info).size as usize;
if out.len() < olen {
return Err(Error::MdBadInputData);
}
md_finish(&mut self.inner, out.as_mut_ptr()).into_result()?;
md_starts(&mut self.inner).into_result()?;
Ok(olen)
}
}
Expand Down Expand Up @@ -203,3 +204,37 @@ pub fn pbkdf_pkcs12(
Ok(())
}
}


#[test]
fn test_digest_restart() {

use crate::hash::Type as MdType;

let mut digest1 = vec![0u8; 32];
Md::hash(MdType::Sha256, &[0,1,2,3], &mut digest1).unwrap();

let mut digest2 = vec![0u8; 32];
let mut md = Md::new(MdType::Sha256).unwrap();
md.update(&[0]).unwrap();
md.update(&[]).unwrap();
md.update(&[1,2]).unwrap();
md.update(&[3]).unwrap();
md.finish(&mut digest2).unwrap();

assert_eq!(digest1, digest2);

// hash some other data using same context
let mut digest3 = vec![0u8; 32];
md.update(&[9,8,7,6,5,4,3,2,2]).unwrap();
md.finish(&mut digest3).unwrap();
assert!(digest1 != digest3);

// finally hash same message as prior ensuring same output
let mut digest4 = vec![0u8; 32];
md.update(&[0,1]).unwrap();
md.update(&[2]).unwrap();
md.update(&[3]).unwrap();
md.finish(&mut digest4).unwrap();
assert_eq!(digest1, digest4);
}

0 comments on commit bf19d76

Please sign in to comment.