DES.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**
  2. * Definition of Data Encryption Standard (DES) taken from:
  3. * http://www.itl.nist.gov/fipspubs/fip46-2.htm
  4. */
  5. (function() {
  6. var C = (typeof window === 'undefined') ? require('./Crypto').Crypto : window.Crypto;
  7. // Shortcuts
  8. var util = C.util, charenc = C.charenc, UTF8 = charenc.UTF8;
  9. /***************************************************************************
  10. *
  11. * DES Key Schedule.
  12. *
  13. * The Key consists of 16 sub-keys of 48 bits each. As each sub-key is
  14. * applied to an expanded 32-bit value where each 4 bits of input is
  15. * expanded into 6 bits of output the sub-key can be broken down into 8
  16. * 32-bit values which allows the key to be used without expansion.
  17. *
  18. * To create the 16 sub-keys, 56 bits are selected from the input 64 bit key
  19. * according to <i>PC1</i>. Each sub-key is generated by left rotating the
  20. * bits a different amount and then selecting 48 bits according to <i>PC2</i>.
  21. *
  22. **************************************************************************/
  23. var KeySchedule;
  24. /**
  25. * Representation of a DES key schedule.
  26. *
  27. * @param {Array
  28. * of 8 bytes} key The cipher key
  29. *
  30. * @constructor
  31. */
  32. KeySchedule = function(key) {
  33. /**
  34. * The schedule of 16 keys
  35. */
  36. this.keys = new Array(16);
  37. this._initialiseKeys(key);
  38. };
  39. /**
  40. * Permuted Choice 1 (PC1) byte offsets into the key. Each of the 56 entries
  41. * selects one bit of DES's 56 bit key.
  42. * <p>
  43. *
  44. * <pre>
  45. * The PC1 is defined as:
  46. *
  47. * 57, 49, 41, 33, 25, 17, 9,
  48. * 1, 58, 50, 42, 34, 26, 18,
  49. * 10, 2, 59, 51, 43, 35, 27,
  50. * 19, 11, 3, 60, 52, 44, 36,
  51. * 63, 55, 47, 39, 31, 23, 15,
  52. * 7, 62, 54, 46, 38, 30, 22,
  53. * 14, 6, 61, 53, 45, 37, 29,
  54. * 21, 13, 5, 28, 20, 12, 4
  55. * </pre>
  56. *
  57. * We represent this as an offset into an 8-byte array and a bit mask upon
  58. * that byte. For example 57=(7*8)+1 so is the first (MSB) of the 7th byte.
  59. *
  60. * @constant
  61. */
  62. KeySchedule.PC1_offsets = [ 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0,
  63. 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6,
  64. 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 3, 2, 1, 0 ];
  65. /**
  66. * Permuted Choice 1 (PC1) bit masks. Each of the 56 entries selects one bit
  67. * of DES's 56 bit key.
  68. *
  69. * @constant
  70. */
  71. KeySchedule.PC1_masks = [ 128, 128, 128, 128, 128, 128, 128, 128, 64, 64,
  72. 64, 64, 64, 64, 64, 64, 32, 32, 32, 32, 32, 32, 32, 32, 16, 16, 16,
  73. 16, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
  74. 8, 8, 8, 16, 16, 16, 16 ];
  75. /**
  76. * Permuted Choice 2 (PC2) selects the active 48 bits from the 56 bits of
  77. * the key.
  78. * <p>
  79. *
  80. * <pre>
  81. * The PC2 is defined as:
  82. *
  83. * 14, 17, 11, 24, 1, 5,
  84. * 3, 28, 15, 6, 21, 10,
  85. * 23, 19, 12, 4, 26, 8,
  86. * 16, 7, 27, 20, 13, 2,
  87. * 41, 52, 31, 37, 47, 55,
  88. * 30, 40, 51, 45, 33, 48,
  89. * 44, 49, 39, 56, 34, 53,
  90. * 46, 42, 50, 36, 29, 32
  91. * </pre>
  92. *
  93. * We invert the choice to specify what each bit adds to each 6-bit value of
  94. * the key. For example, bit 1 is the 5th bit selected so this add 2 to the
  95. * first 6-bit value.
  96. *
  97. * @constant
  98. */
  99. KeySchedule.PC2_offsets1 = [ 0, 3, 1, 2, 0, 1, 3, 2, 0, 1, 0, 2, 3, 0, 1,
  100. 3, 0, 0, 2, 3, 1, 0, 2, 0, 0, 2, 3, 1 ];
  101. /**
  102. * PC2 offsets for 2nd block.
  103. *
  104. * @constant
  105. */
  106. KeySchedule.PC2_offsets2 = [ 7, 5, 4, 7, 5, 6, 0, 7, 4, 0, 6, 5, 4, 7, 0,
  107. 6, 5, 7, 4, 5, 6, 7, 5, 4, 6, 0, 4, 6 ];
  108. /**
  109. * Permuted Choice 2 (PC2) masks for 1st block.
  110. *
  111. * @constant
  112. */
  113. KeySchedule.PC2_masks1 = [ 2, 1, 32, 4, 1, 4, 16, 1, 0, 1, 8, 8, 2, 32, 8,
  114. 32, 16, 0, 16, 4, 2, 0, 32, 4, 0, 2, 8, 16 ];
  115. /**
  116. * PC2 masks for 2nd block.
  117. *
  118. * @constant
  119. */
  120. KeySchedule.PC2_masks2 = [ 2, 32, 8, 1, 2, 2, 0, 4, 4, 0, 8, 16, 32, 16, 0,
  121. 32, 4, 32, 2, 1, 16, 8, 8, 16, 1, 0, 1, 4 ];
  122. /**
  123. * Cumulative key shifts.
  124. *
  125. * @constant
  126. */
  127. KeySchedule.keyShifts = [ 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23,
  128. 25, 27, 28 ];
  129. KeySchedule.prototype._initialiseKeys = function(key) {
  130. var i;
  131. // extract 56 key bits in order determined by PC1
  132. var bits = new Array(56);
  133. for (i = 0; i < 56; i++) {
  134. bits[i] = (key[KeySchedule.PC1_offsets[i]] & KeySchedule.PC1_masks[i]) != 0;
  135. }
  136. // split 56 bits into two 28-bit chunks
  137. var bits1 = bits.slice(0, 28);
  138. var bits2 = bits.slice(28, 56);
  139. // duplicate each half to allow for easy bit shifts
  140. bits1 = bits1.concat(bits1);
  141. bits2 = bits2.concat(bits2);
  142. // assemble the 16 keys
  143. for (i = 0; i < 16; i++) {
  144. var k = [ 0, 0, 0, 0, 0, 0, 0, 0 ];
  145. // select the bits of the key according to PC2
  146. var s = KeySchedule.keyShifts[i];
  147. for ( var j = 0; j < 28; j++) {
  148. if (bits1[j + s]) {
  149. k[KeySchedule.PC2_offsets1[j]] += KeySchedule.PC2_masks1[j];
  150. }
  151. if (bits2[j + s]) {
  152. k[KeySchedule.PC2_offsets2[j]] += KeySchedule.PC2_masks2[j];
  153. }
  154. }
  155. // Scale each of the 8 blocks to a 32-bit mask.
  156. k[0] = ((k[0] & 0x1f) << 27) + ((k[0] & 0x20) >> 5);
  157. for ( var j = 1; j <= 6; j++) {
  158. k[j] = k[j] << (27 - 4 * j);
  159. }
  160. k[7] = ((k[7] & 0x3e) >> 1) + ((k[7] & 0x1) << 31);
  161. this.keys[i] = k;
  162. }
  163. };
  164. /**
  165. * Retrieve the key for a specified round
  166. *
  167. * @param i
  168. * the round
  169. * @returns the key
  170. */
  171. KeySchedule.prototype.getKey = function(i) {
  172. return this.keys[i];
  173. };
  174. /***************************************************************************
  175. *
  176. * DES Engine State
  177. *
  178. **************************************************************************/
  179. var State;
  180. /**
  181. * The algorithm's state. DES operates on two sets of 32-bits, with each
  182. * block of 32-bits treated as a single number.
  183. *
  184. * @class
  185. */
  186. State = function() {
  187. /** The LHS of the Feistel scheme */
  188. this.lhs = 0;
  189. /** The RHS of the Feistel scheme */
  190. this.rhs = 0;
  191. };
  192. /**
  193. * The masks that select the SBOX input. Each SBOX accepts 6 bits from the
  194. * input.
  195. *
  196. * @constant
  197. */
  198. State.SBOX_MASK = [ 0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000,
  199. 0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f ];
  200. /**
  201. * The SBOXes. The 8 SBOXes each map 6 bit masked bit of the input to 4 bits
  202. * of output. These SBOXes include the post SBOX permutation and benefit
  203. * from JavaScript's sparse arrays to make specifying the input match
  204. * simple.
  205. *
  206. * @constant
  207. */
  208. State.SBOX = new Array(8);
  209. var SBOX = State.SBOX;
  210. SBOX[0] = new Array();
  211. SBOX[0][0] = 0x808200; // 0 (0, 0) = 14
  212. SBOX[0][268435456] = 0x8000; // 10000000 (0, 1) = 4
  213. SBOX[0][536870912] = 0x808002; // 20000000 (0, 2) = 13
  214. SBOX[0][805306368] = 0x2; // 30000000 (0, 3) = 1
  215. SBOX[0][1073741824] = 0x200; // 40000000 (0, 4) = 2
  216. SBOX[0][1342177280] = 0x808202; // 50000000 (0, 5) = 15
  217. SBOX[0][1610612736] = 0x800202; // 60000000 (0, 6) = 11
  218. SBOX[0][1879048192] = 0x800000; // 70000000 (0, 7) = 8
  219. SBOX[0][-2147483648] = 0x202; // 80000000 (0, 8) = 3
  220. SBOX[0][-1879048192] = 0x800200; // 90000000 (0, 9) = 10
  221. SBOX[0][-1610612736] = 0x8200; // a0000000 (0, 10) = 6
  222. SBOX[0][-1342177280] = 0x808000; // b0000000 (0, 11) = 12
  223. SBOX[0][-1073741824] = 0x8002; // c0000000 (0, 12) = 5
  224. SBOX[0][-805306368] = 0x800002; // d0000000 (0, 13) = 9
  225. SBOX[0][-536870912] = 0x0; // e0000000 (0, 14) = 0
  226. SBOX[0][-268435456] = 0x8202; // f0000000 (0, 15) = 7
  227. SBOX[0][134217728] = 0x0; // 8000000 (1, 0) = 0
  228. SBOX[0][402653184] = 0x808202; // 18000000 (1, 1) = 15
  229. SBOX[0][671088640] = 0x8202; // 28000000 (1, 2) = 7
  230. SBOX[0][939524096] = 0x8000; // 38000000 (1, 3) = 4
  231. SBOX[0][1207959552] = 0x808200; // 48000000 (1, 4) = 14
  232. SBOX[0][1476395008] = 0x200; // 58000000 (1, 5) = 2
  233. SBOX[0][1744830464] = 0x808002; // 68000000 (1, 6) = 13
  234. SBOX[0][2013265920] = 0x2; // 78000000 (1, 7) = 1
  235. SBOX[0][-2013265920] = 0x800200; // 88000000 (1, 8) = 10
  236. SBOX[0][-1744830464] = 0x8200; // 98000000 (1, 9) = 6
  237. SBOX[0][-1476395008] = 0x808000; // a8000000 (1, 10) = 12
  238. SBOX[0][-1207959552] = 0x800202; // b8000000 (1, 11) = 11
  239. SBOX[0][-939524096] = 0x800002; // c8000000 (1, 12) = 9
  240. SBOX[0][-671088640] = 0x8002; // d8000000 (1, 13) = 5
  241. SBOX[0][-402653184] = 0x202; // e8000000 (1, 14) = 3
  242. SBOX[0][-134217728] = 0x800000; // f8000000 (1, 15) = 8
  243. SBOX[0][1] = 0x8000; // 1 (2, 0) = 4
  244. SBOX[0][268435457] = 0x2; // 10000001 (2, 1) = 1
  245. SBOX[0][536870913] = 0x808200; // 20000001 (2, 2) = 14
  246. SBOX[0][805306369] = 0x800000; // 30000001 (2, 3) = 8
  247. SBOX[0][1073741825] = 0x808002; // 40000001 (2, 4) = 13
  248. SBOX[0][1342177281] = 0x8200; // 50000001 (2, 5) = 6
  249. SBOX[0][1610612737] = 0x200; // 60000001 (2, 6) = 2
  250. SBOX[0][1879048193] = 0x800202; // 70000001 (2, 7) = 11
  251. SBOX[0][-2147483647] = 0x808202; // 80000001 (2, 8) = 15
  252. SBOX[0][-1879048191] = 0x808000; // 90000001 (2, 9) = 12
  253. SBOX[0][-1610612735] = 0x800002; // a0000001 (2, 10) = 9
  254. SBOX[0][-1342177279] = 0x8202; // b0000001 (2, 11) = 7
  255. SBOX[0][-1073741823] = 0x202; // c0000001 (2, 12) = 3
  256. SBOX[0][-805306367] = 0x800200; // d0000001 (2, 13) = 10
  257. SBOX[0][-536870911] = 0x8002; // e0000001 (2, 14) = 5
  258. SBOX[0][-268435455] = 0x0; // f0000001 (2, 15) = 0
  259. SBOX[0][134217729] = 0x808202; // 8000001 (3, 0) = 15
  260. SBOX[0][402653185] = 0x808000; // 18000001 (3, 1) = 12
  261. SBOX[0][671088641] = 0x800000; // 28000001 (3, 2) = 8
  262. SBOX[0][939524097] = 0x200; // 38000001 (3, 3) = 2
  263. SBOX[0][1207959553] = 0x8000; // 48000001 (3, 4) = 4
  264. SBOX[0][1476395009] = 0x800002; // 58000001 (3, 5) = 9
  265. SBOX[0][1744830465] = 0x2; // 68000001 (3, 6) = 1
  266. SBOX[0][2013265921] = 0x8202; // 78000001 (3, 7) = 7
  267. SBOX[0][-2013265919] = 0x8002; // 88000001 (3, 8) = 5
  268. SBOX[0][-1744830463] = 0x800202; // 98000001 (3, 9) = 11
  269. SBOX[0][-1476395007] = 0x202; // a8000001 (3, 10) = 3
  270. SBOX[0][-1207959551] = 0x808200; // b8000001 (3, 11) = 14
  271. SBOX[0][-939524095] = 0x800200; // c8000001 (3, 12) = 10
  272. SBOX[0][-671088639] = 0x0; // d8000001 (3, 13) = 0
  273. SBOX[0][-402653183] = 0x8200; // e8000001 (3, 14) = 6
  274. SBOX[0][-134217727] = 0x808002; // f8000001 (3, 15) = 13
  275. SBOX[1] = new Array();
  276. SBOX[1][0] = 0x40084010; // 0 (0, 0) = 15
  277. SBOX[1][16777216] = 0x4000; // 1000000 (0, 1) = 1
  278. SBOX[1][33554432] = 0x80000; // 2000000 (0, 2) = 8
  279. SBOX[1][50331648] = 0x40080010; // 3000000 (0, 3) = 14
  280. SBOX[1][67108864] = 0x40000010; // 4000000 (0, 4) = 6
  281. SBOX[1][83886080] = 0x40084000; // 5000000 (0, 5) = 11
  282. SBOX[1][100663296] = 0x40004000; // 6000000 (0, 6) = 3
  283. SBOX[1][117440512] = 0x10; // 7000000 (0, 7) = 4
  284. SBOX[1][134217728] = 0x84000; // 8000000 (0, 8) = 9
  285. SBOX[1][150994944] = 0x40004010; // 9000000 (0, 9) = 7
  286. SBOX[1][167772160] = 0x40000000; // a000000 (0, 10) = 2
  287. SBOX[1][184549376] = 0x84010; // b000000 (0, 11) = 13
  288. SBOX[1][201326592] = 0x80010; // c000000 (0, 12) = 12
  289. SBOX[1][218103808] = 0x0; // d000000 (0, 13) = 0
  290. SBOX[1][234881024] = 0x4010; // e000000 (0, 14) = 5
  291. SBOX[1][251658240] = 0x40080000; // f000000 (0, 15) = 10
  292. SBOX[1][8388608] = 0x40004000; // 800000 (1, 0) = 3
  293. SBOX[1][25165824] = 0x84010; // 1800000 (1, 1) = 13
  294. SBOX[1][41943040] = 0x10; // 2800000 (1, 2) = 4
  295. SBOX[1][58720256] = 0x40004010; // 3800000 (1, 3) = 7
  296. SBOX[1][75497472] = 0x40084010; // 4800000 (1, 4) = 15
  297. SBOX[1][92274688] = 0x40000000; // 5800000 (1, 5) = 2
  298. SBOX[1][109051904] = 0x80000; // 6800000 (1, 6) = 8
  299. SBOX[1][125829120] = 0x40080010; // 7800000 (1, 7) = 14
  300. SBOX[1][142606336] = 0x80010; // 8800000 (1, 8) = 12
  301. SBOX[1][159383552] = 0x0; // 9800000 (1, 9) = 0
  302. SBOX[1][176160768] = 0x4000; // a800000 (1, 10) = 1
  303. SBOX[1][192937984] = 0x40080000; // b800000 (1, 11) = 10
  304. SBOX[1][209715200] = 0x40000010; // c800000 (1, 12) = 6
  305. SBOX[1][226492416] = 0x84000; // d800000 (1, 13) = 9
  306. SBOX[1][243269632] = 0x40084000; // e800000 (1, 14) = 11
  307. SBOX[1][260046848] = 0x4010; // f800000 (1, 15) = 5
  308. SBOX[1][268435456] = 0x0; // 10000000 (2, 0) = 0
  309. SBOX[1][285212672] = 0x40080010; // 11000000 (2, 1) = 14
  310. SBOX[1][301989888] = 0x40004010; // 12000000 (2, 2) = 7
  311. SBOX[1][318767104] = 0x40084000; // 13000000 (2, 3) = 11
  312. SBOX[1][335544320] = 0x40080000; // 14000000 (2, 4) = 10
  313. SBOX[1][352321536] = 0x10; // 15000000 (2, 5) = 4
  314. SBOX[1][369098752] = 0x84010; // 16000000 (2, 6) = 13
  315. SBOX[1][385875968] = 0x4000; // 17000000 (2, 7) = 1
  316. SBOX[1][402653184] = 0x4010; // 18000000 (2, 8) = 5
  317. SBOX[1][419430400] = 0x80000; // 19000000 (2, 9) = 8
  318. SBOX[1][436207616] = 0x80010; // 1a000000 (2, 10) = 12
  319. SBOX[1][452984832] = 0x40000010; // 1b000000 (2, 11) = 6
  320. SBOX[1][469762048] = 0x84000; // 1c000000 (2, 12) = 9
  321. SBOX[1][486539264] = 0x40004000; // 1d000000 (2, 13) = 3
  322. SBOX[1][503316480] = 0x40000000; // 1e000000 (2, 14) = 2
  323. SBOX[1][520093696] = 0x40084010; // 1f000000 (2, 15) = 15
  324. SBOX[1][276824064] = 0x84010; // 10800000 (3, 0) = 13
  325. SBOX[1][293601280] = 0x80000; // 11800000 (3, 1) = 8
  326. SBOX[1][310378496] = 0x40080000; // 12800000 (3, 2) = 10
  327. SBOX[1][327155712] = 0x4000; // 13800000 (3, 3) = 1
  328. SBOX[1][343932928] = 0x40004000; // 14800000 (3, 4) = 3
  329. SBOX[1][360710144] = 0x40084010; // 15800000 (3, 5) = 15
  330. SBOX[1][377487360] = 0x10; // 16800000 (3, 6) = 4
  331. SBOX[1][394264576] = 0x40000000; // 17800000 (3, 7) = 2
  332. SBOX[1][411041792] = 0x40084000; // 18800000 (3, 8) = 11
  333. SBOX[1][427819008] = 0x40000010; // 19800000 (3, 9) = 6
  334. SBOX[1][444596224] = 0x40004010; // 1a800000 (3, 10) = 7
  335. SBOX[1][461373440] = 0x80010; // 1b800000 (3, 11) = 12
  336. SBOX[1][478150656] = 0x0; // 1c800000 (3, 12) = 0
  337. SBOX[1][494927872] = 0x4010; // 1d800000 (3, 13) = 5
  338. SBOX[1][511705088] = 0x40080010; // 1e800000 (3, 14) = 14
  339. SBOX[1][528482304] = 0x84000; // 1f800000 (3, 15) = 9
  340. SBOX[2] = new Array();
  341. SBOX[2][0] = 0x104; // 0 (0, 0) = 10
  342. SBOX[2][1048576] = 0x0; // 100000 (0, 1) = 0
  343. SBOX[2][2097152] = 0x4000100; // 200000 (0, 2) = 9
  344. SBOX[2][3145728] = 0x10104; // 300000 (0, 3) = 14
  345. SBOX[2][4194304] = 0x10004; // 400000 (0, 4) = 6
  346. SBOX[2][5242880] = 0x4000004; // 500000 (0, 5) = 3
  347. SBOX[2][6291456] = 0x4010104; // 600000 (0, 6) = 15
  348. SBOX[2][7340032] = 0x4010000; // 700000 (0, 7) = 5
  349. SBOX[2][8388608] = 0x4000000; // 800000 (0, 8) = 1
  350. SBOX[2][9437184] = 0x4010100; // 900000 (0, 9) = 13
  351. SBOX[2][10485760] = 0x10100; // a00000 (0, 10) = 12
  352. SBOX[2][11534336] = 0x4010004; // b00000 (0, 11) = 7
  353. SBOX[2][12582912] = 0x4000104; // c00000 (0, 12) = 11
  354. SBOX[2][13631488] = 0x10000; // d00000 (0, 13) = 4
  355. SBOX[2][14680064] = 0x4; // e00000 (0, 14) = 2
  356. SBOX[2][15728640] = 0x100; // f00000 (0, 15) = 8
  357. SBOX[2][524288] = 0x4010100; // 80000 (1, 0) = 13
  358. SBOX[2][1572864] = 0x4010004; // 180000 (1, 1) = 7
  359. SBOX[2][2621440] = 0x0; // 280000 (1, 2) = 0
  360. SBOX[2][3670016] = 0x4000100; // 380000 (1, 3) = 9
  361. SBOX[2][4718592] = 0x4000004; // 480000 (1, 4) = 3
  362. SBOX[2][5767168] = 0x10000; // 580000 (1, 5) = 4
  363. SBOX[2][6815744] = 0x10004; // 680000 (1, 6) = 6
  364. SBOX[2][7864320] = 0x104; // 780000 (1, 7) = 10
  365. SBOX[2][8912896] = 0x4; // 880000 (1, 8) = 2
  366. SBOX[2][9961472] = 0x100; // 980000 (1, 9) = 8
  367. SBOX[2][11010048] = 0x4010000; // a80000 (1, 10) = 5
  368. SBOX[2][12058624] = 0x10104; // b80000 (1, 11) = 14
  369. SBOX[2][13107200] = 0x10100; // c80000 (1, 12) = 12
  370. SBOX[2][14155776] = 0x4000104; // d80000 (1, 13) = 11
  371. SBOX[2][15204352] = 0x4010104; // e80000 (1, 14) = 15
  372. SBOX[2][16252928] = 0x4000000; // f80000 (1, 15) = 1
  373. SBOX[2][16777216] = 0x4010100; // 1000000 (2, 0) = 13
  374. SBOX[2][17825792] = 0x10004; // 1100000 (2, 1) = 6
  375. SBOX[2][18874368] = 0x10000; // 1200000 (2, 2) = 4
  376. SBOX[2][19922944] = 0x4000100; // 1300000 (2, 3) = 9
  377. SBOX[2][20971520] = 0x100; // 1400000 (2, 4) = 8
  378. SBOX[2][22020096] = 0x4010104; // 1500000 (2, 5) = 15
  379. SBOX[2][23068672] = 0x4000004; // 1600000 (2, 6) = 3
  380. SBOX[2][24117248] = 0x0; // 1700000 (2, 7) = 0
  381. SBOX[2][25165824] = 0x4000104; // 1800000 (2, 8) = 11
  382. SBOX[2][26214400] = 0x4000000; // 1900000 (2, 9) = 1
  383. SBOX[2][27262976] = 0x4; // 1a00000 (2, 10) = 2
  384. SBOX[2][28311552] = 0x10100; // 1b00000 (2, 11) = 12
  385. SBOX[2][29360128] = 0x4010000; // 1c00000 (2, 12) = 5
  386. SBOX[2][30408704] = 0x104; // 1d00000 (2, 13) = 10
  387. SBOX[2][31457280] = 0x10104; // 1e00000 (2, 14) = 14
  388. SBOX[2][32505856] = 0x4010004; // 1f00000 (2, 15) = 7
  389. SBOX[2][17301504] = 0x4000000; // 1080000 (3, 0) = 1
  390. SBOX[2][18350080] = 0x104; // 1180000 (3, 1) = 10
  391. SBOX[2][19398656] = 0x4010100; // 1280000 (3, 2) = 13
  392. SBOX[2][20447232] = 0x0; // 1380000 (3, 3) = 0
  393. SBOX[2][21495808] = 0x10004; // 1480000 (3, 4) = 6
  394. SBOX[2][22544384] = 0x4000100; // 1580000 (3, 5) = 9
  395. SBOX[2][23592960] = 0x100; // 1680000 (3, 6) = 8
  396. SBOX[2][24641536] = 0x4010004; // 1780000 (3, 7) = 7
  397. SBOX[2][25690112] = 0x10000; // 1880000 (3, 8) = 4
  398. SBOX[2][26738688] = 0x4010104; // 1980000 (3, 9) = 15
  399. SBOX[2][27787264] = 0x10104; // 1a80000 (3, 10) = 14
  400. SBOX[2][28835840] = 0x4000004; // 1b80000 (3, 11) = 3
  401. SBOX[2][29884416] = 0x4000104; // 1c80000 (3, 12) = 11
  402. SBOX[2][30932992] = 0x4010000; // 1d80000 (3, 13) = 5
  403. SBOX[2][31981568] = 0x4; // 1e80000 (3, 14) = 2
  404. SBOX[2][33030144] = 0x10100; // 1f80000 (3, 15) = 12
  405. SBOX[3] = new Array();
  406. SBOX[3][0] = 0x80401000; // 0 (0, 0) = 7
  407. SBOX[3][65536] = 0x80001040; // 10000 (0, 1) = 13
  408. SBOX[3][131072] = 0x401040; // 20000 (0, 2) = 14
  409. SBOX[3][196608] = 0x80400000; // 30000 (0, 3) = 3
  410. SBOX[3][262144] = 0x0; // 40000 (0, 4) = 0
  411. SBOX[3][327680] = 0x401000; // 50000 (0, 5) = 6
  412. SBOX[3][393216] = 0x80000040; // 60000 (0, 6) = 9
  413. SBOX[3][458752] = 0x400040; // 70000 (0, 7) = 10
  414. SBOX[3][524288] = 0x80000000; // 80000 (0, 8) = 1
  415. SBOX[3][589824] = 0x400000; // 90000 (0, 9) = 2
  416. SBOX[3][655360] = 0x40; // a0000 (0, 10) = 8
  417. SBOX[3][720896] = 0x80001000; // b0000 (0, 11) = 5
  418. SBOX[3][786432] = 0x80400040; // c0000 (0, 12) = 11
  419. SBOX[3][851968] = 0x1040; // d0000 (0, 13) = 12
  420. SBOX[3][917504] = 0x1000; // e0000 (0, 14) = 4
  421. SBOX[3][983040] = 0x80401040; // f0000 (0, 15) = 15
  422. SBOX[3][32768] = 0x80001040; // 8000 (1, 0) = 13
  423. SBOX[3][98304] = 0x40; // 18000 (1, 1) = 8
  424. SBOX[3][163840] = 0x80400040; // 28000 (1, 2) = 11
  425. SBOX[3][229376] = 0x80001000; // 38000 (1, 3) = 5
  426. SBOX[3][294912] = 0x401000; // 48000 (1, 4) = 6
  427. SBOX[3][360448] = 0x80401040; // 58000 (1, 5) = 15
  428. SBOX[3][425984] = 0x0; // 68000 (1, 6) = 0
  429. SBOX[3][491520] = 0x80400000; // 78000 (1, 7) = 3
  430. SBOX[3][557056] = 0x1000; // 88000 (1, 8) = 4
  431. SBOX[3][622592] = 0x80401000; // 98000 (1, 9) = 7
  432. SBOX[3][688128] = 0x400000; // a8000 (1, 10) = 2
  433. SBOX[3][753664] = 0x1040; // b8000 (1, 11) = 12
  434. SBOX[3][819200] = 0x80000000; // c8000 (1, 12) = 1
  435. SBOX[3][884736] = 0x400040; // d8000 (1, 13) = 10
  436. SBOX[3][950272] = 0x401040; // e8000 (1, 14) = 14
  437. SBOX[3][1015808] = 0x80000040; // f8000 (1, 15) = 9
  438. SBOX[3][1048576] = 0x400040; // 100000 (2, 0) = 10
  439. SBOX[3][1114112] = 0x401000; // 110000 (2, 1) = 6
  440. SBOX[3][1179648] = 0x80000040; // 120000 (2, 2) = 9
  441. SBOX[3][1245184] = 0x0; // 130000 (2, 3) = 0
  442. SBOX[3][1310720] = 0x1040; // 140000 (2, 4) = 12
  443. SBOX[3][1376256] = 0x80400040; // 150000 (2, 5) = 11
  444. SBOX[3][1441792] = 0x80401000; // 160000 (2, 6) = 7
  445. SBOX[3][1507328] = 0x80001040; // 170000 (2, 7) = 13
  446. SBOX[3][1572864] = 0x80401040; // 180000 (2, 8) = 15
  447. SBOX[3][1638400] = 0x80000000; // 190000 (2, 9) = 1
  448. SBOX[3][1703936] = 0x80400000; // 1a0000 (2, 10) = 3
  449. SBOX[3][1769472] = 0x401040; // 1b0000 (2, 11) = 14
  450. SBOX[3][1835008] = 0x80001000; // 1c0000 (2, 12) = 5
  451. SBOX[3][1900544] = 0x400000; // 1d0000 (2, 13) = 2
  452. SBOX[3][1966080] = 0x40; // 1e0000 (2, 14) = 8
  453. SBOX[3][2031616] = 0x1000; // 1f0000 (2, 15) = 4
  454. SBOX[3][1081344] = 0x80400000; // 108000 (3, 0) = 3
  455. SBOX[3][1146880] = 0x80401040; // 118000 (3, 1) = 15
  456. SBOX[3][1212416] = 0x0; // 128000 (3, 2) = 0
  457. SBOX[3][1277952] = 0x401000; // 138000 (3, 3) = 6
  458. SBOX[3][1343488] = 0x400040; // 148000 (3, 4) = 10
  459. SBOX[3][1409024] = 0x80000000; // 158000 (3, 5) = 1
  460. SBOX[3][1474560] = 0x80001040; // 168000 (3, 6) = 13
  461. SBOX[3][1540096] = 0x40; // 178000 (3, 7) = 8
  462. SBOX[3][1605632] = 0x80000040; // 188000 (3, 8) = 9
  463. SBOX[3][1671168] = 0x1000; // 198000 (3, 9) = 4
  464. SBOX[3][1736704] = 0x80001000; // 1a8000 (3, 10) = 5
  465. SBOX[3][1802240] = 0x80400040; // 1b8000 (3, 11) = 11
  466. SBOX[3][1867776] = 0x1040; // 1c8000 (3, 12) = 12
  467. SBOX[3][1933312] = 0x80401000; // 1d8000 (3, 13) = 7
  468. SBOX[3][1998848] = 0x400000; // 1e8000 (3, 14) = 2
  469. SBOX[3][2064384] = 0x401040; // 1f8000 (3, 15) = 14
  470. SBOX[4] = new Array();
  471. SBOX[4][0] = 0x80; // 0 (0, 0) = 2
  472. SBOX[4][4096] = 0x1040000; // 1000 (0, 1) = 12
  473. SBOX[4][8192] = 0x40000; // 2000 (0, 2) = 4
  474. SBOX[4][12288] = 0x20000000; // 3000 (0, 3) = 1
  475. SBOX[4][16384] = 0x20040080; // 4000 (0, 4) = 7
  476. SBOX[4][20480] = 0x1000080; // 5000 (0, 5) = 10
  477. SBOX[4][24576] = 0x21000080; // 6000 (0, 6) = 11
  478. SBOX[4][28672] = 0x40080; // 7000 (0, 7) = 6
  479. SBOX[4][32768] = 0x1000000; // 8000 (0, 8) = 8
  480. SBOX[4][36864] = 0x20040000; // 9000 (0, 9) = 5
  481. SBOX[4][40960] = 0x20000080; // a000 (0, 10) = 3
  482. SBOX[4][45056] = 0x21040080; // b000 (0, 11) = 15
  483. SBOX[4][49152] = 0x21040000; // c000 (0, 12) = 13
  484. SBOX[4][53248] = 0x0; // d000 (0, 13) = 0
  485. SBOX[4][57344] = 0x1040080; // e000 (0, 14) = 14
  486. SBOX[4][61440] = 0x21000000; // f000 (0, 15) = 9
  487. SBOX[4][2048] = 0x1040080; // 800 (1, 0) = 14
  488. SBOX[4][6144] = 0x21000080; // 1800 (1, 1) = 11
  489. SBOX[4][10240] = 0x80; // 2800 (1, 2) = 2
  490. SBOX[4][14336] = 0x1040000; // 3800 (1, 3) = 12
  491. SBOX[4][18432] = 0x40000; // 4800 (1, 4) = 4
  492. SBOX[4][22528] = 0x20040080; // 5800 (1, 5) = 7
  493. SBOX[4][26624] = 0x21040000; // 6800 (1, 6) = 13
  494. SBOX[4][30720] = 0x20000000; // 7800 (1, 7) = 1
  495. SBOX[4][34816] = 0x20040000; // 8800 (1, 8) = 5
  496. SBOX[4][38912] = 0x0; // 9800 (1, 9) = 0
  497. SBOX[4][43008] = 0x21040080; // a800 (1, 10) = 15
  498. SBOX[4][47104] = 0x1000080; // b800 (1, 11) = 10
  499. SBOX[4][51200] = 0x20000080; // c800 (1, 12) = 3
  500. SBOX[4][55296] = 0x21000000; // d800 (1, 13) = 9
  501. SBOX[4][59392] = 0x1000000; // e800 (1, 14) = 8
  502. SBOX[4][63488] = 0x40080; // f800 (1, 15) = 6
  503. SBOX[4][65536] = 0x40000; // 10000 (2, 0) = 4
  504. SBOX[4][69632] = 0x80; // 11000 (2, 1) = 2
  505. SBOX[4][73728] = 0x20000000; // 12000 (2, 2) = 1
  506. SBOX[4][77824] = 0x21000080; // 13000 (2, 3) = 11
  507. SBOX[4][81920] = 0x1000080; // 14000 (2, 4) = 10
  508. SBOX[4][86016] = 0x21040000; // 15000 (2, 5) = 13
  509. SBOX[4][90112] = 0x20040080; // 16000 (2, 6) = 7
  510. SBOX[4][94208] = 0x1000000; // 17000 (2, 7) = 8
  511. SBOX[4][98304] = 0x21040080; // 18000 (2, 8) = 15
  512. SBOX[4][102400] = 0x21000000; // 19000 (2, 9) = 9
  513. SBOX[4][106496] = 0x1040000; // 1a000 (2, 10) = 12
  514. SBOX[4][110592] = 0x20040000; // 1b000 (2, 11) = 5
  515. SBOX[4][114688] = 0x40080; // 1c000 (2, 12) = 6
  516. SBOX[4][118784] = 0x20000080; // 1d000 (2, 13) = 3
  517. SBOX[4][122880] = 0x0; // 1e000 (2, 14) = 0
  518. SBOX[4][126976] = 0x1040080; // 1f000 (2, 15) = 14
  519. SBOX[4][67584] = 0x21000080; // 10800 (3, 0) = 11
  520. SBOX[4][71680] = 0x1000000; // 11800 (3, 1) = 8
  521. SBOX[4][75776] = 0x1040000; // 12800 (3, 2) = 12
  522. SBOX[4][79872] = 0x20040080; // 13800 (3, 3) = 7
  523. SBOX[4][83968] = 0x20000000; // 14800 (3, 4) = 1
  524. SBOX[4][88064] = 0x1040080; // 15800 (3, 5) = 14
  525. SBOX[4][92160] = 0x80; // 16800 (3, 6) = 2
  526. SBOX[4][96256] = 0x21040000; // 17800 (3, 7) = 13
  527. SBOX[4][100352] = 0x40080; // 18800 (3, 8) = 6
  528. SBOX[4][104448] = 0x21040080; // 19800 (3, 9) = 15
  529. SBOX[4][108544] = 0x0; // 1a800 (3, 10) = 0
  530. SBOX[4][112640] = 0x21000000; // 1b800 (3, 11) = 9
  531. SBOX[4][116736] = 0x1000080; // 1c800 (3, 12) = 10
  532. SBOX[4][120832] = 0x40000; // 1d800 (3, 13) = 4
  533. SBOX[4][124928] = 0x20040000; // 1e800 (3, 14) = 5
  534. SBOX[4][129024] = 0x20000080; // 1f800 (3, 15) = 3
  535. SBOX[5] = new Array();
  536. SBOX[5][0] = 0x10000008; // 0 (0, 0) = 12
  537. SBOX[5][256] = 0x2000; // 100 (0, 1) = 1
  538. SBOX[5][512] = 0x10200000; // 200 (0, 2) = 10
  539. SBOX[5][768] = 0x10202008; // 300 (0, 3) = 15
  540. SBOX[5][1024] = 0x10002000; // 400 (0, 4) = 9
  541. SBOX[5][1280] = 0x200000; // 500 (0, 5) = 2
  542. SBOX[5][1536] = 0x200008; // 600 (0, 6) = 6
  543. SBOX[5][1792] = 0x10000000; // 700 (0, 7) = 8
  544. SBOX[5][2048] = 0x0; // 800 (0, 8) = 0
  545. SBOX[5][2304] = 0x10002008; // 900 (0, 9) = 13
  546. SBOX[5][2560] = 0x202000; // a00 (0, 10) = 3
  547. SBOX[5][2816] = 0x8; // b00 (0, 11) = 4
  548. SBOX[5][3072] = 0x10200008; // c00 (0, 12) = 14
  549. SBOX[5][3328] = 0x202008; // d00 (0, 13) = 7
  550. SBOX[5][3584] = 0x2008; // e00 (0, 14) = 5
  551. SBOX[5][3840] = 0x10202000; // f00 (0, 15) = 11
  552. SBOX[5][128] = 0x10200000; // 80 (1, 0) = 10
  553. SBOX[5][384] = 0x10202008; // 180 (1, 1) = 15
  554. SBOX[5][640] = 0x8; // 280 (1, 2) = 4
  555. SBOX[5][896] = 0x200000; // 380 (1, 3) = 2
  556. SBOX[5][1152] = 0x202008; // 480 (1, 4) = 7
  557. SBOX[5][1408] = 0x10000008; // 580 (1, 5) = 12
  558. SBOX[5][1664] = 0x10002000; // 680 (1, 6) = 9
  559. SBOX[5][1920] = 0x2008; // 780 (1, 7) = 5
  560. SBOX[5][2176] = 0x200008; // 880 (1, 8) = 6
  561. SBOX[5][2432] = 0x2000; // 980 (1, 9) = 1
  562. SBOX[5][2688] = 0x10002008; // a80 (1, 10) = 13
  563. SBOX[5][2944] = 0x10200008; // b80 (1, 11) = 14
  564. SBOX[5][3200] = 0x0; // c80 (1, 12) = 0
  565. SBOX[5][3456] = 0x10202000; // d80 (1, 13) = 11
  566. SBOX[5][3712] = 0x202000; // e80 (1, 14) = 3
  567. SBOX[5][3968] = 0x10000000; // f80 (1, 15) = 8
  568. SBOX[5][4096] = 0x10002000; // 1000 (2, 0) = 9
  569. SBOX[5][4352] = 0x10200008; // 1100 (2, 1) = 14
  570. SBOX[5][4608] = 0x10202008; // 1200 (2, 2) = 15
  571. SBOX[5][4864] = 0x2008; // 1300 (2, 3) = 5
  572. SBOX[5][5120] = 0x200000; // 1400 (2, 4) = 2
  573. SBOX[5][5376] = 0x10000000; // 1500 (2, 5) = 8
  574. SBOX[5][5632] = 0x10000008; // 1600 (2, 6) = 12
  575. SBOX[5][5888] = 0x202000; // 1700 (2, 7) = 3
  576. SBOX[5][6144] = 0x202008; // 1800 (2, 8) = 7
  577. SBOX[5][6400] = 0x0; // 1900 (2, 9) = 0
  578. SBOX[5][6656] = 0x8; // 1a00 (2, 10) = 4
  579. SBOX[5][6912] = 0x10200000; // 1b00 (2, 11) = 10
  580. SBOX[5][7168] = 0x2000; // 1c00 (2, 12) = 1
  581. SBOX[5][7424] = 0x10002008; // 1d00 (2, 13) = 13
  582. SBOX[5][7680] = 0x10202000; // 1e00 (2, 14) = 11
  583. SBOX[5][7936] = 0x200008; // 1f00 (2, 15) = 6
  584. SBOX[5][4224] = 0x8; // 1080 (3, 0) = 4
  585. SBOX[5][4480] = 0x202000; // 1180 (3, 1) = 3
  586. SBOX[5][4736] = 0x200000; // 1280 (3, 2) = 2
  587. SBOX[5][4992] = 0x10000008; // 1380 (3, 3) = 12
  588. SBOX[5][5248] = 0x10002000; // 1480 (3, 4) = 9
  589. SBOX[5][5504] = 0x2008; // 1580 (3, 5) = 5
  590. SBOX[5][5760] = 0x10202008; // 1680 (3, 6) = 15
  591. SBOX[5][6016] = 0x10200000; // 1780 (3, 7) = 10
  592. SBOX[5][6272] = 0x10202000; // 1880 (3, 8) = 11
  593. SBOX[5][6528] = 0x10200008; // 1980 (3, 9) = 14
  594. SBOX[5][6784] = 0x2000; // 1a80 (3, 10) = 1
  595. SBOX[5][7040] = 0x202008; // 1b80 (3, 11) = 7
  596. SBOX[5][7296] = 0x200008; // 1c80 (3, 12) = 6
  597. SBOX[5][7552] = 0x0; // 1d80 (3, 13) = 0
  598. SBOX[5][7808] = 0x10000000; // 1e80 (3, 14) = 8
  599. SBOX[5][8064] = 0x10002008; // 1f80 (3, 15) = 13
  600. SBOX[6] = new Array();
  601. SBOX[6][0] = 0x100000; // 0 (0, 0) = 4
  602. SBOX[6][16] = 0x2000401; // 10 (0, 1) = 11
  603. SBOX[6][32] = 0x400; // 20 (0, 2) = 2
  604. SBOX[6][48] = 0x100401; // 30 (0, 3) = 14
  605. SBOX[6][64] = 0x2100401; // 40 (0, 4) = 15
  606. SBOX[6][80] = 0x0; // 50 (0, 5) = 0
  607. SBOX[6][96] = 0x1; // 60 (0, 6) = 8
  608. SBOX[6][112] = 0x2100001; // 70 (0, 7) = 13
  609. SBOX[6][128] = 0x2000400; // 80 (0, 8) = 3
  610. SBOX[6][144] = 0x100001; // 90 (0, 9) = 12
  611. SBOX[6][160] = 0x2000001; // a0 (0, 10) = 9
  612. SBOX[6][176] = 0x2100400; // b0 (0, 11) = 7
  613. SBOX[6][192] = 0x2100000; // c0 (0, 12) = 5
  614. SBOX[6][208] = 0x401; // d0 (0, 13) = 10
  615. SBOX[6][224] = 0x100400; // e0 (0, 14) = 6
  616. SBOX[6][240] = 0x2000000; // f0 (0, 15) = 1
  617. SBOX[6][8] = 0x2100001; // 8 (1, 0) = 13
  618. SBOX[6][24] = 0x0; // 18 (1, 1) = 0
  619. SBOX[6][40] = 0x2000401; // 28 (1, 2) = 11
  620. SBOX[6][56] = 0x2100400; // 38 (1, 3) = 7
  621. SBOX[6][72] = 0x100000; // 48 (1, 4) = 4
  622. SBOX[6][88] = 0x2000001; // 58 (1, 5) = 9
  623. SBOX[6][104] = 0x2000000; // 68 (1, 6) = 1
  624. SBOX[6][120] = 0x401; // 78 (1, 7) = 10
  625. SBOX[6][136] = 0x100401; // 88 (1, 8) = 14
  626. SBOX[6][152] = 0x2000400; // 98 (1, 9) = 3
  627. SBOX[6][168] = 0x2100000; // a8 (1, 10) = 5
  628. SBOX[6][184] = 0x100001; // b8 (1, 11) = 12
  629. SBOX[6][200] = 0x400; // c8 (1, 12) = 2
  630. SBOX[6][216] = 0x2100401; // d8 (1, 13) = 15
  631. SBOX[6][232] = 0x1; // e8 (1, 14) = 8
  632. SBOX[6][248] = 0x100400; // f8 (1, 15) = 6
  633. SBOX[6][256] = 0x2000000; // 100 (2, 0) = 1
  634. SBOX[6][272] = 0x100000; // 110 (2, 1) = 4
  635. SBOX[6][288] = 0x2000401; // 120 (2, 2) = 11
  636. SBOX[6][304] = 0x2100001; // 130 (2, 3) = 13
  637. SBOX[6][320] = 0x100001; // 140 (2, 4) = 12
  638. SBOX[6][336] = 0x2000400; // 150 (2, 5) = 3
  639. SBOX[6][352] = 0x2100400; // 160 (2, 6) = 7
  640. SBOX[6][368] = 0x100401; // 170 (2, 7) = 14
  641. SBOX[6][384] = 0x401; // 180 (2, 8) = 10
  642. SBOX[6][400] = 0x2100401; // 190 (2, 9) = 15
  643. SBOX[6][416] = 0x100400; // 1a0 (2, 10) = 6
  644. SBOX[6][432] = 0x1; // 1b0 (2, 11) = 8
  645. SBOX[6][448] = 0x0; // 1c0 (2, 12) = 0
  646. SBOX[6][464] = 0x2100000; // 1d0 (2, 13) = 5
  647. SBOX[6][480] = 0x2000001; // 1e0 (2, 14) = 9
  648. SBOX[6][496] = 0x400; // 1f0 (2, 15) = 2
  649. SBOX[6][264] = 0x100400; // 108 (3, 0) = 6
  650. SBOX[6][280] = 0x2000401; // 118 (3, 1) = 11
  651. SBOX[6][296] = 0x2100001; // 128 (3, 2) = 13
  652. SBOX[6][312] = 0x1; // 138 (3, 3) = 8
  653. SBOX[6][328] = 0x2000000; // 148 (3, 4) = 1
  654. SBOX[6][344] = 0x100000; // 158 (3, 5) = 4
  655. SBOX[6][360] = 0x401; // 168 (3, 6) = 10
  656. SBOX[6][376] = 0x2100400; // 178 (3, 7) = 7
  657. SBOX[6][392] = 0x2000001; // 188 (3, 8) = 9
  658. SBOX[6][408] = 0x2100000; // 198 (3, 9) = 5
  659. SBOX[6][424] = 0x0; // 1a8 (3, 10) = 0
  660. SBOX[6][440] = 0x2100401; // 1b8 (3, 11) = 15
  661. SBOX[6][456] = 0x100401; // 1c8 (3, 12) = 14
  662. SBOX[6][472] = 0x400; // 1d8 (3, 13) = 2
  663. SBOX[6][488] = 0x2000400; // 1e8 (3, 14) = 3
  664. SBOX[6][504] = 0x100001; // 1f8 (3, 15) = 12
  665. SBOX[7] = new Array();
  666. SBOX[7][0] = 0x8000820; // 0 (0, 0) = 13
  667. SBOX[7][1] = 0x20000; // 1 (0, 1) = 2
  668. SBOX[7][2] = 0x8000000; // 2 (0, 2) = 8
  669. SBOX[7][3] = 0x20; // 3 (0, 3) = 4
  670. SBOX[7][4] = 0x20020; // 4 (0, 4) = 6
  671. SBOX[7][5] = 0x8020820; // 5 (0, 5) = 15
  672. SBOX[7][6] = 0x8020800; // 6 (0, 6) = 11
  673. SBOX[7][7] = 0x800; // 7 (0, 7) = 1
  674. SBOX[7][8] = 0x8020000; // 8 (0, 8) = 10
  675. SBOX[7][9] = 0x8000800; // 9 (0, 9) = 9
  676. SBOX[7][10] = 0x20800; // a (0, 10) = 3
  677. SBOX[7][11] = 0x8020020; // b (0, 11) = 14
  678. SBOX[7][12] = 0x820; // c (0, 12) = 5
  679. SBOX[7][13] = 0x0; // d (0, 13) = 0
  680. SBOX[7][14] = 0x8000020; // e (0, 14) = 12
  681. SBOX[7][15] = 0x20820; // f (0, 15) = 7
  682. SBOX[7][-2147483648] = 0x800; // 80000000 (1, 0) = 1
  683. SBOX[7][-2147483647] = 0x8020820; // 80000001 (1, 1) = 15
  684. SBOX[7][-2147483646] = 0x8000820; // 80000002 (1, 2) = 13
  685. SBOX[7][-2147483645] = 0x8000000; // 80000003 (1, 3) = 8
  686. SBOX[7][-2147483644] = 0x8020000; // 80000004 (1, 4) = 10
  687. SBOX[7][-2147483643] = 0x20800; // 80000005 (1, 5) = 3
  688. SBOX[7][-2147483642] = 0x20820; // 80000006 (1, 6) = 7
  689. SBOX[7][-2147483641] = 0x20; // 80000007 (1, 7) = 4
  690. SBOX[7][-2147483640] = 0x8000020; // 80000008 (1, 8) = 12
  691. SBOX[7][-2147483639] = 0x820; // 80000009 (1, 9) = 5
  692. SBOX[7][-2147483638] = 0x20020; // 8000000a (1, 10) = 6
  693. SBOX[7][-2147483637] = 0x8020800; // 8000000b (1, 11) = 11
  694. SBOX[7][-2147483636] = 0x0; // 8000000c (1, 12) = 0
  695. SBOX[7][-2147483635] = 0x8020020; // 8000000d (1, 13) = 14
  696. SBOX[7][-2147483634] = 0x8000800; // 8000000e (1, 14) = 9
  697. SBOX[7][-2147483633] = 0x20000; // 8000000f (1, 15) = 2
  698. SBOX[7][16] = 0x20820; // 10 (2, 0) = 7
  699. SBOX[7][17] = 0x8020800; // 11 (2, 1) = 11
  700. SBOX[7][18] = 0x20; // 12 (2, 2) = 4
  701. SBOX[7][19] = 0x800; // 13 (2, 3) = 1
  702. SBOX[7][20] = 0x8000800; // 14 (2, 4) = 9
  703. SBOX[7][21] = 0x8000020; // 15 (2, 5) = 12
  704. SBOX[7][22] = 0x8020020; // 16 (2, 6) = 14
  705. SBOX[7][23] = 0x20000; // 17 (2, 7) = 2
  706. SBOX[7][24] = 0x0; // 18 (2, 8) = 0
  707. SBOX[7][25] = 0x20020; // 19 (2, 9) = 6
  708. SBOX[7][26] = 0x8020000; // 1a (2, 10) = 10
  709. SBOX[7][27] = 0x8000820; // 1b (2, 11) = 13
  710. SBOX[7][28] = 0x8020820; // 1c (2, 12) = 15
  711. SBOX[7][29] = 0x20800; // 1d (2, 13) = 3
  712. SBOX[7][30] = 0x820; // 1e (2, 14) = 5
  713. SBOX[7][31] = 0x8000000; // 1f (2, 15) = 8
  714. SBOX[7][-2147483632] = 0x20000; // 80000010 (3, 0) = 2
  715. SBOX[7][-2147483631] = 0x800; // 80000011 (3, 1) = 1
  716. SBOX[7][-2147483630] = 0x8020020; // 80000012 (3, 2) = 14
  717. SBOX[7][-2147483629] = 0x20820; // 80000013 (3, 3) = 7
  718. SBOX[7][-2147483628] = 0x20; // 80000014 (3, 4) = 4
  719. SBOX[7][-2147483627] = 0x8020000; // 80000015 (3, 5) = 10
  720. SBOX[7][-2147483626] = 0x8000000; // 80000016 (3, 6) = 8
  721. SBOX[7][-2147483625] = 0x8000820; // 80000017 (3, 7) = 13
  722. SBOX[7][-2147483624] = 0x8020820; // 80000018 (3, 8) = 15
  723. SBOX[7][-2147483623] = 0x8000020; // 80000019 (3, 9) = 12
  724. SBOX[7][-2147483622] = 0x8000800; // 8000001a (3, 10) = 9
  725. SBOX[7][-2147483621] = 0x0; // 8000001b (3, 11) = 0
  726. SBOX[7][-2147483620] = 0x20800; // 8000001c (3, 12) = 3
  727. SBOX[7][-2147483619] = 0x820; // 8000001d (3, 13) = 5
  728. SBOX[7][-2147483618] = 0x20020; // 8000001e (3, 14) = 6
  729. SBOX[7][-2147483617] = 0x8020800; // 8000001f (3, 15) = 11
  730. State.prototype._exchangeLR = function(v, m) {
  731. var t = ((this.lhs >> v) ^ this.rhs) & m;
  732. this.rhs ^= t;
  733. this.lhs ^= (t << v);
  734. };
  735. State.prototype._exchangeRL = function(v, m) {
  736. var t = ((this.rhs >> v) ^ this.lhs) & m;
  737. this.lhs ^= t;
  738. this.rhs ^= (t << v);
  739. };
  740. /**
  741. * Perform the initial permutation of the input to create the starting state
  742. * of the algorithm. The initial permutation maps each consecutive bit of
  743. * the input into a different byte of the state.
  744. *
  745. * <pre>
  746. * The initial permutation is defined to be:
  747. *
  748. * 58 50 42 34 26 18 10 2
  749. * 60 52 44 36 28 20 12 4
  750. * 62 54 46 38 30 22 14 6
  751. * 64 56 48 40 32 24 16 8
  752. * 57 49 41 33 25 17 9 1
  753. * 59 51 43 35 27 19 11 3
  754. * 61 53 45 37 29 21 13 5
  755. * 63 55 47 39 31 23 15 7
  756. * </pre>
  757. *
  758. *
  759. * @param message
  760. * The message as an array of unsigned bytes.
  761. * @param offset
  762. * The offset into the message that the current 64-bit block
  763. * begins.
  764. * @returns the initial engine state
  765. */
  766. State.prototype.initialPerm = function(message, offset) {
  767. var input = message.slice(offset, offset + 8);
  768. this.lhs = (input[0] << 24) + (input[1] << 16) + (input[2] << 8)
  769. + input[3];
  770. this.rhs = (input[4] << 24) + (input[5] << 16) + (input[6] << 8)
  771. + input[7];
  772. this._exchangeLR(4, 0x0f0f0f0f);
  773. this._exchangeLR(16, 0x0000ffff);
  774. this._exchangeRL(2, 0x33333333);
  775. this._exchangeRL(8, 0x00ff00ff);
  776. this._exchangeLR(1, 0x55555555);
  777. };
  778. /**
  779. * Perform one round of the DES algorithm using the given key. A round is
  780. * defined as:
  781. *
  782. * <pre>
  783. * L&amp;rsquo = R
  784. * R&amp;rsquo = L &circ; f(R, k)
  785. * </pre>
  786. *
  787. * where f consists of expanding, XORing with the key and contracting back
  788. * with the SBOXes.
  789. *
  790. * Note that the final round is defined slightly differently as:
  791. *
  792. * <pre>
  793. * L&amp;rsquo = L &circ; f(R, k)
  794. * R&amp;rsquo = R
  795. * </pre>
  796. *
  797. * Therefore in the final round this function produces LHS and RHS the wrong
  798. * way around.
  799. *
  800. * @param k
  801. * the key
  802. */
  803. State.prototype.round = function(k) {
  804. var r = this.rhs, l = this.lhs;
  805. var f = 0;
  806. for ( var i = 0; i < 8; i++) {
  807. var v = (r ^ k[i]) & State.SBOX_MASK[i];
  808. f += State.SBOX[i][v];
  809. }
  810. this.lhs = r;
  811. this.rhs = l ^ f;
  812. };
  813. /**
  814. * Apply the inverse of the initial permutation.
  815. *
  816. * <pre>
  817. * The inverse is defined to be:
  818. *
  819. * 40 8 48 16 56 24 64 32
  820. * 39 7 47 15 55 23 63 31
  821. * 38 6 46 14 54 22 62 30
  822. * 37 5 45 13 53 21 61 29
  823. * 36 4 44 12 52 20 60 28
  824. * 35 3 43 11 51 19 59 27
  825. * 34 2 42 10 50 18 58 26
  826. * 33 1 41 9 49 17 57 25
  827. * </pre>
  828. *
  829. * @param cipherText
  830. * @param offset
  831. */
  832. State.prototype.finalPerm = function(cipherText, offset) {
  833. var t = this.lhs;
  834. this.lhs = this.rhs;
  835. this.rhs = t;
  836. this._exchangeLR(1, 0x55555555);
  837. this._exchangeRL(8, 0x00ff00ff);
  838. this._exchangeRL(2, 0x33333333);
  839. this._exchangeLR(16, 0x0000ffff);
  840. this._exchangeLR(4, 0x0f0f0f0f);
  841. cipherText[offset] = (this.lhs >> 24) & 0xff;
  842. cipherText[offset + 1] = (this.lhs >> 16) & 0xff;
  843. cipherText[offset + 2] = (this.lhs >> 8) & 0xff;
  844. cipherText[offset + 3] = (this.lhs) & 0xff;
  845. cipherText[offset + 4] = (this.rhs >> 24) & 0xff;
  846. cipherText[offset + 5] = (this.rhs >> 16) & 0xff;
  847. cipherText[offset + 6] = (this.rhs >> 8) & 0xff;
  848. cipherText[offset + 7] = (this.rhs) & 0xff;
  849. };
  850. /**
  851. * DES cipher
  852. */
  853. var DES = C.DES = {
  854. _blocksize : 2,
  855. _keyschedule : null,
  856. _state : new State(),
  857. _init : function(k) {
  858. this._keyschedule = new KeySchedule(k);
  859. },
  860. encrypt : function(message, password, options) {
  861. options = options || {};
  862. // Determine mode
  863. var mode = options.mode || new C.mode.OFB;
  864. // Allow mode to override options
  865. if (mode.fixOptions)
  866. mode.fixOptions(options);
  867. var
  868. // Convert to bytes if message is a string
  869. m = (message.constructor == String ? UTF8.stringToBytes(message)
  870. : message),
  871. // Generate random IV
  872. iv = options.iv || util.randomBytes(8),
  873. // Generate key
  874. k = (password.constructor == String ?
  875. // Derive key from passphrase
  876. C.PBKDF2(password, iv, 8, {
  877. asBytes : true
  878. }) :
  879. // else, assume byte array representing cryptographic key
  880. password);
  881. // Create key schedule
  882. this._keyschedule = new KeySchedule(k);
  883. // Encrypt
  884. mode.encrypt(DES, m, iv);
  885. // Return ciphertext
  886. m = options.iv ? m : iv.concat(m);
  887. return (options && options.asBytes) ? m : util.bytesToBase64(m);
  888. },
  889. _encryptblock : function(message, offset) {
  890. this._state.initialPerm(message, offset);
  891. for ( var i = 0; i <= 15; i++) {
  892. this._state.round(this._keyschedule.getKey(i));
  893. }
  894. this._state.finalPerm(message, offset);
  895. },
  896. decrypt : function(ciphertext, password, options) {
  897. options = options || {};
  898. // Determine mode
  899. var mode = options.mode || new C.mode.OFB;
  900. // Allow mode to override options
  901. if (mode.fixOptions)
  902. mode.fixOptions(options);
  903. var
  904. // Convert to bytes if ciphertext is a string
  905. c = (ciphertext.constructor == String ? util
  906. .base64ToBytes(ciphertext) : ciphertext),
  907. // Separate IV and message
  908. iv = options.iv || c.splice(0, 8),
  909. // Generate key
  910. k = (password.constructor == String ?
  911. // Derive key from passphrase
  912. C.PBKDF2(password, iv, 32, {
  913. asBytes : true
  914. }) :
  915. // else, assume byte array representing cryptographic key
  916. password);
  917. // Create key schedule
  918. this._keyschedule = new KeySchedule(k);
  919. mode.decrypt(DES, c, iv);
  920. // Return plaintext
  921. return (options && options.asBytes) ? c : UTF8.bytesToString(c);
  922. },
  923. _decryptblock : function(message, offset) {
  924. this._state.initialPerm(message, offset);
  925. for ( var i = 15; i >= 0; i--) {
  926. this._state.round(this._keyschedule.getKey(i));
  927. }
  928. this._state.finalPerm(message, offset);
  929. }
  930. };
  931. })();