Skip to content

Commit 3fa761b

Browse files
committed
Add chunking for Node.js implementation
Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 1d371ef commit 3fa761b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/js.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
1616
// Size of our temporary Uint8Array buffer used with WebCrypto methods
1717
// Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
1818
const WEB_CRYPTO_BUFFER_SIZE: usize = 256;
19+
// Node.js's crypto.randomFillSync requires the size to be less than 2**31.
20+
const NODE_MAX_BUFFER_SIZE: usize = (1 << 31) - 1;
1921

2022
enum RngSource {
2123
Node(NodeCrypto),
@@ -38,8 +40,10 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
3840
// have to ensure the memory in `dest` is initialized.
3941
let dest = uninit_slice_fill_zero(dest);
4042

41-
if n.random_fill_sync(dest).is_err() {
42-
return Err(Error::NODE_RANDOM_FILL_SYNC);
43+
for chunk in dest.chunks_mut(NODE_MAX_BUFFER_SIZE) {
44+
if n.random_fill_sync(chunk).is_err() {
45+
return Err(Error::NODE_RANDOM_FILL_SYNC);
46+
}
4347
}
4448
}
4549
RngSource::Web(crypto, buf) => {

0 commit comments

Comments
 (0)