-
Notifications
You must be signed in to change notification settings - Fork 595
/
hash_hs.rs
160 lines (141 loc) · 4.26 KB
/
hash_hs.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use ring::digest;
use std::mem;
use msgs::codec::Codec;
use msgs::message::{Message, MessagePayload};
/// This deals with keeping a running hash of the handshake
/// payloads. This is computed by buffering initially. Once
/// we know what hash function we need to use we switch to
/// incremental hashing.
///
/// For client auth, we also need to buffer all the messages.
/// This is disable in cases where client auth is not possible.
pub struct HandshakeHash {
/// None before we know what hash function we're using
ctx: Option<digest::Context>,
/// true if we need to keep all messages
client_auth_enabled: bool,
/// buffer for pre-hashing stage and client-auth.
buffer: Vec<u8>
}
impl HandshakeHash {
pub fn new() -> HandshakeHash {
HandshakeHash {
ctx: None,
client_auth_enabled: false,
buffer: Vec::new()
}
}
/// We might be doing client auth, so need to keep a full
/// log of the handshake.
pub fn set_client_auth_enabled(&mut self) {
debug_assert!(self.ctx.is_none()); // or we might have already discarded messages
self.client_auth_enabled = true;
}
/// We decided not to do client auth after all, so discard
/// the transcript.
pub fn abandon_client_auth(&mut self) {
self.client_auth_enabled = false;
self.buffer.drain(..);
}
/// We now know what hash function the verify_data will use.
pub fn start_hash(&mut self, alg: &'static digest::Algorithm) {
debug_assert!(self.ctx.is_none());
let mut ctx = digest::Context::new(alg);
ctx.update(&self.buffer);
self.ctx = Some(ctx);
/* Discard buffer if we don't need it now. */
if !self.client_auth_enabled {
self.buffer.drain(..);
}
}
/// Hash/buffer a handshake message.
pub fn add_message(&mut self, m: &Message) -> &mut HandshakeHash {
match m.payload {
MessagePayload::Handshake(ref hs) => {
let buf = hs.get_encoding();
self.update_raw(&buf);
},
_ => unreachable!()
};
self
}
/// Hash or buffer a byte slice.
fn update_raw(&mut self, buf: &[u8]) -> &mut Self {
if self.ctx.is_some() {
self.ctx.as_mut().unwrap().update(buf);
}
if self.ctx.is_none() || self.client_auth_enabled {
self.buffer.extend_from_slice(buf);
}
self
}
/// Get the current hash value.
pub fn get_current_hash(&self) -> Vec<u8> {
let h = self.ctx.as_ref().unwrap().clone().finish();
let mut ret = Vec::new();
ret.extend_from_slice(h.as_ref());
ret
}
/// Takes this object's buffer containing all handshake messages
/// so far. This method only works once; it resets the buffer
/// to empty.
pub fn take_handshake_buf(&mut self) -> Vec<u8> {
debug_assert!(self.client_auth_enabled);
mem::replace(&mut self.buffer, Vec::new())
}
}
#[cfg(test)]
mod test {
use super::HandshakeHash;
use ring::digest;
#[test]
fn hashes_correctly() {
let mut hh = HandshakeHash::new();
hh.update_raw(b"hello");
assert_eq!(hh.buffer.len(), 5);
hh.start_hash(&digest::SHA256);
assert_eq!(hh.buffer.len(), 0);
hh.update_raw(b"world");
let h = hh.get_current_hash();
assert_eq!(h[0], 0x93);
assert_eq!(h[1], 0x6a);
assert_eq!(h[2], 0x18);
assert_eq!(h[3], 0x5c);
}
#[test]
fn buffers_correctly() {
let mut hh = HandshakeHash::new();
hh.set_client_auth_enabled();
hh.update_raw(b"hello");
assert_eq!(hh.buffer.len(), 5);
hh.start_hash(&digest::SHA256);
assert_eq!(hh.buffer.len(), 5);
hh.update_raw(b"world");
assert_eq!(hh.buffer.len(), 10);
let h = hh.get_current_hash();
assert_eq!(h[0], 0x93);
assert_eq!(h[1], 0x6a);
assert_eq!(h[2], 0x18);
assert_eq!(h[3], 0x5c);
let buf = hh.take_handshake_buf();
assert_eq!(b"helloworld".to_vec(), buf);
}
#[test]
fn abandon() {
let mut hh = HandshakeHash::new();
hh.set_client_auth_enabled();
hh.update_raw(b"hello");
assert_eq!(hh.buffer.len(), 5);
hh.start_hash(&digest::SHA256);
assert_eq!(hh.buffer.len(), 5);
hh.abandon_client_auth();
assert_eq!(hh.buffer.len(), 0);
hh.update_raw(b"world");
assert_eq!(hh.buffer.len(), 0);
let h = hh.get_current_hash();
assert_eq!(h[0], 0x93);
assert_eq!(h[1], 0x6a);
assert_eq!(h[2], 0x18);
assert_eq!(h[3], 0x5c);
}
}