Uses the OS random support instead of rand().

This commit is contained in:
Andrea Mazzoleni 2014-12-27 11:56:55 +01:00
parent 95e4ed03bf
commit 8c98864395
9 changed files with 90 additions and 34 deletions

View file

@ -109,6 +109,12 @@ static BOOL (WINAPI* ptr_GetFileInformationByHandleEx)(HANDLE, DWORD, LPVOID, DW
*/
static BOOLEAN (WINAPI* ptr_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
/**
* Direct access to RtlGenRandom().
* This function is accessible only with LoadLibrary() and it's availble from Windows XP.
*/
static BOOLEAN (WINAPI* ptr_RtlGenRandom)(PVOID, ULONG);
/**
* Description of the last error.
*/
@ -124,29 +130,49 @@ static int is_wine;
*/
static int is_scan_winfind;
/**
* Loaded ADVAPI32.DLL.
*/
static HMODULE advapi32;
void os_init(int opt)
{
HMODULE kernel32, ntdll;
HMODULE ntdll, kernel32;
is_scan_winfind = opt != 0;
kernel32 = GetModuleHandle("KERNEL32.DLL");
if (!kernel32) {
fprintf(stderr, "Error loading the KERNEL32 module.\n");
exit(EXIT_FAILURE);
}
ntdll = GetModuleHandle("NTDLL.DLL");
if (!ntdll) {
fprintf(stderr, "Error loading the NTDLL module.\n");
exit(EXIT_FAILURE);
}
kernel32 = GetModuleHandle("KERNEL32.DLL");
if (!kernel32) {
fprintf(stderr, "Error loading the KERNEL32 module.\n");
exit(EXIT_FAILURE);
}
advapi32 = LoadLibrary("ADVAPI32.DLL");
if (!advapi32) {
fprintf(stderr, "Error loading the ADVAPI32 module.\n");
exit(EXIT_FAILURE);
}
/* check for Wine presence */
is_wine = GetProcAddress(ntdll, "wine_get_version") != 0;
/* load functions not always available */
ptr_SetThreadExecutionState = (void*)GetProcAddress(kernel32, "SetThreadExecutionState");
ptr_GetFileInformationByHandleEx = (void*)GetProcAddress(kernel32, "GetFileInformationByHandleEx");
ptr_CreateSymbolicLinkW = (void*)GetProcAddress(kernel32, "CreateSymbolicLinkW");
ptr_RtlGenRandom = (void*)GetProcAddress(advapi32, "SystemFunction036");
if (!ptr_RtlGenRandom) {
fprintf(stderr, "Error loading RtlGenRandom() from the ADVAPI32 module.\n");
exit(EXIT_FAILURE);
}
/* set the thread execution level to avoid sleep */
if (ptr_SetThreadExecutionState) {
/* first try for Windows 7 */
@ -156,8 +182,6 @@ void os_init(int opt)
}
}
is_wine = GetProcAddress(ntdll, "wine_get_version") != 0;
last_error = 0;
}
@ -169,6 +193,8 @@ void os_done(void)
if (ptr_SetThreadExecutionState) {
ptr_SetThreadExecutionState(WIN32_ES_CONTINUOUS);
}
FreeLibrary(advapi32);
}
/**
@ -1718,5 +1744,15 @@ uint64_t tick(void)
return t.QuadPart;
}
int windows_randomize(void* ptr, size_t size)
{
if (!ptr_RtlGenRandom(ptr, size)) {
windows_errno(GetLastError());
return -1;
}
return 0;
}
#endif

View file

@ -115,6 +115,8 @@
#define pread windows_pread
#undef pwrite
#define pwrite windows_pwrite
#undef randomize
#define randomize windows_randomize
/**
* If nanoseconds are not supported, we report the special STAT_NSEC_INVALID value,
@ -352,6 +354,11 @@ ssize_t windows_pread(int f, void* buffer, size_t size, off_t offset);
*/
ssize_t windows_pwrite(int f, const void* buffer, size_t size, off_t offset);
/**
* Like randomize().
*/
int windows_randomize(void* ptr, size_t size);
#endif
#endif

View file

@ -56,7 +56,12 @@ void state_rehash(struct snapraid_state* state)
/* set the new hash and seed */
state->hash = state->besthash;
randomize(state->hashseed, HASH_SIZE);
if (randomize(state->hashseed, HASH_SIZE) != 0) {
/* LCOV_EXCL_START */
fprintf(stderr, "Failed to get random values.\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* mark all the block for rehashing */
for (i = 0; i < blockmax; ++i) {

View file

@ -412,9 +412,6 @@ int main(int argc, char* argv[])
struct tm* tm;
int i;
/* always different random numbers */
srand(time(0));
/* defaults */
config(conf, sizeof(conf), argv[0]);
memset(&opt, 0, sizeof(opt));

View file

@ -896,7 +896,12 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
state->hash = state->besthash;
/* by default use a random hash seed */
randomize(state->hashseed, HASH_SIZE);
if (randomize(state->hashseed, HASH_SIZE) != 0) {
/* LCOV_EXCL_START */
fprintf(stderr, "Failed to get random values.\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* no previous hash by default */
state->prevhash = HASH_UNDEFINED;

View file

@ -300,6 +300,27 @@ uint64_t tick(void)
#endif
}
int randomize(void* ptr, size_t size)
{
int f;
ssize_t ret;
f = open("/dev/urandom", O_RDONLY);
if (f == -1)
return -1;
ret = read(f, ptr, size);
if (ret < 0 || (size_t)ret != size) {
close(f);
return -1;
}
if (close(f) != 0)
return -1;
return 0;
}
void os_init(int opt)
{
(void)opt;

View file

@ -53,5 +53,10 @@ int dirent_hidden(struct dirent* dd);
*/
const char* stat_desc(struct stat* st);
/**
* Fill memory with psudeo-random values.
*/
int randomize(void* ptr, size_t size);
#endif

View file

@ -1685,18 +1685,6 @@ const char* hash_config_name(unsigned kind)
}
}
/****************************************************************************/
/* random */
void randomize(void* void_ptr, unsigned size)
{
unsigned char* ptr = void_ptr;
unsigned i;
for (i = 0; i < size; ++i)
ptr[i] = rand();
}
/****************************************************************************/
/* lock */

View file

@ -526,14 +526,6 @@ void memhash(unsigned kind, const unsigned char* seed, void* digest, const void*
*/
const char* hash_config_name(unsigned kind);
/****************************************************************************/
/* random */
/**
* Write random values.
*/
void randomize(void* ptr, unsigned size);
/****************************************************************************/
/* crc */