|
@ -4,7 +4,7 @@ var crypto = require('./crypto') |
|
|
var BigInteger = require('bigi') |
|
|
var BigInteger = require('bigi') |
|
|
var ECPointFp = require('./ec').ECPointFp |
|
|
var ECPointFp = require('./ec').ECPointFp |
|
|
|
|
|
|
|
|
function deterministicGenerateK(ecparams, hash, D) { |
|
|
function deterministicGenerateK(ecparams, hash, D) { |
|
|
assert(Buffer.isBuffer(hash), 'Hash must be a Buffer, not ' + hash) |
|
|
assert(Buffer.isBuffer(hash), 'Hash must be a Buffer, not ' + hash) |
|
|
assert.equal(hash.length, 32, 'Hash must be 256 bit') |
|
|
assert.equal(hash.length, 32, 'Hash must be 256 bit') |
|
|
assert(D instanceof BigInteger, 'Private key must be a BigInteger') |
|
|
assert(D instanceof BigInteger, 'Private key must be a BigInteger') |
|
@ -28,9 +28,9 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
assert(kB.compareTo(ecparams.getN()) < 0, 'Invalid k value') |
|
|
assert(kB.compareTo(ecparams.getN()) < 0, 'Invalid k value') |
|
|
|
|
|
|
|
|
return kB |
|
|
return kB |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function sign(ecparams, hash, D) { |
|
|
function sign(ecparams, hash, D) { |
|
|
var k = deterministicGenerateK(ecparams, hash, D) |
|
|
var k = deterministicGenerateK(ecparams, hash, D) |
|
|
|
|
|
|
|
|
var n = ecparams.getN() |
|
|
var n = ecparams.getN() |
|
@ -52,15 +52,15 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return {r: r, s: s} |
|
|
return {r: r, s: s} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function verify(ecparams, hash, r, s, Q) { |
|
|
function verify(ecparams, hash, r, s, Q) { |
|
|
var e = BigInteger.fromBuffer(hash) |
|
|
var e = BigInteger.fromBuffer(hash) |
|
|
|
|
|
|
|
|
return verifyRaw(ecparams, e, r, s, Q) |
|
|
return verifyRaw(ecparams, e, r, s, Q) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function verifyRaw(ecparams, e, r, s, Q) { |
|
|
function verifyRaw(ecparams, e, r, s, Q) { |
|
|
var n = ecparams.getN() |
|
|
var n = ecparams.getN() |
|
|
var G = ecparams.getG() |
|
|
var G = ecparams.getG() |
|
|
|
|
|
|
|
@ -80,14 +80,14 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
var v = point.getX().toBigInteger().mod(n) |
|
|
var v = point.getX().toBigInteger().mod(n) |
|
|
|
|
|
|
|
|
return v.equals(r) |
|
|
return v.equals(r) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Serialize a signature into DER format. |
|
|
* Serialize a signature into DER format. |
|
|
* |
|
|
* |
|
|
* Takes two BigIntegers representing r and s and returns a byte array. |
|
|
* Takes two BigIntegers representing r and s and returns a byte array. |
|
|
*/ |
|
|
*/ |
|
|
function serializeSig(r, s) { |
|
|
function serializeSig(r, s) { |
|
|
var rBa = r.toByteArraySigned() |
|
|
var rBa = r.toByteArraySigned() |
|
|
var sBa = s.toByteArraySigned() |
|
|
var sBa = s.toByteArraySigned() |
|
|
|
|
|
|
|
@ -104,9 +104,9 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
sequence.unshift(0x30); // SEQUENCE
|
|
|
sequence.unshift(0x30); // SEQUENCE
|
|
|
|
|
|
|
|
|
return sequence |
|
|
return sequence |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Parses a buffer containing a DER-encoded signature. |
|
|
* Parses a buffer containing a DER-encoded signature. |
|
|
* |
|
|
* |
|
|
* This function will return an object of the form: |
|
|
* This function will return an object of the form: |
|
@ -116,7 +116,7 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
* s: BigInteger |
|
|
* s: BigInteger |
|
|
* } |
|
|
* } |
|
|
*/ |
|
|
*/ |
|
|
function parseSig(buffer) { |
|
|
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') |
|
|
|
|
|
|
|
@ -133,9 +133,9 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
r: BigInteger.fromByteArraySigned(rB), |
|
|
r: BigInteger.fromByteArraySigned(rB), |
|
|
s: BigInteger.fromByteArraySigned(sB) |
|
|
s: BigInteger.fromByteArraySigned(sB) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function serializeSigCompact(r, s, i, compressed) { |
|
|
function serializeSigCompact(r, s, i, compressed) { |
|
|
if (compressed) { |
|
|
if (compressed) { |
|
|
i += 4 |
|
|
i += 4 |
|
|
} |
|
|
} |
|
@ -148,9 +148,9 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
s.toBuffer(32).copy(buffer, 33) |
|
|
s.toBuffer(32).copy(buffer, 33) |
|
|
|
|
|
|
|
|
return buffer |
|
|
return buffer |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function parseSigCompact(buffer) { |
|
|
function parseSigCompact(buffer) { |
|
|
assert.equal(buffer.length, 65, 'Invalid signature length') |
|
|
assert.equal(buffer.length, 65, 'Invalid signature length') |
|
|
var i = buffer.readUInt8(0) - 27 |
|
|
var i = buffer.readUInt8(0) - 27 |
|
|
|
|
|
|
|
@ -170,9 +170,9 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
i: i, |
|
|
i: i, |
|
|
compressed: compressed |
|
|
compressed: compressed |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Recover a public key from a signature. |
|
|
* Recover a public key from a signature. |
|
|
* |
|
|
* |
|
|
* See SEC 1: Elliptic Curve Cryptography, section 4.1.6, "Public |
|
|
* See SEC 1: Elliptic Curve Cryptography, section 4.1.6, "Public |
|
@ -180,7 +180,7 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
* |
|
|
* |
|
|
* http://www.secg.org/download/aid-780/sec1-v2.pdf
|
|
|
* http://www.secg.org/download/aid-780/sec1-v2.pdf
|
|
|
*/ |
|
|
*/ |
|
|
function recoverPubKey(ecparams, e, r, s, i) { |
|
|
function recoverPubKey(ecparams, e, r, s, i) { |
|
|
assert.strictEqual(i & 3, i, 'The recovery param is more than two bits') |
|
|
assert.strictEqual(i & 3, i, 'The recovery param is more than two bits') |
|
|
|
|
|
|
|
|
// A set LSB signifies that the y-coordinate is odd
|
|
|
// A set LSB signifies that the y-coordinate is odd
|
|
@ -233,9 +233,9 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return Q |
|
|
return Q |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Calculate pubkey extraction parameter. |
|
|
* Calculate pubkey extraction parameter. |
|
|
* |
|
|
* |
|
|
* When extracting a pubkey from a signature, we have to |
|
|
* When extracting a pubkey from a signature, we have to |
|
@ -246,7 +246,7 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
* This function simply tries all four cases and returns the value |
|
|
* This function simply tries all four cases and returns the value |
|
|
* that resulted in a successful pubkey recovery. |
|
|
* that resulted in a successful pubkey recovery. |
|
|
*/ |
|
|
*/ |
|
|
function calcPubKeyRecoveryParam(ecparams, e, r, s, Q) { |
|
|
function calcPubKeyRecoveryParam(ecparams, e, r, s, Q) { |
|
|
for (var i = 0; i < 4; i++) { |
|
|
for (var i = 0; i < 4; i++) { |
|
|
var Qprime = recoverPubKey(ecparams, e, r, s, i) |
|
|
var Qprime = recoverPubKey(ecparams, e, r, s, i) |
|
|
|
|
|
|
|
@ -256,17 +256,17 @@ var ECPointFp = require('./ec').ECPointFp |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
throw new Error('Unable to find valid recovery factor') |
|
|
throw new Error('Unable to find valid recovery factor') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
module.exports = { |
|
|
module.exports = { |
|
|
calcPubKeyRecoveryParam: calcPubKeyRecoveryParam, |
|
|
calcPubKeyRecoveryParam: calcPubKeyRecoveryParam, |
|
|
deterministicGenerateK: deterministicGenerateK, |
|
|
deterministicGenerateK: deterministicGenerateK, |
|
|
recoverPubKey: recoverPubKey, |
|
|
recoverPubKey: recoverPubKey, |
|
|
sign: sign, |
|
|
sign: sign, |
|
|
verify: verify, |
|
|
verify: verify, |
|
|
verifyRaw: verifyRaw, |
|
|
verifyRaw: verifyRaw, |
|
|
serializeSig: serializeSig, |
|
|
serializeSig: serializeSig, |
|
|
parseSig: parseSig, |
|
|
parseSig: parseSig, |
|
|
serializeSigCompact: serializeSigCompact, |
|
|
serializeSigCompact: serializeSigCompact, |
|
|
parseSigCompact: parseSigCompact |
|
|
parseSigCompact: parseSigCompact |
|
|
} |
|
|
} |
|
|