des.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Ported from Flashlight VNC ActionScript implementation:
  3. * http://www.wizhelp.com/flashlight-vnc/
  4. *
  5. * Full attribution follows:
  6. *
  7. * -------------------------------------------------------------------------
  8. *
  9. * This DES class has been extracted from package Acme.Crypto for use in VNC.
  10. * The unnecessary odd parity code has been removed.
  11. *
  12. * These changes are:
  13. * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
  14. *
  15. * This software is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * DesCipher - the DES encryption method
  20. *
  21. * The meat of this code is by Dave Zimmerman <dzimm@widget.com>, and is:
  22. *
  23. * Copyright (c) 1996 Widget Workshop, Inc. All Rights Reserved.
  24. *
  25. * Permission to use, copy, modify, and distribute this software
  26. * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  27. * without fee is hereby granted, provided that this copyright notice is kept
  28. * intact.
  29. *
  30. * WIDGET WORKSHOP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  31. * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  32. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WIDGET WORKSHOP SHALL NOT BE LIABLE
  34. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  35. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  36. *
  37. * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  38. * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  39. * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  40. * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  41. * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  42. * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  43. * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). WIDGET WORKSHOP
  44. * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  45. * HIGH RISK ACTIVITIES.
  46. *
  47. *
  48. * The rest is:
  49. *
  50. * Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
  51. *
  52. * Redistribution and use in source and binary forms, with or without
  53. * modification, are permitted provided that the following conditions
  54. * are met:
  55. * 1. Redistributions of source code must retain the above copyright
  56. * notice, this list of conditions and the following disclaimer.
  57. * 2. Redistributions in binary form must reproduce the above copyright
  58. * notice, this list of conditions and the following disclaimer in the
  59. * documentation and/or other materials provided with the distribution.
  60. *
  61. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  62. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  63. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  64. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  65. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  66. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  67. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  68. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  69. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  70. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  71. * SUCH DAMAGE.
  72. *
  73. * Visit the ACME Labs Java page for up-to-date versions of this and other
  74. * fine Java utilities: http://www.acme.com/java/
  75. */
  76. /* eslint-disable comma-spacing */
  77. // Tables, permutations, S-boxes, etc.
  78. const PC2 = [13,16,10,23, 0, 4, 2,27,14, 5,20, 9,22,18,11, 3,
  79. 25, 7,15, 6,26,19,12, 1,40,51,30,36,46,54,29,39,
  80. 50,44,32,47,43,48,38,55,33,52,45,41,49,35,28,31 ],
  81. totrot = [ 1, 2, 4, 6, 8,10,12,14,15,17,19,21,23,25,27,28];
  82. const z = 0x0;
  83. let a,b,c,d,e,f;
  84. a=1<<16; b=1<<24; c=a|b; d=1<<2; e=1<<10; f=d|e;
  85. const SP1 = [c|e,z|z,a|z,c|f,c|d,a|f,z|d,a|z,z|e,c|e,c|f,z|e,b|f,c|d,b|z,z|d,
  86. z|f,b|e,b|e,a|e,a|e,c|z,c|z,b|f,a|d,b|d,b|d,a|d,z|z,z|f,a|f,b|z,
  87. a|z,c|f,z|d,c|z,c|e,b|z,b|z,z|e,c|d,a|z,a|e,b|d,z|e,z|d,b|f,a|f,
  88. c|f,a|d,c|z,b|f,b|d,z|f,a|f,c|e,z|f,b|e,b|e,z|z,a|d,a|e,z|z,c|d];
  89. a=1<<20; b=1<<31; c=a|b; d=1<<5; e=1<<15; f=d|e;
  90. const SP2 = [c|f,b|e,z|e,a|f,a|z,z|d,c|d,b|f,b|d,c|f,c|e,b|z,b|e,a|z,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,c|z,a|f,a|z,c|z,z|f,
  107. z|e,c|d,z|d,a|z,b|z,a|e,c|d,b|f,a|d,b|z,c|e,a|f,b|f,z|d,a|z,c|e,
  108. c|f,z|f,c|z,c|f,a|e,z|z,b|e,c|z,z|f,a|d,b|d,z|e,z|z,b|e,a|f,b|d];
  109. a=1<<22; b=1<<29; c=a|b; d=1<<4; e=1<<14; f=d|e;
  110. const SP6 = [b|d,c|z,z|e,c|f,c|z,z|d,c|f,a|z,b|e,a|f,a|z,b|d,a|d,b|e,b|z,z|f,
  111. z|z,a|d,b|f,z|e,a|e,b|f,z|d,c|d,c|d,z|z,a|f,c|e,z|f,a|e,c|e,b|z,
  112. b|e,z|d,c|d,a|e,c|f,a|z,z|f,b|d,a|z,b|e,b|z,z|f,b|d,c|f,a|e,c|z,
  113. a|f,c|e,z|z,c|d,z|d,z|e,c|z,a|f,z|e,a|d,b|f,z|z,c|e,b|z,a|d,b|f];
  114. a=1<<21; b=1<<26; c=a|b; d=1<<1; e=1<<11; f=d|e;
  115. const SP7 = [a|z,c|d,b|f,z|z,z|e,b|f,a|f,c|e,c|f,a|z,z|z,b|d,z|d,b|z,c|d,z|f,
  116. b|e,a|f,a|d,b|e,b|d,c|z,c|e,a|d,c|z,z|e,z|f,c|f,a|e,z|d,b|z,a|e,
  117. b|z,a|e,a|z,b|f,b|f,c|d,c|d,z|d,a|d,b|z,b|e,a|z,c|e,z|f,a|f,c|e,
  118. z|f,b|d,c|f,c|z,a|e,z|z,z|d,c|f,z|z,a|f,c|z,z|e,b|d,b|e,z|e,a|d];
  119. a=1<<18; b=1<<28; c=a|b; d=1<<6; e=1<<12; f=d|e;
  120. const SP8 = [b|f,z|e,a|z,c|f,b|z,b|f,z|d,b|z,a|d,c|z,c|f,a|e,c|e,a|f,z|e,z|d,
  121. c|z,b|d,b|e,z|f,a|e,a|d,c|d,c|e,z|f,z|z,z|z,c|d,b|d,b|e,a|f,a|z,
  122. a|f,a|z,c|e,z|e,z|d,c|d,z|e,a|f,b|e,z|d,b|d,c|z,c|d,b|z,a|z,b|f,
  123. z|z,c|f,a|d,b|d,c|z,b|e,b|f,z|z,c|f,a|e,a|e,z|f,z|f,a|d,b|z,c|e];
  124. /* eslint-enable comma-spacing */
  125. export default class DES {
  126. constructor(password) {
  127. this.keys = [];
  128. // Set the key.
  129. const pc1m = [], pcr = [], kn = [];
  130. for (let j = 0, l = 56; j < 56; ++j, l -= 8) {
  131. l += l < -5 ? 65 : l < -3 ? 31 : l < -1 ? 63 : l === 27 ? 35 : 0; // PC1
  132. const m = l & 0x7;
  133. pc1m[j] = ((password[l >>> 3] & (1<<m)) !== 0) ? 1: 0;
  134. }
  135. for (let i = 0; i < 16; ++i) {
  136. const m = i << 1;
  137. const n = m + 1;
  138. kn[m] = kn[n] = 0;
  139. for (let o = 28; o < 59; o += 28) {
  140. for (let j = o - 28; j < o; ++j) {
  141. const l = j + totrot[i];
  142. pcr[j] = l < o ? pc1m[l] : pc1m[l - 28];
  143. }
  144. }
  145. for (let j = 0; j < 24; ++j) {
  146. if (pcr[PC2[j]] !== 0) {
  147. kn[m] |= 1 << (23 - j);
  148. }
  149. if (pcr[PC2[j + 24]] !== 0) {
  150. kn[n] |= 1 << (23 - j);
  151. }
  152. }
  153. }
  154. // cookey
  155. for (let i = 0, rawi = 0, KnLi = 0; i < 16; ++i) {
  156. const raw0 = kn[rawi++];
  157. const raw1 = kn[rawi++];
  158. this.keys[KnLi] = (raw0 & 0x00fc0000) << 6;
  159. this.keys[KnLi] |= (raw0 & 0x00000fc0) << 10;
  160. this.keys[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
  161. this.keys[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
  162. ++KnLi;
  163. this.keys[KnLi] = (raw0 & 0x0003f000) << 12;
  164. this.keys[KnLi] |= (raw0 & 0x0000003f) << 16;
  165. this.keys[KnLi] |= (raw1 & 0x0003f000) >>> 4;
  166. this.keys[KnLi] |= (raw1 & 0x0000003f);
  167. ++KnLi;
  168. }
  169. }
  170. // Encrypt 8 bytes of text
  171. enc8(text) {
  172. const b = text.slice();
  173. let i = 0, l, r, x; // left, right, accumulator
  174. // Squash 8 bytes to 2 ints
  175. l = b[i++]<<24 | b[i++]<<16 | b[i++]<<8 | b[i++];
  176. r = b[i++]<<24 | b[i++]<<16 | b[i++]<<8 | b[i++];
  177. x = ((l >>> 4) ^ r) & 0x0f0f0f0f;
  178. r ^= x;
  179. l ^= (x << 4);
  180. x = ((l >>> 16) ^ r) & 0x0000ffff;
  181. r ^= x;
  182. l ^= (x << 16);
  183. x = ((r >>> 2) ^ l) & 0x33333333;
  184. l ^= x;
  185. r ^= (x << 2);
  186. x = ((r >>> 8) ^ l) & 0x00ff00ff;
  187. l ^= x;
  188. r ^= (x << 8);
  189. r = (r << 1) | ((r >>> 31) & 1);
  190. x = (l ^ r) & 0xaaaaaaaa;
  191. l ^= x;
  192. r ^= x;
  193. l = (l << 1) | ((l >>> 31) & 1);
  194. for (let i = 0, keysi = 0; i < 8; ++i) {
  195. x = (r << 28) | (r >>> 4);
  196. x ^= this.keys[keysi++];
  197. let fval = SP7[x & 0x3f];
  198. fval |= SP5[(x >>> 8) & 0x3f];
  199. fval |= SP3[(x >>> 16) & 0x3f];
  200. fval |= SP1[(x >>> 24) & 0x3f];
  201. x = r ^ this.keys[keysi++];
  202. fval |= SP8[x & 0x3f];
  203. fval |= SP6[(x >>> 8) & 0x3f];
  204. fval |= SP4[(x >>> 16) & 0x3f];
  205. fval |= SP2[(x >>> 24) & 0x3f];
  206. l ^= fval;
  207. x = (l << 28) | (l >>> 4);
  208. x ^= this.keys[keysi++];
  209. fval = SP7[x & 0x3f];
  210. fval |= SP5[(x >>> 8) & 0x3f];
  211. fval |= SP3[(x >>> 16) & 0x3f];
  212. fval |= SP1[(x >>> 24) & 0x3f];
  213. x = l ^ this.keys[keysi++];
  214. fval |= SP8[x & 0x0000003f];
  215. fval |= SP6[(x >>> 8) & 0x3f];
  216. fval |= SP4[(x >>> 16) & 0x3f];
  217. fval |= SP2[(x >>> 24) & 0x3f];
  218. r ^= fval;
  219. }
  220. r = (r << 31) | (r >>> 1);
  221. x = (l ^ r) & 0xaaaaaaaa;
  222. l ^= x;
  223. r ^= x;
  224. l = (l << 31) | (l >>> 1);
  225. x = ((l >>> 8) ^ r) & 0x00ff00ff;
  226. r ^= x;
  227. l ^= (x << 8);
  228. x = ((l >>> 2) ^ r) & 0x33333333;
  229. r ^= x;
  230. l ^= (x << 2);
  231. x = ((r >>> 16) ^ l) & 0x0000ffff;
  232. l ^= x;
  233. r ^= (x << 16);
  234. x = ((r >>> 4) ^ l) & 0x0f0f0f0f;
  235. l ^= x;
  236. r ^= (x << 4);
  237. // Spread ints to bytes
  238. x = [r, l];
  239. for (i = 0; i < 8; i++) {
  240. b[i] = (x[i>>>2] >>> (8 * (3 - (i % 4)))) % 256;
  241. if (b[i] < 0) { b[i] += 256; } // unsigned
  242. }
  243. return b;
  244. }
  245. // Encrypt 16 bytes of text using passwd as key
  246. encrypt(t) {
  247. return this.enc8(t.slice(0, 8)).concat(this.enc8(t.slice(8, 16)));
  248. }
  249. }