Skip to content

Commit

Permalink
Merge pull request #31 from eordano/timing
Browse files Browse the repository at this point in the history
Use constant time comparison
  • Loading branch information
maraoz committed Apr 1, 2015
2 parents d674639 + d4e29a3 commit b905f36
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/ecies.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ ECIES.prototype.decrypt = function(encbuf) {
var d = encbuf.slice(encbuf.length - 32, encbuf.length);

var d2 = Hash.sha256hmac(c, this.kM);
if (d.toString('hex') !== d2.toString('hex')) throw new Error('Invalid checksum');

var equal = true;
for (var i = 0; i < d.length; i++) {
equal &= (d[i] === d2[i]);
}
if (!equal) {
throw new Error('Invalid checksum');
}
var messagebuf = AESCBC.decryptCipherkey(c, this.kE);

return messagebuf;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
},
"dependencies": {
"aes": "^0.1.0",
"bitcore": "^0.11.4"
"bitcore": "^0.11.6"
}
}
8 changes: 8 additions & 0 deletions test/ecies.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,13 @@ describe('ECIES', function() {
should.exist(bitcore.errors.ECIES);
});

it('correctly fails if trying to decrypt a bad message', function() {
var encrypted = bitcore.util.buffer.copy(encBuf);
encrypted[encrypted.length - 1] = 2;
(function() {
return bob.decrypt(encrypted);
}).should.throw('Invalid checksum');
});


});

0 comments on commit b905f36

Please sign in to comment.