Skip to content

Commit 1f1f964

Browse files
committed
Merge branch 'master' into 0.2
2 parents add45a3 + 81bd43e commit 1f1f964

12 files changed

+78
-30
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ matrix:
139139
- cargo xbuild --target=x86_64-unknown-uefi
140140
- cargo xbuild --target=x86_64-unknown-hermit
141141
- cargo xbuild --target=x86_64-unknown-l4re-uclibc
142+
- cargo xbuild --target=x86_64-wrs-vxworks
142143
# also test minimum dependency versions are usable
143144
- cargo generate-lockfile -Z minimal-versions
144145
- cargo build --target=x86_64-sun-solaris

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ cfg-if = "0.1.2"
2222
compiler_builtins = { version = "0.1", optional = true }
2323
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
2424

25-
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
26-
libc = { version = "0.2.62", default-features = false }
25+
[target.'cfg(unix)'.dependencies]
26+
libc = { version = "0.2.64", default-features = false }
2727

2828
[target.'cfg(target_os = "wasi")'.dependencies]
2929
wasi = "0.7"

src/freebsd.rs renamed to src/bsd_arandom.rs

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

9-
//! Implementation for FreeBSD
10-
use crate::util_libc::{sys_fill_exact, Weak};
9+
//! Implementation for FreeBSD and NetBSD
10+
use crate::util_libc::sys_fill_exact;
1111
use crate::Error;
12-
use core::{mem, ptr};
13-
14-
type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
12+
use core::ptr;
1513

1614
fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t {
1715
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
@@ -27,19 +25,25 @@ fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t {
2725
)
2826
};
2927
if ret == -1 {
30-
error!("freebsd: kern.arandom syscall failed");
28+
error!("sysctl kern.arandom: syscall failed");
3129
-1
3230
} else {
3331
len as libc::ssize_t
3432
}
3533
}
3634

3735
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
38-
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
39-
if let Some(fptr) = GETRANDOM.ptr() {
40-
let func: GetRandomFn = unsafe { mem::transmute(fptr) };
41-
sys_fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) })
42-
} else {
43-
sys_fill_exact(dest, kern_arnd)
36+
#[cfg(target_os = "freebsd")]
37+
{
38+
use crate::util_libc::Weak;
39+
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
40+
type GetRandomFn =
41+
unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
42+
43+
if let Some(fptr) = GETRANDOM.ptr() {
44+
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
45+
return sys_fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) });
46+
}
4447
}
48+
sys_fill_exact(dest, kern_arnd)
4549
}

