Skip to content

Commit 1049ce8

Browse files
committed
review updates
1 parent 8f6edad commit 1049ce8

File tree

13 files changed

+104
-112
lines changed

13 files changed

+104
-112
lines changed

src/dragonfly_haiku.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,16 @@
88

99
//! Implementation for DragonFly / Haiku
1010
use super::Error;
11+
use super::utils::use_init;
1112
use std::fs::File;
1213
use std::io::Read;
1314
use std::cell::RefCell;
14-
use std::ops::DerefMut;
1515

1616
thread_local!(static RNG_FILE: RefCell<Option<File>> = RefCell::new(None));
1717

1818
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
1919
RNG_FILE.with(|f| {
20-
let mut f = f.borrow_mut();
21-
let f: &mut Option<File> = f.deref_mut();
22-
if let Some(f) = f {
23-
f.read_exact(dest)
24-
} else {
25-
let mut rng_file = File::open("/dev/random")?;
26-
rng_file.read_exact(dest)?;
27-
*f = Some(rng_file);
28-
Ok(())
29-
}
20+
use_init(f, || File::open("/dev/random"), |f| f.read_exact(dest))
3021
}).map_err(|_| Error::Unknown)
3122
}
23+

src/emscripten.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! Implementation for DragonFly / Haiku / Emscripten
9+
//! Implementation for Emscripten
1010
use super::Error;
1111
use std::fs::File;
1212
use std::io::Read;
1313
use std::cell::RefCell;
14-
use std::ops::DerefMut;
14+
use super::utils::use_init;
1515

1616
thread_local!(static RNG_FILE: RefCell<Option<File>> = RefCell::new(None));
1717

@@ -20,19 +20,13 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
2020
// bytes. `crypto.randomBytes` documents: "To minimize threadpool
2121
// task length variation, partition large randomBytes requests when
2222
// doing so as part of fulfilling a client request.
23-
for chunk in dest.chunks_mut(65536) {
24-
RNG_FILE.with(|f| {
25-
let mut f = f.borrow_mut();
26-
let f: &mut Option<File> = f.deref_mut();
27-
if let Some(f) = f {
28-
f.read_exact(chunk)
29-
} else {
30-
let mut rng_file = File::open("/dev/random")?;
31-
rng_file.read_exact(chunk)?;
32-
*f = Some(rng_file);
33-
Ok(())
23+
RNG_FILE.with(|f| {
24+
use_init(f, || File::open("/dev/random"), |f| {
25+
for chunk in dest.chunks_mut(65536) {
26+
f.read_exact(chunk)?;
3427
}
35-
}).map_err(|_| Error::Unknown)?;
36-
}
37-
Ok(())
28+
Ok(())
29+
})
30+
}).map_err(|_| Error::Unknown)
3831
}
32+

src/freebsd.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
extern crate libc;
1111

1212
use super::Error;
13-
use std::ptr;
13+
use core::ptr;
1414

1515
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
1616
let mib = [libc::CTL_KERN, libc::KERN_ARND];
@@ -28,4 +28,5 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
2828
}
2929
}
3030
Ok(())
31-
}
31+
}
32+

src/fuchsia.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
1515
fuchsia_cprng::cprng_draw(dest);
1616
Ok(())
1717
}
18+

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
))]
2222
#[macro_use] extern crate std;
2323

24+
#[cfg(any(
25+
target_os = "android",
26+
target_os = "netbsd",
27+
target_os = "solaris",
28+
target_os = "redox",
29+
target_os = "dragonfly",
30+
target_os = "haiku",
31+
target_os = "emscripten",
32+
target_os = "linux",
33+
))]
34+
mod utils;
2435
mod error;
2536
pub use error::Error;
2637

src/linux_android.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@ extern crate std;
1111
extern crate libc;
1212

1313
use super::Error;
14+
use super::utils::use_init;
1415
use std::fs::File;
1516
use std::io;
1617
use std::io::Read;
1718
use std::cell::RefCell;
18-
use std::ops::DerefMut;
1919

2020
enum RngSource {
2121
GetRandom,
2222
Device(File),
23-
None,
2423
}
2524

2625
thread_local!(
27-
static RNG_SOURCE: RefCell<RngSource> = RefCell::new(RngSource::None);
26+
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);
2827
);
2928

