diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaSignatureFormatTests.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaSignatureFormatTests.cs index 5451458f117a8..2370909f32863 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaSignatureFormatTests.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaSignatureFormatTests.cs @@ -331,7 +331,9 @@ public void Rfc23279TrySignHashUnderMax() ECDsa key = (ECDsa)keyDescription.Key; const DSASignatureFormat SignatureFormat = DSASignatureFormat.Rfc3279DerSequence; - const int RetryCount = 10; + // Make secp521r1 (7/16 chance of being smaller) and mod-8 keys (3/4 chance of being smaller) + // have the same 1-in-a-billion chance of failure. + int retryCount = keyDescription.FieldSizeInBits % 8 == 1 ? 36 : 15; byte[] hash = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; int expectedSize = GetExpectedSize(keyDescription.FieldSizeInBits); @@ -339,7 +341,7 @@ public void Rfc23279TrySignHashUnderMax() Assert.True(expectedSize < maxSize, "expectedSize < maxSize"); byte[] signature = new byte[expectedSize]; - for (int i = 0; i < RetryCount; i++) + for (int i = 0; i < retryCount; i++) { if (key.TrySignHash(hash, signature, SignatureFormat, out int written)) { @@ -359,7 +361,9 @@ public void Rfc23279TrySignDataUnderMax() ECDsa key = (ECDsa)keyDescription.Key; const DSASignatureFormat SignatureFormat = DSASignatureFormat.Rfc3279DerSequence; - const int RetryCount = 10; + // Make secp521r1 (7/16 chance of being smaller) and mod-8 keys (3/4 chance of being smaller) + // have the same 1-in-a-billion chance of failure. + int retryCount = keyDescription.FieldSizeInBits % 8 == 1 ? 36 : 15; HashAlgorithmName hashAlgorithm = HashAlgorithmName.SHA1; int expectedSize = GetExpectedSize(keyDescription.FieldSizeInBits); @@ -367,7 +371,7 @@ public void Rfc23279TrySignDataUnderMax() Assert.True(expectedSize < maxSize, "expectedSize < maxSize"); byte[] signature = new byte[expectedSize]; - for (int i = 0; i < RetryCount; i++) + for (int i = 0; i < retryCount; i++) { if (key.TrySignData(Array.Empty(), signature, hashAlgorithm, SignatureFormat, out int written)) {