Skip to content

[ML-DSA]: Improve error message for Windows MLDsaImplementation when signing with missing private key #117112

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ private MLDsaImplementation(
[MemberNotNullWhen(true, nameof(s_algHandle))]
internal static partial bool SupportsAny() => s_algHandle is not null;

protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination)
{
if (!_hasSecretKey)
{
throw new CryptographicException(SR.Cryptography_MLDsaNoSecretKey);
}

Interop.BCrypt.BCryptSignHashPqcPure(_key, data, context, destination);
}

protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) =>
Interop.BCrypt.BCryptVerifySignaturePqcPure(_key, data, context, signature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,19 @@ public void MLDsaCng_DuplicateHandle(string? name)
key.Delete();
}
}

[Theory]
[MemberData(nameof(MLDsaTestsData.AllMLDsaAlgorithms), MemberType = typeof(MLDsaTestsData))]
public void SignData_WithPublicKey_ThrowsCryptographicException(MLDsaAlgorithm algorithm)
{
using MLDsa full = MLDsa.GenerateKey(algorithm);
using MLDsa pub = MLDsa.ImportSubjectPublicKeyInfo(full.ExportSubjectPublicKeyInfo());

byte[] data = new byte[1];
byte[] signature = new byte[algorithm.SignatureSizeInBytes];

CryptographicException ex = Assert.Throws<CryptographicException>(() => pub.SignData(data, signature));
Assert.Equal("The current instance does not contain a secret key.", ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,23 @@ public void RoundTrip_EncryptedPrivateKey(MLDsaKeyInfo info)
}, validPasswordTypes), validPasswordTypes);
}

[Theory]
[MemberData(nameof(MLDsaTestsData.AllMLDsaAlgorithms), MemberType = typeof(MLDsaTestsData))]
public static void SignData_WithPublicKey_ThrowsCryptographicException(MLDsaAlgorithm algorithm)
{
AssertThrowIfNotSupported(() =>
{
using MLDsa full = MLDsa.GenerateKey(algorithm);
using MLDsa pub = MLDsa.ImportSubjectPublicKeyInfo(full.ExportSubjectPublicKeyInfo());

byte[] data = new byte[1];
byte[] signature = new byte[algorithm.SignatureSizeInBytes];

CryptographicException ex = Assert.Throws<CryptographicException>(() => pub.SignData(data, signature));
Assert.Equal("The current instance does not contain a secret key.", ex.Message);
});
}

/// <summary>
/// Asserts that on platforms that do not support ML-DSA, the input test throws PlatformNotSupportedException.
/// If the test does pass, it implies that the test is validating code after the platform check.
Expand Down
Loading