src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Error {
4040
///
4141
/// This method is identical to `std::io::Error::raw_os_error()`, except
4242
/// that it works in `no_std` contexts. If this method returns `None`, the
43-
/// error value can still be formatted via the `Diplay` implementation.
43+
/// error value can still be formatted via the `Display` implementation.
4444
#[inline]
4545
pub fn raw_os_error(self) -> Option<i32> {
4646
if self.0.get() < Self::INTERNAL_START {
@@ -145,6 +145,7 @@ pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error!(7);
145145
pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error!(8);
146146
pub(crate) const STDWEB_NO_RNG: Error = internal_error!(9);
147147
pub(crate) const STDWEB_RNG_FAILED: Error = internal_error!(10);
148+
pub(crate) const RAND_SECURE_FATAL: Error = internal_error!(11);
148149

149150
fn internal_desc(error: Error) -> Option<&'static str> {
150151
match error {
@@ -159,6 +160,7 @@ fn internal_desc(error: Error) -> Option<&'static str> {
159160
BINDGEN_GRV_UNDEF => Some("wasm-bindgen: crypto.getRandomValues is undefined"),
160161
STDWEB_NO_RNG => Some("stdweb: no randomness source available"),
161162
STDWEB_RNG_FAILED => Some("stdweb: failed to get randomness"),
163+
RAND_SECURE_FATAL => Some("randSecure: random number generator module is not initialized"),
162164
_ => None,
163165
}
164166
}

src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! | iOS | [`SecRandomCopyBytes`][4]
1919
//! | FreeBSD | [`getrandom()`][21] if available, otherwise [`kern.arandom`][5]
2020
//! | OpenBSD | [`getentropy`][6]
21-
//! | NetBSD | [`/dev/urandom`][7] after successfully polling `/dev/random`
21+
//! | NetBSD | [`kern.arandom`][7]
2222
//! | Dragonfly BSD | [`/dev/random`][8]
2323
//! | Solaris, illumos | [`getrandom`][9] system call if available, otherwise [`/dev/random`][10]
2424
//! | Fuchsia OS | [`cprng_draw`][11]
@@ -27,8 +27,9 @@
2727
//! | Haiku | `/dev/random` (identical to `/dev/urandom`)
2828
//! | L4RE, SGX, UEFI | [RDRAND][18]
2929
//! | Hermit | [RDRAND][18] as [`sys_rand`][22] is currently broken.
30-
//! | Web browsers | [`Crypto.getRandomValues`][14] (see [Support for WebAssembly and ams.js][14])
31-
//! | Node.js | [`crypto.randomBytes`][15] (see [Support for WebAssembly and ams.js][16])
30+
//! | VxWorks | `randABytes` after checking entropy pool initialization with `randSecure`
31+
//! | Web browsers | [`Crypto.getRandomValues`][14] (see [Support for WebAssembly and asm.js][16])
32+
//! | Node.js | [`crypto.randomBytes`][15] (see [Support for WebAssembly and asm.js][16])
3233
//! | WASI | [`__wasi_random_get`][17]
3334
//!
3435
//! Getrandom doesn't have a blanket implementation for all Unix-like operating
@@ -80,7 +81,7 @@
8081
//! A few, Linux, NetBSD and Solaris, offer a choice between blocking and
8182
//! getting an error; in these cases we always choose to block.
8283
//!
83-
//! On Linux (when the `genrandom` system call is not available) and on NetBSD
84+
//! On Linux (when the `getrandom` system call is not available) and on NetBSD
8485
//! reading from `/dev/urandom` never blocks, even when the OS hasn't collected
8586
//! enough entropy yet. To avoid returning low-entropy bytes, we first read from
8687
//! `/dev/random` and only switch to `/dev/urandom` once this has succeeded.
@@ -102,7 +103,7 @@
102103
//! [4]: https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc
103104
//! [5]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
104105
//! [6]: https://man.openbsd.org/getentropy.2
105-
//! [7]: http://netbsd.gw.com/cgi-bin/man-cgi?random+4+NetBSD-current
106+
//! [7]: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7+NetBSD-8.0
106107
//! [8]: https://leaf.dragonflybsd.org/cgi/web-man?command=random&section=4
107108
//! [9]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html
108109
//! [10]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html
@@ -111,7 +112,7 @@
111112
//! [13]: https://github.com/nuxinl/cloudabi#random_get
112113
//! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
113114
//! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
114-
//! [16]: #support-for-webassembly-and-amsjs
115+
//! [16]: #support-for-webassembly-and-asmjs
115116
//! [17]: https://github.com/WebAssembly/WASI/blob/master/design/WASI-core.md#__wasi_random_get
116117
//! [18]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
117118
//! [19]: https://www.unix.com/man-page/mojave/2/getentropy/
@@ -196,7 +197,7 @@ cfg_if! {
196197
} else if #[cfg(target_os = "emscripten")] {
197198
#[path = "use_file.rs"] mod imp;
198199
} else if #[cfg(target_os = "freebsd")] {
199-
#[path = "freebsd.rs"] mod imp;
200+
#[path = "bsd_arandom.rs"] mod imp;
200201
} else if #[cfg(target_os = "fuchsia")] {
201202
#[path = "fuchsia.rs"] mod imp;
202203
} else if #[cfg(target_os = "haiku")] {
@@ -210,7 +211,7 @@ cfg_if! {
210211
} else if #[cfg(target_os = "macos")] {
211212
#[path = "macos.rs"] mod imp;
212213
} else if #[cfg(target_os = "netbsd")] {
213-
#[path = "use_file.rs"] mod imp;
214+
#[path = "bsd_arandom.rs"] mod imp;
214215
} else if #[cfg(target_os = "openbsd")] {
215216
#[path = "openbsd.rs"] mod imp;
216217
} else if #[cfg(target_os = "redox")] {
@@ -219,6 +220,8 @@ cfg_if! {
219220
#[path = "solaris_illumos.rs"] mod imp;
220221
} else if #[cfg(target_os = "wasi")] {
221222
#[path = "wasi.rs"] mod imp;
223+
} else if #[cfg(target_os = "vxworks")] {
224+
#[path = "vxworks.rs"] mod imp;
222225
} else if #[cfg(all(windows, getrandom_uwp))] {
223226
#[path = "windows_uwp.rs"] mod imp;
224227
} else if #[cfg(windows)] {

