Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unpredictableSeed use CryptGenRandom (CryptoAPI) on Windows #10624

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions std/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,49 @@ else
}
}

version (Windows)
{
pragma(lib, "advapi32.lib"); // `std.registry` does so, too.

private bool wincryptGenRandom(T)(out T result) @trusted
{
import core.sys.windows.windef : DWORD, PBYTE;
import core.sys.windows.wincrypt :
CryptAcquireContext,
CryptGenRandom,
CryptReleaseContext,
CRYPT_VERIFYCONTEXT,
HCRYPTPROV,
MS_STRONG_PROV,
PROV_RSA_FULL;

HCRYPTPROV wincryptHandle;

const gotHandle = CryptAcquireContext(
&wincryptHandle,
null,
MS_STRONG_PROV.ptr,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT,
);
if (!gotHandle)
return false;

scope (exit)
if (!CryptReleaseContext(wincryptHandle, 0)) { /* ignore */ }

const gotRandom = CryptGenRandom(
wincryptHandle,
DWORD(T.sizeof),
cast(PBYTE) &result,
);
if (!gotRandom)
return false;

return true;
}
}

/**
A "good" seed for initializing random number engines. Initializing
with $(D_PARAM unpredictableSeed) makes engines generate different
Expand All @@ -1788,7 +1831,19 @@ how excellent the source of entropy is.
*/
@property uint unpredictableSeed() @trusted nothrow @nogc
{
version (AnyARC4Random)
version (Windows)
{
uint result;
if (!wincryptGenRandom!uint(result))
{
version (none)
return fallbackSeed();
else
assert(false, "CryptAcquireContext() or CryptGenRandom() failed.");
}
return result;
}
else version (AnyARC4Random)
{
return arc4random();
}
Expand Down Expand Up @@ -1837,7 +1892,19 @@ if (isUnsigned!UIntType)
/// ditto
@property UIntType unpredictableSeed() @nogc nothrow @trusted
{
version (AnyARC4Random)
version (Windows)
{
UIntType result;
if (!wincryptGenRandom!UIntType(result))
{
version (none)
return fallbackSeed();
else
assert(false, "CryptAcquireContext() or CryptGenRandom() failed.");
}
return result;
}
else version (AnyARC4Random)
{
static if (UIntType.sizeof <= uint.sizeof)
{
Expand Down
Loading