Skip to content

Commit

Permalink
Merge branch 'main' into ci-bencher
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert authored Feb 9, 2024
2 parents debda1d + bb74821 commit cfb3f1e
Show file tree
Hide file tree
Showing 31 changed files with 341 additions and 319 deletions.
1 change: 0 additions & 1 deletion neqo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ license.workspace = true
# Sync with https://searchfox.org/mozilla-central/source/Cargo.lock 2024-02-08
enum-map = "2.7"
env_logger = { version = "0.10", default-features = false }
lazy_static = "1.4"
log = { version = "0.4", default-features = false }
qlog = "0.12"
time = { version = "0.3", features = ["formatting"] }
Expand Down
17 changes: 10 additions & 7 deletions neqo-common/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

#![allow(clippy::module_name_repetitions)]

use std::{io::Write, sync::Once, time::Instant};
use std::{
io::Write,
sync::{Once, OnceLock},
time::{Duration, Instant},
};

use env_logger::Builder;
use lazy_static::lazy_static;

#[macro_export]
macro_rules! do_log {
Expand Down Expand Up @@ -42,17 +45,17 @@ macro_rules! log_subject {
}};
}

static INIT_ONCE: Once = Once::new();

lazy_static! {
static ref START_TIME: Instant = Instant::now();
fn since_start() -> Duration {
static START_TIME: OnceLock<Instant> = OnceLock::new();
START_TIME.get_or_init(Instant::now).elapsed()
}

pub fn init() {
static INIT_ONCE: Once = Once::new();
INIT_ONCE.call_once(|| {
let mut builder = Builder::from_env("RUST_LOG");
builder.format(|buf, record| {
let elapsed = START_TIME.elapsed();
let elapsed = since_start();
writeln!(
buf,
"{}s{:3}ms {} {}",
Expand Down
61 changes: 31 additions & 30 deletions neqo-common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,49 +247,50 @@ impl<T> Timer<T> {

#[cfg(test)]
mod test {
use lazy_static::lazy_static;
use std::sync::OnceLock;

use super::{Duration, Instant, Timer};

lazy_static! {
static ref NOW: Instant = Instant::now();
fn now() -> Instant {
static NOW: OnceLock<Instant> = OnceLock::new();
*NOW.get_or_init(Instant::now)
}

const GRANULARITY: Duration = Duration::from_millis(10);
const CAPACITY: usize = 10;
#[test]
fn create() {
let t: Timer<()> = Timer::new(*NOW, GRANULARITY, CAPACITY);
let t: Timer<()> = Timer::new(now(), GRANULARITY, CAPACITY);
assert_eq!(t.span(), Duration::from_millis(100));
assert_eq!(None, t.next_time());
}

#[test]
fn immediate_entry() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
t.add(*NOW, 12);
assert_eq!(*NOW, t.next_time().expect("should have an entry"));
let values: Vec<_> = t.take_until(*NOW).collect();
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
t.add(now(), 12);
assert_eq!(now(), t.next_time().expect("should have an entry"));
let values: Vec<_> = t.take_until(now()).collect();
assert_eq!(vec![12], values);
}

#[test]
fn same_time() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
let v1 = 12;
let v2 = 13;
t.add(*NOW, v1);
t.add(*NOW, v2);
assert_eq!(*NOW, t.next_time().expect("should have an entry"));
let values: Vec<_> = t.take_until(*NOW).collect();
t.add(now(), v1);
t.add(now(), v2);
assert_eq!(now(), t.next_time().expect("should have an entry"));
let values: Vec<_> = t.take_until(now()).collect();
assert!(values.contains(&v1));
assert!(values.contains(&v2));
}

#[test]
fn add() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let near_future = *NOW + Duration::from_millis(17);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
let near_future = now() + Duration::from_millis(17);
let v = 9;
t.add(near_future, v);
assert_eq!(near_future, t.next_time().expect("should return a value"));
Expand All @@ -305,8 +306,8 @@ mod test {

#[test]
fn add_future() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let future = *NOW + Duration::from_millis(117);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
let future = now() + Duration::from_millis(117);
let v = 9;
t.add(future, v);
assert_eq!(future, t.next_time().expect("should return a value"));
Expand All @@ -315,8 +316,8 @@ mod test {

#[test]
fn add_far_future() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let far_future = *NOW + Duration::from_millis(892);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
let far_future = now() + Duration::from_millis(892);
let v = 9;
t.add(far_future, v);
assert_eq!(far_future, t.next_time().expect("should return a value"));
Expand All @@ -333,12 +334,12 @@ mod test {
];

fn with_times() -> Timer<usize> {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
for (i, time) in TIMES.iter().enumerate() {
t.add(*NOW + *time, i);
t.add(now() + *time, i);
}
assert_eq!(
*NOW + *TIMES.iter().min().unwrap(),
now() + *TIMES.iter().min().unwrap(),
t.next_time().expect("should have a time")
);
t
Expand All @@ -348,7 +349,7 @@ mod test {
#[allow(clippy::needless_collect)] // false positive
fn multiple_values() {
let mut t = with_times();
let values: Vec<_> = t.take_until(*NOW + *TIMES.iter().max().unwrap()).collect();
let values: Vec<_> = t.take_until(now() + *TIMES.iter().max().unwrap()).collect();
for i in 0..TIMES.len() {
assert!(values.contains(&i));
}
Expand All @@ -358,7 +359,7 @@ mod test {
#[allow(clippy::needless_collect)] // false positive
fn take_far_future() {
let mut t = with_times();
let values: Vec<_> = t.take_until(*NOW + Duration::from_secs(100)).collect();
let values: Vec<_> = t.take_until(now() + Duration::from_secs(100)).collect();
for i in 0..TIMES.len() {
assert!(values.contains(&i));
}
Expand All @@ -368,15 +369,15 @@ mod test {
fn remove_each() {
let mut t = with_times();
for (i, time) in TIMES.iter().enumerate() {
assert_eq!(Some(i), t.remove(*NOW + *time, |&x| x == i));
assert_eq!(Some(i), t.remove(now() + *time, |&x| x == i));
}
assert_eq!(None, t.next_time());
}

#[test]
fn remove_future() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let future = *NOW + Duration::from_millis(117);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
let future = now() + Duration::from_millis(117);
let v = 9;
t.add(future, v);

Expand All @@ -385,9 +386,9 @@ mod test {

#[test]
fn remove_too_far_future() {
let mut t = Timer::new(*NOW, GRANULARITY, CAPACITY);
let future = *NOW + Duration::from_millis(117);
let too_far_future = *NOW + t.span() + Duration::from_millis(117);
let mut t = Timer::new(now(), GRANULARITY, CAPACITY);
let future = now() + Duration::from_millis(117);
let too_far_future = now() + t.span() + Duration::from_millis(117);
let v = 9;
t.add(future, v);

Expand Down
21 changes: 15 additions & 6 deletions neqo-crypto/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use crate::{
},
err::{Error, Res},
p11::{
random, Item, PK11Origin, PK11SymKey, PK11_ImportDataKey, Slot, SymKey, CKA_DERIVE,
Item, PK11Origin, PK11SymKey, PK11_ImportDataKey, Slot, SymKey, CKA_DERIVE,
CKM_HKDF_DERIVE, CK_ATTRIBUTE_TYPE, CK_MECHANISM_TYPE,
},
random,
};

experimental_api!(SSL_HkdfExtract(
Expand All @@ -40,24 +41,32 @@ experimental_api!(SSL_HkdfExpandLabel(
secret: *mut *mut PK11SymKey,
));

fn key_size(version: Version, cipher: Cipher) -> Res<usize> {
const MAX_KEY_SIZE: usize = 48;
const fn key_size(version: Version, cipher: Cipher) -> Res<usize> {
if version != TLS_VERSION_1_3 {
return Err(Error::UnsupportedVersion);
}
Ok(match cipher {
let size = match cipher {
TLS_AES_128_GCM_SHA256 | TLS_CHACHA20_POLY1305_SHA256 => 32,
TLS_AES_256_GCM_SHA384 => 48,
_ => return Err(Error::UnsupportedCipher),
})
};
debug_assert!(size <= MAX_KEY_SIZE);
Ok(size)
}

/// Generate a random key of the right size for the given suite.
///
/// # Errors
///
/// Only if NSS fails.
/// If the ciphersuite or protocol version is not supported.
pub fn generate_key(version: Version, cipher: Cipher) -> Res<SymKey> {
import_key(version, &random(key_size(version, cipher)?))
// With generic_const_expr, this becomes:
// import_key(version, &random::<{ key_size(version, cipher) }>())
import_key(
version,
&random::<MAX_KEY_SIZE>()[0..key_size(version, cipher)?],
)
}

/// Import a symmetric key for use with HKDF.
Expand Down
Loading

0 comments on commit cfb3f1e

Please sign in to comment.