From 5ec26a501583a90a45f4a2bf20d71219c642374e Mon Sep 17 00:00:00 2001 From: robert Date: Tue, 8 Oct 2024 09:21:13 -0400 Subject: [PATCH] Windows: added alternative to bcrypt lib --- mongoose.c | 11 +++++++++++ mongoose.h | 1 + src/arch_win32.h | 1 + src/util.c | 11 +++++++++++ test/Makefile | 5 +++-- 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mongoose.c b/mongoose.c index 000cbc58..121b407b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -16385,6 +16385,17 @@ bool mg_random(void *buf, size_t len) { if (initialised == true) { success = CryptGenRandom(hProv, len, p); } +#elif defined(_CRT_RAND_S) + size_t i; + for (i = 0; i < len; i++) { + unsigned int rand_v; + if (rand_s(&rand_v) == 0) { + p[i] = (unsigned char)(rand_v & 255); + } else { + break; + } + } + success = (i == len); #else // BCrypt is a "new generation" strong crypto API, so try it first static BCRYPT_ALG_HANDLE hProv; diff --git a/mongoose.h b/mongoose.h index de0982e4..35678140 100644 --- a/mongoose.h +++ b/mongoose.h @@ -470,6 +470,7 @@ typedef enum { false = 0, true = 1 } bool; #endif #include #pragma comment(lib, "advapi32.lib") +#elif defined(_CRT_RAND_S) #else #include #if defined(_MSC_VER) diff --git a/src/arch_win32.h b/src/arch_win32.h index c31310f3..a89dd6a8 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -57,6 +57,7 @@ typedef enum { false = 0, true = 1 } bool; #endif #include #pragma comment(lib, "advapi32.lib") +#elif defined(_CRT_RAND_S) #else #include #if defined(_MSC_VER) diff --git a/src/util.c b/src/util.c index 345fa0f5..71fd5d04 100644 --- a/src/util.c +++ b/src/util.c @@ -30,6 +30,17 @@ bool mg_random(void *buf, size_t len) { if (initialised == true) { success = CryptGenRandom(hProv, len, p); } +#elif defined(_CRT_RAND_S) + size_t i; + for (i = 0; i < len; i++) { + unsigned int rand_v; + if (rand_s(&rand_v) == 0) { + p[i] = (unsigned char)(rand_v & 255); + } else { + break; + } + } + success = (i == len); #else // BCrypt is a "new generation" strong crypto API, so try it first static BCRYPT_ALG_HANDLE hProv; diff --git a/test/Makefile b/test/Makefile index def1a243..44ed69c0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -11,6 +11,7 @@ ENV ?= -e Tmp=. -e WINEDEBUG=-all DOCKER_BIN ?= docker DOCKER ?= $(DOCKER_BIN) run --platform linux/amd64 --rm $(ENV) -v $(ROOT_DIR):$(ROOT_DIR) -w $(CWD) VCFLAGS = /nologo /W3 /O2 /MD /I. $(DEFS) $(TFLAGS) +VCRANDFLAG = /D_CRT_RAND_S IPV6 ?= 1 ASAN ?= -fsanitize=address,undefined,alignment -fno-sanitize-recover=all -fno-omit-frame-pointer -fno-common ASAN_OPTIONS ?= detect_leaks=1 @@ -171,11 +172,11 @@ vc98: Makefile mongoose.h $(SRCS) $(DOCKER) mdashnet/vc98 wine $@.exe vc17: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) /Fe$@.exe + $(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCRANDFLAG) $(VCFLAGS) /Fe$@.exe $(DOCKER) mdashnet/vc17 wine64 $@.exe vc22: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) /Fe$@.exe + $(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCRANDFLAG) $(VCFLAGS) /Fe$@.exe $(DOCKER) mdashnet/vc22 wine64 $@.exe mingw: Makefile mongoose.h $(SRCS)