hpke_http/entropy.rs
1//! Fallible entropy injection.
2
3use crate::Error;
4
5/// A source of cryptographically secure random bytes.
6pub trait EntropySource {
7 /// Fill the destination or return before any protocol output is produced.
8 ///
9 /// # Errors
10 ///
11 /// Returns [`Error::EntropyUnavailable`] when the source cannot fill the
12 /// entire destination with cryptographically secure random bytes.
13 fn fill(&mut self, destination: &mut [u8]) -> Result<(), Error>;
14}
15
16/// The operating system or Web Crypto entropy source selected by `getrandom`.
17#[derive(Clone, Copy, Debug, Default)]
18pub struct SystemEntropy;
19
20impl EntropySource for SystemEntropy {
21 fn fill(&mut self, destination: &mut [u8]) -> Result<(), Error> {
22 getrandom::fill(destination).map_err(|_| Error::EntropyUnavailable)
23 }
24}