Skip to content

Commit

Permalink
Fix undeflow in Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkisemils committed Apr 26, 2021
1 parent 7d84ef9 commit 5a906fa
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/wireguard/handshake/macs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,16 @@ impl Generator {

struct Secret {
value: [u8; 32],
birth: Instant,
birth: Option<Instant>,
}

impl Secret {
fn is_still_valid(&self) -> bool {
match self.birth {
Some(birth) => birth.elapsed() < COOKIE_UPDATE_INTERVAL,
None => false,
}
}
}

pub struct Validator {
Expand All @@ -197,14 +206,15 @@ impl Validator {
cookie_key: HASH!(LABEL_COOKIE, pk.as_bytes()).into(),
secret: RwLock::new(Secret {
value: [0u8; SIZE_SECRET],
birth: Instant::now() - Duration::new(86400, 0),
birth: None,
}),
}
}

fn get_tau(&self, src: &[u8]) -> Option<[u8; SIZE_COOKIE]> {
let secret = self.secret.read();
if secret.birth.elapsed() < COOKIE_UPDATE_INTERVAL {
if secret.is_still_valid()
{
Some(MAC!(&secret.value, src))
} else {
None
Expand All @@ -215,21 +225,21 @@ impl Validator {
// check if current value is still valid
{
let secret = self.secret.read();
if secret.birth.elapsed() < COOKIE_UPDATE_INTERVAL {
if secret.is_still_valid() {
return MAC!(&secret.value, src);
};
}

// take write lock, check again
{
let mut secret = self.secret.write();
if secret.birth.elapsed() < COOKIE_UPDATE_INTERVAL {
if secret.is_still_valid() {
return MAC!(&secret.value, src);
};

// set new random cookie secret
rng.fill_bytes(&mut secret.value);
secret.birth = Instant::now();
secret.birth = Some(Instant::now());
MAC!(&secret.value, src)
}
}
Expand Down

0 comments on commit 5a906fa

Please sign in to comment.