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 (
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.
pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed)
status := C.SecRandomCopyBytes(0, bytes_needed, buffer)
mut buffer := []byte{ len: bytes_needed }
status := C.SecRandomCopyBytes(0, bytes_needed, buffer.data)
if status != 0 {
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.
pub fn read(bytes_needed int) ?[]byte {
mut buffer := &byte(0)
unsafe {
buffer = malloc(bytes_needed)
}
mut bstart := buffer
mut buffer := unsafe { malloc(bytes_needed) }
mut bytes_read := 0
mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes
for bytes_read < bytes_needed {
batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes }
unsafe {
bstart = buffer + bytes_read
}
rbytes := getrandom(batch_size, bstart)
rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
if rbytes == -1 {
unsafe { free(buffer) }
return read_error
}
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 {

View File

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

View File

@ -15,11 +15,11 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS.
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
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 {
return read_error
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
return buffer
}