Repository navigation
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
On Windows,
CSPRNG.init()traps withPlatform does not have a supported CSPRNGthe 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
SystemRandomNumberGeneratoris a CSPRNG on every platform. For Windows the standard library settles that question:SystemRandomNumberGeneratorlists the platform implementations and states: "Windows usesBCryptGenRandom."swift_stdlib_randomcallsBCryptGenRandom(nullptr, …, BCRYPT_USE_SYSTEM_PREFERRED_RNG), the system-preferred CSPRNG, and callsfatalErrorif that reports an error. A failure is never silently turned into weak randomness.Modifications
os(Windows)to the platforms on whichCSPRNGwrapsSystemRandomNumberGenerator.Result
NIOSSH no longer traps on Windows when it generates random bytes. Behaviour on every other platform is unchanged.
Testing
swift testpasses all 346 tests, andswift format lint --strictis clean on the changed file.windows_*_enabledinputs of the sharedunit_tests.ymlare 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