ThreefishEngine.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  9. {
  10. /// <summary>
  11. /// Implementation of the Threefish tweakable large block cipher in 256, 512 and 1024 bit block
  12. /// sizes.
  13. /// </summary>
  14. /// <remarks>
  15. /// This is the 1.3 version of Threefish defined in the Skein hash function submission to the NIST
  16. /// SHA-3 competition in October 2010.
  17. /// <p/>
  18. /// Threefish was designed by Niels Ferguson - Stefan Lucks - Bruce Schneier - Doug Whiting - Mihir
  19. /// Bellare - Tadayoshi Kohno - Jon Callas - Jesse Walker.
  20. /// <p/>
  21. /// This implementation inlines all round functions, unrolls 8 rounds, and uses 1.2k of static tables
  22. /// to speed up key schedule injection. <br/>
  23. /// 2 x block size state is retained by each cipher instance.
  24. /// </remarks>
  25. public class ThreefishEngine
  26. : IBlockCipher
  27. {
  28. /// <summary>
  29. /// 256 bit block size - Threefish-256
  30. /// </summary>
  31. public const int BLOCKSIZE_256 = 256;
  32. /// <summary>
  33. /// 512 bit block size - Threefish-512
  34. /// </summary>
  35. public const int BLOCKSIZE_512 = 512;
  36. /// <summary>
  37. /// 1024 bit block size - Threefish-1024
  38. /// </summary>
  39. public const int BLOCKSIZE_1024 = 1024;
  40. /**
  41. * Size of the tweak in bytes (always 128 bit/16 bytes)
  42. */
  43. private const int TWEAK_SIZE_BYTES = 16;
  44. private const int TWEAK_SIZE_WORDS = TWEAK_SIZE_BYTES / 8;
  45. /**
  46. * Rounds in Threefish-256
  47. */
  48. private const int ROUNDS_256 = 72;
  49. /**
  50. * Rounds in Threefish-512
  51. */
  52. private const int ROUNDS_512 = 72;
  53. /**
  54. * Rounds in Threefish-1024
  55. */
  56. private const int ROUNDS_1024 = 80;
  57. /**
  58. * Max rounds of any of the variants
  59. */
  60. private const int MAX_ROUNDS = ROUNDS_1024;
  61. /**
  62. * Key schedule parity constant
  63. */
  64. private const ulong C_240 = 0x1BD11BDAA9FC1A22L;
  65. /* Pre-calculated modulo arithmetic tables for key schedule lookups */
  66. private static readonly int[] MOD9 = new int[MAX_ROUNDS];
  67. private static readonly int[] MOD17 = new int[MOD9.Length];
  68. private static readonly int[] MOD5 = new int[MOD9.Length];
  69. private static readonly int[] MOD3 = new int[MOD9.Length];
  70. static ThreefishEngine()
  71. {
  72. for (int i = 0; i < MOD9.Length; i++)
  73. {
  74. MOD17[i] = i % 17;
  75. MOD9[i] = i % 9;
  76. MOD5[i] = i % 5;
  77. MOD3[i] = i % 3;
  78. }
  79. }
  80. /**
  81. * Block size in bytes
  82. */
  83. private readonly int blocksizeBytes;
  84. /**
  85. * Block size in 64 bit words
  86. */
  87. private readonly int blocksizeWords;
  88. /**
  89. * Buffer for byte oriented processBytes to call internal word API
  90. */
  91. private readonly ulong[] currentBlock;
  92. /**
  93. * Tweak bytes (2 byte t1,t2, calculated t3 and repeat of t1,t2 for modulo free lookup
  94. */
  95. private readonly ulong[] t = new ulong[5];
  96. /**
  97. * Key schedule words
  98. */
  99. private readonly ulong[] kw;
  100. /**
  101. * The internal cipher implementation (varies by blocksize)
  102. */
  103. private readonly ThreefishCipher cipher;
  104. private bool forEncryption;
  105. /// <summary>
  106. /// Constructs a new Threefish cipher, with a specified block size.
  107. /// </summary>
  108. /// <param name="blocksizeBits">the block size in bits, one of <see cref="BLOCKSIZE_256"/>, <see cref="BLOCKSIZE_512"/>,
  109. /// <see cref="BLOCKSIZE_1024"/> .</param>
  110. public ThreefishEngine(int blocksizeBits)
  111. {
  112. this.blocksizeBytes = (blocksizeBits / 8);
  113. this.blocksizeWords = (this.blocksizeBytes / 8);
  114. this.currentBlock = new ulong[blocksizeWords];
  115. /*
  116. * Provide room for original key words, extended key word and repeat of key words for modulo
  117. * free lookup of key schedule words.
  118. */
  119. this.kw = new ulong[2 * blocksizeWords + 1];
  120. switch (blocksizeBits)
  121. {
  122. case BLOCKSIZE_256:
  123. cipher = new Threefish256Cipher(kw, t);
  124. break;
  125. case BLOCKSIZE_512:
  126. cipher = new Threefish512Cipher(kw, t);
  127. break;
  128. case BLOCKSIZE_1024:
  129. cipher = new Threefish1024Cipher(kw, t);
  130. break;
  131. default:
  132. throw new ArgumentException(
  133. "Invalid blocksize - Threefish is defined with block size of 256, 512, or 1024 bits");
  134. }
  135. }
  136. /// <summary>
  137. /// Initialise the engine.
  138. /// </summary>
  139. /// <param name="forEncryption">Initialise for encryption if true, for decryption if false.</param>
  140. /// <param name="parameters">an instance of <see cref="TweakableBlockCipherParameters"/> or <see cref="KeyParameter"/> (to
  141. /// use a 0 tweak)</param>
  142. public virtual void Init(bool forEncryption, ICipherParameters parameters)
  143. {
  144. byte[] keyBytes;
  145. byte[] tweakBytes;
  146. if (parameters is TweakableBlockCipherParameters)
  147. {
  148. TweakableBlockCipherParameters tParams = (TweakableBlockCipherParameters)parameters;
  149. keyBytes = tParams.Key.GetKey();
  150. tweakBytes = tParams.Tweak;
  151. }
  152. else if (parameters is KeyParameter)
  153. {
  154. keyBytes = ((KeyParameter)parameters).GetKey();
  155. tweakBytes = null;
  156. }
  157. else
  158. {
  159. throw new ArgumentException("Invalid parameter passed to Threefish init - "
  160. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  161. }
  162. ulong[] keyWords = null;
  163. ulong[] tweakWords = null;
  164. if (keyBytes != null)
  165. {
  166. if (keyBytes.Length != this.blocksizeBytes)
  167. {
  168. throw new ArgumentException("Threefish key must be same size as block (" + blocksizeBytes
  169. + " bytes)");
  170. }
  171. keyWords = new ulong[blocksizeWords];
  172. for (int i = 0; i < keyWords.Length; i++)
  173. {
  174. keyWords[i] = BytesToWord(keyBytes, i * 8);
  175. }
  176. }
  177. if (tweakBytes != null)
  178. {
  179. if (tweakBytes.Length != TWEAK_SIZE_BYTES)
  180. {
  181. throw new ArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes");
  182. }
  183. tweakWords = new ulong[]{BytesToWord(tweakBytes, 0), BytesToWord(tweakBytes, 8)};
  184. }
  185. Init(forEncryption, keyWords, tweakWords);
  186. }
  187. /// <summary>
  188. /// Initialise the engine, specifying the key and tweak directly.
  189. /// </summary>
  190. /// <param name="forEncryption">the cipher mode.</param>
  191. /// <param name="key">the words of the key, or <code>null</code> to use the current key.</param>
  192. /// <param name="tweak">the 2 word (128 bit) tweak, or <code>null</code> to use the current tweak.</param>
  193. internal void Init(bool forEncryption, ulong[] key, ulong[] tweak)
  194. {
  195. this.forEncryption = forEncryption;
  196. if (key != null)
  197. {
  198. SetKey(key);
  199. }
  200. if (tweak != null)
  201. {
  202. SetTweak(tweak);
  203. }
  204. }
  205. private void SetKey(ulong[] key)
  206. {
  207. if (key.Length != this.blocksizeWords)
  208. {
  209. throw new ArgumentException("Threefish key must be same size as block (" + blocksizeWords
  210. + " words)");
  211. }
  212. /*
  213. * Full subkey schedule is deferred to execution to avoid per cipher overhead (10k for 512,
  214. * 20k for 1024).
  215. *
  216. * Key and tweak word sequences are repeated, and static MOD17/MOD9/MOD5/MOD3 calculations
  217. * used, to avoid expensive mod computations during cipher operation.
  218. */
  219. ulong knw = C_240;
  220. for (int i = 0; i < blocksizeWords; i++)
  221. {
  222. kw[i] = key[i];
  223. knw = knw ^ kw[i];
  224. }
  225. kw[blocksizeWords] = knw;
  226. Array.Copy(kw, 0, kw, blocksizeWords + 1, blocksizeWords);
  227. }
  228. private void SetTweak(ulong[] tweak)
  229. {
  230. if (tweak.Length != TWEAK_SIZE_WORDS)
  231. {
  232. throw new ArgumentException("Tweak must be " + TWEAK_SIZE_WORDS + " words.");
  233. }
  234. /*
  235. * Tweak schedule partially repeated to avoid mod computations during cipher operation
  236. */
  237. t[0] = tweak[0];
  238. t[1] = tweak[1];
  239. t[2] = t[0] ^ t[1];
  240. t[3] = t[0];
  241. t[4] = t[1];
  242. }
  243. public virtual string AlgorithmName
  244. {
  245. get { return "Threefish-" + (blocksizeBytes * 8); }
  246. }
  247. public virtual bool IsPartialBlockOkay
  248. {
  249. get { return false; }
  250. }
  251. public virtual int GetBlockSize()
  252. {
  253. return blocksizeBytes;
  254. }
  255. public virtual void Reset()
  256. {
  257. }
  258. public virtual int ProcessBlock(byte[] inBytes, int inOff, byte[] outBytes, int outOff)
  259. {
  260. if ((outOff + blocksizeBytes) > outBytes.Length)
  261. {
  262. throw new DataLengthException("Output buffer too short");
  263. }
  264. if ((inOff + blocksizeBytes) > inBytes.Length)
  265. {
  266. throw new DataLengthException("Input buffer too short");
  267. }
  268. for (int i = 0; i < blocksizeBytes; i += 8)
  269. {
  270. currentBlock[i >> 3] = BytesToWord(inBytes, inOff + i);
  271. }
  272. ProcessBlock(this.currentBlock, this.currentBlock);
  273. for (int i = 0; i < blocksizeBytes; i += 8)
  274. {
  275. WordToBytes(this.currentBlock[i >> 3], outBytes, outOff + i);
  276. }
  277. return blocksizeBytes;
  278. }
  279. /// <summary>
  280. /// Process a block of data represented as 64 bit words.
  281. /// </summary>
  282. /// <returns>the number of 8 byte words processed (which will be the same as the block size).</returns>
  283. /// <param name="inWords">a block sized buffer of words to process.</param>
  284. /// <param name="outWords">a block sized buffer of words to receive the output of the operation.</param>
  285. /// <exception cref="DataLengthException">if either the input or output is not block sized</exception>
  286. /// <exception cref="InvalidOperationException">if this engine is not initialised</exception>
  287. internal int ProcessBlock(ulong[] inWords, ulong[] outWords)
  288. {
  289. if (kw[blocksizeWords] == 0)
  290. {
  291. throw new InvalidOperationException("Threefish engine not initialised");
  292. }
  293. if (inWords.Length != blocksizeWords)
  294. {
  295. throw new DataLengthException("Input buffer too short");
  296. }
  297. if (outWords.Length != blocksizeWords)
  298. {
  299. throw new DataLengthException("Output buffer too short");
  300. }
  301. if (forEncryption)
  302. {
  303. cipher.EncryptBlock(inWords, outWords);
  304. }
  305. else
  306. {
  307. cipher.DecryptBlock(inWords, outWords);
  308. }
  309. return blocksizeWords;
  310. }
  311. /// <summary>
  312. /// Read a single 64 bit word from input in LSB first order.
  313. /// </summary>
  314. internal static ulong BytesToWord(byte[] bytes, int off)
  315. {
  316. if ((off + 8) > bytes.Length)
  317. {
  318. // Help the JIT avoid index checks
  319. throw new ArgumentException();
  320. }
  321. ulong word = 0;
  322. int index = off;
  323. word = (bytes[index++] & 0xffUL);
  324. word |= (bytes[index++] & 0xffUL) << 8;
  325. word |= (bytes[index++] & 0xffUL) << 16;
  326. word |= (bytes[index++] & 0xffUL) << 24;
  327. word |= (bytes[index++] & 0xffUL) << 32;
  328. word |= (bytes[index++] & 0xffUL) << 40;
  329. word |= (bytes[index++] & 0xffUL) << 48;
  330. word |= (bytes[index++] & 0xffUL) << 56;
  331. return word;
  332. }
  333. /// <summary>
  334. /// Write a 64 bit word to output in LSB first order.
  335. /// </summary>
  336. internal static void WordToBytes(ulong word, byte[] bytes, int off)
  337. {
  338. if ((off + 8) > bytes.Length)
  339. {
  340. // Help the JIT avoid index checks
  341. throw new ArgumentException();
  342. }
  343. int index = off;
  344. bytes[index++] = (byte)word;
  345. bytes[index++] = (byte)(word >> 8);
  346. bytes[index++] = (byte)(word >> 16);
  347. bytes[index++] = (byte)(word >> 24);
  348. bytes[index++] = (byte)(word >> 32);
  349. bytes[index++] = (byte)(word >> 40);
  350. bytes[index++] = (byte)(word >> 48);
  351. bytes[index++] = (byte)(word >> 56);
  352. }
  353. /**
  354. * Rotate left + xor part of the mix operation.
  355. */
  356. private static ulong RotlXor(ulong x, int n, ulong xor)
  357. {
  358. return ((x << n) | (x >> (64 - n))) ^ xor;
  359. }
  360. /**
  361. * Rotate xor + rotate right part of the unmix operation.
  362. */
  363. private static ulong XorRotr(ulong x, int n, ulong xor)
  364. {
  365. ulong xored = x ^ xor;
  366. return (xored >> n) | (xored << (64 - n));
  367. }
  368. private abstract class ThreefishCipher
  369. {
  370. /**
  371. * The extended + repeated tweak words
  372. */
  373. protected readonly ulong[] t;
  374. /**
  375. * The extended + repeated key words
  376. */
  377. protected readonly ulong[] kw;
  378. protected ThreefishCipher(ulong[] kw, ulong[] t)
  379. {
  380. this.kw = kw;
  381. this.t = t;
  382. }
  383. internal abstract void EncryptBlock(ulong[] block, ulong[] outWords);
  384. internal abstract void DecryptBlock(ulong[] block, ulong[] outWords);
  385. }
  386. private sealed class Threefish256Cipher
  387. : ThreefishCipher
  388. {
  389. /**
  390. * Mix rotation constants defined in Skein 1.3 specification
  391. */
  392. private const int ROTATION_0_0 = 14, ROTATION_0_1 = 16;
  393. private const int ROTATION_1_0 = 52, ROTATION_1_1 = 57;
  394. private const int ROTATION_2_0 = 23, ROTATION_2_1 = 40;
  395. private const int ROTATION_3_0 = 5, ROTATION_3_1 = 37;
  396. private const int ROTATION_4_0 = 25, ROTATION_4_1 = 33;
  397. private const int ROTATION_5_0 = 46, ROTATION_5_1 = 12;
  398. private const int ROTATION_6_0 = 58, ROTATION_6_1 = 22;
  399. private const int ROTATION_7_0 = 32, ROTATION_7_1 = 32;
  400. public Threefish256Cipher(ulong[] kw, ulong[] t)
  401. : base(kw, t)
  402. {
  403. }
  404. internal override void EncryptBlock(ulong[] block, ulong[] outWords)
  405. {
  406. ulong[] kw = this.kw;
  407. ulong[] t = this.t;
  408. int[] mod5 = MOD5;
  409. int[] mod3 = MOD3;
  410. /* Help the JIT avoid index bounds checks */
  411. if (kw.Length != 9)
  412. {
  413. throw new ArgumentException();
  414. }
  415. if (t.Length != 5)
  416. {
  417. throw new ArgumentException();
  418. }
  419. /*
  420. * Read 4 words of plaintext data, not using arrays for cipher state
  421. */
  422. ulong b0 = block[0];
  423. ulong b1 = block[1];
  424. ulong b2 = block[2];
  425. ulong b3 = block[3];
  426. /*
  427. * First subkey injection.
  428. */
  429. b0 += kw[0];
  430. b1 += kw[1] + t[0];
  431. b2 += kw[2] + t[1];
  432. b3 += kw[3];
  433. /*
  434. * Rounds loop, unrolled to 8 rounds per iteration.
  435. *
  436. * Unrolling to multiples of 4 avoids the mod 4 check for key injection, and allows
  437. * inlining of the permutations, which cycle every of 2 rounds (avoiding array
  438. * index/lookup).
  439. *
  440. * Unrolling to multiples of 8 avoids the mod 8 rotation constant lookup, and allows
  441. * inlining constant rotation values (avoiding array index/lookup).
  442. */
  443. for (int d = 1; d < (ROUNDS_256 / 4); d += 2)
  444. {
  445. int dm5 = mod5[d];
  446. int dm3 = mod3[d];
  447. /*
  448. * 4 rounds of mix and permute.
  449. *
  450. * Permute schedule has a 2 round cycle, so permutes are inlined in the mix
  451. * operations in each 4 round block.
  452. */
  453. b1 = RotlXor(b1, ROTATION_0_0, b0 += b1);
  454. b3 = RotlXor(b3, ROTATION_0_1, b2 += b3);
  455. b3 = RotlXor(b3, ROTATION_1_0, b0 += b3);
  456. b1 = RotlXor(b1, ROTATION_1_1, b2 += b1);
  457. b1 = RotlXor(b1, ROTATION_2_0, b0 += b1);
  458. b3 = RotlXor(b3, ROTATION_2_1, b2 += b3);
  459. b3 = RotlXor(b3, ROTATION_3_0, b0 += b3);
  460. b1 = RotlXor(b1, ROTATION_3_1, b2 += b1);
  461. /*
  462. * Subkey injection for first 4 rounds.
  463. */
  464. b0 += kw[dm5];
  465. b1 += kw[dm5 + 1] + t[dm3];
  466. b2 += kw[dm5 + 2] + t[dm3 + 1];
  467. b3 += kw[dm5 + 3] + (uint)d;
  468. /*
  469. * 4 more rounds of mix/permute
  470. */
  471. b1 = RotlXor(b1, ROTATION_4_0, b0 += b1);
  472. b3 = RotlXor(b3, ROTATION_4_1, b2 += b3);
  473. b3 = RotlXor(b3, ROTATION_5_0, b0 += b3);
  474. b1 = RotlXor(b1, ROTATION_5_1, b2 += b1);
  475. b1 = RotlXor(b1, ROTATION_6_0, b0 += b1);
  476. b3 = RotlXor(b3, ROTATION_6_1, b2 += b3);
  477. b3 = RotlXor(b3, ROTATION_7_0, b0 += b3);
  478. b1 = RotlXor(b1, ROTATION_7_1, b2 += b1);
  479. /*
  480. * Subkey injection for next 4 rounds.
  481. */
  482. b0 += kw[dm5 + 1];
  483. b1 += kw[dm5 + 2] + t[dm3 + 1];
  484. b2 += kw[dm5 + 3] + t[dm3 + 2];
  485. b3 += kw[dm5 + 4] + (uint)d + 1;
  486. }
  487. /*
  488. * Output cipher state.
  489. */
  490. outWords[0] = b0;
  491. outWords[1] = b1;
  492. outWords[2] = b2;
  493. outWords[3] = b3;
  494. }
  495. internal override void DecryptBlock(ulong[] block, ulong[] state)
  496. {
  497. ulong[] kw = this.kw;
  498. ulong[] t = this.t;
  499. int[] mod5 = MOD5;
  500. int[] mod3 = MOD3;
  501. /* Help the JIT avoid index bounds checks */
  502. if (kw.Length != 9)
  503. {
  504. throw new ArgumentException();
  505. }
  506. if (t.Length != 5)
  507. {
  508. throw new ArgumentException();
  509. }
  510. ulong b0 = block[0];
  511. ulong b1 = block[1];
  512. ulong b2 = block[2];
  513. ulong b3 = block[3];
  514. for (int d = (ROUNDS_256 / 4) - 1; d >= 1; d -= 2)
  515. {
  516. int dm5 = mod5[d];
  517. int dm3 = mod3[d];
  518. /* Reverse key injection for second 4 rounds */
  519. b0 -= kw[dm5 + 1];
  520. b1 -= kw[dm5 + 2] + t[dm3 + 1];
  521. b2 -= kw[dm5 + 3] + t[dm3 + 2];
  522. b3 -= kw[dm5 + 4] + (uint)d + 1;
  523. /* Reverse second 4 mix/permute rounds */
  524. b3 = XorRotr(b3, ROTATION_7_0, b0);
  525. b0 -= b3;
  526. b1 = XorRotr(b1, ROTATION_7_1, b2);
  527. b2 -= b1;
  528. b1 = XorRotr(b1, ROTATION_6_0, b0);
  529. b0 -= b1;
  530. b3 = XorRotr(b3, ROTATION_6_1, b2);
  531. b2 -= b3;
  532. b3 = XorRotr(b3, ROTATION_5_0, b0);
  533. b0 -= b3;
  534. b1 = XorRotr(b1, ROTATION_5_1, b2);
  535. b2 -= b1;
  536. b1 = XorRotr(b1, ROTATION_4_0, b0);
  537. b0 -= b1;
  538. b3 = XorRotr(b3, ROTATION_4_1, b2);
  539. b2 -= b3;
  540. /* Reverse key injection for first 4 rounds */
  541. b0 -= kw[dm5];
  542. b1 -= kw[dm5 + 1] + t[dm3];
  543. b2 -= kw[dm5 + 2] + t[dm3 + 1];
  544. b3 -= kw[dm5 + 3] + (uint)d;
  545. /* Reverse first 4 mix/permute rounds */
  546. b3 = XorRotr(b3, ROTATION_3_0, b0);
  547. b0 -= b3;
  548. b1 = XorRotr(b1, ROTATION_3_1, b2);
  549. b2 -= b1;
  550. b1 = XorRotr(b1, ROTATION_2_0, b0);
  551. b0 -= b1;
  552. b3 = XorRotr(b3, ROTATION_2_1, b2);
  553. b2 -= b3;
  554. b3 = XorRotr(b3, ROTATION_1_0, b0);
  555. b0 -= b3;
  556. b1 = XorRotr(b1, ROTATION_1_1, b2);
  557. b2 -= b1;
  558. b1 = XorRotr(b1, ROTATION_0_0, b0);
  559. b0 -= b1;
  560. b3 = XorRotr(b3, ROTATION_0_1, b2);
  561. b2 -= b3;
  562. }
  563. /*
  564. * First subkey uninjection.
  565. */
  566. b0 -= kw[0];
  567. b1 -= kw[1] + t[0];
  568. b2 -= kw[2] + t[1];
  569. b3 -= kw[3];
  570. /*
  571. * Output cipher state.
  572. */
  573. state[0] = b0;
  574. state[1] = b1;
  575. state[2] = b2;
  576. state[3] = b3;
  577. }
  578. }
  579. private sealed class Threefish512Cipher
  580. : ThreefishCipher
  581. {
  582. /**
  583. * Mix rotation constants defined in Skein 1.3 specification
  584. */
  585. private const int ROTATION_0_0 = 46, ROTATION_0_1 = 36, ROTATION_0_2 = 19, ROTATION_0_3 = 37;
  586. private const int ROTATION_1_0 = 33, ROTATION_1_1 = 27, ROTATION_1_2 = 14, ROTATION_1_3 = 42;
  587. private const int ROTATION_2_0 = 17, ROTATION_2_1 = 49, ROTATION_2_2 = 36, ROTATION_2_3 = 39;
  588. private const int ROTATION_3_0 = 44, ROTATION_3_1 = 9, ROTATION_3_2 = 54, ROTATION_3_3 = 56;
  589. private const int ROTATION_4_0 = 39, ROTATION_4_1 = 30, ROTATION_4_2 = 34, ROTATION_4_3 = 24;
  590. private const int ROTATION_5_0 = 13, ROTATION_5_1 = 50, ROTATION_5_2 = 10, ROTATION_5_3 = 17;
  591. private const int ROTATION_6_0 = 25, ROTATION_6_1 = 29, ROTATION_6_2 = 39, ROTATION_6_3 = 43;
  592. private const int ROTATION_7_0 = 8, ROTATION_7_1 = 35, ROTATION_7_2 = 56, ROTATION_7_3 = 22;
  593. internal Threefish512Cipher(ulong[] kw, ulong[] t)
  594. : base(kw, t)
  595. {
  596. }
  597. internal override void EncryptBlock(ulong[] block, ulong[] outWords)
  598. {
  599. ulong[] kw = this.kw;
  600. ulong[] t = this.t;
  601. int[] mod9 = MOD9;
  602. int[] mod3 = MOD3;
  603. /* Help the JIT avoid index bounds checks */
  604. if (kw.Length != 17)
  605. {
  606. throw new ArgumentException();
  607. }
  608. if (t.Length != 5)
  609. {
  610. throw new ArgumentException();
  611. }
  612. /*
  613. * Read 8 words of plaintext data, not using arrays for cipher state
  614. */
  615. ulong b0 = block[0];
  616. ulong b1 = block[1];
  617. ulong b2 = block[2];
  618. ulong b3 = block[3];
  619. ulong b4 = block[4];
  620. ulong b5 = block[5];
  621. ulong b6 = block[6];
  622. ulong b7 = block[7];
  623. /*
  624. * First subkey injection.
  625. */
  626. b0 += kw[0];
  627. b1 += kw[1];
  628. b2 += kw[2];
  629. b3 += kw[3];
  630. b4 += kw[4];
  631. b5 += kw[5] + t[0];
  632. b6 += kw[6] + t[1];
  633. b7 += kw[7];
  634. /*
  635. * Rounds loop, unrolled to 8 rounds per iteration.
  636. *
  637. * Unrolling to multiples of 4 avoids the mod 4 check for key injection, and allows
  638. * inlining of the permutations, which cycle every of 4 rounds (avoiding array
  639. * index/lookup).
  640. *
  641. * Unrolling to multiples of 8 avoids the mod 8 rotation constant lookup, and allows
  642. * inlining constant rotation values (avoiding array index/lookup).
  643. */
  644. for (int d = 1; d < (ROUNDS_512 / 4); d += 2)
  645. {
  646. int dm9 = mod9[d];
  647. int dm3 = mod3[d];
  648. /*
  649. * 4 rounds of mix and permute.
  650. *
  651. * Permute schedule has a 4 round cycle, so permutes are inlined in the mix
  652. * operations in each 4 round block.
  653. */
  654. b1 = RotlXor(b1, ROTATION_0_0, b0 += b1);
  655. b3 = RotlXor(b3, ROTATION_0_1, b2 += b3);
  656. b5 = RotlXor(b5, ROTATION_0_2, b4 += b5);
  657. b7 = RotlXor(b7, ROTATION_0_3, b6 += b7);
  658. b1 = RotlXor(b1, ROTATION_1_0, b2 += b1);
  659. b7 = RotlXor(b7, ROTATION_1_1, b4 += b7);
  660. b5 = RotlXor(b5, ROTATION_1_2, b6 += b5);
  661. b3 = RotlXor(b3, ROTATION_1_3, b0 += b3);
  662. b1 = RotlXor(b1, ROTATION_2_0, b4 += b1);
  663. b3 = RotlXor(b3, ROTATION_2_1, b6 += b3);
  664. b5 = RotlXor(b5, ROTATION_2_2, b0 += b5);
  665. b7 = RotlXor(b7, ROTATION_2_3, b2 += b7);
  666. b1 = RotlXor(b1, ROTATION_3_0, b6 += b1);
  667. b7 = RotlXor(b7, ROTATION_3_1, b0 += b7);
  668. b5 = RotlXor(b5, ROTATION_3_2, b2 += b5);
  669. b3 = RotlXor(b3, ROTATION_3_3, b4 += b3);
  670. /*
  671. * Subkey injection for first 4 rounds.
  672. */
  673. b0 += kw[dm9];
  674. b1 += kw[dm9 + 1];
  675. b2 += kw[dm9 + 2];
  676. b3 += kw[dm9 + 3];
  677. b4 += kw[dm9 + 4];
  678. b5 += kw[dm9 + 5] + t[dm3];
  679. b6 += kw[dm9 + 6] + t[dm3 + 1];
  680. b7 += kw[dm9 + 7] + (uint)d;
  681. /*
  682. * 4 more rounds of mix/permute
  683. */
  684. b1 = RotlXor(b1, ROTATION_4_0, b0 += b1);
  685. b3 = RotlXor(b3, ROTATION_4_1, b2 += b3);
  686. b5 = RotlXor(b5, ROTATION_4_2, b4 += b5);
  687. b7 = RotlXor(b7, ROTATION_4_3, b6 += b7);
  688. b1 = RotlXor(b1, ROTATION_5_0, b2 += b1);
  689. b7 = RotlXor(b7, ROTATION_5_1, b4 += b7);
  690. b5 = RotlXor(b5, ROTATION_5_2, b6 += b5);
  691. b3 = RotlXor(b3, ROTATION_5_3, b0 += b3);
  692. b1 = RotlXor(b1, ROTATION_6_0, b4 += b1);
  693. b3 = RotlXor(b3, ROTATION_6_1, b6 += b3);
  694. b5 = RotlXor(b5, ROTATION_6_2, b0 += b5);
  695. b7 = RotlXor(b7, ROTATION_6_3, b2 += b7);
  696. b1 = RotlXor(b1, ROTATION_7_0, b6 += b1);
  697. b7 = RotlXor(b7, ROTATION_7_1, b0 += b7);
  698. b5 = RotlXor(b5, ROTATION_7_2, b2 += b5);
  699. b3 = RotlXor(b3, ROTATION_7_3, b4 += b3);
  700. /*
  701. * Subkey injection for next 4 rounds.
  702. */
  703. b0 += kw[dm9 + 1];
  704. b1 += kw[dm9 + 2];
  705. b2 += kw[dm9 + 3];
  706. b3 += kw[dm9 + 4];
  707. b4 += kw[dm9 + 5];
  708. b5 += kw[dm9 + 6] + t[dm3 + 1];
  709. b6 += kw[dm9 + 7] + t[dm3 + 2];
  710. b7 += kw[dm9 + 8] + (uint)d + 1;
  711. }
  712. /*
  713. * Output cipher state.
  714. */
  715. outWords[0] = b0;
  716. outWords[1] = b1;
  717. outWords[2] = b2;
  718. outWords[3] = b3;
  719. outWords[4] = b4;
  720. outWords[5] = b5;
  721. outWords[6] = b6;
  722. outWords[7] = b7;
  723. }
  724. internal override void DecryptBlock(ulong[] block, ulong[] state)
  725. {
  726. ulong[] kw = this.kw;
  727. ulong[] t = this.t;
  728. int[] mod9 = MOD9;
  729. int[] mod3 = MOD3;
  730. /* Help the JIT avoid index bounds checks */
  731. if (kw.Length != 17)
  732. {
  733. throw new ArgumentException();
  734. }
  735. if (t.Length != 5)
  736. {
  737. throw new ArgumentException();
  738. }
  739. ulong b0 = block[0];
  740. ulong b1 = block[1];
  741. ulong b2 = block[2];
  742. ulong b3 = block[3];
  743. ulong b4 = block[4];
  744. ulong b5 = block[5];
  745. ulong b6 = block[6];
  746. ulong b7 = block[7];
  747. for (int d = (ROUNDS_512 / 4) - 1; d >= 1; d -= 2)
  748. {
  749. int dm9 = mod9[d];
  750. int dm3 = mod3[d];
  751. /* Reverse key injection for second 4 rounds */
  752. b0 -= kw[dm9 + 1];
  753. b1 -= kw[dm9 + 2];
  754. b2 -= kw[dm9 + 3];
  755. b3 -= kw[dm9 + 4];
  756. b4 -= kw[dm9 + 5];
  757. b5 -= kw[dm9 + 6] + t[dm3 + 1];
  758. b6 -= kw[dm9 + 7] + t[dm3 + 2];
  759. b7 -= kw[dm9 + 8] + (uint)d + 1;
  760. /* Reverse second 4 mix/permute rounds */
  761. b1 = XorRotr(b1, ROTATION_7_0, b6);
  762. b6 -= b1;
  763. b7 = XorRotr(b7, ROTATION_7_1, b0);
  764. b0 -= b7;
  765. b5 = XorRotr(b5, ROTATION_7_2, b2);
  766. b2 -= b5;
  767. b3 = XorRotr(b3, ROTATION_7_3, b4);
  768. b4 -= b3;
  769. b1 = XorRotr(b1, ROTATION_6_0, b4);
  770. b4 -= b1;
  771. b3 = XorRotr(b3, ROTATION_6_1, b6);
  772. b6 -= b3;
  773. b5 = XorRotr(b5, ROTATION_6_2, b0);
  774. b0 -= b5;
  775. b7 = XorRotr(b7, ROTATION_6_3, b2);
  776. b2 -= b7;
  777. b1 = XorRotr(b1, ROTATION_5_0, b2);
  778. b2 -= b1;
  779. b7 = XorRotr(b7, ROTATION_5_1, b4);
  780. b4 -= b7;
  781. b5 = XorRotr(b5, ROTATION_5_2, b6);
  782. b6 -= b5;
  783. b3 = XorRotr(b3, ROTATION_5_3, b0);
  784. b0 -= b3;
  785. b1 = XorRotr(b1, ROTATION_4_0, b0);
  786. b0 -= b1;
  787. b3 = XorRotr(b3, ROTATION_4_1, b2);
  788. b2 -= b3;
  789. b5 = XorRotr(b5, ROTATION_4_2, b4);
  790. b4 -= b5;
  791. b7 = XorRotr(b7, ROTATION_4_3, b6);
  792. b6 -= b7;
  793. /* Reverse key injection for first 4 rounds */
  794. b0 -= kw[dm9];
  795. b1 -= kw[dm9 + 1];
  796. b2 -= kw[dm9 + 2];
  797. b3 -= kw[dm9 + 3];
  798. b4 -= kw[dm9 + 4];
  799. b5 -= kw[dm9 + 5] + t[dm3];
  800. b6 -= kw[dm9 + 6] + t[dm3 + 1];
  801. b7 -= kw[dm9 + 7] + (uint)d;
  802. /* Reverse first 4 mix/permute rounds */
  803. b1 = XorRotr(b1, ROTATION_3_0, b6);
  804. b6 -= b1;
  805. b7 = XorRotr(b7, ROTATION_3_1, b0);
  806. b0 -= b7;
  807. b5 = XorRotr(b5, ROTATION_3_2, b2);
  808. b2 -= b5;
  809. b3 = XorRotr(b3, ROTATION_3_3, b4);
  810. b4 -= b3;
  811. b1 = XorRotr(b1, ROTATION_2_0, b4);
  812. b4 -= b1;
  813. b3 = XorRotr(b3, ROTATION_2_1, b6);
  814. b6 -= b3;
  815. b5 = XorRotr(b5, ROTATION_2_2, b0);
  816. b0 -= b5;
  817. b7 = XorRotr(b7, ROTATION_2_3, b2);
  818. b2 -= b7;
  819. b1 = XorRotr(b1, ROTATION_1_0, b2);
  820. b2 -= b1;
  821. b7 = XorRotr(b7, ROTATION_1_1, b4);
  822. b4 -= b7;
  823. b5 = XorRotr(b5, ROTATION_1_2, b6);
  824. b6 -= b5;
  825. b3 = XorRotr(b3, ROTATION_1_3, b0);
  826. b0 -= b3;
  827. b1 = XorRotr(b1, ROTATION_0_0, b0);
  828. b0 -= b1;
  829. b3 = XorRotr(b3, ROTATION_0_1, b2);
  830. b2 -= b3;
  831. b5 = XorRotr(b5, ROTATION_0_2, b4);
  832. b4 -= b5;
  833. b7 = XorRotr(b7, ROTATION_0_3, b6);
  834. b6 -= b7;
  835. }
  836. /*
  837. * First subkey uninjection.
  838. */
  839. b0 -= kw[0];
  840. b1 -= kw[1];
  841. b2 -= kw[2];
  842. b3 -= kw[3];
  843. b4 -= kw[4];
  844. b5 -= kw[5] + t[0];
  845. b6 -= kw[6] + t[1];
  846. b7 -= kw[7];
  847. /*
  848. * Output cipher state.
  849. */
  850. state[0] = b0;
  851. state[1] = b1;
  852. state[2] = b2;
  853. state[3] = b3;
  854. state[4] = b4;
  855. state[5] = b5;
  856. state[6] = b6;
  857. state[7] = b7;
  858. }
  859. }
  860. private sealed class Threefish1024Cipher
  861. : ThreefishCipher
  862. {
  863. /**
  864. * Mix rotation constants defined in Skein 1.3 specification
  865. */
  866. private const int ROTATION_0_0 = 24, ROTATION_0_1 = 13, ROTATION_0_2 = 8, ROTATION_0_3 = 47;
  867. private const int ROTATION_0_4 = 8, ROTATION_0_5 = 17, ROTATION_0_6 = 22, ROTATION_0_7 = 37;
  868. private const int ROTATION_1_0 = 38, ROTATION_1_1 = 19, ROTATION_1_2 = 10, ROTATION_1_3 = 55;
  869. private const int ROTATION_1_4 = 49, ROTATION_1_5 = 18, ROTATION_1_6 = 23, ROTATION_1_7 = 52;
  870. private const int ROTATION_2_0 = 33, ROTATION_2_1 = 4, ROTATION_2_2 = 51, ROTATION_2_3 = 13;
  871. private const int ROTATION_2_4 = 34, ROTATION_2_5 = 41, ROTATION_2_6 = 59, ROTATION_2_7 = 17;
  872. private const int ROTATION_3_0 = 5, ROTATION_3_1 = 20, ROTATION_3_2 = 48, ROTATION_3_3 = 41;
  873. private const int ROTATION_3_4 = 47, ROTATION_3_5 = 28, ROTATION_3_6 = 16, ROTATION_3_7 = 25;
  874. private const int ROTATION_4_0 = 41, ROTATION_4_1 = 9, ROTATION_4_2 = 37, ROTATION_4_3 = 31;
  875. private const int ROTATION_4_4 = 12, ROTATION_4_5 = 47, ROTATION_4_6 = 44, ROTATION_4_7 = 30;
  876. private const int ROTATION_5_0 = 16, ROTATION_5_1 = 34, ROTATION_5_2 = 56, ROTATION_5_3 = 51;
  877. private const int ROTATION_5_4 = 4, ROTATION_5_5 = 53, ROTATION_5_6 = 42, ROTATION_5_7 = 41;
  878. private const int ROTATION_6_0 = 31, ROTATION_6_1 = 44, ROTATION_6_2 = 47, ROTATION_6_3 = 46;
  879. private const int ROTATION_6_4 = 19, ROTATION_6_5 = 42, ROTATION_6_6 = 44, ROTATION_6_7 = 25;
  880. private const int ROTATION_7_0 = 9, ROTATION_7_1 = 48, ROTATION_7_2 = 35, ROTATION_7_3 = 52;
  881. private const int ROTATION_7_4 = 23, ROTATION_7_5 = 31, ROTATION_7_6 = 37, ROTATION_7_7 = 20;
  882. public Threefish1024Cipher(ulong[] kw, ulong[] t)
  883. : base(kw, t)
  884. {
  885. }
  886. internal override void EncryptBlock(ulong[] block, ulong[] outWords)
  887. {
  888. ulong[] kw = this.kw;
  889. ulong[] t = this.t;
  890. int[] mod17 = MOD17;
  891. int[] mod3 = MOD3;
  892. /* Help the JIT avoid index bounds checks */
  893. if (kw.Length != 33)
  894. {
  895. throw new ArgumentException();
  896. }
  897. if (t.Length != 5)
  898. {
  899. throw new ArgumentException();
  900. }
  901. /*
  902. * Read 16 words of plaintext data, not using arrays for cipher state
  903. */
  904. ulong b0 = block[0];
  905. ulong b1 = block[1];
  906. ulong b2 = block[2];
  907. ulong b3 = block[3];
  908. ulong b4 = block[4];
  909. ulong b5 = block[5];
  910. ulong b6 = block[6];
  911. ulong b7 = block[7];
  912. ulong b8 = block[8];
  913. ulong b9 = block[9];
  914. ulong b10 = block[10];
  915. ulong b11 = block[11];
  916. ulong b12 = block[12];
  917. ulong b13 = block[13];
  918. ulong b14 = block[14];
  919. ulong b15 = block[15];
  920. /*
  921. * First subkey injection.
  922. */
  923. b0 += kw[0];
  924. b1 += kw[1];
  925. b2 += kw[2];
  926. b3 += kw[3];
  927. b4 += kw[4];
  928. b5 += kw[5];
  929. b6 += kw[6];
  930. b7 += kw[7];
  931. b8 += kw[8];
  932. b9 += kw[9];
  933. b10 += kw[10];
  934. b11 += kw[11];
  935. b12 += kw[12];
  936. b13 += kw[13] + t[0];
  937. b14 += kw[14] + t[1];
  938. b15 += kw[15];
  939. /*
  940. * Rounds loop, unrolled to 8 rounds per iteration.
  941. *
  942. * Unrolling to multiples of 4 avoids the mod 4 check for key injection, and allows
  943. * inlining of the permutations, which cycle every of 4 rounds (avoiding array
  944. * index/lookup).
  945. *
  946. * Unrolling to multiples of 8 avoids the mod 8 rotation constant lookup, and allows
  947. * inlining constant rotation values (avoiding array index/lookup).
  948. */
  949. for (int d = 1; d < (ROUNDS_1024 / 4); d += 2)
  950. {
  951. int dm17 = mod17[d];
  952. int dm3 = mod3[d];
  953. /*
  954. * 4 rounds of mix and permute.
  955. *
  956. * Permute schedule has a 4 round cycle, so permutes are inlined in the mix
  957. * operations in each 4 round block.
  958. */
  959. b1 = RotlXor(b1, ROTATION_0_0, b0 += b1);
  960. b3 = RotlXor(b3, ROTATION_0_1, b2 += b3);
  961. b5 = RotlXor(b5, ROTATION_0_2, b4 += b5);
  962. b7 = RotlXor(b7, ROTATION_0_3, b6 += b7);
  963. b9 = RotlXor(b9, ROTATION_0_4, b8 += b9);
  964. b11 = RotlXor(b11, ROTATION_0_5, b10 += b11);
  965. b13 = RotlXor(b13, ROTATION_0_6, b12 += b13);
  966. b15 = RotlXor(b15, ROTATION_0_7, b14 += b15);
  967. b9 = RotlXor(b9, ROTATION_1_0, b0 += b9);
  968. b13 = RotlXor(b13, ROTATION_1_1, b2 += b13);
  969. b11 = RotlXor(b11, ROTATION_1_2, b6 += b11);
  970. b15 = RotlXor(b15, ROTATION_1_3, b4 += b15);
  971. b7 = RotlXor(b7, ROTATION_1_4, b10 += b7);
  972. b3 = RotlXor(b3, ROTATION_1_5, b12 += b3);
  973. b5 = RotlXor(b5, ROTATION_1_6, b14 += b5);
  974. b1 = RotlXor(b1, ROTATION_1_7, b8 += b1);
  975. b7 = RotlXor(b7, ROTATION_2_0, b0 += b7);
  976. b5 = RotlXor(b5, ROTATION_2_1, b2 += b5);
  977. b3 = RotlXor(b3, ROTATION_2_2, b4 += b3);
  978. b1 = RotlXor(b1, ROTATION_2_3, b6 += b1);
  979. b15 = RotlXor(b15, ROTATION_2_4, b12 += b15);
  980. b13 = RotlXor(b13, ROTATION_2_5, b14 += b13);
  981. b11 = RotlXor(b11, ROTATION_2_6, b8 += b11);
  982. b9 = RotlXor(b9, ROTATION_2_7, b10 += b9);
  983. b15 = RotlXor(b15, ROTATION_3_0, b0 += b15);
  984. b11 = RotlXor(b11, ROTATION_3_1, b2 += b11);
  985. b13 = RotlXor(b13, ROTATION_3_2, b6 += b13);
  986. b9 = RotlXor(b9, ROTATION_3_3, b4 += b9);
  987. b1 = RotlXor(b1, ROTATION_3_4, b14 += b1);
  988. b5 = RotlXor(b5, ROTATION_3_5, b8 += b5);
  989. b3 = RotlXor(b3, ROTATION_3_6, b10 += b3);
  990. b7 = RotlXor(b7, ROTATION_3_7, b12 += b7);
  991. /*
  992. * Subkey injection for first 4 rounds.
  993. */
  994. b0 += kw[dm17];
  995. b1 += kw[dm17 + 1];
  996. b2 += kw[dm17 + 2];
  997. b3 += kw[dm17 + 3];
  998. b4 += kw[dm17 + 4];
  999. b5 += kw[dm17 + 5];
  1000. b6 += kw[dm17 + 6];
  1001. b7 += kw[dm17 + 7];
  1002. b8 += kw[dm17 + 8];
  1003. b9 += kw[dm17 + 9];
  1004. b10 += kw[dm17 + 10];
  1005. b11 += kw[dm17 + 11];
  1006. b12 += kw[dm17 + 12];
  1007. b13 += kw[dm17 + 13] + t[dm3];
  1008. b14 += kw[dm17 + 14] + t[dm3 + 1];
  1009. b15 += kw[dm17 + 15] + (uint)d;
  1010. /*
  1011. * 4 more rounds of mix/permute
  1012. */
  1013. b1 = RotlXor(b1, ROTATION_4_0, b0 += b1);
  1014. b3 = RotlXor(b3, ROTATION_4_1, b2 += b3);
  1015. b5 = RotlXor(b5, ROTATION_4_2, b4 += b5);
  1016. b7 = RotlXor(b7, ROTATION_4_3, b6 += b7);
  1017. b9 = RotlXor(b9, ROTATION_4_4, b8 += b9);
  1018. b11 = RotlXor(b11, ROTATION_4_5, b10 += b11);
  1019. b13 = RotlXor(b13, ROTATION_4_6, b12 += b13);
  1020. b15 = RotlXor(b15, ROTATION_4_7, b14 += b15);
  1021. b9 = RotlXor(b9, ROTATION_5_0, b0 += b9);
  1022. b13 = RotlXor(b13, ROTATION_5_1, b2 += b13);
  1023. b11 = RotlXor(b11, ROTATION_5_2, b6 += b11);
  1024. b15 = RotlXor(b15, ROTATION_5_3, b4 += b15);
  1025. b7 = RotlXor(b7, ROTATION_5_4, b10 += b7);
  1026. b3 = RotlXor(b3, ROTATION_5_5, b12 += b3);
  1027. b5 = RotlXor(b5, ROTATION_5_6, b14 += b5);
  1028. b1 = RotlXor(b1, ROTATION_5_7, b8 += b1);
  1029. b7 = RotlXor(b7, ROTATION_6_0, b0 += b7);
  1030. b5 = RotlXor(b5, ROTATION_6_1, b2 += b5);
  1031. b3 = RotlXor(b3, ROTATION_6_2, b4 += b3);
  1032. b1 = RotlXor(b1, ROTATION_6_3, b6 += b1);
  1033. b15 = RotlXor(b15, ROTATION_6_4, b12 += b15);
  1034. b13 = RotlXor(b13, ROTATION_6_5, b14 += b13);
  1035. b11 = RotlXor(b11, ROTATION_6_6, b8 += b11);
  1036. b9 = RotlXor(b9, ROTATION_6_7, b10 += b9);
  1037. b15 = RotlXor(b15, ROTATION_7_0, b0 += b15);
  1038. b11 = RotlXor(b11, ROTATION_7_1, b2 += b11);
  1039. b13 = RotlXor(b13, ROTATION_7_2, b6 += b13);
  1040. b9 = RotlXor(b9, ROTATION_7_3, b4 += b9);
  1041. b1 = RotlXor(b1, ROTATION_7_4, b14 += b1);
  1042. b5 = RotlXor(b5, ROTATION_7_5, b8 += b5);
  1043. b3 = RotlXor(b3, ROTATION_7_6, b10 += b3);
  1044. b7 = RotlXor(b7, ROTATION_7_7, b12 += b7);
  1045. /*
  1046. * Subkey injection for next 4 rounds.
  1047. */
  1048. b0 += kw[dm17 + 1];
  1049. b1 += kw[dm17 + 2];
  1050. b2 += kw[dm17 + 3];
  1051. b3 += kw[dm17 + 4];
  1052. b4 += kw[dm17 + 5];
  1053. b5 += kw[dm17 + 6];
  1054. b6 += kw[dm17 + 7];
  1055. b7 += kw[dm17 + 8];
  1056. b8 += kw[dm17 + 9];
  1057. b9 += kw[dm17 + 10];
  1058. b10 += kw[dm17 + 11];
  1059. b11 += kw[dm17 + 12];
  1060. b12 += kw[dm17 + 13];
  1061. b13 += kw[dm17 + 14] + t[dm3 + 1];
  1062. b14 += kw[dm17 + 15] + t[dm3 + 2];
  1063. b15 += kw[dm17 + 16] + (uint)d + 1;
  1064. }
  1065. /*
  1066. * Output cipher state.
  1067. */
  1068. outWords[0] = b0;
  1069. outWords[1] = b1;
  1070. outWords[2] = b2;
  1071. outWords[3] = b3;
  1072. outWords[4] = b4;
  1073. outWords[5] = b5;
  1074. outWords[6] = b6;
  1075. outWords[7] = b7;
  1076. outWords[8] = b8;
  1077. outWords[9] = b9;
  1078. outWords[10] = b10;
  1079. outWords[11] = b11;
  1080. outWords[12] = b12;
  1081. outWords[13] = b13;
  1082. outWords[14] = b14;
  1083. outWords[15] = b15;
  1084. }
  1085. internal override void DecryptBlock(ulong[] block, ulong[] state)
  1086. {
  1087. ulong[] kw = this.kw;
  1088. ulong[] t = this.t;
  1089. int[] mod17 = MOD17;
  1090. int[] mod3 = MOD3;
  1091. /* Help the JIT avoid index bounds checks */
  1092. if (kw.Length != 33)
  1093. {
  1094. throw new ArgumentException();
  1095. }
  1096. if (t.Length != 5)
  1097. {
  1098. throw new ArgumentException();
  1099. }
  1100. ulong b0 = block[0];
  1101. ulong b1 = block[1];
  1102. ulong b2 = block[2];
  1103. ulong b3 = block[3];
  1104. ulong b4 = block[4];
  1105. ulong b5 = block[5];
  1106. ulong b6 = block[6];
  1107. ulong b7 = block[7];
  1108. ulong b8 = block[8];
  1109. ulong b9 = block[9];
  1110. ulong b10 = block[10];
  1111. ulong b11 = block[11];
  1112. ulong b12 = block[12];
  1113. ulong b13 = block[13];
  1114. ulong b14 = block[14];
  1115. ulong b15 = block[15];
  1116. for (int d = (ROUNDS_1024 / 4) - 1; d >= 1; d -= 2)
  1117. {
  1118. int dm17 = mod17[d];
  1119. int dm3 = mod3[d];
  1120. /* Reverse key injection for second 4 rounds */
  1121. b0 -= kw[dm17 + 1];
  1122. b1 -= kw[dm17 + 2];
  1123. b2 -= kw[dm17 + 3];
  1124. b3 -= kw[dm17 + 4];
  1125. b4 -= kw[dm17 + 5];
  1126. b5 -= kw[dm17 + 6];
  1127. b6 -= kw[dm17 + 7];
  1128. b7 -= kw[dm17 + 8];
  1129. b8 -= kw[dm17 + 9];
  1130. b9 -= kw[dm17 + 10];
  1131. b10 -= kw[dm17 + 11];
  1132. b11 -= kw[dm17 + 12];
  1133. b12 -= kw[dm17 + 13];
  1134. b13 -= kw[dm17 + 14] + t[dm3 + 1];
  1135. b14 -= kw[dm17 + 15] + t[dm3 + 2];
  1136. b15 -= kw[dm17 + 16] + (uint)d + 1;
  1137. /* Reverse second 4 mix/permute rounds */
  1138. b15 = XorRotr(b15, ROTATION_7_0, b0);
  1139. b0 -= b15;
  1140. b11 = XorRotr(b11, ROTATION_7_1, b2);
  1141. b2 -= b11;
  1142. b13 = XorRotr(b13, ROTATION_7_2, b6);
  1143. b6 -= b13;
  1144. b9 = XorRotr(b9, ROTATION_7_3, b4);
  1145. b4 -= b9;
  1146. b1 = XorRotr(b1, ROTATION_7_4, b14);
  1147. b14 -= b1;
  1148. b5 = XorRotr(b5, ROTATION_7_5, b8);
  1149. b8 -= b5;
  1150. b3 = XorRotr(b3, ROTATION_7_6, b10);
  1151. b10 -= b3;
  1152. b7 = XorRotr(b7, ROTATION_7_7, b12);
  1153. b12 -= b7;
  1154. b7 = XorRotr(b7, ROTATION_6_0, b0);
  1155. b0 -= b7;
  1156. b5 = XorRotr(b5, ROTATION_6_1, b2);
  1157. b2 -= b5;
  1158. b3 = XorRotr(b3, ROTATION_6_2, b4);
  1159. b4 -= b3;
  1160. b1 = XorRotr(b1, ROTATION_6_3, b6);
  1161. b6 -= b1;
  1162. b15 = XorRotr(b15, ROTATION_6_4, b12);
  1163. b12 -= b15;
  1164. b13 = XorRotr(b13, ROTATION_6_5, b14);
  1165. b14 -= b13;
  1166. b11 = XorRotr(b11, ROTATION_6_6, b8);
  1167. b8 -= b11;
  1168. b9 = XorRotr(b9, ROTATION_6_7, b10);
  1169. b10 -= b9;
  1170. b9 = XorRotr(b9, ROTATION_5_0, b0);
  1171. b0 -= b9;
  1172. b13 = XorRotr(b13, ROTATION_5_1, b2);
  1173. b2 -= b13;
  1174. b11 = XorRotr(b11, ROTATION_5_2, b6);
  1175. b6 -= b11;
  1176. b15 = XorRotr(b15, ROTATION_5_3, b4);
  1177. b4 -= b15;
  1178. b7 = XorRotr(b7, ROTATION_5_4, b10);
  1179. b10 -= b7;
  1180. b3 = XorRotr(b3, ROTATION_5_5, b12);
  1181. b12 -= b3;
  1182. b5 = XorRotr(b5, ROTATION_5_6, b14);
  1183. b14 -= b5;
  1184. b1 = XorRotr(b1, ROTATION_5_7, b8);
  1185. b8 -= b1;
  1186. b1 = XorRotr(b1, ROTATION_4_0, b0);
  1187. b0 -= b1;
  1188. b3 = XorRotr(b3, ROTATION_4_1, b2);
  1189. b2 -= b3;
  1190. b5 = XorRotr(b5, ROTATION_4_2, b4);
  1191. b4 -= b5;
  1192. b7 = XorRotr(b7, ROTATION_4_3, b6);
  1193. b6 -= b7;
  1194. b9 = XorRotr(b9, ROTATION_4_4, b8);
  1195. b8 -= b9;
  1196. b11 = XorRotr(b11, ROTATION_4_5, b10);
  1197. b10 -= b11;
  1198. b13 = XorRotr(b13, ROTATION_4_6, b12);
  1199. b12 -= b13;
  1200. b15 = XorRotr(b15, ROTATION_4_7, b14);
  1201. b14 -= b15;
  1202. /* Reverse key injection for first 4 rounds */
  1203. b0 -= kw[dm17];
  1204. b1 -= kw[dm17 + 1];
  1205. b2 -= kw[dm17 + 2];
  1206. b3 -= kw[dm17 + 3];
  1207. b4 -= kw[dm17 + 4];
  1208. b5 -= kw[dm17 + 5];
  1209. b6 -= kw[dm17 + 6];
  1210. b7 -= kw[dm17 + 7];
  1211. b8 -= kw[dm17 + 8];
  1212. b9 -= kw[dm17 + 9];
  1213. b10 -= kw[dm17 + 10];
  1214. b11 -= kw[dm17 + 11];
  1215. b12 -= kw[dm17 + 12];
  1216. b13 -= kw[dm17 + 13] + t[dm3];
  1217. b14 -= kw[dm17 + 14] + t[dm3 + 1];
  1218. b15 -= kw[dm17 + 15] + (uint)d;
  1219. /* Reverse first 4 mix/permute rounds */
  1220. b15 = XorRotr(b15, ROTATION_3_0, b0);
  1221. b0 -= b15;
  1222. b11 = XorRotr(b11, ROTATION_3_1, b2);
  1223. b2 -= b11;
  1224. b13 = XorRotr(b13, ROTATION_3_2, b6);
  1225. b6 -= b13;
  1226. b9 = XorRotr(b9, ROTATION_3_3, b4);
  1227. b4 -= b9;
  1228. b1 = XorRotr(b1, ROTATION_3_4, b14);
  1229. b14 -= b1;
  1230. b5 = XorRotr(b5, ROTATION_3_5, b8);
  1231. b8 -= b5;
  1232. b3 = XorRotr(b3, ROTATION_3_6, b10);
  1233. b10 -= b3;
  1234. b7 = XorRotr(b7, ROTATION_3_7, b12);
  1235. b12 -= b7;
  1236. b7 = XorRotr(b7, ROTATION_2_0, b0);
  1237. b0 -= b7;
  1238. b5 = XorRotr(b5, ROTATION_2_1, b2);
  1239. b2 -= b5;
  1240. b3 = XorRotr(b3, ROTATION_2_2, b4);
  1241. b4 -= b3;
  1242. b1 = XorRotr(b1, ROTATION_2_3, b6);
  1243. b6 -= b1;
  1244. b15 = XorRotr(b15, ROTATION_2_4, b12);
  1245. b12 -= b15;
  1246. b13 = XorRotr(b13, ROTATION_2_5, b14);
  1247. b14 -= b13;
  1248. b11 = XorRotr(b11, ROTATION_2_6, b8);
  1249. b8 -= b11;
  1250. b9 = XorRotr(b9, ROTATION_2_7, b10);
  1251. b10 -= b9;
  1252. b9 = XorRotr(b9, ROTATION_1_0, b0);
  1253. b0 -= b9;
  1254. b13 = XorRotr(b13, ROTATION_1_1, b2);
  1255. b2 -= b13;
  1256. b11 = XorRotr(b11, ROTATION_1_2, b6);
  1257. b6 -= b11;
  1258. b15 = XorRotr(b15, ROTATION_1_3, b4);
  1259. b4 -= b15;
  1260. b7 = XorRotr(b7, ROTATION_1_4, b10);
  1261. b10 -= b7;
  1262. b3 = XorRotr(b3, ROTATION_1_5, b12);
  1263. b12 -= b3;
  1264. b5 = XorRotr(b5, ROTATION_1_6, b14);
  1265. b14 -= b5;
  1266. b1 = XorRotr(b1, ROTATION_1_7, b8);
  1267. b8 -= b1;
  1268. b1 = XorRotr(b1, ROTATION_0_0, b0);
  1269. b0 -= b1;
  1270. b3 = XorRotr(b3, ROTATION_0_1, b2);
  1271. b2 -= b3;
  1272. b5 = XorRotr(b5, ROTATION_0_2, b4);
  1273. b4 -= b5;
  1274. b7 = XorRotr(b7, ROTATION_0_3, b6);
  1275. b6 -= b7;
  1276. b9 = XorRotr(b9, ROTATION_0_4, b8);
  1277. b8 -= b9;
  1278. b11 = XorRotr(b11, ROTATION_0_5, b10);
  1279. b10 -= b11;
  1280. b13 = XorRotr(b13, ROTATION_0_6, b12);
  1281. b12 -= b13;
  1282. b15 = XorRotr(b15, ROTATION_0_7, b14);
  1283. b14 -= b15;
  1284. }
  1285. /*
  1286. * First subkey uninjection.
  1287. */
  1288. b0 -= kw[0];
  1289. b1 -= kw[1];
  1290. b2 -= kw[2];
  1291. b3 -= kw[3];
  1292. b4 -= kw[4];
  1293. b5 -= kw[5];
  1294. b6 -= kw[6];
  1295. b7 -= kw[7];
  1296. b8 -= kw[8];
  1297. b9 -= kw[9];
  1298. b10 -= kw[10];
  1299. b11 -= kw[11];
  1300. b12 -= kw[12];
  1301. b13 -= kw[13] + t[0];
  1302. b14 -= kw[14] + t[1];
  1303. b15 -= kw[15];
  1304. /*
  1305. * Output cipher state.
  1306. */
  1307. state[0] = b0;
  1308. state[1] = b1;
  1309. state[2] = b2;
  1310. state[3] = b3;
  1311. state[4] = b4;
  1312. state[5] = b5;
  1313. state[6] = b6;
  1314. state[7] = b7;
  1315. state[8] = b8;
  1316. state[9] = b9;
  1317. state[10] = b10;
  1318. state[11] = b11;
  1319. state[12] = b12;
  1320. state[13] = b13;
  1321. state[14] = b14;
  1322. state[15] = b15;
  1323. }
  1324. }
  1325. }
  1326. }
  1327. #pragma warning restore
  1328. #endif