Skip to content

Commit

Permalink
Add chunking for Node.js implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Richey <joerichey@google.com>
  • Loading branch information
josephlr committed Oct 23, 2022
1 parent c6344de commit 6e3cee8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/js.rs
Expand Up @@ -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),
Expand All @@ -38,8 +40,10 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> 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) => {
Expand Down

0 comments on commit 6e3cee8

Please sign in to comment.