From 10630873ebaa42381c5871e20336fbfb46564ac8 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 11 Oct 2014 13:47:32 +1100 Subject: [PATCH] tests: add tests for ecdsa.verify --- src/ecdsa.js | 16 ++++++++-------- test/ecdsa.js | 10 +++++++--- test/fixtures/ecdsa.json | 18 +++++++++--------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index 7b9db0b..75450dc 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -76,14 +76,6 @@ function sign(curve, hash, d) { 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) { var n = curve.n var G = curve.G @@ -114,6 +106,14 @@ function verifyRaw(curve, e, signature, Q) { 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. * diff --git a/test/ecdsa.js b/test/ecdsa.js index 84c0b44..58a0076 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -115,31 +115,35 @@ describe('ecdsa', function() { }) }) - describe('verifyRaw', function() { + describe('verify/verifyRaw', function() { fixtures.valid.forEach(function(f) { it('verifies a valid signature for \"' + f.message + '\"', function() { 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( new BigInteger(f.signature.r), new BigInteger(f.signature.s) ) var Q = curve.G.multiply(d) + assert(ecdsa.verify(curve, H, signature, Q)) assert(ecdsa.verifyRaw(curve, e, signature, Q)) }) }) fixtures.invalid.verifyRaw.forEach(function(f) { 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 e = BigInteger.fromHex(f.e) var signature = new ECSignature( new BigInteger(f.signature.r), new BigInteger(f.signature.s) ) var Q = curve.G.multiply(d) + assert.equal(ecdsa.verify(curve, H, signature, Q), false) assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false) }) }) diff --git a/test/fixtures/ecdsa.json b/test/fixtures/ecdsa.json index ea53d7c..faad864 100644 --- a/test/fixtures/ecdsa.json +++ b/test/fixtures/ecdsa.json @@ -148,7 +148,7 @@ { "description": "The wrong signature", "d": "01", - "e": "06ef2b193b83b3d701f765f1db34672ab84897e1252343cc2197829af3a30456", + "message": "foo", "signature": { "r": "38341707918488238920692284707283974715538935465589664377561695343399725051885", "s": "3180566392414476763164587487324397066658063772201694230600609996154610926757" @@ -157,7 +157,7 @@ { "description": "Invalid r value (< 0)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "-01", "s": "02" @@ -166,7 +166,7 @@ { "description": "Invalid r value (== 0)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "00", "s": "02" @@ -175,7 +175,7 @@ { "description": "Invalid r value (>= n)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", "s": "02" @@ -184,7 +184,7 @@ { "description": "Invalid s value (< 0)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "02", "s": "-01" @@ -193,7 +193,7 @@ { "description": "Invalid s value (== 0)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "02", "s": "00" @@ -202,7 +202,7 @@ { "description": "Invalid s value (>= n)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "02", "s": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141" @@ -211,7 +211,7 @@ { "description": "Invalid r, s values (r = s = -n)", "d": "01", - "e": "01", + "message": "foo", "signature": { "r": "-115792089237316195423570985008687907852837564279074904382605163141518161494337", "s": "-115792089237316195423570985008687907852837564279074904382605163141518161494337" @@ -219,4 +219,4 @@ } ] } -} +} \ No newline at end of file