1#![feature(trait_alias)]
7#![warn(missing_docs)]
8#![deny(
9 trivial_casts,
10 trivial_numeric_casts,
11 unused_import_braces,
12 unused_qualifications
13)]
14#![no_std]
15#![cfg_attr(docsrs, feature(doc_cfg))]
16
17#[cfg(any(feature = "alloc", test))]
18extern crate alloc;
19
20#[cfg(any(test, feature = "std"))]
21#[macro_use]
22extern crate std;
23
24pub mod builder;
26pub mod channel;
28pub mod error;
30pub mod handshake;
32pub mod key;
34pub mod nonce;
36pub mod params;
38pub mod prologue;
40pub mod session;
42pub mod tag;
44pub mod transport;
46
47pub type Result<T> = anyhow::Result<T, error::Error>;
49
50#[cfg(not(any(feature = "alloc", feature = "std")))]
51mod inner {
52 use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
53 use rand_xorshift::XorShiftRng;
54
55 pub struct HeaplessRng(XorShiftRng);
56
57 impl Default for HeaplessRng {
58 fn default() -> Self {
59 Self(XorShiftRng::from_entropy())
60 }
61 }
62
63 impl CryptoRng for HeaplessRng {}
64
65 impl RngCore for HeaplessRng {
66 fn next_u32(&mut self) -> u32 {
67 self.0.next_u32()
68 }
69
70 fn next_u64(&mut self) -> u64 {
71 self.0.next_u64()
72 }
73
74 fn fill_bytes(&mut self, dest: &mut [u8]) {
75 self.0.fill_bytes(dest)
76 }
77
78 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
79 self.0.try_fill_bytes(dest)
80 }
81 }
82
83 pub fn get_rng() -> impl CryptoRng + RngCore {
84 HeaplessRng::default()
85 }
86}
87
88#[cfg(all(not(feature = "std"), feature = "alloc"))]
89mod inner {
90 use rand_core::{CryptoRng, RngCore};
91
92 pub fn get_rng() -> impl CryptoRng + RngCore {
93 rand::thread_rng()
94 }
95}
96
97#[cfg(feature = "std")]
98mod inner {
99 use rand_core::{CryptoRng, RngCore};
100
101 pub fn get_rng() -> impl CryptoRng + RngCore {
102 rand::thread_rng()
103 }
104}