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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::{constants::*, Result};
use std::time::{SystemTime, UNIX_EPOCH};
use rand_core::RngCore;
use subtle::ConstantTimeEq;
use ptrs::trace; // , trace};
pub fn get_epoch_hour() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
/ 3600
}
pub fn make_hs_pad(pad_len: usize) -> Result<Vec<u8>> {
trace!("[make_hs_pad] generating {pad_len}B");
let mut pad = vec![u8::default(); pad_len];
rand::thread_rng()
.try_fill_bytes(&mut pad)
.expect("rng failure");
Ok(pad)
}
pub fn find_mac_mark(
mark: [u8; MARK_LENGTH],
buf: impl AsRef<[u8]>,
start_pos: usize,
max_pos: usize,
from_tail: bool,
) -> Option<usize> {
let buffer = buf.as_ref();
if buffer.len() < MARK_LENGTH {
return None;
}
trace!(
"finding mac mark: buf: {}B, {}-{}, from_tail: {}",
buffer.len(),
start_pos,
max_pos,
from_tail
);
if start_pos > buffer.len() {
return None;
}
let mut end_pos = buffer.len();
if end_pos > max_pos {
end_pos = max_pos;
}
if end_pos - start_pos < MARK_LENGTH + MAC_LENGTH {
return None;
}
let mut pos: usize;
if from_tail {
// The server can optimize the search process by only examining the
// tail of the buffer. The client can't send valid data past M_C |
// MAC_C as it does not have the server's public key yet.
pos = end_pos - (MARK_LENGTH + MAC_LENGTH);
// trace!("{pos}\n{}\n{}", hex::encode(mark), hex::encode(&buffer[pos..pos + MARK_LENGTH]));
if mark[..]
.ct_eq(buffer[pos..pos + MARK_LENGTH].as_ref())
.into()
{
return Some(pos);
}
return None;
}
// The client has to actually do a substring search since the server can
// and will send payload trailing the response.
//
// XXX: .windows().position() uses a naive search, which kind of sucks.
// but better algorithms (like `contains` for String) aren't implemented
// for byte slices in std.
pos = buffer[start_pos..end_pos]
.windows(MARK_LENGTH)
.position(|window| window.ct_eq(&mark[..]).into())?;
// Ensure that there is enough trailing data for the MAC.
if start_pos + pos + MARK_LENGTH + MAC_LENGTH > end_pos {
return None;
}
// Return the index relative to the start of the slice.
pos += start_pos;
Some(pos)
}
#[cfg(test)]
mod test {
use super::*;
struct MacMarkTest {
mark: [u8; MARK_LENGTH],
buf: Vec<u8>,
start_pos: usize,
max_pos: usize,
from_tail: bool,
expected: Option<usize>,
}
#[test]
fn find_mac_mark_thorough() -> Result<()> {
let cases = vec![
MacMarkTest {
mark: [0_u8; MARK_LENGTH],
buf: vec![0_u8; 100],
start_pos: 0,
max_pos: 100,
from_tail: false,
expected: Some(0),
},
MacMarkTest {
mark: hex::decode("00112233445566778899aabbccddeeff")
.unwrap()
.try_into()
.unwrap(),
buf: hex::decode(
"00112233445566778899aabbccddeeff00000000000000000000000000000000",
)
.unwrap(),
start_pos: 0,
max_pos: 100,
from_tail: false,
expected: Some(0),
},
MacMarkTest {
// from tail
mark: hex::decode("00112233445566778899aabbccddeeff")
.unwrap()
.try_into()
.unwrap(),
buf: hex::decode(
"00112233445566778899aabbccddeeff00000000000000000000000000000000",
)
.unwrap(),
start_pos: 0,
max_pos: 100,
from_tail: true,
expected: Some(0),
},
MacMarkTest {
// from tail not align with start
mark: hex::decode("00112233445566778899aabbccddeeff")
.unwrap()
.try_into()
.unwrap(),
buf: hex::decode(
"000000112233445566778899aabbccddeeff00000000000000000000000000000000",
)
.unwrap(),
start_pos: 0,
max_pos: 100,
from_tail: true,
expected: Some(2),
},
MacMarkTest {
mark: hex::decode("00112233445566778899aabbccddeeff")
.unwrap()
.try_into()
.unwrap(),
buf: hex::decode(
"000000112233445566778899aabbccddeeff00000000000000000000000000000000",
)
.unwrap(),
start_pos: 0,
max_pos: 100,
from_tail: false,
expected: Some(2),
},
MacMarkTest {
mark: hex::decode("00112233445566778899aabbccddeeff")
.unwrap()
.try_into()
.unwrap(),
buf: hex::decode(
"00000000112233445566778899aabbccddeeff00000000000000000000000000000000",
)
.unwrap(),
start_pos: 2,
max_pos: 100,
from_tail: false,
expected: Some(3),
},
MacMarkTest {
// Not long enough to contain MAC
mark: hex::decode("00112233445566778899aabbccddeeff")
.unwrap()
.try_into()
.unwrap(),
buf: hex::decode("00112233445566778899aabbccddeeff").unwrap(),
start_pos: 0,
max_pos: 100,
from_tail: false,
expected: None,
},
MacMarkTest {
// Access from tail success
mark: [0_u8; MARK_LENGTH],
buf: vec![0_u8; 100],
start_pos: 0,
max_pos: 100,
from_tail: true,
expected: Some(100 - MARK_LENGTH - MAC_LENGTH),
},
MacMarkTest {
// from tail fail
mark: [0_u8; MARK_LENGTH],
buf: hex::decode(
"00112233445566778899aabbccddeeff00000000000000000000000000000000",
)
.unwrap(),
start_pos: 0,
max_pos: 100,
from_tail: true,
expected: None,
},
MacMarkTest {
// provided buf too short
mark: [0_u8; MARK_LENGTH],
buf: vec![0_u8; MARK_LENGTH - 1],
start_pos: 0,
max_pos: 100,
from_tail: false,
expected: None,
},
MacMarkTest {
// provided buf cant contain mark and mac
mark: [0_u8; MARK_LENGTH],
buf: vec![0_u8; MARK_LENGTH + MAC_LENGTH - 1],
start_pos: 0,
max_pos: 100,
from_tail: false,
expected: None,
},
];
for m in cases {
let actual = find_mac_mark(m.mark, m.buf, m.start_pos, m.max_pos, m.from_tail);
assert_eq!(actual, m.expected);
}
Ok(())
}
#[test]
fn epoch_format() {
let _h = format!("{}", get_epoch_hour());
// println!("{h} {}", hex::encode(h.as_bytes()));
}
}