Skip to content

Commit

Permalink
Add tests + testvectos from blake2b
Browse files Browse the repository at this point in the history
  • Loading branch information
cynecx committed May 31, 2018
1 parent 6dd5ff7 commit b075afb
Show file tree
Hide file tree
Showing 2 changed files with 1,116 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/crypto/generichash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,94 @@ impl State {
result
}
}

#[cfg(test)]
mod test {
use super::*;
#[cfg(not(feature = "std"))]
use prelude::*;

#[test]
fn test_vector_1() {
// hash of empty string
let x = [];
let h_expected = [
0x0e, 0x57, 0x51, 0xc0, 0x26, 0xe5, 0x43, 0xb2, 0xe8, 0xab, 0x2e,
0xb0, 0x60, 0x99, 0xda, 0xa1, 0xd1, 0xe5, 0xdf, 0x47, 0x77, 0x8f,
0x77, 0x87, 0xfa, 0xab, 0x45, 0xcd, 0xf1, 0x2f, 0xe3, 0xa8,
];
let mut hasher = State::new(32, None).unwrap();
hasher.update(&x);
let h = hasher.finalize();
assert!(h.as_ref() == h_expected);
}

#[test]
fn test_vector_2() {
// The quick brown fox jumps over the lazy dog
let x = [
0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62,
0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75,
0x6d, 0x70, 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68,
0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67,
];
let h_expected = [
0x01, 0x71, 0x8c, 0xec, 0x35, 0xcd, 0x3d, 0x79, 0x6d, 0xd0, 0x00,
0x20, 0xe0, 0xbf, 0xec, 0xb4, 0x73, 0xad, 0x23, 0x45, 0x7d, 0x06,
0x3b, 0x75, 0xef, 0xf2, 0x9c, 0x0f, 0xfa, 0x2e, 0x58, 0xa9,
];
let mut hasher = State::new(32, None).unwrap();
hasher.update(&x);
let h = hasher.finalize();
assert!(h.as_ref() == h_expected);
}

#[test]
fn test_blake2b_vectors() {
use rustc_serialize::hex::FromHex;
use std::fs::File;
use std::io::{BufRead, BufReader};

let mut r =
BufReader::new(File::open("testvectors/blake2b-kat.txt").unwrap());
let mut line = String::new();

loop {
let msg = {
line.clear();
if let Err(_) = r.read_line(&mut line) {
break;
}

match line.len() {
0 => break,
1...3 => continue,
_ => {}
}

assert!(line.starts_with("in:"));
line[3..].trim().from_hex().unwrap()
};

let key = {
line.clear();
r.read_line(&mut line).unwrap();
assert!(line.starts_with("key:"));
line[4..].trim().from_hex().unwrap()
};

let expected_hash = {
line.clear();
r.read_line(&mut line).unwrap();
assert!(line.starts_with("hash:"));
line[5..].from_hex().unwrap()
};

let mut hasher = State::new(64, Some(&key)).unwrap();
hasher.update(&msg);

let result_hash = hasher.finalize();
assert!(result_hash.as_ref() == expected_hash.as_slice());
}
}
}
Loading

0 comments on commit b075afb

Please sign in to comment.