use microvms_app::entropy::Entropy;
use microvms_app::error::{Error, ErrorKind};
#[derive(Clone, Copy, Debug, Default)]
pub struct OsEntropy;
impl Entropy for OsEntropy {
fn fill(&self, buf: &mut [u8]) -> Result<(), Error> {
getrandom::fill(buf).map_err(|error| {
Error::new(
ErrorKind::Unexpected,
format!("the OS random pool is unavailable: {error}"),
)
})
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn the_os_source_draws_distinct_bytes() {
let drawn: HashSet<[u8; 8]> = (0..200)
.map(|_| {
let mut bytes = [0_u8; 8];
OsEntropy.fill(&mut bytes).expect("the pool is available");
bytes
})
.collect();
assert_eq!(drawn.len(), 200);
}
}