Implement X25519DiffieHellmanCng#127924
Merged
vcsjones merged 3 commits intodotnet:mainfrom May 9, 2026
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a Windows CNG-backed implementation of X25519DiffieHellman (X25519DiffieHellmanCng) to enable interop with CNG keys/providers, and factors shared Curve25519/CNG-blob handling into a reusable Windows helper.
Changes:
- Introduces
X25519DiffieHellmanCng(Windows implementation + public surface + non-Windows thrower). - Refactors Windows X25519 key import/export and public-key reduction logic into
X25519WindowsHelpers. - Adds CNG-focused test coverage and adjusts base tests to account for platforms that can’t roundtrip “unclamped” private scalars.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography/tests/X25519DiffieHellmanCngTests.cs | Adds CNG-specific tests for construction, export policies, and non-exportable behavior. |
| src/libraries/System.Security.Cryptography/tests/X25519DiffieHellmanBaseTests.cs | Adds CanRoundTripKeys and shared helper logic for clamped/unclamped private key expectations. |
| src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj | Includes new test and required Windows/CNG helper sources for the test assembly. |
| src/libraries/System.Security.Cryptography/tests/CngHelpers.cs | Adds a small test-only helper for mapping NCrypt error codes to CryptographicException. |
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.Windows.cs | Switches Windows BCrypt implementation to use the shared helper for reduction/blob operations. |
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs | Implements the Windows CNG key agreement + export logic for X25519DiffieHellmanCng. |
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.cs | Adds the public X25519DiffieHellmanCng API (Windows-only) and docs. |
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Cng.NotSupported.cs | Adds non-Windows stubs for X25519DiffieHellmanCng that throw PlatformNotSupportedException. |
| src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj | Wires new source files into the product build and links the shared Windows helper. |
| src/libraries/System.Security.Cryptography/src/Resources/Strings.resx | Adds a new resource string for invalid CNG X25519 key usage. |
| src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs | Adds the new public ref surface for X25519DiffieHellmanCng. |
| src/libraries/Common/src/System/Security/Cryptography/X25519WindowsHelpers.cs | New shared helper for public-key reduction + CNG blob create/export + scalar fixup/refix. |
| src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.Encrypted.cs | Adds stateful overloads to avoid captures/allocations (supports ref-struct state). |
| src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs | Adds a stateful KeyReader delegate + overload to pass state into PKCS#8 readers. |
| src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.NamedCurve.cs | Adds an optional flags parameter to ImportKeyBlob to support import flags. |
| src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs | Adds a Span-based P/Invoke overload and TryDeriveKeyMaterialTruncate helper. |
| src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Keys.cs | Adds a Span-based NCryptExportKey overload for allocation-free export. |
Copilot's findings
- Files reviewed: 17/17 changed files
- Comments generated: 9
bartonjs
reviewed
May 8, 2026
bartonjs
approved these changes
May 8, 2026
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.
Contributes to #126206