Skip to content

Commit fb943cb

Browse files
committed
Fix underflow in Validator
Signed-off-by: Emīls <[email protected]>
1 parent 7d84ef9 commit fb943cb

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/wireguard/handshake/macs.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,16 @@ impl Generator {
181181

182182
struct Secret {
183183
value: [u8; 32],
184-
birth: Instant,
184+
birth: Option<Instant>,
185+
}
186+
187+
impl Secret {
188+
fn is_still_valid(&self) -> bool {
189+
match self.birth {
190+
Some(birth) => birth.elapsed() < COOKIE_UPDATE_INTERVAL,
191+
None => false,
192+
}
193+
}
185194
}
186195

187196
pub struct Validator {
@@ -197,14 +206,15 @@ impl Validator {
197206
cookie_key: HASH!(LABEL_COOKIE, pk.as_bytes()).into(),
198207
secret: RwLock::new(Secret {
199208
value: [0u8; SIZE_SECRET],
200-
birth: Instant::now() - Duration::new(86400, 0),
209+
birth: None,
201210
}),
202211
}
203212
}
204213

205214
fn get_tau(&self, src: &[u8]) -> Option<[u8; SIZE_COOKIE]> {
206215
let secret = self.secret.read();
207-
if secret.birth.elapsed() < COOKIE_UPDATE_INTERVAL {
216+
if secret.is_still_valid()
217+
{
208218
Some(MAC!(&secret.value, src))
209219
} else {
210220
None
@@ -215,21 +225,21 @@ impl Validator {
215225
// check if current value is still valid
216226
{
217227
let secret = self.secret.read();
218-
if secret.birth.elapsed() < COOKIE_UPDATE_INTERVAL {
228+
if secret.is_still_valid() {
219229
return MAC!(&secret.value, src);
220230
};
221231
}
222232

223233
// take write lock, check again
224234
{
225235
let mut secret = self.secret.write();
226-
if secret.birth.elapsed() < COOKIE_UPDATE_INTERVAL {
236+
if secret.is_still_valid() {
227237
return MAC!(&secret.value, src);
228238
};
229239

230240
// set new random cookie secret
231241
rng.fill_bytes(&mut secret.value);
232-
secret.birth = Instant::now();
242+
secret.birth = Some(Instant::now());
233243
MAC!(&secret.value, src)
234244
}
235245
}

0 commit comments

Comments
 (0)