Repository navigation
Add X25519 to Microsoft.Bcl.Cryptography - #129639
Merged
Merged
Conversation
Share the X25519DiffieHellman implementation and tests through Common so Microsoft.Bcl.Cryptography can build the API for downlevel targets while forwarding to System.Security.Cryptography on .NET 11 and later. Wire the Microsoft.Bcl.Cryptography project and tests for X25519, add the required downlevel shims/resources, and deduplicate CNG key duplication helpers used by the CNG-backed cryptography types. Co-authored-by: Copilot <[email protected]>
vcsjones
requested review from
bartonjs and
Copilot
and removed request for
Copilot
June 19, 2026 21:44
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 refactors the X25519 Diffie-Hellman implementation and its test suite into src/libraries/Common so it can be shared, then wires Microsoft.Bcl.Cryptography to expose the X25519 APIs for downlevel targets while type-forwarding to System.Security.Cryptography on .NET 11+.
Changes:
- Move/link X25519 implementation and tests to
Commonand update the System.Security.Cryptography csproj/test csproj to consume those shared sources. - Add X25519 build switches, type-forwarders, resources, and shims so
Microsoft.Bcl.Cryptographycan expose X25519 on downlevel TFMs. - Deduplicate CNG key duplication helpers and add downlevel-safe shims for APIs like RNG fill and fixed-time comparison helpers.
Review notes (blocking):
Interop.NCryptDeriveKeyMaterial.DeriveKeyMaterialTruncategates on Win10 viaEnvironment.OSVersionfor NETFRAMEWORK/NETSTANDARD, which can be version-lied and cause falsePlatformNotSupportedExceptionon Win10+; see stored comment [ID: 001].Microsoft.Bcl.Cryptography’sBuildX25519wiring currently only compiles the Windows implementation pieces; if cross-platform downlevel support is intended, additional platform implementations (Apple/Android/OpenSSL) likely need to be included or tests/expectations adjusted; see stored comment [ID: 002].- The PR description doesn’t reference an
api-approvedissue for the newly exposed public surface inMicrosoft.Bcl.Cryptography, so API approval status cannot be verified from the provided metadata.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj | Switch X25519 test compilation to linked Common test sources. |
| src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj | Switch X25519 core implementation compilation to linked Common sources. |
| src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj | Add X25519 tests and required Windows interop/helpers for the BCL test project. |
| src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/NetStandardShims.cs | Add downlevel shims/polyfills needed by shared X25519 code/tests. |
| src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/CngExtensions.cs | Remove now-duplicated CNG key duplication extension. |
| src/libraries/Microsoft.Bcl.Cryptography/src/Resources/Strings.resx | Add new resource strings used by X25519 code paths. |
| src/libraries/Microsoft.Bcl.Cryptography/src/Microsoft.Bcl.Cryptography.Forwards.cs | Add .NET 11+ type-forwarders for X25519 types. |
| src/libraries/Microsoft.Bcl.Cryptography/src/Microsoft.Bcl.Cryptography.csproj | Wire BuildX25519 flag and include Common/shared sources for downlevel X25519. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanTests.cs | New shared X25519 API-level tests (imports/PEM/encrypted PKCS#8). |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanTestData.cs | New shared X25519 test vectors and encoded key blobs. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanNotSupportedTests.cs | Adjust hex parsing helpers for shared test infra. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanKeyTests.cs | New shared tests for key generation/import/export and edge-case roundtrips. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanImplementationTests.cs | Adjust supported-platform expectation logic for shared tests. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanContractTests.cs | Replace Random.Shared usage for broader TFM compatibility. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanCngTests.Windows.cs | Add Windows-only attributes and adjust return types for shared test base. |
| src/libraries/Common/tests/System/Security/Cryptography/X25519DiffieHellmanBaseTests.cs | Replace Convert.FromHexString usage with shared hex helpers. |
| src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.Windows.cs | Add OS guard for handle open and make implementation type partial for sharing. |
| src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.NotSupported.cs | Make implementation type partial for shared build layout. |
| src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs | Use deduped CNG duplication helper; adjust handle usage for downlevel builds. |
| src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellmanCng.cs | New public CNG wrapper type for X25519 (shared source). |
| src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellman.cs | Make base type partial; adjust delegates/constraints for downlevel compilation. |
| src/libraries/Common/src/System/Security/Cryptography/SlhDsa.cs | Remove now-centralized delegate definition. |
| src/libraries/Common/src/System/Security/Cryptography/MLKemCng.Windows.cs | Switch to shared CngKey.Duplicate() helper. |
| src/libraries/Common/src/System/Security/Cryptography/MLKem.cs | Remove now-centralized delegate definitions. |
| src/libraries/Common/src/System/Security/Cryptography/MLDsaCng.Windows.cs | Switch to shared CngKey.Duplicate() helper. |
| src/libraries/Common/src/System/Security/Cryptography/MLDsa.cs | Remove now-centralized delegate definition. |
| src/libraries/Common/src/System/Security/Cryptography/KeyFormatHelper.cs | Centralize shared delegate types used across crypto code. |
| src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsa.cs | Switch callback delegate to centralized definition. |
| src/libraries/Common/src/System/Security/Cryptography/CngHelpers.cs | Add CngKey.Duplicate() helper and downlevel RNG fill helper. |
| src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs | Remove dependency on Internal.Cryptography.Helpers and adjust annotations/asserts. |
| src/libraries/Common/src/Interop/Windows/NCrypt/Interop.NCryptDeriveKeyMaterial.cs | Add downlevel Win10 gating helper for RAW KDF; introduce version-check logic requiring adjustment. |
Copilot's findings
- Files reviewed: 27/31 changed files
- Comments generated: 2
This was referenced Jun 21, 2026
Open
vcsjones
marked this pull request as draft
June 23, 2026 16:59
Member
Author
|
Draft until we tweak the targets that this is reachable on. |
Contributor
There was a problem hiding this comment.
Copilot's findings
Comments suppressed due to low confidence (2)
src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellman.cs:1473
- This method rents
bufferfromCryptoPooland then loops onTryExportPkcs8PrivateKeyCore. IfTryExportPkcs8PrivateKeyCorethrows (e.g., public-key-only instance exporting PKCS#8 throws), the current rented buffer isn't returned to the pool. Consider making the resize loop exception-safe by returningbufferon exceptions.
src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellman.cs:1555 ExportEncryptedPkcs8PrivateKeyCorerents a buffer and loops callingTryExportPkcs8PrivateKey. IfTryExportPkcs8PrivateKeythrows (e.g., public-key-only instance), the currently rented buffer isn't returned toCryptoPool. Wrapping the loop in a try/catch to return the buffer would avoid leaking pooled arrays on expected exception paths.
- Files reviewed: 30/34 changed files
- Comments generated: 0 new
vcsjones
marked this pull request as ready for review
June 23, 2026 19:14
This was referenced Jun 24, 2026
bartonjs
reviewed
Jul 2, 2026
bartonjs
approved these changes
Jul 2, 2026
bartonjs
approved these changes
Jul 6, 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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Share the X25519DiffieHellman implementation and tests through Common so Microsoft.Bcl.Cryptography can build the API for downlevel targets while forwarding to System.Security.Cryptography on .NET 11 and later.
Wire the Microsoft.Bcl.Cryptography project and tests for X25519, add the required downlevel shims/resources, and deduplicate CNG key duplication helpers used by the CNG-backed cryptography types.