Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace time with std::time #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ gcc = "^0.3"

[dependencies]
libc = "^0.2"
time = "^0.1"
rand = "^0.3"
rustc-serialize = "^0.3"
20 changes: 12 additions & 8 deletions src/fortuna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

use cryptoutil::copy_memory;

use std::time::{Duration, Instant};
use rand::{Rng, SeedableRng};
use time::precise_time_s;

use aessafe::AesSafe256Encryptor;
use cryptoutil::read_u32_le;
Expand Down Expand Up @@ -180,7 +180,7 @@ pub struct Fortuna {
pool: [Pool; NUM_POOLS],
generator: FortunaGenerator,
reseed_count: u32,
last_reseed_time: f64
last_reseed_time: Option<Instant>
}

impl Fortuna {
Expand All @@ -190,7 +190,7 @@ impl Fortuna {
pool: [Pool::new(); NUM_POOLS],
generator: FortunaGenerator::new(),
reseed_count: 0,
last_reseed_time: 0.0
last_reseed_time: None,
}
}

Expand All @@ -216,11 +216,15 @@ impl Rng for Fortuna {
/// pool, this function will fail the task.
fn fill_bytes(&mut self, dest: &mut [u8]) {
// Reseed if necessary
let now = precise_time_s();
if self.pool[0].count >= MIN_POOL_SIZE &&
now - self.last_reseed_time > 0.1 {
if let Some(last_reseed_time) = self.last_reseed_time {
last_reseed_time.elapsed() > Duration::from_millis(100)
} else {
true
}
{
self.reseed_count += 1;
self.last_reseed_time = now;
self.last_reseed_time = Some(Instant::now());
// Compute key as Sha256d( key || s )
let mut hash = [0; (32 * NUM_POOLS)];
let mut n_pools = 0;
Expand Down Expand Up @@ -259,14 +263,14 @@ impl<'a> SeedableRng<&'a [u8]> for Fortuna {

fn reseed(&mut self, seed: &'a [u8]) {
self.reseed_count += 1;
self.last_reseed_time = precise_time_s();
self.last_reseed_time = Some(Instant::now());
self.generator.reseed(seed);
}
}

#[cfg(test)]
fn test_force_reseed(f: &mut Fortuna) {
f.last_reseed_time -= 0.2;
f.last_reseed_time = Some(Instant::now() - Duration::from_millis(200));
}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

extern crate rand;
extern crate rustc_serialize as serialize;
extern crate time;
extern crate libc;

#[cfg(all(test, feature = "with-bench"))]
Expand Down