Skip to content

Add X25519 to Microsoft.Bcl.Cryptography#129639

Open
vcsjones wants to merge 6 commits into
dotnet:mainfrom
vcsjones:oob-x25519
Open

Add X25519 to Microsoft.Bcl.Cryptography#129639
vcsjones wants to merge 6 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 <223556219+Copilot@users.noreply.github.com>
@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

Comment thread src/libraries/Common/src/System/Security/Cryptography/CngHelpers.cs
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants