Browse Source

core/crypto: Add a `HAS_RAND_BYTES` constant

Yawning Angel 1 year ago
parent
commit
a6eb64df6c

+ 3 - 6
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
 // the system entropy source.  This routine will block if the system entropy
 // source is not ready yet.  All system entropy source failures are treated
 // source is not ready yet.  All system entropy source failures are treated
 // as catastrophic, resulting in a panic.
 // 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) {
 rand_bytes :: proc (dst: []byte) {
 	// zero-fill the buffer first
 	// zero-fill the buffer first
 	mem.zero_explicit(raw_data(dst), len(dst))
 	mem.zero_explicit(raw_data(dst), len(dst))
 
 
 	_rand_bytes(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()
-}

+ 3 - 4
core/crypto/rand_bsd.odin

@@ -3,14 +3,13 @@ package crypto
 
 
 foreign import libc "system:c"
 foreign import libc "system:c"
 
 
+HAS_RAND_BYTES :: true
+
 foreign libc {
 foreign libc {
 	arc4random_buf :: proc(buf: [^]byte, nbytes: uint) ---
 	arc4random_buf :: proc(buf: [^]byte, nbytes: uint) ---
 }
 }
 
 
+@(private)
 _rand_bytes :: proc(dst: []byte) {
 _rand_bytes :: proc(dst: []byte) {
 	arc4random_buf(raw_data(dst), len(dst))
 	arc4random_buf(raw_data(dst), len(dst))
 }
 }
-
-_has_rand_bytes :: proc() -> bool {
-	return true
-}

+ 3 - 4
core/crypto/rand_darwin.odin

@@ -5,6 +5,9 @@ import "core:fmt"
 import CF "core:sys/darwin/CoreFoundation"
 import CF "core:sys/darwin/CoreFoundation"
 import Sec "core:sys/darwin/Security"
 import Sec "core:sys/darwin/Security"
 
 
+HAS_RAND_BYTES :: true
+
+@(private)
 _rand_bytes :: proc(dst: []byte) {
 _rand_bytes :: proc(dst: []byte) {
 	err := Sec.RandomCopyBytes(count=len(dst), bytes=raw_data(dst))
 	err := Sec.RandomCopyBytes(count=len(dst), bytes=raw_data(dst))
 	if err != .Success {
 	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))
         panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", err, msg))
 	}
 	}
 }
 }
-
-_has_rand_bytes :: proc() -> bool {
-	return true
-}

+ 3 - 4
core/crypto/rand_generic.odin

@@ -6,10 +6,9 @@
 //+build !js
 //+build !js
 package crypto
 package crypto
 
 
+HAS_RAND_BYTES :: false
+
+@(private)
 _rand_bytes :: proc(dst: []byte) {
 _rand_bytes :: proc(dst: []byte) {
 	unimplemented("crypto: rand_bytes not supported on this OS")
 	unimplemented("crypto: rand_bytes not supported on this OS")
 }
 }
-
-_has_rand_bytes :: proc() -> bool {
-	return false
-}

+ 4 - 4
core/crypto/rand_js.odin

@@ -6,8 +6,12 @@ foreign odin_env {
 	env_rand_bytes :: proc "contextless" (buf: []byte) ---
 	env_rand_bytes :: proc "contextless" (buf: []byte) ---
 }
 }
 
 
+HAS_RAND_BYTES :: true
+
+@(private)
 _MAX_PER_CALL_BYTES :: 65536 // 64kiB
 _MAX_PER_CALL_BYTES :: 65536 // 64kiB
 
 
+@(private)
 _rand_bytes :: proc(dst: []byte) {
 _rand_bytes :: proc(dst: []byte) {
 	dst := dst
 	dst := dst
 
 
@@ -18,7 +22,3 @@ _rand_bytes :: proc(dst: []byte) {
 		dst = dst[to_read:]
 		dst = dst[to_read:]
 	}
 	}
 }
 }
-
-_has_rand_bytes :: proc() -> bool {
-	return true
-}

+ 4 - 4
core/crypto/rand_linux.odin

@@ -4,8 +4,12 @@ import "core:fmt"
 
 
 import "core:sys/linux"
 import "core:sys/linux"
 
 
+HAS_RAND_BYTES :: true
+
+@(private)
 _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1
 _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1
 
 
+@(private)
 _rand_bytes :: proc (dst: []byte) {
 _rand_bytes :: proc (dst: []byte) {
 	dst := dst
 	dst := dst
 	l := len(dst)
 	l := len(dst)
@@ -34,7 +38,3 @@ _rand_bytes :: proc (dst: []byte) {
 		dst = dst[n_read:]
 		dst = dst[n_read:]
 	}
 	}
 }
 }
-
-_has_rand_bytes :: proc() -> bool {
-	return true
-}

+ 3 - 4
core/crypto/rand_windows.odin

@@ -4,6 +4,9 @@ import win32 "core:sys/windows"
 import "core:os"
 import "core:os"
 import "core:fmt"
 import "core:fmt"
 
 
+HAS_RAND_BYTES :: true
+
+@(private)
 _rand_bytes :: proc(dst: []byte) {
 _rand_bytes :: proc(dst: []byte) {
 	ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG))
 	ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG))
 	if ret != os.ERROR_NONE {
 	if ret != os.ERROR_NONE {
@@ -21,7 +24,3 @@ _rand_bytes :: proc(dst: []byte) {
 		}
 		}
 	}
 	}
 }
 }
-
-_has_rand_bytes :: proc() -> bool {
-	return true
-}

+ 1 - 1
tests/core/crypto/test_core_crypto.odin

@@ -277,7 +277,7 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
 test_rand_bytes :: proc(t: ^testing.T) {
 test_rand_bytes :: proc(t: ^testing.T) {
 	tc.log(t, "Testing rand_bytes")
 	tc.log(t, "Testing rand_bytes")
 
 
-	if !crypto.has_rand_bytes() {
+	if !crypto.HAS_RAND_BYTES {
 		tc.log(t, "rand_bytes not supported - skipping")
 		tc.log(t, "rand_bytes not supported - skipping")
 		return
 		return
 	}
 	}