|
1 | 1 | //! Implementation for WASM based on Web and Node.js
|
| 2 | +
|
| 3 | +/// Proc-macro is not hygienic. |
| 4 | +/// See <https://github.com/rustwasm/wasm-bindgen/pull/4315>. |
| 5 | +#[cfg(feature = "std")] |
| 6 | +extern crate std; |
| 7 | + |
2 | 8 | use crate::Error;
|
3 | 9 | use core::mem::MaybeUninit;
|
| 10 | +#[cfg(feature = "std")] |
| 11 | +use std::thread_local; |
4 | 12 |
|
5 | 13 | pub use crate::util::{inner_u32, inner_u64};
|
6 | 14 |
|
7 | 15 | #[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
|
8 | 16 | compile_error!("`wasm_js` backend can be enabled only for OS-less WASM targets!");
|
9 | 17 |
|
10 |
| -use js_sys::{global, Uint8Array}; |
11 |
| -use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue}; |
| 18 | +use js_sys::Uint8Array; |
| 19 | +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; |
12 | 20 |
|
13 | 21 | // Size of our temporary Uint8Array buffer used with WebCrypto methods
|
14 | 22 | // Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
15 | 23 | const CRYPTO_BUFFER_SIZE: u16 = 256;
|
16 | 24 |
|
17 | 25 | pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
18 |
| - let global: Global = global().unchecked_into(); |
19 |
| - let crypto = global.crypto(); |
20 |
| - |
21 |
| - if !crypto.is_object() { |
22 |
| - return Err(Error::WEB_CRYPTO); |
23 |
| - } |
24 |
| - |
25 |
| - // getRandomValues does not work with all types of WASM memory, |
26 |
| - // so we initially write to browser memory to avoid exceptions. |
27 |
| - let buf = Uint8Array::new_with_length(CRYPTO_BUFFER_SIZE.into()); |
28 |
| - for chunk in dest.chunks_mut(CRYPTO_BUFFER_SIZE.into()) { |
29 |
| - let chunk_len: u32 = chunk |
30 |
| - .len() |
31 |
| - .try_into() |
32 |
| - .expect("chunk length is bounded by CRYPTO_BUFFER_SIZE"); |
33 |
| - // The chunk can be smaller than buf's length, so we call to |
34 |
| - // JS to create a smaller view of buf without allocation. |
35 |
| - let sub_buf = buf.subarray(0, chunk_len); |
36 |
| - |
37 |
| - if crypto.get_random_values(&sub_buf).is_err() { |
38 |
| - return Err(Error::WEB_GET_RANDOM_VALUES); |
| 26 | + CRYPTO.with(|crypto| { |
| 27 | + let crypto = crypto.as_ref().ok_or(Error::WEB_CRYPTO)?; |
| 28 | + |
| 29 | + // getRandomValues does not work with all types of WASM memory, |
| 30 | + // so we initially write to browser memory to avoid exceptions. |
| 31 | + let buf = Uint8Array::new_with_length(CRYPTO_BUFFER_SIZE.into()); |
| 32 | + for chunk in dest.chunks_mut(CRYPTO_BUFFER_SIZE.into()) { |
| 33 | + let chunk_len: u32 = chunk |
| 34 | + .len() |
| 35 | + .try_into() |
| 36 | + .expect("chunk length is bounded by CRYPTO_BUFFER_SIZE"); |
| 37 | + // The chunk can be smaller than buf's length, so we call to |
| 38 | + // JS to create a smaller view of buf without allocation. |
| 39 | + let sub_buf = buf.subarray(0, chunk_len); |
| 40 | + |
| 41 | + if crypto.get_random_values(&sub_buf).is_err() { |
| 42 | + return Err(Error::WEB_GET_RANDOM_VALUES); |
| 43 | + } |
| 44 | + |
| 45 | + // SAFETY: `sub_buf`'s length is the same length as `chunk` |
| 46 | + unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr().cast::<u8>()) }; |
39 | 47 | }
|
40 |
| - |
41 |
| - // SAFETY: `sub_buf`'s length is the same length as `chunk` |
42 |
| - unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr().cast::<u8>()) }; |
43 |
| - } |
44 |
| - Ok(()) |
| 48 | + Ok(()) |
| 49 | + }) |
45 | 50 | }
|
46 | 51 |
|
47 | 52 | #[wasm_bindgen]
|
48 | 53 | extern "C" {
|
49 |
| - // Return type of js_sys::global() |
50 |
| - type Global; |
51 | 54 | // Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/)
|
52 | 55 | type Crypto;
|
53 |
| - // Getters for the Crypto API |
54 |
| - #[wasm_bindgen(method, getter)] |
55 |
| - fn crypto(this: &Global) -> Crypto; |
| 56 | + // Holds the global `Crypto` object. |
| 57 | + #[wasm_bindgen(thread_local_v2, js_namespace = globalThis, js_name = crypto)] |
| 58 | + static CRYPTO: Option<Crypto>; |
56 | 59 | // Crypto.getRandomValues()
|
57 | 60 | #[wasm_bindgen(method, js_name = getRandomValues, catch)]
|
58 | 61 | fn get_random_values(this: &Crypto, buf: &Uint8Array) -> Result<(), JsValue>;
|
|
0 commit comments