chachapoly: do not reinitialize state.
This commit is contained in:
parent
73deb5430e
commit
b0bb5d516d
@ -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() {
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user