-
Notifications
You must be signed in to change notification settings - Fork 5k
ML-KEM: Fix-up ASN.1 reading and writing logic #115066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This addresses a couple of validation deficiencies when loading ASN.1 content. This ensures invalid ASN.1 receives the proper exception and that SubjectPublicKeyInfo imports do not permit trailing data. For PKCS#8 export, we more reliably clear buffers and handle incorrectly implemented exports when the amount written is negative.
There was a problem hiding this 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 improves the ASN.1 reading and writing logic for MLKem by enhancing exception handling, validating trailing data, and improving the handling of private key exports.
- Improved exception types and messages for ASN.1 issues.
- Added tests for non-ASN.1 inputs and trailing data.
- Updated logic in MLKem key reader and private key export to validate key lengths and buffer handling.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/libraries/Common/tests/System/Security/Cryptography/MLKemTests.cs | Added tests for non-ASN.1 inputs and trailing data. |
src/libraries/Common/tests/System/Security/Cryptography/MLKemContractTests.cs | Renamed and added tests for misbehaving bytes-written scenarios in private key export. |
src/libraries/Common/src/System/Security/Cryptography/MLKem.cs | Updated ASN.1 trailing data validation and key reader logic and revised buffer handling in the private key export callback. |
Comments suppressed due to low confidence (1)
src/libraries/Common/src/System/Security/Cryptography/MLKem.cs:1823
- In ExportPkcs8PrivateKeyCallback, the initial allocation uses CryptoPool but the fallback allocation uses ArrayPool.Shared. For consistency and to avoid potential pool misuse, allocate all buffers using the same pool.
buffer = ArrayPool<byte>.Shared.Rent(size);
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones |
ICYMI:
|
runtime-ci run is here: https://github.com/vcsjones/runtime-ci/actions/runs/14720877862/job/41314348123 Only commit since that green run was a merge commit. |
This has a couple of improvements to how MLKem interacts with SubjectPublicKeyInfo and PrivateKeyInfo.
TryExportPkcs8PrivateKeyCore
Fixes #115063