Skip to content

Add X25519 to Microsoft.Bcl.Cryptography - #129639

Merged
vcsjones merged 11 commits into
dotnet:mainfrom
vcsjones:oob-x25519
Jul 6, 2026
Merged

vcsjones merged 11 commits into
dotnet:mainfrom
vcsjones:oob-x25519

Conversation

@vcsjones

Copy link
Copy Markdown
Member

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.

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]>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

@vcsjones vcsjones added this to the 11.0.0 milestone Jun 19, 2026
Copilot AI review requested due to automatic review settings June 20, 2026 04:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Common and 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.Cryptography can 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.DeriveKeyMaterialTruncate gates on Win10 via Environment.OSVersion for NETFRAMEWORK/NETSTANDARD, which can be version-lied and cause false PlatformNotSupportedException on Win10+; see stored comment [ID: 001].
  • Microsoft.Bcl.Cryptography’s BuildX25519 wiring 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-approved issue for the newly exposed public surface in Microsoft.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

Copilot AI review requested due to automatic review settings June 20, 2026 18:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 30/34 changed files
  • Comments generated: 1

@vcsjones
vcsjones marked this pull request as draft June 23, 2026 16:59
@vcsjones

Copy link
Copy Markdown
Member Author

Draft until we tweak the targets that this is reachable on.

Copilot AI review requested due to automatic review settings June 23, 2026 18:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Comments suppressed due to low confidence (2)

src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellman.cs:1473

  • This method rents buffer from CryptoPool and then loops on TryExportPkcs8PrivateKeyCore. If TryExportPkcs8PrivateKeyCore throws (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 returning buffer on exceptions.
    src/libraries/Common/src/System/Security/Cryptography/X25519DiffieHellman.cs:1555
  • ExportEncryptedPkcs8PrivateKeyCore rents a buffer and loops calling TryExportPkcs8PrivateKey. If TryExportPkcs8PrivateKey throws (e.g., public-key-only instance), the currently rented buffer isn't returned to CryptoPool. 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

Copilot AI review requested due to automatic review settings July 3, 2026 01:17
@vcsjones
vcsjones requested a review from bartonjs July 3, 2026 01:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 30/34 changed files
  • Comments generated: 0 new

@vcsjones
vcsjones merged commit 5aa2f08 into dotnet:main Jul 6, 2026
84 of 88 checks passed
@vcsjones
vcsjones deleted the oob-x25519 branch July 6, 2026 20:15
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 7, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 7, 2026
@bartonjs bartonjs added the cryptographic-docs-impact Issues impacting cryptographic docs. Cleared and reused after documentation is updated each release. label Sep 11, 2026
@bartonjs bartonjs added the tracking This issue is tracking the completion of other related issues. label Oct 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Security cryptographic-docs-impact Issues impacting cryptographic docs. Cleared and reused after documentation is updated each release. tracking This issue is tracking the completion of other related issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants