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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Cryptographically secure random number generation
//!
//! # Usage
//!
//! To generate cryptographically secure random numbers, start by opening a
//! [`RandomNumberGenerator`]. This can be done either via the [`open`] method
//! where you specify the random algorithm to use or with the [`system_preferred`]
//! method, where the system default is used. Then, to fill a buffer with random
//! numbers, call the [`gen_random`] method.
//!
//! ```
//! use win_crypto_ng::random::{RandomAlgorithmId, RandomNumberGenerator};
//!
//! let mut buffer = [0u8; 32];
//! let rng = RandomNumberGenerator::open(RandomAlgorithmId::Rng).unwrap();
//! rng.gen_random(&mut buffer).unwrap();
//!
//! assert_ne!(&buffer, &[0u8; 32]);
//! ```
//!
//! [`RandomNumberGenerator`]: struct.RandomNumberGenerator.html
//! [`open`]: struct.RandomNumberGenerator.html#method.open
//! [`system_preferred`]: struct.RandomNumberGenerator.html#method.system_preferred
//! [`gen_random`]: struct.RandomNumberGenerator.html#method.gen_random

use crate::helpers::{AlgoHandle, Handle};
use crate::Error;
use core::convert::TryFrom;
use core::fmt;
use core::ptr;
use winapi::shared::bcrypt::*;
use winapi::shared::ntdef::ULONG;

/// Random number generation algorithms identifiers
#[derive(Clone, Copy, PartialOrd, PartialEq)]
pub enum RandomAlgorithmId {
    /// The random-number generator algorithm.
    ///
    /// Standard: FIPS 186-2, FIPS 140-2, NIST SP 800-90
    ///
    /// Beginning with Windows Vista with SP1 and Windows Server 2008, the
    /// random number generator is based on the AES counter mode specified in
    /// the NIST SP 800-90 standard.
    ///
    /// **Windows Vista**: The random number generator is based on the hash-based
    /// random number generator specified in the FIPS 186-2 standard.
    ///
    /// **Windows 8**: Beginning with Windows 8, the RNG algorithm supports
    /// FIPS 186-3. Keys less than or equal to 1024 bits adhere to FIPS 186-2
    /// and keys greater than 1024 to FIPS 186-3.
    Rng,
    /// The dual elliptic curve random-number generator algorithm.
    ///
    /// Standard: SP800-90.
    ///
    /// **Windows 8**: Beginning with Windows 8, the EC RNG algorithm supports
    /// FIPS 186-3. Keys less than or equal to 1024 bits adhere to FIPS 186-2
    /// and keys greater than 1024 to FIPS 186-3.
    ///
    /// **Windows 10**: Beginning with Windows 10, the dual elliptic curve random
    /// number generator algorithm has been removed. Existing uses of this
    /// algorithm will continue to work; however, the random number generator is
    /// based on the AES counter mode specified in the NIST SP 800-90 standard.
    /// New code should use [`Rng`](#variant.Rng), and it is recommended that
    /// existing code be changed to use [`Rng`](#variant.Rng).
    DualECRng,
    /// The random-number generator algorithm suitable for DSA (Digital
    /// Signature RandomAlgorithmId).
    ///
    /// Standard: FIPS 186-2.
    ///
    /// **Windows 8**: Support for FIPS 186-3 begins.
    Fips186DsaRng,
}

impl<'a> TryFrom<&'a str> for RandomAlgorithmId {
    type Error = &'a str;

    fn try_from(value: &'a str) -> Result<RandomAlgorithmId, Self::Error> {
        match value {
            BCRYPT_RNG_ALGORITHM => Ok(RandomAlgorithmId::Rng),
            BCRYPT_RNG_DUAL_EC_ALGORITHM => Ok(RandomAlgorithmId::DualECRng),
            BCRYPT_RNG_FIPS186_DSA_ALGORITHM => Ok(RandomAlgorithmId::Fips186DsaRng),
            _ => Err(value),
        }
    }
}

impl From<RandomAlgorithmId> for &'static str {
    fn from(val: RandomAlgorithmId) -> Self {
        match val {
            RandomAlgorithmId::Rng => BCRYPT_RNG_ALGORITHM,
            RandomAlgorithmId::DualECRng => BCRYPT_RNG_DUAL_EC_ALGORITHM,
            RandomAlgorithmId::Fips186DsaRng => BCRYPT_RNG_FIPS186_DSA_ALGORITHM,
        }
    }
}

impl fmt::Display for RandomAlgorithmId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", Into::<&'static str>::into(*self))
    }
}

/// Random number generator
///
/// Main type that is capable of generating random
/// numbers.
pub struct RandomNumberGenerator {
    handle: RandomAlgoHandle,
}

impl RandomNumberGenerator {
    /// Open a random number generator using the provided algorithm.
    ///
    /// # Examples
    ///
    /// ```
    /// # use win_crypto_ng::random::{RandomAlgorithmId, RandomNumberGenerator};
    /// let rng = RandomNumberGenerator::open(RandomAlgorithmId::Rng);
    ///
    /// assert!(rng.is_ok());
    /// ```
    pub fn open(id: RandomAlgorithmId) -> crate::Result<RandomNumberGenerator> {
        let handle = RandomAlgoHandle::open(id)?;
        Ok(Self { handle })
    }

    /// Open a random number generator using the system preferred algorithm.
    ///
    /// **Windows Vista**: This is not supported.
    pub fn system_preferred() -> RandomNumberGenerator {
        let handle = RandomAlgoHandle::SystemPreferred;
        Self { handle }
    }

    /// Fills a buffer with random bytes.
    ///
    /// Use a random number for the entropy.
    ///
    /// # Examples
    ///
    /// ```
    /// # use win_crypto_ng::random::{RandomAlgorithmId, RandomNumberGenerator};
    /// let mut buffer = [0u8; 32];
    /// let rng = RandomNumberGenerator::system_preferred();
    /// rng.gen_random(&mut buffer).unwrap();
    ///
    /// assert_ne!(&buffer, &[0u8; 32]);
    /// ```
    pub fn gen_random(&self, buffer: &mut [u8]) -> crate::Result<()> {
        self.gen_random_with_opts(buffer, self.handle.flags())
    }

    /// Fills a buffer with random bytes.
    ///
    /// This function will use the number in the buffer as additional
    /// entropy for the random number.
    ///
    /// **Windows 8 and later**: This does the exact same thing as
    /// [`gen_random`](#method.gen_random).
    ///
    /// # Examples
    ///
    /// ```
    /// # use win_crypto_ng::random::{RandomAlgorithmId, RandomNumberGenerator};
    /// let mut buffer = [0u8; 32];
    /// let rng = RandomNumberGenerator::system_preferred();
    /// rng.gen_random_with_entropy_in_buffer(&mut buffer).unwrap();
    ///
    /// assert_ne!(&buffer, &[0u8; 32]);
    /// ```
    pub fn gen_random_with_entropy_in_buffer(&self, buffer: &mut [u8]) -> crate::Result<()> {
        self.gen_random_with_opts(
            buffer,
            self.handle.flags() | BCRYPT_RNG_USE_ENTROPY_IN_BUFFER,
        )
    }

    fn gen_random_with_opts(&self, buffer: &mut [u8], opts: ULONG) -> crate::Result<()> {
        let handle = self.handle.handle();

        Error::check(unsafe {
            BCryptGenRandom(handle, buffer.as_mut_ptr(), buffer.len() as ULONG, opts)
        })
    }
}

#[cfg(feature = "rand")]
impl rand_core::CryptoRng for RandomNumberGenerator {}

#[cfg(feature = "rand")]
impl rand_core::RngCore for RandomNumberGenerator {
    fn next_u32(&mut self) -> u32 {
        rand_core::impls::next_u32_via_fill(self)
    }

    fn next_u64(&mut self) -> u64 {
        rand_core::impls::next_u64_via_fill(self)
    }

    fn fill_bytes(&mut self, dst: &mut [u8]) {
        // Panics are allowed in `fill_bytes`
        self.try_fill_bytes(dst).unwrap()
    }

    fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), rand_core::Error> {
        self.gen_random(dst)
            .map_err(|e| From::<core::num::NonZeroU32>::from(e.into()))
    }
}

/// Wrapper around `AlgoHandle` that can only specify RNG algorithms.
enum RandomAlgoHandle {
    /// System-preferred algorithm provider.
    SystemPreferred,
    /// An already opened provider for a specified algorithm.
    Specified(AlgoHandle),
}

impl RandomAlgoHandle {
    fn open(id: RandomAlgorithmId) -> crate::Result<Self> {
        Ok(Self::Specified(AlgoHandle::open(id.into())?))
    }

    fn handle(&self) -> BCRYPT_ALG_HANDLE {
        match self {
            Self::SystemPreferred => ptr::null_mut(),
            Self::Specified(handle) => handle.as_ptr(),
        }
    }

    fn flags(&self) -> ULONG {
        match self {
            Self::SystemPreferred => BCRYPT_USE_SYSTEM_PREFERRED_RNG,
            Self::Specified(_) => 0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_rng(rng: RandomNumberGenerator) {
        let empty = vec![0; 32];

        let mut buf = empty.clone();
        rng.gen_random(&mut buf).expect("RNG to succeed");
        assert_ne!(&buf, &empty);

        let mut buf2 = buf.clone();
        rng.gen_random_with_entropy_in_buffer(&mut buf2)
            .expect("RNG to succeeed");
        assert_ne!(&buf2, &empty);
        assert_ne!(&buf2, &buf);
    }

    #[test]
    fn system_preferred() {
        let rng = RandomNumberGenerator::system_preferred();
        test_rng(rng);
    }

    #[test]
    fn rng() {
        let rng = RandomNumberGenerator::open(RandomAlgorithmId::Rng).unwrap();
        test_rng(rng);
    }

    #[test]
    fn dualecrng() {
        let rng = RandomNumberGenerator::open(RandomAlgorithmId::DualECRng).unwrap();
        test_rng(rng);
    }

    #[test]
    fn fips186dsarng() {
        let rng = RandomNumberGenerator::open(RandomAlgorithmId::Fips186DsaRng).unwrap();
        test_rng(rng);
    }
}