From 0e1fba3d1e4f45ae93fdd19d81da65c9cc7bff8d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 17 Dec 2024 21:01:20 +1100 Subject: [PATCH] And for Windows too --- src/Platform_Windows.c | 17 ++++++++- third_party/bearssl/src/config.h | 5 +++ third_party/bearssl/src/ssl_engine.c | 56 ---------------------------- 3 files changed, 21 insertions(+), 57 deletions(-) diff --git a/src/Platform_Windows.c b/src/Platform_Windows.c index 160e491c3..4e995a581 100644 --- a/src/Platform_Windows.c +++ b/src/Platform_Windows.c @@ -1081,8 +1081,23 @@ cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { return 0; } +static BOOL (WINAPI *_RtlGenRandom)(PVOID data, ULONG len); + cc_result Platform_GetEntropy(void* data, int len) { - return ERR_NOT_SUPPORTED; + static const struct DynamicLibSym funcs[] = { + DynamicLib_Sym2("SystemFunction036", RtlGenRandom) + }; + + if (!_RtlGenRandom) { + static const cc_string kernel32 = String_FromConst("ADVAPI32.DLL"); + void* lib; + + DynamicLib_LoadAll(&kernel32, funcs, Array_Elems(funcs), &lib); + if (!_RtlGenRandom) return ERR_NOT_SUPPORTED; + } + + if (!_RtlGenRandom(data, len)) return GetLastError(); + return 0; } diff --git a/third_party/bearssl/src/config.h b/third_party/bearssl/src/config.h index edf5d254b..d5d27705d 100644 --- a/third_party/bearssl/src/config.h +++ b/third_party/bearssl/src/config.h @@ -42,6 +42,11 @@ #undef _WIN32 #endif +/* Rely on ClassiCube's implementation for RNG */ +#define BR_USE_WIN32_RAND 0 +#define BR_USE_URANDOM 0 +#define BR_USE_GETENTROPY 0 + /* * When BR_64 is enabled, 64-bit integer types are assumed to be * efficient (i.e. the architecture has 64-bit registers and can diff --git a/third_party/bearssl/src/ssl_engine.c b/third_party/bearssl/src/ssl_engine.c index f59fe1a0f..b36a46d94 100644 --- a/third_party/bearssl/src/ssl_engine.c +++ b/third_party/bearssl/src/ssl_engine.c @@ -24,62 +24,6 @@ #include "inner.h" -#if 0 -/* obsolete */ - -/* - * If BR_USE_URANDOM is not defined, then try to autodetect its presence - * through compiler macros. - */ -#ifndef BR_USE_URANDOM - -/* - * Macro values documented on: - * https://sourceforge.net/p/predef/wiki/OperatingSystems/ - * - * Only the most common systems have been included here for now. This - * should be enriched later on. - */ -#if defined _AIX \ - || defined __ANDROID__ \ - || defined __FreeBSD__ \ - || defined __NetBSD__ \ - || defined __OpenBSD__ \ - || defined __DragonFly__ \ - || defined __linux__ \ - || (defined __sun && (defined __SVR4 || defined __svr4__)) \ - || (defined __APPLE__ && defined __MACH__) -#define BR_USE_URANDOM 1 -#endif - -#endif - -/* - * If BR_USE_WIN32_RAND is not defined, perform autodetection here. - */ -#ifndef BR_USE_WIN32_RAND - -#if defined _WIN32 || defined _WIN64 -#define BR_USE_WIN32_RAND 1 -#endif - -#endif - -#if BR_USE_URANDOM -#include -#include -#include -#include -#endif - -#if BR_USE_WIN32_RAND -#include -#include -#pragma comment(lib, "advapi32") -#endif - -#endif - /* ==================================================================== */ /* * This part of the file does the low-level record management.