Seperate dst_out length check from finalize() to seperate function so that finalize() returns FinalizationCryptoError
@@ -65,6 +65,14 @@ pub struct CShake {
}
impl CShake {
+ /// A function that checks the `dst_out` in `finalize`, so that return errors are seperated.
+ fn check_dst_out(&mut self, dst_out: &mut [u8]) -> Result<(), UnknownCryptoError> {
+ if dst_out.is_empty() || dst_out.len() > 65536 {
+ return Err(UnknownCryptoError);
+ } else {
+ Ok(())
+ }
+ }
/// Initial setup with encoding of `custom` and `name`.
fn setup(&mut self, custom: &[u8], name: &[u8]) {
// Only append the left encoded rate, not the rate itself as with `name` and `custom`
@@ -103,16 +111,13 @@ impl CShake {
}
}
/// Return a cSHAKE hash.
- pub fn finalize(&mut self, dst_out: &mut [u8]) -> Result<(), UnknownCryptoError> {
+ pub fn finalize(&mut self, dst_out: &mut [u8]) -> Result<(), FinalizationCryptoError> {
if self.is_finalized {
- panic!("You need to reset before calling finalize() again");
+ return Err(FinalizationCryptoError);
}
self.is_finalized = true;
-
- if dst_out.is_empty() || dst_out.len() > 65536 {
- return Err(UnknownCryptoError);
- }
+ self.check_dst_out(dst_out).unwrap();
let mut hasher_new = Keccak::new(136, 0x04);
mem::swap(&mut self.hasher, &mut hasher_new);
@@ -240,6 +245,7 @@ mod test {
}
#[test]
+ #[should_panic]
fn err_on_zero_length() {
let input = b"\x00\x01\x02\x03";
let custom = b"";
@@ -248,11 +254,11 @@ mod test {
let mut hash = init(custom, Some(name)).unwrap();
hash.update(input).unwrap();
-
- assert!(hash.finalize(&mut out).is_err());
+ hash.finalize(&mut out).unwrap();
}
#[test]
+ #[should_panic]
fn err_on_above_max_length() {
let input = b"\x00\x01\x02\x03";
let custom = b"";
@@ -261,8 +267,7 @@ mod test {
let mut hash = init(custom, Some(name)).unwrap();
hash.update(input).unwrap();
-
- assert!(hash.finalize(&mut out).is_err());
+ hash.finalize(&mut out).unwrap();
}
#[test]
