These two function should be equivalent but VerifyBouncyCastle()
returns false (failed verification) for the same input that VerifyDotNet()
returns true on. The message was signed with .Net's DSACryptoServiceProvider.SignHash()
. What's wrong with VerifyBouncyCastle()
that makes it behave differently from the .Net version?
.Net built in crypto library:
Private Function VerifyDotNet(hash() As Byte, p() As Byte, q() As Byte, g() As Byte, y() As Byte, r() As Byte, s() As Byte) As Boolean
'Public key
Dim dotNetDSA As New DSACryptoServiceProvider()
Dim dotNetParameters As New DSAParameters
dotNetParameters.P = p
dotNetParameters.Q = q
dotNetParameters.G = g
dotNetParameters.Y = y
dotNetDSA.ImportParameters(dotNetParameters)
'Signature
Dim signature(39) As Byte
Array.ConstrainedCopy(r, 0, signature, 0, 20)
Array.ConstrainedCopy(s, 0, signature, 20, 20)
'Verify
Return dotNetDSA.VerifyHash(hash, "SHA1", signature)
End Function
BouncyCastle:
Private Function VerifyBouncyCastle(hash() As Byte, p() As Byte, q() As Byte, g() As Byte, y() As Byte, r() As Byte, s() As Byte) As Boolean
'Public key
Dim pBigInt As New Org.BouncyCastle.Math.BigInteger(p)
Dim qBigInt As New Org.BouncyCastle.Math.BigInteger(q)
Dim gBigInt As New Org.BouncyCastle.Math.BigInteger(g)
Dim yBigInt As New Org.BouncyCastle.Math.BigInteger(y)
Dim bouncyParameters As New Org.BouncyCastle.Crypto.Parameters.DsaParameters(pBigInt, qBigInt, gBigInt)
Dim bouncyPublicKey As New Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters(yBigInt, bouncyParameters)
Dim bouncyDSA As New Org.BouncyCastle.Crypto.Signers.DsaSigner
bouncyDSA.Init(False, bouncyPublicKey)
'Signature
Dim signatureR As New Org.BouncyCastle.Math.BigInteger(r)
Dim signatureS As New Org.BouncyCastle.Math.BigInteger(s)
'Verify
Return bouncyDSA.VerifySignature(hash, signatureR, signatureS)
End Function
I've tried reversing the byte arrays, and also verified that BouncyCastle's Sha1Digest.DoFinal()
generates the same hash for the same message as .Net's SHA1Managed.ComputeHash()
which was used to generate the hash for signing.