bcrypt_rng: accept BCRYPT_RNG_ALG_HANDLE in BCryptGenRandom (BCRYPT-RNG-PSEUDO-HANDLE) #2132

Open
github-bot wants to merge 1 commit from github-bot/gh-2132 into main AGit
Owner

@⁠syfyra on GitHub · 2026-11-25 22:25 UTC · original
syfyra:fix/bcrypt-rng-pseudo-handle → main · +54 −1 · 2 files · 1 commits · draft

Claimed in the coordination issue as: BCRYPT-RNG-PSEUDO-HANDLE (claim comment to follow)

What and why

BCRYPT_RNG_ALG_HANDLE ((BCRYPT_ALG_HANDLE)0x81, bcrypt.h for NTDDI_WINTHRESHOLD and later) is the CNG pseudo-handle of the RNG: on Windows 10 and later it can be passed wherever an RNG algorithm handle is, with no BCryptOpenAlgorithmProvider ("CNG Algorithm Pseudo-handles").

runtime/shims/bcrypt_rng.cpp gen_random looked every handle up among the ones it had opened itself, found nothing for 0x81 and returned STATUS_INVALID_HANDLE with the buffer left as it was, in the LEONIDA_RNG_SEED mode too. A caller that does not check the status (common despite _Must_inspect_result_) then uses whatever the buffer held as its random bytes.

The pseudo-handle is now an RNG handle: it goes to the host's BCryptGenRandom as it is, or to the seeded stream under LEONIDA_RNG_SEED, and the fail-closed path (no host RNG) still returns STATUS_UNSUCCESSFUL without touching the buffer. Every other answer is unchanged and matches Windows 11's bcrypt.dll, measured with a small probe:

call Windows 11 bcrypt.dll shim before shim after
BCryptGenRandom(0x81, buf, 16, 0) STATUS_SUCCESS STATUS_INVALID_HANDLE STATUS_SUCCESS
BCryptGenRandom(BCRYPT_SHA256_ALG_HANDLE, ...) STATUS_INVALID_HANDLE same same
BCryptGenRandom(0x81, ..., BCRYPT_USE_SYSTEM_PREFERRED_RNG) STATUS_INVALID_PARAMETER same same
BCryptCloseAlgorithmProvider(0x81, 0) STATUS_INVALID_HANDLE same same

Tested

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

  • bcrypt_rng_test and bcrypt_rng_test --seeded (LEONIDA_RNG_SEED=0x123456789abcdef0, as ctest runs it) get five new checks: the pseudo-handle fills the buffer, another algorithm's pseudo-handle and closing 0x81 are refused, 0x81 with the system-RNG flag is an invalid parameter, the fail-closed path leaves the buffer untouched, and with the host RNG switched off the seeded run still serves 0x81 (so the bytes come from the seeded stream). On main 3 checks fail in each run; with the fix all 27 pass in both. No new /W4 warnings.
  • The full CPU ctest (162 tests, -j 8) passes.
  • scripts/check_no_game_data.py on both files.

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

**@⁠syfyra** on GitHub · 2026-11-25 22:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2132) `syfyra:fix/bcrypt-rng-pseudo-handle` → `main` · +54 −1 · 2 files · 1 commits · draft Claimed in the coordination issue as: BCRYPT-RNG-PSEUDO-HANDLE (claim comment to follow) ### What and why `BCRYPT_RNG_ALG_HANDLE` (`(BCRYPT_ALG_HANDLE)0x81`, bcrypt.h for NTDDI_WINTHRESHOLD and later) is the CNG pseudo-handle of the RNG: on Windows 10 and later it can be passed wherever an RNG algorithm handle is, with no `BCryptOpenAlgorithmProvider` ("CNG Algorithm Pseudo-handles"). `runtime/shims/bcrypt_rng.cpp` `gen_random` looked every handle up among the ones it had opened itself, found nothing for 0x81 and returned `STATUS_INVALID_HANDLE` with the buffer left as it was, in the `LEONIDA_RNG_SEED` mode too. A caller that does not check the status (common despite `_Must_inspect_result_`) then uses whatever the buffer held as its random bytes. The pseudo-handle is now an RNG handle: it goes to the host's `BCryptGenRandom` as it is, or to the seeded stream under `LEONIDA_RNG_SEED`, and the fail-closed path (no host RNG) still returns `STATUS_UNSUCCESSFUL` without touching the buffer. Every other answer is unchanged and matches Windows 11's bcrypt.dll, measured with a small probe: | call | Windows 11 bcrypt.dll | shim before | shim after | |---|---|---|---| | `BCryptGenRandom(0x81, buf, 16, 0)` | `STATUS_SUCCESS` | `STATUS_INVALID_HANDLE` | `STATUS_SUCCESS` | | `BCryptGenRandom(BCRYPT_SHA256_ALG_HANDLE, ...)` | `STATUS_INVALID_HANDLE` | same | same | | `BCryptGenRandom(0x81, ..., BCRYPT_USE_SYSTEM_PREFERRED_RNG)` | `STATUS_INVALID_PARAMETER` | same | same | | `BCryptCloseAlgorithmProvider(0x81, 0)` | `STATUS_INVALID_HANDLE` | same | same | ### Tested Windows 11, MSVC 14.44 (Build Tools), RelWithDebInfo: - `bcrypt_rng_test` and `bcrypt_rng_test --seeded` (`LEONIDA_RNG_SEED=0x123456789abcdef0`, as ctest runs it) get five new checks: the pseudo-handle fills the buffer, another algorithm's pseudo-handle and closing 0x81 are refused, 0x81 with the system-RNG flag is an invalid parameter, the fail-closed path leaves the buffer untouched, and with the host RNG switched off the seeded run still serves 0x81 (so the bytes come from the seeded stream). On `main` 3 checks fail in each run; with the fix all 27 pass in both. No new `/W4` warnings. - The full CPU ctest (162 tests, `-j 8`) passes. - `scripts/check_no_game_data.py` on both files. 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)
BCRYPT_RNG_ALG_HANDLE (0x81) is the CNG pseudo-handle of the RNG: on
Windows 10 and later it can be passed wherever an RNG algorithm handle is,
with no BCryptOpenAlgorithmProvider. The shim looked every handle up among
the ones it had opened itself, found nothing, and returned
STATUS_INVALID_HANDLE with the buffer untouched, in the seeded mode too. A
caller that does not check the status uses whatever the buffer held as its
random bytes.

