1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

crypto.rand module

This commit is contained in:
joe-conigliaro 2019-07-31 11:24:12 +10:00 committed by Alexander Medvednikov
parent 17e8c1d628
commit 1202631fa6
5 changed files with 134 additions and 0 deletions

21
vlib/crypto/rand/rand.v Normal file
View File

@ -0,0 +1,21 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
const (
ReadError = error('crypro.rand.read() error reading random bytes.')
)
// NOTE: temp until we have []bytes(buff)
fn c_array_to_bytes_tmp(len, buffer voidptr) []byte {
mut arr := []byte
arr = array {
len: len
cap: 1
element_size: 1
data: buffer
}
return arr
}

View File

@ -0,0 +1,51 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
import math
#include <sys/syscall.h>
import const(
SYS_getrandom
)
// const (
// SYS_getrandom = 278 // AArch65
// SYS_getrandom = 384 // ARM
// SYS_getrandom = 355 // x86
// SYS_getrandom = 318 // x86_64
// )
const (
ReadBatchSize = 256
)
pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed)
mut bytes_read := 0
// getrandom syscall wont block if requesting <= 256 bytes
if bytes_needed > ReadBatchSize {
no_batches := int(math.floor(f64(bytes_needed/ReadBatchSize)))
for i:=0; i<no_batches; i++ {
if _getrandom(ReadBatchSize, buffer+bytes_read) == -1 {
return ReadError
}
bytes_read += ReadBatchSize
}
}
if _getrandom(bytes_needed-bytes_read, buffer+bytes_read) == -1 {
return ReadError
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
}
fn _getrandom(bytes_needed int, buffer voidptr) int {
if bytes_needed > ReadBatchSize {
panic('_getrandom() dont request more thane $ReadBatchSize bytes at once.')
}
return C.syscall(SYS_getrandom, buffer, bytes_needed, 0)
}

View File

@ -0,0 +1,26 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
#flag darwin -framework Security
// import const (
// kSecRandomDefault
// errSecSuccess
// )
const (
kSecRandomDefault = 0
errSecSuccess = 0
)
pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed)
status := C.SecRandomCopyBytes(kSecRandomDefault, bytes_needed, buffer)
if status != errSecSuccess {
return ReadError
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
}

View File

@ -0,0 +1,13 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.rand
fn test_crypto_rand() {
r := rand.read(100) or {
assert false
return
}
assert r.len == 100
}

View File

@ -0,0 +1,23 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
#flag windows -Llibraries/bcrypt -lbcrypt
#include <bcrypt.h>
const (
STATUS_SUCCESS = 0x00000000
BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002
)
pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(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)
if status != STATUS_SUCCESS {
return ReadError
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
}