Skip to content

Commit 5545d52

Browse files
authored
Fix MLDsaImplementation on Windows to throw a more clear exception and add a test
1 parent a131d0b commit 5545d52

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/libraries/Common/src/System/Security/Cryptography/MLDsaImplementation.Windows.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ private MLDsaImplementation(
3434
[MemberNotNullWhen(true, nameof(s_algHandle))]
3535
internal static partial bool SupportsAny() => s_algHandle is not null;
3636

37-
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>
37+
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination)
38+
{
39+
if (!_hasSecretKey)
40+
{
41+
throw new CryptographicException(SR.Cryptography_MLDsaNoSecretKey);
42+
}
43+
3844
Interop.BCrypt.BCryptSignHashPqcPure(_key, data, context, destination);
45+
}
3946

4047
protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) =>
4148
Interop.BCrypt.BCryptVerifySignaturePqcPure(_key, data, context, signature);

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/MLDsa/MLDsaTestsBase.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ public void ImportPrivateSeed_Export(MLDsaKeyInfo info)
279279
AssertExtensions.SequenceEqual(info.PrivateSeed, export(mldsa)));
280280
}
281281

282+
[Theory]
283+
[MemberData(nameof(MLDsaTestsData.IetfMLDsaAlgorithms), MemberType = typeof(MLDsaTestsData))]
284+
public void SignData_PublicKeyOnlyThrows(MLDsaKeyInfo info)
285+
{
286+
using MLDsa mldsa = ImportPublicKey(info.Algorithm, info.PublicKey);
287+
byte[] destination = new byte[info.Algorithm.SignatureSizeInBytes];
288+
CryptographicException ce =
289+
Assert.ThrowsAny<CryptographicException>(() => mldsa.SignData("hello"u8, destination));
290+
291+
Assert.DoesNotContain("unknown", ce.Message, StringComparison.OrdinalIgnoreCase);
292+
}
293+
282294
protected static void ExerciseSuccessfulVerify(MLDsa mldsa, byte[] data, byte[] signature, byte[] context)
283295
{
284296
ReadOnlySpan<byte> buffer = [0, 1, 2, 3];

0 commit comments

Comments
 (0)