src/rdrand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
2626
let mut el = mem::zeroed();
2727
if _rdrand64_step(&mut el) == 1 {
2828
// AMD CPUs from families 14h to 16h (pre Ryzen) sometimes fail to
29-
// set CF on bogus random data, so we check these values explictly.
29+
// set CF on bogus random data, so we check these values explicitly.
3030
// See https://github.com/systemd/systemd/issues/11810#issuecomment-489727505
3131
// We perform this check regardless of target to guard against
3232
// any implementation that incorrectly fails to set CF.

src/solaris_illumos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//!
1616
//! Since Solaris 11.3 and mid-2015 illumos, the `getrandom` syscall is available.
1717
//! To make sure we can compile on both Solaris and its derivatives, as well as
18-
//! function, we check for the existance of getrandom(2) in libc by calling
18+
//! function, we check for the existence of getrandom(2) in libc by calling
1919
//! libc::dlsym.
2020
use crate::util_libc::{sys_fill_exact, Weak};
2121
use crate::{use_file, Error};

src/use_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3939
}
4040

4141
cfg_if! {
42-
if #[cfg(any(target_os = "android", target_os = "linux", target_os = "netbsd"))] {
42+
if #[cfg(any(target_os = "android", target_os = "linux"))] {
4343
fn init_file() -> Option<libc::c_int> {
4444
// Poll /dev/random to make sure it is ok to read from /dev/urandom.
4545
let mut pfd = libc::pollfd {

src/util.rs

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

99
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
1010

11-
// This structure represents a laziliy initialized static usize value. Useful
12-
// when it is perferable to just rerun initialization instead of locking.
11+
// This structure represents a lazily initialized static usize value. Useful
12+
// when it is preferable to just rerun initialization instead of locking.
1313
// Both unsync_init and sync_init will invoke an init() function until it
1414
// succeeds, then return the cached value for future calls.
1515
//
@@ -25,7 +25,7 @@ use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
2525
// v
2626
// }
2727
// the effects of c() or writes to shared memory will not necessarily be
28-
// observed and additional syncronization methods with be needed.
28+
// observed and additional synchronization methods with be needed.
2929
pub struct LazyUsize(AtomicUsize);
3030

3131
impl LazyUsize {

src/util_libc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ cfg_if! {
2626
}
2727

2828
pub fn last_os_error() -> Error {
29+
#[cfg(not(target_os = "vxworks"))]
2930
let errno = unsafe { *errno_location() };
31+
#[cfg(target_os = "vxworks")]
32+
let errno = unsafe { libc::errnoGet() };
3033
if errno > 0 {
3134
Error::from(NonZeroU32::new(errno as u32).unwrap())
3235
} else {

src/vxworks.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implementation for VxWorks
10+
use crate::error::{Error, RAND_SECURE_FATAL};
11+
use crate::util_libc::last_os_error;
12+
use core::sync::atomic::{AtomicBool, Ordering::Relaxed};
13+
14+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
15+
static RNG_INIT: AtomicBool = AtomicBool::new(false);
16+
while !RNG_INIT.load(Relaxed) {
17+
let ret = unsafe { libc::randSecure() };
18+
if ret < 0 {
19+
return Err(RAND_SECURE_FATAL);
20+
} else if ret > 0 {
21+
RNG_INIT.store(true, Relaxed);
22+
break;
23+
}
24+
unsafe { libc::usleep(10) };
25+
}
26+
27+
// Prevent overflow of i32
28+
for chunk in dest.chunks_mut(i32::max_value() as usize) {
29+
let ret = unsafe { libc::randABytes(chunk.as_mut_ptr(), chunk.len() as i32) };
30+
if ret != 0 {
31+
return Err(last_os_error());
32+
}
33+
}
34+
Ok(())
35+
}

src/windows_uwp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for Windows UWP targets. After deprecation of Windows XP
10-
//! and Vista, this can superseed the `RtlGenRandom`-based implementation.
10+
//! and Vista, this can supersede the `RtlGenRandom`-based implementation.
1111
use crate::Error;
1212
use core::{ffi::c_void, num::NonZeroU32, ptr};
1313

0 commit comments

Comments
 (0)