From 6cfa729dae4699fb68bac754e749fdd54646324d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 29 May 2014 15:42:12 +1000 Subject: [PATCH] ecdsa: fix missing exceptions --- src/ecdsa.js | 9 ++++++--- test/ec.js | 2 +- test/ecdsa.js | 20 ++++++++++---------- test/fixtures/ecdsa.json | 28 ++++++++++++---------------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index 2f3c11b..ee0dcde 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -119,14 +119,17 @@ function parseSig(buffer) { assert.equal(buffer.readUInt8(0), 0x30, 'Not a DER sequence') assert.equal(buffer.readUInt8(1), buffer.length - 2, 'Invalid sequence length') - assert.equal(buffer.readUInt8(2), 0x02, 'Expected DER integer') + assert.equal(buffer.readUInt8(2), 0x02, 'Expected a DER integer') var rLen = buffer.readUInt8(3) var rB = buffer.slice(4, 4 + rLen) var offset = 4 + rLen - assert.equal(buffer.readUInt8(offset), 0x02, 'Expected a 2nd DER integer') + assert.equal(buffer.readUInt8(offset), 0x02, 'Expected a DER integer (2)') var sLen = buffer.readUInt8(1 + offset) var sB = buffer.slice(2 + offset) + offset += 2 + sLen + + assert.equal(offset, buffer.length, 'Invalid DER encoding') return { r: BigInteger.fromDERInteger(rB), @@ -155,7 +158,7 @@ function parseSigCompact(buffer) { var i = buffer.readUInt8(0) - 27 // At most 3 bits - assert.equal(i, i & 7, 'Invalid signature type') + assert.equal(i, i & 7, 'Invalid signature parameter') var compressed = !!(i & 4) // Recovery param only diff --git a/test/ec.js b/test/ec.js index a166bde..9c53eef 100644 --- a/test/ec.js +++ b/test/ec.js @@ -61,7 +61,7 @@ describe('ec', function() { assert.throws(function() { ECPointFp.decodeFrom(curve, buffer) - }) + }, /Invalid sequence length|Invalid sequence tag/) }) }) diff --git a/test/ecdsa.js b/test/ecdsa.js index ca86af4..0c66c69 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -118,19 +118,19 @@ describe('ecdsa', function() { }) fixtures.invalid.DER.forEach(function(f) { - it('throws on ' + f.description, function() { - var buffer = new Buffer(f.hex) + it('throws on ' + f.hex, function() { + var buffer = new Buffer(f.hex, 'hex') assert.throws(function() { ecdsa.parseSig(buffer) - }) + }, new RegExp(f.exception)) }) }) }) describe('serializeSigCompact', function() { - it('encodes a compact signature', function() { - fixtures.valid.forEach(function(f) { + fixtures.valid.forEach(function(f) { + it('encodes ' + f.compact.hex + ' correctly', function() { var signature = { r: new BigInteger(f.signature.r), s: new BigInteger(f.signature.s) @@ -145,8 +145,8 @@ describe('ecdsa', function() { }) describe('parseSigCompact', function() { - it('decodes the correct signature', function() { - fixtures.valid.forEach(function(f) { + fixtures.valid.forEach(function(f) { + it('decodes ' + f.compact.hex + ' correctly', function() { var buffer = new Buffer(f.compact.hex, 'hex') var parsed = ecdsa.parseSigCompact(buffer) @@ -158,12 +158,12 @@ describe('ecdsa', function() { }) fixtures.invalid.compact.forEach(function(f) { - it('throws on ' + f.description, function() { - var buffer = new Buffer(f.hex) + it('throws on ' + f.hex, function() { + var buffer = new Buffer(f.hex, 'hex') assert.throws(function() { ecdsa.parseSigCompact(buffer) - }) + }, new RegExp(f.exception)) }) }) }) diff --git a/test/fixtures/ecdsa.json b/test/fixtures/ecdsa.json index ce56f9f..e3ff799 100644 --- a/test/fixtures/ecdsa.json +++ b/test/fixtures/ecdsa.json @@ -109,42 +109,38 @@ "invalid": { "compact": [ { - "description": "Invalid signature parameters", + "exception": "Invalid signature parameter", "hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62" }, { - "description": "Signature too long", + "exception": "Invalid signature length", "hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000" }, { - "description": "Signature too short", + "exception": "Invalid signature length", "hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379" } ], "DER": [ { - "description": "Invalid sequence length", + "exception": "Invalid sequence length", "hex": "30ff0204ffffffff0204ffffffff" }, { - "description": "Invalid integer(s) length", - "hex": "30080201ffffffff0201ffffffff" + "exception": "Invalid sequence length", + "hex": "300c0304ffffffff0304ffffffff0000" }, { - "description": "Invalid sequence tag", - "hex": "ff080204ffffffff0204ffffffff" + "exception": "Expected a DER integer", + "hex": "300cff04ffffffff0204ffffffff" }, { - "description": "Invalid integer tag", - "hex": "30080304ffffffff0304ffffffff" + "exception": "Expected a DER integer \\(2\\)", + "hex": "300c0202ffffffff0204ffffffff" }, { - "description": "Sequence too short", - "hex": "30080304ffffffff0304ff" - }, - { - "description": "Sequence too long", - "hex": "30080304ffffffff0304ffffffffffffff" + "exception": "Invalid DER encoding", + "hex": "300c0204ffffffff0202ffffffff" } ], "verifyRaw": [