chachapoly: do not reinitialize state.

This commit is contained in:
Christopher Jeffrey 2016-07-25 22:11:32 -07:00
parent 73deb5430e
commit b0bb5d516d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 25 additions and 4 deletions

View File

@ -104,9 +104,8 @@ BIP151Stream.prototype.rekey = function rekey() {
BIP151Stream.prototype.sequence = function sequence() {
this.seq++;
this.chacha.init(this.k1, this.iv());
this.aead.init(this.k2, this.iv());
this.aead.aad(this.sid);
this.chacha.init(null, this.iv());
this.aead.init(null, this.iv());
};
BIP151Stream.prototype.iv = function iv() {

View File

@ -34,6 +34,19 @@ function ChaCha20() {
*/
ChaCha20.prototype.init = function init(key, iv, counter) {
if (key)
this.initKey(key);
if (iv)
this.initIV(iv, counter);
};
/**
* Set key.
* @param {Buffer} key
*/
ChaCha20.prototype.initKey = function initKey(key) {
this.state[0] = 0x61707865;
this.state[1] = 0x3320646e;
this.state[2] = 0x79622d32;
@ -50,6 +63,16 @@ ChaCha20.prototype.init = function init(key, iv, counter) {
this.state[12] = 0;
this.pos = 0xffffffff;
};
/**
* Set IV and counter.
* @param {Buffer} iv
* @param {Number} counter
*/
ChaCha20.prototype.initIV = function initIV(iv, counter) {
if (iv.length === 8) {
this.state[13] = 0;
this.state[14] = iv.readUInt32LE(0, true);
@ -62,7 +85,6 @@ ChaCha20.prototype.init = function init(key, iv, counter) {
assert(false, 'Bad iv size.');
}
this.pos = 0xffffffff;
this.ivSize = iv.length * 8;
this.setCounter(counter);