From a6eb64df6cd136639d1234e5a157ad280a1a32a8 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Sun, 21 Apr 2024 21:06:21 +0900 Subject: [PATCH] core/crypto: Add a `HAS_RAND_BYTES` constant --- core/crypto/crypto.odin | 9 +++------ core/crypto/rand_bsd.odin | 7 +++---- core/crypto/rand_darwin.odin | 7 +++---- core/crypto/rand_generic.odin | 7 +++---- core/crypto/rand_js.odin | 8 ++++---- core/crypto/rand_linux.odin | 8 ++++---- core/crypto/rand_windows.odin | 7 +++---- tests/core/crypto/test_core_crypto.odin | 2 +- 8 files changed, 24 insertions(+), 31 deletions(-) diff --git a/core/crypto/crypto.odin b/core/crypto/crypto.odin index 05f25111a..f0874cc6d 100644 --- a/core/crypto/crypto.odin +++ b/core/crypto/crypto.odin @@ -49,15 +49,12 @@ compare_byte_ptrs_constant_time :: proc "contextless" (a, b: ^byte, n: int) -> i // the system entropy source. This routine will block if the system entropy // source is not ready yet. All system entropy source failures are treated // as catastrophic, resulting in a panic. +// +// Support for the system entropy source can be checked with the +// `HAS_RAND_BYTES` boolean constant. rand_bytes :: proc (dst: []byte) { // zero-fill the buffer first mem.zero_explicit(raw_data(dst), len(dst)) _rand_bytes(dst) } - -// has_rand_bytes returns true iff the target has support for accessing the -// system entropty source. -has_rand_bytes :: proc () -> bool { - return _has_rand_bytes() -} diff --git a/core/crypto/rand_bsd.odin b/core/crypto/rand_bsd.odin index 7a0c42683..a31e4f2b2 100644 --- a/core/crypto/rand_bsd.odin +++ b/core/crypto/rand_bsd.odin @@ -3,14 +3,13 @@ package crypto foreign import libc "system:c" +HAS_RAND_BYTES :: true + foreign libc { arc4random_buf :: proc(buf: [^]byte, nbytes: uint) --- } +@(private) _rand_bytes :: proc(dst: []byte) { arc4random_buf(raw_data(dst), len(dst)) } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_darwin.odin b/core/crypto/rand_darwin.odin index c1a3d1dbc..5355f31c5 100644 --- a/core/crypto/rand_darwin.odin +++ b/core/crypto/rand_darwin.odin @@ -5,6 +5,9 @@ import "core:fmt" import CF "core:sys/darwin/CoreFoundation" import Sec "core:sys/darwin/Security" +HAS_RAND_BYTES :: true + +@(private) _rand_bytes :: proc(dst: []byte) { err := Sec.RandomCopyBytes(count=len(dst), bytes=raw_data(dst)) if err != .Success { @@ -12,7 +15,3 @@ _rand_bytes :: proc(dst: []byte) { panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", err, msg)) } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_generic.odin b/core/crypto/rand_generic.odin index cba49f700..4ea61ec91 100644 --- a/core/crypto/rand_generic.odin +++ b/core/crypto/rand_generic.odin @@ -6,10 +6,9 @@ //+build !js package crypto +HAS_RAND_BYTES :: false + +@(private) _rand_bytes :: proc(dst: []byte) { unimplemented("crypto: rand_bytes not supported on this OS") } - -_has_rand_bytes :: proc() -> bool { - return false -} diff --git a/core/crypto/rand_js.odin b/core/crypto/rand_js.odin index 90f60b99b..72093810e 100644 --- a/core/crypto/rand_js.odin +++ b/core/crypto/rand_js.odin @@ -6,8 +6,12 @@ foreign odin_env { env_rand_bytes :: proc "contextless" (buf: []byte) --- } +HAS_RAND_BYTES :: true + +@(private) _MAX_PER_CALL_BYTES :: 65536 // 64kiB +@(private) _rand_bytes :: proc(dst: []byte) { dst := dst @@ -18,7 +22,3 @@ _rand_bytes :: proc(dst: []byte) { dst = dst[to_read:] } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_linux.odin b/core/crypto/rand_linux.odin index a9dc37415..43b3b3075 100644 --- a/core/crypto/rand_linux.odin +++ b/core/crypto/rand_linux.odin @@ -4,8 +4,12 @@ import "core:fmt" import "core:sys/linux" +HAS_RAND_BYTES :: true + +@(private) _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1 +@(private) _rand_bytes :: proc (dst: []byte) { dst := dst l := len(dst) @@ -34,7 +38,3 @@ _rand_bytes :: proc (dst: []byte) { dst = dst[n_read:] } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_windows.odin b/core/crypto/rand_windows.odin index 5cafe7fb5..a92d376cb 100644 --- a/core/crypto/rand_windows.odin +++ b/core/crypto/rand_windows.odin @@ -4,6 +4,9 @@ import win32 "core:sys/windows" import "core:os" import "core:fmt" +HAS_RAND_BYTES :: true + +@(private) _rand_bytes :: proc(dst: []byte) { ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG)) if ret != os.ERROR_NONE { @@ -21,7 +24,3 @@ _rand_bytes :: proc(dst: []byte) { } } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/tests/core/crypto/test_core_crypto.odin b/tests/core/crypto/test_core_crypto.odin index 72d8e7c78..95db3f292 100644 --- a/tests/core/crypto/test_core_crypto.odin +++ b/tests/core/crypto/test_core_crypto.odin @@ -277,7 +277,7 @@ test_chacha20poly1305 :: proc(t: ^testing.T) { test_rand_bytes :: proc(t: ^testing.T) { tc.log(t, "Testing rand_bytes") - if !crypto.has_rand_bytes() { + if !crypto.HAS_RAND_BYTES { tc.log(t, "rand_bytes not supported - skipping") return }