* API for time remains the same, but reduced dependencies on external time things,
like strftime, localtime and mktime.
* Ergnomic improvements to `onyx pkg`.
+* Not relying on time for random number seeding, if cryptographic random number generation is possible.
+ - Using `getrandom` on Linux, and `BcryptGenRandom` on Windows.
+ - Using `random_get` on WASI.
Bugfixes:
* Fixed missing `use core` in `optional.onyx`.
package core.random
use core
+use runtime
#doc "The state of a random number generator."
Random :: struct {
RANDOM_MULTIPLIER :: 25214903917
RANDOM_INCREMENT :: cast(i64) 11
- #if #defined(core.os.time) {
- __initial_value :: #(core.os.time())
+ #if #defined(runtime.platform.__random_get) {
+ __initial_value :: #(do {
+ v: u64;
+ runtime.platform.__random_get(.{ ~~&v, sizeof typeof v });
+ return v;
+ })
} else {
- __initial_value :: #(8675309)
+ #if #defined(core.os.time) {
+ __initial_value :: #(core.os.time())
+ } else {
+ __initial_value :: #(8675309)
+ }
}
}
// Misc
__file_get_standard :: (fd: i32, out: &FileData) -> bool ---
+ __random_get :: (buf: [] u8) -> void ---
}
__start :: () {
poll_oneoff, fd_write, fd_datasync, fd_read,
args_get, args_sizes_get,
proc_exit,
- clock_time_get, Timestamp
+ clock_time_get, Timestamp,
+ random_get,
}
use runtime {
__runtime_initialize,
return time / 1000000;
}
+__random_get :: (buf: [] u8) {
+ random_get(buf.data, buf.length);
+}
+
// Sets up everything needed for execution.
__start :: () {
ONYX_FUNC(__sleep)
ONYX_FUNC(__time)
ONYX_FUNC(__lookup_env)
+ ONYX_FUNC(__random_get)
ONYX_FUNC(__register_cleanup)
ONYX_FUNC(__net_create_socket)
}
+ONYX_DEF(__random_get, (WASM_PTR, WASM_I32), ()) {
+ #ifdef _BH_LINUX
+ getrandom(ONYX_PTR(params->data[0].of.i32), params->data[1].of.i32, 0);
+ #endif
+
+ #ifdef _BH_WINDOWS
+ BCRYPT_ALG_HANDLE alg;
+ BCryptOpenAlgorithmProvider(&alg, L"SHA256", NULL, 0);
+ BCryptGenRandom(alg, ONYX_PTR(params->data[0].of.i32), params->data[1].of.i32, 0);
+ BCryptCloseAlgorithmProvider(alg, 0);
+ #endif
+
+ return NULL;
+}
} \
struct WasmFuncDefinition *ONYX_MODULE_NAME_GEN(ONYX_LIBRARY_NAME)[] =
+#define WASM_PTR WASM_I32
+
// Shorter names
#ifndef ONYX_NO_SHORT_NAMES
#undef BOOL
#define LONG WASM_I64
#define FLOAT WASM_F32
#define DOUBLE WASM_F64
-#define PTR WASM_I32
+#define PTR WASM_PTR
#endif
#define ONYX_PTR(p) ((void*) (p != 0 ? (runtime->wasm_memory_data(runtime->wasm_memory) + p) : NULL))
typedef HANDLE HLOCAL;
typedef HANDLE GLOBALHANDLE;
typedef HANDLE LOCALHANDLE;
+typedef HANDLE BCRYPT_ALG_HANDLE;
typedef void *HGDIOBJ;
#define DECLARE_HANDLE(name) typedef HANDLE name
GB_DLL_IMPORT int WINAPI htonl(int hostint);
GB_DLL_IMPORT short WINAPI ntohs(short netshort);
GB_DLL_IMPORT int WINAPI ntohl(int netint);
+
+
+GB_DLL_IMPORT NTSTATUS BCryptGenRandom(
+ BCRYPT_ALG_HANDLE hAlgorithm,
+ unsigned char * pbBuffer,
+ unsigned long cbBuffer,
+ unsigned long wFlags
+);
+
+GB_DLL_IMPORT NTSTATUS BCryptOpenAlgorithmProvider(
+ BCRYPT_ALG_HANDLE *phAlgorithm,
+ const wchar_t * pszAlgId,
+ const wchar_t * pszImplementation,
+ unsigned long dwFlags
+);
+
+GB_DLL_IMPORT NTSTATUS BCryptCloseAlgorithmProvider(
+ BCRYPT_ALG_HANDLE hAlgorithm,
+ unsigned long dwFlags
+);