Skip to content

Commit

Permalink
Merge branches 'period-clock' and 'crypto-static-keys' into connectio…
Browse files Browse the repository at this point in the history
…n-cookie
  • Loading branch information
da2ce7 committed Sep 14, 2022
2 parents cdbe0d1 + a898b18 commit 86ec337
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ pub mod static_time {
pub static ref TIME_AT_APP_START: SystemTime = SystemTime::now();
}
}

pub mod static_keys {
pub mod static_seeds {
use rand::rngs::ThreadRng;
use rand::Rng;

pub type SeedSize = [u8; 32];

lazy_static! {
pub static ref INSTANCE_SEED: SeedSize = Rng::gen(&mut ThreadRng::default());
}

#[cfg(test)]
lazy_static! {
pub static ref ZEROED_TEST_SEED: SeedSize = [0u8; 32];
}
}
}
82 changes: 82 additions & 0 deletions src/protocol/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
pub mod keys {

pub mod seeds {
use crate::static_keys::static_seeds::{SeedSize, INSTANCE_SEED};

pub trait Seed {
type Seed: Sized + Default + AsMut<[u8]>;
fn seed() -> &'static Self::Seed;
}

pub struct InstanceSeed;
pub struct DefaultSeed;

impl Seed for InstanceSeed {
type Seed = SeedSize;
fn seed() -> &'static Self::Seed {
&*INSTANCE_SEED
}
}

impl Seed for DefaultSeed {
type Seed = SeedSize;
fn seed() -> &'static Self::Seed {
&*self::detail::DEFAULT_SEED
}
}

pub fn initialize_default_seed() {
lazy_static::initialize(&self::detail::DEFAULT_SEED);
}

#[cfg(test)]
mod tests {
use super::{DefaultSeed, InstanceSeed, Seed};
use crate::static_keys::static_seeds::SeedSize;

pub struct ZeroedTestSeed;

impl Seed for ZeroedTestSeed {
type Seed = SeedSize;
fn seed() -> &'static Self::Seed {
&*crate::static_keys::static_seeds::ZEROED_TEST_SEED
}
}

#[test]
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() {
assert_eq!(DefaultSeed::seed(), ZeroedTestSeed::seed())
}

#[test]
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() {
assert_ne!(DefaultSeed::seed(), InstanceSeed::seed())
}
}

mod detail {
#[cfg(not(test))]
pub(super) use crate::static_keys::static_seeds::INSTANCE_SEED as DEFAULT_SEED;
#[cfg(test)]
pub(super) use crate::static_keys::static_seeds::ZEROED_TEST_SEED as DEFAULT_SEED;

#[cfg(test)]
mod tests {
use std::convert::TryInto;

use crate::static_keys::static_seeds::{INSTANCE_SEED, ZEROED_TEST_SEED};

#[test]
fn it_should_have_a_zero_test_seed() {
assert_eq!(*ZEROED_TEST_SEED, [0u8; 32])
}

#[test]
fn it_should_have_a_large_random_seed() {
assert!(u128::from_ne_bytes((*INSTANCE_SEED)[..16].try_into().unwrap()) > u64::MAX as u128);
assert!(u128::from_ne_bytes((*INSTANCE_SEED)[16..].try_into().unwrap()) > u64::MAX as u128);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod clock;
pub mod common;
pub mod crypto;
pub mod utils;

0 comments on commit 86ec337

Please sign in to comment.