Skip to content

Allow Windows in CSPRNG, where SystemRandomNumberGenerator uses BCryptGenRandom - #255

Open
odrobnik wants to merge 1 commit into
apple:mainfrom
odrobnik:windows-csprng
Open

odrobnik wants to merge 1 commit into
apple:mainfrom
odrobnik:windows-csprng

Conversation

@odrobnik

Copy link
Copy Markdown

Motivation

On Windows, CSPRNG.init() traps with Platform does not have a supported CSPRNG the first time NIOSSH needs random bytes (key exchange, packet padding), so every SSH connection crashes the process. We hit this in a downstream package's Windows CI on the first handshake: job log. The README says NIOSSH supports all platforms supported by SwiftNIO and Swift Crypto, and that includes Windows.

The trap exists because it is unclear whether SystemRandomNumberGenerator is a CSPRNG on every platform. For Windows the standard library settles that question:

  • The documentation of SystemRandomNumberGenerator lists the platform implementations and states: "Windows uses BCryptGenRandom."
  • swift_stdlib_random calls BCryptGenRandom(nullptr, …, BCRYPT_USE_SYSTEM_PREFERRED_RNG), the system-preferred CSPRNG, and calls fatalError if that reports an error. A failure is never silently turned into weak randomness.

Modifications

  • Add os(Windows) to the platforms on which CSPRNG wraps SystemRandomNumberGenerator.
  • Extend the doc comment with the reason Windows qualifies.

Result

NIOSSH no longer traps on Windows when it generates random bytes. Behaviour on every other platform is unchanged.

Testing

  • macOS: swift test passes all 346 tests, and swift format lint --strict is clean on the changed file.
  • This repository's CI doesn't run unit tests on Windows (the windows_*_enabled inputs of the shared unit_tests.yml are off), so there's no Windows regression test here. I'm happy to enable Windows unit tests in this PR or a follow-up if you'd like.

🤖 Generated with Claude Code

…tGenRandom

Motivation:

On Windows, `CSPRNG.init()` traps with "Platform does not have a
supported CSPRNG" the first time NIOSSH needs random bytes (key exchange,
packet padding), so every SSH connection crashes the process. The README
says NIOSSH supports all platforms supported by SwiftNIO and Swift Crypto,
which includes Windows.

The trap exists because it is unclear whether `SystemRandomNumberGenerator`
is a CSPRNG on every platform. For Windows the standard library settles
that question. Its documentation for `SystemRandomNumberGenerator` states
"Windows uses `BCryptGenRandom`" (stdlib/public/core/Random.swift), and the
runtime calls `BCryptGenRandom(nullptr, ..., BCRYPT_USE_SYSTEM_PREFERRED_RNG)`,
the system-preferred CSPRNG, and fails hard if that call reports an error
(stdlib/public/stubs/Random.cpp).

Modifications:

- Add `os(Windows)` to the platforms on which `CSPRNG` wraps
  `SystemRandomNumberGenerator`.
- Extend the doc comment with the reason Windows qualifies.

Result:

NIOSSH no longer traps on Windows when it generates random bytes.
Behaviour on every other platform is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant