disco_rs/
lib.rs

1/*
2    Copyright David Huseby, All Rights Reserved.
3    SPDX-License-Identifier: Apache-2.0
4*/
5//!
6#![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
24/// Disco session builder
25pub mod builder;
26/// Disco channel
27pub mod channel;
28/// Disco errors
29pub mod error;
30/// Disco handshake
31pub mod handshake;
32/// Disco key traits
33pub mod key;
34/// Disco nonce traits
35pub mod nonce;
36/// Disco params
37pub mod params;
38/// Disco prologue trait
39pub mod prologue;
40/// Disco session
41pub mod session;
42/// Disco tag trait
43pub mod tag;
44/// Disco transport
45pub mod transport;
46
47/// the Result type for all operations
48pub 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}