UNPKG

triplesec

Version:

A CommonJS-compliant system for secure encryption of smallish secrets

1,493 lines (1,306 loc) 312 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.triplesec = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ // Generated by IcedCoffeeScript 108.0.8 (function() { var AES, BlockCipher, G, Global, scrub_vec, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; BlockCipher = require('./algbase').BlockCipher; scrub_vec = require('./util').scrub_vec; Global = (function() { 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]; } 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); } else { _results.push((i << 1) ^ 0x11b); } } 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; if (x === 0) { x = xi = 1; } else { x = x2 ^ d[d[d[x8 ^ x2]]]; xi ^= d[d[xi]]; } } return true; }; return Global; })(); G = new Global(); AES = (function(_super) { __extends(AES, _super); AES.blockSize = 4 * 4; AES.prototype.blockSize = AES.blockSize; AES.keySize = 256 / 8; AES.prototype.keySize = AES.keySize; AES.ivSize = AES.blockSize; AES.prototype.ivSize = AES.ivSize; function AES(key) { this._key = key.clone(); this._doReset(); } AES.prototype._doReset = function() { var invKsRow, keySize, keyWords, ksRow, ksRows, t, _i, _j; keyWords = this._key.words; keySize = this._key.sigBytes / 4; 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._keySchedule[ksRow - keySize] ^ t); } 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]]; } return true; }; AES.prototype.encryptBlock = function(M, offset) { if (offset == null) { offset = 0; } return this._doCryptBlock(M, offset, this._keySchedule, G.SUB_MIX, G.SBOX); }; AES.prototype.decryptBlock = function(M, offset) { var _ref, _ref1; if (offset == null) { offset = 0; } _ref = [M[offset + 3], M[offset + 1]], M[offset + 1] = _ref[0], M[offset + 3] = _ref[1]; this._doCryptBlock(M, offset, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX); return _ref1 = [M[offset + 3], M[offset + 1]], M[offset + 1] = _ref1[0], M[offset + 3] = _ref1[1], _ref1; }; AES.prototype.scrub = function() { scrub_vec(this._keySchedule); scrub_vec(this._invKeySchedule); return this._key.scrub(); }; AES.prototype._doCryptBlock = function(M, offset, keySchedule, SUB_MIX, SBOX) { var ksRow, round, s0, s1, s2, s3, t0, t1, t2, t3, _i, _ref; s0 = M[offset] ^ keySchedule[0]; s1 = M[offset + 1] ^ keySchedule[1]; s2 = M[offset + 2] ^ keySchedule[2]; s3 = M[offset + 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; } 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++]; M[offset] = t0; M[offset + 1] = t1; M[offset + 2] = t2; return M[offset + 3] = t3; }; return AES; })(BlockCipher); exports.AES = AES; }).call(this); },{"./algbase":2,"./util":25}],2:[function(require,module,exports){ // Generated by IcedCoffeeScript 108.0.8 (function() { var BlockCipher, BufferedBlockAlgorithm, Hasher, StreamCipher, WordArray, util, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; WordArray = require('./wordarray').WordArray; util = require('./util'); BufferedBlockAlgorithm = (function() { BufferedBlockAlgorithm.prototype._minBufferSize = 0; function BufferedBlockAlgorithm() { this.reset(); } BufferedBlockAlgorithm.prototype.reset = function() { this._data = new WordArray(); return this._nDataBytes = 0; }; BufferedBlockAlgorithm.prototype._append = function(data) { this._data.concat(data); return this._nDataBytes += data.sigBytes; }; BufferedBlockAlgorithm.prototype._process = function(doFlush) { var blockSizeBytes, data, dataSigBytes, dataWords, nBlocksReady, nBytesReady, nWordsReady, offset, processedWords, _i, _ref; data = this._data; dataWords = data.words; dataSigBytes = data.sigBytes; blockSizeBytes = this.blockSize * 4; nBlocksReady = dataSigBytes / blockSizeBytes; if (doFlush) { nBlocksReady = Math.ceil(nBlocksReady); } else { nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0); } nWordsReady = nBlocksReady * this.blockSize; nBytesReady = Math.min(nWordsReady * 4, dataSigBytes); if (nWordsReady) { for (offset = _i = 0, _ref = this.blockSize; _ref > 0 ? _i < nWordsReady : _i > nWordsReady; offset = _i += _ref) { this._doProcessBlock(dataWords, offset); } processedWords = dataWords.splice(0, nWordsReady); data.sigBytes -= nBytesReady; } return new WordArray(processedWords, nBytesReady); }; BufferedBlockAlgorithm.prototype.copy_to = function(out) { out._data = this._data.clone(); return out._nDataBytes = this._nDataBytes; }; BufferedBlockAlgorithm.prototype.clone = function() { var obj; obj = new BufferedBlockAlgorithm(); this.copy_to(obj); return obj; }; return BufferedBlockAlgorithm; })(); Hasher = (function(_super) { __extends(Hasher, _super); function Hasher() { Hasher.__super__.constructor.call(this); } Hasher.prototype.reset = function() { Hasher.__super__.reset.call(this); this._doReset(); return this; }; Hasher.prototype.update = function(messageUpdate) { this._append(messageUpdate); this._process(); return this; }; Hasher.prototype.finalize = function(messageUpdate) { if (messageUpdate) { this._append(messageUpdate); } return this._doFinalize(); }; Hasher.prototype.bufhash = function(input) { var out, wa_in, wa_out; wa_in = WordArray.from_buffer(input); wa_out = this.finalize(wa_in); out = wa_out.to_buffer(); wa_in.scrub(); wa_out.scrub(); return out; }; return Hasher; })(BufferedBlockAlgorithm); exports.BlockCipher = BlockCipher = (function() { function BlockCipher(key) {} BlockCipher.prototype.encryptBlock = function(M, offset) {}; return BlockCipher; })(); StreamCipher = (function() { function StreamCipher() {} StreamCipher.prototype.encryptBlock = function(word_array, dst_offset) { var n_words, pad; if (dst_offset == null) { dst_offset = 0; } pad = this.get_pad(); n_words = Math.min(word_array.words.length - dst_offset, this.bsiw); word_array.xor(pad, { dst_offset: dst_offset, n_words: n_words }); pad.scrub(); return this.bsiw; }; StreamCipher.prototype.encrypt = function(word_array) { var i, _i, _ref, _ref1; for (i = _i = 0, _ref = word_array.words.length, _ref1 = this.bsiw; _ref1 > 0 ? _i < _ref : _i > _ref; i = _i += _ref1) { this.encryptBlock(word_array, i); } return word_array; }; StreamCipher.prototype.bulk_encrypt = function(_arg, cb) { var async_args, input, progress_hook, slice_args, what; input = _arg.input, progress_hook = _arg.progress_hook, what = _arg.what; slice_args = { update: (function(_this) { return function(lo, hi) { var i, _i, _ref, _results; _results = []; for (i = _i = lo, _ref = _this.bsiw; _ref > 0 ? _i < hi : _i > hi; i = _i += _ref) { _results.push(_this.encryptBlock(input, i)); } return _results; }; })(this), finalize: function() { return input; }, default_n: this.bsiw * 1024 }; async_args = { progress_hook: progress_hook, cb: cb, what: what }; return util.bulk(input.sigBytes, slice_args, async_args); }; return StreamCipher; })(); exports.BlockCipher = BlockCipher; exports.Hasher = Hasher; exports.BufferedBlockAlgorithm = BufferedBlockAlgorithm; exports.StreamCipher = StreamCipher; }).call(this); },{"./util":25,"./wordarray":26}],3:[function(require,module,exports){ // Generated by IcedCoffeeScript 108.0.8 (function() { var CombineBase, Concat, HMAC, KECCAK, SHA512, WordArray, XOR, bulk_sign, _ref, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; _ref = require('./hmac'), HMAC = _ref.HMAC, bulk_sign = _ref.bulk_sign; SHA512 = require('./sha512').SHA512; KECCAK = require('./keccak').KECCAK; WordArray = require('./wordarray').WordArray; CombineBase = (function() { function CombineBase() { this.hasherBlockSize = this.hashers[0].hasherBlockSize; this.hasherBlockSizeBytes = this.hasherBlockSize * 4; this.reset(); } CombineBase.prototype.reset = function() { var h, _i, _len, _ref1; _ref1 = this.hashers; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; h.reset(); } return this; }; CombineBase.prototype.update = function(w) { var h, _i, _len, _ref1; _ref1 = this.hashers; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; h.update(w); } return this; }; CombineBase.prototype.scrub = function() { var h, _i, _len, _ref1; _ref1 = this.hashers; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; h.scrub(); } return this; }; CombineBase.prototype.finalize = function(w) { var h, hashes, out, _i, _len, _ref1; hashes = (function() { var _i, _len, _ref1, _results; _ref1 = this.hashers; _results = []; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; _results.push(h.finalize(w)); } return _results; }).call(this); out = hashes[0]; _ref1 = hashes.slice(1); for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; this._coalesce(out, h); h.scrub(); } return out; }; return CombineBase; })(); Concat = (function(_super) { __extends(Concat, _super); function Concat(key, klasses) { var hm, i, klass, subkey, subkeys; if (klasses == null) { klasses = [SHA512, KECCAK]; } subkeys = key.split(klasses.length); this.hashers = (function() { var _i, _len, _results; _results = []; for (i = _i = 0, _len = klasses.length; _i < _len; i = ++_i) { klass = klasses[i]; subkey = subkeys[i]; hm = new HMAC(subkey, klass); subkey.scrub(); _results.push(hm); } return _results; })(); Concat.__super__.constructor.call(this); } Concat.get_output_size = function() { return SHA512.output_size + KECCAK.output_size; }; Concat.prototype._coalesce = function(out, h) { return out.concat(h); }; Concat.prototype.get_output_size = function() { var h, tot, _i, _len, _ref1; tot = 0; _ref1 = this.hashers; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; tot += h.get_output_size(); } return tot; }; Concat.sign = function(_arg) { var input, key; key = _arg.key, input = _arg.input; return (new Concat(key)).finalize(input); }; Concat.bulk_sign = function(args, cb) { args.klass = Concat; args.what = "HMAC-SHA512-SHA3"; return bulk_sign(args, cb); }; return Concat; })(CombineBase); XOR = (function(_super) { __extends(XOR, _super); function XOR(key, klasses) { var klass; if (klasses == null) { klasses = [SHA512, KECCAK]; } this.hashers = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = klasses.length; _i < _len; _i++) { klass = klasses[_i]; _results.push(new HMAC(key, klass)); } return _results; })(); XOR.__super__.constructor.call(this); } XOR.prototype.reset = function() { var h, i, _i, _len, _ref1; XOR.__super__.reset.call(this); _ref1 = this.hashers; for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) { h = _ref1[i]; h.update(new WordArray([i])); } return this; }; XOR.get_output_size = function() { return Math.max(SHA512.output_size, KECCAK.output_size); }; XOR.prototype._coalesce = function(out, h) { return out.xor(h, {}); }; XOR.prototype.get_output_size = function() { var h; return Math.max.apply(Math, (function() { var _i, _len, _ref1, _results; _ref1 = this.hashers; _results = []; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { h = _ref1[_i]; _results.push(h.get_output_size()); } return _results; }).call(this)); }; XOR.sign = function(_arg) { var input, key; key = _arg.key, input = _arg.input; return (new XOR(key)).finalize(input); }; XOR.bulk_sign = function(arg, cb) { arg.klass = XOR; arg.what = "HMAC-SHA512-XOR-SHA3"; return bulk_sign(arg, cb); }; return XOR; })(CombineBase); exports.Concat = Concat; exports.XOR = XOR; }).call(this); },{"./hmac":8,"./keccak":9,"./sha512":23,"./wordarray":26}],4:[function(require,module,exports){ // Generated by IcedCoffeeScript 108.0.8 (function() { var Cipher, Counter, StreamCipher, WordArray, bulk_encrypt, encrypt, iced, __iced_k, __iced_k_noop, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; iced = require('iced-runtime'); __iced_k = __iced_k_noop = function() {}; WordArray = require('./wordarray').WordArray; StreamCipher = require('./algbase').StreamCipher; Counter = (function() { Counter.prototype.WORD_MAX = 0xffffffff; function Counter(_arg) { var i, len, value; value = _arg.value, len = _arg.len; this._value = value != null ? value.clone() : (len == null ? len = 2 : void 0, new WordArray((function() { var _i, _results; _results = []; for (i = _i = 0; 0 <= len ? _i < len : _i > len; i = 0 <= len ? ++_i : --_i) { _results.push(0); } return _results; })())); } Counter.prototype.inc = function() { var go, i; go = true; i = this._value.words.length - 1; while (go && i >= 0) { if ((++this._value.words[i]) > Counter.WORD_MAX) { this._value.words[i] = 0; } else { go = false; } i--; } return this; }; Counter.prototype.inc_le = function() { var go, i; go = true; i = 0; while (go && i < this._value.words.length) { if ((++this._value.words[i]) > Counter.WORD_MAX) { this._value.words[i] = 0; } else { go = false; } i++; } return this; }; Counter.prototype.get = function() { return this._value; }; Counter.prototype.copy = function() { return this._value.clone(); }; return Counter; })(); Cipher = (function(_super) { __extends(Cipher, _super); function Cipher(_arg) { this.block_cipher = _arg.block_cipher, this.iv = _arg.iv; Cipher.__super__.constructor.call(this); this.bsiw = this.block_cipher.blockSize / 4; if (!(this.iv.sigBytes === this.block_cipher.blockSize)) { throw new Error("IV is wrong length (" + this.iv.sigBytes + ")"); } this.ctr = new Counter({ value: this.iv }); } Cipher.prototype.scrub = function() { return this.block_cipher.scrub(); }; Cipher.prototype.get_pad = function() { var pad; pad = this.ctr.copy(); this.ctr.inc(); this.block_cipher.encryptBlock(pad.words); return pad; }; return Cipher; })(StreamCipher); encrypt = function(_arg) { var block_cipher, cipher, input, iv, ret; block_cipher = _arg.block_cipher, iv = _arg.iv, input = _arg.input; cipher = new Cipher({ block_cipher: block_cipher, iv: iv }); ret = cipher.encrypt(input); cipher.scrub(); return ret; }; bulk_encrypt = function(_arg, cb) { var block_cipher, cipher, input, iv, progress_hook, ret, what, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); block_cipher = _arg.block_cipher, iv = _arg.iv, input = _arg.input, progress_hook = _arg.progress_hook, what = _arg.what; cipher = new Cipher({ block_cipher: block_cipher, iv: iv }); (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/ctr.iced" }); cipher.bulk_encrypt({ input: input, progress_hook: progress_hook, what: what }, __iced_deferrals.defer({ assign_fn: (function() { return function() { return ret = arguments[0]; }; })(), lineno: 121 })); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { return cb(ret); }; })(this)); }; exports.Counter = Counter; exports.Cipher = Cipher; exports.encrypt = encrypt; exports.bulk_encrypt = bulk_encrypt; }).call(this); },{"./algbase":2,"./wordarray":26,"iced-runtime":33}],5:[function(require,module,exports){ // Generated by IcedCoffeeScript 108.0.8 (function() { var AES, Base, Concat, Decryptor, SHA512, Salsa20, TwoFish, V, WordArray, ctr, decrypt, iced, make_esc, salsa20, __iced_k, __iced_k_noop, _ref, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; iced = require('iced-runtime'); __iced_k = __iced_k_noop = function() {}; WordArray = require('./wordarray').WordArray; salsa20 = require('./salsa20'); AES = require('./aes').AES; TwoFish = require('./twofish').TwoFish; ctr = require('./ctr'); Concat = require('./combine').Concat; SHA512 = require('./sha512').SHA512; Salsa20 = require('./salsa20').Salsa20; _ref = require('./enc'), Base = _ref.Base, V = _ref.V; make_esc = require('iced-error').make_esc; Decryptor = (function(_super) { __extends(Decryptor, _super); function Decryptor(_arg) { var enc, key; key = _arg.key, enc = _arg.enc; Decryptor.__super__.constructor.call(this, { key: key }); if (enc != null) { this.key = enc.key; this.derived_keys = enc.derived_keys; } } Decryptor.prototype.read_header = function(cb) { var err, wa; err = (wa = this.ct.unshift(2)) == null ? new Error("Ciphertext underrun in header") : (this.version = V[wa.words[1]]) == null ? new Error("bad header; couldn't find a good version (got " + wa.words[1] + ")") : wa.words[0] !== this.version.header[0] ? new Error("Bad header: unrecognized magic value") : null; return cb(err); }; Decryptor.prototype.verify_sig = function(key, cb) { var computed, err, received, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); (function(_this) { return (function(__iced_k) { if ((received = _this.ct.unshift(Concat.get_output_size() / 4)) == null) { return __iced_k(err = new Error("Ciphertext underrun in signature")); } else { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.verify_sig" }); _this.sign({ input: _this.ct, key: key, salt: _this.salt }, __iced_deferrals.defer({ assign_fn: (function() { return function() { err = arguments[0]; return computed = arguments[1]; }; })(), lineno: 63 })); __iced_deferrals._fulfill(); })(function() { return __iced_k(err = err != null ? err : received.equal(computed) ? null : new Error('Signature mismatch or bad decryption key')); }); } }); })(this)((function(_this) { return function() { return cb(err); }; })(this)); }; Decryptor.prototype.unshift_iv = function(n_bytes, which, cb) { var err, iv; err = (iv = this.ct.unshift(n_bytes / 4)) != null ? null : new Error("Ciphertext underrun in " + which); return cb(err, iv); }; Decryptor.prototype.read_salt = function(cb) { var err; err = (this.salt = this.ct.unshift(this.version.salt_size / 4)) == null ? new Error("Ciphertext underrrun in read_salt") : null; return cb(err); }; Decryptor.prototype.generate_keys = function(_arg, cb) { var err, keys, progress_hook, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); progress_hook = _arg.progress_hook; (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.generate_keys" }); _this.kdf({ salt: _this.salt, progress_hook: progress_hook }, __iced_deferrals.defer({ assign_fn: (function() { return function() { err = arguments[0]; return keys = arguments[1]; }; })(), lineno: 114 })); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { return cb(err, keys); }; })(this)); }; Decryptor.prototype.run = function(_arg, cb) { var data, esc, iv, progress_hook, pt, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); data = _arg.data, progress_hook = _arg.progress_hook; esc = make_esc(cb, "Decryptor::run"); this.ct = WordArray.from_buffer(data); (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.read_header(esc(__iced_deferrals.defer({ lineno: 141 }))); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.read_salt(esc(__iced_deferrals.defer({ lineno: 142 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.generate_keys({ progress_hook: progress_hook }, esc(__iced_deferrals.defer({ assign_fn: (function(__slot_1) { return function() { return __slot_1.keys = arguments[0]; }; })(_this), lineno: 143 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.verify_sig(_this.keys.hmac, esc(__iced_deferrals.defer({ lineno: 144 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.unshift_iv(AES.ivSize, "AES", esc(__iced_deferrals.defer({ assign_fn: (function() { return function() { return iv = arguments[0]; }; })(), lineno: 145 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.run_aes({ iv: iv, input: _this.ct, key: _this.keys.aes, progress_hook: progress_hook }, esc(__iced_deferrals.defer({ assign_fn: (function() { return function() { return __iced_deferrals.ret = arguments[0]; }; })(), lineno: 146 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { if (_this.version.use_twofish) { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.unshift_iv(TwoFish.ivSize, "2fish", esc(__iced_deferrals.defer({ assign_fn: (function() { return function() { return iv = arguments[0]; }; })(), lineno: 148 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.run_twofish({ iv: iv, input: _this.ct, key: _this.keys.twofish, progress_hook: progress_hook }, esc(__iced_deferrals.defer({ assign_fn: (function() { return function() { return __iced_deferrals.ret = arguments[0]; }; })(), lineno: 149 }))); __iced_deferrals._fulfill(); })(__iced_k); }); } else { return __iced_k(); } })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.unshift_iv(Salsa20.ivSize, "Salsa", esc(__iced_deferrals.defer({ assign_fn: (function() { return function() { return iv = arguments[0]; }; })(), lineno: 150 }))); __iced_deferrals._fulfill(); })(function() { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced", funcname: "Decryptor.run" }); _this.run_salsa20({ iv: iv, input: _this.ct, key: _this.keys.salsa20, output_iv: false, progress_hook: progress_hook }, esc(__iced_deferrals.defer({ assign_fn: (function() { return function() { return pt = arguments[0]; }; })(), lineno: 151 }))); __iced_deferrals._fulfill(); })(function() { return cb(null, pt.to_buffer()); }); }); }); }); }); }); }); }); }; })(this)); }; Decryptor.prototype.clone = function() { var ret, _ref1; ret = new Decryptor({ key: (_ref1 = this.key) != null ? _ref1.to_buffer() : void 0, rng: this.rng, version: this.version }); ret.derived_keys = this.clone_derived_keys(); return ret; }; return Decryptor; })(Base); decrypt = function(_arg, cb) { var data, dec, err, key, progress_hook, pt, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); key = _arg.key, data = _arg.data, progress_hook = _arg.progress_hook; dec = new Decryptor({ key: key }); (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/dec.iced" }); dec.run({ data: data, progress_hook: progress_hook }, __iced_deferrals.defer({ assign_fn: (function() { return function() { err = arguments[0]; return pt = arguments[1]; }; })(), lineno: 181 })); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { dec.scrub(); return cb(err, pt); }; })(this)); }; exports.Decryptor = Decryptor; exports.decrypt = decrypt; }).call(this); },{"./aes":1,"./combine":3,"./ctr":4,"./enc":7,"./salsa20":15,"./sha512":23,"./twofish":24,"./wordarray":26,"iced-error":29,"iced-runtime":33}],6:[function(require,module,exports){ (function (Buffer){ // Generated by IcedCoffeeScript 108.0.8 (function() { var ADRBG, DRBG, Lock, WordArray, hmac, iced, __iced_k, __iced_k_noop; iced = require('iced-runtime'); __iced_k = __iced_k_noop = function() {}; hmac = require('./hmac'); WordArray = require('./wordarray').WordArray; Lock = require('iced-lock').Lock; DRBG = (function() { function DRBG(entropy, personalization_string, hmac_func) { this.hmac = hmac_func || hmac.sign; this.security_strength = 256; entropy = this.check_entropy(entropy); personalization_string || (personalization_string = new WordArray([])); this._instantiate(entropy, personalization_string); } DRBG.prototype.check_entropy = function(entropy, reseed) { if (reseed == null) { reseed = false; } if ((entropy.sigBytes * 8 * 2) < ((reseed ? 2 : 3) * this.security_strength)) { throw new Error("entropy must be at least " + (1.5 * this.security_strength) + " bits."); } return entropy; }; DRBG.prototype._hmac = function(key, input) { return this.hmac({ key: key, input: input }); }; DRBG.prototype._update = function(provided_data) { var V, V_in; V = new WordArray([0], 1); if (provided_data != null) { V = V.concat(provided_data); } V_in = this.V.clone().concat(V); this.K = this._hmac(this.K, V_in); V_in.scrub(); V.scrub(); this.V = this._hmac(this.K, this.V); if (provided_data != null) { V_in = this.V.clone().concat(new WordArray([1 << 24], 1)).concat(provided_data); this.K = this._hmac(this.K, V_in); V_in.scrub(); this.V = this._hmac(this.K, this.V); } return provided_data != null ? provided_data.scrub() : void 0; }; DRBG.prototype._instantiate = function(entropy, personalization_string) { var i, n, seed_material; seed_material = entropy.concat(personalization_string); n = 64; this.K = WordArray.from_buffer(Buffer.from((function() { var _i, _results; _results = []; for (i = _i = 0; 0 <= n ? _i < n : _i > n; i = 0 <= n ? ++_i : --_i) { _results.push(0); } return _results; })())); this.V = WordArray.from_buffer(Buffer.from((function() { var _i, _results; _results = []; for (i = _i = 0; 0 <= n ? _i < n : _i > n; i = 0 <= n ? ++_i : --_i) { _results.push(1); } return _results; })())); this._update(seed_material); entropy.scrub(); return this.reseed_counter = 1; }; DRBG.prototype.reseed = function(entropy) { this._update(this.check_entropy(entropy, true)); return this.reseed_counter = 1; }; DRBG.prototype.generate = function(num_bytes) { var i, tmp, _ref; if ((num_bytes * 8) > 7500) { throw new Error("generate cannot generate > 7500 bits in 1 call."); } if (this.reseed_counter >= 10000) { throw new Error("Need a reseed!"); } tmp = []; i = 0; while ((tmp.length === 0) || (tmp.length * tmp[0].length * 4) < num_bytes) { this.V = this._hmac(this.K, this.V); tmp.push(this.V.words); } this._update(); this.reseed_counter += 1; return (new WordArray((_ref = []).concat.apply(_ref, tmp))).truncate(num_bytes); }; return DRBG; })(); ADRBG = (function() { function ADRBG(gen_seed, hmac) { this.gen_seed = gen_seed; this.hmac = hmac; this.drbg = null; this.lock = new Lock(); } ADRBG.prototype.generate = function(n, cb) { var ret, seed, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/drbg.iced", funcname: "ADRBG.generate" }); _this.lock.acquire(__iced_deferrals.defer({ lineno: 145 })); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { (function(__iced_k) { if (_this.drbg == null) { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/drbg.iced", funcname: "ADRBG.generate" }); _this.gen_seed(256, __iced_deferrals.defer({ assign_fn: (function() { return function() { return seed = arguments[0]; }; })(), lineno: 147 })); __iced_deferrals._fulfill(); })(function() { return __iced_k(_this.drbg = new DRBG(seed, null, _this.hmac)); }); } else { return __iced_k(); } })(function() { (function(__iced_k) { if (_this.drbg.reseed_counter > 100) { (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/drbg.iced", funcname: "ADRBG.generate" }); _this.gen_seed(256, __iced_deferrals.defer({ assign_fn: (function() { return function() { return seed = arguments[0]; }; })(), lineno: 150 })); __iced_deferrals._fulfill(); })(function() { return __iced_k(_this.drbg.reseed(seed)); }); } else { return __iced_k(); } })(function() { ret = _this.drbg.generate(n); _this.lock.release(); return cb(ret); }); }); }; })(this)); }; return ADRBG; })(); exports.DRBG = DRBG; exports.ADRBG = ADRBG; }).call(this); }).call(this,require("buffer").Buffer) },{"./hmac":8,"./wordarray":26,"buffer":27,"iced-lock":30,"iced-runtime":33}],7:[function(require,module,exports){ // Generated by IcedCoffeeScript 108.0.8 (function() { var AES, Base, CURRENT_VERSION, Concat, Encryptor, HMAC_SHA256, KECCAK, PBKDF2, SHA3STD, SHA512, Scrypt, TwoFish, V, WordArray, XOR, ctr, encrypt, iced, make_esc, prng, salsa20, util, __iced_k, __iced_k_noop, _ref, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; iced = require('iced-runtime'); __iced_k = __iced_k_noop = function() {}; WordArray = require('./wordarray').WordArray; salsa20 = require('./salsa20'); AES = require('./aes').AES; TwoFish = require('./twofish').TwoFish; ctr = require('./ctr'); _ref = require('./combine'), XOR = _ref.XOR, Concat = _ref.Concat; SHA512 = require('./sha512').SHA512; SHA3STD = require('./sha3std').SHA3STD; KECCAK = require('./keccak').KECCAK; PBKDF2 = require('./pbkdf2').PBKDF2; Scrypt = require('./scrypt').Scrypt; util = require('./util'); prng = require('./prng'); make_esc = require('iced-error').make_esc; HMAC_SHA256 = require('./hmac').HMAC_SHA256; V = { "1": { header: [0x1c94d7de, 1], salt_size: 8, xsalsa20_rev: true, kdf: { klass: PBKDF2, opts: { c: 1024, klass: XOR } }, use_twofish: true, hmac_hashes: [SHA512, KECCAK], hmac_key_size: 768 / 8, version: 1 }, "2": { header: [0x1c94d7de, 2], salt_size: 16, xsalsa20_rev: true, kdf: { klass: Scrypt, opts: { c: 64, klass: XOR, N: 12, r: 8, p: 1 } }, use_twofish: true, hmac_hashes: [SHA512, KECCAK], hmac_key_size: 768 / 8, version: 2 }, "3": { header: [0x1c94d7de, 3], salt_size: 16, xsalsa20_rev: false, kdf: { klass: Scrypt, opts: { c: 1, klass: HMAC_SHA256, N: 15, r: 8, p: 1 } }, use_twofish: true, hmac_hashes: [SHA512, KECCAK], hmac_key_size: 768 / 8, version: 3 }, "4": { header: [0x1c94d7de, 4], salt_size: 16, xsalsa20_rev: false, kdf: { klass: Scrypt, opts: { c: 1, klass: HMAC_SHA256, N: 15, r: 8, p: 1 } }, hmac_key_size: 768 / 8, use_twofish: false, hmac_hashes: [SHA512, SHA3STD], version: 4 } }; exports.CURRENT_VERSION = CURRENT_VERSION = 4; Base = (function() { function Base(_arg) { var key, version; key = _arg.key, version = _arg.version; this.version = V[version != null ? version : CURRENT_VERSION]; if (this.version == null) { throw new Error("unknown version: " + version); } this.set_key(key); this.derived_keys = {}; } Base.prototype.kdf = function(_arg, cb) { var args, dkLen, end, extra_keymaterial, i, k, key, keys, len, lens, order, progress_hook, raw, salt, salt_hex, v, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); salt = _arg.salt, extra_keymaterial = _arg.extra_keymaterial, progress_hook = _arg.progress_hook; (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/enc.iced", funcname: "Base.kdf" }); _this._check_scrubbed(_this.key, "in KDF", cb, __iced_deferrals.defer({ lineno: 121 })); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { salt_hex = salt.to_hex(); key = _this.key.clone(); (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/Users/max/src/keybase/triplesec/src/enc.iced", funcname: "Base.kdf" }); _this._check_scrubbed(key, "KDF", cb, __iced_deferr