DES.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. namespace SharpCifs.Util
  19. {
  20. /// <summary>
  21. /// This code is derived from the above source
  22. /// JCIFS API
  23. /// Norbert Hranitzky
  24. /// <p>and modified again by Michael B.
  25. /// </summary>
  26. /// <remarks>
  27. /// This code is derived from the above source
  28. /// JCIFS API
  29. /// Norbert Hranitzky
  30. /// <p>and modified again by Michael B. Allen
  31. /// </remarks>
  32. public class DES
  33. {
  34. private int[] _encryptKeys = new int[32];
  35. private int[] _decryptKeys = new int[32];
  36. private int[] _tempInts = new int[2];
  37. public DES()
  38. {
  39. }
  40. public DES(byte[] key)
  41. {
  42. // DesCipher - the DES encryption method
  43. //
  44. // The meat of this code is by Dave Zimmerman <dzimm@widget.com>, and is:
  45. //
  46. // Copyright (c) 1996 Widget Workshop, Inc. All Rights Reserved.
  47. //
  48. // Permission to use, copy, modify, and distribute this software
  49. // and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  50. // without fee is hereby granted, provided that this copyright notice is kept
  51. // intact.
  52. //
  53. // WIDGET WORKSHOP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  54. // OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  55. // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  56. // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WIDGET WORKSHOP SHALL NOT BE LIABLE
  57. // FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  58. // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  59. //
  60. // THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  61. // CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  62. // PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  63. // NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  64. // SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  65. // SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  66. // PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). WIDGET WORKSHOP
  67. // SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  68. // HIGH RISK ACTIVITIES.
  69. //
  70. //
  71. // The rest is:
  72. //
  73. // Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
  74. //
  75. // Copyright (C) 1996 by Wolfgang Platzer
  76. // email: wplatzer@iaik.tu-graz.ac.at
  77. //
  78. // All rights reserved.
  79. //
  80. // Redistribution and use in source and binary forms, with or without
  81. // modification, are permitted provided that the following conditions
  82. // are met:
  83. // 1. Redistributions of source code must retain the above copyright
  84. // notice, this list of conditions and the following disclaimer.
  85. // 2. Redistributions in binary form must reproduce the above copyright
  86. // notice, this list of conditions and the following disclaimer in the
  87. // documentation and/or other materials provided with the distribution.
  88. //
  89. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  90. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  91. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  92. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  93. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  94. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  95. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  96. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  97. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  98. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  99. // SUCH DAMAGE.
  100. //
  101. // Constructor, byte-array key.
  102. if (key.Length == 7)
  103. {
  104. byte[] key8 = new byte[8];
  105. MakeSmbKey(key, key8);
  106. SetKey(key8);
  107. }
  108. else
  109. {
  110. SetKey(key);
  111. }
  112. }
  113. public static void MakeSmbKey(byte[] key7, byte[] key8)
  114. {
  115. int i;
  116. key8[0] = unchecked((byte)((key7[0] >> 1) & unchecked(0xff)));
  117. key8[1] = unchecked((byte)((((key7[0] & unchecked(0x01)) << 6) | (((key7[1
  118. ] & unchecked(0xff)) >> 2) & unchecked(0xff))) & unchecked(0xff)));
  119. key8[2] = unchecked((byte)((((key7[1] & unchecked(0x03)) << 5) | (((key7[2
  120. ] & unchecked(0xff)) >> 3) & unchecked(0xff))) & unchecked(0xff)));
  121. key8[3] = unchecked((byte)((((key7[2] & unchecked(0x07)) << 4) | (((key7[3
  122. ] & unchecked(0xff)) >> 4) & unchecked(0xff))) & unchecked(0xff)));
  123. key8[4] = unchecked((byte)((((key7[3] & unchecked(0x0F)) << 3) | (((key7[4
  124. ] & unchecked(0xff)) >> 5) & unchecked(0xff))) & unchecked(0xff)));
  125. key8[5] = unchecked((byte)((((key7[4] & unchecked(0x1F)) << 2) | (((key7[5
  126. ] & unchecked(0xff)) >> 6) & unchecked(0xff))) & unchecked(0xff)));
  127. key8[6] = unchecked((byte)((((key7[5] & unchecked(0x3F)) << 1) | (((key7[6
  128. ] & unchecked(0xff)) >> 7) & unchecked(0xff))) & unchecked(0xff)));
  129. key8[7] = unchecked((byte)(key7[6] & unchecked(0x7F)));
  130. for (i = 0; i < 8; i++)
  131. {
  132. key8[i] = unchecked((byte)(key8[i] << 1));
  133. }
  134. }
  135. /// Set the key.
  136. public virtual void SetKey(byte[] key)
  137. {
  138. // CHECK PAROTY TBD
  139. Deskey(key, true, _encryptKeys);
  140. Deskey(key, false, _decryptKeys);
  141. }
  142. // Turn an 8-byte key into internal keys.
  143. private void Deskey(byte[] keyBlock, bool encrypting, int[] knL)
  144. {
  145. int i;
  146. int j;
  147. int l;
  148. int m;
  149. int n;
  150. int[] pc1M = new int[56];
  151. int[] pcr = new int[56];
  152. int[] kn = new int[32];
  153. for (j = 0; j < 56; ++j)
  154. {
  155. l = _pc1[j];
  156. m = l & 0x7;
  157. pc1M[j] = ((keyBlock[(int)(((uint)l) >> 3)] & _bytebit[m]) != 0) ? 1 : 0;
  158. }
  159. for (i = 0; i < 16; ++i)
  160. {
  161. if (encrypting)
  162. {
  163. m = i << 1;
  164. }
  165. else
  166. {
  167. m = (15 - i) << 1;
  168. }
  169. n = m + 1;
  170. kn[m] = kn[n] = 0;
  171. for (j = 0; j < 28; ++j)
  172. {
  173. l = j + _totrot[i];
  174. if (l < 28)
  175. {
  176. pcr[j] = pc1M[l];
  177. }
  178. else
  179. {
  180. pcr[j] = pc1M[l - 28];
  181. }
  182. }
  183. for (j = 28; j < 56; ++j)
  184. {
  185. l = j + _totrot[i];
  186. if (l < 56)
  187. {
  188. pcr[j] = pc1M[l];
  189. }
  190. else
  191. {
  192. pcr[j] = pc1M[l - 28];
  193. }
  194. }
  195. for (j = 0; j < 24; ++j)
  196. {
  197. if (pcr[_pc2[j]] != 0)
  198. {
  199. kn[m] |= _bigbyte[j];
  200. }
  201. if (pcr[_pc2[j + 24]] != 0)
  202. {
  203. kn[n] |= _bigbyte[j];
  204. }
  205. }
  206. }
  207. Cookey(kn, knL);
  208. }
  209. private void Cookey(int[] raw, int[] knL)
  210. {
  211. int raw0;
  212. int raw1;
  213. int rawi;
  214. int knLi;
  215. int i;
  216. for (i = 0, rawi = 0, knLi = 0; i < 16; ++i)
  217. {
  218. raw0 = raw[rawi++];
  219. raw1 = raw[rawi++];
  220. knL[knLi] = (raw0 & unchecked(0x00fc0000)) << 6;
  221. knL[knLi] |= (raw0 & unchecked(0x00000fc0)) << 10;
  222. knL[knLi] |= (int)(((uint)(raw1 & unchecked(0x00fc0000))) >> 10);
  223. knL[knLi] |= (int)(((uint)(raw1 & unchecked(0x00000fc0))) >> 6);
  224. ++knLi;
  225. knL[knLi] = (raw0 & unchecked(0x0003f000)) << 12;
  226. knL[knLi] |= (raw0 & unchecked(0x0000003f)) << 16;
  227. knL[knLi] |= (int)(((uint)(raw1 & unchecked(0x0003f000))) >> 4);
  228. knL[knLi] |= (raw1 & unchecked(0x0000003f));
  229. ++knLi;
  230. }
  231. }
  232. /// Encrypt a block of eight bytes.
  233. private void Encrypt(byte[] clearText, int clearOff, byte[] cipherText, int cipherOff
  234. )
  235. {
  236. SquashBytesToInts(clearText, clearOff, _tempInts, 0, 2);
  237. Des(_tempInts, _tempInts, _encryptKeys);
  238. SpreadIntsToBytes(_tempInts, 0, cipherText, cipherOff, 2);
  239. }
  240. /// Decrypt a block of eight bytes.
  241. private void Decrypt(byte[] cipherText, int cipherOff, byte[] clearText, int clearOff
  242. )
  243. {
  244. SquashBytesToInts(cipherText, cipherOff, _tempInts, 0, 2);
  245. Des(_tempInts, _tempInts, _decryptKeys);
  246. SpreadIntsToBytes(_tempInts, 0, clearText, clearOff, 2);
  247. }
  248. // The DES function.
  249. private void Des(int[] inInts, int[] outInts, int[] keys)
  250. {
  251. int fval;
  252. int work;
  253. int right;
  254. int leftt;
  255. int round;
  256. int keysi = 0;
  257. leftt = inInts[0];
  258. right = inInts[1];
  259. work = (((int)(((uint)leftt) >> 4)) ^ right) & unchecked(0x0f0f0f0f);
  260. right ^= work;
  261. leftt ^= (work << 4);
  262. work = (((int)(((uint)leftt) >> 16)) ^ right) & unchecked(0x0000ffff);
  263. right ^= work;
  264. leftt ^= (work << 16);
  265. work = (((int)(((uint)right) >> 2)) ^ leftt) & unchecked(0x33333333);
  266. leftt ^= work;
  267. right ^= (work << 2);
  268. work = (((int)(((uint)right) >> 8)) ^ leftt) & unchecked(0x00ff00ff);
  269. leftt ^= work;
  270. right ^= (work << 8);
  271. right = (right << 1) | (((int)(((uint)right) >> 31)) & 1);
  272. work = (leftt ^ right) & unchecked((int)(0xaaaaaaaa));
  273. leftt ^= work;
  274. right ^= work;
  275. leftt = (leftt << 1) | (((int)(((uint)leftt) >> 31)) & 1);
  276. for (round = 0; round < 8; ++round)
  277. {
  278. work = (right << 28) | ((int)(((uint)right) >> 4));
  279. work ^= keys[keysi++];
  280. fval = _sp7[work & unchecked(0x0000003f)];
  281. fval |= _sp5[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
  282. fval |= _sp3[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
  283. fval |= _sp1[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
  284. work = right ^ keys[keysi++];
  285. fval |= _sp8[work & unchecked(0x0000003f)];
  286. fval |= _sp6[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
  287. fval |= _sp4[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
  288. fval |= _sp2[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
  289. leftt ^= fval;
  290. work = (leftt << 28) | ((int)(((uint)leftt) >> 4));
  291. work ^= keys[keysi++];
  292. fval = _sp7[work & unchecked(0x0000003f)];
  293. fval |= _sp5[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
  294. fval |= _sp3[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
  295. fval |= _sp1[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
  296. work = leftt ^ keys[keysi++];
  297. fval |= _sp8[work & unchecked(0x0000003f)];
  298. fval |= _sp6[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
  299. fval |= _sp4[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
  300. fval |= _sp2[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
  301. right ^= fval;
  302. }
  303. right = (right << 31) | ((int)(((uint)right) >> 1));
  304. work = (leftt ^ right) & unchecked((int)(0xaaaaaaaa));
  305. leftt ^= work;
  306. right ^= work;
  307. leftt = (leftt << 31) | ((int)(((uint)leftt) >> 1));
  308. work = (((int)(((uint)leftt) >> 8)) ^ right) & unchecked(0x00ff00ff);
  309. right ^= work;
  310. leftt ^= (work << 8);
  311. work = (((int)(((uint)leftt) >> 2)) ^ right) & unchecked(0x33333333);
  312. right ^= work;
  313. leftt ^= (work << 2);
  314. work = (((int)(((uint)right) >> 16)) ^ leftt) & unchecked(0x0000ffff);
  315. leftt ^= work;
  316. right ^= (work << 16);
  317. work = (((int)(((uint)right) >> 4)) ^ leftt) & unchecked(0x0f0f0f0f);
  318. leftt ^= work;
  319. right ^= (work << 4);
  320. outInts[0] = right;
  321. outInts[1] = leftt;
  322. }
  323. /// Encrypt a block of bytes.
  324. public virtual void Encrypt(byte[] clearText, byte[] cipherText)
  325. {
  326. Encrypt(clearText, 0, cipherText, 0);
  327. }
  328. /// Decrypt a block of bytes.
  329. public virtual void Decrypt(byte[] cipherText, byte[] clearText)
  330. {
  331. Decrypt(cipherText, 0, clearText, 0);
  332. }
  333. /// <summary>encrypts an array where the length must be a multiple of 8</summary>
  334. public virtual byte[] Encrypt(byte[] clearText)
  335. {
  336. int length = clearText.Length;
  337. if (length % 8 != 0)
  338. {
  339. Console.Out.WriteLine("Array must be a multiple of 8");
  340. return null;
  341. }
  342. byte[] cipherText = new byte[length];
  343. int count = length / 8;
  344. for (int i = 0; i < count; i++)
  345. {
  346. Encrypt(clearText, i * 8, cipherText, i * 8);
  347. }
  348. return cipherText;
  349. }
  350. /// <summary>decrypts an array where the length must be a multiple of 8</summary>
  351. public virtual byte[] Decrypt(byte[] cipherText)
  352. {
  353. int length = cipherText.Length;
  354. if (length % 8 != 0)
  355. {
  356. Console.Out.WriteLine("Array must be a multiple of 8");
  357. return null;
  358. }
  359. byte[] clearText = new byte[length];
  360. int count = length / 8;
  361. for (int i = 0; i < count; i++)
  362. {
  363. Encrypt(cipherText, i * 8, clearText, i * 8);
  364. }
  365. return clearText;
  366. }
  367. private static byte[] _bytebit = { unchecked(unchecked(0x80)), unchecked(unchecked(0x40)), unchecked(unchecked(0x20)), unchecked(unchecked(0x10)), unchecked(unchecked(0x08)), unchecked(unchecked(0x04)), unchecked(unchecked(0x02)), unchecked(unchecked(0x01)) };
  368. private static int[] _bigbyte = { unchecked(0x800000), unchecked(
  369. 0x400000), unchecked(0x200000), unchecked(0x100000), unchecked(
  370. 0x080000), unchecked(0x040000), unchecked(0x020000), unchecked(
  371. 0x010000), unchecked(0x008000), unchecked(0x004000), unchecked(
  372. 0x002000), unchecked(0x001000), unchecked(0x000800), unchecked(
  373. 0x000400), unchecked(0x000200), unchecked(0x000100), unchecked(
  374. 0x000080), unchecked(0x000040), unchecked(0x000020), unchecked(
  375. 0x000010), unchecked(0x000008), unchecked(0x000004), unchecked(
  376. 0x000002), unchecked(0x000001) };
  377. private static byte[] _pc1 = { unchecked(56), unchecked(48)
  378. , unchecked(40), unchecked(32), unchecked(24), unchecked(16), unchecked(8), unchecked(0), unchecked(57), unchecked(49), unchecked(41), unchecked(33), unchecked(25), unchecked(17), unchecked(9), unchecked(1), unchecked(58), unchecked(
  379. 50), unchecked(42), unchecked(34), unchecked(26), unchecked(
  380. 18), unchecked(10), unchecked(2), unchecked(59), unchecked(
  381. 51), unchecked(43), unchecked(35), unchecked(62), unchecked(
  382. 54), unchecked(46), unchecked(38), unchecked(30), unchecked(
  383. 22), unchecked(14), unchecked(6), unchecked(61), unchecked(
  384. 53), unchecked(45), unchecked(37), unchecked(29), unchecked(
  385. 21), unchecked(13), unchecked(5), unchecked(60), unchecked(
  386. 52), unchecked(44), unchecked(36), unchecked(28), unchecked(
  387. 20), unchecked(12), unchecked(4), unchecked(27), unchecked(
  388. 19), unchecked(11), unchecked(3) };
  389. private static int[] _totrot = { 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19,
  390. 21, 23, 25, 27, 28 };
  391. private static byte[] _pc2 = { unchecked(13), unchecked(16)
  392. , unchecked(10), unchecked(23), unchecked(0), unchecked(4), unchecked(2), unchecked(27), unchecked(14), unchecked(5), unchecked(20), unchecked(9), unchecked(22), unchecked(18), unchecked(11), unchecked(3), unchecked(25), unchecked(7), unchecked(15), unchecked(6), unchecked(26), unchecked(19), unchecked(12), unchecked(1), unchecked(40), unchecked(51), unchecked(30), unchecked(36), unchecked(46), unchecked(54), unchecked(29), unchecked(39), unchecked(50), unchecked(
  393. 44), unchecked(32), unchecked(47), unchecked(43), unchecked(
  394. 48), unchecked(38), unchecked(55), unchecked(33), unchecked(
  395. 52), unchecked(45), unchecked(41), unchecked(49), unchecked(
  396. 35), unchecked(28), unchecked(31) };
  397. private static int[] _sp1 = { unchecked(0x01010400), unchecked(0x00000000), unchecked(0x00010000), unchecked(0x01010404), unchecked(
  398. 0x01010004), unchecked(0x00010404), unchecked(0x00000004),
  399. unchecked(0x00010000), unchecked(0x00000400), unchecked(0x01010400), unchecked(0x01010404), unchecked(0x00000400), unchecked(0x01000404), unchecked(0x01010004), unchecked(0x01000000), unchecked(
  400. 0x00000004), unchecked(0x00000404), unchecked(0x01000400),
  401. unchecked(0x01000400), unchecked(0x00010400), unchecked(0x00010400), unchecked(0x01010000), unchecked(0x01010000), unchecked(0x01000404), unchecked(0x00010004), unchecked(0x01000004), unchecked(
  402. 0x01000004), unchecked(0x00010004), unchecked(0x00000000),
  403. unchecked(0x00000404), unchecked(0x00010404), unchecked(0x01000000), unchecked(0x00010000), unchecked(0x01010404), unchecked(0x00000004), unchecked(0x01010000), unchecked(0x01010400), unchecked(
  404. 0x01000000), unchecked(0x01000000), unchecked(0x00000400),
  405. unchecked(0x01010004), unchecked(0x00010000), unchecked(0x00010400), unchecked(0x01000004), unchecked(0x00000400), unchecked(0x00000004), unchecked(0x01000404), unchecked(0x00010404), unchecked(
  406. 0x01010404), unchecked(0x00010004), unchecked(0x01010000),
  407. unchecked(0x01000404), unchecked(0x01000004), unchecked(0x00000404), unchecked(0x00010404), unchecked(0x01010400), unchecked(0x00000404), unchecked(0x01000400), unchecked(0x01000400), unchecked(
  408. 0x00000000), unchecked(0x00010004), unchecked(0x00010400),
  409. unchecked(0x00000000), unchecked(0x01010004) };
  410. private static int[] _sp2 = { unchecked((int)(0x80108020)), unchecked((int
  411. )(0x80008000)), unchecked(0x00008000), unchecked(0x00108020), unchecked(
  412. 0x00100000), unchecked(0x00000020), unchecked((int)(0x80100020)),
  413. unchecked((int)(0x80008020)), unchecked((int)(0x80000020)), unchecked((int)(0x80108020
  414. )), unchecked((int)(0x80108000)), unchecked((int)(0x80000000)), unchecked((int)(
  415. 0x80008000)), unchecked(0x00100000), unchecked(0x00000020), unchecked(
  416. (int)(0x80100020)), unchecked(0x00108000), unchecked(0x00100020),
  417. unchecked((int)(0x80008020)), unchecked(0x00000000), unchecked((int)(0x80000000
  418. )), unchecked(0x00008000), unchecked(0x00108020), unchecked((int)(
  419. 0x80100000)), unchecked(0x00100020), unchecked((int)(0x80000020)), unchecked(
  420. 0x00000000), unchecked(0x00108000), unchecked(0x00008020),
  421. unchecked((int)(0x80108000)), unchecked((int)(0x80100000)), unchecked(0x00008020), unchecked(0x00000000), unchecked(0x00108020), unchecked((int)(
  422. 0x80100020)), unchecked(0x00100000), unchecked((int)(0x80008020)), unchecked(
  423. (int)(0x80100000)), unchecked((int)(0x80108000)), unchecked(0x00008000),
  424. unchecked((int)(0x80100000)), unchecked((int)(0x80008000)), unchecked(0x00000020), unchecked((int)(0x80108020)), unchecked(0x00108020), unchecked(0x00000020), unchecked(0x00008000), unchecked((int)(0x80000000)), unchecked(
  425. 0x00008020), unchecked((int)(0x80108000)), unchecked(0x00100000),
  426. unchecked((int)(0x80000020)), unchecked(0x00100020), unchecked((int)(0x80008020
  427. )), unchecked((int)(0x80000020)), unchecked(0x00100020), unchecked(0x00108000), unchecked(0x00000000), unchecked((int)(0x80008000)), unchecked(
  428. 0x00008020), unchecked((int)(0x80000000)), unchecked((int)(0x80100020)),
  429. unchecked((int)(0x80108020)), unchecked(0x00108000) };
  430. private static int[] _sp3 = { unchecked(0x00000208), unchecked(0x08020200), unchecked(0x00000000), unchecked(0x08020008), unchecked(
  431. 0x08000200), unchecked(0x00000000), unchecked(0x00020208),
  432. unchecked(0x08000200), unchecked(0x00020008), unchecked(0x08000008), unchecked(0x08000008), unchecked(0x00020000), unchecked(0x08020208), unchecked(0x00020008), unchecked(0x08020000), unchecked(
  433. 0x00000208), unchecked(0x08000000), unchecked(0x00000008),
  434. unchecked(0x08020200), unchecked(0x00000200), unchecked(0x00020200), unchecked(0x08020000), unchecked(0x08020008), unchecked(0x00020208), unchecked(0x08000208), unchecked(0x00020200), unchecked(
  435. 0x00020000), unchecked(0x08000208), unchecked(0x00000008),
  436. unchecked(0x08020208), unchecked(0x00000200), unchecked(0x08000000), unchecked(0x08020200), unchecked(0x08000000), unchecked(0x00020008), unchecked(0x00000208), unchecked(0x00020000), unchecked(
  437. 0x08020200), unchecked(0x08000200), unchecked(0x00000000),
  438. unchecked(0x00000200), unchecked(0x00020008), unchecked(0x08020208), unchecked(0x08000200), unchecked(0x08000008), unchecked(0x00000200), unchecked(0x00000000), unchecked(0x08020008), unchecked(
  439. 0x08000208), unchecked(0x00020000), unchecked(0x08000000),
  440. unchecked(0x08020208), unchecked(0x00000008), unchecked(0x00020208), unchecked(0x00020200), unchecked(0x08000008), unchecked(0x08020000), unchecked(0x08000208), unchecked(0x00000208), unchecked(
  441. 0x08020000), unchecked(0x00020208), unchecked(0x00000008),
  442. unchecked(0x08020008), unchecked(0x00020200) };
  443. private static int[] _sp4 = { unchecked(0x00802001), unchecked(0x00002081), unchecked(0x00002081), unchecked(0x00000080), unchecked(
  444. 0x00802080), unchecked(0x00800081), unchecked(0x00800001),
  445. unchecked(0x00002001), unchecked(0x00000000), unchecked(0x00802000), unchecked(0x00802000), unchecked(0x00802081), unchecked(0x00000081), unchecked(0x00000000), unchecked(0x00800080), unchecked(
  446. 0x00800001), unchecked(0x00000001), unchecked(0x00002000),
  447. unchecked(0x00800000), unchecked(0x00802001), unchecked(0x00000080), unchecked(0x00800000), unchecked(0x00002001), unchecked(0x00002080), unchecked(0x00800081), unchecked(0x00000001), unchecked(
  448. 0x00002080), unchecked(0x00800080), unchecked(0x00002000),
  449. unchecked(0x00802080), unchecked(0x00802081), unchecked(0x00000081), unchecked(0x00800080), unchecked(0x00800001), unchecked(0x00802000), unchecked(0x00802081), unchecked(0x00000081), unchecked(
  450. 0x00000000), unchecked(0x00000000), unchecked(0x00802000),
  451. unchecked(0x00002080), unchecked(0x00800080), unchecked(0x00800081), unchecked(0x00000001), unchecked(0x00802001), unchecked(0x00002081), unchecked(0x00002081), unchecked(0x00000080), unchecked(
  452. 0x00802081), unchecked(0x00000081), unchecked(0x00000001),
  453. unchecked(0x00002000), unchecked(0x00800001), unchecked(0x00002001), unchecked(0x00802080), unchecked(0x00800081), unchecked(0x00002001), unchecked(0x00002080), unchecked(0x00800000), unchecked(
  454. 0x00802001), unchecked(0x00000080), unchecked(0x00800000),
  455. unchecked(0x00002000), unchecked(0x00802080) };
  456. private static int[] _sp5 = { unchecked(0x00000100), unchecked(0x02080100), unchecked(0x02080000), unchecked(0x42000100), unchecked(
  457. 0x00080000), unchecked(0x00000100), unchecked(0x40000000),
  458. unchecked(0x02080000), unchecked(0x40080100), unchecked(0x00080000), unchecked(0x02000100), unchecked(0x40080100), unchecked(0x42000100), unchecked(0x42080000), unchecked(0x00080100), unchecked(
  459. 0x40000000), unchecked(0x02000000), unchecked(0x40080000),
  460. unchecked(0x40080000), unchecked(0x00000000), unchecked(0x40000100), unchecked(0x42080100), unchecked(0x42080100), unchecked(0x02000100), unchecked(0x42080000), unchecked(0x40000100), unchecked(
  461. 0x00000000), unchecked(0x42000000), unchecked(0x02080100),
  462. unchecked(0x02000000), unchecked(0x42000000), unchecked(0x00080100), unchecked(0x00080000), unchecked(0x42000100), unchecked(0x00000100), unchecked(0x02000000), unchecked(0x40000000), unchecked(
  463. 0x02080000), unchecked(0x42000100), unchecked(0x40080100),
  464. unchecked(0x02000100), unchecked(0x40000000), unchecked(0x42080000), unchecked(0x02080100), unchecked(0x40080100), unchecked(0x00000100), unchecked(0x02000000), unchecked(0x42080000), unchecked(
  465. 0x42080100), unchecked(0x00080100), unchecked(0x42000000),
  466. unchecked(0x42080100), unchecked(0x02080000), unchecked(0x00000000), unchecked(0x40080000), unchecked(0x42000000), unchecked(0x00080100), unchecked(0x02000100), unchecked(0x40000100), unchecked(
  467. 0x00080000), unchecked(0x00000000), unchecked(0x40080000),
  468. unchecked(0x02080100), unchecked(0x40000100) };
  469. private static int[] _sp6 = { unchecked(0x20000010), unchecked(0x20400000), unchecked(0x00004000), unchecked(0x20404010), unchecked(
  470. 0x20400000), unchecked(0x00000010), unchecked(0x20404010),
  471. unchecked(0x00400000), unchecked(0x20004000), unchecked(0x00404010), unchecked(0x00400000), unchecked(0x20000010), unchecked(0x00400010), unchecked(0x20004000), unchecked(0x20000000), unchecked(
  472. 0x00004010), unchecked(0x00000000), unchecked(0x00400010),
  473. unchecked(0x20004010), unchecked(0x00004000), unchecked(0x00404000), unchecked(0x20004010), unchecked(0x00000010), unchecked(0x20400010), unchecked(0x20400010), unchecked(0x00000000), unchecked(
  474. 0x00404010), unchecked(0x20404000), unchecked(0x00004010),
  475. unchecked(0x00404000), unchecked(0x20404000), unchecked(0x20000000), unchecked(0x20004000), unchecked(0x00000010), unchecked(0x20400010), unchecked(0x00404000), unchecked(0x20404010), unchecked(
  476. 0x00400000), unchecked(0x00004010), unchecked(0x20000010),
  477. unchecked(0x00400000), unchecked(0x20004000), unchecked(0x20000000), unchecked(0x00004010), unchecked(0x20000010), unchecked(0x20404010), unchecked(0x00404000), unchecked(0x20400000), unchecked(
  478. 0x00404010), unchecked(0x20404000), unchecked(0x00000000),
  479. unchecked(0x20400010), unchecked(0x00000010), unchecked(0x00004000), unchecked(0x20400000), unchecked(0x00404010), unchecked(0x00004000), unchecked(0x00400010), unchecked(0x20004010), unchecked(
  480. 0x00000000), unchecked(0x20404000), unchecked(0x20000000),
  481. unchecked(0x00400010), unchecked(0x20004010) };
  482. private static int[] _sp7 = { unchecked(0x00200000), unchecked(0x04200002), unchecked(0x04000802), unchecked(0x00000000), unchecked(
  483. 0x00000800), unchecked(0x04000802), unchecked(0x00200802),
  484. unchecked(0x04200800), unchecked(0x04200802), unchecked(0x00200000), unchecked(0x00000000), unchecked(0x04000002), unchecked(0x00000002), unchecked(0x04000000), unchecked(0x04200002), unchecked(
  485. 0x00000802), unchecked(0x04000800), unchecked(0x00200802),
  486. unchecked(0x00200002), unchecked(0x04000800), unchecked(0x04000002), unchecked(0x04200000), unchecked(0x04200800), unchecked(0x00200002), unchecked(0x04200000), unchecked(0x00000800), unchecked(
  487. 0x00000802), unchecked(0x04200802), unchecked(0x00200800),
  488. unchecked(0x00000002), unchecked(0x04000000), unchecked(0x00200800), unchecked(0x04000000), unchecked(0x00200800), unchecked(0x00200000), unchecked(0x04000802), unchecked(0x04000802), unchecked(
  489. 0x04200002), unchecked(0x04200002), unchecked(0x00000002),
  490. unchecked(0x00200002), unchecked(0x04000000), unchecked(0x04000800), unchecked(0x00200000), unchecked(0x04200800), unchecked(0x00000802), unchecked(0x00200802), unchecked(0x04200800), unchecked(
  491. 0x00000802), unchecked(0x04000002), unchecked(0x04200802),
  492. unchecked(0x04200000), unchecked(0x00200800), unchecked(0x00000000), unchecked(0x00000002), unchecked(0x04200802), unchecked(0x00000000), unchecked(0x00200802), unchecked(0x04200000), unchecked(
  493. 0x00000800), unchecked(0x04000002), unchecked(0x04000800),
  494. unchecked(0x00000800), unchecked(0x00200002) };
  495. private static int[] _sp8 = { unchecked(0x10001040), unchecked(0x00001000), unchecked(0x00040000), unchecked(0x10041040), unchecked(
  496. 0x10000000), unchecked(0x10001040), unchecked(0x00000040),
  497. unchecked(0x10000000), unchecked(0x00040040), unchecked(0x10040000), unchecked(0x10041040), unchecked(0x00041000), unchecked(0x10041000), unchecked(0x00041040), unchecked(0x00001000), unchecked(
  498. 0x00000040), unchecked(0x10040000), unchecked(0x10000040),
  499. unchecked(0x10001000), unchecked(0x00001040), unchecked(0x00041000), unchecked(0x00040040), unchecked(0x10040040), unchecked(0x10041000), unchecked(0x00001040), unchecked(0x00000000), unchecked(
  500. 0x00000000), unchecked(0x10040040), unchecked(0x10000040),
  501. unchecked(0x10001000), unchecked(0x00041040), unchecked(0x00040000), unchecked(0x00041040), unchecked(0x00040000), unchecked(0x10041000), unchecked(0x00001000), unchecked(0x00000040), unchecked(
  502. 0x10040040), unchecked(0x00001000), unchecked(0x00041040),
  503. unchecked(0x10001000), unchecked(0x00000040), unchecked(0x10000040), unchecked(0x10040000), unchecked(0x10040040), unchecked(0x10000000), unchecked(0x00040000), unchecked(0x10001040), unchecked(
  504. 0x00000000), unchecked(0x10041040), unchecked(0x00040040),
  505. unchecked(0x10000040), unchecked(0x10040000), unchecked(0x10001000), unchecked(0x10001040), unchecked(0x00000000), unchecked(0x10041040), unchecked(0x00041000), unchecked(0x00041000), unchecked(
  506. 0x00001040), unchecked(0x00001040), unchecked(0x00040040),
  507. unchecked(0x10000000), unchecked(0x10041000) };
  508. // Tables, permutations, S-boxes, etc.
  509. /// Squash bytes down to ints.
  510. public static void SquashBytesToInts(byte[] inBytes, int inOff, int[] outInts, int
  511. outOff, int intLen)
  512. {
  513. for (int i = 0; i < intLen; ++i)
  514. {
  515. outInts[outOff + i] = ((inBytes[inOff + i * 4] & unchecked(0xff)) << 24) |
  516. ((inBytes[inOff + i * 4 + 1] & unchecked(0xff)) << 16) | ((inBytes[inOff
  517. + i * 4 + 2] & unchecked(0xff)) << 8) | (inBytes[inOff + i * 4 + 3] & unchecked(
  518. 0xff));
  519. }
  520. }
  521. /// Spread ints into bytes.
  522. public static void SpreadIntsToBytes(int[] inInts, int inOff, byte[] outBytes, int
  523. outOff, int intLen)
  524. {
  525. for (int i = 0; i < intLen; ++i)
  526. {
  527. outBytes[outOff + i * 4] = unchecked((byte)((int)(((uint)inInts[inOff + i]) >> 24
  528. )));
  529. outBytes[outOff + i * 4 + 1] = unchecked((byte)((int)(((uint)inInts[inOff + i]) >>
  530. 16)));
  531. outBytes[outOff + i * 4 + 2] = unchecked((byte)((int)(((uint)inInts[inOff + i]) >>
  532. 8)));
  533. outBytes[outOff + i * 4 + 3] = unchecked((byte)inInts[inOff + i]);
  534. }
  535. }
  536. }
  537. }