Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 boringtun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tracing-subscriber = { version = "0.3", features = ["fmt"], optional = true }
ip_network = "0.4.1"
ip_network_table = "0.2.0"
ring = "0.17"
subtle = "2.6"
x25519-dalek = { version = "2.0.1", features = [
"reusable_secrets",
"static_secrets",
Expand Down
16 changes: 11 additions & 5 deletions boringtun/src/noise/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rand_core::OsRng;
use ring::aead::{Aad, LessSafeKey, Nonce, UnboundKey, CHACHA20_POLY1305};
use std::convert::TryInto;
use std::time::{Duration, SystemTime};
use subtle::ConstantTimeEq;

#[cfg(feature = "mock-instant")]
use mock_instant::Instant;
Expand Down Expand Up @@ -521,11 +522,16 @@ impl Handshake {
&hash,
)?;

ring::constant_time::verify_slices_are_equal(
self.params.peer_static_public.as_bytes(),
&peer_static_public_decrypted,
)
.map_err(|_| WireGuardError::WrongKey)?;
if self
.params
.peer_static_public
.as_bytes()
.ct_eq(&peer_static_public_decrypted)
.unwrap_u8()
!= 1
{
return Err(WireGuardError::WrongKey);
}

// initiator.hash = HASH(initiator.hash || msg.encrypted_static)
hash = b2s_hash(&hash, packet.encrypted_static);
Expand Down
9 changes: 5 additions & 4 deletions boringtun/src/noise/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use aead::{AeadInPlace, KeyInit};
use chacha20poly1305::{Key, XChaCha20Poly1305};
use parking_lot::Mutex;
use rand_core::{OsRng, RngCore};
use ring::constant_time::verify_slices_are_equal;
use subtle::ConstantTimeEq;

const COOKIE_REFRESH: u64 = 128; // Use 128 and not 120 so the compiler can optimize out the division
const COOKIE_SIZE: usize = 16;
Expand Down Expand Up @@ -166,8 +166,9 @@ impl RateLimiter {
let (mac1, mac2) = macs.split_at(16);

let computed_mac1 = b2s_keyed_mac_16(&self.mac1_key, msg);
verify_slices_are_equal(&computed_mac1[..16], mac1)
.map_err(|_| TunnResult::Err(WireGuardError::InvalidMac))?;
if computed_mac1[..16].ct_eq(mac1).unwrap_u8() != 1 {
return Err(TunnResult::Err(WireGuardError::InvalidMac));
}

if self.is_under_load() {
let addr = match src_addr {
Expand All @@ -179,7 +180,7 @@ impl RateLimiter {
let cookie = self.current_cookie(addr);
let computed_mac2 = b2s_keyed_mac_16_2(&cookie, msg, mac1);

if verify_slices_are_equal(&computed_mac2[..16], mac2).is_err() {
if computed_mac2[..16].ct_eq(mac2).unwrap_u8() != 1 {
let cookie_packet = self
.format_cookie_reply(sender_idx, cookie, mac1, dst)
.map_err(TunnResult::Err)?;
Expand Down