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

Closed
syfyra wants to merge 1 commit from upstream-pr/2381 into main
syfyra commented 2026-11-26 16:36:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by syfyra on 2026-11-26T16:36:49Z.


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:

  • bcrypt_rng_test gets five checks in the part that runs in both modes, matching the table above: the three unknown flags are refused and leave the buffer untouched, a zero-byte call with an unknown flag is refused, so is an unknown flag next to BCRYPT_USE_SYSTEM_PREFERRED_RNG, a null handle with an unknown flag is still STATUS_INVALID_HANDLE, and BCRYPT_RNG_USE_ENTROPY_IN_BUFFER still succeeds.
    • main: bcrypt_rng and bcrypt_rng_seeded each fail 3 of 26 checks. All three unknown flags return STATUS_SUCCESS and write the buffer.
    • this branch: both pass, 26 of 26.
  • Merges cleanly with #2132 (BCRYPT-RNG-PSEUDO-HANDLE, the other change to this function and test) and #983, the open pull requests that change these files (scripts/pr_overlap.py from #2349).
  • scripts/check_no_game_data.py on both files.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5513792559 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2381). Originally posted by [syfyra](https://github.com/syfyra) on 2026-11-26T16:36:49Z. --- 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: - `bcrypt_rng_test` gets five checks in the part that runs in both modes, matching the table above: the three unknown flags are refused and leave the buffer untouched, a zero-byte call with an unknown flag is refused, so is an unknown flag next to `BCRYPT_USE_SYSTEM_PREFERRED_RNG`, a null handle with an unknown flag is still `STATUS_INVALID_HANDLE`, and `BCRYPT_RNG_USE_ENTROPY_IN_BUFFER` still succeeds. - `main`: `bcrypt_rng` and `bcrypt_rng_seeded` each fail 3 of 26 checks. All three unknown flags return `STATUS_SUCCESS` and write the buffer. - this branch: both pass, 26 of 26. - Merges cleanly with #2132 (BCRYPT-RNG-PSEUDO-HANDLE, the other change to this function and test) and #983, the open pull requests that change these files (`scripts/pr_overlap.py` from #2349). - `scripts/check_no_game_data.py` on both files. 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
aslan2142 commented 2026-12-01 12:33:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:33:54Z.


Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7311322142 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2381#issuecomment-7311322142). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:33:54Z. --- Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 commented 2026-12-01 16:44:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:44:24Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318136712 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2381#issuecomment-7318136712). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:44:24Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2381
No description provided.