Beispiel #1
0
/* Replace MS C rtl rand which is 15bit with 32 bit */
int replace_random() {
#if defined(_WIN64) || defined(_MSC_VER)
    unsigned int x=0;
    RtlGenRandom(&x, sizeof(UINT_MAX));
    return (int)(x >> 1);
#else
    unsigned int x=0;
    RtlGenRandom(&x, sizeof(UINT_MAX));
    return (int)(x >> 1);

    //return (int)(rand() * rand());
#endif
}
Beispiel #2
0
unsigned int I_MakeRNGSeed()
{
	unsigned int seed;

	// If RtlGenRandom is available, use that to avoid increasing the
	// working set by pulling in all of the crytographic API.
	HMODULE advapi = GetModuleHandle("advapi32.dll");
	if (advapi != NULL)
	{
		BOOLEAN (APIENTRY *RtlGenRandom)(void *, ULONG) =
			(BOOLEAN (APIENTRY *)(void *, ULONG))GetProcAddress(advapi, "SystemFunction036");
		if (RtlGenRandom != NULL)
		{
			if (RtlGenRandom(&seed, sizeof(seed)))
			{
				return seed;
			}
		}
	}

	// Use the full crytographic API to produce a seed. If that fails,
	// time() is used as a fallback.
	HCRYPTPROV prov;

	if (!CryptAcquireContext(&prov, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		return (unsigned int)time(NULL);
	}
	if (!CryptGenRandom(prov, sizeof(seed), (BYTE *)&seed))
	{
		seed = (unsigned int)time(NULL);
	}
	CryptReleaseContext(prov, 0);
	return seed;
}
Beispiel #3
0
static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
{
    int i;
    DWORD status = RPC_S_OK;

    ULONG buflen = sizeof(IP_ADAPTER_INFO);
    PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);

    if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
        HeapFree(GetProcessHeap(), 0, adapter);
        adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
    }

    if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
        for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
            address[i] = adapter->Address[i];
        }
    }
    /* We can't get a hardware address, just use random numbers.
       Set the multicast bit to prevent conflicts with real cards. */
    else {
        RtlGenRandom(address, ADDRESS_BYTES_NEEDED);
        address[0] |= 0x01;
        status = RPC_S_UUID_LOCAL_ONLY;
    }

    HeapFree(GetProcessHeap(), 0, adapter);
    return status;
}
Beispiel #4
0
NTSTATUS WINAPI BCryptGenRandom(BCRYPT_ALG_HANDLE algorithm, UCHAR *buffer, ULONG count, ULONG flags)
{
    const DWORD supported_flags = BCRYPT_USE_SYSTEM_PREFERRED_RNG;
    TRACE("%p, %p, %u, %08x - semi-stub\n", algorithm, buffer, count, flags);

    if (!algorithm)
    {
        /* It's valid to call without an algorithm if BCRYPT_USE_SYSTEM_PREFERRED_RNG
         * is set. In this case the preferred system RNG is used.
         */
        if (!(flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG))
            return STATUS_INVALID_HANDLE;
    }
    if (!buffer)
        return STATUS_INVALID_PARAMETER;

    if (flags & ~supported_flags)
        FIXME("unsupported flags %08x\n", flags & ~supported_flags);

    if (algorithm)
        FIXME("ignoring selected algorithm\n");

    /* When zero bytes are requested the function returns success too. */
    if (!count)
        return STATUS_SUCCESS;

    if (flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG)
    {
        if (RtlGenRandom(buffer, count))
            return STATUS_SUCCESS;
    }

    FIXME("called with unsupported parameters, returning error\n");
    return STATUS_NOT_IMPLEMENTED;
}
Beispiel #5
0
/*********************************************************************
 *		rand_s (MSVCRT.@)
 */
int CDECL MSVCRT_rand_s(unsigned int *pval)
{
    if (!pval || !RtlGenRandom(pval, sizeof(*pval)))
    {
        *MSVCRT__errno() = MSVCRT_EINVAL;
        return MSVCRT_EINVAL;
    }
    return 0;
}
int ioGatherEntropy(char *bufPtr, int bufSize) {
  if(!loaded) {
    loaded = 1;
    hAdvApi32 = LoadLibrary("advapi32.dll");
    RtlGenRandom = (void*)GetProcAddress(hAdvApi32, "SystemFunction036");
  }
  if(!RtlGenRandom) return 0;
  return RtlGenRandom(bufPtr, bufSize);
}
Beispiel #7
0
DEFINE_SYSCALL(getrandom, void *, buf, size_t, buflen, unsigned int, flags)
{
	log_info("getrandom(%p, %d, %x)", buf, buflen, flags);
	if (!mm_check_write(buf, buflen))
		return -L_EFAULT;
	if (!RtlGenRandom(buf, buflen))
		return 0;
	return buflen;
}
Beispiel #8
0
/* Replace MS C rtl rand which is 15bit with 32 bit */
int replace_random() {
    unsigned int x=0;
    if (RtlGenRandom == NULL) {
        // load proc if not loaded
        HMODULE lib = LoadLibraryA("advapi32.dll");
        RtlGenRandom = (RtlGenRandomFunc)GetProcAddress(lib, "SystemFunction036");
        if (RtlGenRandom == NULL) return 1;
    }
    RtlGenRandom(&x, sizeof(UINT_MAX));
    return (int)(x >> 1);
}
Beispiel #9
0
void CRYPTO_sysrand(void *out, size_t requested) {
  while (requested > 0) {
    ULONG output_bytes_this_pass = ULONG_MAX;
    if (requested < output_bytes_this_pass) {
      output_bytes_this_pass = requested;
    }
    if (RtlGenRandom(out, output_bytes_this_pass) == FALSE) {
      abort();
    }
    requested -= output_bytes_this_pass;
    out = (uint8_t *)out + output_bytes_this_pass;
  }
  return;
}
Beispiel #10
0
/* ECMA-262 3rd Edition    15.8.2.14 */
static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
        jsval_t *r)
{
    UINT x;

    TRACE("\n");

    if(!RtlGenRandom(&x, sizeof(x)))
        return E_UNEXPECTED;

    if(r)
        *r = jsval_number((double)x/(double)UINT_MAX);
    return S_OK;
}
Beispiel #11
0
/* ECMA-262 3rd Edition    15.8.2.14 */
static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
        VARIANT *retv, jsexcept_t *ei)
{
    UINT r;

    TRACE("\n");

    if(!RtlGenRandom(&r, sizeof(r)))
        return E_UNEXPECTED;

    if(retv)
        num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);

    return S_OK;
}
Beispiel #12
0
/*************************************************************************
 *           UuidCreate   [RPCRT4.@]
 *
 * Creates a 128bit UUID.
 *
 * RETURNS
 *
 *  RPC_S_OK if successful.
 *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
 *
 * NOTES
 *
 *  Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
 *  Truly Random or Pseudo-Random Numbers)
 */
RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
{
    RtlGenRandom(Uuid, sizeof(*Uuid));
    /* Clear the version bits and set the version (4) */
    Uuid->Data3 &= 0x0fff;
    Uuid->Data3 |= (4 << 12);
    /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
     * specified in RFC 4122, section 4.4.
     */
    Uuid->Data4[0] &= 0x3f;
    Uuid->Data4[0] |= 0x80;

    TRACE("%s\n", debugstr_guid(Uuid));

    return RPC_S_OK;
}
Beispiel #13
0
CAMLprim value stub_RtlGenRandom(value len){
  CAMLparam1(len);
  CAMLlocal3(ret, some, str);
  ret = Val_none;
#ifdef WIN32
  /* Allocate an OCaml string of the required length and zero it so we
     never return garbage and think it's random */
  int c_len = Int_val(len);
  str = caml_alloc_string(c_len);
  ZeroMemory(String_val(str), c_len);

  if (!RtlGenRandom((PVOID)(String_val(str)), c_len)) {
    win32_maperr(GetLastError());
    unix_error(errno, "RtlGenRandom", Nothing);
  }
  some = caml_alloc(1, 0);
  Store_field(some, 0, str);
  ret = some;
#endif
  CAMLreturn(ret);
}
static void NaClSecureRngFilbuf(struct NaClSecureRng *self) {
  if (!RtlGenRandom(self->buf, sizeof self->buf)) {
    NaClLog(LOG_FATAL, "RtlGenRandom failed: error 0x%x\n", GetLastError());
  }
  self->nvalid = sizeof self->buf;
}
Beispiel #15
0
static size_t random_read(int tag, void *buf, size_t count)
{
	if (!RtlGenRandom(buf, count))
		return 0;
	return count;
}