Skip to content

windows init_random  #15

@bigo2050

Description

@bigo2050
#include <windows.h>
#include <bcrypt.h>
#include <ntstatus.h>
#include <wincred.h>

static void init_random()
{
unsigned char buff[64];

if (g_initialized)
    return;

#ifdef WIN32
{
BCRYPT_ALG_HANDLE hAlg = NULL;
void* hRng = NULL;
NTSTATUS status = 0;

    status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, NULL, 0);
    if (status != STATUS_SUCCESS) {
        goto cleanup;
    }

    status = BCryptGenRandom(hAlg, buff, sizeof(buff), 0);
    if (status != STATUS_SUCCESS) {
        goto cleanup;
    }

    g_initialized = 1;
    RAND_seed(buff, sizeof(buff));

cleanup:
    if (hRng) {
        BCryptCloseAlgorithmProvider((BCRYPT_ALG_HANDLE)hRng, 0);
    }
    if (hAlg) {
        BCryptCloseAlgorithmProvider(hAlg, 0);
    }
}

#else
{
FILE* fp = fopen("/dev/urandom", "rb");
if (fp) {
size_t read = fread(buff, sizeof(buff), 1, fp);
g_initialized = read == 1;
fclose(fp);
}
}

if (g_initialized)
    RAND_seed(buff, sizeof(buff));

#endif
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions