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
use crate::io;
use crate::mem;
use crate::sync;
use crate::sys::c;

/// The kinds of HashMap RNG that may be available
#[derive(Clone, Copy, Debug, PartialEq)]
enum HashMapRng {
    Preferred,
    Fallback,
}

pub fn hashmap_random_keys() -> (u64, u64) {
    match get_hashmap_rng() {
        HashMapRng::Preferred => {
            preferred_rng().expect("couldn't generate random bytes with preferred RNG")
        }
        HashMapRng::Fallback => {
            fallback_rng().expect("couldn't generate random bytes with fallback RNG")
        }
    }
}

/// Returns the HashMap RNG that should be used
///
/// Panics if they are both broken
fn get_hashmap_rng() -> HashMapRng {
    // Assume that if the preferred RNG is broken the first time we use it, it likely means
    // that: the DLL has failed to load, there is no point to calling it over-and-over again,
    // and we should cache the result
    static VALUE: sync::OnceLock<HashMapRng> = sync::OnceLock::new();
    *VALUE.get_or_init(choose_hashmap_rng)
}

/// Test whether we should use the preferred or fallback RNG
///
/// If the preferred RNG is successful, we choose it. Otherwise, if the fallback RNG is successful,
/// we choose that
///
/// Panics if both the preferred and the fallback RNG are both non-functional
fn choose_hashmap_rng() -> HashMapRng {
    let preferred_error = match preferred_rng() {
        Ok(_) => return HashMapRng::Preferred,
        Err(e) => e,
    };

    match fallback_rng() {
        Ok(_) => return HashMapRng::Fallback,
        Err(fallback_error) => panic!(
            "preferred RNG broken: `{}`, fallback RNG broken: `{}`",
            preferred_error, fallback_error
        ),
    }
}

/// Generate random numbers using the preferred RNG function (BCryptGenRandom)
fn preferred_rng() -> Result<(u64, u64), io::Error> {
    use crate::ptr;

    let mut v = (0, 0);
    let ret = unsafe {
        c::BCryptGenRandom(
            ptr::null_mut(),
            &mut v as *mut _ as *mut u8,
            mem::size_of_val(&v) as c::ULONG,
            c::BCRYPT_USE_SYSTEM_PREFERRED_RNG,
        )
    };

    if ret == 0 { Ok(v) } else { Err(io::Error::last_os_error()) }
}

/// Generate random numbers using the fallback RNG function (RtlGenRandom)
#[cfg(not(target_vendor = "uwp"))]
fn fallback_rng() -> Result<(u64, u64), io::Error> {
    let mut v = (0, 0);
    let ret =
        unsafe { c::RtlGenRandom(&mut v as *mut _ as *mut u8, mem::size_of_val(&v) as c::ULONG) };

    if ret != 0 { Ok(v) } else { Err(io::Error::last_os_error()) }
}

/// We can't use RtlGenRandom with UWP, so there is no fallback
#[cfg(target_vendor = "uwp")]
fn fallback_rng() -> Result<(u64, u64), io::Error> {
    Err(io::const_io_error!(io::ErrorKind::Unsupported, "RtlGenRandom() not supported on UWP"))
}