Fix doubling of rental for PKCS#8 exports in PQC#128115
Open
vcsjones wants to merge 1 commit into
Open
Conversation
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 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, andSlhDsa, update the resize loop to setsize = buffer.Lengthbefore 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
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
MLKem,MLDsa, andSlhDsahad 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.Rentis only guaranteed to return a buffer that is at least large enough.If the
TryExportPkcs8PrivateKeyCoreimplementation returnedfalse, 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
TryExportPkcs8PrivateKeyCorewould be called with the same size multiple times in a row.A unit test
ExportPkcs8PrivateKey_Resizesexists 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,
CompositeMLDsaalready has the fixruntime/src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsa.cs
Line 1890 in 214a9a4
So it needed no changes.
Fixes #128110