Browse Source

ecdsa: fix missing exceptions

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
6cfa729dae
  1. 9
      src/ecdsa.js
  2. 2
      test/ec.js
  3. 20
      test/ecdsa.js
  4. 28
      test/fixtures/ecdsa.json

9
src/ecdsa.js

@ -119,14 +119,17 @@ function parseSig(buffer) {
assert.equal(buffer.readUInt8(0), 0x30, 'Not a DER sequence') 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(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 rLen = buffer.readUInt8(3)
var rB = buffer.slice(4, 4 + rLen) var rB = buffer.slice(4, 4 + rLen)
var offset = 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 sLen = buffer.readUInt8(1 + offset)
var sB = buffer.slice(2 + offset) var sB = buffer.slice(2 + offset)
offset += 2 + sLen
assert.equal(offset, buffer.length, 'Invalid DER encoding')
return { return {
r: BigInteger.fromDERInteger(rB), r: BigInteger.fromDERInteger(rB),
@ -155,7 +158,7 @@ function parseSigCompact(buffer) {
var i = buffer.readUInt8(0) - 27 var i = buffer.readUInt8(0) - 27
// At most 3 bits // At most 3 bits
assert.equal(i, i & 7, 'Invalid signature type') assert.equal(i, i & 7, 'Invalid signature parameter')
var compressed = !!(i & 4) var compressed = !!(i & 4)
// Recovery param only // Recovery param only

2
test/ec.js

@ -61,7 +61,7 @@ describe('ec', function() {
assert.throws(function() { assert.throws(function() {
ECPointFp.decodeFrom(curve, buffer) ECPointFp.decodeFrom(curve, buffer)
}) }, /Invalid sequence length|Invalid sequence tag/)
}) })
}) })

20
test/ecdsa.js

@ -118,19 +118,19 @@ describe('ecdsa', function() {
}) })
fixtures.invalid.DER.forEach(function(f) { fixtures.invalid.DER.forEach(function(f) {
it('throws on ' + f.description, function() { it('throws on ' + f.hex, function() {
var buffer = new Buffer(f.hex) var buffer = new Buffer(f.hex, 'hex')
assert.throws(function() { assert.throws(function() {
ecdsa.parseSig(buffer) ecdsa.parseSig(buffer)
}) }, new RegExp(f.exception))
}) })
}) })
}) })
describe('serializeSigCompact', function() { 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 = { var signature = {
r: new BigInteger(f.signature.r), r: new BigInteger(f.signature.r),
s: new BigInteger(f.signature.s) s: new BigInteger(f.signature.s)
@ -145,8 +145,8 @@ describe('ecdsa', function() {
}) })
describe('parseSigCompact', 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 buffer = new Buffer(f.compact.hex, 'hex')
var parsed = ecdsa.parseSigCompact(buffer) var parsed = ecdsa.parseSigCompact(buffer)
@ -158,12 +158,12 @@ describe('ecdsa', function() {
}) })
fixtures.invalid.compact.forEach(function(f) { fixtures.invalid.compact.forEach(function(f) {
it('throws on ' + f.description, function() { it('throws on ' + f.hex, function() {
var buffer = new Buffer(f.hex) var buffer = new Buffer(f.hex, 'hex')
assert.throws(function() { assert.throws(function() {
ecdsa.parseSigCompact(buffer) ecdsa.parseSigCompact(buffer)
}) }, new RegExp(f.exception))
}) })
}) })
}) })

28
test/fixtures/ecdsa.json

@ -109,42 +109,38 @@
"invalid": { "invalid": {
"compact": [ "compact": [
{ {
"description": "Invalid signature parameters", "exception": "Invalid signature parameter",
"hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62" "hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62"
}, },
{ {
"description": "Signature too long", "exception": "Invalid signature length",
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000" "hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000"
}, },
{ {
"description": "Signature too short", "exception": "Invalid signature length",
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379" "hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379"
} }
], ],
"DER": [ "DER": [
{ {
"description": "Invalid sequence length", "exception": "Invalid sequence length",
"hex": "30ff0204ffffffff0204ffffffff" "hex": "30ff0204ffffffff0204ffffffff"
}, },
{ {
"description": "Invalid integer(s) length", "exception": "Invalid sequence length",
"hex": "30080201ffffffff0201ffffffff" "hex": "300c0304ffffffff0304ffffffff0000"
}, },
{ {
"description": "Invalid sequence tag", "exception": "Expected a DER integer",
"hex": "ff080204ffffffff0204ffffffff" "hex": "300cff04ffffffff0204ffffffff"
}, },
{ {
"description": "Invalid integer tag", "exception": "Expected a DER integer \\(2\\)",
"hex": "30080304ffffffff0304ffffffff" "hex": "300c0202ffffffff0204ffffffff"
}, },
{ {
"description": "Sequence too short", "exception": "Invalid DER encoding",
"hex": "30080304ffffffff0304ff" "hex": "300c0204ffffffff0202ffffffff"
},
{
"description": "Sequence too long",
"hex": "30080304ffffffff0304ffffffffffffff"
} }
], ],
"verifyRaw": [ "verifyRaw": [

Loading…
Cancel
Save