tests: add tests for ecdsa.verify

This commit is contained in:
Daniel Cousens 2014-10-11 13:47:32 +11:00
parent 98bc1685b3
commit 10630873eb
3 changed files with 24 additions and 20 deletions

View file

@ -76,14 +76,6 @@ function sign(curve, hash, d) {
return new ECSignature(r, s) return new ECSignature(r, s)
} }
function verify(curve, hash, signature, Q) {
// 1.4.2 H = Hash(M), already done by the user
// 1.4.3 e = H
var e = BigInteger.fromBuffer(hash)
return verifyRaw(curve, e, signature, Q)
}
function verifyRaw(curve, e, signature, Q) { function verifyRaw(curve, e, signature, Q) {
var n = curve.n var n = curve.n
var G = curve.G var G = curve.G
@ -114,6 +106,14 @@ function verifyRaw(curve, e, signature, Q) {
return v.equals(r) return v.equals(r)
} }
function verify(curve, hash, signature, Q) {
// 1.4.2 H = Hash(M), already done by the user
// 1.4.3 e = H
var e = BigInteger.fromBuffer(hash)
return verifyRaw(curve, e, signature, Q)
}
/** /**
* Recover a public key from a signature. * Recover a public key from a signature.
* *

View file

@ -115,31 +115,35 @@ describe('ecdsa', function() {
}) })
}) })
describe('verifyRaw', function() { describe('verify/verifyRaw', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('verifies a valid signature for \"' + f.message + '\"', function() { it('verifies a valid signature for \"' + f.message + '\"', function() {
var d = BigInteger.fromHex(f.d) var d = BigInteger.fromHex(f.d)
var e = BigInteger.fromBuffer(crypto.sha256(f.message)) var H = crypto.sha256(f.message)
var e = BigInteger.fromBuffer(H)
var signature = new ECSignature( var signature = new ECSignature(
new BigInteger(f.signature.r), new BigInteger(f.signature.r),
new BigInteger(f.signature.s) new BigInteger(f.signature.s)
) )
var Q = curve.G.multiply(d) var Q = curve.G.multiply(d)
assert(ecdsa.verify(curve, H, signature, Q))
assert(ecdsa.verifyRaw(curve, e, signature, Q)) assert(ecdsa.verifyRaw(curve, e, signature, Q))
}) })
}) })
fixtures.invalid.verifyRaw.forEach(function(f) { fixtures.invalid.verifyRaw.forEach(function(f) {
it('fails to verify with ' + f.description, function() { it('fails to verify with ' + f.description, function() {
var H = crypto.sha256(f.message)
var e = BigInteger.fromBuffer(H)
var d = BigInteger.fromHex(f.d) var d = BigInteger.fromHex(f.d)
var e = BigInteger.fromHex(f.e)
var signature = new ECSignature( var signature = new ECSignature(
new BigInteger(f.signature.r), new BigInteger(f.signature.r),
new BigInteger(f.signature.s) new BigInteger(f.signature.s)
) )
var Q = curve.G.multiply(d) var Q = curve.G.multiply(d)
assert.equal(ecdsa.verify(curve, H, signature, Q), false)
assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false) assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false)
}) })
}) })

View file

@ -148,7 +148,7 @@
{ {
"description": "The wrong signature", "description": "The wrong signature",
"d": "01", "d": "01",
"e": "06ef2b193b83b3d701f765f1db34672ab84897e1252343cc2197829af3a30456", "message": "foo",
"signature": { "signature": {
"r": "38341707918488238920692284707283974715538935465589664377561695343399725051885", "r": "38341707918488238920692284707283974715538935465589664377561695343399725051885",
"s": "3180566392414476763164587487324397066658063772201694230600609996154610926757" "s": "3180566392414476763164587487324397066658063772201694230600609996154610926757"
@ -157,7 +157,7 @@
{ {
"description": "Invalid r value (< 0)", "description": "Invalid r value (< 0)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "-01", "r": "-01",
"s": "02" "s": "02"
@ -166,7 +166,7 @@
{ {
"description": "Invalid r value (== 0)", "description": "Invalid r value (== 0)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "00", "r": "00",
"s": "02" "s": "02"
@ -175,7 +175,7 @@
{ {
"description": "Invalid r value (>= n)", "description": "Invalid r value (>= n)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", "r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
"s": "02" "s": "02"
@ -184,7 +184,7 @@
{ {
"description": "Invalid s value (< 0)", "description": "Invalid s value (< 0)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "02", "r": "02",
"s": "-01" "s": "-01"
@ -193,7 +193,7 @@
{ {
"description": "Invalid s value (== 0)", "description": "Invalid s value (== 0)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "02", "r": "02",
"s": "00" "s": "00"
@ -202,7 +202,7 @@
{ {
"description": "Invalid s value (>= n)", "description": "Invalid s value (>= n)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "02", "r": "02",
"s": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141" "s": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"
@ -211,7 +211,7 @@
{ {
"description": "Invalid r, s values (r = s = -n)", "description": "Invalid r, s values (r = s = -n)",
"d": "01", "d": "01",
"e": "01", "message": "foo",
"signature": { "signature": {
"r": "-115792089237316195423570985008687907852837564279074904382605163141518161494337", "r": "-115792089237316195423570985008687907852837564279074904382605163141518161494337",
"s": "-115792089237316195423570985008687907852837564279074904382605163141518161494337" "s": "-115792089237316195423570985008687907852837564279074904382605163141518161494337"
@ -219,4 +219,4 @@
} }
] ]
} }
} }