Skip to content

Commit

Permalink
fix eq check for TicketName
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Oct 15, 2024
1 parent 0c82ea7 commit 2ea9550
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ thiserror = "1.0"
rand = "0.8"
fastrand = "2.0"
memchr = "2.4"
constant_time_eq = "0.3"
uuid = "1.3"
base64 = "0.22"
regex = "1.11"
Expand Down
1 change: 1 addition & 0 deletions lib/g3-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ crc32fast = "1.4"
smallvec.workspace = true
smol_str.workspace = true
memchr.workspace = true
constant_time_eq.workspace = true
url.workspace = true
num-traits.workspace = true
arc-swap.workspace = true
Expand Down
28 changes: 16 additions & 12 deletions lib/g3-types/src/net/tls/ticket_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::hash::{Hash, Hasher};

pub const TICKET_KEY_NAME_LENGTH: usize = 16;

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct TicketKeyName([u8; TICKET_KEY_NAME_LENGTH]);

impl From<[u8; TICKET_KEY_NAME_LENGTH]> for TicketKeyName {
Expand Down Expand Up @@ -53,12 +53,7 @@ impl Hash for TicketKeyName {

impl PartialEq for TicketKeyName {
fn eq(&self, other: &Self) -> bool {
// use Constant Time Eq
let mut xor_sum: u32 = 0;
for i in 0..TICKET_KEY_NAME_LENGTH {
xor_sum += (self.0[i] ^ other.0[0]) as u32;
}
xor_sum == 0
constant_time_eq::constant_time_eq_n(&self.0, &other.0)
}
}

Expand All @@ -70,11 +65,7 @@ impl TicketKeyName {
return false;
}

let mut xor_sum: u32 = 0;
for i in 0..TICKET_KEY_NAME_LENGTH {
xor_sum += (self.0[i] ^ buf[0]) as u32;
}
xor_sum == 0
constant_time_eq::constant_time_eq(&self.0, buf)
}

/// Safety: `b` should be of size `TICKET_KEY_NAME_LENGTH`
Expand All @@ -84,3 +75,16 @@ impl TicketKeyName {
TicketKeyName(v)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn constant_eq() {
let name1 = TicketKeyName([0u8; 16]);
let name2 = TicketKeyName([0u8; 16]);
assert_eq!(name1, name2);
assert!(name1.constant_time_eq(name2.as_ref()));
}
}

0 comments on commit 2ea9550

Please sign in to comment.