des.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * DES (Data Encryption Standard) implementation.
  3. *
  4. * This implementation supports DES as well as 3DES-EDE in ECB and CBC mode.
  5. * It is based on the BSD-licensed implementation by Paul Tero:
  6. *
  7. * Paul Tero, July 2001
  8. * http://www.tero.co.uk/des/
  9. *
  10. * Optimised for performance with large blocks by
  11. * Michael Hayworth, November 2001
  12. * http://www.netdealing.com
  13. *
  14. * THIS SOFTWARE IS PROVIDED "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * @author Stefan Siegl
  27. * @author Dave Longley
  28. *
  29. * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
  30. * Copyright (c) 2012-2014 Digital Bazaar, Inc.
  31. */
  32. var forge = require('./forge');
  33. require('./cipher');
  34. require('./cipherModes');
  35. require('./util');
  36. /* DES API */
  37. module.exports = forge.des = forge.des || {};
  38. /**
  39. * Deprecated. Instead, use:
  40. *
  41. * var cipher = forge.cipher.createCipher('DES-<mode>', key);
  42. * cipher.start({iv: iv});
  43. *
  44. * Creates an DES cipher object to encrypt data using the given symmetric key.
  45. * The output will be stored in the 'output' member of the returned cipher.
  46. *
  47. * The key and iv may be given as binary-encoded strings of bytes or
  48. * byte buffers.
  49. *
  50. * @param key the symmetric key to use (64 or 192 bits).
  51. * @param iv the initialization vector to use.
  52. * @param output the buffer to write to, null to create one.
  53. * @param mode the cipher mode to use (default: 'CBC' if IV is
  54. * given, 'ECB' if null).
  55. *
  56. * @return the cipher.
  57. */
  58. forge.des.startEncrypting = function(key, iv, output, mode) {
  59. var cipher = _createCipher({
  60. key: key,
  61. output: output,
  62. decrypt: false,
  63. mode: mode || (iv === null ? 'ECB' : 'CBC')
  64. });
  65. cipher.start(iv);
  66. return cipher;
  67. };
  68. /**
  69. * Deprecated. Instead, use:
  70. *
  71. * var cipher = forge.cipher.createCipher('DES-<mode>', key);
  72. *
  73. * Creates an DES cipher object to encrypt data using the given symmetric key.
  74. *
  75. * The key may be given as a binary-encoded string of bytes or a byte buffer.
  76. *
  77. * @param key the symmetric key to use (64 or 192 bits).
  78. * @param mode the cipher mode to use (default: 'CBC').
  79. *
  80. * @return the cipher.
  81. */
  82. forge.des.createEncryptionCipher = function(key, mode) {
  83. return _createCipher({
  84. key: key,
  85. output: null,
  86. decrypt: false,
  87. mode: mode
  88. });
  89. };
  90. /**
  91. * Deprecated. Instead, use:
  92. *
  93. * var decipher = forge.cipher.createDecipher('DES-<mode>', key);
  94. * decipher.start({iv: iv});
  95. *
  96. * Creates an DES cipher object to decrypt data using the given symmetric key.
  97. * The output will be stored in the 'output' member of the returned cipher.
  98. *
  99. * The key and iv may be given as binary-encoded strings of bytes or
  100. * byte buffers.
  101. *
  102. * @param key the symmetric key to use (64 or 192 bits).
  103. * @param iv the initialization vector to use.
  104. * @param output the buffer to write to, null to create one.
  105. * @param mode the cipher mode to use (default: 'CBC' if IV is
  106. * given, 'ECB' if null).
  107. *
  108. * @return the cipher.
  109. */
  110. forge.des.startDecrypting = function(key, iv, output, mode) {
  111. var cipher = _createCipher({
  112. key: key,
  113. output: output,
  114. decrypt: true,
  115. mode: mode || (iv === null ? 'ECB' : 'CBC')
  116. });
  117. cipher.start(iv);
  118. return cipher;
  119. };
  120. /**
  121. * Deprecated. Instead, use:
  122. *
  123. * var decipher = forge.cipher.createDecipher('DES-<mode>', key);
  124. *
  125. * Creates an DES cipher object to decrypt data using the given symmetric key.
  126. *
  127. * The key may be given as a binary-encoded string of bytes or a byte buffer.
  128. *
  129. * @param key the symmetric key to use (64 or 192 bits).
  130. * @param mode the cipher mode to use (default: 'CBC').
  131. *
  132. * @return the cipher.
  133. */
  134. forge.des.createDecryptionCipher = function(key, mode) {
  135. return _createCipher({
  136. key: key,
  137. output: null,
  138. decrypt: true,
  139. mode: mode
  140. });
  141. };
  142. /**
  143. * Creates a new DES cipher algorithm object.
  144. *
  145. * @param name the name of the algorithm.
  146. * @param mode the mode factory function.
  147. *
  148. * @return the DES algorithm object.
  149. */
  150. forge.des.Algorithm = function(name, mode) {
  151. var self = this;
  152. self.name = name;
  153. self.mode = new mode({
  154. blockSize: 8,
  155. cipher: {
  156. encrypt: function(inBlock, outBlock) {
  157. return _updateBlock(self._keys, inBlock, outBlock, false);
  158. },
  159. decrypt: function(inBlock, outBlock) {
  160. return _updateBlock(self._keys, inBlock, outBlock, true);
  161. }
  162. }
  163. });
  164. self._init = false;
  165. };
  166. /**
  167. * Initializes this DES algorithm by expanding its key.
  168. *
  169. * @param options the options to use.
  170. * key the key to use with this algorithm.
  171. * decrypt true if the algorithm should be initialized for decryption,
  172. * false for encryption.
  173. */
  174. forge.des.Algorithm.prototype.initialize = function(options) {
  175. if(this._init) {
  176. return;
  177. }
  178. var key = forge.util.createBuffer(options.key);
  179. if(this.name.indexOf('3DES') === 0) {
  180. if(key.length() !== 24) {
  181. throw new Error('Invalid Triple-DES key size: ' + key.length() * 8);
  182. }
  183. }
  184. // do key expansion to 16 or 48 subkeys (single or triple DES)
  185. this._keys = _createKeys(key);
  186. this._init = true;
  187. };
  188. /** Register DES algorithms **/
  189. registerAlgorithm('DES-ECB', forge.cipher.modes.ecb);
  190. registerAlgorithm('DES-CBC', forge.cipher.modes.cbc);
  191. registerAlgorithm('DES-CFB', forge.cipher.modes.cfb);
  192. registerAlgorithm('DES-OFB', forge.cipher.modes.ofb);
  193. registerAlgorithm('DES-CTR', forge.cipher.modes.ctr);
  194. registerAlgorithm('3DES-ECB', forge.cipher.modes.ecb);
  195. registerAlgorithm('3DES-CBC', forge.cipher.modes.cbc);
  196. registerAlgorithm('3DES-CFB', forge.cipher.modes.cfb);
  197. registerAlgorithm('3DES-OFB', forge.cipher.modes.ofb);
  198. registerAlgorithm('3DES-CTR', forge.cipher.modes.ctr);
  199. function registerAlgorithm(name, mode) {
  200. var factory = function() {
  201. return new forge.des.Algorithm(name, mode);
  202. };
  203. forge.cipher.registerAlgorithm(name, factory);
  204. }
  205. /** DES implementation **/
  206. var spfunction1 = [0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004];
  207. var spfunction2 = [-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000];
  208. var spfunction3 = [0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200];
  209. var spfunction4 = [0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080];
  210. var spfunction5 = [0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100];
  211. var spfunction6 = [0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010];
  212. var spfunction7 = [0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002];
  213. var spfunction8 = [0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000];
  214. /**
  215. * Create necessary sub keys.
  216. *
  217. * @param key the 64-bit or 192-bit key.
  218. *
  219. * @return the expanded keys.
  220. */
  221. function _createKeys(key) {
  222. var pc2bytes0 = [0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204],
  223. pc2bytes1 = [0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101],
  224. pc2bytes2 = [0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808],
  225. pc2bytes3 = [0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000],
  226. pc2bytes4 = [0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010],
  227. pc2bytes5 = [0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420],
  228. pc2bytes6 = [0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002],
  229. pc2bytes7 = [0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800],
  230. pc2bytes8 = [0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002],
  231. pc2bytes9 = [0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408],
  232. pc2bytes10 = [0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020],
  233. pc2bytes11 = [0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200],
  234. pc2bytes12 = [0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010],
  235. pc2bytes13 = [0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105];
  236. // how many iterations (1 for des, 3 for triple des)
  237. // changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys
  238. var iterations = key.length() > 8 ? 3 : 1;
  239. // stores the return keys
  240. var keys = [];
  241. // now define the left shifts which need to be done
  242. var shifts = [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0];
  243. var n = 0, tmp;
  244. for(var j = 0; j < iterations; j++) {
  245. var left = key.getInt32();
  246. var right = key.getInt32();
  247. tmp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
  248. right ^= tmp;
  249. left ^= (tmp << 4);
  250. tmp = ((right >>> -16) ^ left) & 0x0000ffff;
  251. left ^= tmp;
  252. right ^= (tmp << -16);
  253. tmp = ((left >>> 2) ^ right) & 0x33333333;
  254. right ^= tmp;
  255. left ^= (tmp << 2);
  256. tmp = ((right >>> -16) ^ left) & 0x0000ffff;
  257. left ^= tmp;
  258. right ^= (tmp << -16);
  259. tmp = ((left >>> 1) ^ right) & 0x55555555;
  260. right ^= tmp;
  261. left ^= (tmp << 1);
  262. tmp = ((right >>> 8) ^ left) & 0x00ff00ff;
  263. left ^= tmp;
  264. right ^= (tmp << 8);
  265. tmp = ((left >>> 1) ^ right) & 0x55555555;
  266. right ^= tmp;
  267. left ^= (tmp << 1);
  268. // right needs to be shifted and OR'd with last four bits of left
  269. tmp = (left << 8) | ((right >>> 20) & 0x000000f0);
  270. // left needs to be put upside down
  271. left = ((right << 24) | ((right << 8) & 0xff0000) |
  272. ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0));
  273. right = tmp;
  274. // now go through and perform these shifts on the left and right keys
  275. for(var i = 0; i < shifts.length; ++i) {
  276. //shift the keys either one or two bits to the left
  277. if(shifts[i]) {
  278. left = (left << 2) | (left >>> 26);
  279. right = (right << 2) | (right >>> 26);
  280. } else {
  281. left = (left << 1) | (left >>> 27);
  282. right = (right << 1) | (right >>> 27);
  283. }
  284. left &= -0xf;
  285. right &= -0xf;
  286. // now apply PC-2, in such a way that E is easier when encrypting or
  287. // decrypting this conversion will look like PC-2 except only the last 6
  288. // bits of each byte are used rather than 48 consecutive bits and the
  289. // order of lines will be according to how the S selection functions will
  290. // be applied: S2, S4, S6, S8, S1, S3, S5, S7
  291. var lefttmp = (
  292. pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf] |
  293. pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf] |
  294. pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf] |
  295. pc2bytes6[(left >>> 4) & 0xf]);
  296. var righttmp = (
  297. pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf] |
  298. pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf] |
  299. pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf] |
  300. pc2bytes13[(right >>> 4) & 0xf]);
  301. tmp = ((righttmp >>> 16) ^ lefttmp) & 0x0000ffff;
  302. keys[n++] = lefttmp ^ tmp;
  303. keys[n++] = righttmp ^ (tmp << 16);
  304. }
  305. }
  306. return keys;
  307. }
  308. /**
  309. * Updates a single block (1 byte) using DES. The update will either
  310. * encrypt or decrypt the block.
  311. *
  312. * @param keys the expanded keys.
  313. * @param input the input block (an array of 32-bit words).
  314. * @param output the updated output block.
  315. * @param decrypt true to decrypt the block, false to encrypt it.
  316. */
  317. function _updateBlock(keys, input, output, decrypt) {
  318. // set up loops for single or triple DES
  319. var iterations = keys.length === 32 ? 3 : 9;
  320. var looping;
  321. if(iterations === 3) {
  322. looping = decrypt ? [30, -2, -2] : [0, 32, 2];
  323. } else {
  324. looping = (decrypt ?
  325. [94, 62, -2, 32, 64, 2, 30, -2, -2] :
  326. [0, 32, 2, 62, 30, -2, 64, 96, 2]);
  327. }
  328. var tmp;
  329. var left = input[0];
  330. var right = input[1];
  331. // first each 64 bit chunk of the message must be permuted according to IP
  332. tmp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
  333. right ^= tmp;
  334. left ^= (tmp << 4);
  335. tmp = ((left >>> 16) ^ right) & 0x0000ffff;
  336. right ^= tmp;
  337. left ^= (tmp << 16);
  338. tmp = ((right >>> 2) ^ left) & 0x33333333;
  339. left ^= tmp;
  340. right ^= (tmp << 2);
  341. tmp = ((right >>> 8) ^ left) & 0x00ff00ff;
  342. left ^= tmp;
  343. right ^= (tmp << 8);
  344. tmp = ((left >>> 1) ^ right) & 0x55555555;
  345. right ^= tmp;
  346. left ^= (tmp << 1);
  347. // rotate left 1 bit
  348. left = ((left << 1) | (left >>> 31));
  349. right = ((right << 1) | (right >>> 31));
  350. for(var j = 0; j < iterations; j += 3) {
  351. var endloop = looping[j + 1];
  352. var loopinc = looping[j + 2];
  353. // now go through and perform the encryption or decryption
  354. for(var i = looping[j]; i != endloop; i += loopinc) {
  355. var right1 = right ^ keys[i];
  356. var right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1];
  357. // passing these bytes through the S selection functions
  358. tmp = left;
  359. left = right;
  360. right = tmp ^ (
  361. spfunction2[(right1 >>> 24) & 0x3f] |
  362. spfunction4[(right1 >>> 16) & 0x3f] |
  363. spfunction6[(right1 >>> 8) & 0x3f] |
  364. spfunction8[right1 & 0x3f] |
  365. spfunction1[(right2 >>> 24) & 0x3f] |
  366. spfunction3[(right2 >>> 16) & 0x3f] |
  367. spfunction5[(right2 >>> 8) & 0x3f] |
  368. spfunction7[right2 & 0x3f]);
  369. }
  370. // unreverse left and right
  371. tmp = left;
  372. left = right;
  373. right = tmp;
  374. }
  375. // rotate right 1 bit
  376. left = ((left >>> 1) | (left << 31));
  377. right = ((right >>> 1) | (right << 31));
  378. // now perform IP-1, which is IP in the opposite direction
  379. tmp = ((left >>> 1) ^ right) & 0x55555555;
  380. right ^= tmp;
  381. left ^= (tmp << 1);
  382. tmp = ((right >>> 8) ^ left) & 0x00ff00ff;
  383. left ^= tmp;
  384. right ^= (tmp << 8);
  385. tmp = ((right >>> 2) ^ left) & 0x33333333;
  386. left ^= tmp;
  387. right ^= (tmp << 2);
  388. tmp = ((left >>> 16) ^ right) & 0x0000ffff;
  389. right ^= tmp;
  390. left ^= (tmp << 16);
  391. tmp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
  392. right ^= tmp;
  393. left ^= (tmp << 4);
  394. output[0] = left;
  395. output[1] = right;
  396. }
  397. /**
  398. * Deprecated. Instead, use:
  399. *
  400. * forge.cipher.createCipher('DES-<mode>', key);
  401. * forge.cipher.createDecipher('DES-<mode>', key);
  402. *
  403. * Creates a deprecated DES cipher object. This object's mode will default to
  404. * CBC (cipher-block-chaining).
  405. *
  406. * The key may be given as a binary-encoded string of bytes or a byte buffer.
  407. *
  408. * @param options the options to use.
  409. * key the symmetric key to use (64 or 192 bits).
  410. * output the buffer to write to.
  411. * decrypt true for decryption, false for encryption.
  412. * mode the cipher mode to use (default: 'CBC').
  413. *
  414. * @return the cipher.
  415. */
  416. function _createCipher(options) {
  417. options = options || {};
  418. var mode = (options.mode || 'CBC').toUpperCase();
  419. var algorithm = 'DES-' + mode;
  420. var cipher;
  421. if(options.decrypt) {
  422. cipher = forge.cipher.createDecipher(algorithm, options.key);
  423. } else {
  424. cipher = forge.cipher.createCipher(algorithm, options.key);
  425. }
  426. // backwards compatible start API
  427. var start = cipher.start;
  428. cipher.start = function(iv, options) {
  429. // backwards compatibility: support second arg as output buffer
  430. var output = null;
  431. if(options instanceof forge.util.ByteBuffer) {
  432. output = options;
  433. options = {};
  434. }
  435. options = options || {};
  436. options.output = output;
  437. options.iv = iv;
  438. start.call(cipher, options);
  439. };
  440. return cipher;
  441. }