1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//!
#![feature(trait_alias)]
#![warn(missing_docs)]
#![deny(
    trivial_casts,
    trivial_numeric_casts,
    unused_import_braces,
    unused_qualifications
)]
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(any(feature = "alloc", test))]
extern crate alloc;

#[cfg(any(test, feature = "std"))]
#[macro_use]
extern crate std;

/// Disco session builder
pub mod builder;
/// Disco errors
pub mod error;
/// Disco handshake
pub mod handshake;
/// Disco key traits
pub mod key;
/// Disco nonce traits
pub mod nonce;
/// Disco params
pub mod params;
/// Disco session
pub mod session;
/// Disco tag trait
pub mod tag;
/// Disco transport
pub mod transport;

/// the Result type for all operations
pub type Result<T> = anyhow::Result<T, error::Error>;

#[cfg(not(any(feature = "alloc", feature = "std")))]
mod inner {
    use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
    use rand_xorshift::XorShiftRng;

    pub struct HeaplessRng(XorShiftRng);

    impl Default for HeaplessRng {
        fn default() -> Self {
            Self(XorShiftRng::from_entropy())
        }
    }

    impl CryptoRng for HeaplessRng {}

    impl RngCore for HeaplessRng {
        fn next_u32(&mut self) -> u32 {
            self.0.next_u32()
        }

        fn next_u64(&mut self) -> u64 {
            self.0.next_u64()
        }

        fn fill_bytes(&mut self, dest: &mut [u8]) {
            self.0.fill_bytes(dest)
        }

        fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
            self.0.try_fill_bytes(dest)
        }
    }

    pub fn get_rng() -> impl CryptoRng + RngCore {
        HeaplessRng::default()
    }
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
mod inner {
    use rand_core::{CryptoRng, RngCore};

    pub fn get_rng() -> impl CryptoRng + RngCore {
        rand::thread_rng()
    }
}

#[cfg(feature = "std")]
mod inner {
    use rand_core::{CryptoRng, RngCore};

    pub fn get_rng() -> impl CryptoRng + RngCore {
        rand::thread_rng()
    }
}