Skip to content

Commit 4ca4200

Browse files
committed
Loop fetching randomness on Windows
1 parent 17fdca1 commit 4ca4200

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

randombytes.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,24 @@
7676

7777

7878
#if defined(_WIN32)
79-
static int randombytes_win32_randombytes(void* buf, const size_t n)
79+
static int randombytes_win32_randombytes(void* buf, size_t n)
8080
{
8181
HCRYPTPROV ctx;
8282
BOOL tmp;
83+
DWORD to_read = 0;
84+
const size_t MAX_DWORD = 0xFFFFFFFF;
8385

8486
tmp = CryptAcquireContext(&ctx, NULL, NULL, PROV_RSA_FULL,
8587
CRYPT_VERIFYCONTEXT);
8688
if (tmp == FALSE) return -1;
8789

88-
tmp = CryptGenRandom(ctx, n, (BYTE*) buf);
89-
if (tmp == FALSE) return -1;
90+
while (n > 0) {
91+
to_read = (DWORD)(n < MAX_DWORD ? n : MAX_DWORD);
92+
tmp = CryptGenRandom(ctx, to_read, (BYTE*) buf);
93+
if (tmp == FALSE) return -1;
94+
buf = ((char*)buf) + to_read;
95+
n -= to_read;
96+
}
9097

9198
tmp = CryptReleaseContext(ctx, 0);
9299
if (tmp == FALSE) return -1;

0 commit comments

Comments
 (0)