ISAACEngine.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. namespace Best.HTTP.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 - " + 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. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  87. public virtual void ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
  88. {
  89. if (!initialised)
  90. throw new InvalidOperationException(AlgorithmName + " not initialised");
  91. Check.OutputLength(output, input.Length, "output buffer too short");
  92. for (int i = 0; i < input.Length; i++)
  93. {
  94. if (index == 0)
  95. {
  96. isaac();
  97. keyStream = Pack.UInt32_To_BE(results);
  98. }
  99. output[i] = (byte)(keyStream[index++] ^ input[i]);
  100. index &= 1023;
  101. }
  102. }
  103. #endif
  104. public virtual string AlgorithmName
  105. {
  106. get { return "ISAAC"; }
  107. }
  108. public virtual void Reset()
  109. {
  110. setKey(workingKey);
  111. }
  112. // Private implementation
  113. private void setKey(
  114. byte[] keyBytes)
  115. {
  116. workingKey = keyBytes;
  117. if (engineState == null)
  118. {
  119. engineState = new uint[stateArraySize];
  120. }
  121. if (results == null)
  122. {
  123. results = new uint[stateArraySize];
  124. }
  125. int i, j, k;
  126. // Reset state
  127. for (i = 0; i < stateArraySize; i++)
  128. {
  129. engineState[i] = results[i] = 0;
  130. }
  131. a = b = c = 0;
  132. // Reset index counter for output
  133. index = 0;
  134. // Convert the key bytes to ints and put them into results[] for initialization
  135. byte[] t = new byte[keyBytes.Length + (keyBytes.Length & 3)];
  136. Array.Copy(keyBytes, 0, t, 0, keyBytes.Length);
  137. for (i = 0; i < t.Length; i+=4)
  138. {
  139. results[i >> 2] = Pack.LE_To_UInt32(t, i);
  140. }
  141. // It has begun?
  142. uint[] abcdefgh = new uint[sizeL];
  143. for (i = 0; i < sizeL; i++)
  144. {
  145. abcdefgh[i] = 0x9e3779b9; // Phi (golden ratio)
  146. }
  147. for (i = 0; i < 4; i++)
  148. {
  149. mix(abcdefgh);
  150. }
  151. for (i = 0; i < 2; i++)
  152. {
  153. for (j = 0; j < stateArraySize; j+=sizeL)
  154. {
  155. for (k = 0; k < sizeL; k++)
  156. {
  157. abcdefgh[k] += (i<1) ? results[j+k] : engineState[j+k];
  158. }
  159. mix(abcdefgh);
  160. for (k = 0; k < sizeL; k++)
  161. {
  162. engineState[j+k] = abcdefgh[k];
  163. }
  164. }
  165. }
  166. isaac();
  167. initialised = true;
  168. }
  169. private void isaac()
  170. {
  171. uint x, y;
  172. b += ++c;
  173. for (int i = 0; i < stateArraySize; i++)
  174. {
  175. x = engineState[i];
  176. switch (i & 3)
  177. {
  178. case 0: a ^= (a << 13); break;
  179. case 1: a ^= (a >> 6); break;
  180. case 2: a ^= (a << 2); break;
  181. case 3: a ^= (a >> 16); break;
  182. }
  183. a += engineState[(i+128) & 0xFF];
  184. engineState[i] = y = engineState[(int)((uint)x >> 2) & 0xFF] + a + b;
  185. results[i] = b = engineState[(int)((uint)y >> 10) & 0xFF] + x;
  186. }
  187. }
  188. private void mix(uint[] x)
  189. {
  190. x[0]^=x[1]<< 11; x[3]+=x[0]; x[1]+=x[2];
  191. x[1]^=x[2]>> 2; x[4]+=x[1]; x[2]+=x[3];
  192. x[2]^=x[3]<< 8; x[5]+=x[2]; x[3]+=x[4];
  193. x[3]^=x[4]>> 16; x[6]+=x[3]; x[4]+=x[5];
  194. x[4]^=x[5]<< 10; x[7]+=x[4]; x[5]+=x[6];
  195. x[5]^=x[6]>> 4; x[0]+=x[5]; x[6]+=x[7];
  196. x[6]^=x[7]<< 8; x[1]+=x[6]; x[7]+=x[0];
  197. x[7]^=x[0]>> 9; x[2]+=x[7]; x[0]+=x[1];
  198. }
  199. }
  200. }
  201. #pragma warning restore
  202. #endif