1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use ffi::{crypto_hash_sha512,
crypto_hash_sha512_state,
crypto_hash_sha512_init,
crypto_hash_sha512_update,
crypto_hash_sha512_final,
crypto_hash_sha512_BYTES};
hash_module!(crypto_hash_sha512,
crypto_hash_sha512_state,
crypto_hash_sha512_init,
crypto_hash_sha512_update,
crypto_hash_sha512_final,
crypto_hash_sha512_BYTES,
128);
#[cfg(test)]
mod test {
use super::*;
#[cfg(not(feature = "std"))] use prelude::*;
#[test]
fn test_vector_1() {
let x = [0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0xa];
let h_expected = [0x24, 0xf9, 0x50, 0xaa, 0xc7, 0xb9, 0xea, 0x9b
,0x3c, 0xb7, 0x28, 0x22, 0x8a, 0x0c, 0x82, 0xb6
,0x7c, 0x39, 0xe9, 0x6b, 0x4b, 0x34, 0x47, 0x98
,0x87, 0x0d, 0x5d, 0xae, 0xe9, 0x3e, 0x3a, 0xe5
,0x93, 0x1b, 0xaa, 0xe8, 0xc7, 0xca, 0xcf, 0xea
,0x4b, 0x62, 0x94, 0x52, 0xc3, 0x80, 0x26, 0xa8
,0x1d, 0x13, 0x8b, 0xc7, 0xaa, 0xd1, 0xaf, 0x3e
,0xf7, 0xbf, 0xd5, 0xec, 0x64, 0x6d, 0x6c, 0x28];
let Digest(h) = hash(&x);
assert!(&h[..] == &h_expected[..]);
}
fn test_nist_vector(filename: &str) {
use rustc_serialize::hex::FromHex;
use std::fs::File;
use std::io::{BufRead, BufReader};
let mut r = BufReader::new(File::open(filename).unwrap());
let mut line = String::new();
loop {
line.clear();
r.read_line(&mut line).unwrap();
if line.len() == 0 {
break;
}
let starts_with_len = line.starts_with("Len = ");
if starts_with_len {
let len: usize = line[6..].trim().parse().unwrap();
line.clear();
r.read_line(&mut line).unwrap();
let rawmsg = line[6..].from_hex().unwrap();
let msg = &rawmsg[..len/8];
line.clear();
r.read_line(&mut line).unwrap();
let md = line[5..].from_hex().unwrap();
let Digest(digest) = hash(msg);
assert!(&digest[..] == &md[..]);
}
}
}
#[test]
fn test_vectors_nist_short() {
test_nist_vector("testvectors/SHA512ShortMsg.rsp");
}
#[test]
fn test_vectors_nist_long() {
test_nist_vector("testvectors/SHA512LongMsg.rsp");
}
#[test]
fn test_hash_state_multi_parts() {
let x = [0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0xa];
let h_expected = [0x24, 0xf9, 0x50, 0xaa, 0xc7, 0xb9, 0xea, 0x9b
,0x3c, 0xb7, 0x28, 0x22, 0x8a, 0x0c, 0x82, 0xb6
,0x7c, 0x39, 0xe9, 0x6b, 0x4b, 0x34, 0x47, 0x98
,0x87, 0x0d, 0x5d, 0xae, 0xe9, 0x3e, 0x3a, 0xe5
,0x93, 0x1b, 0xaa, 0xe8, 0xc7, 0xca, 0xcf, 0xea
,0x4b, 0x62, 0x94, 0x52, 0xc3, 0x80, 0x26, 0xa8
,0x1d, 0x13, 0x8b, 0xc7, 0xaa, 0xd1, 0xaf, 0x3e
,0xf7, 0xbf, 0xd5, 0xec, 0x64, 0x6d, 0x6c, 0x28];
let mut hash_state = State::new();
for chunk in x.chunks(3) {
hash_state.update(chunk);
}
let Digest(h) = hash_state.finalize();
assert!(&h[..] == &h_expected[..]);
}
}