Skip to content

bcrypt_rng: BCryptGenRandom refuses flags the host does not take (BCRYPT-GEN-RANDOM-FLAGS) - #2381

Draft
syfyra wants to merge 1 commit into
ProjectLeonida:mainfrom
syfyra:fix/bcrypt-gen-random-flags
Draft

syfyra wants to merge 1 commit into
ProjectLeonida:mainfrom
syfyra:fix/bcrypt-gen-random-flags

Conversation

@syfyra

@syfyra syfyra commented Nov 26, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: BCRYPT-GEN-RANDOM-FLAGS

What and why

BCryptGenRandom takes two flags: BCRYPT_RNG_USE_ENTROPY_IN_BUFFER (ignored since Windows 7) and BCRYPT_USE_SYSTEM_PREFERRED_RNG. The host refuses any other flag with STATUS_INVALID_PARAMETER. The shim passed the flags on, and when the host refused them, gen_random fell through to its SystemFunction036 fallback, which takes no flags. The call came back as STATUS_SUCCESS, with random bytes in the buffer. With LEONIDA_RNG_SEED set, the seeded stream ignored the flags as well. A call Windows refuses succeeded under the shim.

Measured on Windows 11 against the in-box bcrypt.dll with a small probe:

call host
RNG handle, 16 bytes, flags 0x4, 0x10 or 0x80000000 STATUS_INVALID_PARAMETER
RNG handle, 0 bytes, flags 0x4 STATUS_INVALID_PARAMETER
null handle, flags BCRYPT_USE_SYSTEM_PREFERRED_RNG | 0x4 STATUS_INVALID_PARAMETER
null handle or a SHA-256 handle, flags 0x4 STATUS_INVALID_HANDLE (the handle is checked first)
RNG handle, flags BCRYPT_RNG_USE_ENTROPY_IN_BUFFER succeeds
null handle, flags BCRYPT_USE_SYSTEM_PREFERRED_RNG | BCRYPT_RNG_USE_ENTROPY_IN_BUFFER succeeds

gen_random now refuses any other flag with STATUS_INVALID_PARAMETER. The check sits where the host makes it: after the handle checks and before a zero-byte call is answered, and before the seeded and fallback paths. Valid calls are unchanged.

Tested

Windows 11, MSVC 14.44 (Build Tools), RelWithDebInfo:

The check comes before the host is called, so it behaves the same under Wine; not run there.

GPUs tested: n/a (CPU tests)

AI-assisted: yes (Claude Code, Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

…YPT-GEN-RANDOM-FLAGS)

BCryptGenRandom passed any flags to the host, and the host's refusal
(STATUS_INVALID_PARAMETER for every flag but
BCRYPT_RNG_USE_ENTROPY_IN_BUFFER and BCRYPT_USE_SYSTEM_PREFERRED_RNG,
measured on Windows 11 for 0x4, 0x10 and 0x80000000) fell through to the
SystemFunction036 fallback, which ignores flags: the call returned
STATUS_SUCCESS with random bytes. The seeded stream ignored them too.
The flags are now checked where the host checks them, after the handle
and before a zero-byte call is answered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant