mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 15:01:03 +08:00
Merge pull request #2933 from cesanta/win_rng
Windows: added alternative to bcrypt lib
This commit is contained in:
commit
9aef821862
17
mongoose.c
17
mongoose.c
@ -16388,15 +16388,16 @@ bool mg_random(void *buf, size_t len) {
|
|||||||
success = CryptGenRandom(hProv, len, p);
|
success = CryptGenRandom(hProv, len, p);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// BCrypt is a "new generation" strong crypto API, so try it first
|
size_t i;
|
||||||
static BCRYPT_ALG_HANDLE hProv;
|
for (i = 0; i < len; i++) {
|
||||||
if (initialised == false &&
|
unsigned int rand_v;
|
||||||
BCryptOpenAlgorithmProvider(&hProv, BCRYPT_RNG_ALGORITHM, NULL, 0) == 0) {
|
if (rand_s(&rand_v) == 0) {
|
||||||
initialised = true;
|
p[i] = (unsigned char)(rand_v & 255);
|
||||||
}
|
} else {
|
||||||
if (initialised == true) {
|
break;
|
||||||
success = BCryptGenRandom(hProv, p, (ULONG) len, 0) == 0;
|
}
|
||||||
}
|
}
|
||||||
|
success = (i == len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#elif MG_ARCH == MG_ARCH_UNIX
|
#elif MG_ARCH == MG_ARCH_UNIX
|
||||||
|
@ -415,6 +415,10 @@ static inline int mg_mkdir(const char *path, mode_t mode) {
|
|||||||
|
|
||||||
#if MG_ARCH == MG_ARCH_WIN32
|
#if MG_ARCH == MG_ARCH_WIN32
|
||||||
|
|
||||||
|
#ifndef _CRT_RAND_S
|
||||||
|
#define _CRT_RAND_S
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#endif
|
#endif
|
||||||
@ -470,11 +474,6 @@ typedef enum { false = 0, true = 1 } bool;
|
|||||||
#endif
|
#endif
|
||||||
#include <wincrypt.h>
|
#include <wincrypt.h>
|
||||||
#pragma comment(lib, "advapi32.lib")
|
#pragma comment(lib, "advapi32.lib")
|
||||||
#else
|
|
||||||
#include <bcrypt.h>
|
|
||||||
#if defined(_MSC_VER)
|
|
||||||
#pragma comment(lib, "bcrypt.lib")
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Protect from calls like std::snprintf in app code
|
// Protect from calls like std::snprintf in app code
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
#if MG_ARCH == MG_ARCH_WIN32
|
#if MG_ARCH == MG_ARCH_WIN32
|
||||||
|
|
||||||
|
#ifndef _CRT_RAND_S
|
||||||
|
#define _CRT_RAND_S
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#endif
|
#endif
|
||||||
@ -57,11 +61,6 @@ typedef enum { false = 0, true = 1 } bool;
|
|||||||
#endif
|
#endif
|
||||||
#include <wincrypt.h>
|
#include <wincrypt.h>
|
||||||
#pragma comment(lib, "advapi32.lib")
|
#pragma comment(lib, "advapi32.lib")
|
||||||
#else
|
|
||||||
#include <bcrypt.h>
|
|
||||||
#if defined(_MSC_VER)
|
|
||||||
#pragma comment(lib, "bcrypt.lib")
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Protect from calls like std::snprintf in app code
|
// Protect from calls like std::snprintf in app code
|
||||||
|
17
src/util.c
17
src/util.c
@ -31,15 +31,16 @@ bool mg_random(void *buf, size_t len) {
|
|||||||
success = CryptGenRandom(hProv, len, p);
|
success = CryptGenRandom(hProv, len, p);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// BCrypt is a "new generation" strong crypto API, so try it first
|
size_t i;
|
||||||
static BCRYPT_ALG_HANDLE hProv;
|
for (i = 0; i < len; i++) {
|
||||||
if (initialised == false &&
|
unsigned int rand_v;
|
||||||
BCryptOpenAlgorithmProvider(&hProv, BCRYPT_RNG_ALGORITHM, NULL, 0) == 0) {
|
if (rand_s(&rand_v) == 0) {
|
||||||
initialised = true;
|
p[i] = (unsigned char)(rand_v & 255);
|
||||||
}
|
} else {
|
||||||
if (initialised == true) {
|
break;
|
||||||
success = BCryptGenRandom(hProv, p, (ULONG) len, 0) == 0;
|
}
|
||||||
}
|
}
|
||||||
|
success = (i == len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#elif MG_ARCH == MG_ARCH_UNIX
|
#elif MG_ARCH == MG_ARCH_UNIX
|
||||||
|
Loading…
x
Reference in New Issue
Block a user