Skip to content

Commit ea999a6

Browse files
josephlrnewpavlov
authored andcommitted
Remove cloudabi, winapi, and fuchsia-cprng dependancies (#40)
1 parent 5c7faa5 commit ea999a6

File tree

7 files changed

+40
-41
lines changed

7 files changed

+40
-41
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ matrix:
102102
- rustup target add x86_64-sun-solaris
103103
- rustup target add x86_64-unknown-cloudabi
104104
- rustup target add x86_64-unknown-freebsd
105-
#- rustup target add x86_64-unknown-fuchsia
105+
- rustup target add x86_64-fuchsia
106106
- rustup target add x86_64-unknown-netbsd
107107
- rustup target add x86_64-unknown-redox
108108
- rustup target add x86_64-fortanix-unknown-sgx
@@ -113,7 +113,7 @@ matrix:
113113
- cargo build --target=x86_64-sun-solaris --all-features
114114
- cargo build --target=x86_64-unknown-cloudabi --all-features
115115
- cargo build --target=x86_64-unknown-freebsd --all-features
116-
#- cargo build --target=x86_64-unknown-fuchsia --all-features
116+
- cargo build --target=x86_64-fuchsia --all-features
117117
- cargo build --target=x86_64-unknown-netbsd --all-features
118118
- cargo build --target=x86_64-unknown-redox --all-features
119119
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features
@@ -123,7 +123,7 @@ matrix:
123123
- cargo build --target=x86_64-sun-solaris --all-features
124124
- cargo build --target=x86_64-unknown-cloudabi --all-features
125125
- cargo build --target=x86_64-unknown-freebsd --all-features
126-
#- cargo build --target=x86_64-unknown-fuchsia --all-features
126+
- cargo build --target=x86_64-fuchsia --all-features
127127
- cargo build --target=x86_64-unknown-netbsd --all-features
128128
- cargo build --target=x86_64-unknown-redox --all-features
129129
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features

Cargo.toml

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,21 @@ members = ["tests/wasm_bindgen"]
2020
[dependencies]
2121
log = { version = "0.4", optional = true }
2222

23-
[target.'cfg(unix)'.dependencies]
24-
libc = "0.2.34"
25-
lazy_static = "1.3.0"
26-
27-
[target.'cfg(windows)'.dependencies]
28-
winapi = { version = "0.3.6", features = ["minwindef", "ntsecapi", "winnt"] }
29-
30-
[target.'cfg(target_os = "cloudabi")'.dependencies]
31-
cloudabi = "0.0.3"
32-
33-
[target.'cfg(fuchsia)'.dependencies]
34-
fuchsia-cprng = "0.1"
23+
[target.'cfg(any(unix, target_os = "wasi"))'.dependencies]
24+
libc = "0.2.54"
3525

36-
[target.'cfg(target_os = "redox")'.dependencies]
26+
# For holding file descriptors
27+
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
3728
lazy_static = "1.3.0"
3829

30+
# For caching result of CPUID check for RDRAND
31+
[target.'cfg(any(target_env = "sgx", target_os = "uefi"))'.dependencies]
32+
lazy_static = { version = "1.3.0", features = ["spin_no_std"] }
33+
3934
[target.wasm32-unknown-unknown.dependencies]
4035
wasm-bindgen = { version = "0.2.29", optional = true }
4136
stdweb = { version = "0.4.9", optional = true }
4237
lazy_static = "1.3.0"
4338

44-
[target.wasm32-wasi.dependencies]
45-
libc = "0.2.54"
46-
47-
[target.'cfg(any(target_env = "sgx", target_os = "uefi"))'.dependencies]
48-
lazy_static = { version = "1.3.0", features = ["spin_no_std"] }
49-
5039
[features]
5140
std = []

src/cloudabi.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
use crate::Error;
1111
use core::num::NonZeroU32;
1212

13+
extern "C" {
14+
fn cloudabi_sys_random_get(buf: *mut u8, buf_len: usize) -> u16;
15+
}
16+
1317
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
14-
let errno = unsafe { cloudabi::random_get(dest) };
15-
if errno == cloudabi::errno::SUCCESS {
16-
Ok(())
17-
} else {
18-
let code = NonZeroU32::new(errno as u32).unwrap();
19-
error!("cloudabi::random_get syscall failed with code {}", code);
18+
let errno = unsafe { cloudabi_sys_random_get(dest.as_mut_ptr(), dest.len()) };
19+
if let Some(code) = NonZeroU32::new(errno as u32) {
20+
error!("cloudabi_sys_random_get failed with code {}", code);
2021
Err(Error::from(code))
22+
} else {
23+
Ok(()) // Zero means success for CloudABI
2124
}
2225
}
2326

src/fuchsia.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
use crate::Error;
1111
use core::num::NonZeroU32;
1212

13+
#[link(name = "zircon")]
14+
extern "C" {
15+
fn zx_cprng_draw(buffer: *mut u8, length: usize);
16+
}
17+
1318
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
14-
fuchsia_cprng::cprng_draw(dest);
19+
unsafe { zx_cprng_draw(dest.as_mut_ptr(), dest.len()) }
1520
Ok(())
1621
}
1722

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! | Solaris, illumos | [`getrandom`][9] system call if available, otherwise [`/dev/random`][10]
2323
//! | Fuchsia OS | [`cprng_draw`][11]
2424
//! | Redox | [`rand:`][12]
25-
//! | CloudABI | [`random_get`][13]
25+
//! | CloudABI | [`cloudabi_sys_random_get`][13]
2626
//! | Haiku | `/dev/random` (identical to `/dev/urandom`)
2727
//! | SGX, UEFI | [RDRAND][18]
2828
//! | Web browsers | [`Crypto.getRandomValues`][14] (see [Support for WebAssembly and ams.js][14])
@@ -99,17 +99,17 @@
9999
//!
100100
//! [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html
101101
//! [2]: http://man7.org/linux/man-pages/man4/urandom.4.html
102-
//! [3]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx
102+
//! [3]: https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/nf-ntsecapi-rtlgenrandom
103103
//! [4]: https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc
104104
//! [5]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
105105
//! [6]: https://man.openbsd.org/getentropy.2
106106
//! [7]: http://netbsd.gw.com/cgi-bin/man-cgi?random+4+NetBSD-current
107107
//! [8]: https://leaf.dragonflybsd.org/cgi/web-man?command=random&section=4
108108
//! [9]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html
109109
//! [10]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html
110-
//! [11]: https://fuchsia.googlesource.com/zircon/+/HEAD/docs/syscalls/cprng_draw.md
110+
//! [11]: https://fuchsia.googlesource.com/fuchsia/+/master/zircon/docs/syscalls/cprng_draw.md
111111
//! [12]: https://github.com/redox-os/randd/blob/master/src/main.rs
112-
//! [13]: https://github.com/NuxiNL/cloudabi/blob/v0.20/cloudabi.txt#L1826
112+
//! [13]: https://github.com/nuxinl/cloudabi#random_get
113113
//! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
114114
//! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
115115
//! [16]: #support-for-webassembly-and-amsjs
@@ -153,11 +153,11 @@ macro_rules! mod_use {
153153
};
154154
}
155155

