From 6e3cee8a4e58ffa5fffcea705a3e932c30f5579d Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Sat, 22 Oct 2022 20:50:56 -0700 Subject: [PATCH] Add chunking for Node.js implementation Signed-off-by: Joe Richey --- src/js.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/js.rs b/src/js.rs index 7b9ae313..e692a1bd 100644 --- a/src/js.rs +++ b/src/js.rs @@ -16,6 +16,8 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue}; // Size of our temporary Uint8Array buffer used with WebCrypto methods // Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues const WEB_CRYPTO_BUFFER_SIZE: usize = 256; +// Node.js's crypto.randomFillSync requires the size to be less than 2**31. +const NODE_MAX_BUFFER_SIZE: usize = (1 << 31) - 1; enum RngSource { Node(NodeCrypto), @@ -38,8 +40,10 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> // have to ensure the memory in `dest` is initialized. let dest = uninit_slice_fill_zero(dest); - if n.random_fill_sync(dest).is_err() { - return Err(Error::NODE_RANDOM_FILL_SYNC); + for chunk in dest.chunks_mut(NODE_MAX_BUFFER_SIZE) { + if n.random_fill_sync(chunk).is_err() { + return Err(Error::NODE_RANDOM_FILL_SYNC); + } } } RngSource::Web(crypto, buf) => {