Skip to content
Draft
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions runtime/shims/bcrypt_rng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ NTSTATUS WINAPI gen_random(BCRYPT_ALG_HANDLE hAlgorithm, PUCHAR pbBuffer, ULONG
if (!is_rng) return kStatusInvalidHandle;
}

// The host takes only these two flags and refuses any other with STATUS_INVALID_PARAMETER, after the handle checks
// above and before a zero-byte call is answered (measured on Windows 11 for 0x4, 0x10 and 0x80000000). Checked here
// because neither the seeded stream nor the SystemFunction036 fallback below reads the flags: the host's refusal
// fell through to that fallback, and a call Windows refuses came back as a success with random bytes.
if (dwFlags & ~static_cast<ULONG>(BCRYPT_RNG_USE_ENTROPY_IN_BUFFER | BCRYPT_USE_SYSTEM_PREFERRED_RNG))
return kStatusInvalidParameter;

if (cbBuffer == 0) return kStatusSuccess;

if (g_seeded_mode) {
Expand Down
25 changes: 25 additions & 0 deletions runtime/test/bcrypt_rng_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ void test_provider_handles_and_validation() {
"BCryptGenRandom rejects null handle without BCRYPT_USE_SYSTEM_PREFERRED_RNG");
check(gen_random(h, buf, sizeof(buf), BCRYPT_USE_SYSTEM_PREFERRED_RNG) == kStatusInvalidParameter,
"BCryptGenRandom rejects non-null handle with BCRYPT_USE_SYSTEM_PREFERRED_RNG");
// Flags the host does not take are STATUS_INVALID_PARAMETER and write nothing, also for zero bytes and next to
// BCRYPT_USE_SYSTEM_PREFERRED_RNG; a bad handle is still reported first (measured on Windows 11). In both modes:
// the seeded stream and the SystemFunction036 fallback do not read the flags.
{
bool refused = true;
for (const ULONG flags : {0x4ul, 0x10ul, 0x80000000ul}) {
std::memset(buf, 0xA5, sizeof(buf));
const NTSTATUS st = gen_random(h, buf, sizeof(buf), flags);
bool untouched = true;
for (uint8_t b : buf) untouched = untouched && b == 0xA5;
if (st != kStatusInvalidParameter || !untouched) {
std::printf(" flags 0x%lx: status 0x%08lx%s\n", flags, static_cast<unsigned long>(st),
untouched ? "" : ", buffer written");
refused = false;
}
}
check(refused, "BCryptGenRandom refuses unknown flags with STATUS_INVALID_PARAMETER and writes nothing");
check(gen_random(h, buf, 0, 0x4) == kStatusInvalidParameter, "an unknown flag is refused for zero bytes too");
check(gen_random(nullptr, buf, sizeof(buf), BCRYPT_USE_SYSTEM_PREFERRED_RNG | 0x4) == kStatusInvalidParameter,
"an unknown flag next to BCRYPT_USE_SYSTEM_PREFERRED_RNG is refused");
check(gen_random(nullptr, buf, sizeof(buf), 0x4) == kStatusInvalidHandle,
"a null handle with an unknown flag is still STATUS_INVALID_HANDLE, as on the host");
check(gen_random(h, buf, sizeof(buf), BCRYPT_RNG_USE_ENTROPY_IN_BUFFER) == kStatusSuccess,
"BCRYPT_RNG_USE_ENTROPY_IN_BUFFER is accepted, as on the host");
}

std::memset(buf, 0, sizeof(buf));
check(gen_random(h, buf, sizeof(buf), 0) == kStatusSuccess,
Expand Down