Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: added alternative to bcrypt lib #2933

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ typedef enum { false = 0, true = 1 } bool;
#endif
#include <wincrypt.h>
#pragma comment(lib, "advapi32.lib")
#elif defined(_CRT_RAND_S)
#else
#include <bcrypt.h>
#if defined(_MSC_VER)
Expand Down
1 change: 1 addition & 0 deletions src/arch_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef enum { false = 0, true = 1 } bool;
#endif
#include <wincrypt.h>
#pragma comment(lib, "advapi32.lib")
#elif defined(_CRT_RAND_S)
#else
#include <bcrypt.h>
#if defined(_MSC_VER)
Expand Down
11 changes: 11 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -171,11 +172,11 @@ vc98: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc98 wine [email protected]

vc17: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCRANDFLAG) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc17 wine64 [email protected]

vc22: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCRANDFLAG) $(VCFLAGS) /[email protected]
$(DOCKER) mdashnet/vc22 wine64 [email protected]

mingw: Makefile mongoose.h $(SRCS)
Expand Down
Loading