Use RtlGenRandom when available to elminiate TLS and heap leaks

This commit is contained in:
djp952 2019-07-08 11:43:27 -04:00
commit a68406e84c
2 changed files with 8 additions and 22 deletions

View file

@ -20,34 +20,16 @@
#include "hdhomerun.h"
#if defined(_WINRT)
uint32_t random_get32(void)
{
return (uint32_t)getcurrenttime();
}
#else
uint32_t random_get32(void)
{
static DWORD random_get32_context_tls = 0xFFFFFFFF;
if (random_get32_context_tls == 0xFFFFFFFF) {
random_get32_context_tls = TlsAlloc();
}
uint32_t Result = (uint32_t)getcurrenttime();
HCRYPTPROV *phProv = (HCRYPTPROV *)TlsGetValue(random_get32_context_tls);
if (!phProv) {
phProv = (HCRYPTPROV *)calloc(1, sizeof(HCRYPTPROV));
CryptAcquireContext(phProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
TlsSetValue(random_get32_context_tls, phProv);
}
uint32_t Result;
if (!CryptGenRandom(*phProv, sizeof(Result), (BYTE *)&Result)) {
return (uint32_t)getcurrenttime();
}
#if defined(RtlGenRandom)
RtlGenRandom(&Result, sizeof(Result));
#endif
return Result;
}
#endif
uint64_t getcurrenttime(void)
{

View file

@ -46,6 +46,10 @@
#include <time.h>
#include <sys/types.h>
#define SystemFunction036 NTAPI SystemFunction036
#include <ntsecapi.h>
#undef SystemFunction036
#ifdef LIBHDHOMERUN_DLLEXPORT
#define LIBHDHOMERUN_API __declspec(dllexport)
#endif