Skip to content

Commit

Permalink
Add a test for ECDSA signatures at zero
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored Apr 26, 2022
1 parent fb61718 commit c101540
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,62 @@ public void PublicKey_CannotSign()
() => SignData(ecdsa, new byte[] { 1, 2, 3, 4, 5 }, HashAlgorithmName.SHA256));
}
}

[Theory]
[MemberData(nameof(TestCurves))]
public void SignaturesAtZeroDoNotVerify_IEEEP1363(CurveDef curveDef)
{
using (ECDsa ec = ECDsaFactory.Create(curveDef.Curve))
{
byte[] data = new byte[] { 1, 2, 3, 4 };
byte[] signature = ec.SignData(data, HashAlgorithmName.SHA256, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);

// Verify it now.
bool verified = ec.VerifyData(
data,
signature,
HashAlgorithmName.SHA256,
DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
Assert.True(verified, nameof(ec.VerifyData));

// Since the signature is fixed field, create a zero signature just by zeroing it out.
// The important thing is that it is the right length.
Array.Clear(signature);

verified = ec.VerifyData(
data,
signature,
HashAlgorithmName.SHA256,
DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
Assert.False(verified, nameof(ec.VerifyData));
}
}

[Theory]
[MemberData(nameof(TestCurves))]
public void SignaturesAtZeroDoNotVerify_DER(CurveDef curveDef)
{
using (ECDsa ec = ECDsaFactory.Create(curveDef.Curve))
{
byte[] data = new byte[] { 1, 2, 3, 4 };

// ASN.1:
// SEQUENCE {
// INTEGER 0,
// INTEGER 0
// }
byte[] zeroSignature = new byte[]
{
0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00
};

bool verified = ec.VerifyData(
data,
zeroSignature,
HashAlgorithmName.SHA256,
DSASignatureFormat.Rfc3279DerSequence);
Assert.False(verified, nameof(ec.VerifyData));
}
}
}
}

0 comments on commit c101540

Please sign in to comment.