Skip to content

Commit

Permalink
Ensure verifySignature does not leak useful timing information
Browse files Browse the repository at this point in the history
This avoids easy timing attacks against signature verification
by double-hashing before comparing values. The information we
leak after this patch is useless unless the hash function is
completely broken.

Closes #36
  • Loading branch information
alokmenghrajani authored and arekinath committed Sep 21, 2015
1 parent 5f13706 commit 78ab1da
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,23 @@ module.exports = {
if (!alg || alg.length !== 2)
throw new TypeError('parsedSignature: unsupported algorithm ' +
parsedSignature.algorithm);
var hashAlg = alg[1].toUpperCase();

var hmac = crypto.createHmac(alg[1].toUpperCase(), secret);
var hmac = crypto.createHmac(hashAlg, secret);
hmac.update(parsedSignature.signingString);
return (hmac.digest('base64') === parsedSignature.params.signature);

/*
* Now double-hash to avoid leaking timing information - there's
* no easy constant-time compare in JS, so we use this approach
* instead. See for more info:
* https://www.isecpartners.com/blog/2011/february/double-hmac-
* verification.aspx
*/
var h1 = crypto.createHmac(hashAlg, secret);
h1.update(hmac.digest());
var h2 = crypto.createHmac(hashAlg, secret);
h2.update(new Buffer(parsedSignature.params.signature, 'base64'));

return (h1.digest().equals(h2.digest()));
}
};

0 comments on commit 78ab1da

Please sign in to comment.