-
Notifications
You must be signed in to change notification settings - Fork 5.1k
ML-DSA add byte array APIs #117044
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
base: main
Are you sure you want to change the base?
ML-DSA add byte array APIs #117044
Conversation
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 adds new byte[]-based overloads for ML-DSA export and signing APIs and updates existing code/tests to adopt these simpler overloads, replacing manual buffer management.
- Introduces
byte[] ExportMLDsaPublicKey()
,ExportMLDsaSecretKey()
,ExportMLDsaPrivateSeed()
,SignData(byte[], byte[]?)
andVerifyData(byte[], byte[], byte[]?)
overloads. - Updates all X509, PFX, COSE, OpenSSL, and common tests to use the new overloads and removes manual buffer length assertions.
- Extends the reference assembly (
ref/System.Security.Cryptography.cs
) and implementation (MLDsa.cs
,MLDsaImplementation.cs
,X509Certificate2.cs
,MLDsaX509SignatureGenerator.cs
) to support these overloads.
Reviewed Changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
src/libraries/System.Security.Cryptography/tests/X509Certificates/X509Certificate2PemTests.cs | Use new SignData(data) overload instead of manual buffer |
src/libraries/System.Security.Cryptography/tests/X509Certificates/PfxTests.cs | Switch to ExportMLDsaPrivateSeed() /ExportMLDsaSecretKey() overloads |
src/libraries/System.Security.Cryptography/tests/X509Certificates/ExportTests.cs | Use ExportMLDsaSecretKey() overload |
src/libraries/System.Security.Cryptography/tests/X509Certificates/CertificateCreation/PrivateKeyAssociationTests.cs | Use ExportMLDsaPublicKey() and SignData(data) overloads |
src/libraries/System.Security.Cryptography/tests/X509Certificates/CertTests.cs | Use ExportMLDsaSecretKey() overload |
src/libraries/System.Security.Cryptography/tests/MLDsaOpenSslTests.Unix.cs | Use SignData(data, context) and ExportMLDsa*() overloads |
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs | Simplify public key comparison using new ExportMLDsaPublicKey() |
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/MLDsaX509SignatureGenerator.cs | Return SignData(data) directly |
src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs | Add and reorder new byte[] overloads for export and signing APIs |
src/libraries/System.Security.Cryptography.Cose/tests/TestKeyRing.cs | Add centralized helper for ECDsa/RSA/MLDsa test keys |
src/libraries/System.Security.Cryptography.Cose/tests/System.Security.Cryptography.Cose.Tests.csproj | Include new TestKeyRing.cs in test project |
src/libraries/System.Security.Cryptography.Cose/tests/CoseTestHelpers.cs | Delegate to TestKeyRing and remove thread-static duplications |
src/libraries/Common/tests/.../MLDsa/MLDsaTestsBase.cs | Update tests to use SignData overloads |
src/libraries/Common/tests/.../MLDsa/MLDsaTests.cs | Add null-argument validation for new overloads |
src/libraries/Common/tests/.../MLDsa/MLDsaTestHelpers.cs | Cover default ReadOnlySpan<byte> in import tests |
src/libraries/Common/tests/.../MLDsa/MLDsaImplementationTests.cs | Add tests for null byte[] source imports |
src/libraries/Common/src/System/Security/Cryptography/SlhDsaImplementation.cs | Minor formatting around secret-key export |
src/libraries/Common/src/System/Security/Cryptography/MLDsaImplementation.cs | Adapt DuplicatePrivateKey to new export APIs |
src/libraries/Common/src/System/Security/Cryptography/MLDsa.cs | Implement new byte[] overloads and strengthen span overload checks |
Comments suppressed due to low confidence (1)
src/libraries/System.Security.Cryptography.Cose/tests/TestKeyRing.cs:23
- The property references an undeclared backing variable 'field', which will not compile. You should declare a private ECDsa? field for each property (e.g. '_es256') or use auto-properties with initialization.
internal ECDsa ES256 => field ??= CreateECDsa(_ec256Parameters, true);
...libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseKey.cs
Show resolved
Hide resolved
...ryptography/src/System/Security/Cryptography/X509Certificates/MLDsaX509SignatureGenerator.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Security.Cryptography/tests/MLDsaOpenSslTests.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Security.Cryptography/tests/MLDsaOpenSslTests.Unix.cs
Outdated
Show resolved
Hide resolved
} | ||
finally | ||
{ | ||
CryptoPool.Return(rented, written); | ||
CryptoPool.Return(rented); |
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.
Consider changing the try/finally to using (PinAndClear.Track(rented)) { }
.
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.
I'm not certain what the benefit would be from pinning here - if we do it we should probably do it for all algorithms, not just here
testDirectCall(() => MLDsa.ImportMLDsaPublicKey(algorithm, ReadOnlySpan<byte>.Empty)); | ||
testDirectCall(() => MLDsa.ImportMLDsaPublicKey(algorithm, default(ReadOnlySpan<byte>))); |
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.
In .NET 10 JIT produces a 0 length null pointer for both ReadOnlySpan<byte>.Empty
and default(ReadOnlySpan<byte>)
so I don't think both are needed (unless it does something different downlevel).
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.
This is a really cheap test and we we also have M.B.C which should also respect this, not just .NET 10 and higher
...aries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/MLDsa/MLDsaTests.cs
Outdated
Show resolved
Hide resolved
...ryptography/src/System/Security/Cryptography/X509Certificates/MLDsaX509SignatureGenerator.cs
Outdated
Show resolved
Hide resolved
{ | ||
byte[] seed = new byte[mldsa.Algorithm.PrivateSeedSizeInBytes]; | ||
Assert.Equal(mldsa.Algorithm.PrivateSeedSizeInBytes, mldsa.ExportMLDsaPrivateSeed(seed)); // does not throw | ||
byte[] seed = mldsa.ExportMLDsaPrivateSeed(); | ||
Assert.Equal(mldsa.Algorithm.PrivateSeedSizeInBytes, seed.Length); // does not throw | ||
|
||
byte[] secretKey = new byte[mldsa.Algorithm.SecretKeySizeInBytes]; | ||
Assert.Equal(mldsa.Algorithm.SecretKeySizeInBytes, mldsa.ExportMLDsaSecretKey(secretKey)); // does not throw | ||
byte[] secretKey = mldsa.ExportMLDsaSecretKey(); | ||
Assert.Equal(mldsa.Algorithm.SecretKeySizeInBytes, secretKey.Length); // does not throw | ||
|
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.
The "Does not throw" comments feel misplaced to me. Assuming they are commenting the line that they are the end of, the only possible "throw" would be that seed.Length
threw ANE.
They should either be somewhere that makes sense, or just deleted.
This also addresses optional feedback from my last COSE PR.