Skip to content

Commit e5d68a9

Browse files
committed
Move check for global Crypto object to getRandomValues
1 parent cf72d6c commit e5d68a9

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

src/backends/wasm_js.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,18 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
1313
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
1414
const MAX_BUFFER_SIZE: usize = 65536;
1515

16-
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
17-
CRYPTO.with(|crypto| {
18-
let crypto = crypto.as_ref().ok_or(Error::WEB_CRYPTO)?;
19-
inner(crypto, dest)
20-
})
21-
}
22-
2316
#[cfg(not(target_feature = "atomics"))]
24-
fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
17+
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2518
for chunk in dest.chunks_mut(MAX_BUFFER_SIZE) {
26-
if crypto.get_random_values(chunk).is_err() {
27-
return Err(Error::WEB_GET_RANDOM_VALUES);
19+
if get_random_values(chunk).is_err() {
20+
return Err(Error::WEB_CRYPTO);
2821
}
2922
}
3023
Ok(())
3124
}
3225

3326
#[cfg(target_feature = "atomics")]
34-
fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
27+
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3528
// getRandomValues does not work with all types of WASM memory,
3629
// so we initially write to browser memory to avoid exceptions.
3730
let buf_len = usize::min(dest.len(), MAX_BUFFER_SIZE);
@@ -52,8 +45,8 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
5245
&buf.subarray(0, chunk_len)
5346
};
5447

55-
if crypto.get_random_values(sub_buf).is_err() {
56-
return Err(Error::WEB_GET_RANDOM_VALUES);
48+
if get_random_values(sub_buf).is_err() {
49+
return Err(Error::WEB_CRYPTO);
5750
}
5851

5952
// SAFETY: `sub_buf`'s length is the same length as `chunk`
@@ -64,16 +57,11 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6457

6558
#[wasm_bindgen]
6659
extern "C" {
67-
// Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/)
68-
type Crypto;
69-
// Holds the global `Crypto` object.
70-
#[wasm_bindgen(thread_local_v2, js_namespace = globalThis, js_name = crypto)]
71-
static CRYPTO: Option<Crypto>;
7260
// Crypto.getRandomValues()
7361
#[cfg(not(target_feature = "atomics"))]
74-
#[wasm_bindgen(method, js_name = getRandomValues, catch)]
75-
fn get_random_values(this: &Crypto, buf: &mut [MaybeUninit<u8>]) -> Result<(), JsValue>;
62+
#[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)]
63+
fn get_random_values(buf: &mut [MaybeUninit<u8>]) -> Result<(), JsValue>;
7664
#[cfg(target_feature = "atomics")]
77-
#[wasm_bindgen(method, js_name = getRandomValues, catch)]
78-
fn get_random_values(this: &Crypto, buf: &js_sys::Uint8Array) -> Result<(), JsValue>;
65+
#[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)]
66+
fn get_random_values(buf: &js_sys::Uint8Array) -> Result<(), JsValue>;
7967
}

src/error.rs

-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ impl Error {
3939
pub const NO_RDRAND: Error = Self::new_internal(6);
4040
/// The environment does not support the Web Crypto API.
4141
pub const WEB_CRYPTO: Error = Self::new_internal(7);
42-
/// Calling Web Crypto API `crypto.getRandomValues` failed.
43-
pub const WEB_GET_RANDOM_VALUES: Error = Self::new_internal(8);
4442
/// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
4543
pub const VXWORKS_RAND_SECURE: Error = Self::new_internal(11);
4644
/// Calling Windows ProcessPrng failed.
@@ -155,7 +153,6 @@ fn internal_desc(error: Error) -> Option<&'static str> {
155153
Error::FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely",
156154
Error::NO_RDRAND => "RDRAND: instruction not supported",
157155
Error::WEB_CRYPTO => "Web Crypto API is unavailable",
158-
Error::WEB_GET_RANDOM_VALUES => "Calling Web API crypto.getRandomValues failed",
159156
Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized",
160157
Error::WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure",
161158
Error::RNDR_FAILURE => "RNDR: Could not generate a random number",

0 commit comments

Comments
 (0)