HC128Engine.cs 5.0 KB

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