ISAACEngine.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  8. {
  9. /**
  10. * Implementation of Bob Jenkin's ISAAC (Indirection Shift Accumulate Add and Count).
  11. * see: http://www.burtleburtle.net/bob/rand/isaacafa.html
  12. */
  13. public class IsaacEngine
  14. : IStreamCipher
  15. {
  16. // Constants
  17. private static readonly int sizeL = 8,
  18. stateArraySize = sizeL<<5; // 256
  19. // Cipher's internal state
  20. private uint[] engineState = null, // mm
  21. results = null; // randrsl
  22. private uint a = 0, b = 0, c = 0;
  23. // Engine state
  24. private int index = 0;
  25. private byte[] keyStream = new byte[stateArraySize<<2], // results expanded into bytes
  26. workingKey = null;
  27. private bool initialised = false;
  28. /**
  29. * initialise an ISAAC cipher.
  30. *
  31. * @param forEncryption whether or not we are for encryption.
  32. * @param params the parameters required to set up the cipher.
  33. * @exception ArgumentException if the params argument is
  34. * inappropriate.
  35. */
  36. public virtual void Init(
  37. bool forEncryption,
  38. ICipherParameters parameters)
  39. {
  40. if (!(parameters is KeyParameter))
  41. throw new ArgumentException(
  42. "invalid parameter passed to ISAAC Init - " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters),
  43. "parameters");
  44. /*
  45. * ISAAC encryption and decryption is completely
  46. * symmetrical, so the 'forEncryption' is
  47. * irrelevant.
  48. */
  49. KeyParameter p = (KeyParameter) parameters;
  50. setKey(p.GetKey());
  51. }
  52. public virtual byte ReturnByte(
  53. byte input)
  54. {
  55. if (index == 0)
  56. {
  57. isaac();
  58. keyStream = Pack.UInt32_To_BE(results);
  59. }
  60. byte output = (byte)(keyStream[index]^input);
  61. index = (index + 1) & 1023;
  62. return output;
  63. }
  64. public virtual void ProcessBytes(
  65. byte[] input,
  66. int inOff,
  67. int len,
  68. byte[] output,
  69. int outOff)
  70. {
  71. if (!initialised)
  72. throw new InvalidOperationException(AlgorithmName + " not initialised");
  73. Check.DataLength(input, inOff, len, "input buffer too short");
  74. Check.OutputLength(output, outOff, len, "output buffer too short");
  75. for (int i = 0; i < len; i++)
  76. {
  77. if (index == 0)
  78. {
  79. isaac();
  80. keyStream = Pack.UInt32_To_BE(results);
  81. }
  82. output[i+outOff] = (byte)(keyStream[index]^input[i+inOff]);
  83. index = (index + 1) & 1023;
  84. }
  85. }
  86. public virtual string AlgorithmName
  87. {
  88. get { return "ISAAC"; }
  89. }
  90. public virtual void Reset()
  91. {
  92. setKey(workingKey);
  93. }
  94. // Private implementation
  95. private void setKey(
  96. byte[] keyBytes)
  97. {
  98. workingKey = keyBytes;
  99. if (engineState == null)
  100. {
  101. engineState = new uint[stateArraySize];
  102. }
  103. if (results == null)
  104. {
  105. results = new uint[stateArraySize];
  106. }
  107. int i, j, k;
  108. // Reset state
  109. for (i = 0; i < stateArraySize; i++)
  110. {
  111. engineState[i] = results[i] = 0;
  112. }
  113. a = b = c = 0;
  114. // Reset index counter for output
  115. index = 0;
  116. // Convert the key bytes to ints and put them into results[] for initialization
  117. byte[] t = new byte[keyBytes.Length + (keyBytes.Length & 3)];
  118. Array.Copy(keyBytes, 0, t, 0, keyBytes.Length);
  119. for (i = 0; i < t.Length; i+=4)
  120. {
  121. results[i >> 2] = Pack.LE_To_UInt32(t, i);
  122. }
  123. // It has begun?
  124. uint[] abcdefgh = new uint[sizeL];
  125. for (i = 0; i < sizeL; i++)
  126. {
  127. abcdefgh[i] = 0x9e3779b9; // Phi (golden ratio)
  128. }
  129. for (i = 0; i < 4; i++)
  130. {
  131. mix(abcdefgh);
  132. }
  133. for (i = 0; i < 2; i++)
  134. {
  135. for (j = 0; j < stateArraySize; j+=sizeL)
  136. {
  137. for (k = 0; k < sizeL; k++)
  138. {
  139. abcdefgh[k] += (i<1) ? results[j+k] : engineState[j+k];
  140. }
  141. mix(abcdefgh);
  142. for (k = 0; k < sizeL; k++)
  143. {
  144. engineState[j+k] = abcdefgh[k];
  145. }
  146. }
  147. }
  148. isaac();
  149. initialised = true;
  150. }
  151. private void isaac()
  152. {
  153. uint x, y;
  154. b += ++c;
  155. for (int i = 0; i < stateArraySize; i++)
  156. {
  157. x = engineState[i];
  158. switch (i & 3)
  159. {
  160. case 0: a ^= (a << 13); break;
  161. case 1: a ^= (a >> 6); break;
  162. case 2: a ^= (a << 2); break;
  163. case 3: a ^= (a >> 16); break;
  164. }
  165. a += engineState[(i+128) & 0xFF];
  166. engineState[i] = y = engineState[(int)((uint)x >> 2) & 0xFF] + a + b;
  167. results[i] = b = engineState[(int)((uint)y >> 10) & 0xFF] + x;
  168. }
  169. }
  170. private void mix(uint[] x)
  171. {
  172. x[0]^=x[1]<< 11; x[3]+=x[0]; x[1]+=x[2];
  173. x[1]^=x[2]>> 2; x[4]+=x[1]; x[2]+=x[3];
  174. x[2]^=x[3]<< 8; x[5]+=x[2]; x[3]+=x[4];
  175. x[3]^=x[4]>> 16; x[6]+=x[3]; x[4]+=x[5];
  176. x[4]^=x[5]<< 10; x[7]+=x[4]; x[5]+=x[6];
  177. x[5]^=x[6]>> 4; x[0]+=x[5]; x[6]+=x[7];
  178. x[6]^=x[7]<< 8; x[1]+=x[6]; x[7]+=x[0];
  179. x[7]^=x[0]>> 9; x[2]+=x[7]; x[0]+=x[1];
  180. }
  181. }
  182. }
  183. #pragma warning restore
  184. #endif