[Pkg-javascript-commits] [node-browserify-aes] 61/92: standard
Bastien Roucariès
rouca at moszumanska.debian.org
Sun Jun 4 09:35:20 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 bf552981fa23c072db1bbca605305e21a5b6920f
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date: Mon May 4 12:46:51 2015 -0400
standard
---
.eslintrc | 172 +++++++++++
.jshintrc | 36 ---
EVP_BytesToKey.js | 94 +++---
aes.js | 293 +++++++++---------
authCipher.js | 124 ++++----
browser.js | 18 +-
cipherBase.js | 44 +--
decrypter.js | 147 +++++----
encrypter.js | 140 ++++-----
ghash.js | 112 +++----
index.js | 12 +-
modes.js | 54 ++--
modes/cbc.js | 20 +-
modes/cfb.js | 38 +--
modes/cfb1.js | 53 ++--
modes/cfb8.js | 22 +-
modes/ctr.js | 38 +--
modes/ecb.js | 8 +-
modes/ofb.js | 18 +-
populateFixtures.js | 40 +--
streamCipher.js | 38 +--
test/index.js | 850 ++++++++++++++++++++++++++--------------------------
xor.js | 16 +-
23 files changed, 1242 insertions(+), 1145 deletions(-)
diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..ffa074d
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,172 @@
+{
+ "ecmaFeatures": {
+ "globalReturn": true,
+ "modules": true
+ },
+
+ "env": {
+ "browser": false,
+ "es6": true,
+ "node": true,
+ },
+
+ "globals": {
+ "document": false,
+ "escape": false,
+ "navigator": false,
+ "unescape": false,
+ "window": false
+ },
+
+ "plugins": [
+ "react"
+ ],
+
+ "rules": {
+ "block-scoped-var": 0,
+ "brace-style": [2, "1tbs", { "allowSingleLine": true }],
+ "camelcase": 0,
+ "comma-dangle": [2, "never"],
+ "comma-spacing": [2, { "before": false, "after": true }],
+ "comma-style": [2, "last"],
+ "complexity": 0,
+ "consistent-return": 0,
+ "consistent-this": 0,
+ "curly": [2, "multi-line"],
+ "default-case": 0,
+ "dot-notation": 0,
+ "eol-last": 2,
+ "eqeqeq": [2, "allow-null"],
+ "func-names": 0,
+ "func-style": [0, "declaration"],
+ "generator-star": [2, "middle"],
+ "guard-for-in": 0,
+ "handle-callback-err": [2, "^(err|error|anySpecificError)$" ],
+ "indent": [2, 2],
+ "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
+ "max-depth": 0,
+ "max-len": 0,
+ "max-nested-callbacks": 0,
+ "max-params": 0,
+ "max-statements": 0,
+ "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
+ "new-parens": 2,
+ "no-alert": 0,
+ "no-array-constructor": 2,
+ "no-bitwise": 0,
+ "no-caller": 2,
+ "no-catch-shadow": 0,
+ "no-cond-assign": 2,
+ "no-console": 0,
+ "no-constant-condition": 0,
+ "no-control-regex": 2,
+ "no-debugger": 2,
+ "no-delete-var": 2,
+ "no-div-regex": 0,
+ "no-dupe-args": 2,
+ "no-dupe-keys": 2,
+ "no-duplicate-case": 2,
+ "no-else-return": 0,
+ "no-empty": 0,
+ "no-empty-class": 2,
+ "no-empty-label": 2,
+ "no-eq-null": 0,
+ "no-eval": 2,
+ "no-ex-assign": 2,
+ "no-extend-native": 2,
+ "no-extra-bind": 2,
+ "no-extra-boolean-cast": 2,
+ "no-extra-parens": 0,
+ "no-extra-semi": 0,
+ "no-extra-strict": 0,
+ "no-fallthrough": 2,
+ "no-floating-decimal": 2,
+ "no-func-assign": 2,
+ "no-implied-eval": 2,
+ "no-inline-comments": 0,
+ "no-inner-declarations": [2, "functions"],
+ "no-invalid-regexp": 2,
+ "no-irregular-whitespace": 2,
+ "no-iterator": 2,
+ "no-label-var": 2,
+ "no-labels": 2,
+ "no-lone-blocks": 0,
+ "no-lonely-if": 0,
+ "no-loop-func": 0,
+ "no-mixed-requires": [0, false],
+ "no-mixed-spaces-and-tabs": [2, false],
+ "no-multi-spaces": 2,
+ "no-multi-str": 2,
+ "no-multiple-empty-lines": [2, { "max": 1 }],
+ "no-native-reassign": 2,
+ "no-negated-in-lhs": 2,
+ "no-nested-ternary": 0,
+ "no-new": 2,
+ "no-new-func": 2,
+ "no-new-object": 2,
+ "no-new-require": 2,
+ "no-new-wrappers": 2,
+ "no-obj-calls": 2,
+ "no-octal": 2,
+ "no-octal-escape": 2,
+ "no-path-concat": 0,
+ "no-plusplus": 0,
+ "no-process-env": 0,
+ "no-process-exit": 0,
+ "no-proto": 2,
+ "no-redeclare": 2,
+ "no-regex-spaces": 2,
+ "no-reserved-keys": 0,
+ "no-restricted-modules": 0,
+ "no-return-assign": 2,
+ "no-script-url": 0,
+ "no-self-compare": 2,
+ "no-sequences": 2,
+ "no-shadow": 0,
+ "no-shadow-restricted-names": 2,
+ "no-spaced-func": 2,
+ "no-sparse-arrays": 2,
+ "no-sync": 0,
+ "no-ternary": 0,
+ "no-throw-literal": 2,
+ "no-trailing-spaces": 2,
+ "no-undef": 2,
+ "no-undef-init": 2,
+ "no-undefined": 0,
+ "no-underscore-dangle": 0,
+ "no-unreachable": 2,
+ "no-unused-expressions": 0,
+ "no-unused-vars": [2, { "vars": "all", "args": "none" }],
+ "no-use-before-define": 0,
+ "no-var": 0,
+ "no-void": 0,
+ "no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }],
+ "no-with": 2,
+ "no-wrap-func": 2,
+ "one-var": 0,
+ "operator-assignment": [0, "always"],
+ "padded-blocks": [2, "never"],
+ "quote-props": 0,
+ "quotes": [2, "single", "avoid-escape"],
+ "radix": 2,
+ "semi": [2, "never"],
+ "semi-spacing": 0,
+ "sort-vars": 0,
+ "space-after-keywords": [2, "always"],
+ "space-before-blocks": [2, "always"],
+ "space-in-brackets": 0,
+ "space-in-parens": [2, "never"],
+ "space-infix-ops": 2,
+ "space-return-throw-case": 2,
+ "space-unary-ops": [2, { "words": true, "nonwords": false }],
+ "spaced-line-comment": [2, "always"],
+ "strict": 0,
+ "use-isnan": 2,
+ "valid-jsdoc": 0,
+ "valid-typeof": 2,
+ "vars-on-top": 0,
+ "wrap-iife": [2, "any"],
+ "wrap-regex": 0,
+ "yoda": [2, "never"]
+ }
+}
diff --git a/.jshintrc b/.jshintrc
deleted file mode 100644
index 2d7234e..0000000
--- a/.jshintrc
+++ /dev/null
@@ -1,36 +0,0 @@
-{
- "predef": {
- "document": true,
- "window": true,
- "FeldmanENV": true,
- "google": true,
- "MarkerClusterer": true
- },
- "browser" : true,
- "node": true,
- "boss" : true,
- "curly": true,
- "debug": false,
- "devel": true,
- "esnext": true,
- "eqeqeq": true,
- "evil": true,
- "forin": false,
- "immed": false,
- "laxbreak": false,
- "newcap": true,
- "noarg": true,
- "noempty": false,
- "nonew": false,
- "nomen": false,
- "onevar": false,
- "plusplus": false,
- "regexp": false,
- "undef": true,
- "sub": true,
- "strict": false,
- "white": false,
- "eqnull": true,
- "esnext": true,
- "unused": true
-}
\ No newline at end of file
diff --git a/EVP_BytesToKey.js b/EVP_BytesToKey.js
index c55cbe3..0788128 100644
--- a/EVP_BytesToKey.js
+++ b/EVP_BytesToKey.js
@@ -1,62 +1,62 @@
-var md5 = require('create-hash/md5');
-module.exports = EVP_BytesToKey;
-function EVP_BytesToKey(password, keyLen, ivLen) {
+var md5 = require('create-hash/md5')
+module.exports = EVP_BytesToKey
+function EVP_BytesToKey (password, keyLen, ivLen) {
if (!Buffer.isBuffer(password)) {
- password = new Buffer(password, 'binary');
+ password = new Buffer(password, 'binary')
}
- keyLen = keyLen/8;
- ivLen = ivLen || 0;
- var ki = 0;
- var ii = 0;
- var key = new Buffer(keyLen);
- var iv = new Buffer(ivLen);
- var addmd = 0;
- var md_buf;
- var i;
- var bufs = [];
+ keyLen = keyLen / 8
+ ivLen = ivLen || 0
+ var ki = 0
+ var ii = 0
+ var key = new Buffer(keyLen)
+ var iv = new Buffer(ivLen)
+ var addmd = 0
+ var md_buf
+ var i
+ var bufs = []
while (true) {
- if(addmd++ > 0) {
- bufs.push(md_buf);
+ if (addmd++ > 0) {
+ bufs.push(md_buf)
}
- bufs.push(password);
- md_buf = md5(Buffer.concat(bufs));
- bufs = [];
- i = 0;
- if(keyLen > 0) {
- while(true) {
- if(keyLen === 0) {
- break;
+ bufs.push(password)
+ md_buf = md5(Buffer.concat(bufs))
+ bufs = []
+ i = 0
+ if (keyLen > 0) {
+ while (true) {
+ if (keyLen === 0) {
+ break
}
- if(i === md_buf.length) {
- break;
+ if (i === md_buf.length) {
+ break
}
- key[ki++] = md_buf[i];
- keyLen--;
- i++;
- }
+ key[ki++] = md_buf[i]
+ keyLen--
+ i++
+ }
}
- if(ivLen > 0 && i !== md_buf.length) {
- while(true) {
- if(ivLen === 0) {
- break;
+ if (ivLen > 0 && i !== md_buf.length) {
+ while (true) {
+ if (ivLen === 0) {
+ break
}
- if(i === md_buf.length) {
- break;
+ if (i === md_buf.length) {
+ break
}
- iv[ii++] = md_buf[i];
- ivLen--;
- i++;
- }
- }
- if(keyLen === 0 && ivLen === 0) {
- break;
+ iv[ii++] = md_buf[i]
+ ivLen--
+ i++
+ }
+ }
+ if (keyLen === 0 && ivLen === 0) {
+ break
}
}
- for(i=0;i<md_buf.length;i++) {
- md_buf[i] = 0;
+ for (i = 0; i < md_buf.length; i++) {
+ md_buf[i] = 0
}
return {
key: key,
iv: iv
- };
-}
\ No newline at end of file
+ }
+}
diff --git a/aes.js b/aes.js
index ff3314e..4829057 100644
--- a/aes.js
+++ b/aes.js
@@ -4,195 +4,174 @@
// which is in turn based on the one from crypto-js
// https://code.google.com/p/crypto-js/
-var uint_max = Math.pow(2, 32);
-function fixup_uint32(x) {
- var ret, x_pos;
- ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x;
- return ret;
+var uint_max = Math.pow(2, 32)
+function fixup_uint32 (x) {
+ var ret, x_pos
+ ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x
+ return ret
}
-function scrub_vec(v) {
- var i, _i, _ref;
- for (i = _i = 0, _ref = v.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
- v[i] = 0;
+function scrub_vec (v) {
+ for (var i = 0; i < v.length; v++) {
+ v[i] = 0
}
- return false;
+ return false
}
-function Global() {
- var i;
- this.SBOX = [];
- this.INV_SBOX = [];
- this.SUB_MIX = (function() {
- var _i, _results;
- _results = [];
- for (i = _i = 0; _i < 4; i = ++_i) {
- _results.push([]);
- }
- return _results;
- })();
- this.INV_SUB_MIX = (function() {
- var _i, _results;
- _results = [];
- for (i = _i = 0; _i < 4; i = ++_i) {
- _results.push([]);
- }
- return _results;
- })();
- this.init();
- this.RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
+function Global () {
+ this.SBOX = []
+ this.INV_SBOX = []
+ this.SUB_MIX = [[], [], [], []]
+ this.INV_SUB_MIX = [[], [], [], []]
+ this.init()
+ this.RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]
}
-Global.prototype.init = function() {
- var d, i, sx, t, x, x2, x4, x8, xi, _i;
- d = (function() {
- var _i, _results;
- _results = [];
+Global.prototype.init = function () {
+ var d, i, sx, t, x, x2, x4, x8, xi, _i
+ d = (function () {
+ var _i, _results
+ _results = []
for (i = _i = 0; _i < 256; i = ++_i) {
if (i < 128) {
- _results.push(i << 1);
+ _results.push(i << 1)
} else {
- _results.push((i << 1) ^ 0x11b);
+ _results.push((i << 1) ^ 0x11b)
}
}
- return _results;
- })();
- x = 0;
- xi = 0;
+ return _results
+ })()
+ x = 0
+ xi = 0
for (i = _i = 0; _i < 256; i = ++_i) {
- sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
- sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
- this.SBOX[x] = sx;
- this.INV_SBOX[sx] = x;
- x2 = d[x];
- x4 = d[x2];
- x8 = d[x4];
- t = (d[sx] * 0x101) ^ (sx * 0x1010100);
- this.SUB_MIX[0][x] = (t << 24) | (t >>> 8);
- this.SUB_MIX[1][x] = (t << 16) | (t >>> 16);
- this.SUB_MIX[2][x] = (t << 8) | (t >>> 24);
- this.SUB_MIX[3][x] = t;
- t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
- this.INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8);
- this.INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16);
- this.INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24);
- this.INV_SUB_MIX[3][sx] = t;
+ sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4)
+ sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63
+ this.SBOX[x] = sx
+ this.INV_SBOX[sx] = x
+ x2 = d[x]
+ x4 = d[x2]
+ x8 = d[x4]
+ t = (d[sx] * 0x101) ^ (sx * 0x1010100)
+ this.SUB_MIX[0][x] = (t << 24) | (t >>> 8)
+ this.SUB_MIX[1][x] = (t << 16) | (t >>> 16)
+ this.SUB_MIX[2][x] = (t << 8) | (t >>> 24)
+ this.SUB_MIX[3][x] = t
+ t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100)
+ this.INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8)
+ this.INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16)
+ this.INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24)
+ this.INV_SUB_MIX[3][sx] = t
if (x === 0) {
- x = xi = 1;
+ x = xi = 1
} else {
- x = x2 ^ d[d[d[x8 ^ x2]]];
- xi ^= d[d[xi]];
+ x = x2 ^ d[d[d[x8 ^ x2]]]
+ xi ^= d[d[xi]]
}
}
- return true;
-};
-
-var G = new Global();
+ return true
+}
+var G = new Global()
-AES.blockSize = 4 * 4;
+AES.blockSize = 4 * 4
-AES.prototype.blockSize = AES.blockSize;
+AES.prototype.blockSize = AES.blockSize
-AES.keySize = 256 / 8;
+AES.keySize = 256 / 8
-AES.prototype.keySize = AES.keySize;
+AES.prototype.keySize = AES.keySize
- function bufferToArray(buf) {
- var len = buf.length/4;
- var out = new Array(len);
- var i = -1;
+function bufferToArray (buf) {
+ var len = buf.length / 4
+ var out = new Array(len)
+ var i = -1
while (++i < len) {
- out[i] = buf.readUInt32BE(i * 4);
+ out[i] = buf.readUInt32BE(i * 4)
}
- return out;
- }
-function AES(key) {
- this._key = bufferToArray(key);
- this._doReset();
+ return out
+}
+function AES (key) {
+ this._key = bufferToArray(key)
+ this._doReset()
}
-AES.prototype._doReset = function() {
- var invKsRow, keySize, keyWords, ksRow, ksRows, t, _i, _j;
- keyWords = this._key;
- keySize = keyWords.length;
- this._nRounds = keySize + 6;
- ksRows = (this._nRounds + 1) * 4;
- this._keySchedule = [];
- for (ksRow = _i = 0; 0 <= ksRows ? _i < ksRows : _i > ksRows; ksRow = 0 <= ksRows ? ++_i : --_i) {
- this._keySchedule[ksRow] = ksRow < keySize ? keyWords[ksRow] : (t = this._keySchedule[ksRow - 1], (ksRow % keySize) === 0 ? (t = (t << 8) | (t >>> 24), t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff], t ^= G.RCON[(ksRow / keySize) | 0] << 24) : keySize > 6 && ksRow % keySize === 4 ? t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff] : void 0, this._ [...]
+AES.prototype._doReset = function () {
+ var invKsRow, keySize, keyWords, ksRow, ksRows, t
+ keyWords = this._key
+ keySize = keyWords.length
+ this._nRounds = keySize + 6
+ ksRows = (this._nRounds + 1) * 4
+ this._keySchedule = []
+ for (ksRow = 0; ksRow < ksRows; ksRow++) {
+ this._keySchedule[ksRow] = ksRow < keySize ? keyWords[ksRow] : (t = this._keySchedule[ksRow - 1], (ksRow % keySize) === 0 ? (t = (t << 8) | (t >>> 24), t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff], t ^= G.RCON[(ksRow / keySize) | 0] << 24) : keySize > 6 && ksRow % keySize === 4 ? t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff] : void 0, this._ [...]
}
- this._invKeySchedule = [];
- for (invKsRow = _j = 0; 0 <= ksRows ? _j < ksRows : _j > ksRows; invKsRow = 0 <= ksRows ? ++_j : --_j) {
- ksRow = ksRows - invKsRow;
- t = this._keySchedule[ksRow - (invKsRow % 4 ? 0 : 4)];
- this._invKeySchedule[invKsRow] = invKsRow < 4 || ksRow <= 4 ? t : G.INV_SUB_MIX[0][G.SBOX[t >>> 24]] ^ G.INV_SUB_MIX[1][G.SBOX[(t >>> 16) & 0xff]] ^ G.INV_SUB_MIX[2][G.SBOX[(t >>> 8) & 0xff]] ^ G.INV_SUB_MIX[3][G.SBOX[t & 0xff]];
+ this._invKeySchedule = []
+ for (invKsRow = 0; invKsRow < ksRows; invKsRow++) {
+ ksRow = ksRows - invKsRow
+ t = this._keySchedule[ksRow - (invKsRow % 4 ? 0 : 4)]
+ this._invKeySchedule[invKsRow] = invKsRow < 4 || ksRow <= 4 ? t : G.INV_SUB_MIX[0][G.SBOX[t >>> 24]] ^ G.INV_SUB_MIX[1][G.SBOX[(t >>> 16) & 0xff]] ^ G.INV_SUB_MIX[2][G.SBOX[(t >>> 8) & 0xff]] ^ G.INV_SUB_MIX[3][G.SBOX[t & 0xff]]
}
- return true;
-};
-
-AES.prototype.encryptBlock = function(M) {
- M = bufferToArray(new Buffer(M));
- var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX);
- var buf = new Buffer(16);
- buf.writeUInt32BE(out[0], 0);
- buf.writeUInt32BE(out[1], 4);
- buf.writeUInt32BE(out[2], 8);
- buf.writeUInt32BE(out[3], 12);
- return buf;
-};
-
-AES.prototype.decryptBlock = function(M) {
- M = bufferToArray(new Buffer(M));
- var temp = [M[3], M[1]];
- M[1] = temp[0];
- M[3] = temp[1];
- var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX);
- var buf = new Buffer(16);
- buf.writeUInt32BE(out[0], 0);
- buf.writeUInt32BE(out[3], 4);
- buf.writeUInt32BE(out[2], 8);
- buf.writeUInt32BE(out[1], 12);
- return buf;
-};
-
-AES.prototype.scrub = function() {
- scrub_vec(this._keySchedule);
- scrub_vec(this._invKeySchedule);
- scrub_vec(this._key);
-};
-
-AES.prototype._doCryptBlock = function(M, keySchedule, SUB_MIX, SBOX) {
- var ksRow, round, s0, s1, s2, s3, t0, t1, t2, t3, _i, _ref;
-
- s0 = M[0] ^ keySchedule[0];
- s1 = M[1] ^ keySchedule[1];
- s2 = M[2] ^ keySchedule[2];
- s3 = M[3] ^ keySchedule[3];
- ksRow = 4;
- for (round = _i = 1, _ref = this._nRounds; 1 <= _ref ? _i < _ref : _i > _ref; round = 1 <= _ref ? ++_i : --_i) {
- t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++];
- t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++];
- t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++];
- t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++];
- s0 = t0;
- s1 = t1;
- s2 = t2;
- s3 = t3;
+ return true
+}
+
+AES.prototype.encryptBlock = function (M) {
+ M = bufferToArray(new Buffer(M))
+ var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX)
+ var buf = new Buffer(16)
+ buf.writeUInt32BE(out[0], 0)
+ buf.writeUInt32BE(out[1], 4)
+ buf.writeUInt32BE(out[2], 8)
+ buf.writeUInt32BE(out[3], 12)
+ return buf
+}
+
+AES.prototype.decryptBlock = function (M) {
+ M = bufferToArray(new Buffer(M))
+ var temp = [M[3], M[1]]
+ M[1] = temp[0]
+ M[3] = temp[1]
+ var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX)
+ var buf = new Buffer(16)
+ buf.writeUInt32BE(out[0], 0)
+ buf.writeUInt32BE(out[3], 4)
+ buf.writeUInt32BE(out[2], 8)
+ buf.writeUInt32BE(out[1], 12)
+ return buf
+}
+
+AES.prototype.scrub = function () {
+ scrub_vec(this._keySchedule)
+ scrub_vec(this._invKeySchedule)
+ scrub_vec(this._key)
+}
+
+AES.prototype._doCryptBlock = function (M, keySchedule, SUB_MIX, SBOX) {
+ var ksRow, s0, s1, s2, s3, t0, t1, t2, t3
+
+ s0 = M[0] ^ keySchedule[0]
+ s1 = M[1] ^ keySchedule[1]
+ s2 = M[2] ^ keySchedule[2]
+ s3 = M[3] ^ keySchedule[3]
+ ksRow = 4
+ for (var round = 1; round < this._nRounds; round++) {
+ t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++]
+ t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++]
+ t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++]
+ t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++]
+ s0 = t0
+ s1 = t1
+ s2 = t2
+ s3 = t3
}
- t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
- t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
- t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
- t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
+ t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]
+ t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]
+ t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]
+ t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]
return [
fixup_uint32(t0),
fixup_uint32(t1),
fixup_uint32(t2),
fixup_uint32(t3)
- ];
-
-};
-
-
-
+ ]
+}
- exports.AES = AES;
\ No newline at end of file
+exports.AES = AES
diff --git a/authCipher.js b/authCipher.js
index dc1b993..ee01feb 100644
--- a/authCipher.js
+++ b/authCipher.js
@@ -1,99 +1,97 @@
-var aes = require('./aes');
-var Transform = require('./cipherBase');
-var inherits = require('inherits');
-var GHASH = require('./ghash');
-var xor = require('./xor');
-inherits(StreamCipher, Transform);
-module.exports = StreamCipher;
+var aes = require('./aes')
+var Transform = require('./cipherBase')
+var inherits = require('inherits')
+var GHASH = require('./ghash')
+var xor = require('./xor')
+inherits(StreamCipher, Transform)
+module.exports = StreamCipher
-function StreamCipher(mode, key, iv, decrypt) {
+function StreamCipher (mode, key, iv, decrypt) {
if (!(this instanceof StreamCipher)) {
- return new StreamCipher(mode, key, iv);
+ return new StreamCipher(mode, key, iv)
}
- Transform.call(this);
- this._finID = Buffer.concat([iv, new Buffer([0, 0, 0, 1])]);
- iv = Buffer.concat([iv, new Buffer([0, 0, 0, 2])]);
- this._cipher = new aes.AES(key);
- this._prev = new Buffer(iv.length);
- this._cache = new Buffer('');
- this._secCache = new Buffer('');
- this._decrypt = decrypt;
- this._alen = 0;
- this._len = 0;
- iv.copy(this._prev);
- this._mode = mode;
- var h = new Buffer(4);
- h.fill(0);
- this._ghash = new GHASH(this._cipher.encryptBlock(h));
- this._authTag = null;
- this._called = false;
+ Transform.call(this)
+ this._finID = Buffer.concat([iv, new Buffer([0, 0, 0, 1])])
+ iv = Buffer.concat([iv, new Buffer([0, 0, 0, 2])])
+ this._cipher = new aes.AES(key)
+ this._prev = new Buffer(iv.length)
+ this._cache = new Buffer('')
+ this._secCache = new Buffer('')
+ this._decrypt = decrypt
+ this._alen = 0
+ this._len = 0
+ iv.copy(this._prev)
+ this._mode = mode
+ var h = new Buffer(4)
+ h.fill(0)
+ this._ghash = new GHASH(this._cipher.encryptBlock(h))
+ this._authTag = null
+ this._called = false
}
StreamCipher.prototype._update = function (chunk) {
if (!this._called && this._alen) {
- var rump = 16 - (this._alen % 16);
- if (rump <16) {
- rump = new Buffer(rump);
- rump.fill(0);
- this._ghash.update(rump);
+ var rump = 16 - (this._alen % 16)
+ if (rump < 16) {
+ rump = new Buffer(rump)
+ rump.fill(0)
+ this._ghash.update(rump)
}
}
- this._called = true;
- var out = this._mode.encrypt(this, chunk);
+ this._called = true
+ var out = this._mode.encrypt(this, chunk)
if (this._decrypt) {
- this._ghash.update(chunk);
+ this._ghash.update(chunk)
} else {
- this._ghash.update(out);
+ this._ghash.update(out)
}
- this._len += chunk.length;
- return out;
-};
+ this._len += chunk.length
+ return out
+}
StreamCipher.prototype._final = function () {
if (this._decrypt && !this._authTag) {
- throw new Error('Unsupported state or unable to authenticate data');
+ throw new Error('Unsupported state or unable to authenticate data')
}
- var tag = xor(this._ghash.final(this._alen * 8, this._len * 8), this._cipher.encryptBlock(this._finID));
+ var tag = xor(this._ghash.final(this._alen * 8, this._len * 8), this._cipher.encryptBlock(this._finID))
if (this._decrypt) {
if (xorTest(tag, this._authTag)) {
- throw new Error('Unsupported state or unable to authenticate data');
+ throw new Error('Unsupported state or unable to authenticate data')
}
} else {
- this._authTag = tag;
+ this._authTag = tag
}
- this._cipher.scrub();
-};
+ this._cipher.scrub()
+}
StreamCipher.prototype.getAuthTag = function getAuthTag () {
if (!this._decrypt && Buffer.isBuffer(this._authTag)) {
- return this._authTag;
+ return this._authTag
} else {
- throw new Error('Attempting to get auth tag in unsupported state');
+ throw new Error('Attempting to get auth tag in unsupported state')
}
-};
+}
StreamCipher.prototype.setAuthTag = function setAuthTag (tag) {
if (this._decrypt) {
- this._authTag = tag;
+ this._authTag = tag
} else {
- throw new Error('Attempting to set auth tag in unsupported state');
+ throw new Error('Attempting to set auth tag in unsupported state')
}
-};
+}
StreamCipher.prototype.setAAD = function setAAD (buf) {
if (!this._called) {
- this._ghash.update(buf);
- this._alen += buf.length;
+ this._ghash.update(buf)
+ this._alen += buf.length
} else {
- throw new Error('Attempting to set AAD in unsupported state');
+ throw new Error('Attempting to set AAD in unsupported state')
}
-};
-function xorTest(a, b) {
- var out = 0;
+}
+function xorTest (a, b) {
+ var out = 0
if (a.length !== b.length) {
- out++;
+ out++
}
- var len = Math.min(a.length, b.length);
- var i = -1;
+ var len = Math.min(a.length, b.length)
+ var i = -1
while (++i < len) {
- out += (a[i] ^ b[i]);
+ out += (a[i] ^ b[i])
}
- return out;
+ return out
}
-
-
diff --git a/browser.js b/browser.js
index e9cbf4b..a058a84 100644
--- a/browser.js
+++ b/browser.js
@@ -1,11 +1,11 @@
-var ciphers = require('./encrypter');
-exports.createCipher = exports.Cipher = ciphers.createCipher;
-exports.createCipheriv = exports.Cipheriv = ciphers.createCipheriv;
-var deciphers = require('./decrypter');
-exports.createDecipher = exports.Decipher = deciphers.createDecipher;
-exports.createDecipheriv = exports.Decipheriv = deciphers.createDecipheriv;
-var modes = require('./modes');
+var ciphers = require('./encrypter')
+exports.createCipher = exports.Cipher = ciphers.createCipher
+exports.createCipheriv = exports.Cipheriv = ciphers.createCipheriv
+var deciphers = require('./decrypter')
+exports.createDecipher = exports.Decipher = deciphers.createDecipher
+exports.createDecipheriv = exports.Decipheriv = deciphers.createDecipheriv
+var modes = require('./modes')
function getCiphers () {
- return Object.keys(modes);
+ return Object.keys(modes)
}
-exports.listCiphers = exports.getCiphers = getCiphers;
+exports.listCiphers = exports.getCiphers = getCiphers
diff --git a/cipherBase.js b/cipherBase.js
index 0983a72..45450f8 100644
--- a/cipherBase.js
+++ b/cipherBase.js
@@ -1,37 +1,37 @@
-var Transform = require('stream').Transform;
-var inherits = require('inherits');
+var Transform = require('stream').Transform
+var inherits = require('inherits')
-module.exports = CipherBase;
-inherits(CipherBase, Transform);
-function CipherBase() {
- Transform.call(this);
+module.exports = CipherBase
+inherits(CipherBase, Transform)
+function CipherBase () {
+ Transform.call(this)
}
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
if (typeof data === 'string') {
- data = new Buffer(data, inputEnc);
+ data = new Buffer(data, inputEnc)
}
- var outData = this._update(data);
+ var outData = this._update(data)
if (outputEnc) {
- outData = outData.toString(outputEnc);
+ outData = outData.toString(outputEnc)
}
- return outData;
-};
+ return outData
+}
CipherBase.prototype._transform = function (data, _, next) {
- this.push(this._update(data));
- next();
-};
+ this.push(this._update(data))
+ next()
+}
CipherBase.prototype._flush = function (next) {
try {
- this.push(this._final());
+ this.push(this._final())
} catch(e) {
- return next(e);
+ return next(e)
}
- next();
-};
+ next()
+}
CipherBase.prototype.final = function (outputEnc) {
- var outData = this._final() || new Buffer('');
+ var outData = this._final() || new Buffer('')
if (outputEnc) {
- outData = outData.toString(outputEnc);
+ outData = outData.toString(outputEnc)
}
- return outData;
-};
\ No newline at end of file
+ return outData
+}
diff --git a/decrypter.js b/decrypter.js
index 4942b4d..983c218 100644
--- a/decrypter.js
+++ b/decrypter.js
@@ -1,91 +1,91 @@
-var aes = require('./aes');
-var Transform = require('./cipherBase');
-var inherits = require('inherits');
-var modes = require('./modes');
-var StreamCipher = require('./streamCipher');
-var AuthCipher = require('./authCipher');
-var ebtk = require('./EVP_BytesToKey');
+var aes = require('./aes')
+var Transform = require('./cipherBase')
+var inherits = require('inherits')
+var modes = require('./modes')
+var StreamCipher = require('./streamCipher')
+var AuthCipher = require('./authCipher')
+var ebtk = require('./EVP_BytesToKey')
-inherits(Decipher, Transform);
-function Decipher(mode, key, iv) {
+inherits(Decipher, Transform)
+function Decipher (mode, key, iv) {
if (!(this instanceof Decipher)) {
- return new Decipher(mode, key, iv);
+ return new Decipher(mode, key, iv)
}
- Transform.call(this);
- this._cache = new Splitter();
- this._last = void 0;
- this._cipher = new aes.AES(key);
- this._prev = new Buffer(iv.length);
- iv.copy(this._prev);
- this._mode = mode;
- this._autopadding = true;
+ Transform.call(this)
+ this._cache = new Splitter()
+ this._last = void 0
+ this._cipher = new aes.AES(key)
+ this._prev = new Buffer(iv.length)
+ iv.copy(this._prev)
+ this._mode = mode
+ this._autopadding = true
}
Decipher.prototype._update = function (data) {
- this._cache.add(data);
- var chunk;
- var thing;
- var out = [];
+ this._cache.add(data)
+ var chunk
+ var thing
+ var out = []
while ((chunk = this._cache.get(this._autopadding))) {
- thing = this._mode.decrypt(this, chunk);
- out.push(thing);
+ thing = this._mode.decrypt(this, chunk)
+ out.push(thing)
}
- return Buffer.concat(out);
-};
+ return Buffer.concat(out)
+}
Decipher.prototype._final = function () {
- var chunk = this._cache.flush();
+ var chunk = this._cache.flush()
if (this._autopadding) {
- return unpad(this._mode.decrypt(this, chunk));
+ return unpad(this._mode.decrypt(this, chunk))
} else if (chunk) {
- throw new Error('data not multiple of block length');
+ throw new Error('data not multiple of block length')
}
-};
+}
Decipher.prototype.setAutoPadding = function (setTo) {
- this._autopadding = !!setTo;
-};
-function Splitter() {
- if (!(this instanceof Splitter)) {
- return new Splitter();
+ this._autopadding = !!setTo
+}
+function Splitter () {
+ if (!(this instanceof Splitter)) {
+ return new Splitter()
}
- this.cache = new Buffer('');
+ this.cache = new Buffer('')
}
Splitter.prototype.add = function (data) {
- this.cache = Buffer.concat([this.cache, data]);
-};
+ this.cache = Buffer.concat([this.cache, data])
+}
Splitter.prototype.get = function (autoPadding) {
- var out;
+ var out
if (autoPadding) {
if (this.cache.length > 16) {
- out = this.cache.slice(0, 16);
- this.cache = this.cache.slice(16);
- return out;
+ out = this.cache.slice(0, 16)
+ this.cache = this.cache.slice(16)
+ return out
}
} else {
if (this.cache.length >= 16) {
- out = this.cache.slice(0, 16);
- this.cache = this.cache.slice(16);
- return out;
+ out = this.cache.slice(0, 16)
+ this.cache = this.cache.slice(16)
+ return out
}
}
- return null;
-};
+ return null
+}
Splitter.prototype.flush = function () {
if (this.cache.length) {
- return this.cache;
+ return this.cache
}
-};
-function unpad(last) {
- var padded = last[15];
- var i = -1;
+}
+function unpad (last) {
+ var padded = last[15]
+ var i = -1
while (++i < padded) {
if (last[(i + (16 - padded))] !== padded) {
- throw new Error('unable to decrypt data');
+ throw new Error('unable to decrypt data')
}
}
if (padded === 16) {
- return;
+ return
}
- return last.slice(0, 16 - padded);
+ return last.slice(0, 16 - padded)
}
var modelist = {
@@ -97,41 +97,40 @@ var modelist = {
OFB: require('./modes/ofb'),
CTR: require('./modes/ctr'),
GCM: require('./modes/ctr')
-};
-
+}
-function createDecipheriv(suite, password, iv) {
- var config = modes[suite.toLowerCase()];
+function createDecipheriv (suite, password, iv) {
+ var config = modes[suite.toLowerCase()]
if (!config) {
- throw new TypeError('invalid suite type');
+ throw new TypeError('invalid suite type')
}
if (typeof iv === 'string') {
- iv = new Buffer(iv);
+ iv = new Buffer(iv)
}
if (typeof password === 'string') {
- password = new Buffer(password);
+ password = new Buffer(password)
}
- if (password.length !== config.key/8) {
- throw new TypeError('invalid key length ' + password.length);
+ if (password.length !== config.key / 8) {
+ throw new TypeError('invalid key length ' + password.length)
}
if (iv.length !== config.iv) {
- throw new TypeError('invalid iv length ' + iv.length);
+ throw new TypeError('invalid iv length ' + iv.length)
}
if (config.type === 'stream') {
- return new StreamCipher(modelist[config.mode], password, iv, true);
+ return new StreamCipher(modelist[config.mode], password, iv, true)
} else if (config.type === 'auth') {
- return new AuthCipher(modelist[config.mode], password, iv, true);
+ return new AuthCipher(modelist[config.mode], password, iv, true)
}
- return new Decipher(modelist[config.mode], password, iv);
+ return new Decipher(modelist[config.mode], password, iv)
}
function createDecipher (suite, password) {
- var config = modes[suite.toLowerCase()];
+ var config = modes[suite.toLowerCase()]
if (!config) {
- throw new TypeError('invalid suite type');
+ throw new TypeError('invalid suite type')
}
- var keys = ebtk(password, config.key, config.iv);
- return createDecipheriv(suite, keys.key, keys.iv);
+ var keys = ebtk(password, config.key, config.iv)
+ return createDecipheriv(suite, keys.key, keys.iv)
}
-exports.createDecipher = createDecipher;
-exports.createDecipheriv = createDecipheriv;
\ No newline at end of file
+exports.createDecipher = createDecipher
+exports.createDecipheriv = createDecipheriv
diff --git a/encrypter.js b/encrypter.js
index 4212b3e..a74e257 100644
--- a/encrypter.js
+++ b/encrypter.js
@@ -1,78 +1,78 @@
-var aes = require('./aes');
-var Transform = require('./cipherBase');
-var inherits = require('inherits');
-var modes = require('./modes');
-var ebtk = require('./EVP_BytesToKey');
-var StreamCipher = require('./streamCipher');
-var AuthCipher = require('./authCipher');
-inherits(Cipher, Transform);
-function Cipher(mode, key, iv) {
+var aes = require('./aes')
+var Transform = require('./cipherBase')
+var inherits = require('inherits')
+var modes = require('./modes')
+var ebtk = require('./EVP_BytesToKey')
+var StreamCipher = require('./streamCipher')
+var AuthCipher = require('./authCipher')
+inherits(Cipher, Transform)
+function Cipher (mode, key, iv) {
if (!(this instanceof Cipher)) {
- return new Cipher(mode, key, iv);
+ return new Cipher(mode, key, iv)
}
- Transform.call(this);
- this._cache = new Splitter();
- this._cipher = new aes.AES(key);
- this._prev = new Buffer(iv.length);
- iv.copy(this._prev);
- this._mode = mode;
- this._autopadding = true;
+ Transform.call(this)
+ this._cache = new Splitter()
+ this._cipher = new aes.AES(key)
+ this._prev = new Buffer(iv.length)
+ iv.copy(this._prev)
+ this._mode = mode
+ this._autopadding = true
}
Cipher.prototype._update = function (data) {
- this._cache.add(data);
- var chunk;
- var thing;
- var out = [];
+ this._cache.add(data)
+ var chunk
+ var thing
+ var out = []
while ((chunk = this._cache.get())) {
- thing = this._mode.encrypt(this, chunk);
- out.push(thing);
+ thing = this._mode.encrypt(this, chunk)
+ out.push(thing)
}
- return Buffer.concat(out);
-};
+ return Buffer.concat(out)
+}
Cipher.prototype._final = function () {
- var chunk = this._cache.flush();
+ var chunk = this._cache.flush()
if (this._autopadding) {
- chunk = this._mode.encrypt(this, chunk);
- this._cipher.scrub();
- return chunk;
+ chunk = this._mode.encrypt(this, chunk)
+ this._cipher.scrub()
+ return chunk
} else if (chunk.toString('hex') !== '10101010101010101010101010101010') {
- this._cipher.scrub();
- throw new Error('data not multiple of block length');
+ this._cipher.scrub()
+ throw new Error('data not multiple of block length')
}
-};
+}
Cipher.prototype.setAutoPadding = function (setTo) {
- this._autopadding = !!setTo;
-};
+ this._autopadding = !!setTo
+}
-function Splitter() {
- if (!(this instanceof Splitter)) {
- return new Splitter();
+function Splitter () {
+ if (!(this instanceof Splitter)) {
+ return new Splitter()
}
- this.cache = new Buffer('');
+ this.cache = new Buffer('')
}
Splitter.prototype.add = function (data) {
- this.cache = Buffer.concat([this.cache, data]);
-};
+ this.cache = Buffer.concat([this.cache, data])
+}
Splitter.prototype.get = function () {
if (this.cache.length > 15) {
- var out = this.cache.slice(0, 16);
- this.cache = this.cache.slice(16);
- return out;
+ var out = this.cache.slice(0, 16)
+ this.cache = this.cache.slice(16)
+ return out
}
- return null;
-};
+ return null
+}
Splitter.prototype.flush = function () {
- var len = 16 - this.cache.length;
- var padBuff = new Buffer(len);
+ var len = 16 - this.cache.length
+ var padBuff = new Buffer(len)
- var i = -1;
+ var i = -1
while (++i < len) {
- padBuff.writeUInt8(len, i);
+ padBuff.writeUInt8(len, i)
}
- var out = Buffer.concat([this.cache, padBuff]);
- return out;
-};
+ var out = Buffer.concat([this.cache, padBuff])
+ return out
+}
var modelist = {
ECB: require('./modes/ecb'),
CBC: require('./modes/cbc'),
@@ -82,40 +82,40 @@ var modelist = {
OFB: require('./modes/ofb'),
CTR: require('./modes/ctr'),
GCM: require('./modes/ctr')
-};
+}
-function createCipheriv(suite, password, iv) {
- var config = modes[suite.toLowerCase()];
+function createCipheriv (suite, password, iv) {
+ var config = modes[suite.toLowerCase()]
if (!config) {
- throw new TypeError('invalid suite type');
+ throw new TypeError('invalid suite type')
}
if (typeof iv === 'string') {
- iv = new Buffer(iv);
+ iv = new Buffer(iv)
}
if (typeof password === 'string') {
- password = new Buffer(password);
+ password = new Buffer(password)
}
- if (password.length !== config.key/8) {
- throw new TypeError('invalid key length ' + password.length);
+ if (password.length !== config.key / 8) {
+ throw new TypeError('invalid key length ' + password.length)
}
if (iv.length !== config.iv) {
- throw new TypeError('invalid iv length ' + iv.length);
+ throw new TypeError('invalid iv length ' + iv.length)
}
if (config.type === 'stream') {
- return new StreamCipher(modelist[config.mode], password, iv);
+ return new StreamCipher(modelist[config.mode], password, iv)
} else if (config.type === 'auth') {
- return new AuthCipher(modelist[config.mode], password, iv);
+ return new AuthCipher(modelist[config.mode], password, iv)
}
- return new Cipher(modelist[config.mode], password, iv);
+ return new Cipher(modelist[config.mode], password, iv)
}
function createCipher (suite, password) {
- var config = modes[suite.toLowerCase()];
+ var config = modes[suite.toLowerCase()]
if (!config) {
- throw new TypeError('invalid suite type');
+ throw new TypeError('invalid suite type')
}
- var keys = ebtk(password, config.key, config.iv);
- return createCipheriv(suite, keys.key, keys.iv);
+ var keys = ebtk(password, config.key, config.iv)
+ return createCipheriv(suite, keys.key, keys.iv)
}
-exports.createCipheriv = createCipheriv;
-exports.createCipher = createCipher;
\ No newline at end of file
+exports.createCipheriv = createCipheriv
+exports.createCipher = createCipher
diff --git a/ghash.js b/ghash.js
index 5499d33..0ca143c 100644
--- a/ghash.js
+++ b/ghash.js
@@ -1,98 +1,98 @@
-var zeros = new Buffer(16);
-zeros.fill(0);
-module.exports = GHASH;
-function GHASH(key){
- this.h = key;
- this.state = new Buffer(16);
- this.state.fill(0);
- this.cache = new Buffer('');
+var zeros = new Buffer(16)
+zeros.fill(0)
+module.exports = GHASH
+function GHASH (key) {
+ this.h = key
+ this.state = new Buffer(16)
+ this.state.fill(0)
+ this.cache = new Buffer('')
}
// from http://bitwiseshiftleft.github.io/sjcl/doc/symbols/src/core_gcm.js.html
// by Juho Vähä-Herttua
GHASH.prototype.ghash = function (block) {
- var i = -1;
+ var i = -1
while (++i < block.length) {
- this.state[i] ^= block[i];
+ this.state[i] ^= block[i]
}
- this._multiply();
-};
+ this._multiply()
+}
GHASH.prototype._multiply = function () {
- var Vi = toArray(this.h);
- var Zi = [0, 0, 0, 0];
- var j, xi, lsb_Vi;
- var i = -1;
+ var Vi = toArray(this.h)
+ var Zi = [0, 0, 0, 0]
+ var j, xi, lsb_Vi
+ var i = -1
while (++i < 128) {
- xi = (this.state[~~(i/8)] & (1 << (7-i%8))) !== 0;
+ xi = (this.state[~~(i / 8)] & (1 << (7 - i % 8))) !== 0
if (xi) {
// Z_i+1 = Z_i ^ V_i
- Zi = xor(Zi, Vi);
+ Zi = xor(Zi, Vi)
}
// Store the value of LSB(V_i)
- lsb_Vi = (Vi[3] & 1) !== 0;
+ lsb_Vi = (Vi[3] & 1) !== 0
// V_i+1 = V_i >> 1
- for (j=3; j>0; j--) {
- Vi[j] = (Vi[j] >>> 1) | ((Vi[j-1]&1) << 31);
+ for (j = 3; j > 0; j--) {
+ Vi[j] = (Vi[j] >>> 1) | ((Vi[j - 1] & 1) << 31)
}
- Vi[0] = Vi[0] >>> 1;
+ Vi[0] = Vi[0] >>> 1
// If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R
if (lsb_Vi) {
- Vi[0] = Vi[0] ^ (0xe1 << 24);
+ Vi[0] = Vi[0] ^ (0xe1 << 24)
}
}
- this.state = fromArray(Zi);
-};
+ this.state = fromArray(Zi)
+}
GHASH.prototype.update = function (buf) {
- this.cache = Buffer.concat([this.cache, buf]);
- var chunk;
+ this.cache = Buffer.concat([this.cache, buf])
+ var chunk
while (this.cache.length >= 16) {
- chunk = this.cache.slice(0, 16);
- this.cache = this.cache.slice(16);
- this.ghash(chunk);
+ chunk = this.cache.slice(0, 16)
+ this.cache = this.cache.slice(16)
+ this.ghash(chunk)
}
-};
+}
GHASH.prototype.final = function (abl, bl) {
if (this.cache.length) {
- this.ghash(Buffer.concat([this.cache, zeros], 16));
+ this.ghash(Buffer.concat([this.cache, zeros], 16))
}
this.ghash(fromArray([
- 0, abl,
- 0, bl
- ]));
- return this.state;
-};
+ 0, abl,
+ 0, bl
+ ]))
+ return this.state
+}
-function toArray(buf) {
+function toArray (buf) {
return [
buf.readUInt32BE(0),
buf.readUInt32BE(4),
buf.readUInt32BE(8),
buf.readUInt32BE(12)
- ];
+ ]
}
-function fromArray(out) {
- out = out.map(fixup_uint32);
- var buf = new Buffer(16);
- buf.writeUInt32BE(out[0], 0);
- buf.writeUInt32BE(out[1], 4);
- buf.writeUInt32BE(out[2], 8);
- buf.writeUInt32BE(out[3], 12);
- return buf;
+function fromArray (out) {
+ out = out.map(fixup_uint32)
+ var buf = new Buffer(16)
+ buf.writeUInt32BE(out[0], 0)
+ buf.writeUInt32BE(out[1], 4)
+ buf.writeUInt32BE(out[2], 8)
+ buf.writeUInt32BE(out[3], 12)
+ return buf
}
-var uint_max = Math.pow(2, 32);
-function fixup_uint32(x) {
- var ret, x_pos;
- ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x;
- return ret;
+var uint_max = Math.pow(2, 32)
+function fixup_uint32 (x) {
+ var ret, x_pos
+ ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x
+ return ret
}
-function xor(a, b) {
+function xor (a, b) {
return [
a[0] ^ b[0],
a[1] ^ b[1],
a[2] ^ b[2],
- a[3] ^ b[3],
- ];
-}
\ No newline at end of file
+ a[3] ^ b[3]
+ ]
+}
diff --git a/index.js b/index.js
index 320ce82..58fa883 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,7 @@
-var crypto = require('crypto');
+var crypto = require('crypto')
-exports.createCipher = exports.Cipher = crypto.createCipher;
-exports.createCipheriv = exports.Cipheriv = crypto.createCipheriv;
-exports.createDecipher = exports.Decipher = crypto.createDecipher;
-exports.createDecipheriv = exports.Decipheriv = crypto.createDecipheriv;
-exports.listCiphers = exports.getCiphers = crypto.getCiphers;
+exports.createCipher = exports.Cipher = crypto.createCipher
+exports.createCipheriv = exports.Cipheriv = crypto.createCipheriv
+exports.createDecipher = exports.Decipher = crypto.createDecipher
+exports.createDecipheriv = exports.Decipheriv = crypto.createDecipheriv
+exports.listCiphers = exports.getCiphers = crypto.getCiphers
diff --git a/modes.js b/modes.js
index a19c54e..c070086 100644
--- a/modes.js
+++ b/modes.js
@@ -4,168 +4,168 @@ exports['aes-128-ecb'] = {
iv: 0,
mode: 'ECB',
type: 'block'
-};
+}
exports['aes-192-ecb'] = {
cipher: 'AES',
key: 192,
iv: 0,
mode: 'ECB',
type: 'block'
-};
+}
exports['aes-256-ecb'] = {
cipher: 'AES',
key: 256,
iv: 0,
mode: 'ECB',
type: 'block'
-};
+}
exports['aes-128-cbc'] = {
cipher: 'AES',
key: 128,
iv: 16,
mode: 'CBC',
type: 'block'
-};
+}
exports['aes-192-cbc'] = {
cipher: 'AES',
key: 192,
iv: 16,
mode: 'CBC',
type: 'block'
-};
+}
exports['aes-256-cbc'] = {
cipher: 'AES',
key: 256,
iv: 16,
mode: 'CBC',
type: 'block'
-};
-exports['aes128'] = exports['aes-128-cbc'];
-exports['aes192'] = exports['aes-192-cbc'];
-exports['aes256'] = exports['aes-256-cbc'];
+}
+exports['aes128'] = exports['aes-128-cbc']
+exports['aes192'] = exports['aes-192-cbc']
+exports['aes256'] = exports['aes-256-cbc']
exports['aes-128-cfb'] = {
cipher: 'AES',
key: 128,
iv: 16,
mode: 'CFB',
type: 'stream'
-};
+}
exports['aes-192-cfb'] = {
cipher: 'AES',
key: 192,
iv: 16,
mode: 'CFB',
type: 'stream'
-};
+}
exports['aes-256-cfb'] = {
cipher: 'AES',
key: 256,
iv: 16,
mode: 'CFB',
type: 'stream'
-};
+}
exports['aes-128-cfb8'] = {
cipher: 'AES',
key: 128,
iv: 16,
mode: 'CFB8',
type: 'stream'
-};
+}
exports['aes-192-cfb8'] = {
cipher: 'AES',
key: 192,
iv: 16,
mode: 'CFB8',
type: 'stream'
-};
+}
exports['aes-256-cfb8'] = {
cipher: 'AES',
key: 256,
iv: 16,
mode: 'CFB8',
type: 'stream'
-};
+}
exports['aes-128-cfb1'] = {
cipher: 'AES',
key: 128,
iv: 16,
mode: 'CFB1',
type: 'stream'
-};
+}
exports['aes-192-cfb1'] = {
cipher: 'AES',
key: 192,
iv: 16,
mode: 'CFB1',
type: 'stream'
-};
+}
exports['aes-256-cfb1'] = {
cipher: 'AES',
key: 256,
iv: 16,
mode: 'CFB1',
type: 'stream'
-};
+}
exports['aes-128-ofb'] = {
cipher: 'AES',
key: 128,
iv: 16,
mode: 'OFB',
type: 'stream'
-};
+}
exports['aes-192-ofb'] = {
cipher: 'AES',
key: 192,
iv: 16,
mode: 'OFB',
type: 'stream'
-};
+}
exports['aes-256-ofb'] = {
cipher: 'AES',
key: 256,
iv: 16,
mode: 'OFB',
type: 'stream'
-};
+}
exports['aes-128-ctr'] = {
cipher: 'AES',
key: 128,
iv: 16,
mode: 'CTR',
type: 'stream'
-};
+}
exports['aes-192-ctr'] = {
cipher: 'AES',
key: 192,
iv: 16,
mode: 'CTR',
type: 'stream'
-};
+}
exports['aes-256-ctr'] = {
cipher: 'AES',
key: 256,
iv: 16,
mode: 'CTR',
type: 'stream'
-};
+}
exports['aes-128-gcm'] = {
cipher: 'AES',
key: 128,
iv: 12,
mode: 'GCM',
type: 'auth'
-};
+}
exports['aes-192-gcm'] = {
cipher: 'AES',
key: 192,
iv: 12,
mode: 'GCM',
type: 'auth'
-};
+}
exports['aes-256-gcm'] = {
cipher: 'AES',
key: 256,
iv: 12,
mode: 'GCM',
type: 'auth'
-};
\ No newline at end of file
+}
diff --git a/modes/cbc.js b/modes/cbc.js
index bdc3cbc..74b6323 100644
--- a/modes/cbc.js
+++ b/modes/cbc.js
@@ -1,12 +1,12 @@
-var xor = require('../xor');
+var xor = require('../xor')
exports.encrypt = function (self, block) {
- var data = xor(block, self._prev);
- self._prev = self._cipher.encryptBlock(data);
- return self._prev;
-};
+ 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
+ var pad = self._prev
+ self._prev = block
+ var out = self._cipher.decryptBlock(block)
+ return xor(out, pad)
+}
diff --git a/modes/cfb.js b/modes/cfb.js
index 13712b4..62c5fa5 100644
--- a/modes/cfb.js
+++ b/modes/cfb.js
@@ -1,27 +1,27 @@
-var xor = require('../xor');
+var xor = require('../xor')
exports.encrypt = function (self, data, decrypt) {
- var out = new Buffer('');
- var len;
+ var out = new Buffer('')
+ var len
while (data.length) {
if (self._cache.length === 0) {
- self._cache = self._cipher.encryptBlock(self._prev);
- self._prev = new Buffer('');
+ self._cache = self._cipher.encryptBlock(self._prev)
+ self._prev = new Buffer('')
}
if (self._cache.length <= data.length) {
- len = self._cache.length;
- out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)]);
- data = data.slice(len);
+ len = self._cache.length
+ out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)])
+ data = data.slice(len)
} else {
- out = Buffer.concat([out, encryptStart(self, data, decrypt)]);
- break;
+ out = Buffer.concat([out, encryptStart(self, data, decrypt)])
+ break
}
}
- return out;
-};
-function encryptStart(self, data, decrypt) {
- var len = data.length;
- var out = xor(data, self._cache);
- self._cache = self._cache.slice(len);
- self._prev = Buffer.concat([self._prev, decrypt?data:out]);
- return out;
-}
\ No newline at end of file
+ return out
+}
+function encryptStart (self, data, decrypt) {
+ var len = data.length
+ var out = xor(data, self._cache)
+ self._cache = self._cache.slice(len)
+ self._prev = Buffer.concat([self._prev, decrypt ? data : out])
+ return out
+}
diff --git a/modes/cfb1.js b/modes/cfb1.js
index ff39ce9..335542e 100644
--- a/modes/cfb1.js
+++ b/modes/cfb1.js
@@ -1,35 +1,34 @@
-
-function encryptByte(self, byteParam, decrypt) {
- var pad;
- var i = -1;
- var len = 8;
- var out = 0;
- var bit, value;
+function encryptByte (self, byteParam, decrypt) {
+ var pad
+ var i = -1
+ var len = 8
+ var out = 0
+ var bit, value
while (++i < len) {
- pad = self._cipher.encryptBlock(self._prev);
- bit = (byteParam & (1 << (7-i))) ? 0x80:0;
- value = pad[0] ^ bit;
- out += ((value&0x80) >> (i%8));
- self._prev = shiftIn(self._prev, decrypt?bit:value);
+ pad = self._cipher.encryptBlock(self._prev)
+ bit = (byteParam & (1 << (7 - i))) ? 0x80 : 0
+ value = pad[0] ^ bit
+ out += ((value & 0x80) >> (i % 8))
+ self._prev = shiftIn(self._prev, decrypt ? bit : value)
}
- return out;
+ return out
}
exports.encrypt = function (self, chunk, decrypt) {
- var len = chunk.length;
- var out = new Buffer(len);
- var i = -1;
+ var len = chunk.length
+ var out = new Buffer(len)
+ var i = -1
while (++i < len) {
- out[i] = encryptByte(self, chunk[i], decrypt);
+ out[i] = encryptByte(self, chunk[i], decrypt)
}
- return out;
-};
-function shiftIn(buffer, value) {
- var len = buffer.length;
- var i = -1;
- var out = new Buffer(buffer.length);
- buffer = Buffer.concat([buffer, new Buffer([value])]);
- while(++i < len) {
- out[i] = buffer[i]<<1 | buffer[i+1]>>(7);
+ return out
+}
+function shiftIn (buffer, value) {
+ var len = buffer.length
+ var i = -1
+ var out = new Buffer(buffer.length)
+ buffer = Buffer.concat([buffer, new Buffer([value])])
+ while (++i < len) {
+ out[i] = buffer[i] << 1 | buffer[i + 1] >> (7)
}
- return out;
+ return out
}
diff --git a/modes/cfb8.js b/modes/cfb8.js
index 046b30c..c967a95 100644
--- a/modes/cfb8.js
+++ b/modes/cfb8.js
@@ -1,15 +1,15 @@
-function encryptByte(self, byteParam, decrypt) {
- var pad = self._cipher.encryptBlock(self._prev);
- var out = pad[0] ^ byteParam;
- self._prev = Buffer.concat([self._prev.slice(1), new Buffer([decrypt?byteParam:out])]);
- return out;
+function encryptByte (self, byteParam, decrypt) {
+ var pad = self._cipher.encryptBlock(self._prev)
+ var out = pad[0] ^ byteParam
+ self._prev = Buffer.concat([self._prev.slice(1), new Buffer([decrypt ? byteParam : out])])
+ return out
}
exports.encrypt = function (self, chunk, decrypt) {
- var len = chunk.length;
- var out = new Buffer(len);
- var i = -1;
+ var len = chunk.length
+ var out = new Buffer(len)
+ var i = -1
while (++i < len) {
- out[i] = encryptByte(self, chunk[i], decrypt);
+ out[i] = encryptByte(self, chunk[i], decrypt)
}
- return out;
-};
+ return out
+}
diff --git a/modes/ctr.js b/modes/ctr.js
index b345b0e..13e1ce3 100644
--- a/modes/ctr.js
+++ b/modes/ctr.js
@@ -1,28 +1,28 @@
-var xor = require('../xor');
-function getBlock(self) {
- var out = self._cipher.encryptBlock(self._prev);
- incr32(self._prev);
- return out;
+var xor = require('../xor')
+function getBlock (self) {
+ var out = self._cipher.encryptBlock(self._prev)
+ incr32(self._prev)
+ return out
}
exports.encrypt = function (self, chunk) {
while (self._cache.length < chunk.length) {
- self._cache = Buffer.concat([self._cache, getBlock(self)]);
+ self._cache = Buffer.concat([self._cache, getBlock(self)])
}
- var pad = self._cache.slice(0, chunk.length);
- self._cache = self._cache.slice(chunk.length);
- return xor(chunk, pad);
-};
-function incr32(iv) {
- var len = iv.length;
- var item;
+ var pad = self._cache.slice(0, chunk.length)
+ self._cache = self._cache.slice(chunk.length)
+ return xor(chunk, pad)
+}
+function incr32 (iv) {
+ var len = iv.length
+ var item
while (len--) {
- item = iv.readUInt8(len);
+ item = iv.readUInt8(len)
if (item === 255) {
- iv.writeUInt8(0, len);
+ iv.writeUInt8(0, len)
} else {
- item++;
- iv.writeUInt8(item, len);
- break;
+ item++
+ iv.writeUInt8(item, len)
+ break
}
}
-}
\ No newline at end of file
+}
diff --git a/modes/ecb.js b/modes/ecb.js
index 1cef515..4dd97e7 100644
--- a/modes/ecb.js
+++ b/modes/ecb.js
@@ -1,6 +1,6 @@
exports.encrypt = function (self, block) {
- return self._cipher.encryptBlock(block);
-};
+ return self._cipher.encryptBlock(block)
+}
exports.decrypt = function (self, block) {
- return self._cipher.decryptBlock(block);
-};
\ No newline at end of file
+ return self._cipher.decryptBlock(block)
+}
diff --git a/modes/ofb.js b/modes/ofb.js
index 43efc5a..32a5dd5 100644
--- a/modes/ofb.js
+++ b/modes/ofb.js
@@ -1,13 +1,13 @@
-var xor = require('../xor');
-function getBlock(self) {
- self._prev = self._cipher.encryptBlock(self._prev);
- return self._prev;
+var xor = require('../xor')
+function getBlock (self) {
+ self._prev = self._cipher.encryptBlock(self._prev)
+ return self._prev
}
exports.encrypt = function (self, chunk) {
while (self._cache.length < chunk.length) {
- self._cache = Buffer.concat([self._cache, getBlock(self)]);
+ self._cache = Buffer.concat([self._cache, getBlock(self)])
}
- var pad = self._cache.slice(0, chunk.length);
- self._cache = self._cache.slice(chunk.length);
- return xor(chunk, pad);
-};
\ No newline at end of file
+ var pad = self._cache.slice(0, chunk.length)
+ self._cache = self._cache.slice(chunk.length)
+ return xor(chunk, pad)
+}
diff --git a/populateFixtures.js b/populateFixtures.js
index c8a733b..ac31eb3 100644
--- a/populateFixtures.js
+++ b/populateFixtures.js
@@ -1,25 +1,25 @@
-var modes = require('./modes');
-var fixtures = require('./test/fixtures.json');
-var crypto = require('crypto');
-var types = ['aes-128-cfb1','aes-192-cfb1','aes-256-cfb1'];
-var ebtk = require('./EVP_BytesToKey');
-var fs = require('fs');
+var modes = require('./modes')
+var fixtures = require('./test/fixtures.json')
+var crypto = require('crypto')
+var types = ['aes-128-cfb1', 'aes-192-cfb1', 'aes-256-cfb1']
+var ebtk = require('./EVP_BytesToKey')
+var fs = require('fs')
fixtures.forEach(function (fixture) {
types.forEach(function (cipher) {
- var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
- var buf = new Buffer('');
- buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]);
- buf = Buffer.concat([buf, suite.final()]);
- fixture.results.ciphers[cipher] = buf.toString('hex');
+ var suite = crypto.createCipher(cipher, new Buffer(fixture.password))
+ var buf = new Buffer('')
+ buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))])
+ buf = Buffer.concat([buf, suite.final()])
+ fixture.results.ciphers[cipher] = buf.toString('hex')
if (modes[cipher].mode === 'ECB') {
- return;
+ return
}
- var suite2 = crypto.createCipheriv(cipher, ebtk(crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
- var buf2 = new Buffer('');
- buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]);
- buf2 = Buffer.concat([buf2, suite2.final()]);
- fixture.results.cipherivs[cipher] = buf2.toString('hex');
- });
-});
-fs.writeFileSync('./test/fixturesNew.json', JSON.stringify(fixtures, false, 4));
\ No newline at end of file
+ var suite2 = crypto.createCipheriv(cipher, ebtk(crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'))
+ var buf2 = new Buffer('')
+ buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))])
+ buf2 = Buffer.concat([buf2, suite2.final()])
+ fixture.results.cipherivs[cipher] = buf2.toString('hex')
+ })
+})
+fs.writeFileSync('./test/fixturesNew.json', JSON.stringify(fixtures, false, 4))
diff --git a/streamCipher.js b/streamCipher.js
index 9d1929d..2e0fb88 100644
--- a/streamCipher.js
+++ b/streamCipher.js
@@ -1,25 +1,25 @@
-var aes = require('./aes');
-var Transform = require('./cipherBase');
-var inherits = require('inherits');
+var aes = require('./aes')
+var Transform = require('./cipherBase')
+var inherits = require('inherits')
-inherits(StreamCipher, Transform);
-module.exports = StreamCipher;
-function StreamCipher(mode, key, iv, decrypt) {
+inherits(StreamCipher, Transform)
+module.exports = StreamCipher
+function StreamCipher (mode, key, iv, decrypt) {
if (!(this instanceof StreamCipher)) {
- return new StreamCipher(mode, key, iv);
+ return new StreamCipher(mode, key, iv)
}
- Transform.call(this);
- this._cipher = new aes.AES(key);
- this._prev = new Buffer(iv.length);
- this._cache = new Buffer('');
- this._secCache = new Buffer('');
- this._decrypt = decrypt;
- iv.copy(this._prev);
- this._mode = mode;
+ Transform.call(this)
+ this._cipher = new aes.AES(key)
+ this._prev = new Buffer(iv.length)
+ this._cache = new Buffer('')
+ this._secCache = new Buffer('')
+ this._decrypt = decrypt
+ iv.copy(this._prev)
+ this._mode = mode
}
StreamCipher.prototype._update = function (chunk) {
- return this._mode.encrypt(this, chunk, this._decrypt);
-};
+ return this._mode.encrypt(this, chunk, this._decrypt)
+}
StreamCipher.prototype._final = function () {
- this._cipher.scrub();
-};
\ No newline at end of file
+ this._cipher.scrub()
+}
diff --git a/test/index.js b/test/index.js
index 3c9d8ee..893b33d 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1,512 +1,498 @@
-var test = require('tape');
-var fixtures = require('./fixtures.json');
-var _crypto = require('crypto');
-var crypto = require('../browser.js');
-var modes = require('../modes');
-var types = Object.keys(modes);
-var ebtk = require('../EVP_BytesToKey');
-function isGCM(cipher) {
- return modes[cipher].mode === 'GCM';
+var test = require('tape')
+var fixtures = require('./fixtures.json')
+var _crypto = require('crypto')
+var crypto = require('../browser.js')
+var modes = require('../modes')
+var types = Object.keys(modes)
+var ebtk = require('../EVP_BytesToKey')
+function isGCM (cipher) {
+ return modes[cipher].mode === 'GCM'
}
-function isNode10() {
- return process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) <= 10;
+function isNode10 () {
+ return process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) <= 10
}
fixtures.forEach(function (fixture, i) {
- //var ciphers = fixture.results.ciphers = {};
+ // var ciphers = fixture.results.ciphers = {}
types.forEach(function (cipher) {
if (isGCM(cipher)) {
- return;
+ return
}
test('fixture ' + i + ' ' + cipher, function (t) {
- t.plan(1);
- var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
- var buf = new Buffer('');
+ t.plan(1)
+ var suite = crypto.createCipher(cipher, new Buffer(fixture.password))
+ var buf = new Buffer('')
suite.on('data', function (d) {
- buf = Buffer.concat([buf, d]);
- });
+ buf = Buffer.concat([buf, d])
+ })
suite.on('error', function (e) {
- console.log(e);
- });
- suite.on("end", function () {
- // console.log(fixture.text);
- // decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a');
- // decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b');
- t.equals(buf.toString('hex'), fixture.results.ciphers[cipher]);
- });
- suite.write(new Buffer(fixture.text));
- suite.end();
- });
+ console.log(e)
+ })
+ suite.on('end', function () {
+ // console.log(fixture.text)
+ // decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a')
+ // decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b')
+ t.equals(buf.toString('hex'), fixture.results.ciphers[cipher])
+ })
+ suite.write(new Buffer(fixture.text))
+ suite.end()
+ })
test('fixture ' + i + ' ' + cipher + '-legacy', function (t) {
- t.plan(3);
- var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
- var buf = new Buffer('');
- var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password));
- var buf2 = new Buffer('');
- var inbuf = new Buffer(fixture.text);
- var mid = ~~(inbuf.length/2);
- buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate');
- buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2');
- buf = Buffer.concat([buf, suite.final()]);
- buf2 = Buffer.concat([buf2, suite2.final()]);
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'final');
- });
+ t.plan(3)
+ var suite = crypto.createCipher(cipher, new Buffer(fixture.password))
+ var buf = new Buffer('')
+ var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password))
+ var buf2 = new Buffer('')
+ var inbuf = new Buffer(fixture.text)
+ var mid = ~~(inbuf.length / 2)
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate')
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2')
+ buf = Buffer.concat([buf, suite.final()])
+ buf2 = Buffer.concat([buf2, suite2.final()])
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'final')
+ })
test('fixture ' + i + ' ' + cipher + '-decrypt', function (t) {
- t.plan(1);
- var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
- var buf = new Buffer('');
+ t.plan(1)
+ var suite = crypto.createDecipher(cipher, new Buffer(fixture.password))
+ var buf = new Buffer('')
suite.on('data', function (d) {
- buf = Buffer.concat([buf, d]);
- });
+ buf = Buffer.concat([buf, d])
+ })
suite.on('error', function (e) {
- console.log(e);
- });
- suite.on("end", function () {
- // console.log(fixture.text);
- // decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a');
- // decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b');
- t.equals(buf.toString('utf8'), fixture.text);
- });
- suite.write(new Buffer(fixture.results.ciphers[cipher], 'hex'));
- suite.end();
- });
+ console.log(e)
+ })
+ suite.on('end', function () {
+ // console.log(fixture.text)
+ // decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a')
+ // decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b')
+ t.equals(buf.toString('utf8'), fixture.text)
+ })
+ suite.write(new Buffer(fixture.results.ciphers[cipher], 'hex'))
+ suite.end()
+ })
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
- t.plan(4);
- var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
- var buf = new Buffer('');
- var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password));
- var buf2 = new Buffer('');
- var inbuf = new Buffer(fixture.results.ciphers[cipher], 'hex');
- var mid = ~~(inbuf.length/2);
- buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate');
- buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2');
- buf = Buffer.concat([buf, suite.final()]);
- buf2 = Buffer.concat([buf2, suite2.final()]);
- t.equals(buf.toString('utf8'), fixture.text);
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final');
- });
- //var cipherivs = fixture.results.cipherivs = {};
+ t.plan(4)
+ var suite = crypto.createDecipher(cipher, new Buffer(fixture.password))
+ var buf = new Buffer('')
+ var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password))
+ var buf2 = new Buffer('')
+ var inbuf = new Buffer(fixture.results.ciphers[cipher], 'hex')
+ var mid = ~~(inbuf.length / 2)
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate')
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2')
+ buf = Buffer.concat([buf, suite.final()])
+ buf2 = Buffer.concat([buf2, suite2.final()])
+ t.equals(buf.toString('utf8'), fixture.text)
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final')
+ })
+ // var cipherivs = fixture.results.cipherivs = {}
types.forEach(function (cipher) {
if (modes[cipher].mode === 'ECB') {
- return;
+ return
}
if (isGCM(cipher) && isNode10()) {
- return;
+ return
}
test('fixture ' + i + ' ' + cipher + '-iv', function (t) {
if (isGCM(cipher)) {
- t.plan(4);
+ t.plan(4)
} else {
- t.plan(2);
+ t.plan(2)
}
- var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var buf = new Buffer('');
- var buf2 = new Buffer('');
+ var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var buf = new Buffer('')
+ var buf2 = new Buffer('')
suite.on('data', function (d) {
- buf = Buffer.concat([buf, d]);
- });
+ buf = Buffer.concat([buf, d])
+ })
suite.on('error', function (e) {
- console.log(e);
- });
+ console.log(e)
+ })
suite2.on('data', function (d) {
- buf2 = Buffer.concat([buf2, d]);
- });
+ buf2 = Buffer.concat([buf2, d])
+ })
suite2.on('error', function (e) {
- console.log(e);
- });
- suite.on("end", function () {
- t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher], 'vs fixture');
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'vs node');
+ console.log(e)
+ })
+ suite.on('end', function () {
+ t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher], 'vs fixture')
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'vs node')
if (isGCM(cipher)) {
- t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture');
- t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node');
+ t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture')
+ t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node')
}
- });
+ })
if (isGCM(cipher)) {
- suite.setAAD(new Buffer(fixture.aad, 'hex'));
- suite2.setAAD(new Buffer(fixture.aad, 'hex'));
+ suite.setAAD(new Buffer(fixture.aad, 'hex'))
+ suite2.setAAD(new Buffer(fixture.aad, 'hex'))
}
- suite2.write(new Buffer(fixture.text));
- suite2.end();
- suite.write(new Buffer(fixture.text));
- suite.end();
- });
-
+ suite2.write(new Buffer(fixture.text))
+ suite2.end()
+ suite.write(new Buffer(fixture.text))
+ suite.end()
+ })
+
test('fixture ' + i + ' ' + cipher + '-legacy-iv', function (t) {
if (isGCM(cipher)) {
- t.plan(6);
+ t.plan(6)
} else {
- t.plan(4);
+ t.plan(4)
}
- var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var buf = new Buffer('');
- var buf2 = new Buffer('');
- var inbuf = new Buffer(fixture.text);
- var mid = ~~(inbuf.length/2);
+ var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var buf = new Buffer('')
+ var buf2 = new Buffer('')
+ var inbuf = new Buffer(fixture.text)
+ var mid = ~~(inbuf.length / 2)
if (isGCM(cipher)) {
- suite.setAAD(new Buffer(fixture.aad, 'hex'));
- suite2.setAAD(new Buffer(fixture.aad, 'hex'));
+ suite.setAAD(new Buffer(fixture.aad, 'hex'))
+ suite2.setAAD(new Buffer(fixture.aad, 'hex'))
}
- buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate');
- buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2');
- buf = Buffer.concat([buf, suite.final()]);
- buf2 = Buffer.concat([buf2, suite2.final()]);
- t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher]);
- t.equals(buf.toString('hex'), buf2.toString('hex'), 'final');
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate')
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2')
+ buf = Buffer.concat([buf, suite.final()])
+ buf2 = Buffer.concat([buf2, suite2.final()])
+ t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher])
+ t.equals(buf.toString('hex'), buf2.toString('hex'), 'final')
if (isGCM(cipher)) {
- t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture');
- t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node');
+ t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture')
+ t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node')
}
- });
+ })
test('fixture ' + i + ' ' + cipher + '-iv-decrypt', function (t) {
- t.plan(2);
- var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var buf = new Buffer('');
- var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var buf2 = new Buffer('');
+ t.plan(2)
+ var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var buf = new Buffer('')
+ var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var buf2 = new Buffer('')
suite.on('data', function (d) {
- buf = Buffer.concat([buf, d]);
- });
+ buf = Buffer.concat([buf, d])
+ })
suite.on('error', function (e) {
- t.notOk(e);
- });
+ t.notOk(e)
+ })
suite2.on('data', function (d) {
- buf2 = Buffer.concat([buf2, d]);
- });
+ buf2 = Buffer.concat([buf2, d])
+ })
suite2.on('error', function (e) {
- t.notOk(e);
- });
- suite.on("end", function () {
- t.equals(buf.toString('utf8'), fixture.text, 'correct text vs fixture');
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'correct text vs node');
- });
+ t.notOk(e)
+ })
+ suite.on('end', function () {
+ t.equals(buf.toString('utf8'), fixture.text, 'correct text vs fixture')
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'correct text vs node')
+ })
if (isGCM(cipher)) {
- suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
- suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
- suite.setAAD(new Buffer(fixture.aad, 'hex'));
- suite2.setAAD(new Buffer(fixture.aad, 'hex'));
+ suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
+ suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
+ suite.setAAD(new Buffer(fixture.aad, 'hex'))
+ suite2.setAAD(new Buffer(fixture.aad, 'hex'))
}
-
- suite2.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
- suite.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
- suite2.end();
- suite.end();
- });
+
+ suite2.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'))
+ suite.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'))
+ suite2.end()
+ suite.end()
+ })
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
- t.plan(4);
- var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var buf = new Buffer('');
- var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
- var buf2 = new Buffer('');
- var inbuf = new Buffer(fixture.results.cipherivs[cipher], 'hex');
- var mid = ~~(inbuf.length/2);
+ t.plan(4)
+ var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var buf = new Buffer('')
+ var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
+ var buf2 = new Buffer('')
+ var inbuf = new Buffer(fixture.results.cipherivs[cipher], 'hex')
+ var mid = ~~(inbuf.length / 2)
if (isGCM(cipher)) {
- suite.setAAD(new Buffer(fixture.aad, 'hex'));
- suite2.setAAD(new Buffer(fixture.aad, 'hex'));
- suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
- suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
+ suite.setAAD(new Buffer(fixture.aad, 'hex'))
+ suite2.setAAD(new Buffer(fixture.aad, 'hex'))
+ suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
+ suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
}
- buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate');
- buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
- buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2');
- buf = Buffer.concat([buf, suite.final()]);
- buf2 = Buffer.concat([buf2, suite2.final()]);
- t.equals(buf.toString('utf8'), fixture.text);
- t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final');
- });
- });
- });
-});
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate')
+ buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
+ buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2')
+ buf = Buffer.concat([buf, suite.final()])
+ buf2 = Buffer.concat([buf2, suite2.final()])
+ t.equals(buf.toString('utf8'), fixture.text)
+ t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final')
+ })
+ })
+ })
+})
if (!isNode10()) {
test('node tests', function (t) {
- var TEST_CASES = [
- { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
- iv: '583673497131313748307652', plain: 'Hello World!',
- ct: '4BE13896F64DFA2C2D0F2C76',
- tag: '272B422F62EB545EAA15B5FF84092447', tampered: false },
- { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
- iv: '583673497131313748307652', plain: 'Hello World!',
- ct: '4BE13896F64DFA2C2D0F2C76', aad: '000000FF',
- tag: 'BA2479F66275665A88CB7B15F43EB005', tampered: false },
- { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
- iv: '583673497131313748307652', plain: 'Hello World!',
- ct: '4BE13596F64DFA2C2D0FAC76',
- tag: '272B422F62EB545EAA15B5FF84092447', tampered: true },
- { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
- iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
- ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
- tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false },
- { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
- iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
- ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
- tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true },
- { algo: 'aes-192-gcm', key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
- iv: '0e1791e9db3bd21a9122c416', plain: 'Hello node.js world!',
- password: 'very bad password', aad: '63616c76696e',
- ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
- tag: 'D2A35E5C611E5E3D2258360241C5B045', tampered: false }
-];
-
-var ciphers = Object.keys(modes);
-function testIt(i) {
- t.test('test case ' + i, function (t) {
- var test = TEST_CASES[i];
-
- if (ciphers.indexOf(test.algo) == -1) {
- console.log('skipping unsupported ' + test.algo + ' test');
- return;
- }
-
- (function() {
- var encrypt = crypto.createCipheriv(test.algo,
- new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
- if (test.aad)
- encrypt.setAAD(new Buffer(test.aad, 'hex'));
- var hex = encrypt.update(test.plain, 'ascii', 'hex');
- hex += encrypt.final('hex');
- var auth_tag = encrypt.getAuthTag();
- // only test basic encryption run if output is marked as tampered.
- if (!test.tampered) {
- t.equal(hex.toUpperCase(), test.ct);
- t.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
- }
- })();
+ var TEST_CASES = [
+ { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
+ iv: '583673497131313748307652', plain: 'Hello World!',
+ ct: '4BE13896F64DFA2C2D0F2C76',
+ tag: '272B422F62EB545EAA15B5FF84092447', tampered: false },
+ { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
+ iv: '583673497131313748307652', plain: 'Hello World!',
+ ct: '4BE13896F64DFA2C2D0F2C76', aad: '000000FF',
+ tag: 'BA2479F66275665A88CB7B15F43EB005', tampered: false },
+ { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
+ iv: '583673497131313748307652', plain: 'Hello World!',
+ ct: '4BE13596F64DFA2C2D0FAC76',
+ tag: '272B422F62EB545EAA15B5FF84092447', tampered: true },
+ { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
+ iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
+ ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
+ tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false },
+ { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
+ iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
+ ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
+ tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true },
+ { algo: 'aes-192-gcm', key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
+ iv: '0e1791e9db3bd21a9122c416', plain: 'Hello node.js world!',
+ password: 'very bad password', aad: '63616c76696e',
+ ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
+ tag: 'D2A35E5C611E5E3D2258360241C5B045', tampered: false }
+ ]
- (function() {
- var decrypt = crypto.createDecipheriv(test.algo,
- new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
- decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
- if (test.aad)
- decrypt.setAAD(new Buffer(test.aad, 'hex'));
- var msg = decrypt.update(test.ct, 'hex', 'ascii');
- if (!test.tampered) {
- msg += decrypt.final('ascii');
- t.equal(msg, test.plain);
- } else {
- // assert that final throws if input data could not be verified!
- t.throws(function() { decrypt.final('ascii'); }, / auth/);
- }
- })();
-
- (function() {
- if (!test.password) return;
- var encrypt = crypto.createCipher(test.algo, test.password);
- if (test.aad)
- encrypt.setAAD(new Buffer(test.aad, 'hex'));
- var hex = encrypt.update(test.plain, 'ascii', 'hex');
- hex += encrypt.final('hex');
- var auth_tag = encrypt.getAuthTag();
- // only test basic encryption run if output is marked as tampered.
- if (!test.tampered) {
- t.equal(hex.toUpperCase(), test.ct);
- t.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
- }
- })();
-
- (function() {
- if (!test.password) return;
- var decrypt = crypto.createDecipher(test.algo, test.password);
- decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
- if (test.aad)
- decrypt.setAAD(new Buffer(test.aad, 'hex'));
- var msg = decrypt.update(test.ct, 'hex', 'ascii');
- if (!test.tampered) {
- msg += decrypt.final('ascii');
- t.equal(msg, test.plain);
- } else {
- // assert that final throws if input data could not be verified!
- t.throws(function() { decrypt.final('ascii'); }, / auth/);
- }
- })();
+ var ciphers = Object.keys(modes)
+ function testIt (i) {
+ t.test('test case ' + i, function (t) {
+ var test = TEST_CASES[i]
- // after normal operation, test some incorrect ways of calling the API:
- // it's most certainly enough to run these tests with one algorithm only.
+ if (ciphers.indexOf(test.algo) === -1) {
+ console.log('skipping unsupported ' + test.algo + ' test')
+ return
+ }
- if (i > 0) {
- t.end();
- return;
- }
+ (function () {
+ var encrypt = crypto.createCipheriv(test.algo,
+ new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
+ if (test.aad) encrypt.setAAD(new Buffer(test.aad, 'hex'))
- (function() {
- // non-authenticating mode:
- var encrypt = crypto.createCipheriv('aes-128-cbc',
- 'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC');
- encrypt.update('blah', 'ascii');
- encrypt.final();
- t.throws(function() { encrypt.getAuthTag(); });
- t.throws(function() {
- encrypt.setAAD(new Buffer('123', 'ascii')); });
- })();
+ var hex = encrypt.update(test.plain, 'ascii', 'hex')
+ hex += encrypt.final('hex')
+ var auth_tag = encrypt.getAuthTag()
+ // only test basic encryption run if output is marked as tampered.
+ if (!test.tampered) {
+ t.equal(hex.toUpperCase(), test.ct)
+ t.equal(auth_tag.toString('hex').toUpperCase(), test.tag)
+ }
+ })()(function () {
+ var decrypt = crypto.createDecipheriv(test.algo,
+ new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
+ decrypt.setAuthTag(new Buffer(test.tag, 'hex'))
+ if (test.aad) decrypt.setAAD(new Buffer(test.aad, 'hex'))
+ var msg = decrypt.update(test.ct, 'hex', 'ascii')
+ if (!test.tampered) {
+ msg += decrypt.final('ascii')
+ t.equal(msg, test.plain)
+ } else {
+ // assert that final throws if input data could not be verified!
+ t.throws(function () { decrypt.final('ascii') }, / auth/)
+ }
+ })()(function () {
+ if (!test.password) return
+ var encrypt = crypto.createCipher(test.algo, test.password)
+ if (test.aad) encrypt.setAAD(new Buffer(test.aad, 'hex'))
+ var hex = encrypt.update(test.plain, 'ascii', 'hex')
+ hex += encrypt.final('hex')
+ var auth_tag = encrypt.getAuthTag()
+ // only test basic encryption run if output is marked as tampered.
+ if (!test.tampered) {
+ t.equal(hex.toUpperCase(), test.ct)
+ t.equal(auth_tag.toString('hex').toUpperCase(), test.tag)
+ }
+ })()(function () {
+ if (!test.password) return
+ var decrypt = crypto.createDecipher(test.algo, test.password)
+ decrypt.setAuthTag(new Buffer(test.tag, 'hex'))
+ if (test.aad) decrypt.setAAD(new Buffer(test.aad, 'hex'))
+ var msg = decrypt.update(test.ct, 'hex', 'ascii')
+ if (!test.tampered) {
+ msg += decrypt.final('ascii')
+ t.equal(msg, test.plain)
+ } else {
+ // assert that final throws if input data could not be verified!
+ t.throws(function () { decrypt.final('ascii') }, / auth/)
+ }
+ })()
- (function() {
- // trying to get tag before inputting all data:
- var encrypt = crypto.createCipheriv(test.algo,
- new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
- encrypt.update('blah', 'ascii');
- t.throws(function() { encrypt.getAuthTag(); }, / state/);
- })();
+ // after normal operation, test some incorrect ways of calling the API:
+ // it's most certainly enough to run these tests with one algorithm only.
- (function() {
- // trying to set tag on encryption object:
- var encrypt = crypto.createCipheriv(test.algo,
- new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
- t.throws(function() {
- encrypt.setAuthTag(new Buffer(test.tag, 'hex')); }, / state/);
- })();
+ if (i > 0) {
+ t.end()
+ return
+ }
- (function() {
- // trying to read tag from decryption object:
- var decrypt = crypto.createDecipheriv(test.algo,
- new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
- t.throws(function() { decrypt.getAuthTag(); }, / state/);
- })();
- t.end();
- });
-}
-for (var i in TEST_CASES) {
- testIt(i);
-
-}
-});
+ (function () {
+ // non-authenticating mode:
+ var encrypt = crypto.createCipheriv('aes-128-cbc',
+ 'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC')
+ encrypt.update('blah', 'ascii')
+ encrypt.final()
+ t.throws(function () { encrypt.getAuthTag() })
+ t.throws(function () {
+ encrypt.setAAD(new Buffer('123', 'ascii'))
+ })
+ })()(function () {
+ // trying to get tag before inputting all data:
+ var encrypt = crypto.createCipheriv(test.algo,
+ new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
+ encrypt.update('blah', 'ascii')
+ t.throws(function () { encrypt.getAuthTag() }, / state/)
+ })()(function () {
+ // trying to set tag on encryption object:
+ var encrypt = crypto.createCipheriv(test.algo,
+ new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
+ t.throws(function () {
+ encrypt.setAuthTag(new Buffer(test.tag, 'hex'))
+ }, / state/)
+ })()(function () {
+ // trying to read tag from decryption object:
+ var decrypt = crypto.createDecipheriv(test.algo,
+ new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
+ t.throws(function () { decrypt.getAuthTag() }, / state/)
+ })()
+ t.end()
+ })
+ }
+ for (var i in TEST_CASES) {
+ testIt(i)
+ }
+ })
}
-function corectPaddingWords(padding, result) {
+function corectPaddingWords (padding, result) {
test('correct padding ' + padding.toString('hex'), function (t) {
- t.plan(1);
- var block1 = new Buffer(16);
- block1.fill(4);
- result = block1.toString('hex') + result.toString('hex');
- var cipher = _crypto.createCipher('aes128', new Buffer('password'));
- cipher.setAutoPadding(false);
- var decipher = crypto.createDecipher('aes128', new Buffer('password'));
- var out = new Buffer('');
- out = Buffer.concat([out, cipher.update(block1)]);
- out = Buffer.concat([out, cipher.update(padding)]);
- var deciphered = decipher.update(out);
- deciphered = Buffer.concat([deciphered, decipher.final()]);
- t.equals(deciphered.toString('hex'), result);
- });
+ t.plan(1)
+ var block1 = new Buffer(16)
+ block1.fill(4)
+ result = block1.toString('hex') + result.toString('hex')
+ var cipher = _crypto.createCipher('aes128', new Buffer('password'))
+ cipher.setAutoPadding(false)
+ var decipher = crypto.createDecipher('aes128', new Buffer('password'))
+ var out = new Buffer('')
+ out = Buffer.concat([out, cipher.update(block1)])
+ out = Buffer.concat([out, cipher.update(padding)])
+ var deciphered = decipher.update(out)
+ deciphered = Buffer.concat([deciphered, decipher.final()])
+ t.equals(deciphered.toString('hex'), result)
+ })
}
-var sixteens = new Buffer(16);
-sixteens.fill(16);
-corectPaddingWords(sixteens, new Buffer(''));
-var fifteens = new Buffer(16);
-fifteens.fill(15);
-fifteens[0] = 5;
-corectPaddingWords(fifteens, new Buffer([5]));
-var one = _crypto.randomBytes(16);
-one[15] = 1;
-corectPaddingWords(one, one.slice(0, -1));
-function incorectPaddingthrows(padding) {
+var sixteens = new Buffer(16)
+sixteens.fill(16)
+corectPaddingWords(sixteens, new Buffer(''))
+var fifteens = new Buffer(16)
+fifteens.fill(15)
+fifteens[0] = 5
+corectPaddingWords(fifteens, new Buffer([5]))
+var one = _crypto.randomBytes(16)
+one[15] = 1
+corectPaddingWords(one, one.slice(0, -1))
+function incorectPaddingthrows (padding) {
test('incorrect padding ' + padding.toString('hex'), function (t) {
- t.plan(2);
- var block1 = new Buffer(16);
- block1.fill(4);
- var cipher = crypto.createCipher('aes128', new Buffer('password'));
- cipher.setAutoPadding(false);
- var decipher = crypto.createDecipher('aes128', new Buffer('password'));
- var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'));
- var out = new Buffer('');
- out = Buffer.concat([out, cipher.update(block1)]);
- out = Buffer.concat([out, cipher.update(padding)]);
- decipher.update(out);
- decipher2.update(out);
+ t.plan(2)
+ var block1 = new Buffer(16)
+ block1.fill(4)
+ var cipher = crypto.createCipher('aes128', new Buffer('password'))
+ cipher.setAutoPadding(false)
+ var decipher = crypto.createDecipher('aes128', new Buffer('password'))
+ var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'))
+ var out = new Buffer('')
+ out = Buffer.concat([out, cipher.update(block1)])
+ out = Buffer.concat([out, cipher.update(padding)])
+ decipher.update(out)
+ decipher2.update(out)
t.throws(function () {
- decipher.final();
- }, 'mine');
+ decipher.final()
+ }, 'mine')
t.throws(function () {
- decipher2.final();
- }, 'node');
- });
+ decipher2.final()
+ }, 'node')
+ })
}
-function incorectPaddingDoesNotThrow(padding) {
+function incorectPaddingDoesNotThrow (padding) {
test('stream incorrect padding ' + padding.toString('hex'), function (t) {
- t.plan(2);
- var block1 = new Buffer(16);
- block1.fill(4);
- var cipher = crypto.createCipher('aes128', new Buffer('password'));
- cipher.setAutoPadding(false);
- var decipher = crypto.createDecipher('aes128', new Buffer('password'));
- var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'));
- cipher.pipe(decipher);
- cipher.pipe(decipher2);
- cipher.write(block1);
- cipher.write(padding);
+ t.plan(2)
+ var block1 = new Buffer(16)
+ block1.fill(4)
+ var cipher = crypto.createCipher('aes128', new Buffer('password'))
+ cipher.setAutoPadding(false)
+ var decipher = crypto.createDecipher('aes128', new Buffer('password'))
+ var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'))
+ cipher.pipe(decipher)
+ cipher.pipe(decipher2)
+ cipher.write(block1)
+ cipher.write(padding)
decipher.on('error', function (e) {
- t.ok(e, 'mine');
- });
+ t.ok(e, 'mine')
+ })
decipher2.on('error', function (e) {
- t.ok(e, 'node');
- });
- cipher.end();
- });
+ t.ok(e, 'node')
+ })
+ cipher.end()
+ })
}
-var sixteens2 = new Buffer(16);
-sixteens2.fill(16);
-sixteens2[3] = 5;
-incorectPaddingthrows(sixteens2);
-incorectPaddingDoesNotThrow(sixteens2);
-var fifteens2 = new Buffer(16);
-fifteens2.fill(15);
-fifteens2[0] = 5;
-fifteens2[1] = 6;
-incorectPaddingthrows(fifteens2);
-incorectPaddingDoesNotThrow(fifteens2);
-var two = _crypto.randomBytes(16);
-two[15] = 2;
-two[14] = 1;
-incorectPaddingthrows(two);
-incorectPaddingDoesNotThrow(two);
+var sixteens2 = new Buffer(16)
+sixteens2.fill(16)
+sixteens2[3] = 5
+incorectPaddingthrows(sixteens2)
+incorectPaddingDoesNotThrow(sixteens2)
+var fifteens2 = new Buffer(16)
+fifteens2.fill(15)
+fifteens2[0] = 5
+fifteens2[1] = 6
+incorectPaddingthrows(fifteens2)
+incorectPaddingDoesNotThrow(fifteens2)
+var two = _crypto.randomBytes(16)
+two[15] = 2
+two[14] = 1
+incorectPaddingthrows(two)
+incorectPaddingDoesNotThrow(two)
test('autopadding false decipher', function (t) {
- t.plan(2);
- var mycipher = crypto.createCipher('AES-128-ECB', new Buffer('password'));
- var nodecipher = _crypto.createCipher('AES-128-ECB', new Buffer('password'));
- var myEnc = mycipher.final();
- var nodeEnc = nodecipher.final();
- t.equals(myEnc.toString('hex'), nodeEnc.toString('hex'), 'same encryption');
- var decipher = crypto.createDecipher('aes-128-ecb', new Buffer('password'));
- decipher.setAutoPadding(false);
- var decipher2 = _crypto.createDecipher('aes-128-ecb', new Buffer('password'));
- decipher2.setAutoPadding(false);
- t.equals(decipher.update(myEnc).toString('hex'), decipher2.update(nodeEnc).toString('hex'), 'same decryption');
-});
+ t.plan(2)
+ var mycipher = crypto.createCipher('AES-128-ECB', new Buffer('password'))
+ var nodecipher = _crypto.createCipher('AES-128-ECB', new Buffer('password'))
+ var myEnc = mycipher.final()
+ var nodeEnc = nodecipher.final()
+ t.equals(myEnc.toString('hex'), nodeEnc.toString('hex'), 'same encryption')
+ var decipher = crypto.createDecipher('aes-128-ecb', new Buffer('password'))
+ decipher.setAutoPadding(false)
+ var decipher2 = _crypto.createDecipher('aes-128-ecb', new Buffer('password'))
+ decipher2.setAutoPadding(false)
+ t.equals(decipher.update(myEnc).toString('hex'), decipher2.update(nodeEnc).toString('hex'), 'same decryption')
+})
test('autopadding false cipher throws', function (t) {
- t.plan(2);
- var mycipher = crypto.createCipher('aes-128-ecb', new Buffer('password'));
- mycipher.setAutoPadding(false);
- var nodecipher = _crypto.createCipher('aes-128-ecb', new Buffer('password'));
- nodecipher.setAutoPadding(false);
- mycipher.update('foo');
- nodecipher.update('foo');
+ t.plan(2)
+ var mycipher = crypto.createCipher('aes-128-ecb', new Buffer('password'))
+ mycipher.setAutoPadding(false)
+ var nodecipher = _crypto.createCipher('aes-128-ecb', new Buffer('password'))
+ nodecipher.setAutoPadding(false)
+ mycipher.update('foo')
+ nodecipher.update('foo')
t.throws(function () {
- mycipher.final();
- }, 'mine');
+ mycipher.final()
+ }, 'mine')
t.throws(function () {
- nodecipher.final();
- }, 'node');
-});
+ nodecipher.final()
+ }, 'node')
+})
test('getCiphers works', function (t) {
- t.plan(1);
- t.ok(crypto.getCiphers().length, 'get some ciphers');
-})
\ No newline at end of file
+ t.plan(1)
+ t.ok(crypto.getCiphers().length, 'get some ciphers')
+})
diff --git a/xor.js b/xor.js
index f504ea8..71aa0a5 100644
--- a/xor.js
+++ b/xor.js
@@ -1,10 +1,10 @@
-module.exports = xor;
-function xor(a, b) {
- var len = Math.min(a.length, b.length);
- var out = new Buffer(len);
- var i = -1;
+module.exports = xor
+function xor (a, b) {
+ var len = Math.min(a.length, b.length)
+ var out = new Buffer(len)
+ var i = -1
while (++i < len) {
- out.writeUInt8(a[i] ^ b[i], i);
+ out.writeUInt8(a[i] ^ b[i], i)
}
- return out;
-}
\ No newline at end of file
+ return out
+}
--
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