Skip to content

Commit ca07f32

Browse files
committed
Merge #84: Crypto: Ephemeral Instance Random Seeds with Keepers
873293a crypto: ephemeral instance seeds with keepers (Cameron Garnham) Pull request description: This module makes use of [lazy_static](https://docs.rs/lazy_static/latest/lazy_static/), to initialize an instance ephemeral random seed for the server to use on startup. This seed can be used to derive other instance ephemeral keys. The seed is forgotten upon shutdown of the server. To help with testing, a `DefaultSeed` helping structure that provides a zeroed seed when testing, and the random seed when not testing. ACKs for top commit: da2ce7: ACK 873293a Tree-SHA512: b382bda57352a6fbeefb7345f1ff2d0bb7f6fcdf781c588ae7658f869c4506a7cd127f593ae8df24fef575758f52045593d3c0592a57848e81bad122889b5c9a
2 parents 4f4959e + 873293a commit ca07f32

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ pub mod static_time {
2727
pub static ref TIME_AT_APP_START: SystemTime = SystemTime::now();
2828
}
2929
}
30+
31+
pub mod ephemeral_instance_keys {
32+
use rand::rngs::ThreadRng;
33+
use rand::Rng;
34+
35+
pub type Seed = [u8; 32];
36+
37+
lazy_static! {
38+
pub static ref RANDOM_SEED: Seed = Rng::gen(&mut ThreadRng::default());
39+
}
40+
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use log::info;
44
use torrust_tracker::tracker::statistics::StatsTracker;
55
use torrust_tracker::tracker::tracker::TorrentTracker;
6-
use torrust_tracker::{logging, setup, static_time, Configuration};
6+
use torrust_tracker::{ephemeral_instance_keys, logging, setup, static_time, Configuration};
77

88
#[tokio::main]
99
async fn main() {
@@ -12,6 +12,9 @@ async fn main() {
1212
// Set the time of Torrust app starting
1313
lazy_static::initialize(&static_time::TIME_AT_APP_START);
1414

15+
// Initialize the Ephemeral Instance Random Seed
16+
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
17+
1518
// Initialize Torrust config
1619
let config = match Configuration::load_from_file(CONFIG_PATH) {
1720
Ok(config) => Arc::new(config),

src/protocol/crypto.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
pub mod keys {
2+
3+
pub mod seeds {
4+
use self::detail::DEFAULT_SEED;
5+
use crate::ephemeral_instance_keys::{Seed, RANDOM_SEED};
6+
7+
pub trait SeedKeeper {
8+
type Seed: Sized + Default + AsMut<[u8]>;
9+
fn get_seed() -> &'static Self::Seed;
10+
}
11+
12+
pub struct InstanceSeed;
13+
pub struct DefaultSeed;
14+
15+
impl SeedKeeper for InstanceSeed {
16+
type Seed = Seed;
17+
18+
fn get_seed() -> &'static Self::Seed {
19+
&RANDOM_SEED
20+
}
21+
}
22+
23+
impl SeedKeeper for DefaultSeed {
24+
type Seed = Seed;
25+
26+
#[allow(clippy::needless_borrow)]
27+
fn get_seed() -> &'static Self::Seed {
28+
&DEFAULT_SEED
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::detail::ZEROED_TEST_SEED;
35+
use super::{DefaultSeed, InstanceSeed, SeedKeeper};
36+
use crate::ephemeral_instance_keys::Seed;
37+
38+
pub struct ZeroedTestSeed;
39+
40+
impl SeedKeeper for ZeroedTestSeed {
41+
type Seed = Seed;
42+
43+
#[allow(clippy::needless_borrow)]
44+
fn get_seed() -> &'static Self::Seed {
45+
&ZEROED_TEST_SEED
46+
}
47+
}
48+
49+
#[test]
50+
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() {
51+
assert_eq!(DefaultSeed::get_seed(), ZeroedTestSeed::get_seed())
52+
}
53+
54+
#[test]
55+
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() {
56+
assert_ne!(DefaultSeed::get_seed(), InstanceSeed::get_seed())
57+
}
58+
}
59+
60+
mod detail {
61+
use crate::ephemeral_instance_keys::Seed;
62+
63+
#[allow(dead_code)]
64+
pub const ZEROED_TEST_SEED: &Seed = &[0u8; 32];
65+
66+
#[cfg(test)]
67+
pub use ZEROED_TEST_SEED as DEFAULT_SEED;
68+
69+
#[cfg(not(test))]
70+
pub use crate::ephemeral_instance_keys::RANDOM_SEED as DEFAULT_SEED;
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use std::convert::TryInto;
75+
76+
use crate::ephemeral_instance_keys::RANDOM_SEED;
77+
use crate::protocol::crypto::keys::seeds::detail::ZEROED_TEST_SEED;
78+
use crate::protocol::crypto::keys::seeds::DEFAULT_SEED;
79+
80+
#[test]
81+
fn it_should_have_a_zero_test_seed() {
82+
assert_eq!(*ZEROED_TEST_SEED, [0u8; 32])
83+
}
84+
85+
#[test]
86+
fn it_should_default_to_zeroed_seed_when_testing() {
87+
assert_eq!(*DEFAULT_SEED, *ZEROED_TEST_SEED)
88+
}
89+
90+
#[test]
91+
fn it_should_have_a_large_random_seed() {
92+
assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u64::MAX as u128);
93+
assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u64::MAX as u128);
94+
}
95+
}
96+
}
97+
}
98+
}

src/protocol/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod clock;
22
pub mod common;
3+
pub mod crypto;
34
pub mod utils;

0 commit comments

Comments
 (0)