The pseudo-handle is now an RNG handle that goes to the host as it is (the
host's BCryptGenRandom takes it) or to the seeded stream under
LEONIDA_RNG_SEED. Everything else keeps its answer, matching Windows 11's
bcrypt.dll: another algorithm's pseudo-handle is STATUS_INVALID_HANDLE, so
is closing 0x81, and 0x81 with BCRYPT_USE_SYSTEM_PREFERRED_RNG is
STATUS_INVALID_PARAMETER.

Tested (Windows 11, MSVC 14.44): bcrypt_rng_test and its --seeded run get
5 new checks; on main 3 of them fail in each run, with the fix all 27 pass.
The host answers were measured with a separate probe against bcrypt.dll.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:19:17 +00:00
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 13:55 UTC · original

I tested e99a2de45 merged onto main 73516a290 (no conflicts) on Windows 10 Pro 19045, MSVC RelWithDebInfo.

Windows 10 matches your Windows 11 table. The same four calls, against the in-box bcrypt.dll 10.0.19041.1 through a small P/Invoke probe:

call Windows 10 bcrypt.dll
BCryptGenRandom(0x81, buf, 16, 0) STATUS_SUCCESS, buffer filled
BCryptGenRandom(0x41 = BCRYPT_SHA256_ALG_HANDLE, buf, 16, 0) 0xC0000008 STATUS_INVALID_HANDLE
BCryptGenRandom(0x81, buf, 16, BCRYPT_USE_SYSTEM_PREFERRED_RNG) 0xC000000D STATUS_INVALID_PARAMETER
BCryptCloseAlgorithmProvider(0x81, 0) 0xC0000008 STATUS_INVALID_HANDLE

So the shim's answers after this change are the host's on both Windows versions. The Close row is worth having: a caller that closes the pseudo-handle gets the same failure from the shim as from Windows, and no shim bookkeeping is touched.

Tests:

  • bcrypt_rng and the seeded run pass, 2/2, plain and with LEONIDA_VK_VALIDATION=1, three repeats each.

  • They catch the bug. With main's runtime/shims/bcrypt_rng.cpp and this PR's test, bcrypt_rng fails 3 of 27 checks, exactly the new ones:

    • the pseudo-handle without an open;
    • random bytes written through it;
    • the fail-closed pseudo-handle path.

    The other 24 pass on both.

The change reads right. The pseudo-handle branch sits after the BCRYPT_USE_SYSTEM_PREFERRED_RNG check, so 0x81 with that flag still gets STATUS_INVALID_PARAMETER as on the host. It also never enters the opened-handle table, so a Close of it cannot remove anything.

AI-assisted (Claude Code).

**@⁠qhukz13** on GitHub · 2026-11-26 13:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2132#issuecomment-7194032980) I tested `e99a2de45` merged onto main `73516a290` (no conflicts) on Windows 10 Pro 19045, MSVC RelWithDebInfo. **Windows 10 matches your Windows 11 table.** The same four calls, against the in-box `bcrypt.dll` 10.0.19041.1 through a small P/Invoke probe: | call | Windows 10 bcrypt.dll | |---|---| | `BCryptGenRandom(0x81, buf, 16, 0)` | `STATUS_SUCCESS`, buffer filled | | `BCryptGenRandom(0x41 = BCRYPT_SHA256_ALG_HANDLE, buf, 16, 0)` | `0xC0000008` STATUS_INVALID_HANDLE | | `BCryptGenRandom(0x81, buf, 16, BCRYPT_USE_SYSTEM_PREFERRED_RNG)` | `0xC000000D` STATUS_INVALID_PARAMETER | | `BCryptCloseAlgorithmProvider(0x81, 0)` | `0xC0000008` STATUS_INVALID_HANDLE | So the shim's answers after this change are the host's on both Windows versions. The Close row is worth having: a caller that closes the pseudo-handle gets the same failure from the shim as from Windows, and no shim bookkeeping is touched. **Tests:** - `bcrypt_rng` and the seeded run pass, 2/2, plain and with `LEONIDA_VK_VALIDATION=1`, three repeats each. - **They catch the bug.** With main's `runtime/shims/bcrypt_rng.cpp` and this PR's test, `bcrypt_rng` fails 3 of 27 checks, exactly the new ones: - the pseudo-handle without an open; - random bytes written through it; - the fail-closed pseudo-handle path. The other 24 pass on both. The change reads right. The pseudo-handle branch sits after the `BCRYPT_USE_SYSTEM_PREFERRED_RNG` check, so `0x81` with that flag still gets STATUS_INVALID_PARAMETER as on the host. It also never enters the opened-handle table, so a Close of it cannot remove anything. AI-assisted (Claude Code).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2132/head:github-bot/gh-2132'
git switch 'github-bot/gh-2132'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2132'
git switch 'github-bot/gh-2132'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2132'
git switch 'github-bot/gh-2132'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2132'
git switch 'main'
git merge --squash 'github-bot/gh-2132'
git switch 'main'
git merge --ff-only 'github-bot/gh-2132'
git switch 'main'
git merge 'github-bot/gh-2132'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.