[Pkg-javascript-commits] [node-browserify-aes] 15/92: reorg

Bastien Roucariès rouca at moszumanska.debian.org
Sun Jun 4 09:35:16 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-browserify-aes.

commit 6eb73ca2f5e7b5d6afde1cc54187ba12bb25c5b3
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Fri Oct 17 08:18:39 2014 -0400

    reorg
---
 aes.js       |   1 -
 decrypter.js | 280 ++++++++++++++++-------------------------------------------
 encrypter.js | 240 ++++++++++++++------------------------------------
 modes/cbc.js |  12 +++
 modes/cfb.js |  12 +++
 modes/ctr.js |  20 +++++
 modes/ecb.js |   6 ++
 modes/ofb.js |   5 ++
 test.js      |  17 ----
 9 files changed, 197 insertions(+), 396 deletions(-)

diff --git a/aes.js b/aes.js
index 9c62da5..a11d912 100644
--- a/aes.js
+++ b/aes.js
@@ -1,4 +1,3 @@
-var crypto = require('crypto');
 var uint_max = Math.pow(2, 32);
 function fixup_uint32(x) {
     var ret, x_pos;
diff --git a/decrypter.js b/decrypter.js
index d92745e..63ad418 100644
--- a/decrypter.js
+++ b/decrypter.js
@@ -1,203 +1,87 @@
 var aes = require('./aes');
 var Transform = require('stream').Transform;
 var inherits = require('inherits');
-var duplexer = require('duplexer2');
 var modes = require('./modes');
 var ebtk = require('./EVP_BytesToKey');
-var xor = require('./xor');
-inherits(Splitter, Transform);
-function unpad(last) {
-  var padded = last[15];
-  if (padded === 16) {
-    return;
-  }
-  return last.slice(0, 16 - padded);
-}
-function Splitter(padding) {
-  if (!(this instanceof Splitter)) {
-    return new Splitter(padding);
+
+
+inherits(Decipher, Transform);
+function Decipher(padding, mode, key, iv) {
+  if (!(this instanceof Decipher)) {
+    return new Decipher(padding, mode, key, iv);
   }
-  if (padding === false) {
+  Transform.call(this);
+  this._cache = new Splitter();
+   if (padding === false) {
     this._padding = false;
   } else {
     this._padding = true;
   }
-  Transform.call(this);
-  this.cache = new Buffer('');
-}
-
-Splitter.prototype._transform = function (data, _, next) {
-  this.cache = Buffer.concat([this.cache, data]);
-  var i = 0;
-  var len = this.cache.length;
-  while (i + 15 < len) {
-    this.push(this.cache.slice(i, i + 16));
-    i += 16;
-  }
-  if (i) {
-    this.cache = this.cache.slice(i);
-  }
-  next();
-};
-Splitter.prototype._flush = function (next) {
-  if (this._padding === false) {
-    this.push(this.cache);
-  }
-  next();
-};
-
-inherits(ECB, Transform);
-function ECB(key) {
-  if (!(this instanceof ECB)) {
-    return new ECB(key);
-  }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
   this._last = void 0;
-  this._pad = true;
-}
-
-ECB.prototype._transform = function (data, _, next) {
-  var last = this._last;
-  if (last) {
-    this.push(last);
-  }
-  this._last = this._cipher.decryptBlock(data);
-  next(null);
-};
-
-
-ECB.prototype._flush = function (next) {
-  this._cipher.scrub();
-  if (this._pad === false) {
-    if (this._last) {
-      this.push(this._last);
-    }
-    return next();
-  }
-  var depadded = unpad(this._last);
-  if (depadded) {
-    this.push(depadded);
-  }
-  next();
-};
-inherits(CBC, Transform);
-function CBC(key, iv) {
-  if (!(this instanceof CBC)) {
-    return new CBC(key, iv);
-  }
-  Transform.call(this);
   this._cipher = new aes.AES(key);
-  this._prev = iv;
-  this._last = void 0;
+  this._prev = new Buffer(iv.length);
+  iv.copy(this._prev);
+  this._mode = mode;
 }
-
-CBC.prototype._transform = function (data, _, next) {
-  var indata = data;
-  var out = this._cipher.decryptBlock(data);
-  if (this._last) {
-    this.push(this._last);
+Decipher.prototype._transform = function (data, _, next) {
+  this._cache.add(data);
+  var chunk;
+  var thing;
+  while ((chunk = this._cache.get())) {
+    thing = this._mode.decrypt(this, chunk);
+    this.push(thing);
   }
-  this._last = xor(out, this._prev);
-  this._prev = indata;
-  this._pad = true;
   next();
 };
-CBC.prototype._flush = function (next) {
-  this._cipher.scrub();
-  if (this._pad === false) {
-    if (this._last) {
-      this.push(this._last);
-    }
-    return next();
+Decipher.prototype._flush = function (next) {
+  var chunk = this._cache.flush();
+  if (!chunk) {
+    return next;
   }
-  var depadded = unpad(this._last);
-  if (depadded) {
-    this.push(depadded);
+  if (this._padding) {
+    this.push(unpad(this._mode.decrypt(this, chunk)));
+  } else {
+    this.push(this._mode.decrypt(this, chunk));
   }
   next();
 };
-inherits(CFB, Transform);
-function CFB(key, iv) {
-  if (!(this instanceof CFB)) {
-    return new CFB(key, iv);
+function Splitter() {
+   if (!(this instanceof Splitter)) {
+    return new Splitter();
   }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._prev = iv;
+  this.cache = new Buffer('');
 }
-
-CFB.prototype._transform = function (data, _, next) {
-  // yes encrypt
-  var pad = this._cipher.encryptBlock(this._prev);
-  this._prev = data;
-  next(null, xor(pad, data));
-};
-CFB.prototype._flush = function (next) {
-  this._cipher.scrub();
-  next();
+Splitter.prototype.add = function (data) {
+  this.cache = Buffer.concat([this.cache, data]);
 };
 
-//the same as encryption
-inherits(OFB, Transform);
-function OFB(key, iv) {
-  if (!(this instanceof OFB)) {
-    return new OFB(key, iv);
+Splitter.prototype.get = function () {
+  if (this.cache.length > 16) {
+    var out = this.cache.slice(0, 16);
+    this.cache = this.cache.slice(16);
+    return out;
   }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._prev = iv;
-}
-
-OFB.prototype._transform = function (data, _, next) {
-  this._prev = this._cipher.encryptBlock(this._prev);
-  next(null, xor(data, this._prev));
+  return null;
 };
-OFB.prototype._flush = function (next) {
-  this._cipher.scrub();
-  next();
+Splitter.prototype.flush = function () {
+  if (this.cache.length) {
+    return this.cache;
+  }
 };
-inherits(CTR, Transform);
-function CTR(key, iv) {
-  if (!(this instanceof CTR)) {
-    return new CTR(key, iv);
+function unpad(last) {
+  var padded = last[15];
+  if (padded === 16) {
+    return;
   }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._iv = new Buffer(iv.length);
-  iv.copy(this._iv);
+  return last.slice(0, 16 - padded);
 }
 
-CTR.prototype._transform = function (data, _, next) {
-  this.push(xor(data, this._cipher.encryptBlock(this._iv)));
-  this._incr32();
-  next();
-};
-CTR.prototype._flush = function (next) {
-  this._cipher.scrub();
-  this._iv.fill(0);
-  next();
-};
-CTR.prototype._incr32 = function () {
-  var len = this._iv.length;
-  var item;
-  while (--len) {
-    item = this._iv.readUInt8(len);
-    if (item === 255) {
-      this._iv.writeUInt8(0, len);
-    } else {
-      item++;
-      this._iv.writeUInt8(item, len);
-      break;
-    }
-  }
-};
-var modeStreams = {
-  ECB: ECB,
-  CBC: CBC,
-  CFB: CFB,
-  OFB: OFB,
-  CTR: CTR
+var modelist = {
+  ECB: require('./modes/ecb'),
+  CBC: require('./modes/cbc'),
+  CFB: require('./modes/cfb'),
+  OFB: require('./modes/ofb'),
+  CTR: require('./modes/ctr')
 };
 
 module.exports = function (crypto) {
@@ -218,49 +102,35 @@ module.exports = function (crypto) {
     if (iv.length !== config.iv) {
       throw new TypeError('invalid iv length ' + iv.length);
     }
-    var splitter = new Splitter(config.padding);
-    var stream = new modeStreams[config.mode](password, iv);
-    splitter.on('data', function (d) {
-      stream.write(d);
-    });
-    splitter.on('finish', function () {
-      stream.end();
-    });
-    var out = duplexer(splitter, stream);
-    out.setAutoPadding = function (padding) {
-      stream._padding = padding;
-    };
-    out._legacy = false;
-    var outData = new Buffer('');
-    out.update = function (data, inputEnd, outputEnc) {
-      if (out._legacy === false) {
-        out._legacy = true;
-        stream.on('data', function (chunk) {
-          outData = Buffer.concat([outData, chunk]);
-        });
-        stream.pause = function (){
-          // else it will stall out
-        };
+    var decipher = new Decipher(config.padding, modelist[config.mode], password, iv);
+
+    decipher.update = function (data, inputEnd, outputEnc) {
+      decipher.write(data, inputEnd);
+      var outData = new Buffer('');
+      var chunk;
+      while ((chunk = decipher.read())) {
+        outData = Buffer.concat([outData, chunk]);
       }
-      splitter.write(data, inputEnd);
-      var ourData = outData;
-      outData = new Buffer('');
       if (outputEnc) {
-        ourData = ourData.toString(outputEnc);
+        outData = outData.toString(outputEnc);
       }
-      return ourData;
+      return outData;
     };
-    out.final = function (outputEnc) {
-      splitter.end();
-      var ourData = outData;
-      outData = null;
+    decipher.final = function (outputEnc) {
+      decipher.end();
+      var outData = new Buffer('');
+      var chunk;
+      while ((chunk = decipher.read())) {
+        outData = Buffer.concat([outData, chunk]);
+      }
       if (outputEnc) {
-        ourData = ourData.toString(outputEnc);
+        outData = outData.toString(outputEnc);
       }
-      return ourData;
+      return outData;
     };
-    return out;
+    return decipher;
   }
+
   function createDecipher (suite, password) {
     var config = modes[suite];
     if (!config) {
diff --git a/encrypter.js b/encrypter.js
index 4f99a20..2f01eb1 100644
--- a/encrypter.js
+++ b/encrypter.js
@@ -1,13 +1,38 @@
 var aes = require('./aes');
 var Transform = require('stream').Transform;
 var inherits = require('inherits');
-var duplexer = require('duplexer2');
 var modes = require('./modes');
 var ebtk = require('./EVP_BytesToKey');
-var xor = require('./xor');
-inherits(Splitter, Transform);
+inherits(Cipher, Transform);
+function Cipher(padding, mode, key, iv) {
+  if (!(this instanceof Cipher)) {
+    return new Cipher(padding, mode, key, iv);
+  }
+  Transform.call(this);
+  this._cache = new Splitter(padding);
+  this._cipher = new aes.AES(key);
+  this._prev = new Buffer(iv.length);
+  iv.copy(this._prev);
+  this._mode = mode;
+}
+Cipher.prototype._transform = function (data, _, next) {
+  this._cache.add(data);
+  var chunk;
+  var thing;
+  while ((chunk = this._cache.get())) {
+    thing = this._mode.encrypt(this, chunk);
+    this.push(thing);
+  }
+  next();
+};
+Cipher.prototype._flush = function (next) {
+  var chunk = this._cache.flush();
+  this.push(this._mode.encrypt(this, chunk));
+  this._cipher.scrub();
+  next();
+};
 function Splitter(padding) {
-  if (!(this instanceof Splitter)) {
+   if (!(this instanceof Splitter)) {
     return new Splitter(padding);
   }
   if (padding === false) {
@@ -15,28 +40,23 @@ function Splitter(padding) {
   } else {
     this._padding = true;
   }
-  Transform.call(this);
   this.cache = new Buffer('');
 }
-
-Splitter.prototype._transform = function (data, _, next) {
+Splitter.prototype.add = function (data) {
   this.cache = Buffer.concat([this.cache, data]);
-  var i = 0;
-  var len = this.cache.length;
-  while (i + 15 < len) {
-    this.push(this.cache.slice(i, i + 16));
-    i += 16;
-  }
-  if (i) {
-    this.cache = this.cache.slice(i);
-  }
-  next();
 };
 
-Splitter.prototype._flush = function (next) {
+Splitter.prototype.get = function () {
+  if (this.cache.length > 15) {
+    var out = this.cache.slice(0, 16);
+    this.cache = this.cache.slice(16);
+    return out;
+  }
+  return null;
+};
+Splitter.prototype.flush = function () {
   if (!this._padding) {
-    this.push(this.cache);
-    return next();
+    return this.cache;
   }
   var len = 16 - this.cache.length;
   var padBuff = new Buffer(len);
@@ -46,125 +66,14 @@ Splitter.prototype._flush = function (next) {
     padBuff.writeUInt8(len, i);
   }
   var out = Buffer.concat([this.cache, padBuff]);
-  this.push(out);
-  next();
-};
-
-inherits(ECB, Transform);
-function ECB(key) {
-  if (!(this instanceof ECB)) {
-    return new ECB(key);
-  }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-}
-
-ECB.prototype._transform = function (data, _, next) {
-  var out = this._cipher.encryptBlock(data);
-  next(null, out);
-};
-ECB.prototype._flush = function (next) {
-  this._cipher.scrub();
-  next();
-};
-
-inherits(CBC, Transform);
-function CBC(key, iv) {
-  if (!(this instanceof CBC)) {
-    return new CBC(key, iv);
-  }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._prev = iv;
-}
-
-CBC.prototype._transform = function (data, _, next) {
-  data = xor(data, this._prev);
-  this._prev = this._cipher.encryptBlock(data);
-  next(null, this._prev);
-};
-CBC.prototype._flush = function (next) {
-  this._cipher.scrub();
-  next();
-};
-inherits(CFB, Transform);
-function CFB(key, iv) {
-  if (!(this instanceof CFB)) {
-    return new CFB(key, iv);
-  }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._prev = iv;
-}
-
-CFB.prototype._transform = function (data, _, next) {
-  var pad = this._cipher.encryptBlock(this._prev);
-  this._prev = xor(data, pad);
-  next(null, this._prev);
+  return out;
 };
-CFB.prototype._flush = function (next) {
-  this._cipher.scrub();
-  next();
-};
-inherits(OFB, Transform);
-function OFB(key, iv) {
-  if (!(this instanceof OFB)) {
-    return new OFB(key, iv);
-  }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._prev = iv;
-}
-
-OFB.prototype._transform = function (data, _, next) {
-  this._prev = this._cipher.encryptBlock(this._prev);
-  next(null, xor(data, this._prev));
-};
-OFB.prototype._flush = function (next) {
-  this._cipher.scrub();
-  next();
-};
-inherits(CTR, Transform);
-function CTR(key, iv) {
-  if (!(this instanceof CTR)) {
-    return new CTR(key, iv);
-  }
-  Transform.call(this);
-  this._cipher = new aes.AES(key);
-  this._iv = new Buffer(iv.length);
-  iv.copy(this._iv);
-}
-
-CTR.prototype._transform = function (data, _, next) {
-  this.push(xor(data, this._cipher.encryptBlock(this._iv)));
-  this._incr32();
-  next();
-};
-CTR.prototype._flush = function (next) {
-  this._cipher.scrub();
-  this._iv.fill(0);
-  next();
-};
-CTR.prototype._incr32 = function () {
-  var len = this._iv.length;
-  var item;
-  while (--len) {
-    item = this._iv.readUInt8(len);
-    if (item === 255) {
-      this._iv.writeUInt8(0, len);
-    } else {
-      item++;
-      this._iv.writeUInt8(item, len);
-      break;
-    }
-  }
-};
-var modeStreams = {
-  ECB: ECB,
-  CBC: CBC,
-  CFB: CFB,
-  OFB: OFB,
-  CTR: CTR
+var modelist = {
+  ECB: require('./modes/ecb'),
+  CBC: require('./modes/cbc'),
+  CFB: require('./modes/cfb'),
+  OFB: require('./modes/ofb'),
+  CTR: require('./modes/ctr')
 };
 module.exports = function (crypto) {
   function createCipheriv(suite, password, iv) {
@@ -184,48 +93,33 @@ module.exports = function (crypto) {
     if (iv.length !== config.iv) {
       throw new TypeError('invalid iv length ' + iv.length);
     }
-    var splitter = new Splitter(config.padding);
-    var stream = new modeStreams[config.mode](password, iv);
-    splitter.on('data', function (d) {
-      stream.write(d);
-    });
-    splitter.on('finish', function () {
-      stream.end();
-    });
-    var out = duplexer(splitter, stream);
-    out.setAutoPadding = function (padding) {
-      splitter._padding = padding;
-    };
-    out._legacy = false;
-    var outData = new Buffer('');
-    out.update = function (data, inputEnd, outputEnc) {
-      if (out._legacy === false) {
-        out._legacy = true;
-        stream.on('data', function (chunk) {
-          outData = Buffer.concat([outData, chunk]);
-        });
-        stream.pause = function (){
-          // else it will stall out
-        };
+    var cipher = new Cipher(config.padding, modelist[config.mode], password, iv);
+
+    cipher.update = function (data, inputEnd, outputEnc) {
+      cipher.write(data, inputEnd);
+      var outData = new Buffer('');
+      var chunk;
+      while ((chunk = cipher.read())) {
+        outData = Buffer.concat([outData, chunk]);
       }
-      splitter.write(data, inputEnd);
-      var ourData = outData;
-      outData = new Buffer('');
       if (outputEnc) {
-        ourData = ourData.toString(outputEnc);
+        outData = outData.toString(outputEnc);
       }
-      return ourData;
+      return outData;
     };
-    out.final = function (outputEnc) {
-      splitter.end();
-      var ourData = outData;
-      outData = null;
+    cipher.final = function (outputEnc) {
+      cipher.end();
+      var outData = new Buffer('');
+      var chunk;
+      while ((chunk = cipher.read())) {
+        outData = Buffer.concat([outData, chunk]);
+      }
       if (outputEnc) {
-        ourData = ourData.toString(outputEnc);
+        outData = outData.toString(outputEnc);
       }
-      return ourData;
+      return outData;
     };
-    return out;
+    return cipher;
   }
   function createCipher (suite, password) {
     var config = modes[suite];
diff --git a/modes/cbc.js b/modes/cbc.js
new file mode 100644
index 0000000..bdc3cbc
--- /dev/null
+++ b/modes/cbc.js
@@ -0,0 +1,12 @@
+var xor = require('../xor');
+exports.encrypt = function (self, block) {
+  var data = xor(block, self._prev);
+  self._prev = self._cipher.encryptBlock(data);
+  return self._prev;
+};
+exports.decrypt = function (self, block) {
+  var pad = self._prev;
+  self._prev = block;
+  var out = self._cipher.decryptBlock(block);
+  return xor(out, pad);
+};
\ No newline at end of file
diff --git a/modes/cfb.js b/modes/cfb.js
new file mode 100644
index 0000000..978b637
--- /dev/null
+++ b/modes/cfb.js
@@ -0,0 +1,12 @@
+var xor = require('../xor');
+exports.encrypt = function (self, block) {
+  var pad = self._cipher.encryptBlock(self._prev);
+  self._prev = xor(block, pad);
+  return self._prev;
+};
+exports.decrypt = function (self, block) {
+  // yes encrypt
+  var pad = self._cipher.encryptBlock(self._prev);
+  self._prev = block;
+  return xor(pad, block);
+};
\ No newline at end of file
diff --git a/modes/ctr.js b/modes/ctr.js
new file mode 100644
index 0000000..cbf3fe0
--- /dev/null
+++ b/modes/ctr.js
@@ -0,0 +1,20 @@
+var xor = require('../xor');
+exports.encrypt = exports.decrypt = function (self, block) {
+  var out = xor(block, self._cipher.encryptBlock(self._prev));
+  incr32(self._prev);
+  return out;
+};
+function incr32(iv) {
+  var len = iv.length;
+  var item;
+  while (len--) {
+    item = iv.readUInt8(len);
+    if (item === 255) {
+      iv.writeUInt8(0, len);
+    } else {
+      item++;
+      iv.writeUInt8(item, len);
+      break;
+    }
+  }
+}
\ No newline at end of file
diff --git a/modes/ecb.js b/modes/ecb.js
new file mode 100644
index 0000000..1cef515
--- /dev/null
+++ b/modes/ecb.js
@@ -0,0 +1,6 @@
+exports.encrypt = function (self, block) {
+  return self._cipher.encryptBlock(block);
+};
+exports.decrypt = function (self, block) {
+  return self._cipher.decryptBlock(block);
+};
\ No newline at end of file
diff --git a/modes/ofb.js b/modes/ofb.js
new file mode 100644
index 0000000..49d49b2
--- /dev/null
+++ b/modes/ofb.js
@@ -0,0 +1,5 @@
+var xor = require('../xor');
+exports.encrypt = exports.decrypt = function (self, block) {
+  self._prev = self._cipher.encryptBlock(self._prev);
+  return xor(block, self._prev);
+};
\ No newline at end of file
diff --git a/test.js b/test.js
deleted file mode 100644
index 9833aef..0000000
--- a/test.js
+++ /dev/null
@@ -1,17 +0,0 @@
-var createCipher = require('./index').createCipher;
-var encStream = createCipher('aes192', new Buffer('password'));
-var crypto = require('crypto');
-var decStream = crypto.createDecipher('aes192', new Buffer('password'));
-encStream.pipe(decStream).on('data', function (d) {
-  console.log(d.toString());
-});
-
-var data = [
-  "foo",
-  "abcdefghijklmnopqrstuvwxyz",
-  "You can disable automatic padding of the input data to block size. If auto_padding is false, the length of the entire input data must be a multiple of the cipher's block size or final will fail. Useful for non-standard padding, e.g. using 0x0 instead of PKCS padding. You must call this before cipher.final."
-];
-data.forEach(function (item) {
-  console.log(item);
-  encStream.write(item);
-});
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browserify-aes.git



More information about the Pkg-javascript-commits mailing list