simplify ECIES interface

the toKeypair doesn't really need to be a keypair. upon encrypting, it merely
needs to be a pubkey. and upon decrypting, it needs to be a privkey.
This commit is contained in:
Ryan X. Charles 2014-09-15 11:19:18 -07:00
parent 8dc6a5023a
commit 385edbcc92
3 changed files with 13 additions and 12 deletions

View File

@ -3,6 +3,7 @@ var Keypair = require('../keypair');
var Point = require('../point');
var Hash = require('../hash');
var Pubkey = require('../pubkey');
var Privkey = require('../privkey');
// http://en.wikipedia.org/wiki/Integrated_Encryption_Scheme
var ECIES = function ECIES() {
@ -10,14 +11,14 @@ var ECIES = function ECIES() {
return new ECIES();
};
ECIES.encrypt = function(messagebuf, tokeypair, fromkeypair, ivbuf) {
ECIES.encrypt = function(messagebuf, topubkey, fromkeypair, ivbuf) {
if (!fromkeypair)
fromkeypair = Keypair().fromRandom();
var r = fromkeypair.privkey.bn;
var R = fromkeypair.pubkey.point;
var Rpubkey = fromkeypair.pubkey;
var Rbuf = Rpubkey.toDER(true);
var KB = tokeypair.pubkey.point;
var KB = topubkey.point;
var P = KB.mul(r);
var S = P.getX();
var Sbuf = S.toBuffer({size: 32});
@ -30,8 +31,8 @@ ECIES.encrypt = function(messagebuf, tokeypair, fromkeypair, ivbuf) {
return encbuf;
};
ECIES.decrypt = function(encbuf, tokeypair) {
var kB = tokeypair.privkey.bn;
ECIES.decrypt = function(encbuf, toprivkey) {
var kB = toprivkey.bn;
var frompubkey = Pubkey().fromDER(encbuf.slice(0, 33));
var R = frompubkey.point;
var P = R.mul(kB);

View File

@ -63,13 +63,13 @@ StealthMessage.prototype.encrypt = function(ivbuf) {
this.fromKeypair = Keypair().fromRandom();
var receivePubkey = this.toStealthAddress.getReceivePubkey(this.fromKeypair);
this.receiveAddress = Address().fromPubkey(receivePubkey);
this.encbuf = ECIES.encrypt(this.messagebuf, Keypair().set({pubkey: receivePubkey}), this.fromKeypair, ivbuf);
this.encbuf = ECIES.encrypt(this.messagebuf, receivePubkey, this.fromKeypair, ivbuf);
return this;
};
StealthMessage.prototype.decrypt = function() {
var receiveKeypair = this.toStealthkey.getReceiveKeypair(this.fromKeypair.pubkey);
this.messagebuf = ECIES.decrypt(this.encbuf, receiveKeypair);
this.messagebuf = ECIES.decrypt(this.encbuf, receiveKeypair.privkey);
return this;
};

View File

@ -22,12 +22,12 @@ describe('#ECIES', function() {
describe('@encrypt', function() {
it('should return a buffer', function() {
var encbuf = ECIES.encrypt(messagebuf, tokey, fromkey);
var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey, fromkey);
Buffer.isBuffer(encbuf).should.equal(true);
});
it('should return a buffer if fromkey is not present', function() {
var encbuf = ECIES.encrypt(messagebuf, tokey);
var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey);
Buffer.isBuffer(encbuf).should.equal(true);
});
@ -36,14 +36,14 @@ describe('#ECIES', function() {
describe('@decrypt', function() {
it('should decrypt that which was encrypted', function() {
var encbuf = ECIES.encrypt(messagebuf, tokey, fromkey);
var messagebuf2 = ECIES.decrypt(encbuf, tokey);
var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey, fromkey);
var messagebuf2 = ECIES.decrypt(encbuf, tokey.privkey);
messagebuf2.toString('hex').should.equal(messagebuf.toString('hex'));
});
it('should decrypt that which was encrypted if fromkeypair was randomly generated', function() {
var encbuf = ECIES.encrypt(messagebuf, tokey);
var messagebuf2 = ECIES.decrypt(encbuf, tokey);
var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey);
var messagebuf2 = ECIES.decrypt(encbuf, tokey.privkey);
messagebuf2.toString('hex').should.equal(messagebuf.toString('hex'));
});