crypto.rand: use byteptr.vbytes instead of c_array_to_bytes_tmp (#8786)

pull/8811/head
Nick Treleaven 2021-02-17 19:47:19 +00:00 committed by GitHub
parent 4ccf991f61
commit 33d8074846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 44 deletions

View File

@ -7,20 +7,3 @@ module rand
const ( const (
read_error = error('crypto.rand.read() error reading random bytes') read_error = error('crypto.rand.read() error reading random bytes')
) )
// NOTE: temp until we have []bytes(buff)
fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte {
mut arr := []byte{len:len, cap:1}
arr.data = buffer
/*
arr = array {
len: len
cap: 1
element_size: 1
data: buffer
}
*/
return arr
}

View File

@ -12,10 +12,10 @@ fn C.SecRandomCopyBytes() int
// read returns an array of `bytes_needed` random bytes read from the OS. // read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte { pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed) mut buffer := []byte{ len: bytes_needed }
status := C.SecRandomCopyBytes(0, bytes_needed, buffer) status := C.SecRandomCopyBytes(0, bytes_needed, buffer.data)
if status != 0 { if status != 0 {
return read_error return read_error
} }
return c_array_to_bytes_tmp(bytes_needed, buffer) return buffer
} }

View File

@ -10,27 +10,20 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS. // read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte { pub fn read(bytes_needed int) ?[]byte {
mut buffer := &byte(0) mut buffer := unsafe { malloc(bytes_needed) }
unsafe {
buffer = malloc(bytes_needed)
}
mut bstart := buffer
mut bytes_read := 0 mut bytes_read := 0
mut remaining_bytes := bytes_needed mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes // getrandom syscall wont block if requesting <= 256 bytes
for bytes_read < bytes_needed { for bytes_read < bytes_needed {
batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes } batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes }
unsafe { rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
bstart = buffer + bytes_read
}
rbytes := getrandom(batch_size, bstart)
if rbytes == -1 { if rbytes == -1 {
unsafe { free(buffer) } unsafe { free(buffer) }
return read_error return read_error
} }
bytes_read += rbytes bytes_read += rbytes
} }
return c_array_to_bytes_tmp(bytes_needed, buffer) return unsafe {buffer.vbytes(bytes_needed)}
} }
fn getrandom(bytes_needed int, buffer voidptr) int { fn getrandom(bytes_needed int, buffer voidptr) int {

View File

@ -14,27 +14,20 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS. // read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte { pub fn read(bytes_needed int) ?[]byte {
mut buffer := &byte(0) mut buffer := unsafe { malloc(bytes_needed) }
unsafe {
buffer = malloc(bytes_needed)
}
mut bstart := buffer
mut bytes_read := 0 mut bytes_read := 0
mut remaining_bytes := bytes_needed mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes // getrandom syscall wont block if requesting <= 256 bytes
for bytes_read < bytes_needed { for bytes_read < bytes_needed {
batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes } batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes }
unsafe { rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
bstart = buffer + bytes_read
}
rbytes := getrandom(batch_size, bstart)
if rbytes == -1 { if rbytes == -1 {
free(buffer) unsafe { free(buffer) }
return read_error return read_error
} }
bytes_read += rbytes bytes_read += rbytes
} }
return c_array_to_bytes_tmp(bytes_needed, buffer) return unsafe {buffer.vbytes(bytes_needed)}
} }
fn v_getrandom(bytes_needed int, buffer voidptr) int { fn v_getrandom(bytes_needed int, buffer voidptr) int {

View File

@ -15,11 +15,11 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS. // read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte { pub fn read(bytes_needed int) ?[]byte {
mut buffer := unsafe {malloc(bytes_needed)} mut buffer := []byte{ len: bytes_needed }
// use bcrypt_use_system_preferred_rng because we passed null as algo // use bcrypt_use_system_preferred_rng because we passed null as algo
status := C.BCryptGenRandom(0, buffer, bytes_needed, bcrypt_use_system_preferred_rng) status := C.BCryptGenRandom(0, buffer.data, bytes_needed, bcrypt_use_system_preferred_rng)
if status != status_success { if status != status_success {
return read_error return read_error
} }
return c_array_to_bytes_tmp(bytes_needed, buffer) return buffer
} }