Skip to main content

dope_core/
random.rs

1//! OS-provided seed material retained by a driver.
2
3use std::mem;
4
5use dope_sys::thread::Thread as _;
6
7/// Two seed words acquired from the selected operating-system backend.
8#[repr(transparent)]
9pub struct Seed([u64; 2]);
10
11impl Seed {
12    pub fn acquire() -> Result<Self, crate::Errno> {
13        let mut bytes = mem::MaybeUninit::uninit();
14        let [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] =
15            *crate::Backend::entropy::<16>(&mut bytes)?;
16        Ok(Self([
17            u64::from_ne_bytes([a, b, c, d, e, f, g, h]),
18            u64::from_ne_bytes([i, j, k, l, m, n, o, p]),
19        ]))
20    }
21
22    /// Applies the owning layer's key derivation without adding seed storage.
23    pub fn map(&self, derive: impl FnOnce([u64; 2]) -> [u64; 2]) -> Self {
24        Self(derive(self.0))
25    }
26}
27
28impl AsRef<[u64; 2]> for Seed {
29    fn as_ref(&self) -> &[u64; 2] {
30        &self.0
31    }
32}