[PR #2132] bcrypt_rng: accept BCRYPT_RNG_ALG_HANDLE in BCryptGenRandom (BCRYPT-RNG-PSEUDO-HANDLE) #2136

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

📋 Pull Request Information

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

Base: main ← Head: fix/bcrypt-rng-pseudo-handle


📝 Commits (1)

  • e99a2de bcrypt_rng: accept BCRYPT_RNG_ALG_HANDLE in BCryptGenRandom (BCRYPT-RNG-PSEUDO-HANDLE)

📊 Changes

2 files changed (+54 additions, -1 deletions)

View changed files

📝 runtime/shims/bcrypt_rng.cpp (+13 -1)
📝 runtime/test/bcrypt_rng_test.cpp (+41 -0)

📄 Description

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


🔄 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/2132 **Author:** [@syfyra](https://github.com/syfyra) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/bcrypt-rng-pseudo-handle` --- ### 📝 Commits (1) - [`e99a2de`](https://github.com/ubiquitarianism/leonida/commit/e99a2de45194b47a5a0844e593eaa396da809f61) bcrypt_rng: accept BCRYPT_RNG_ALG_HANDLE in BCryptGenRandom (BCRYPT-RNG-PSEUDO-HANDLE) ### 📊 Changes **2 files changed** (+54 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/bcrypt_rng.cpp` (+13 -1) 📝 `runtime/test/bcrypt_rng_test.cpp` (+41 -0) </details> ### 📄 Description 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) --- <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#2136
No description provided.