1#![cfg_attr(not(feature = "std"), no_std)]
2extern crate alloc;
3
4pub mod aes128;
5pub mod aes256;
6pub mod ed25519;
7pub mod hkdf;
8pub mod hmac;
9pub mod identity;
10pub mod pkcs7;
11pub mod ratchet;
12pub mod sha256;
13pub mod sha512;
14pub mod token;
15pub mod x25519;
16
17pub trait Rng {
20 fn fill_bytes(&mut self, dest: &mut [u8]);
21}
22
23pub struct FixedRng {
25 bytes: alloc::vec::Vec<u8>,
26 pos: usize,
27}
28
29impl FixedRng {
30 pub fn new(bytes: &[u8]) -> Self {
31 Self {
32 bytes: bytes.to_vec(),
33 pos: 0,
34 }
35 }
36}
37
38impl Rng for FixedRng {
39 fn fill_bytes(&mut self, dest: &mut [u8]) {
40 for b in dest.iter_mut() {
41 *b = self.bytes[self.pos % self.bytes.len()];
42 self.pos += 1;
43 }
44 }
45}
46
47#[cfg(feature = "std")]
49pub struct OsRng;
50
51#[cfg(feature = "std")]
52impl Rng for OsRng {
53 fn fill_bytes(&mut self, dest: &mut [u8]) {
54 #[cfg(target_os = "espidf")]
56 {
57 unsafe {
58 esp_idf_sys::esp_fill_random(
59 dest.as_mut_ptr() as *mut core::ffi::c_void,
60 dest.len(),
61 );
62 }
63 }
64 #[cfg(not(target_os = "espidf"))]
65 {
66 use std::io::Read;
67 let mut f = std::fs::File::open("/dev/urandom").expect("Failed to open /dev/urandom");
68 f.read_exact(dest)
69 .expect("Failed to read from /dev/urandom");
70 }
71 }
72}