HC256Engine.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. using Org.BouncyCastle.Crypto.Utilities;
  5. using Org.BouncyCastle.Utilities;
  6. namespace Org.BouncyCastle.Crypto.Engines
  7. {
  8. /**
  9. * HC-256 is a software-efficient stream cipher created by Hongjun Wu. It
  10. * generates keystream from a 256-bit secret key and a 256-bit initialization
  11. * vector.
  12. * <p>
  13. * http://www.ecrypt.eu.org/stream/p3ciphers/hc/hc256_p3.pdf
  14. * </p><p>
  15. * Its brother, HC-128, is a third phase candidate in the eStream contest.
  16. * The algorithm is patent-free. No attacks are known as of today (April 2007).
  17. * See
  18. *
  19. * http://www.ecrypt.eu.org/stream/hcp3.html
  20. * </p>
  21. */
  22. public class HC256Engine
  23. : IStreamCipher
  24. {
  25. private uint[] p = new uint[1024];
  26. private uint[] q = new uint[1024];
  27. private uint cnt = 0;
  28. private uint Step()
  29. {
  30. uint j = cnt & 0x3FF;
  31. uint ret;
  32. if (cnt < 1024)
  33. {
  34. uint x = p[(j - 3 & 0x3FF)];
  35. uint y = p[(j - 1023 & 0x3FF)];
  36. p[j] += p[(j - 10 & 0x3FF)]
  37. + (RotateRight(x, 10) ^ RotateRight(y, 23))
  38. + q[((x ^ y) & 0x3FF)];
  39. x = p[(j - 12 & 0x3FF)];
  40. ret = (q[x & 0xFF] + q[((x >> 8) & 0xFF) + 256]
  41. + q[((x >> 16) & 0xFF) + 512] + q[((x >> 24) & 0xFF) + 768])
  42. ^ p[j];
  43. }
  44. else
  45. {
  46. uint x = q[(j - 3 & 0x3FF)];
  47. uint y = q[(j - 1023 & 0x3FF)];
  48. q[j] += q[(j - 10 & 0x3FF)]
  49. + (RotateRight(x, 10) ^ RotateRight(y, 23))
  50. + p[((x ^ y) & 0x3FF)];
  51. x = q[(j - 12 & 0x3FF)];
  52. ret = (p[x & 0xFF] + p[((x >> 8) & 0xFF) + 256]
  53. + p[((x >> 16) & 0xFF) + 512] + p[((x >> 24) & 0xFF) + 768])
  54. ^ q[j];
  55. }
  56. cnt = cnt + 1 & 0x7FF;
  57. return ret;
  58. }
  59. private byte[] key, iv;
  60. private bool initialised;
  61. private void Init()
  62. {
  63. if (key.Length != 32 && key.Length != 16)
  64. throw new ArgumentException("The key must be 128/256 bits long");
  65. if (iv.Length < 16)
  66. throw new ArgumentException("The IV must be at least 128 bits long");
  67. if (key.Length != 32)
  68. {
  69. byte[] k = new byte[32];
  70. Array.Copy(key, 0, k, 0, key.Length);
  71. Array.Copy(key, 0, k, 16, key.Length);
  72. key = k;
  73. }
  74. if (iv.Length < 32)
  75. {
  76. byte[] newIV = new byte[32];
  77. Array.Copy(iv, 0, newIV, 0, iv.Length);
  78. Array.Copy(iv, 0, newIV, iv.Length, newIV.Length - iv.Length);
  79. iv = newIV;
  80. }
  81. idx = 0;
  82. cnt = 0;
  83. uint[] w = new uint[2560];
  84. for (int i = 0; i < 32; i++)
  85. {
  86. w[i >> 2] |= ((uint)key[i] << (8 * (i & 0x3)));
  87. }
  88. for (int i = 0; i < 32; i++)
  89. {
  90. w[(i >> 2) + 8] |= ((uint)iv[i] << (8 * (i & 0x3)));
  91. }
  92. for (uint i = 16; i < 2560; i++)
  93. {
  94. uint x = w[i - 2];
  95. uint y = w[i - 15];
  96. w[i] = (RotateRight(x, 17) ^ RotateRight(x, 19) ^ (x >> 10))
  97. + w[i - 7]
  98. + (RotateRight(y, 7) ^ RotateRight(y, 18) ^ (y >> 3))
  99. + w[i - 16] + i;
  100. }
  101. Array.Copy(w, 512, p, 0, 1024);
  102. Array.Copy(w, 1536, q, 0, 1024);
  103. for (int i = 0; i < 4096; i++)
  104. {
  105. Step();
  106. }
  107. cnt = 0;
  108. }
  109. public virtual string AlgorithmName
  110. {
  111. get { return "HC-256"; }
  112. }
  113. /**
  114. * Initialise a HC-256 cipher.
  115. *
  116. * @param forEncryption whether or not we are for encryption. Irrelevant, as
  117. * encryption and decryption are the same.
  118. * @param params the parameters required to set up the cipher.
  119. * @throws ArgumentException if the params argument is
  120. * inappropriate (ie. the key is not 256 bit long).
  121. */
  122. public virtual void Init(
  123. bool forEncryption,
  124. ICipherParameters parameters)
  125. {
  126. ICipherParameters keyParam = parameters;
  127. if (parameters is ParametersWithIV)
  128. {
  129. iv = ((ParametersWithIV)parameters).GetIV();
  130. keyParam = ((ParametersWithIV)parameters).Parameters;
  131. }
  132. else
  133. {
  134. iv = new byte[0];
  135. }
  136. if (keyParam is KeyParameter)
  137. {
  138. key = ((KeyParameter)keyParam).GetKey();
  139. Init();
  140. }
  141. else
  142. {
  143. throw new ArgumentException(
  144. "Invalid parameter passed to HC256 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters),
  145. "parameters");
  146. }
  147. initialised = true;
  148. }
  149. private byte[] buf = new byte[4];
  150. private int idx = 0;
  151. private byte GetByte()
  152. {
  153. if (idx == 0)
  154. {
  155. Pack.UInt32_To_LE(Step(), buf);
  156. }
  157. byte ret = buf[idx];
  158. idx = idx + 1 & 0x3;
  159. return ret;
  160. }
  161. public virtual void ProcessBytes(
  162. byte[] input,
  163. int inOff,
  164. int len,
  165. byte[] output,
  166. int outOff)
  167. {
  168. if (!initialised)
  169. throw new InvalidOperationException(AlgorithmName + " not initialised");
  170. Check.DataLength(input, inOff, len, "input buffer too short");
  171. Check.OutputLength(output, outOff, len, "output buffer too short");
  172. for (int i = 0; i < len; i++)
  173. {
  174. output[outOff + i] = (byte)(input[inOff + i] ^ GetByte());
  175. }
  176. }
  177. public virtual void Reset()
  178. {
  179. Init();
  180. }
  181. public virtual byte ReturnByte(byte input)
  182. {
  183. return (byte)(input ^ GetByte());
  184. }
  185. private static uint RotateRight(uint x, int bits)
  186. {
  187. return (x >> bits) | (x << -bits);
  188. }
  189. }
  190. }
  191. #endif