diff --git a/src/backends/wasm_js.rs b/src/backends/wasm_js.rs index 2eb3da4c..b838d08f 100644 --- a/src/backends/wasm_js.rs +++ b/src/backends/wasm_js.rs @@ -13,25 +13,18 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues const MAX_BUFFER_SIZE: usize = 65536; -pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - CRYPTO.with(|crypto| { - let crypto = crypto.as_ref().ok_or(Error::WEB_CRYPTO)?; - inner(crypto, dest) - }) -} - #[cfg(not(target_feature = "atomics"))] -fn inner(crypto: &Crypto, dest: &mut [MaybeUninit]) -> Result<(), Error> { +fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { for chunk in dest.chunks_mut(MAX_BUFFER_SIZE) { - if crypto.get_random_values(chunk).is_err() { - return Err(Error::WEB_GET_RANDOM_VALUES); + if get_random_values(chunk).is_err() { + return Err(Error::WEB_CRYPTO); } } Ok(()) } #[cfg(target_feature = "atomics")] -fn inner(crypto: &Crypto, dest: &mut [MaybeUninit]) -> Result<(), Error> { +fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { // getRandomValues does not work with all types of WASM memory, // so we initially write to browser memory to avoid exceptions. let buf_len = usize::min(dest.len(), MAX_BUFFER_SIZE); @@ -52,8 +45,8 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit]) -> Result<(), Error> { &buf.subarray(0, chunk_len) }; - if crypto.get_random_values(sub_buf).is_err() { - return Err(Error::WEB_GET_RANDOM_VALUES); + if get_random_values(sub_buf).is_err() { + return Err(Error::WEB_CRYPTO); } // SAFETY: `sub_buf`'s length is the same length as `chunk` @@ -64,16 +57,11 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit]) -> Result<(), Error> { #[wasm_bindgen] extern "C" { - // Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/) - type Crypto; - // Holds the global `Crypto` object. - #[wasm_bindgen(thread_local_v2, js_namespace = globalThis, js_name = crypto)] - static CRYPTO: Option; // Crypto.getRandomValues() #[cfg(not(target_feature = "atomics"))] - #[wasm_bindgen(method, js_name = getRandomValues, catch)] - fn get_random_values(this: &Crypto, buf: &mut [MaybeUninit]) -> Result<(), JsValue>; + #[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)] + fn get_random_values(buf: &mut [MaybeUninit]) -> Result<(), JsValue>; #[cfg(target_feature = "atomics")] - #[wasm_bindgen(method, js_name = getRandomValues, catch)] - fn get_random_values(this: &Crypto, buf: &js_sys::Uint8Array) -> Result<(), JsValue>; + #[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)] + fn get_random_values(buf: &js_sys::Uint8Array) -> Result<(), JsValue>; } diff --git a/src/error.rs b/src/error.rs index 0f486c7b..0c60f929 100644 --- a/src/error.rs +++ b/src/error.rs @@ -39,8 +39,6 @@ impl Error { pub const NO_RDRAND: Error = Self::new_internal(6); /// The environment does not support the Web Crypto API. pub const WEB_CRYPTO: Error = Self::new_internal(7); - /// Calling Web Crypto API `crypto.getRandomValues` failed. - pub const WEB_GET_RANDOM_VALUES: Error = Self::new_internal(8); /// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized). pub const VXWORKS_RAND_SECURE: Error = Self::new_internal(11); /// Calling Windows ProcessPrng failed. @@ -155,7 +153,6 @@ fn internal_desc(error: Error) -> Option<&'static str> { Error::FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely", Error::NO_RDRAND => "RDRAND: instruction not supported", Error::WEB_CRYPTO => "Web Crypto API is unavailable", - Error::WEB_GET_RANDOM_VALUES => "Calling Web API crypto.getRandomValues failed", Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized", Error::WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure", Error::RNDR_FAILURE => "RNDR: Could not generate a random number",