@@ -27,28 +27,33 @@ thread_local!(
27
27
static RNG_SOURCE : Result <RngSource , Error > = getrandom_init( ) ;
28
28
) ;
29
29
30
- pub ( crate ) fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
30
+ pub ( crate ) unsafe fn getrandom_inner ( dst : * mut u8 , len : usize ) -> Result < ( ) , Error > {
31
31
RNG_SOURCE . with ( |result| {
32
32
let source = result. as_ref ( ) . map_err ( |& e| e) ?;
33
33
34
34
match source {
35
35
RngSource :: Node ( n) => {
36
- if n. random_fill_sync ( dest) . is_err ( ) {
36
+ let dst = core:: slice:: from_raw_parts_mut ( dst, len) ;
37
+ if n. random_fill_sync ( dst) . is_err ( ) {
37
38
return Err ( Error :: NODE_RANDOM_FILL_SYNC ) ;
38
39
}
39
40
}
40
41
RngSource :: Browser ( crypto, buf) => {
41
42
// getRandomValues does not work with all types of WASM memory,
42
43
// so we initially write to browser memory to avoid exceptions.
43
- for chunk in dest. chunks_mut ( BROWSER_CRYPTO_BUFFER_SIZE ) {
44
+ while len != 0 {
45
+ let chunk_len = core:: cmp:: min ( len, BROWSER_CRYPTO_BUFFER_SIZE ) ;
44
46
// The chunk can be smaller than buf's length, so we call to
45
47
// JS to create a smaller view of buf without allocation.
46
- let sub_buf = buf. subarray ( 0 , chunk . len ( ) as u32 ) ;
48
+ let sub_buf = buf. subarray ( 0 , chunk_len as u32 ) ;
47
49
48
50
if crypto. get_random_values ( & sub_buf) . is_err ( ) {
49
51
return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
50
52
}
51
- sub_buf. copy_to ( chunk) ;
53
+ sub_buf. raw_copy_to_ptr ( dst) ;
54
+
55
+ dst = dst. add ( chunk_len) ;
56
+ len -= chunk_len;
52
57
}
53
58
}
54
59
} ;
0 commit comments