ecdsa/HDNode: use node crypto for Hmacs
This commit is contained in:
parent
f3ddc508f1
commit
7ef134436e
2 changed files with 30 additions and 12 deletions
33
src/ecdsa.js
33
src/ecdsa.js
|
@ -1,10 +1,13 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var crypto = require('./crypto')
|
var crypto = require('crypto')
|
||||||
var enforceType = require('./types')
|
var enforceType = require('./types')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECSignature = require('./ecsignature')
|
var ECSignature = require('./ecsignature')
|
||||||
|
|
||||||
|
var ZERO = new Buffer([0])
|
||||||
|
var ONE = new Buffer([1])
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6979#section-3.2
|
// https://tools.ietf.org/html/rfc6979#section-3.2
|
||||||
function deterministicGenerateK(curve, hash, d) {
|
function deterministicGenerateK(curve, hash, d) {
|
||||||
enforceType('Buffer', hash)
|
enforceType('Buffer', hash)
|
||||||
|
@ -24,27 +27,41 @@ function deterministicGenerateK(curve, hash, d) {
|
||||||
k.fill(0)
|
k.fill(0)
|
||||||
|
|
||||||
// Step D
|
// 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
|
// Step E
|
||||||
v = crypto.HmacSHA256(v, k)
|
v = crypto.createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
// Step F
|
// 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
|
// 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 H1/H2a, ignored as tlen === qlen (256 bit)
|
||||||
// Step H2b
|
// Step H2b
|
||||||
v = crypto.HmacSHA256(v, k)
|
v = crypto.createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
var T = BigInteger.fromBuffer(v)
|
var T = BigInteger.fromBuffer(v)
|
||||||
|
|
||||||
// Step H3, repeat until T is within the interval [1, n - 1]
|
// Step H3, repeat until T is within the interval [1, n - 1]
|
||||||
while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0)) {
|
while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0)) {
|
||||||
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0])]), k)
|
k = crypto.createHmac('sha256', k)
|
||||||
v = crypto.HmacSHA256(v, k)
|
.update(v)
|
||||||
|
.update(ZERO)
|
||||||
|
.digest()
|
||||||
|
|
||||||
|
v = crypto.createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
T = BigInteger.fromBuffer(v)
|
T = BigInteger.fromBuffer(v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var base58check = require('bs58check')
|
var base58check = require('bs58check')
|
||||||
var crypto = require('./crypto')
|
var bcrypto = require('./crypto')
|
||||||
|
var crypto = require('crypto')
|
||||||
var enforceType = require('./types')
|
var enforceType = require('./types')
|
||||||
var networks = require('./networks')
|
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 >= 16, 'Seed should be at least 128 bits')
|
||||||
assert(seed.length <= 64, 'Seed should be at most 512 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 IL = I.slice(0, 32)
|
||||||
var IR = I.slice(32)
|
var IR = I.slice(32)
|
||||||
|
|
||||||
|
@ -145,7 +146,7 @@ HDNode.fromHex = function(hex, network) {
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.prototype.getIdentifier = function() {
|
HDNode.prototype.getIdentifier = function() {
|
||||||
return crypto.hash160(this.pubKey.toBuffer())
|
return bcrypto.hash160(this.pubKey.toBuffer())
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.prototype.getFingerprint = function() {
|
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 IL = I.slice(0, 32)
|
||||||
var IR = I.slice(32)
|
var IR = I.slice(32)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue