diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/des.js | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) | |
download | wallet-core-de98e0b232509d5f40c135d540a70e415272ff85.tar.xz |
node_modules
Diffstat (limited to 'node_modules/des.js')
-rw-r--r-- | node_modules/des.js/.jscsrc | 46 | ||||
-rw-r--r-- | node_modules/des.js/.jshintrc | 89 | ||||
-rw-r--r-- | node_modules/des.js/.npmignore | 2 | ||||
-rw-r--r-- | node_modules/des.js/README.md | 26 | ||||
-rw-r--r-- | node_modules/des.js/lib/des.js | 7 | ||||
-rw-r--r-- | node_modules/des.js/lib/des/cbc.js | 65 | ||||
-rw-r--r-- | node_modules/des.js/lib/des/cipher.js | 141 | ||||
-rw-r--r-- | node_modules/des.js/lib/des/des.js | 143 | ||||
-rw-r--r-- | node_modules/des.js/lib/des/ede.js | 55 | ||||
-rw-r--r-- | node_modules/des.js/lib/des/utils.js | 256 | ||||
-rw-r--r-- | node_modules/des.js/package.json | 34 | ||||
-rw-r--r-- | node_modules/des.js/test/cbc-test.js | 73 | ||||
-rw-r--r-- | node_modules/des.js/test/des-test.js | 139 | ||||
-rw-r--r-- | node_modules/des.js/test/ede-test.js | 73 | ||||
-rw-r--r-- | node_modules/des.js/test/fixtures.js | 5 | ||||
-rw-r--r-- | node_modules/des.js/test/utils-test.js | 169 |
16 files changed, 1323 insertions, 0 deletions
diff --git a/node_modules/des.js/.jscsrc b/node_modules/des.js/.jscsrc new file mode 100644 index 000000000..dbaae2057 --- /dev/null +++ b/node_modules/des.js/.jscsrc @@ -0,0 +1,46 @@ +{ + "disallowKeywordsOnNewLine": [ "else" ], + "disallowMixedSpacesAndTabs": true, + "disallowMultipleLineStrings": true, + "disallowMultipleVarDecl": true, + "disallowNewlineBeforeBlockStatements": true, + "disallowQuotedKeysInObjects": true, + "disallowSpaceAfterObjectKeys": true, + "disallowSpaceAfterPrefixUnaryOperators": true, + "disallowSpaceBeforePostfixUnaryOperators": true, + "disallowSpacesInCallExpression": true, + "disallowTrailingComma": true, + "disallowTrailingWhitespace": true, + "disallowYodaConditions": true, + + "requireCommaBeforeLineBreak": true, + "requireOperatorBeforeLineBreak": true, + "requireSpaceAfterBinaryOperators": true, + "requireSpaceAfterKeywords": [ "if", "for", "while", "else", "try", "catch" ], + "requireSpaceAfterLineComment": true, + "requireSpaceBeforeBinaryOperators": true, + "requireSpaceBeforeBlockStatements": true, + "requireSpaceBeforeKeywords": [ "else", "catch" ], + "requireSpaceBeforeObjectValues": true, + "requireSpaceBetweenArguments": true, + "requireSpacesInAnonymousFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInFunctionDeclaration": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInConditionalExpression": true, + "requireSpacesInForStatement": true, + "requireSpacesInsideArrayBrackets": "all", + "requireSpacesInsideObjectBrackets": "all", + "requireDotNotation": true, + + "maximumLineLength": 80, + "validateIndentation": 2, + "validateLineBreaks": "LF", + "validateParameterSeparator": ", ", + "validateQuoteMarks": "'" +} diff --git a/node_modules/des.js/.jshintrc b/node_modules/des.js/.jshintrc new file mode 100644 index 000000000..7e9739029 --- /dev/null +++ b/node_modules/des.js/.jshintrc @@ -0,0 +1,89 @@ +{ + // JSHint Default Configuration File (as on JSHint website) + // See http://jshint.com/docs/ for more details + + "maxerr" : 50, // {int} Maximum error before stopping + + // Enforcing + "bitwise" : false, // true: Prohibit bitwise operators (&, |, ^, etc.) + "camelcase" : false, // true: Identifiers must be in camelCase + "curly" : false, // true: Require {} for every new block or scope + "eqeqeq" : true, // true: Require triple equals (===) for comparison + "forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty() + "freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc. + "immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` + "indent" : 2, // {int} Number of spaces to use for indentation + "latedef" : true, // true: Require variables/functions to be defined before being used + "newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()` + "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` + "noempty" : false, // true: Prohibit use of empty blocks + "nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters. + "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) + "plusplus" : false, // true: Prohibit use of `++` & `--` + "quotmark" : "single", // Quotation mark consistency: + // false : do nothing (default) + // true : ensure whatever is used is consistent + // "single" : require single quotes + // "double" : require double quotes + "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) + "unused" : true, // true: Require all defined variables be used + "strict" : true, // true: Requires all functions run in ES5 Strict Mode + "maxparams" : false, // {int} Max number of formal params allowed per function + "maxdepth" : 3, // {int} Max depth of nested blocks (within functions) + "maxstatements" : false, // {int} Max number statements per function + "maxcomplexity" : false, // {int} Max cyclomatic complexity per function + "maxlen" : false, // {int} Max number of characters per line + + // Relaxing + "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) + "boss" : false, // true: Tolerate assignments where comparisons would be expected + "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. + "eqnull" : false, // true: Tolerate use of `== null` + "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) + "esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`) + "moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features) + // (ex: `for each`, multiple try/catch, function expression…) + "evil" : false, // true: Tolerate use of `eval` and `new Function()` + "expr" : false, // true: Tolerate `ExpressionStatement` as Programs + "funcscope" : false, // true: Tolerate defining variables inside control statements + "globalstrict" : false, // true: Allow global "use strict" (also enables 'strict') + "iterator" : false, // true: Tolerate using the `__iterator__` property + "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block + "laxbreak" : false, // true: Tolerate possibly unsafe line breakings + "laxcomma" : false, // true: Tolerate comma-first style coding + "loopfunc" : false, // true: Tolerate functions being defined in loops + "multistr" : false, // true: Tolerate multi-line strings + "noyield" : false, // true: Tolerate generator functions with no yield statement in them. + "notypeof" : false, // true: Tolerate invalid typeof operator values + "proto" : false, // true: Tolerate using the `__proto__` property + "scripturl" : false, // true: Tolerate script-targeted URLs + "shadow" : true, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` + "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation + "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` + "validthis" : false, // true: Tolerate using this in a non-constructor function + + // Environments + "browser" : true, // Web Browser (window, document, etc) + "browserify" : true, // Browserify (node.js code in the browser) + "couch" : false, // CouchDB + "devel" : true, // Development/debugging (alert, confirm, etc) + "dojo" : false, // Dojo Toolkit + "jasmine" : false, // Jasmine + "jquery" : false, // jQuery + "mocha" : true, // Mocha + "mootools" : false, // MooTools + "node" : true, // Node.js + "nonstandard" : false, // Widely adopted globals (escape, unescape, etc) + "prototypejs" : false, // Prototype and Scriptaculous + "qunit" : false, // QUnit + "rhino" : false, // Rhino + "shelljs" : false, // ShellJS + "worker" : false, // Web Workers + "wsh" : false, // Windows Scripting Host + "yui" : false, // Yahoo User Interface + + // Custom Globals + "globals" : { + "module": true + } // additional predefined global variables +} diff --git a/node_modules/des.js/.npmignore b/node_modules/des.js/.npmignore new file mode 100644 index 000000000..1ca957177 --- /dev/null +++ b/node_modules/des.js/.npmignore @@ -0,0 +1,2 @@ +node_modules/ +npm-debug.log diff --git a/node_modules/des.js/README.md b/node_modules/des.js/README.md new file mode 100644 index 000000000..976de33bd --- /dev/null +++ b/node_modules/des.js/README.md @@ -0,0 +1,26 @@ +# DES.js + +## LICENSE + +This software is licensed under the MIT License. + +Copyright Fedor Indutny, 2015. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/des.js/lib/des.js b/node_modules/des.js/lib/des.js new file mode 100644 index 000000000..2fd04a6dc --- /dev/null +++ b/node_modules/des.js/lib/des.js @@ -0,0 +1,7 @@ +'use strict'; + +exports.utils = require('./des/utils'); +exports.Cipher = require('./des/cipher'); +exports.DES = require('./des/des'); +exports.CBC = require('./des/cbc'); +exports.EDE = require('./des/ede'); diff --git a/node_modules/des.js/lib/des/cbc.js b/node_modules/des.js/lib/des/cbc.js new file mode 100644 index 000000000..208ea3133 --- /dev/null +++ b/node_modules/des.js/lib/des/cbc.js @@ -0,0 +1,65 @@ +'use strict'; + +var assert = require('minimalistic-assert'); +var inherits = require('inherits'); + +var proto = {}; + +function CBCState(iv) { + assert.equal(iv.length, 8, 'Invalid IV length'); + + this.iv = new Array(8); + for (var i = 0; i < this.iv.length; i++) + this.iv[i] = iv[i]; +} + +function instantiate(Base) { + function CBC(options) { + Base.call(this, options); + this._cbcInit(); + } + inherits(CBC, Base); + + var keys = Object.keys(proto); + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + CBC.prototype[key] = proto[key]; + } + + CBC.create = function create(options) { + return new CBC(options); + }; + + return CBC; +} + +exports.instantiate = instantiate; + +proto._cbcInit = function _cbcInit() { + var state = new CBCState(this.options.iv); + this._cbcState = state; +}; + +proto._update = function _update(inp, inOff, out, outOff) { + var state = this._cbcState; + var superProto = this.constructor.super_.prototype; + + var iv = state.iv; + if (this.type === 'encrypt') { + for (var i = 0; i < this.blockSize; i++) + iv[i] ^= inp[inOff + i]; + + superProto._update.call(this, iv, 0, out, outOff); + + for (var i = 0; i < this.blockSize; i++) + iv[i] = out[outOff + i]; + } else { + superProto._update.call(this, inp, inOff, out, outOff); + + for (var i = 0; i < this.blockSize; i++) + out[outOff + i] ^= iv[i]; + + for (var i = 0; i < this.blockSize; i++) + iv[i] = inp[inOff + i]; + } +}; diff --git a/node_modules/des.js/lib/des/cipher.js b/node_modules/des.js/lib/des/cipher.js new file mode 100644 index 000000000..d0090b4cd --- /dev/null +++ b/node_modules/des.js/lib/des/cipher.js @@ -0,0 +1,141 @@ +'use strict'; + +var assert = require('minimalistic-assert'); + +function Cipher(options) { + this.options = options; + + this.type = this.options.type; + this.blockSize = 8; + this._init(); + + this.buffer = new Array(this.blockSize); + this.bufferOff = 0; +} +module.exports = Cipher; + +Cipher.prototype._init = function _init() { + // Might be overrided +}; + +Cipher.prototype.update = function update(data) { + if (data.length === 0) + return []; + + if (this.type === 'decrypt') + return this._updateDecrypt(data); + else + return this._updateEncrypt(data); +}; + +Cipher.prototype._buffer = function _buffer(data, off) { + // Append data to buffer + var min = Math.min(this.buffer.length - this.bufferOff, data.length - off); + for (var i = 0; i < min; i++) + this.buffer[this.bufferOff + i] = data[off + i]; + this.bufferOff += min; + + // Shift next + return min; +}; + +Cipher.prototype._flushBuffer = function _flushBuffer(out, off) { + this._update(this.buffer, 0, out, off); + this.bufferOff = 0; + return this.blockSize; +}; + +Cipher.prototype._updateEncrypt = function _updateEncrypt(data) { + var inputOff = 0; + var outputOff = 0; + + var count = ((this.bufferOff + data.length) / this.blockSize) | 0; + var out = new Array(count * this.blockSize); + + if (this.bufferOff !== 0) { + inputOff += this._buffer(data, inputOff); + + if (this.bufferOff === this.buffer.length) + outputOff += this._flushBuffer(out, outputOff); + } + + // Write blocks + var max = data.length - ((data.length - inputOff) % this.blockSize); + for (; inputOff < max; inputOff += this.blockSize) { + this._update(data, inputOff, out, outputOff); + outputOff += this.blockSize; + } + + // Queue rest + for (; inputOff < data.length; inputOff++, this.bufferOff++) + this.buffer[this.bufferOff] = data[inputOff]; + + return out; +}; + +Cipher.prototype._updateDecrypt = function _updateDecrypt(data) { + var inputOff = 0; + var outputOff = 0; + + var count = Math.ceil((this.bufferOff + data.length) / this.blockSize) - 1; + var out = new Array(count * this.blockSize); + + // TODO(indutny): optimize it, this is far from optimal + for (; count > 0; count--) { + inputOff += this._buffer(data, inputOff); + outputOff += this._flushBuffer(out, outputOff); + } + + // Buffer rest of the input + inputOff += this._buffer(data, inputOff); + + return out; +}; + +Cipher.prototype.final = function final(buffer) { + var first; + if (buffer) + first = this.update(buffer); + + var last; + if (this.type === 'encrypt') + last = this._finalEncrypt(); + else + last = this._finalDecrypt(); + + if (first) + return first.concat(last); + else + return last; +}; + +Cipher.prototype._pad = function _pad(buffer, off) { + if (off === 0) + return false; + + while (off < buffer.length) + buffer[off++] = 0; + + return true; +}; + +Cipher.prototype._finalEncrypt = function _finalEncrypt() { + if (!this._pad(this.buffer, this.bufferOff)) + return []; + + var out = new Array(this.blockSize); + this._update(this.buffer, 0, out, 0); + return out; +}; + +Cipher.prototype._unpad = function _unpad(buffer) { + return buffer; +}; + +Cipher.prototype._finalDecrypt = function _finalDecrypt() { + assert.equal(this.bufferOff, this.blockSize, 'Not enough data to decrypt'); + var out = new Array(this.blockSize); + this._flushBuffer(out, 0); + + return this._unpad(out); +}; diff --git a/node_modules/des.js/lib/des/des.js b/node_modules/des.js/lib/des/des.js new file mode 100644 index 000000000..b64a6d1b0 --- /dev/null +++ b/node_modules/des.js/lib/des/des.js @@ -0,0 +1,143 @@ +'use strict'; + +var assert = require('minimalistic-assert'); +var inherits = require('inherits'); + +var des = require('../des'); +var utils = des.utils; +var Cipher = des.Cipher; + +function DESState() { + this.tmp = new Array(2); + this.keys = null; +} + +function DES(options) { + Cipher.call(this, options); + + var state = new DESState(); + this._desState = state; + + this.deriveKeys(state, options.key); +} +inherits(DES, Cipher); +module.exports = DES; + +DES.create = function create(options) { + return new DES(options); +}; + +var shiftTable = [ + 1, 1, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 1 +]; + +DES.prototype.deriveKeys = function deriveKeys(state, key) { + state.keys = new Array(16 * 2); + + assert.equal(key.length, this.blockSize, 'Invalid key length'); + + var kL = utils.readUInt32BE(key, 0); + var kR = utils.readUInt32BE(key, 4); + + utils.pc1(kL, kR, state.tmp, 0); + kL = state.tmp[0]; + kR = state.tmp[1]; + for (var i = 0; i < state.keys.length; i += 2) { + var shift = shiftTable[i >>> 1]; + kL = utils.r28shl(kL, shift); + kR = utils.r28shl(kR, shift); + utils.pc2(kL, kR, state.keys, i); + } +}; + +DES.prototype._update = function _update(inp, inOff, out, outOff) { + var state = this._desState; + + var l = utils.readUInt32BE(inp, inOff); + var r = utils.readUInt32BE(inp, inOff + 4); + + // Initial Permutation + utils.ip(l, r, state.tmp, 0); + l = state.tmp[0]; + r = state.tmp[1]; + + if (this.type === 'encrypt') + this._encrypt(state, l, r, state.tmp, 0); + else + this._decrypt(state, l, r, state.tmp, 0); + + l = state.tmp[0]; + r = state.tmp[1]; + + utils.writeUInt32BE(out, l, outOff); + utils.writeUInt32BE(out, r, outOff + 4); +}; + +DES.prototype._pad = function _pad(buffer, off) { + var value = buffer.length - off; + for (var i = off; i < buffer.length; i++) + buffer[i] = value; + + return true; +}; + +DES.prototype._unpad = function _unpad(buffer) { + var pad = buffer[buffer.length - 1]; + for (var i = buffer.length - pad; i < buffer.length; i++) + assert.equal(buffer[i], pad); + + return buffer.slice(0, buffer.length - pad); +}; + +DES.prototype._encrypt = function _encrypt(state, lStart, rStart, out, off) { + var l = lStart; + var r = rStart; + + // Apply f() x16 times + for (var i = 0; i < state.keys.length; i += 2) { + var keyL = state.keys[i]; + var keyR = state.keys[i + 1]; + + // f(r, k) + utils.expand(r, state.tmp, 0); + + keyL ^= state.tmp[0]; + keyR ^= state.tmp[1]; + var s = utils.substitute(keyL, keyR); + var f = utils.permute(s); + + var t = r; + r = (l ^ f) >>> 0; + l = t; + } + + // Reverse Initial Permutation + utils.rip(r, l, out, off); +}; + +DES.prototype._decrypt = function _decrypt(state, lStart, rStart, out, off) { + var l = rStart; + var r = lStart; + + // Apply f() x16 times + for (var i = state.keys.length - 2; i >= 0; i -= 2) { + var keyL = state.keys[i]; + var keyR = state.keys[i + 1]; + + // f(r, k) + utils.expand(l, state.tmp, 0); + + keyL ^= state.tmp[0]; + keyR ^= state.tmp[1]; + var s = utils.substitute(keyL, keyR); + var f = utils.permute(s); + + var t = l; + l = (r ^ f) >>> 0; + r = t; + } + + // Reverse Initial Permutation + utils.rip(l, r, out, off); +}; diff --git a/node_modules/des.js/lib/des/ede.js b/node_modules/des.js/lib/des/ede.js new file mode 100644 index 000000000..cefc4a5c7 --- /dev/null +++ b/node_modules/des.js/lib/des/ede.js @@ -0,0 +1,55 @@ +'use strict'; + +var assert = require('minimalistic-assert'); +var inherits = require('inherits'); + +var des = require('../des'); +var Cipher = des.Cipher; +var DES = des.DES; + +function EDEState(type, key) { + assert.equal(key.length, 24, 'Invalid key length'); + + var k1 = key.slice(0, 8); + var k2 = key.slice(8, 16); + var k3 = key.slice(16, 24); + + if (type === 'encrypt') { + this.ciphers = [ + DES.create({ type: 'encrypt', key: k1 }), + DES.create({ type: 'decrypt', key: k2 }), + DES.create({ type: 'encrypt', key: k3 }) + ]; + } else { + this.ciphers = [ + DES.create({ type: 'decrypt', key: k3 }), + DES.create({ type: 'encrypt', key: k2 }), + DES.create({ type: 'decrypt', key: k1 }) + ]; + } +} + +function EDE(options) { + Cipher.call(this, options); + + var state = new EDEState(this.type, this.options.key); + this._edeState = state; +} +inherits(EDE, Cipher); + +module.exports = EDE; + +EDE.create = function create(options) { + return new EDE(options); +}; + +EDE.prototype._update = function _update(inp, inOff, out, outOff) { + var state = this._edeState; + + state.ciphers[0]._update(inp, inOff, out, outOff); + state.ciphers[1]._update(out, outOff, out, outOff); + state.ciphers[2]._update(out, outOff, out, outOff); +}; + +EDE.prototype._pad = DES.prototype._pad; +EDE.prototype._unpad = DES.prototype._unpad; diff --git a/node_modules/des.js/lib/des/utils.js b/node_modules/des.js/lib/des/utils.js new file mode 100644 index 000000000..08a5ccd9c --- /dev/null +++ b/node_modules/des.js/lib/des/utils.js @@ -0,0 +1,256 @@ +'use strict'; + +exports.readUInt32BE = function readUInt32BE(bytes, off) { + var res = (bytes[0 + off] << 24) | + (bytes[1 + off] << 16) | + (bytes[2 + off] << 8) | + bytes[3 + off]; + return res >>> 0; +}; + +exports.writeUInt32BE = function writeUInt32BE(bytes, value, off) { + bytes[0 + off] = value >>> 24; + bytes[1 + off] = (value >>> 16) & 0xff; + bytes[2 + off] = (value >>> 8) & 0xff; + bytes[3 + off] = value & 0xff; +}; + +exports.ip = function ip(inL, inR, out, off) { + var outL = 0; + var outR = 0; + + for (var i = 6; i >= 0; i -= 2) { + for (var j = 0; j <= 24; j += 8) { + outL <<= 1; + outL |= (inR >>> (j + i)) & 1; + } + for (var j = 0; j <= 24; j += 8) { + outL <<= 1; + outL |= (inL >>> (j + i)) & 1; + } + } + + for (var i = 6; i >= 0; i -= 2) { + for (var j = 1; j <= 25; j += 8) { + outR <<= 1; + outR |= (inR >>> (j + i)) & 1; + } + for (var j = 1; j <= 25; j += 8) { + outR <<= 1; + outR |= (inL >>> (j + i)) & 1; + } + } + + out[off + 0] = outL >>> 0; + out[off + 1] = outR >>> 0; +}; + +exports.rip = function rip(inL, inR, out, off) { + var outL = 0; + var outR = 0; + + for (var i = 0; i < 4; i++) { + for (var j = 24; j >= 0; j -= 8) { + outL <<= 1; + outL |= (inR >>> (j + i)) & 1; + outL <<= 1; + outL |= (inL >>> (j + i)) & 1; + } + } + for (var i = 4; i < 8; i++) { + for (var j = 24; j >= 0; j -= 8) { + outR <<= 1; + outR |= (inR >>> (j + i)) & 1; + outR <<= 1; + outR |= (inL >>> (j + i)) & 1; + } + } + + out[off + 0] = outL >>> 0; + out[off + 1] = outR >>> 0; +}; + +exports.pc1 = function pc1(inL, inR, out, off) { + var outL = 0; + var outR = 0; + + // 7, 15, 23, 31, 39, 47, 55, 63 + // 6, 14, 22, 30, 39, 47, 55, 63 + // 5, 13, 21, 29, 39, 47, 55, 63 + // 4, 12, 20, 28 + for (var i = 7; i >= 5; i--) { + for (var j = 0; j <= 24; j += 8) { + outL <<= 1; + outL |= (inR >> (j + i)) & 1; + } + for (var j = 0; j <= 24; j += 8) { + outL <<= 1; + outL |= (inL >> (j + i)) & 1; + } + } + for (var j = 0; j <= 24; j += 8) { + outL <<= 1; + outL |= (inR >> (j + i)) & 1; + } + + // 1, 9, 17, 25, 33, 41, 49, 57 + // 2, 10, 18, 26, 34, 42, 50, 58 + // 3, 11, 19, 27, 35, 43, 51, 59 + // 36, 44, 52, 60 + for (var i = 1; i <= 3; i++) { + for (var j = 0; j <= 24; j += 8) { + outR <<= 1; + outR |= (inR >> (j + i)) & 1; + } + for (var j = 0; j <= 24; j += 8) { + outR <<= 1; + outR |= (inL >> (j + i)) & 1; + } + } + for (var j = 0; j <= 24; j += 8) { + outR <<= 1; + outR |= (inL >> (j + i)) & 1; + } + + out[off + 0] = outL >>> 0; + out[off + 1] = outR >>> 0; +}; + +exports.r28shl = function r28shl(num, shift) { + return ((num << shift) & 0xfffffff) | (num >>> (28 - shift)); +}; + +var pc2table = [ + // inL => outL + 14, 11, 17, 4, 27, 23, 25, 0, + 13, 22, 7, 18, 5, 9, 16, 24, + 2, 20, 12, 21, 1, 8, 15, 26, + + // inR => outR + 15, 4, 25, 19, 9, 1, 26, 16, + 5, 11, 23, 8, 12, 7, 17, 0, + 22, 3, 10, 14, 6, 20, 27, 24 +]; + +exports.pc2 = function pc2(inL, inR, out, off) { + var outL = 0; + var outR = 0; + + var len = pc2table.length >>> 1; + for (var i = 0; i < len; i++) { + outL <<= 1; + outL |= (inL >>> pc2table[i]) & 0x1; + } + for (var i = len; i < pc2table.length; i++) { + outR <<= 1; + outR |= (inR >>> pc2table[i]) & 0x1; + } + + out[off + 0] = outL >>> 0; + out[off + 1] = outR >>> 0; +}; + +exports.expand = function expand(r, out, off) { + var outL = 0; + var outR = 0; + + outL = ((r & 1) << 5) | (r >>> 27); + for (var i = 23; i >= 15; i -= 4) { + outL <<= 6; + outL |= (r >>> i) & 0x3f; + } + for (var i = 11; i >= 3; i -= 4) { + outR |= (r >>> i) & 0x3f; + outR <<= 6; + } + outR |= ((r & 0x1f) << 1) | (r >>> 31); + + out[off + 0] = outL >>> 0; + out[off + 1] = outR >>> 0; +}; + +var sTable = [ + 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1, + 3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8, + 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7, + 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13, + + 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14, + 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5, + 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2, + 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9, + + 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10, + 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1, + 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7, + 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12, + + 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3, + 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9, + 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8, + 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14, + + 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1, + 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6, + 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13, + 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3, + + 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5, + 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8, + 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10, + 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13, + + 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10, + 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6, + 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7, + 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12, + + 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4, + 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2, + 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13, + 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11 +]; + +exports.substitute = function substitute(inL, inR) { + var out = 0; + for (var i = 0; i < 4; i++) { + var b = (inL >>> (18 - i * 6)) & 0x3f; + var sb = sTable[i * 0x40 + b]; + + out <<= 4; + out |= sb; + } + for (var i = 0; i < 4; i++) { + var b = (inR >>> (18 - i * 6)) & 0x3f; + var sb = sTable[4 * 0x40 + i * 0x40 + b]; + + out <<= 4; + out |= sb; + } + return out >>> 0; +}; + +var permuteTable = [ + 16, 25, 12, 11, 3, 20, 4, 15, 31, 17, 9, 6, 27, 14, 1, 22, + 30, 24, 8, 18, 0, 5, 29, 23, 13, 19, 2, 26, 10, 21, 28, 7 +]; + +exports.permute = function permute(num) { + var out = 0; + for (var i = 0; i < permuteTable.length; i++) { + out <<= 1; + out |= (num >>> permuteTable[i]) & 0x1; + } + return out >>> 0; +}; + +exports.padSplit = function padSplit(num, size, group) { + var str = num.toString(2); + while (str.length < size) + str = '0' + str; + + var out = []; + for (var i = 0; i < size; i += group) + out.push(str.slice(i, i + group)); + return out.join(' '); +}; diff --git a/node_modules/des.js/package.json b/node_modules/des.js/package.json new file mode 100644 index 000000000..2ec391927 --- /dev/null +++ b/node_modules/des.js/package.json @@ -0,0 +1,34 @@ +{ + "name": "des.js", + "version": "1.0.0", + "description": "DES implementation", + "main": "lib/des.js", + "scripts": { + "test": "mocha --reporter=spec test/*-test.js && jscs lib/*.js lib/**/*.js test/*.js && jshint lib/*.js lib/**/*.js" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/indutny/des.js.git" + }, + "keywords": [ + "DES", + "3DES", + "EDE", + "CBC" + ], + "author": "Fedor Indutny <fedor@indutny.com>", + "license": "MIT", + "bugs": { + "url": "https://github.com/indutny/des.js/issues" + }, + "homepage": "https://github.com/indutny/des.js#readme", + "devDependencies": { + "jscs": "^2.1.1", + "jshint": "^2.8.0", + "mocha": "^2.3.0" + }, + "dependencies": { + "inherits": "^2.0.1", + "minimalistic-assert": "^1.0.0" + } +} diff --git a/node_modules/des.js/test/cbc-test.js b/node_modules/des.js/test/cbc-test.js new file mode 100644 index 000000000..d07881c13 --- /dev/null +++ b/node_modules/des.js/test/cbc-test.js @@ -0,0 +1,73 @@ +'use strict'; + +var assert = require('assert'); +var crypto = require('crypto'); +var Buffer = require('buffer').Buffer; + +var des = require('../'); + +var fixtures = require('./fixtures'); +var bin = fixtures.bin; + +describe('DES-CBC', function() { + var CBC = des.CBC.instantiate(des.DES); + + describe('encryption/decryption', function() { + var vectors = [ + { + key: '133457799bbcdff1', + iv: '0102030405060708', + input: '0123456789abcdef' + }, + { + key: '0000000000000000', + iv: 'ffffffffffffffff', + input: '0000000000000000' + }, + { + key: 'a3a3a3a3b3b3b3b3', + iv: 'cdcdcdcdcdcdcdcd', + input: 'cccccccccccccccc' + }, + { + key: 'deadbeefabbadead', + iv: 'a0da0da0da0da0da', + input: '0102030405060708090a' + }, + { + key: 'aabbccddeeff0011', + iv: 'fefefefefefefefe', + input: '0102030405060708090a0102030405060708090a0102030405060708090a' + + '0102030405060708090a0102030405060607080a0102030405060708090a' + } + ]; + + vectors.forEach(function(vec, i) { + it('should encrypt vector ' + i, function() { + var key = new Buffer(vec.key, 'hex'); + var iv = new Buffer(vec.iv, 'hex'); + var input = new Buffer(vec.input, 'hex'); + + var enc = CBC.create({ + type: 'encrypt', + key: key, + iv: iv + }); + var out = new Buffer(enc.update(input).concat(enc.final())); + + var cipher = crypto.createCipheriv('des-cbc', key, iv); + var expected = Buffer.concat([ cipher.update(input), cipher.final() ]); + + assert.deepEqual(out, expected); + + var dec = CBC.create({ + type: 'decrypt', + key: key, + iv: iv + }); + assert.deepEqual(new Buffer(dec.update(out).concat(dec.final())), + input); + }); + }); + }); +}); diff --git a/node_modules/des.js/test/des-test.js b/node_modules/des.js/test/des-test.js new file mode 100644 index 000000000..b6a5ee84f --- /dev/null +++ b/node_modules/des.js/test/des-test.js @@ -0,0 +1,139 @@ +'use strict'; + +var assert = require('assert'); +var crypto = require('crypto'); +var Buffer = require('buffer').Buffer; + +var des = require('../'); + +var fixtures = require('./fixtures'); +var bin = fixtures.bin; + +describe('DES', function() { + describe('Key Derivation', function() { + it('should derive proper keys', function() { + var d = des.DES.create({ + type: 'encrypt', + key: [ 0x13, 0x34, 0x57, 0x79, 0x9B, 0xBC, 0xDF, 0xF1 ] + }); + + var expected = [ + '000110 110000 001011 101111', + '111111 000111 000001 110010', + '011110 011010 111011 011001', + '110110 111100 100111 100101', + '010101 011111 110010 001010', + '010000 101100 111110 011001', + '011100 101010 110111 010110', + '110110 110011 010100 011101', + '011111 001110 110000 000111', + '111010 110101 001110 101000', + '011000 111010 010100 111110', + '010100 000111 101100 101111', + '111011 001000 010010 110111', + '111101 100001 100010 111100', + '111101 111000 101000 111010', + '110000 010011 101111 111011', + '111000 001101 101111 101011', + '111011 011110 011110 000001', + '101100 011111 001101 000111', + '101110 100100 011001 001111', + '001000 010101 111111 010011', + '110111 101101 001110 000110', + '011101 010111 000111 110101', + '100101 000110 011111 101001', + '100101 111100 010111 010001', + '111110 101011 101001 000001', + '010111 110100 001110 110111', + '111100 101110 011100 111010', + '101111 111001 000110 001101', + '001111 010011 111100 001010', + '110010 110011 110110 001011', + '000011 100001 011111 110101' + ]; + + expected = expected.map(fixtures.bin); + assert.deepEqual(d._desState.keys, expected); + }); + }); + + describe('encryption/decryption', function() { + var vectors = [ + { + key: '133457799bbcdff1', + input: '0123456789abcdef' + }, + { + key: '0000000000000000', + input: '0000000000000000' + }, + { + key: 'a3a3a3a3b3b3b3b3', + input: 'cccccccccccccccc' + }, + { + key: 'deadbeefabbadead', + input: '0102030405060708090a' + }, + { + key: 'aabbccddeeff0011', + input: '0102030405060708090a0102030405060708090a0102030405060708090a' + + '0102030405060708090a0102030405060607080a0102030405060708090a' + } + ]; + + vectors.forEach(function(vec, i) { + it('should encrypt vector ' + i, function() { + var key = new Buffer(vec.key, 'hex'); + var input = new Buffer(vec.input, 'hex'); + + var enc = des.DES.create({ + type: 'encrypt', + key: key + }); + var dec = des.DES.create({ + type: 'decrypt', + key: key + }); + var out = new Buffer(enc.update(input).concat(enc.final())); + + var cipher = crypto.createCipheriv('des-ecb', key, new Buffer(0)); + var expected = Buffer.concat([ cipher.update(input), cipher.final() ]); + + assert.deepEqual(out, expected); + + assert.deepEqual(new Buffer(dec.update(out).concat(dec.final())), + input); + }); + }); + + it('should buffer during encryption/decryption', function() { + var key = new Buffer('0102030405060708', 'hex'); + var chunk = new Buffer('01020304050607', 'hex'); + var count = 257; + var expected = new Buffer( + new Array(count + 1).join('01020304050607'), 'hex'); + + var enc = des.DES.create({ + type: 'encrypt', + key: key + }); + var cipher = []; + for (var i = 0; i < count; i++) + cipher = cipher.concat(enc.update(chunk)); + cipher = cipher.concat(enc.final()); + + var dec = des.DES.create({ + type: 'decrypt', + key: key + }); + var out = []; + for (var i = 0; i < count; i++) + out = out.concat(dec.update(cipher.slice(i * 7, (i + 1) * 7))); + out = out.concat(dec.final(cipher.slice(i * 7))); + + out = new Buffer(out); + assert.deepEqual(out, expected); + }); + }); +}); diff --git a/node_modules/des.js/test/ede-test.js b/node_modules/des.js/test/ede-test.js new file mode 100644 index 000000000..116a76a82 --- /dev/null +++ b/node_modules/des.js/test/ede-test.js @@ -0,0 +1,73 @@ +'use strict'; + +var assert = require('assert'); +var crypto = require('crypto'); +var Buffer = require('buffer').Buffer; + +var des = require('../'); + +var fixtures = require('./fixtures'); +var bin = fixtures.bin; + +describe('DES-EDE-CBC', function() { + var CBC = des.CBC.instantiate(des.EDE); + + describe('encryption/decryption', function() { + var vectors = [ + { + key: new Array(4).join('133457799bbcdff1'), + iv: '0102030405060708', + input: '0123456789abcdef' + }, + { + key: new Array(4).join('0000000000000000'), + iv: 'ffffffffffffffff', + input: '0000000000000000' + }, + { + key: new Array(4).join('a3a3a3a3b3b3b3b3'), + iv: 'cdcdcdcdcdcdcdcd', + input: 'cccccccccccccccc' + }, + { + key: new Array(4).join('deadbeefabbadead'), + iv: 'a0da0da0da0da0da', + input: '0102030405060708090a' + }, + { + key: 'aabbccddeeff0011' + '1111222233334444' + 'ffffeeeeddddcccc', + iv: 'fefefefefefefefe', + input: '0102030405060708090a0102030405060708090a0102030405060708090a' + + '0102030405060708090a0102030405060607080a0102030405060708090a' + } + ]; + + vectors.forEach(function(vec, i) { + it('should encrypt vector ' + i, function() { + var key = new Buffer(vec.key, 'hex'); + var iv = new Buffer(vec.iv, 'hex'); + var input = new Buffer(vec.input, 'hex'); + + var enc = CBC.create({ + type: 'encrypt', + key: key, + iv: iv + }); + var out = new Buffer(enc.update(input).concat(enc.final())); + + var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv); + var expected = Buffer.concat([ cipher.update(input), cipher.final() ]); + + assert.deepEqual(out, expected); + + var dec = CBC.create({ + type: 'decrypt', + key: key, + iv: iv + }); + assert.deepEqual(new Buffer(dec.update(out).concat(dec.final())), + input); + }); + }); + }); +}); diff --git a/node_modules/des.js/test/fixtures.js b/node_modules/des.js/test/fixtures.js new file mode 100644 index 000000000..fe8ccd8b6 --- /dev/null +++ b/node_modules/des.js/test/fixtures.js @@ -0,0 +1,5 @@ +'use strict'; + +exports.bin = function bin(str) { + return parseInt(str.replace(/[^01]/g, ''), 2); +} diff --git a/node_modules/des.js/test/utils-test.js b/node_modules/des.js/test/utils-test.js new file mode 100644 index 000000000..4c576de08 --- /dev/null +++ b/node_modules/des.js/test/utils-test.js @@ -0,0 +1,169 @@ +'use strict'; + +var assert = require('assert'); + +var des = require('../'); +var utils = des.utils; + +var fixtures = require('./fixtures'); +var bin = fixtures.bin; + +describe('utils', function() { + describe('IP', function() { + it('should permute properly', function() { + var out = new Array(2); + var inp = [ + bin('00000001 00100011 01000101 01100111'), + bin('10001001 10101011 11001101 11101111') + ]; + + utils.ip(inp[0], inp[1], out, 0); + + var expected = [ + bin('11001100 00000000 11001100 11111111'), + bin('11110000 10101010 11110000 10101010') + ]; + + assert.deepEqual(out, expected); + }); + + it('should rev-permute properly', function() { + var out = new Array(2); + var inp = [ + bin('11001100 00000000 11001100 11111111'), + bin('11110000 10101010 11110000 10101010') + ]; + + utils.rip(inp[0], inp[1], out, 0); + + var expected = [ + bin('00000001 00100011 01000101 01100111'), + bin('10001001 10101011 11001101 11101111') + ]; + + assert.deepEqual(out, expected); + }); + }); + + describe('PC1', function() { + it('should permute properly', function() { + var out = new Array(2); + var inp = [ + bin('00010011 00110100 01010111 01111001'), + bin('10011011 10111100 11011111 11110001') + ]; + + utils.pc1(inp[0], inp[1], out, 0); + + var expected = [ + bin('1111000 0110011 0010101 0101111'), + bin('0101010 1011001 1001111 0001111') + ]; + + assert.deepEqual(out, expected); + }); + }); + + describe('r28shl', function() { + it('should shl properly', function() { + assert.equal(utils.r28shl(bin('1111000011001100101010101111'), 1), + bin('1110000110011001010101011111')); + + assert.equal(utils.r28shl(bin('0101010101100110011110001111'), 1), + bin('1010101011001100111100011110')); + + assert.equal(utils.r28shl(bin('1111000011001100101010101111'), 4), + bin('0000110011001010101011111111')); + + assert.equal(utils.r28shl(bin('0101010101100110011110001111'), 4), + bin('0101011001100111100011110101')); + }); + }); + + describe('PC2', function() { + it('should permute properly', function() { + var out = new Array(2); + var inp = [ + bin('1110000 1100110 0101010 1011111'), + bin('1010101 0110011 0011110 0011110') + ]; + + utils.pc2(inp[0], inp[1], out, 0); + + var expected = [ + bin('000110 110000 001011 101111'), + bin('111111 000111 000001 110010') + ]; + + assert.deepEqual(out, expected); + }); + }); + + describe('readUInt32BE', function() { + it('should read number properly', function() { + var a = [ 0xde, 0xad, 0xbe, 0xef ]; + var o = utils.readUInt32BE(a, 0); + assert.equal(o, 0xdeadbeef); + }); + }); + + describe('writeUInt32BE', function() { + it('should read number properly', function() { + var a = [ 0, 0, 0, 0 ]; + utils.writeUInt32BE(a, 0xdeadbeef, 0); + var expected = [ 0xde, 0xad, 0xbe, 0xef ]; + assert.deepEqual(a, expected); + }); + }); + + describe('expand', function() { + it('should expand', function() { + var out = [ 0, 0 ]; + utils.expand(bin('1111 0000 1010 1010 1111 0000 1010 1010'), out, 0); + var expected = [ + bin('011110 100001 010101 010101'), + bin('011110 100001 010101 010101') + ]; + assert.deepEqual(out, expected); + }); + + it('should expand with low 1', function() { + var out = [ 0, 0 ]; + utils.expand(bin('1111 0000 1010 1010 1111 0000 1010 1011'), out, 0); + var expected = [ + bin('111110 100001 010101 010101'), + bin('011110 100001 010101 010111') + ]; + assert.deepEqual(out, expected); + }); + + it('should expand with low 1', function() { + var out = [ 0, 0 ]; + utils.expand(bin('10100010 01011100 00001011 11110100'), out, 0); + var expected = [ + bin('010100 000100 001011 111000'), + bin('000001 010111 111110 101001') + ]; + assert.deepEqual(out, expected); + }); + }); + + describe('substitute', function() { + it('should substitute', function() { + var input = [ + bin('011000 010001 011110 111010'), + bin('100001 100110 010100 100111') + ]; + var output = utils.substitute(input[0], input[1]); + assert.equal(output, bin('0101 1100 1000 0010 1011 0101 1001 0111')); + }); + }); + + describe('permute', function() { + it('should permute', function() { + var output = utils.permute( + bin('0101 1100 1000 0010 1011 0101 1001 0111')); + assert.equal(output, bin('0010 0011 0100 1010 1010 1001 1011 1011')); + }); + }); +}); |