bundle.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /*
  3. * pkcs7.pad
  4. * https://github.com/brightcove/pkcs7
  5. *
  6. * Copyright (c) 2014 Brightcove
  7. * Licensed under the apache2 license.
  8. */
  9. 'use strict';
  10. var PADDING;
  11. /**
  12. * Returns a new Uint8Array that is padded with PKCS#7 padding.
  13. * @param plaintext {Uint8Array} the input bytes before encryption
  14. * @return {Uint8Array} the padded bytes
  15. * @see http://tools.ietf.org/html/rfc5652
  16. */
  17. module.exports = function pad(plaintext) {
  18. var padding = PADDING[(plaintext.byteLength % 16) || 0],
  19. result = new Uint8Array(plaintext.byteLength + padding.length);
  20. result.set(plaintext);
  21. result.set(padding, plaintext.byteLength);
  22. return result;
  23. };
  24. // pre-define the padding values
  25. PADDING = [
  26. [16, 16, 16, 16,
  27. 16, 16, 16, 16,
  28. 16, 16, 16, 16,
  29. 16, 16, 16, 16],
  30. [15, 15, 15, 15,
  31. 15, 15, 15, 15,
  32. 15, 15, 15, 15,
  33. 15, 15, 15],
  34. [14, 14, 14, 14,
  35. 14, 14, 14, 14,
  36. 14, 14, 14, 14,
  37. 14, 14],
  38. [13, 13, 13, 13,
  39. 13, 13, 13, 13,
  40. 13, 13, 13, 13,
  41. 13],
  42. [12, 12, 12, 12,
  43. 12, 12, 12, 12,
  44. 12, 12, 12, 12],
  45. [11, 11, 11, 11,
  46. 11, 11, 11, 11,
  47. 11, 11, 11],
  48. [10, 10, 10, 10,
  49. 10, 10, 10, 10,
  50. 10, 10],
  51. [9, 9, 9, 9,
  52. 9, 9, 9, 9,
  53. 9],
  54. [8, 8, 8, 8,
  55. 8, 8, 8, 8],
  56. [7, 7, 7, 7,
  57. 7, 7, 7],
  58. [6, 6, 6, 6,
  59. 6, 6],
  60. [5, 5, 5, 5,
  61. 5],
  62. [4, 4, 4, 4],
  63. [3, 3, 3],
  64. [2, 2],
  65. [1]
  66. ];
  67. },{}],2:[function(require,module,exports){
  68. /*
  69. * pkcs7
  70. * https://github.com/brightcove/pkcs7
  71. *
  72. * Copyright (c) 2014 Brightcove
  73. * Licensed under the apache2 license.
  74. */
  75. 'use strict';
  76. exports.pad = require('./pad.js');
  77. exports.unpad = require('./unpad.js');
  78. },{"./pad.js":1,"./unpad.js":3}],3:[function(require,module,exports){
  79. /*
  80. * pkcs7.unpad
  81. * https://github.com/brightcove/pkcs7
  82. *
  83. * Copyright (c) 2014 Brightcove
  84. * Licensed under the apache2 license.
  85. */
  86. 'use strict';
  87. /**
  88. * Returns the subarray of a Uint8Array without PKCS#7 padding.
  89. * @param padded {Uint8Array} unencrypted bytes that have been padded
  90. * @return {Uint8Array} the unpadded bytes
  91. * @see http://tools.ietf.org/html/rfc5652
  92. */
  93. module.exports = function unpad(padded) {
  94. return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
  95. };
  96. },{}],4:[function(require,module,exports){
  97. /**
  98. * @file aes.js
  99. *
  100. * This file contains an adaptation of the AES decryption algorithm
  101. * from the Standford Javascript Cryptography Library. That work is
  102. * covered by the following copyright and permissions notice:
  103. *
  104. * Copyright 2009-2010 Emily Stark, Mike Hamburg, Dan Boneh.
  105. * All rights reserved.
  106. *
  107. * Redistribution and use in source and binary forms, with or without
  108. * modification, are permitted provided that the following conditions are
  109. * met:
  110. *
  111. * 1. Redistributions of source code must retain the above copyright
  112. * notice, this list of conditions and the following disclaimer.
  113. *
  114. * 2. Redistributions in binary form must reproduce the above
  115. * copyright notice, this list of conditions and the following
  116. * disclaimer in the documentation and/or other materials provided
  117. * with the distribution.
  118. *
  119. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  120. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  121. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  122. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE
  123. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  124. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  125. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  126. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  127. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  128. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  129. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  130. *
  131. * The views and conclusions contained in the software and documentation
  132. * are those of the authors and should not be interpreted as representing
  133. * official policies, either expressed or implied, of the authors.
  134. */
  135. /**
  136. * Expand the S-box tables.
  137. *
  138. * @private
  139. */
  140. 'use strict';
  141. Object.defineProperty(exports, '__esModule', {
  142. value: true
  143. });
  144. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  145. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  146. var precompute = function precompute() {
  147. var tables = [[[], [], [], [], []], [[], [], [], [], []]];
  148. var encTable = tables[0];
  149. var decTable = tables[1];
  150. var sbox = encTable[4];
  151. var sboxInv = decTable[4];
  152. var i = undefined;
  153. var x = undefined;
  154. var xInv = undefined;
  155. var d = [];
  156. var th = [];
  157. var x2 = undefined;
  158. var x4 = undefined;
  159. var x8 = undefined;
  160. var s = undefined;
  161. var tEnc = undefined;
  162. var tDec = undefined;
  163. // Compute double and third tables
  164. for (i = 0; i < 256; i++) {
  165. th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
  166. }
  167. for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
  168. // Compute sbox
  169. s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
  170. s = s >> 8 ^ s & 255 ^ 99;
  171. sbox[x] = s;
  172. sboxInv[s] = x;
  173. // Compute MixColumns
  174. x8 = d[x4 = d[x2 = d[x]]];
  175. tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
  176. tEnc = d[s] * 0x101 ^ s * 0x1010100;
  177. for (i = 0; i < 4; i++) {
  178. encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
  179. decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
  180. }
  181. }
  182. // Compactify. Considerable speedup on Firefox.
  183. for (i = 0; i < 5; i++) {
  184. encTable[i] = encTable[i].slice(0);
  185. decTable[i] = decTable[i].slice(0);
  186. }
  187. return tables;
  188. };
  189. var aesTables = null;
  190. /**
  191. * Schedule out an AES key for both encryption and decryption. This
  192. * is a low-level class. Use a cipher mode to do bulk encryption.
  193. *
  194. * @class AES
  195. * @param key {Array} The key as an array of 4, 6 or 8 words.
  196. */
  197. var AES = (function () {
  198. function AES(key) {
  199. _classCallCheck(this, AES);
  200. /**
  201. * The expanded S-box and inverse S-box tables. These will be computed
  202. * on the client so that we don't have to send them down the wire.
  203. *
  204. * There are two tables, _tables[0] is for encryption and
  205. * _tables[1] is for decryption.
  206. *
  207. * The first 4 sub-tables are the expanded S-box with MixColumns. The
  208. * last (_tables[01][4]) is the S-box itself.
  209. *
  210. * @private
  211. */
  212. // if we have yet to precompute the S-box tables
  213. // do so now
  214. if (!aesTables) {
  215. aesTables = precompute();
  216. }
  217. // then make a copy of that object for use
  218. this._tables = [[aesTables[0][0].slice(), aesTables[0][1].slice(), aesTables[0][2].slice(), aesTables[0][3].slice(), aesTables[0][4].slice()], [aesTables[1][0].slice(), aesTables[1][1].slice(), aesTables[1][2].slice(), aesTables[1][3].slice(), aesTables[1][4].slice()]];
  219. var i = undefined;
  220. var j = undefined;
  221. var tmp = undefined;
  222. var encKey = undefined;
  223. var decKey = undefined;
  224. var sbox = this._tables[0][4];
  225. var decTable = this._tables[1];
  226. var keyLen = key.length;
  227. var rcon = 1;
  228. if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
  229. throw new Error('Invalid aes key size');
  230. }
  231. encKey = key.slice(0);
  232. decKey = [];
  233. this._key = [encKey, decKey];
  234. // schedule encryption keys
  235. for (i = keyLen; i < 4 * keyLen + 28; i++) {
  236. tmp = encKey[i - 1];
  237. // apply sbox
  238. if (i % keyLen === 0 || keyLen === 8 && i % keyLen === 4) {
  239. tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
  240. // shift rows and add rcon
  241. if (i % keyLen === 0) {
  242. tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
  243. rcon = rcon << 1 ^ (rcon >> 7) * 283;
  244. }
  245. }
  246. encKey[i] = encKey[i - keyLen] ^ tmp;
  247. }
  248. // schedule decryption keys
  249. for (j = 0; i; j++, i--) {
  250. tmp = encKey[j & 3 ? i : i - 4];
  251. if (i <= 4 || j < 4) {
  252. decKey[j] = tmp;
  253. } else {
  254. decKey[j] = decTable[0][sbox[tmp >>> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]];
  255. }
  256. }
  257. }
  258. /**
  259. * Decrypt 16 bytes, specified as four 32-bit words.
  260. *
  261. * @param {Number} encrypted0 the first word to decrypt
  262. * @param {Number} encrypted1 the second word to decrypt
  263. * @param {Number} encrypted2 the third word to decrypt
  264. * @param {Number} encrypted3 the fourth word to decrypt
  265. * @param {Int32Array} out the array to write the decrypted words
  266. * into
  267. * @param {Number} offset the offset into the output array to start
  268. * writing results
  269. * @return {Array} The plaintext.
  270. */
  271. _createClass(AES, [{
  272. key: 'decrypt',
  273. value: function decrypt(encrypted0, encrypted1, encrypted2, encrypted3, out, offset) {
  274. var key = this._key[1];
  275. // state variables a,b,c,d are loaded with pre-whitened data
  276. var a = encrypted0 ^ key[0];
  277. var b = encrypted3 ^ key[1];
  278. var c = encrypted2 ^ key[2];
  279. var d = encrypted1 ^ key[3];
  280. var a2 = undefined;
  281. var b2 = undefined;
  282. var c2 = undefined;
  283. // key.length === 2 ?
  284. var nInnerRounds = key.length / 4 - 2;
  285. var i = undefined;
  286. var kIndex = 4;
  287. var table = this._tables[1];
  288. // load up the tables
  289. var table0 = table[0];
  290. var table1 = table[1];
  291. var table2 = table[2];
  292. var table3 = table[3];
  293. var sbox = table[4];
  294. // Inner rounds. Cribbed from OpenSSL.
  295. for (i = 0; i < nInnerRounds; i++) {
  296. a2 = table0[a >>> 24] ^ table1[b >> 16 & 255] ^ table2[c >> 8 & 255] ^ table3[d & 255] ^ key[kIndex];
  297. b2 = table0[b >>> 24] ^ table1[c >> 16 & 255] ^ table2[d >> 8 & 255] ^ table3[a & 255] ^ key[kIndex + 1];
  298. c2 = table0[c >>> 24] ^ table1[d >> 16 & 255] ^ table2[a >> 8 & 255] ^ table3[b & 255] ^ key[kIndex + 2];
  299. d = table0[d >>> 24] ^ table1[a >> 16 & 255] ^ table2[b >> 8 & 255] ^ table3[c & 255] ^ key[kIndex + 3];
  300. kIndex += 4;
  301. a = a2;b = b2;c = c2;
  302. }
  303. // Last round.
  304. for (i = 0; i < 4; i++) {
  305. out[(3 & -i) + offset] = sbox[a >>> 24] << 24 ^ sbox[b >> 16 & 255] << 16 ^ sbox[c >> 8 & 255] << 8 ^ sbox[d & 255] ^ key[kIndex++];
  306. a2 = a;a = b;b = c;c = d;d = a2;
  307. }
  308. }
  309. }]);
  310. return AES;
  311. })();
  312. exports['default'] = AES;
  313. module.exports = exports['default'];
  314. },{}],5:[function(require,module,exports){
  315. /**
  316. * @file async-stream.js
  317. */
  318. 'use strict';
  319. Object.defineProperty(exports, '__esModule', {
  320. value: true
  321. });
  322. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  323. var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  324. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  325. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  326. function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  327. var _stream = require('./stream');
  328. var _stream2 = _interopRequireDefault(_stream);
  329. /**
  330. * A wrapper around the Stream class to use setTiemout
  331. * and run stream "jobs" Asynchronously
  332. *
  333. * @class AsyncStream
  334. * @extends Stream
  335. */
  336. var AsyncStream = (function (_Stream) {
  337. _inherits(AsyncStream, _Stream);
  338. function AsyncStream() {
  339. _classCallCheck(this, AsyncStream);
  340. _get(Object.getPrototypeOf(AsyncStream.prototype), 'constructor', this).call(this, _stream2['default']);
  341. this.jobs = [];
  342. this.delay = 1;
  343. this.timeout_ = null;
  344. }
  345. /**
  346. * process an async job
  347. *
  348. * @private
  349. */
  350. _createClass(AsyncStream, [{
  351. key: 'processJob_',
  352. value: function processJob_() {
  353. this.jobs.shift()();
  354. if (this.jobs.length) {
  355. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  356. } else {
  357. this.timeout_ = null;
  358. }
  359. }
  360. /**
  361. * push a job into the stream
  362. *
  363. * @param {Function} job the job to push into the stream
  364. */
  365. }, {
  366. key: 'push',
  367. value: function push(job) {
  368. this.jobs.push(job);
  369. if (!this.timeout_) {
  370. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  371. }
  372. }
  373. }]);
  374. return AsyncStream;
  375. })(_stream2['default']);
  376. exports['default'] = AsyncStream;
  377. module.exports = exports['default'];
  378. },{"./stream":8}],6:[function(require,module,exports){
  379. /**
  380. * @file decrypter.js
  381. *
  382. * An asynchronous implementation of AES-128 CBC decryption with
  383. * PKCS#7 padding.
  384. */
  385. 'use strict';
  386. Object.defineProperty(exports, '__esModule', {
  387. value: true
  388. });
  389. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  390. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  391. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  392. var _aes = require('./aes');
  393. var _aes2 = _interopRequireDefault(_aes);
  394. var _asyncStream = require('./async-stream');
  395. var _asyncStream2 = _interopRequireDefault(_asyncStream);
  396. var _pkcs7 = require('pkcs7');
  397. /**
  398. * Convert network-order (big-endian) bytes into their little-endian
  399. * representation.
  400. */
  401. var ntoh = function ntoh(word) {
  402. return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
  403. };
  404. /**
  405. * Decrypt bytes using AES-128 with CBC and PKCS#7 padding.
  406. *
  407. * @param {Uint8Array} encrypted the encrypted bytes
  408. * @param {Uint32Array} key the bytes of the decryption key
  409. * @param {Uint32Array} initVector the initialization vector (IV) to
  410. * use for the first round of CBC.
  411. * @return {Uint8Array} the decrypted bytes
  412. *
  413. * @see http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  414. * @see http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_.28CBC.29
  415. * @see https://tools.ietf.org/html/rfc2315
  416. */
  417. var decrypt = function decrypt(encrypted, key, initVector) {
  418. // word-level access to the encrypted bytes
  419. var encrypted32 = new Int32Array(encrypted.buffer, encrypted.byteOffset, encrypted.byteLength >> 2);
  420. var decipher = new _aes2['default'](Array.prototype.slice.call(key));
  421. // byte and word-level access for the decrypted output
  422. var decrypted = new Uint8Array(encrypted.byteLength);
  423. var decrypted32 = new Int32Array(decrypted.buffer);
  424. // temporary variables for working with the IV, encrypted, and
  425. // decrypted data
  426. var init0 = undefined;
  427. var init1 = undefined;
  428. var init2 = undefined;
  429. var init3 = undefined;
  430. var encrypted0 = undefined;
  431. var encrypted1 = undefined;
  432. var encrypted2 = undefined;
  433. var encrypted3 = undefined;
  434. // iteration variable
  435. var wordIx = undefined;
  436. // pull out the words of the IV to ensure we don't modify the
  437. // passed-in reference and easier access
  438. init0 = initVector[0];
  439. init1 = initVector[1];
  440. init2 = initVector[2];
  441. init3 = initVector[3];
  442. // decrypt four word sequences, applying cipher-block chaining (CBC)
  443. // to each decrypted block
  444. for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
  445. // convert big-endian (network order) words into little-endian
  446. // (javascript order)
  447. encrypted0 = ntoh(encrypted32[wordIx]);
  448. encrypted1 = ntoh(encrypted32[wordIx + 1]);
  449. encrypted2 = ntoh(encrypted32[wordIx + 2]);
  450. encrypted3 = ntoh(encrypted32[wordIx + 3]);
  451. // decrypt the block
  452. decipher.decrypt(encrypted0, encrypted1, encrypted2, encrypted3, decrypted32, wordIx);
  453. // XOR with the IV, and restore network byte-order to obtain the
  454. // plaintext
  455. decrypted32[wordIx] = ntoh(decrypted32[wordIx] ^ init0);
  456. decrypted32[wordIx + 1] = ntoh(decrypted32[wordIx + 1] ^ init1);
  457. decrypted32[wordIx + 2] = ntoh(decrypted32[wordIx + 2] ^ init2);
  458. decrypted32[wordIx + 3] = ntoh(decrypted32[wordIx + 3] ^ init3);
  459. // setup the IV for the next round
  460. init0 = encrypted0;
  461. init1 = encrypted1;
  462. init2 = encrypted2;
  463. init3 = encrypted3;
  464. }
  465. return decrypted;
  466. };
  467. exports.decrypt = decrypt;
  468. /**
  469. * The `Decrypter` class that manages decryption of AES
  470. * data through `AsyncStream` objects and the `decrypt`
  471. * function
  472. *
  473. * @param {Uint8Array} encrypted the encrypted bytes
  474. * @param {Uint32Array} key the bytes of the decryption key
  475. * @param {Uint32Array} initVector the initialization vector (IV) to
  476. * @param {Function} done the function to run when done
  477. * @class Decrypter
  478. */
  479. var Decrypter = (function () {
  480. function Decrypter(encrypted, key, initVector, done) {
  481. _classCallCheck(this, Decrypter);
  482. var step = Decrypter.STEP;
  483. var encrypted32 = new Int32Array(encrypted.buffer);
  484. var decrypted = new Uint8Array(encrypted.byteLength);
  485. var i = 0;
  486. this.asyncStream_ = new _asyncStream2['default']();
  487. // split up the encryption job and do the individual chunks asynchronously
  488. this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
  489. for (i = step; i < encrypted32.length; i += step) {
  490. initVector = new Uint32Array([ntoh(encrypted32[i - 4]), ntoh(encrypted32[i - 3]), ntoh(encrypted32[i - 2]), ntoh(encrypted32[i - 1])]);
  491. this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
  492. }
  493. // invoke the done() callback when everything is finished
  494. this.asyncStream_.push(function () {
  495. // remove pkcs#7 padding from the decrypted bytes
  496. done(null, (0, _pkcs7.unpad)(decrypted));
  497. });
  498. }
  499. /**
  500. * a getter for step the maximum number of bytes to process at one time
  501. *
  502. * @return {Number} the value of step 32000
  503. */
  504. _createClass(Decrypter, [{
  505. key: 'decryptChunk_',
  506. /**
  507. * @private
  508. */
  509. value: function decryptChunk_(encrypted, key, initVector, decrypted) {
  510. return function () {
  511. var bytes = decrypt(encrypted, key, initVector);
  512. decrypted.set(bytes, encrypted.byteOffset);
  513. };
  514. }
  515. }], [{
  516. key: 'STEP',
  517. get: function get() {
  518. // 4 * 8000;
  519. return 32000;
  520. }
  521. }]);
  522. return Decrypter;
  523. })();
  524. exports.Decrypter = Decrypter;
  525. exports['default'] = {
  526. Decrypter: Decrypter,
  527. decrypt: decrypt
  528. };
  529. },{"./aes":4,"./async-stream":5,"pkcs7":2}],7:[function(require,module,exports){
  530. /**
  531. * @file index.js
  532. *
  533. * Index module to easily import the primary components of AES-128
  534. * decryption. Like this:
  535. *
  536. * ```js
  537. * import {Decrypter, decrypt, AsyncStream} from 'aes-decrypter';
  538. * ```
  539. */
  540. 'use strict';
  541. Object.defineProperty(exports, '__esModule', {
  542. value: true
  543. });
  544. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  545. var _decrypter = require('./decrypter');
  546. var _asyncStream = require('./async-stream');
  547. var _asyncStream2 = _interopRequireDefault(_asyncStream);
  548. exports['default'] = {
  549. decrypt: _decrypter.decrypt,
  550. Decrypter: _decrypter.Decrypter,
  551. AsyncStream: _asyncStream2['default']
  552. };
  553. module.exports = exports['default'];
  554. },{"./async-stream":5,"./decrypter":6}],8:[function(require,module,exports){
  555. /**
  556. * @file stream.js
  557. */
  558. /**
  559. * A lightweight readable stream implemention that handles event dispatching.
  560. *
  561. * @class Stream
  562. */
  563. 'use strict';
  564. Object.defineProperty(exports, '__esModule', {
  565. value: true
  566. });
  567. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  568. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  569. var Stream = (function () {
  570. function Stream() {
  571. _classCallCheck(this, Stream);
  572. this.listeners = {};
  573. }
  574. /**
  575. * Add a listener for a specified event type.
  576. *
  577. * @param {String} type the event name
  578. * @param {Function} listener the callback to be invoked when an event of
  579. * the specified type occurs
  580. */
  581. _createClass(Stream, [{
  582. key: 'on',
  583. value: function on(type, listener) {
  584. if (!this.listeners[type]) {
  585. this.listeners[type] = [];
  586. }
  587. this.listeners[type].push(listener);
  588. }
  589. /**
  590. * Remove a listener for a specified event type.
  591. *
  592. * @param {String} type the event name
  593. * @param {Function} listener a function previously registered for this
  594. * type of event through `on`
  595. * @return {Boolean} if we could turn it off or not
  596. */
  597. }, {
  598. key: 'off',
  599. value: function off(type, listener) {
  600. var index = undefined;
  601. if (!this.listeners[type]) {
  602. return false;
  603. }
  604. index = this.listeners[type].indexOf(listener);
  605. this.listeners[type].splice(index, 1);
  606. return index > -1;
  607. }
  608. /**
  609. * Trigger an event of the specified type on this stream. Any additional
  610. * arguments to this function are passed as parameters to event listeners.
  611. *
  612. * @param {String} type the event name
  613. */
  614. }, {
  615. key: 'trigger',
  616. value: function trigger(type) {
  617. var callbacks = undefined;
  618. var i = undefined;
  619. var length = undefined;
  620. var args = undefined;
  621. callbacks = this.listeners[type];
  622. if (!callbacks) {
  623. return;
  624. }
  625. // Slicing the arguments on every invocation of this method
  626. // can add a significant amount of overhead. Avoid the
  627. // intermediate object creation for the common case of a
  628. // single callback argument
  629. if (arguments.length === 2) {
  630. length = callbacks.length;
  631. for (i = 0; i < length; ++i) {
  632. callbacks[i].call(this, arguments[1]);
  633. }
  634. } else {
  635. args = Array.prototype.slice.call(arguments, 1);
  636. length = callbacks.length;
  637. for (i = 0; i < length; ++i) {
  638. callbacks[i].apply(this, args);
  639. }
  640. }
  641. }
  642. /**
  643. * Destroys the stream and cleans up.
  644. */
  645. }, {
  646. key: 'dispose',
  647. value: function dispose() {
  648. this.listeners = {};
  649. }
  650. /**
  651. * Forwards all `data` events on this stream to the destination stream. The
  652. * destination stream should provide a method `push` to receive the data
  653. * events as they arrive.
  654. *
  655. * @param {Stream} destination the stream that will receive all `data` events
  656. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  657. */
  658. }, {
  659. key: 'pipe',
  660. value: function pipe(destination) {
  661. this.on('data', function (data) {
  662. destination.push(data);
  663. });
  664. }
  665. }]);
  666. return Stream;
  667. })();
  668. exports['default'] = Stream;
  669. module.exports = exports['default'];
  670. },{}],9:[function(require,module,exports){
  671. (function (global){
  672. // see docs/hlse.md for instructions on how test data was generated
  673. 'use strict';
  674. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  675. var _qunit = (typeof window !== "undefined" ? window['QUnit'] : typeof global !== "undefined" ? global['QUnit'] : null);
  676. var _qunit2 = _interopRequireDefault(_qunit);
  677. var _pkcs7 = require('pkcs7');
  678. var _sinon = (typeof window !== "undefined" ? window['sinon'] : typeof global !== "undefined" ? global['sinon'] : null);
  679. var _sinon2 = _interopRequireDefault(_sinon);
  680. var _src = require('../src');
  681. // see docs/hlse.md for instructions on how test data was generated
  682. var stringFromBytes = function stringFromBytes(bytes) {
  683. var result = '';
  684. for (var i = 0; i < bytes.length; i++) {
  685. result += String.fromCharCode(bytes[i]);
  686. }
  687. return result;
  688. };
  689. _qunit2['default'].module('Decryption');
  690. _qunit2['default'].test('decrypts a single AES-128 with PKCS7 block', function () {
  691. var key = new Uint32Array([0, 0, 0, 0]);
  692. var initVector = key;
  693. // the string "howdy folks" encrypted
  694. var encrypted = new Uint8Array([0xce, 0x90, 0x97, 0xd0, 0x08, 0x46, 0x4d, 0x18, 0x4f, 0xae, 0x01, 0x1c, 0x82, 0xa8, 0xf0, 0x67]);
  695. _qunit2['default'].deepEqual('howdy folks', stringFromBytes((0, _pkcs7.unpad)((0, _src.decrypt)(encrypted, key, initVector))), 'decrypted with a byte array key');
  696. });
  697. _qunit2['default'].test('decrypts multiple AES-128 blocks with CBC', function () {
  698. var key = new Uint32Array([0, 0, 0, 0]);
  699. var initVector = key;
  700. // the string "0123456789abcdef01234" encrypted
  701. var encrypted = new Uint8Array([0x14, 0xf5, 0xfe, 0x74, 0x69, 0x66, 0xf2, 0x92, 0x65, 0x1c, 0x22, 0x88, 0xbb, 0xff, 0x46, 0x09, 0x0b, 0xde, 0x5e, 0x71, 0x77, 0x87, 0xeb, 0x84, 0xa9, 0x54, 0xc2, 0x45, 0xe9, 0x4e, 0x29, 0xb3]);
  702. _qunit2['default'].deepEqual('0123456789abcdef01234', stringFromBytes((0, _pkcs7.unpad)((0, _src.decrypt)(encrypted, key, initVector))), 'decrypted multiple blocks');
  703. });
  704. _qunit2['default'].test('verify that the deepcopy works by doing two decrypts in the same test', function () {
  705. var key = new Uint32Array([0, 0, 0, 0]);
  706. var initVector = key;
  707. // the string "howdy folks" encrypted
  708. var pkcs7Block = new Uint8Array([0xce, 0x90, 0x97, 0xd0, 0x08, 0x46, 0x4d, 0x18, 0x4f, 0xae, 0x01, 0x1c, 0x82, 0xa8, 0xf0, 0x67]);
  709. _qunit2['default'].deepEqual('howdy folks', stringFromBytes((0, _pkcs7.unpad)((0, _src.decrypt)(pkcs7Block, key, initVector))), 'decrypted with a byte array key');
  710. // the string "0123456789abcdef01234" encrypted
  711. var cbcBlocks = new Uint8Array([0x14, 0xf5, 0xfe, 0x74, 0x69, 0x66, 0xf2, 0x92, 0x65, 0x1c, 0x22, 0x88, 0xbb, 0xff, 0x46, 0x09, 0x0b, 0xde, 0x5e, 0x71, 0x77, 0x87, 0xeb, 0x84, 0xa9, 0x54, 0xc2, 0x45, 0xe9, 0x4e, 0x29, 0xb3]);
  712. _qunit2['default'].deepEqual('0123456789abcdef01234', stringFromBytes((0, _pkcs7.unpad)((0, _src.decrypt)(cbcBlocks, key, initVector))), 'decrypted multiple blocks');
  713. });
  714. _qunit2['default'].module('Incremental Processing', {
  715. beforeEach: function beforeEach() {
  716. this.clock = _sinon2['default'].useFakeTimers();
  717. },
  718. afterEach: function afterEach() {
  719. this.clock.restore();
  720. }
  721. });
  722. _qunit2['default'].test('executes a callback after a timeout', function () {
  723. var asyncStream = new _src.AsyncStream();
  724. var calls = '';
  725. asyncStream.push(function () {
  726. calls += 'a';
  727. });
  728. this.clock.tick(asyncStream.delay);
  729. _qunit2['default'].equal(calls, 'a', 'invoked the callback once');
  730. this.clock.tick(asyncStream.delay);
  731. _qunit2['default'].equal(calls, 'a', 'only invoked the callback once');
  732. });
  733. _qunit2['default'].test('executes callback in series', function () {
  734. var asyncStream = new _src.AsyncStream();
  735. var calls = '';
  736. asyncStream.push(function () {
  737. calls += 'a';
  738. });
  739. asyncStream.push(function () {
  740. calls += 'b';
  741. });
  742. this.clock.tick(asyncStream.delay);
  743. _qunit2['default'].equal(calls, 'a', 'invoked the first callback');
  744. this.clock.tick(asyncStream.delay);
  745. _qunit2['default'].equal(calls, 'ab', 'invoked the second');
  746. });
  747. _qunit2['default'].module('Incremental Decryption', {
  748. beforeEach: function beforeEach() {
  749. this.clock = _sinon2['default'].useFakeTimers();
  750. },
  751. afterEach: function afterEach() {
  752. this.clock.restore();
  753. }
  754. });
  755. _qunit2['default'].test('asynchronously decrypts a 4-word block', function () {
  756. var key = new Uint32Array([0, 0, 0, 0]);
  757. var initVector = key;
  758. // the string "howdy folks" encrypted
  759. var encrypted = new Uint8Array([0xce, 0x90, 0x97, 0xd0, 0x08, 0x46, 0x4d, 0x18, 0x4f, 0xae, 0x01, 0x1c, 0x82, 0xa8, 0xf0, 0x67]);
  760. var decrypted = undefined;
  761. var decrypter = new _src.Decrypter(encrypted, key, initVector, function (error, result) {
  762. if (error) {
  763. throw new Error(error);
  764. }
  765. decrypted = result;
  766. });
  767. _qunit2['default'].ok(!decrypted, 'asynchronously decrypts');
  768. this.clock.tick(decrypter.asyncStream_.delay * 2);
  769. _qunit2['default'].ok(decrypted, 'completed decryption');
  770. _qunit2['default'].deepEqual('howdy folks', stringFromBytes(decrypted), 'decrypts and unpads the result');
  771. });
  772. _qunit2['default'].test('breaks up input greater than the step value', function () {
  773. var encrypted = new Int32Array(_src.Decrypter.STEP + 4);
  774. var done = false;
  775. var decrypter = new _src.Decrypter(encrypted, new Uint32Array(4), new Uint32Array(4), function () {
  776. done = true;
  777. });
  778. this.clock.tick(decrypter.asyncStream_.delay * 2);
  779. _qunit2['default'].ok(!done, 'not finished after two ticks');
  780. this.clock.tick(decrypter.asyncStream_.delay);
  781. _qunit2['default'].ok(done, 'finished after the last chunk is decrypted');
  782. });
  783. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  784. },{"../src":7,"pkcs7":2}]},{},[9]);