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

Open
opened 2026-11-26 21:21:03 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2381
Author: @syfyra
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: fix/bcrypt-gen-random-flags


📝 Commits (1)

  • 2a3baba bcrypt_rng: BCryptGenRandom refuses flags the host does not take (BCRYPT-GEN-RANDOM-FLAGS)

📊 Changes

2 files changed (+32 additions, -0 deletions)

View changed files

📝 runtime/shims/bcrypt_rng.cpp (+7 -0)
📝 runtime/test/bcrypt_rng_test.cpp (+25 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2381 **Author:** [@syfyra](https://github.com/syfyra) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/bcrypt-gen-random-flags` --- ### 📝 Commits (1) - [`2a3baba`](https://github.com/ubiquitarianism/leonida/commit/2a3baba0ccceeb815f73eaff0f0f59b9ac464040) bcrypt_rng: BCryptGenRandom refuses flags the host does not take (BCRYPT-GEN-RANDOM-FLAGS) ### 📊 Changes **2 files changed** (+32 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/bcrypt_rng.cpp` (+7 -0) 📝 `runtime/test/bcrypt_rng_test.cpp` (+25 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2381
No description provided.