diff options
| author | flysand7 <yyakut.ac@gmail.com> | 2023-10-18 01:57:26 +1100 |
|---|---|---|
| committer | flysand7 <yyakut.ac@gmail.com> | 2023-10-27 10:51:21 +1100 |
| commit | 4d65b1ab9cb86bcbbfb0e5b26e3552f6f3582004 (patch) | |
| tree | b61fb2dbcfe8fbd8574cbda546c27ed91e49d44a /core/crypto | |
| parent | 8e4bdcfb9837d70e94634db02e79a06036a3dde7 (diff) | |
Implement new sys/unix package
Diffstat (limited to 'core/crypto')
| -rw-r--r-- | core/crypto/rand_linux.odin | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/core/crypto/rand_linux.odin b/core/crypto/rand_linux.odin index e5c194220..86fc425d6 100644 --- a/core/crypto/rand_linux.odin +++ b/core/crypto/rand_linux.odin @@ -1,8 +1,8 @@ package crypto import "core:fmt" -import "core:os" -import "core:sys/unix" + +import "core:sys/linux" _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1 @@ -12,26 +12,25 @@ _rand_bytes :: proc (dst: []byte) { for l > 0 { to_read := min(l, _MAX_PER_CALL_BYTES) - ret := unix.sys_getrandom(raw_data(dst), uint(to_read), 0) - if ret < 0 { - switch os.Errno(-ret) { - case os.EINTR: - // Call interupted by a signal handler, just retry the - // request. - continue - case os.ENOSYS: - // The kernel is apparently prehistoric (< 3.17 circa 2014) - // and does not support getrandom. - panic("crypto: getrandom not available in kernel") - case: - // All other failures are things that should NEVER happen - // unless the kernel interface changes (ie: the Linux - // developers break userland). - panic(fmt.tprintf("crypto: getrandom failed: %d", ret)) - } + n_read, errno := linux.getrandom(dst[:to_read], {}) + #partial switch errno { + case .NONE: + // Do nothing + case .EINTR: + // Call interupted by a signal handler, just retry the + // request. + continue + case .ENOSYS: + // The kernel is apparently prehistoric (< 3.17 circa 2014) + // and does not support getrandom. + panic("crypto: getrandom not available in kernel") + case: + // All other failures are things that should NEVER happen + // unless the kernel interface changes (ie: the Linux + // developers break userland). + panic(fmt.tprintf("crypto: getrandom failed: %v", errno)) } - - l -= ret - dst = dst[ret:] + l -= n_read + dst = dst[n_read:] } } |