Skip to content

Fix doubling of rental for PKCS#8 exports in PQC and X25519#128115

Merged
vcsjones merged 2 commits into
dotnet:mainfrom
vcsjones:fix-rent-doubling
May 13, 2026
Merged

Fix doubling of rental for PKCS#8 exports in PQC and X25519#128115
vcsjones merged 2 commits into
dotnet:mainfrom
vcsjones:fix-rent-doubling

Conversation

@vcsjones
Copy link
Copy Markdown
Member

@vcsjones vcsjones commented May 13, 2026

The MLKem, MLDsa, SlhDsa, and X25519DiffieHellman class had a small quirk with the way their PKCS#8 export work.

It would rent from the CryptoPool to create a buffer to populate, based on size. Rent is only guaranteed to return a buffer that is at least large enough.

If the TryExportPkcs8PrivateKeyCore implementation returned false, indicating the buffer was too small, we would double the size and rent again. However, we double based on how much we last asked for, not how much we actually got back.

For example, CryptoPool.Rent would ask for 2592 bytes for ML-DSA-44, and sometimes CryptoPool would give it back an array that is 8192 bytes in size. The Core implementation would return false, indicating that the buffer is too small. The loop would then double it to 5184, and get 8192 back again. So the TryExportPkcs8PrivateKeyCore would be called with the same size multiple times in a row.

A unit test ExportPkcs8PrivateKey_Resizes exists to ensure that does not happen and that subsequent calls to TryExportPkcs8PrivateKeyCore are always given a larger buffer on the next call - and that test would sometimes fail.

Interestingly, CompositeMLDsa already has the fix

So it needed no changes.

Fixes #128110

Copilot AI review requested due to automatic review settings May 13, 2026 03:08
@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 updates the PKCS#8 private key export retry loop for PQC key types so that when a rented buffer is still too small, the next retry request is based on the actual rented buffer length (not the previously requested size). This ensures each subsequent TryExportPkcs8PrivateKeyCore attempt is made with a strictly larger buffer, preventing repeated calls with the same buffer size when CryptoPool.Rent returns an oversized array.

Changes:

  • In MLKem, MLDsa, and SlhDsa, update the resize loop to set size = buffer.Length before doubling and re-renting.
  • Align behavior with the already-correct pattern used in CompositeMLDsa.
Show a summary per file
File Description
src/libraries/Common/src/System/Security/Cryptography/SlhDsa.cs Fix PKCS#8 export buffer growth to double based on the actual rented buffer length.
src/libraries/Common/src/System/Security/Cryptography/MLKem.cs Same buffer growth fix for ML-KEM PKCS#8 exports.
src/libraries/Common/src/System/Security/Cryptography/MLDsa.cs Same buffer growth fix for ML-DSA PKCS#8 exports.

Copilot's findings

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

Comment thread src/libraries/Common/src/System/Security/Cryptography/MLKem.cs
@vcsjones vcsjones changed the title Fix doubling of rental for PKCS#8 exports in PQC Fix doubling of rental for PKCS#8 exports in PQC and X25519 May 13, 2026
@vcsjones vcsjones merged commit 9bb2c58 into dotnet:main May 13, 2026
95 checks passed
@vcsjones vcsjones deleted the fix-rent-doubling branch May 13, 2026 19:35
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.

[ci-scan] Known Build Error: MLDsaTests.ExportPkcs8PrivateKey_Resizes boundary assertion failure

3 participants