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

Implement JitterRng, based on jitterentropy-library. #28

Merged
merged 7 commits into from
Nov 17, 2017
Merged
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions src/jitter_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl ErrorKind {
ErrorKind::CoarseTimer => "coarse timer",
ErrorKind::NotMonotonic => "timer not monotonic",
ErrorKind::TinyVariantions => "time delta variations too small",
ErrorKind::ToManyStuck => "to many stuck results",
ErrorKind::ToManyStuck => "too many stuck results",
ErrorKind::__Nonexhaustive => unreachable!(),
}
}
Expand All @@ -127,7 +127,7 @@ impl fmt::Display for TimerError {
ErrorKind::TinyVariantions =>
write!(f, "Variations of deltas of time too small."),
ErrorKind::ToManyStuck =>
write!(f, "To many stuck results (indicating no added entropy)."),
write!(f, "Too many stuck results (indicating no added entropy)."),
ErrorKind::__Nonexhaustive => unreachable!(),
}
}
Expand Down Expand Up @@ -361,6 +361,9 @@ impl JitterRng {
// Get time stamp and calculate time delta to previous
// invocation to measure the timing variations
let time = (self.timer)();
// Note: wrapping_sub combined with a cast to `i64` generates a correct
// delta, even in the unlikely case this is a timer than is not strictly
// monotonic.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a timer that is not strictly ... ?

let current_delta = time.wrapping_sub(self.prev_time) as i64;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding a note that wrapping arithmetic on unsigned integers followed by cast to i64 generates correct deltas in most cases (including where both times have the highest bit set and where one has wrapped). [The exception is where the actual delta is not representable as an i64, but this shouldn't be the case.]

self.prev_time = time;

Expand Down Expand Up @@ -422,7 +425,7 @@ impl JitterRng {
// we rely on it to not recognise the opportunity.
for i in 0..64 {
let apply = (self.data >> i) & 1;
let mask = !((apply as i64) - 1) as u64;
let mask = !apply.wrapping_sub(1);
mixer ^= CONSTANT & mask;
mixer = mixer.rotate_left(1);
}
Expand Down