Skip to content

Commit

Permalink
Merge pull request #2933 from cesanta/win_rng
Browse files Browse the repository at this point in the history
Windows: added alternative to bcrypt lib
  • Loading branch information
cpq authored Oct 29, 2024
2 parents 7d33699 + f394df8 commit 9aef821
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
17 changes: 9 additions & 8 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -16388,15 +16388,16 @@ bool mg_random(void *buf, size_t len) {
success = CryptGenRandom(hProv, len, p);
}
#else
// BCrypt is a "new generation" strong crypto API, so try it first
static BCRYPT_ALG_HANDLE hProv;
if (initialised == false &&
BCryptOpenAlgorithmProvider(&hProv, BCRYPT_RNG_ALGORITHM, NULL, 0) == 0) {
initialised = true;
}
if (initialised == true) {
success = BCryptGenRandom(hProv, p, (ULONG) len, 0) == 0;
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);
#endif

#elif MG_ARCH == MG_ARCH_UNIX
Expand Down
9 changes: 4 additions & 5 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ static inline int mg_mkdir(const char *path, mode_t mode) {

#if MG_ARCH == MG_ARCH_WIN32

#ifndef _CRT_RAND_S
#define _CRT_RAND_S
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
Expand Down Expand Up @@ -470,11 +474,6 @@ typedef enum { false = 0, true = 1 } bool;
#endif
#include <wincrypt.h>
#pragma comment(lib, "advapi32.lib")
#else
#include <bcrypt.h>
#if defined(_MSC_VER)
#pragma comment(lib, "bcrypt.lib")
#endif
#endif

// Protect from calls like std::snprintf in app code
Expand Down
9 changes: 4 additions & 5 deletions src/arch_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#if MG_ARCH == MG_ARCH_WIN32

#ifndef _CRT_RAND_S
#define _CRT_RAND_S
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
Expand Down Expand Up @@ -57,11 +61,6 @@ typedef enum { false = 0, true = 1 } bool;
#endif
#include <wincrypt.h>
#pragma comment(lib, "advapi32.lib")
#else
#include <bcrypt.h>
#if defined(_MSC_VER)
#pragma comment(lib, "bcrypt.lib")
#endif
#endif

// Protect from calls like std::snprintf in app code
Expand Down
17 changes: 9 additions & 8 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ bool mg_random(void *buf, size_t len) {
success = CryptGenRandom(hProv, len, p);
}
#else
// BCrypt is a "new generation" strong crypto API, so try it first
static BCRYPT_ALG_HANDLE hProv;
if (initialised == false &&
BCryptOpenAlgorithmProvider(&hProv, BCRYPT_RNG_ALGORITHM, NULL, 0) == 0) {
initialised = true;
}
if (initialised == true) {
success = BCryptGenRandom(hProv, p, (ULONG) len, 0) == 0;
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);
#endif

#elif MG_ARCH == MG_ARCH_UNIX
Expand Down

0 comments on commit 9aef821

Please sign in to comment.