Skip to content
Permalink
Browse files

clean up

  • Loading branch information
calvinmetcalf committed Jan 21, 2015
1 parent 841ff59 commit 1e81978c174cf4293ffd0b52ec0b3841eda2a3eb
Showing with 53 additions and 69 deletions.
  1. +3 −5 aead.js
  2. +5 −11 chacha20.js
  3. +4 −5 chachastream.js
  4. +31 −17 cipherbase.js
  5. +4 −7 legacy-aead.js
  6. +6 −24 polystream.js
@@ -30,7 +30,7 @@ Cipher.prototype.setAAD = function (aad) {
this.poly.update(padding);
}
};
Cipher.prototype._transform = function (chunk, _, next) {
Cipher.prototype._update = function (chunk) {
if (!this._hasData) {
this._hasData = true;
}
@@ -49,10 +49,9 @@ Cipher.prototype._transform = function (chunk, _, next) {
} else {
this.poly.update(pad);
}
this.push(pad);
next();
return pad;
};
Cipher.prototype._flush = function (next) {
Cipher.prototype._final = function () {
if (this._decrypt && !this.tag) {
throw new Error('Unsupported state or unable to authenticate data');
}
@@ -73,7 +72,6 @@ Cipher.prototype._flush = function (next) {
} else {
this.tag = tag;
}
next();
};
Cipher.prototype.getAuthTag = function () {
if(this._decrypt || this.tag === null) {
@@ -1,22 +1,16 @@
var maxInt = Math.pow(2, 32);
function fixInt(int) {
if (int >= maxInt) {
return int - maxInt;
}
return int;
}
function ROTATE(v, c) {
return (v << c) | (v >>> (32 - c));
}
var constants = new Buffer('expand 32-byte k');
module.exports = Chacha20;
function Chacha20(key, nonce) {
this.input = new Uint32Array(16);

// https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-01#section-2.3
this.input[0] = 1634760805;
this.input[1] = 857760878;
this.input[2] = 2036477234;
this.input[3] = 1797285236;
this.input[0] = constants.readUInt32LE(0);
this.input[1] = constants.readUInt32LE(4);
this.input[2] = constants.readUInt32LE(8);
this.input[3] = constants.readUInt32LE(12);
this.input[4] = key.readUInt32LE(0);
this.input[5] = key.readUInt32LE(4);
this.input[6] = key.readUInt32LE(8);
@@ -10,16 +10,15 @@ function ChaChaStream (key, iv) {
CipherBase.call(this);
this.chacha = new Chacha20(key, iv);
}
ChaChaStream.prototype._transform = function (chunk, _, next) {
ChaChaStream.prototype._update = function (chunk) {
var len = chunk.length;
if (!len) {
return next();
return;
}
var pad = this.chacha.getBytes(len);
var i = -1;
while (++i < len) {
pad[i] ^= chunk[i];
}
this.push(pad);
next();
};
return pad;
};
@@ -1,33 +1,47 @@
var inherits = require('inherits');

var Transform = require('readable-stream').Transform;
var inherits = require('inherits');

inherits(CipherBase, Transform);
module.exports = CipherBase;
function CipherBase() {
inherits(CipherBase, Transform);
function CipherBase(digest) {
Transform.call(this);
if (digest) {
this.digest = finalFunc;
} else {
this.final = finalFunc;
}

}
CipherBase.prototype.update = function (data, inputEnd, outputEnc) {
this.write(data, inputEnd);
var outData = new Buffer('');
var chunk;
while ((chunk = this.read())) {
outData = Buffer.concat([outData, chunk]);
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
if (typeof data === 'string') {
data = new Buffer(data, inputEnc);
}
var outData = this._update(data) || new Buffer('');
if (outputEnc) {
outData = outData.toString(outputEnc);
}
if (this.digest) {
return this;
}
return outData;
};
CipherBase.prototype.final = function (outputEnc) {
this.end();
var outData = new Buffer('');
var chunk;
while ((chunk = this.read())) {
outData = Buffer.concat([outData, chunk]);
CipherBase.prototype._transform = function (data, _, next) {
this.push(this._update(data));
next();
};
CipherBase.prototype._flush = function (next) {
try {
this.push(this._final());
} catch(e) {
return next(e);
}
next();
};
function finalFunc (outputEnc) {
var outData = this._final() || new Buffer('');
if (outputEnc) {
outData = outData.toString(outputEnc);
}
return outData;
};
};
CipherBase.prototype._final = function () {};
@@ -1,7 +1,6 @@
var inherits = require('inherits');
var CipherBase = require('./cipherbase');
var Chacha20 = require('./chacha20');
var CipherBase = require('./cipherbase');
var Poly1305 = require('./poly1305');
inherits(Cipher, CipherBase);
module.exports = Cipher;
@@ -40,13 +39,13 @@ Cipher.prototype._flushlentag = function () {
len.writeUInt32LE(this.alen, 0);
this.poly.update(len);
};
Cipher.prototype._transform = function (chunk, _, next) {
Cipher.prototype._update = function (chunk) {
if (!this._hasData) {
this._flushlentag();
}
var len = chunk.length;
if (!len) {
return next();
return;
}
this.clen += len;
var pad = this.chacha.getBytes(len);
@@ -59,10 +58,9 @@ Cipher.prototype._transform = function (chunk, _, next) {
} else {
this.poly.update(pad);
}
this.push(pad);
next();
return pad;
};
Cipher.prototype._flush = function (next) {
Cipher.prototype._final = function () {
if (this._decrypt && !this.tag) {
throw new Error('Unsupported state or unable to authenticate data');
}
@@ -86,7 +84,6 @@ Cipher.prototype._flush = function (next) {
} else {
this.tag = tag;
}
next();
};
Cipher.prototype.getAuthTag = function () {
if(this._decrypt || this.tag === null) {
@@ -1,38 +1,20 @@
var inherits = require('inherits');

var Transform = require('readable-stream').Transform;
var CipherBase = require('./cipherbase');
var Poly1305 = require('./poly1305');
module.exports = PolyStream;
inherits(PolyStream, Transform);
inherits(PolyStream, CipherBase);
function PolyStream (key) {
if (!(this instanceof PolyStream)) {
return new PolyStream(key);
}
Transform.call(this);
CipherBase.call(this, true);
this.poly = new Poly1305(key);
}
PolyStream.prototype.update = function (data, enc) {
this.write(data, enc);
return this;
};
PolyStream.prototype._transform = function (data, _, next) {
PolyStream.prototype._update = function (data) {
this.poly.update(data);
next();
};

PolyStream.prototype._flush = function (next) {
this.push(this.poly.finish());
next();
};
PolyStream.prototype.digest = function (outputEnc) {
this.end();
var outData = new Buffer('');
var chunk;
while ((chunk = this.read())) {
outData = Buffer.concat([outData, chunk]);
}
if (outputEnc) {
outData = outData.toString(outputEnc);
}
return outData;
PolyStream.prototype._final = function () {
return this.poly.finish();
};

0 comments on commit 1e81978

Please sign in to comment.
You can’t perform that action at this time.