From 7ef134436ea9af45f4585481f443c24bce6b1fbf Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 8 Dec 2014 11:24:16 +1100 Subject: [PATCH] ecdsa/HDNode: use node crypto for Hmacs --- src/ecdsa.js | 33 +++++++++++++++++++++++++-------- src/hdnode.js | 9 +++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index 75450dc..e3343fa 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -1,10 +1,13 @@ var assert = require('assert') -var crypto = require('./crypto') +var crypto = require('crypto') var enforceType = require('./types') var BigInteger = require('bigi') var ECSignature = require('./ecsignature') +var ZERO = new Buffer([0]) +var ONE = new Buffer([1]) + // https://tools.ietf.org/html/rfc6979#section-3.2 function deterministicGenerateK(curve, hash, d) { enforceType('Buffer', hash) @@ -24,27 +27,41 @@ function deterministicGenerateK(curve, hash, d) { k.fill(0) // Step D - k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k) + k = crypto.createHmac('sha256', k) + .update(v) + .update(ZERO) + .update(x) + .update(hash) + .digest() // Step E - v = crypto.HmacSHA256(v, k) + v = crypto.createHmac('sha256', k).update(v).digest() // Step F - k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k) + k = crypto.createHmac('sha256', k) + .update(v) + .update(ONE) + .update(x) + .update(hash) + .digest() // Step G - v = crypto.HmacSHA256(v, k) + v = crypto.createHmac('sha256', k).update(v).digest() // Step H1/H2a, ignored as tlen === qlen (256 bit) // Step H2b - v = crypto.HmacSHA256(v, k) + v = crypto.createHmac('sha256', k).update(v).digest() var T = BigInteger.fromBuffer(v) // Step H3, repeat until T is within the interval [1, n - 1] while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0)) { - k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0])]), k) - v = crypto.HmacSHA256(v, k) + k = crypto.createHmac('sha256', k) + .update(v) + .update(ZERO) + .digest() + + v = crypto.createHmac('sha256', k).update(v).digest() T = BigInteger.fromBuffer(v) } diff --git a/src/hdnode.js b/src/hdnode.js index 8979a9e..a3c9f4b 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -1,6 +1,7 @@ var assert = require('assert') var base58check = require('bs58check') -var crypto = require('./crypto') +var bcrypto = require('./crypto') +var crypto = require('crypto') var enforceType = require('./types') var networks = require('./networks') @@ -56,7 +57,7 @@ HDNode.fromSeedBuffer = function(seed, network) { assert(seed.length >= 16, 'Seed should be at least 128 bits') assert(seed.length <= 64, 'Seed should be at most 512 bits') - var I = crypto.HmacSHA512(seed, HDNode.MASTER_SECRET) + var I = crypto.createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest() var IL = I.slice(0, 32) var IR = I.slice(32) @@ -145,7 +146,7 @@ HDNode.fromHex = function(hex, network) { } HDNode.prototype.getIdentifier = function() { - return crypto.hash160(this.pubKey.toBuffer()) + return bcrypto.hash160(this.pubKey.toBuffer()) } HDNode.prototype.getFingerprint = function() { @@ -255,7 +256,7 @@ HDNode.prototype.derive = function(index) { ]) } - var I = crypto.HmacSHA512(data, this.chainCode) + var I = crypto.createHmac('sha512', this.chainCode).update(data).digest() var IL = I.slice(0, 32) var IR = I.slice(32)