ThreefishEngine.cs 40 KB

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