Skip to content

X25519DiffieHellman agreement with byte-based public keys#128086

Open
vcsjones wants to merge 6 commits into
dotnet:mainfrom
vcsjones:x25519-agree-with-bytes
Open

X25519DiffieHellman agreement with byte-based public keys#128086
vcsjones wants to merge 6 commits into
dotnet:mainfrom
vcsjones:x25519-agree-with-bytes

Conversation

@vcsjones
Copy link
Copy Markdown
Member

Contributes to #128040.

This introduces a DeriveRawSecretAgreement that does key agreement with the other party's key as a sequence of bytes instead of forcing creating the instance of X25519DiffieHellman. This reduces allocations in the typical use case of X25519.

Copilot AI review requested due to automatic review settings May 12, 2026 16:52
@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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 extends X25519DiffieHellman with byte-based public-key key agreement APIs and wires them through the platform implementations (OpenSSL/Apple/Windows/CNG), adding native shim entrypoints where needed, plus expanded contract and functional test coverage.

Changes:

  • Add DeriveRawSecretAgreement(byte[] otherPartyPublicKey) and DeriveRawSecretAgreement(ReadOnlySpan<byte> otherPartyPublicKey, Span<byte> destination) along with a new abstract DeriveRawSecretAgreementCore(ReadOnlySpan<byte>, Span<byte>).
  • Implement the new core overload across OpenSSL/Apple/Windows/CNG implementations and add new native entrypoints for OpenSSL and Apple to derive directly from raw public-key bytes.
  • Add/extend tests validating argument checks, disposal behavior, symmetry, vectors, and zero-shared-secret behavior for the new overloads.
Show a summary per file
File Description
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellman.cs Adds the new public overloads and new abstract core method plus length validation helper.
src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs Updates the public ref surface to include the new overloads and abstract core method.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.OpenSsl.cs Implements the new core overload and switches fallback to the new raw-public-key interop.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanOpenSsl.OpenSsl.cs Same as above for the public OpenSSL-backed type.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.Apple.cs Implements the new core overload and routes to the new Apple bytes-based interop.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.Windows.cs Implements the new core overload and refactors shared Windows derivation path.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.Windows.cs Implements the new core overload and factors shared public-key handling.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanCng.cs Declares the new protected override in the partial type.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanImplementation.NotSupported.cs Adds the new core overload stub for unsupported platforms.
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X25519DiffieHellmanOpenSsl.NotSupported.cs Adds the new core overload stub (currently throws NotImplementedException).
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Cng.NotSupported.cs Adds the new core overload stub in the CNG PNSE implementation.
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.X25519.cs Adds interop for the new OpenSSL native entrypoint deriving from raw public-key bytes.
src/native/libs/System.Security.Cryptography.Native/pal_evp_pkey_x25519.h Declares CryptoNative_X25519DeriveSecretAgreementWithPublicKey.
src/native/libs/System.Security.Cryptography.Native/pal_evp_pkey_x25519.c Implements the new OpenSSL helper by importing the peer key and deriving.
src/native/libs/System.Security.Cryptography.Native/entrypoints.c Exposes the new OpenSSL entrypoint via the resolver table.
src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.X25519.cs Adds interop for the new Apple bytes-based derivation export.
src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.swift Implements AppleCryptoNative_X25519DeriveRawSecretAgreementWithBytes and shares logic via a helper.
src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.h Declares the new Apple export.
src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c Exposes the new Apple export via the resolver table.
src/libraries/System.Security.Cryptography/tests/X25519DiffieHellmanContractTests.cs Adds contract tests for the new overloads and adds callback plumbing for the new core method.
src/libraries/System.Security.Cryptography/tests/X25519DiffieHellmanBaseTests.cs Adds functional tests covering new overloads (symmetry, vectors, public-key-only, zero-secret).

Copilot's findings

  • Files reviewed: 21/21 changed files
  • Comments generated: 3

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 12, 2026 17:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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: 21/21 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.

2 participants