From 43de4ac4b59aa254d28aa3453cbed194b2dcdd9a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 12 Mar 2019 10:47:28 -0700 Subject: [PATCH] uuid: make `read_random` work on Windows Windows does not have a `/dev/urandom` source. Provide a wrapper around the `BCryptGetRandom` interface to provide the equivalent functionality. --- uuid/uuid.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/uuid/uuid.c b/uuid/uuid.c index f5cc7f2160..004af3de6a 100644 --- a/uuid/uuid.c +++ b/uuid/uuid.c @@ -39,6 +39,9 @@ #include #elif defined(_WIN32) #include +#define WIN32_LEAN_AND_MEAN +#include +#include #endif #include @@ -68,9 +71,6 @@ static inline void nanotime(struct timespec *tv) { #elif TARGET_OS_WINDOWS #include -#define WIN32_LEAN_AND_MEAN -#include - static inline void nanotime(struct timespec *tv) { FILETIME ftTime; @@ -83,11 +83,18 @@ static inline void nanotime(struct timespec *tv) { } #endif +#if TARGET_OS_WINDOWS +static inline void read_random(void *buffer, unsigned numBytes) { + BCryptGenRandom(NULL, buffer, numBytes, + BCRYPT_RNG_USE_ENTROPY_IN_BUFFER | BCRYPT_USE_SYSTEM_PREFERRED_RNG); +} +#else static inline void read_random(void *buffer, unsigned numBytes) { int fd = open("/dev/urandom", O_RDONLY); read(fd, buffer, numBytes); close(fd); } +#endif UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);