Skip to content

Commit

Permalink
Port WinCSP usage from libtommath
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
  • Loading branch information
sjaeckel committed Aug 7, 2023
1 parent 5fa7837 commit e591a35
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/prngs/rng_get_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ static unsigned long s_rng_ansic(unsigned char *buf, unsigned long len,
/* Try the Microsoft CSP */
#if defined(_WIN32) || defined(_WIN32_WCE)
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#define _WIN32_WINNT 0x0501
#endif
#ifdef _WIN32_WCE
#define UNDER_CE
#define ARM
#ifndef WINVER
#define WINVER 0x0501
#endif

#define WIN32_LEAN_AND_MEAN
Expand All @@ -97,23 +96,22 @@ static unsigned long s_rng_ansic(unsigned char *buf, unsigned long len,
static unsigned long s_rng_win32(unsigned char *buf, unsigned long len,
void (*callback)(void))
{
HCRYPTPROV hProv = 0;
LTC_UNUSED_PARAM(callback);
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
!CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET))
return 0;

if (CryptGenRandom(hProv, len, buf) == TRUE) {
CryptReleaseContext(hProv, 0);
return len;
} else {
CryptReleaseContext(hProv, 0);
return 0;

static HCRYPTPROV hProv = 0;
if (hProv == 0) {
HCRYPTPROV h = 0;
if (!CryptAcquireContextW(&h, NULL, MS_DEF_PROV_W, PROV_RSA_FULL,
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
!CryptAcquireContextW(&h, NULL, MS_DEF_PROV_W, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
return 0;
}
hProv = h;
}
}

return CryptGenRandom(hProv, (DWORD)len, (BYTE *)buf) == TRUE ? len : 0;
}
#endif /* WIN32 */

/**
Expand Down

0 comments on commit e591a35

Please # to comment.