1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/// A generator of random data. The two methods provide the same functionality for
/// different use cases. One for "public" randomly generated data that may appear
/// in the clear, and one for "private" data that should remain secret. This approach
/// lessens the risk of potential predictability weaknesses in random number generation
/// algorithms from leaking information across contexts.
pub trait Generator: 'static + Send {
/// Fills `dest` with unpredictable bits that may be
/// sent over the wire and viewable in the clear.
fn public_random_fill(&mut self, dest: &mut [u8]);
/// Fills `dest` with unpredictable bits that will only be
/// used internally within the endpoint, remaining secret.
fn private_random_fill(&mut self, dest: &mut [u8]);
}
#[cfg(any(test, feature = "testing"))]
pub mod testing {
use crate::random;
#[derive(Debug, Default)]
pub struct Generator(pub u8);
impl random::Generator for Generator {
fn public_random_fill(&mut self, dest: &mut [u8]) {
let seed = self.0;
for (i, elem) in dest.iter_mut().enumerate() {
*elem = seed ^ i as u8;
}
self.0 = self.0.wrapping_add(1)
}
fn private_random_fill(&mut self, dest: &mut [u8]) {
let seed = u8::max_value() - self.0;
for (i, elem) in dest.iter_mut().enumerate() {
*elem = seed ^ i as u8;
}
self.0 = self.0.wrapping_add(1)
}
}
}