3029
fn syscall_getrandom(dest: &mut [u8]) -> Result<(), io::Error> {
@@ -39,30 +38,24 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), io::Error> {
3938

4039
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
4140
RNG_SOURCE.with(|f| {
42-
let mut f = f.borrow_mut();
43-
let f: &mut RngSource = f.deref_mut();
44-
if let RngSource::None = f {
45-
*f = if is_getrandom_available() {
41+
use_init(f,
42+
|| {
43+
let s = if is_getrandom_available() {
4644
RngSource::GetRandom
4745
} else {
48-
let mut buf = [0u8; 1];
49-
File::open("/dev/random")
50-
.and_then(|mut f| f.read_exact(&mut buf))
51-
.map_err(|_| Error::Unknown)?;
52-
let mut rng_file = File::open("/dev/urandom")
53-
.map_err(|_| Error::Unknown)?;
54-
RngSource::Device(rng_file)
46+
// read one byte from "/dev/random" to ensure that
47+
// OS RNG has initialized
48+
File::open("/dev/random")?.read_exact(&mut [0u8; 1])?;
49+
RngSource::Device(File::open("/dev/urandom")?)
50+
};
51+
Ok(s)
52+
}, |f| {
53+
match f {
54+
RngSource::GetRandom => syscall_getrandom(dest),
55+
RngSource::Device(f) => f.read_exact(dest),
5556
}
56-
}
57-
if let RngSource::Device(f) = f {
58-
f.read_exact(dest)
59-
.map_err(|_| Error::Unknown)
60-
} else {
61-
syscall_getrandom(dest)
62-
.map_err(|_| Error::Unknown)
63-
}
64-
})?;
65-
Ok(())
57+
}).map_err(|_| Error::Unknown)
58+
})
6659
}
6760

6861
fn is_getrandom_available() -> bool {
@@ -76,8 +69,7 @@ fn is_getrandom_available() -> bool {
7669
let mut buf: [u8; 0] = [];
7770
let available = match syscall_getrandom(&mut buf) {
7871
Ok(()) => true,
79-
Err(ref err) if err.raw_os_error() == Some(libc::ENOSYS) => false,
80-
Err(_) => true,
72+
Err(err) => err.raw_os_error() != Some(libc::ENOSYS),
8173
};
8274
AVAILABLE.store(available, Ordering::Relaxed);
8375
});

src/macos.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// except according to those terms.
88

99
//! Implementation for MacOS / iOS
10-
extern crate libc;
11-
1210
use super::Error;
1311

1412
// TODO: check correctness

src/netbsd.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,21 @@
99
//! Implementation for NetBSD
1010
1111
use super::Error;
12+
use super::utils::use_init;
1213
use std::fs::File;
1314
use std::io::Read;
1415
use std::cell::RefCell;
15-
use std::ops::DerefMut;
1616

1717
thread_local!(static RNG_FILE: RefCell<Option<File>> = RefCell::new(None));
1818

1919
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
2020
RNG_FILE.with(|f| {
21-
let mut f = f.borrow_mut();
22-
let f: &mut Option<File> = f.deref_mut();
23-
if let Some(f) = f {
24-
f.read_exact(dest)
25-
} else {
26-
// read one byte from /dev/random to ensure that RNG is bootstrapped
27-
let mut buf = [0u8];
28-
File::open("/dev/random")?.read_exact(&mut buf)?;
29-
30-
let mut rng_file = File::open("/dev/urandom")?;
31-
rng_file.read_exact(dest)?;
32-
*f = Some(rng_file);
33-
Ok(())
34-
}
21+
use_init(f, || {
22+
// read one byte from "/dev/random" to ensure that
23+
// OS RNG has initialized
24+
File::open("/dev/random")?.read_exact(&mut [0u8; 1])?;
25+
File::open("/dev/urandom")
26+
}, |f| f.read_exact(dest))
3527
}).map_err(|_| Error::Unknown)
3628
}
29+

src/openbsd_bitrig.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
1515
for chunk in dest.chunks_mut(256) {
1616
let ret = unsafe {
1717
libc::getentropy(
18-
dest.as_mut_ptr() as *mut libc::c_void,
19-
dest.len()
18+
chunk.as_mut_ptr() as *mut libc::c_void,
19+
chunk.len()
2020
)
2121
};
2222
if ret == -1 {
@@ -25,3 +25,4 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
2525
}
2626
Ok(())
2727
}
28+

src/redox.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,16 @@
88

99
//! Implementation for Redox
1010
use super::Error;
11+
use super::utils::use_init;
1112
use std::fs::File;
1213
use std::io::Read;
1314
use std::cell::RefCell;
14-
use std::ops::DerefMut;
1515

1616
thread_local!(static RNG_FILE: RefCell<Option<File>> = RefCell::new(None));
1717

1818
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
1919
RNG_FILE.with(|f| {
20-
let mut f = f.borrow_mut();
21-
let f: &mut Option<File> = f.deref_mut();
22-
if let Some(f) = f {
23-
f.read_exact(dest)
24-
} else {
25-
let mut rng_file = File::open("rand:")?;
26-
rng_file.read_exact(dest)?;
27-
*f = Some(rng_file);
28-
Ok(())
29-
}
20+
use_init(f, || File::open("rand:"), |f| f.read_exact(dest))
3021
}).map_err(|_| Error::Unknown)
3122
}
23+

0 commit comments

Comments
 (0)