156+
// These targets use std anyway, so we use the std declarations.
156157
#[cfg(any(
157158
feature = "std",
158159
windows,
159160
unix,
160-
target_os = "cloudabi",
161161
target_os = "redox",
162162
target_arch = "wasm32",
163163
))]

src/macos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct SecRandom([u8; 0]);
2222
extern "C" {
2323
static kSecRandomDefault: *const SecRandom;
2424

25-
fn SecRandomCopyBytes(rnd: *const SecRandom, count: usize, bytes: *mut u8) -> libc::c_int;
25+
fn SecRandomCopyBytes(rnd: *const SecRandom, count: usize, bytes: *mut u8) -> i32;
2626
}
2727

2828
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {

src/windows.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ extern crate std;
1212
use crate::Error;
1313
use core::num::NonZeroU32;
1414
use std::io;
15-
use winapi::shared::minwindef::ULONG;
16-
use winapi::um::ntsecapi::RtlGenRandom;
17-
use winapi::um::winnt::PVOID;
15+
16+
extern "system" {
17+
#[link_name = "SystemFunction036"]
18+
fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8;
19+
}
1820

1921
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
20-
// Prevent overflow of ULONG
21-
for chunk in dest.chunks_mut(ULONG::max_value() as usize) {
22-
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr() as PVOID, chunk.len() as ULONG) };
22+
// Prevent overflow of u32
23+
for chunk in dest.chunks_mut(u32::max_value() as usize) {
24+
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr(), chunk.len() as u32) };
2325
if ret == 0 {
2426
error!("RtlGenRandom call failed");
2527
return Err(io::Error::last_os_error().into());

0 commit comments

Comments
 (0)