diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ddcfffe3..313be5ca 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -66,7 +66,10 @@ jobs: target: ${{ matrix.target }} toolchain: stable override: true - - run: sudo apt install gcc-multilib + # update is needed to fix the 404 error on install, see: + # https://github.com/actions/virtual-environments/issues/675 + - run: sudo apt-get update + - run: sudo apt-get install gcc-multilib - run: cargo test --target ${{ matrix.target }} windows-tests: diff --git a/Cargo.toml b/Cargo.toml index 8f43e16b..912a6243 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,12 +27,10 @@ libc = { version = "0.2.64", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] wasi = "0.10" -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", cargo_web))'.dependencies] -stdweb = { version = "0.4.18", default-features = false, optional = true } -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dependencies] +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] wasm-bindgen = { version = "0.2.62", default-features = false, optional = true } js-sys = { version = "0.3", optional = true } -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dev-dependencies] +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies] wasm-bindgen-test = "0.3.18" [features] @@ -41,7 +39,7 @@ std = [] # Feature to enable fallback RDRAND-based implementation on x86/x86_64 rdrand = [] # Feature to enable JavaScript bindings on wasm32-unknown-unknown -js = ["stdweb", "wasm-bindgen", "js-sys"] +js = ["wasm-bindgen", "js-sys"] # Feature to enable custom RNG implementations custom = [] # Unstable feature to support being a libstd dependency diff --git a/src/cloudabi.rs b/src/cloudabi.rs deleted file mode 100644 index 5c0ae411..00000000 --- a/src/cloudabi.rs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Implementation for CloudABI -use crate::Error; -use core::num::NonZeroU32; - -extern "C" { - fn cloudabi_sys_random_get(buf: *mut u8, buf_len: usize) -> u16; -} - -pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - let errno = unsafe { cloudabi_sys_random_get(dest.as_mut_ptr(), dest.len()) }; - if let Some(code) = NonZeroU32::new(errno as u32) { - Err(Error::from(code)) - } else { - Ok(()) // Zero means success for CloudABI - } -} diff --git a/src/wasm-bindgen.rs b/src/js.rs similarity index 100% rename from src/wasm-bindgen.rs rename to src/js.rs diff --git a/src/lib.rs b/src/lib.rs index 8f0bbf5f..7d33e79e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,8 +22,7 @@ //! | Dragonfly BSD | `*‑dragonfly` | [`/dev/random`][8] //! | Solaris, illumos | `*‑solaris`, `*‑illumos` | [`getrandom()`][9] if available, otherwise [`/dev/random`][10] //! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`][11] -//! | Redox | `*‑cloudabi` | [`rand:`][12] -//! | CloudABI | `*‑redox` | [`cloudabi_sys_random_get`][13] +//! | Redox | `*‑redox` | [`rand:`][12] //! | Haiku | `*‑haiku` | `/dev/random` (identical to `/dev/urandom`) //! | SGX | `x86_64‑*‑sgx` | [RDRAND][18] //! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure` @@ -68,9 +67,8 @@ //! that you are building for an environment containing JavaScript, and will //! call the appropriate methods. Both web browser (main window and Web Workers) //! and Node.js environments are supported, invoking the methods -//! [described above](#supported-targets). This crate can be built with either -//! the [wasm-bindgen](https://github.com/rust-lang/rust-bindgen) or -//! [cargo-web](https://github.com/koute/cargo-web) toolchains. +//! [described above](#supported-targets) using the +//! [wasm-bindgen](https://github.com/rust-lang/rust-bindgen) toolchain. //! //! This feature has no effect on targets other than `wasm32-unknown-unknown`. //! @@ -132,7 +130,6 @@ //! [10]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html //! [11]: https://fuchsia.dev/fuchsia-src/zircon/syscalls/cprng_draw //! [12]: https://github.com/redox-os/randd/blob/master/src/main.rs -//! [13]: https://github.com/nuxinl/cloudabi#random_get //! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues //! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback //! [16]: #webassembly-support @@ -149,6 +146,8 @@ )] #![no_std] #![warn(rust_2018_idioms, unused_lifetimes, missing_docs)] +// `matches!` macro was added only in Rust 1.42, which is bigger than our MSRV +#![allow(clippy::match_like_matches_macro)] #[macro_use] extern crate cfg_if; @@ -183,8 +182,6 @@ cfg_if! { } else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] { mod util_libc; #[path = "bsd_arandom.rs"] mod imp; - } else if #[cfg(target_os = "cloudabi")] { - #[path = "cloudabi.rs"] mod imp; } else if #[cfg(target_os = "fuchsia")] { #[path = "fuchsia.rs"] mod imp; } else if #[cfg(target_os = "ios")] { @@ -210,9 +207,7 @@ cfg_if! { #[path = "rdrand.rs"] mod imp; } else if #[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] { - #[cfg_attr(cargo_web, path = "stdweb.rs")] - #[cfg_attr(not(cargo_web), path = "wasm-bindgen.rs")] - mod imp; + #[path = "js.rs"] mod imp; } else if #[cfg(feature = "custom")] { use custom as imp; } else { diff --git a/src/stdweb.rs b/src/stdweb.rs deleted file mode 100644 index 5a50b68d..00000000 --- a/src/stdweb.rs +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. -use crate::Error; - -extern crate std; -use std::thread_local; - -use stdweb::js; - -#[derive(Clone, Copy, PartialEq)] -enum RngSource { - Browser, - Node, -} - -thread_local!( - static RNG_SOURCE: Result = getrandom_init(); -); - -pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - RNG_SOURCE.with(|&source| getrandom_fill(source?, dest)) -} - -fn getrandom_init() -> Result { - if js! { return typeof self === "object"; } == true { - // We are in a Browser or WebWorker - let supported = js! { return typeof self.crypto === "object"; }; - if supported == true { - Ok(RngSource::Browser) - } else { - Err(Error::WEB_CRYPTO) - } - } else { - // We are in Node.js - let supported = js! { - try { - require("crypto"); - return true; - } catch(err) { - return false; - } - }; - if supported == true { - Ok(RngSource::Node) - } else { - Err(Error::NODE_CRYPTO) - } - } -} - -fn getrandom_fill(source: RngSource, dest: &mut [u8]) -> Result<(), Error> { - for chunk in dest.chunks_mut(65536) { - let len = chunk.len() as u32; - let ptr = chunk.as_mut_ptr() as i32; - - let success = js! { - try { - let array = new Uint8Array(@{ len }); - - if @{ source == RngSource::Browser } { - self.crypto.getRandomValues(array); - } else { - require("crypto").randomFillSync(array); - } - - HEAPU8.set(array, @{ ptr }); - return true; - } catch(err) { - return false; - } - }; - - if success != true { - return match source { - RngSource::Browser => Err(Error::WEB_GET_RANDOM_VALUES), - RngSource::Node => Err(Error::NODE_RANDOM_FILL_SYNC), - }; - } - } - Ok(()) -}