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

Closed
leonida/mandy
syfyra wants to merge 0 commits from fix/bcrypt-rng-pseudo-handle into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

syfyra commented 2026-11-25 22:25:59 +00:00

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

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)
qhukz13 commented 2026-11-26 13:55:44 +00:00

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).

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).
aslan2142 commented 2026-12-01 12:22:07 +00:00

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.)

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:42:26 +00:00

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.

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.
Sign in to join this conversation.
No description provided.