mirror of
https://github.com/Silicondust/libhdhomerun
synced 2025-08-20 05:23:27 -07:00
Use RtlGenRandom when available to elminiate TLS and heap leaks
This commit is contained in:
parent
b0e5d5f5c8
commit
a68406e84c
2 changed files with 8 additions and 22 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue