Skip to content

Commit 6c254b0

Browse files
Fix ML-DSA PKCS8 copying bug (#117847)
1 parent ad12574 commit 6c254b0

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out
216216
}
217217

218218
bytesWritten = pkcs8.Count;
219-
pkcs8.Array.CopyTo(destination);
219+
pkcs8.AsSpan().CopyTo(destination);
220220
return true;
221221
}
222222
finally

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ public void NistImportPublicKeyVerify(MLDsaNistTestCase testCase)
222222
[MemberData(nameof(MLDsaTestsData.AllPreHashMLDsaNistTestCases), MemberType = typeof(MLDsaTestsData))]
223223
public void NistImportPublicKeyVerifyPreHash(MLDsaNistTestCase testCase)
224224
{
225+
if (!HashInfo.KnownHashAlgorithmOids.Contains(testCase.HashAlgOid))
226+
{
227+
// This test case is not supported by the current platform.
228+
return;
229+
}
230+
225231
byte[] hash = HashInfo.HashData(testCase.HashAlgOid, testCase.Message);
226232
using MLDsa mldsa = ImportPublicKey(testCase.Algorithm, testCase.PublicKey);
227233
Assert.Equal(testCase.ShouldPass, mldsa.VerifyPreHash(hash, testCase.Signature, testCase.HashAlgOid, testCase.Context));
@@ -317,7 +323,7 @@ public void SignData_PublicKeyOnlyThrows(MLDsaKeyInfo info)
317323
public void SignPreHash_ThrowsForUnsupportedAlgorithmCombinations(MLDsaAlgorithm algorithm, HashInfo hashInfo)
318324
{
319325
using MLDsa mldsa = GenerateKey(algorithm);
320-
byte[] hash = hashInfo.GetHash([1, 2, 3, 4]);
326+
byte[] hash = new byte[hashInfo.OutputSize];
321327

322328
CryptographicException ce = Assert.Throws<CryptographicException>(() => mldsa.SignPreHash(hash, hashInfo.Oid));
323329
Assert.Contains(algorithm.Name, ce.Message);

src/libraries/Common/tests/System/Security/Cryptography/HashInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public static IEnumerable<HashInfo> AllHashInfos()
101101
#endif
102102
}
103103

104+
internal static HashSet<string> KnownHashAlgorithmOids => field ??= AllHashInfos().Select(h => h.Oid).ToHashSet();
105+
104106
private HashInfo(string oid, int outputSize, HashAlgorithmName name)
105107
{
106108
Oid = oid;

0 commit comments

Comments
 (0)