1pub use rand_chacha::*;
7pub use reseeding::ReseedingRng;
8
9use crate::matter::crypto::{CryptoRng, RngCore};
10
11mod reseeding;
12
13pub struct RngAdaptor<T>(T);
15
16impl<T> RngAdaptor<T> {
17 pub const fn new(rng: T) -> Self {
19 Self(rng)
20 }
21}
22
23impl<T> rand_core09::RngCore for RngAdaptor<T>
24where
25 T: RngCore,
26{
27 fn next_u32(&mut self) -> u32 {
28 self.0.next_u32()
29 }
30
31 fn next_u64(&mut self) -> u64 {
32 self.0.next_u64()
33 }
34
35 fn fill_bytes(&mut self, dest: &mut [u8]) {
36 self.0.fill_bytes(dest);
37 }
38}
39
40impl<T> rand_core09::CryptoRng for RngAdaptor<T> where T: RngCore + CryptoRng {}
41
42impl<T> RngCore for RngAdaptor<T>
43where
44 T: rand_core09::RngCore,
45{
46 fn next_u32(&mut self) -> u32 {
47 self.0.next_u32()
48 }
49
50 fn next_u64(&mut self) -> u64 {
51 self.0.next_u64()
52 }
53
54 fn fill_bytes(&mut self, dest: &mut [u8]) {
55 self.0.fill_bytes(dest);
56 }
57
58 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core06::Error> {
59 self.0.fill_bytes(dest);
60
61 Ok(())
62 }
63}
64
65impl<T> CryptoRng for RngAdaptor<T> where T: rand_core09::RngCore + rand_core09::CryptoRng {}
66
67pub fn reseeding_csprng<T: rand_core09::TryRngCore>(
77 trng: T,
78 reseed_threshold: u64,
79) -> Result<RngAdaptor<ReseedingRng<ChaCha12Core, T>>, T::Error> {
80 Ok(RngAdaptor::new(ReseedingRng::new(reseed_threshold, trng)?))
81}