From c1fcf745fee4bf667906410fda30eec5bdc11851 Mon Sep 17 00:00:00 2001 From: Lars Viklund Date: Mon, 16 Nov 2026 16:13:52 +1300 Subject: [PATCH] feat: expose platform generation of secret bytes Stock Lua libraries doesn't have any easy access to generation of random bytes suitable for moderately secure purposes like API authentication. This introduces `GenerateSecretBytes(count)` which leverages underlying operating system functionality to generate sufficiently good random bytes. On Windows it uses BCrypt and on other platforms it's stubbed to use `/dev/urandom`. It's intentionally named in a way that suggests that it's not a primary source of fast random numbers for other purposes. --- CMakeLists.txt | 1 + engine/system/sys_main.h | 2 ++ engine/system/win/sys_local.h | 1 + engine/system/win/sys_main.cpp | 26 +++++++++++++++++++++++++- ui_api.cpp | 19 +++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eab67767..f6f28796 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,6 +223,7 @@ if (WIN32) ) target_link_libraries(SimpleGraphic PRIVATE + "bcrypt.lib" "winmm.lib" ) endif () diff --git a/engine/system/sys_main.h b/engine/system/sys_main.h index e2ffb447..5db28ac2 100644 --- a/engine/system/sys_main.h +++ b/engine/system/sys_main.h @@ -6,6 +6,7 @@ #include #include +#include #include // ======= @@ -79,6 +80,7 @@ class sys_IMain { virtual bool SetWorkDir(std::filesystem::path const& newCwd = {}) = 0; virtual void SpawnProcess(std::filesystem::path cmdName, const char* argList) = 0; virtual std::optional OpenURL(const char* url) = 0; + virtual bool GenerateSecretBytes(std::span output) = 0; virtual void Error(const char* fmt, ...) = 0; virtual void Exit(const char* msg = NULL) = 0; virtual void Restart() = 0; diff --git a/engine/system/win/sys_local.h b/engine/system/win/sys_local.h index 86d4759a..0d486e1b 100644 --- a/engine/system/win/sys_local.h +++ b/engine/system/win/sys_local.h @@ -38,6 +38,7 @@ class sys_main_c: public sys_IMain { bool SetWorkDir(std::filesystem::path const& newCwd = {}); void SpawnProcess(std::filesystem::path cmdName, const char* argList); std::optional OpenURL(const char* url); // return value has failure reason + bool GenerateSecretBytes(std::span output); void Error(const char* fmt, ...); void Exit(const char* msg = NULL); void Restart(); diff --git a/engine/system/win/sys_main.cpp b/engine/system/win/sys_main.cpp index a93f3a89..4db8c4b0 100644 --- a/engine/system/win/sys_main.cpp +++ b/engine/system/win/sys_main.cpp @@ -13,6 +13,8 @@ #include "core.h" #ifdef _WIN32 +#include +#include #include #include #elif __linux__ @@ -30,6 +32,7 @@ #include #include +#include #include #include #include @@ -498,7 +501,7 @@ const char* PlatformOpenURL(const char* url) #endif } #else -const char* PlatformOpenURL(const char* url); +extern "C" const char* PlatformOpenURL(const char* url); #endif std::optional sys_main_c::OpenURL(const char* url) @@ -512,6 +515,27 @@ std::optional sys_main_c::OpenURL(const char* url) return {}; } +bool PlatformGenerateSecretBytes(char* output, size_t count) +{ +#ifdef _WIN32 + if (BCRYPT_SUCCESS(BCryptGenRandom(nullptr, (UCHAR*)output, (ULONG)count, BCRYPT_USE_SYSTEM_PREFERRED_RNG))) { + return true; + } + return false; +#else + std::ifstream is("/dev/urandom", std::ios::binary); + return !!is.read(output, count); +#endif +} + +bool sys_main_c::GenerateSecretBytes(std::span output) +{ + if (output.empty()) { + return true; + } + return PlatformGenerateSecretBytes((char*)output.data(), output.size()); +} + // ============================== // System Initialisation/Shutdown // ============================== diff --git a/ui_api.cpp b/ui_api.cpp index 3fb81882..37404d56 100644 --- a/ui_api.cpp +++ b/ui_api.cpp @@ -87,6 +87,7 @@ ** compressedGzip = Deflate(uncompressed, true) ** uncompressed = Inflate(compressed) ** uncompressedGzip = Inflate(compressedGzip) +** bytes = GenerateSecretBytes(count) ** msec = GetTime() ** path[, pathACP[, err]] = GetScriptPath() ** path[, pathACP[, err]] = GetRuntimePath() @@ -1646,6 +1647,23 @@ static int l_Inflate(lua_State* L) } } +static int l_GenerateSecretBytes(lua_State* L) +{ + ui_main_c* ui = GetUIPtr(L); + int n = lua_gettop(L); + ui->LAssert(L, n >= 1, "Usage: GenerateSecretBytes(count)"); + ui->LAssert(L, lua_isnumber(L, 1), "GenerateSecretBytes() argument 1: expected number, got %s", luaL_typename(L, 1)); + int isInteger{}; + lua_Number count = lua_tonumberx(L, 1, &isInteger); + ui->LAssert(L, isInteger == 1 && count >= 0, "GenerateSecretBytes() argument 1: expected a whole non-negative count"); + std::vector buf((size_t)count); + if (!ui->sys->GenerateSecretBytes(std::as_writable_bytes(std::span(buf)))) { + return 0; + } + lua_pushlstring(L, buf.data(), buf.size()); + return 1; +} + static int l_GetTime(lua_State* L) { ui_main_c* ui = GetUIPtr(L); @@ -2261,6 +2279,7 @@ int ui_main_c::InitAPI(lua_State* L) ADDFUNC(Paste); ADDFUNC(Deflate); ADDFUNC(Inflate); + ADDFUNC(GenerateSecretBytes); ADDFUNC(GetTime); ADDFUNC(GetScriptPath); ADDFUNC(GetRuntimePath);