bitcore/lib/crypto/ecdsa.js

296 lines
7.2 KiB
JavaScript
Raw Normal View History

2014-11-20 07:16:27 -08:00
'use strict';
2014-08-19 17:15:54 -07:00
var BN = require('./bn');
var Point = require('./point');
2014-11-27 11:42:44 -08:00
var Signature = require('./signature');
var PublicKey = require('../publickey');
var Random = require('./random');
var Hash = require('./hash');
2014-12-19 12:10:58 -08:00
var BufferUtil = require('../util/buffer');
2014-12-19 14:23:30 -08:00
var _ = require('lodash');
var $ = require('../util/preconditions');
2014-08-09 17:43:24 -07:00
2014-08-28 16:07:28 -07:00
var ECDSA = function ECDSA(obj) {
2014-11-27 11:42:44 -08:00
if (!(this instanceof ECDSA)) {
2014-08-28 16:07:28 -07:00
return new ECDSA(obj);
2014-11-27 11:42:44 -08:00
}
if (obj) {
2014-08-28 16:07:28 -07:00
this.set(obj);
2014-11-27 11:42:44 -08:00
}
2014-08-28 16:07:28 -07:00
};
2014-12-19 12:10:58 -08:00
/* jshint maxcomplexity: 9 */
2014-08-28 16:07:28 -07:00
ECDSA.prototype.set = function(obj) {
2014-09-16 10:10:06 -07:00
this.hashbuf = obj.hashbuf || this.hashbuf;
this.endian = obj.endian || this.endian; //the endianness of hashbuf
this.privkey = obj.privkey || this.privkey;
this.pubkey = obj.pubkey || (this.privkey ? this.privkey.publicKey : this.pubkey);
2014-09-16 10:10:06 -07:00
this.sig = obj.sig || this.sig;
this.k = obj.k || this.k;
this.verified = obj.verified || this.verified;
2014-08-28 16:07:28 -07:00
return this;
2014-08-09 17:43:24 -07:00
};
ECDSA.prototype.privkey2pubkey = function() {
this.pubkey = this.privkey.toPublicKey();
};
2014-08-19 17:15:54 -07:00
ECDSA.prototype.calci = function() {
for (var i = 0; i < 4; i++) {
this.sig.i = i;
var Qprime;
2014-08-19 17:15:54 -07:00
try {
2014-12-19 12:10:58 -08:00
Qprime = this.toPublicKey();
2014-08-19 17:15:54 -07:00
} catch (e) {
console.error(e);
2014-08-19 17:15:54 -07:00
continue;
}
if (Qprime.point.eq(this.pubkey.point)) {
this.sig.compressed = this.pubkey.compressed;
2014-08-19 17:15:54 -07:00
return this;
}
}
this.sig.i = undefined;
throw new Error('Unable to find valid recovery factor');
};
2014-12-19 12:10:58 -08:00
ECDSA.fromString = function(str) {
2014-08-09 19:42:25 -07:00
var obj = JSON.parse(str);
2014-12-19 12:10:58 -08:00
return new ECDSA(obj);
2014-08-09 19:42:25 -07:00
};
ECDSA.prototype.randomK = function() {
2014-08-19 17:15:54 -07:00
var N = Point.getN();
2014-08-09 19:42:25 -07:00
var k;
do {
2014-12-19 12:10:58 -08:00
k = BN.fromBuffer(Random.getRandomBuffer(32));
} while (!(k.lt(N) && k.gt(BN.Zero)));
2014-08-09 19:42:25 -07:00
this.k = k;
return this;
};
2014-12-19 14:23:30 -08:00
// https://tools.ietf.org/html/rfc6979#section-3.2
ECDSA.prototype.deterministicK = function(badrs) {
2014-12-19 14:23:30 -08:00
/* jshint maxstatements: 25 */
2014-12-22 12:33:00 -08:00
// if r or s were invalid when this function was used in signing,
// we do not want to actually compute r, s here for efficiency, so,
// we can increment badrs. explained at end of RFC 6979 section 3.2
2014-12-19 14:23:30 -08:00
if (_.isUndefined(badrs)) {
badrs = 0;
}
var v = new Buffer(32);
v.fill(0x01);
var k = new Buffer(32);
k.fill(0x00);
2014-12-19 12:10:58 -08:00
var x = this.privkey.bn.toBuffer({
size: 32
});
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00]), x, this.hashbuf]), k);
v = Hash.sha256hmac(v, k);
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x01]), x, this.hashbuf]), k);
v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k);
2014-12-19 12:10:58 -08:00
var T = BN.fromBuffer(v);
var N = Point.getN();
// also explained in 3.2, we must ensure T is in the proper range (0, N)
for (var i = 0; i < badrs || !(T.lt(N) && T.gt(BN.Zero)); i++) {
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00])]), k);
v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k);
2014-12-19 12:10:58 -08:00
T = BN.fromBuffer(v);
}
this.k = T;
return this;
};
2014-08-24 14:26:17 -07:00
// Information about public key recovery:
// https://bitcointalk.org/index.php?topic=6430.0
// http://stackoverflow.com/questions/19665491/how-do-i-get-an-ecdsa-public-key-from-just-a-bitcoin-signature-sec1-4-1-6-k
2014-12-19 12:10:58 -08:00
ECDSA.prototype.toPublicKey = function() {
2014-12-19 14:23:30 -08:00
/* jshint maxstatements: 25 */
2014-08-19 17:15:54 -07:00
var i = this.sig.i;
2014-12-19 14:23:30 -08:00
$.checkArgument(i === 0 || i === 1 || i === 2 || i === 3, new Error('i must be equal to 0, 1, 2, or 3'));
2014-08-19 17:15:54 -07:00
2014-12-19 12:10:58 -08:00
var e = BN.fromBuffer(this.hashbuf);
2014-08-19 17:15:54 -07:00
var r = this.sig.r;
var s = this.sig.s;
// A set LSB signifies that the y-coordinate is odd
var isYOdd = i & 1;
// The more significant bit specifies whether we should use the
// first or second candidate key.
var isSecondKey = i >> 1;
var n = Point.getN();
var G = Point.getG();
// 1.1 Let x = r + jn
var x = isSecondKey ? r.add(n) : r;
var R = Point.fromX(isYOdd, x);
// 1.4 Check that nR is at infinity
var nR = R.mul(n);
2014-12-19 12:10:58 -08:00
if (!nR.isInfinity()) {
2014-08-19 17:15:54 -07:00
throw new Error('nR is not a valid curve point');
2014-12-19 12:10:58 -08:00
}
2014-08-19 17:15:54 -07:00
// Compute -e from e
var eNeg = e.neg().mod(n);
// 1.6.1 Compute Q = r^-1 (sR - eG)
// Q = r^-1 (sR + -eG)
var rInv = r.invm(n);
//var Q = R.multiplyTwo(s, G, eNeg).mul(rInv);
var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv);
var pubkey = PublicKey.fromPoint(Q, this.sig.compressed);
2014-08-19 17:15:54 -07:00
return pubkey;
};
2014-08-09 17:43:24 -07:00
ECDSA.prototype.sigError = function() {
2014-12-19 14:23:30 -08:00
/* jshint maxstatements: 25 */
if (!BufferUtil.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
2014-09-16 11:44:51 -07:00
return 'hashbuf must be a 32 byte buffer';
}
2014-08-09 17:43:24 -07:00
var r = this.sig.r;
var s = this.sig.s;
if (!(r.gt(BN.Zero) && r.lt(Point.getN())) || !(s.gt(BN.Zero) && s.lt(Point.getN()))) {
2014-08-09 17:43:24 -07:00
return 'r and s not in range';
2014-12-19 12:10:58 -08:00
}
2014-08-09 17:43:24 -07:00
2014-12-19 12:10:58 -08:00
var e = BN.fromBuffer(this.hashbuf, this.endian ? {
endian: this.endian
} : undefined);
2014-08-19 17:15:54 -07:00
var n = Point.getN();
2014-08-09 17:43:24 -07:00
var sinv = s.invm(n);
var u1 = sinv.mul(e).mod(n);
var u2 = sinv.mul(r).mod(n);
var p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
2014-12-19 14:23:30 -08:00
if (p.isInfinity()) {
2014-08-09 17:43:24 -07:00
return 'p is infinity';
2014-12-19 14:23:30 -08:00
}
2014-08-09 17:43:24 -07:00
2014-12-19 14:23:30 -08:00
if (p.getX().mod(n).cmp(r) !== 0) {
2014-08-09 17:43:24 -07:00
return 'Invalid signature';
2014-12-19 14:23:30 -08:00
} else {
2014-08-09 17:43:24 -07:00
return false;
2014-12-19 12:10:58 -08:00
}
2014-12-19 14:23:30 -08:00
};
2014-08-09 17:43:24 -07:00
2014-12-19 14:23:30 -08:00
ECDSA.toLowS = function(s) {
//enforce low s
//see BIP 62, "low S values in signatures"
if (s.gt(BN.fromBuffer(new Buffer('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex')))) {
s = Point.getN().sub(s);
2014-12-19 12:10:58 -08:00
}
2014-12-19 14:23:30 -08:00
return s;
};
2014-09-16 11:33:49 -07:00
2014-12-19 14:23:30 -08:00
ECDSA.prototype._findSignature = function(d, e) {
2014-08-19 17:15:54 -07:00
var N = Point.getN();
var G = Point.getG();
// try different values of k until r, s are valid
var badrs = 0;
2014-12-19 12:10:58 -08:00
var k, Q, r, s;
2014-08-09 17:43:24 -07:00
do {
2014-12-19 12:10:58 -08:00
if (!this.k || badrs > 0) {
this.deterministicK(badrs);
2014-12-19 12:10:58 -08:00
}
badrs++;
2014-12-19 12:10:58 -08:00
k = this.k;
Q = G.mul(k);
r = Q.x.mod(N);
s = k.invm(N).mul(e.add(d.mul(r))).mod(N);
} while (r.cmp(BN.Zero) <= 0 || s.cmp(BN.Zero) <= 0);
2014-08-09 17:43:24 -07:00
2014-12-19 14:23:30 -08:00
s = ECDSA.toLowS(s);
return {
2014-12-19 12:10:58 -08:00
s: s,
2014-12-19 14:23:30 -08:00
r: r
};
};
ECDSA.prototype.sign = function() {
var hashbuf = this.hashbuf;
var privkey = this.privkey;
var d = privkey.bn;
$.checkState(hashbuf && privkey && d, new Error('invalid parameters'));
$.checkState(BufferUtil.isBuffer(hashbuf) && hashbuf.length === 32, new Error('hashbuf must be a 32 byte buffer'));
var e = BN.fromBuffer(hashbuf, this.endian ? {
endian: this.endian
} : undefined);
var obj = this._findSignature(d, e);
obj.compressed = this.pubkey.compressed;
this.sig = new Signature(obj);
return this;
2014-08-09 17:43:24 -07:00
};
ECDSA.prototype.signRandomK = function() {
this.randomK();
2014-08-09 17:43:24 -07:00
return this.sign();
};
2014-08-09 19:42:25 -07:00
ECDSA.prototype.toString = function() {
var obj = {};
if (this.hashbuf) {
2014-08-20 10:46:01 -07:00
obj.hashbuf = this.hashbuf.toString('hex');
}
if (this.privkey) {
obj.privkey = this.privkey.toString();
}
if (this.pubkey) {
obj.pubkey = this.pubkey.toString();
}
if (this.sig) {
2014-08-09 19:42:25 -07:00
obj.sig = this.sig.toString();
}
if (this.k) {
2014-08-09 19:42:25 -07:00
obj.k = this.k.toString();
}
2014-08-09 19:42:25 -07:00
return JSON.stringify(obj);
};
2014-08-09 17:43:24 -07:00
ECDSA.prototype.verify = function() {
2014-12-19 14:23:30 -08:00
if (!this.sigError()) {
this.verified = true;
2014-12-19 14:23:30 -08:00
} else {
this.verified = false;
2014-12-19 14:23:30 -08:00
}
return this;
2014-08-09 17:43:24 -07:00
};
ECDSA.sign = function(hashbuf, privkey, endian) {
2014-09-16 11:34:28 -07:00
return ECDSA().set({
hashbuf: hashbuf,
endian: endian,
privkey: privkey
}).sign().sig;
2014-09-16 11:34:28 -07:00
};
ECDSA.verify = function(hashbuf, sig, pubkey, endian) {
2014-09-16 11:34:28 -07:00
return ECDSA().set({
hashbuf: hashbuf,
endian: endian,
2014-09-16 11:34:28 -07:00
sig: sig,
pubkey: pubkey
}).verify().verified;
2014-09-16 11:34:28 -07:00
};
2014-08-09 17:43:24 -07:00
module.exports = ECDSA;