From 6a2615d3cc695c29bbd6dbfb85408b8903f096a5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 27 Jul 2016 04:06:05 -0700 Subject: [PATCH] bip151: 32 bit sequence and 64 bit iv. --- lib/bcoin/bip151.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/bip151.js b/lib/bcoin/bip151.js index ba150125..8ae277c8 100644 --- a/lib/bcoin/bip151.js +++ b/lib/bcoin/bip151.js @@ -70,9 +70,8 @@ function BIP151Stream(cipher, key) { this.chacha = new chachapoly.ChaCha20(); this.aead = new chachapoly.AEAD(); this.tag = null; - this.seqHi = 0; - this.seqLo = 0; - this.iv = new Buffer(12); + this.seq = 0; + this.iv = new Buffer(8); this.iv.fill(0); this.highWaterMark = 1024 * (1 << 20); @@ -106,8 +105,7 @@ BIP151Stream.prototype.init = function init(publicKey) { this.k2 = utils.hkdfExpand(this.prk, INFO_KEY2, 32, 'sha256'); this.sid = utils.hkdfExpand(this.prk, INFO_SID, 32, 'sha256'); - this.seqHi = 0; - this.seqLo = 0; + this.seq = 0; this.update(); @@ -163,11 +161,8 @@ BIP151Stream.prototype.rekey = function rekey() { BIP151Stream.prototype.sequence = function sequence() { // Wrap sequence number a la openssh. - if (++this.seqLo === 0x100000000) { - this.seqLo = 0; - if (++this.seqHi === 0x100000000) - this.seqHi = 0; - } + if (++this.seq === 0x100000000) + this.seq = 0; this.update(); @@ -183,8 +178,7 @@ BIP151Stream.prototype.sequence = function sequence() { */ BIP151Stream.prototype.update = function update() { - this.iv.writeUInt32LE(this.seqLo, 0, true); - this.iv.writeUInt32LE(this.seqHi, 4, true); + this.iv.writeUInt32LE(this.seq, 4, true); return this.iv; };