FastChaChaEngine.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. #if NETCOREAPP3_0_OR_GREATER
  6. using System.Buffers.Binary;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. using System.Runtime.Intrinsics;
  10. using System.Runtime.Intrinsics.X86;
  11. #endif
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  14. namespace Best.HTTP.Shared.TLS.Crypto.Impl
  15. {
  16. /// <summary>
  17. /// Implementation of Daniel J. Bernstein's ChaCha stream cipher.
  18. /// </summary>
  19. [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  20. public sealed class FastChaChaEngine
  21. : FastSalsa20Engine
  22. {
  23. /// <summary>
  24. /// Creates a 20 rounds ChaCha engine.
  25. /// </summary>
  26. public FastChaChaEngine()
  27. {
  28. }
  29. /// <summary>
  30. /// Creates a ChaCha engine with a specific number of rounds.
  31. /// </summary>
  32. /// <param name="rounds">the number of rounds (must be an even number).</param>
  33. public FastChaChaEngine(int rounds)
  34. : base(rounds)
  35. {
  36. }
  37. public override string AlgorithmName
  38. {
  39. get { return "ChaCha" + rounds; }
  40. }
  41. protected override void AdvanceCounter()
  42. {
  43. if (++engineState[12] == 0)
  44. {
  45. ++engineState[13];
  46. }
  47. }
  48. protected override void ResetCounter()
  49. {
  50. engineState[12] = engineState[13] = 0;
  51. }
  52. protected override void SetKey(byte[] keyBytes, byte[] ivBytes)
  53. {
  54. if (keyBytes != null)
  55. {
  56. if ((keyBytes.Length != 16) && (keyBytes.Length != 32))
  57. throw new ArgumentException(AlgorithmName + " requires 128 bit or 256 bit key");
  58. PackTauOrSigma(keyBytes.Length, engineState, 0);
  59. // Key
  60. Pack.LE_To_UInt32(keyBytes, 0, engineState, 4, 4);
  61. Pack.LE_To_UInt32(keyBytes, keyBytes.Length - 16, engineState, 8, 4);
  62. }
  63. // IV
  64. Pack.LE_To_UInt32(ivBytes, 0, engineState, 14, 2);
  65. }
  66. protected override void GenerateKeyStream(byte[] output)
  67. {
  68. //ChachaCore(rounds, engineState, output);
  69. FastChaChaEngineHelper.ChachaCore(rounds, engineState, output);
  70. }
  71. internal static void ChachaCore(int rounds, uint[] input, byte[] output)
  72. {
  73. Debug.Assert(rounds % 2 == 0);
  74. Debug.Assert(input.Length >= 16);
  75. Debug.Assert(output.Length >= 64);
  76. #if NETCOREAPP3_0_OR_GREATER
  77. if (Sse2.IsSupported)
  78. {
  79. var x0 = Load128_UInt32(input.AsSpan());
  80. var x1 = Load128_UInt32(input.AsSpan(4));
  81. var x2 = Load128_UInt32(input.AsSpan(8));
  82. var x3 = Load128_UInt32(input.AsSpan(12));
  83. var v0 = x0;
  84. var v1 = x1;
  85. var v2 = x2;
  86. var v3 = x3;
  87. for (int i = rounds; i > 0; i -= 2)
  88. {
  89. v0 = Sse2.Add(v0, v1);
  90. v3 = Sse2.Xor(v3, v0);
  91. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 16), Sse2.ShiftRightLogical(v3, 16));
  92. v2 = Sse2.Add(v2, v3);
  93. v1 = Sse2.Xor(v1, v2);
  94. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 12), Sse2.ShiftRightLogical(v1, 20));
  95. v0 = Sse2.Add(v0, v1);
  96. v3 = Sse2.Xor(v3, v0);
  97. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 8), Sse2.ShiftRightLogical(v3, 24));
  98. v2 = Sse2.Add(v2, v3);
  99. v1 = Sse2.Xor(v1, v2);
  100. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 7), Sse2.ShiftRightLogical(v1, 25));
  101. v1 = Sse2.Shuffle(v1, 0x39);
  102. v2 = Sse2.Shuffle(v2, 0x4E);
  103. v3 = Sse2.Shuffle(v3, 0x93);
  104. v0 = Sse2.Add(v0, v1);
  105. v3 = Sse2.Xor(v3, v0);
  106. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 16), Sse2.ShiftRightLogical(v3, 16));
  107. v2 = Sse2.Add(v2, v3);
  108. v1 = Sse2.Xor(v1, v2);
  109. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 12), Sse2.ShiftRightLogical(v1, 20));
  110. v0 = Sse2.Add(v0, v1);
  111. v3 = Sse2.Xor(v3, v0);
  112. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 8), Sse2.ShiftRightLogical(v3, 24));
  113. v2 = Sse2.Add(v2, v3);
  114. v1 = Sse2.Xor(v1, v2);
  115. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 7), Sse2.ShiftRightLogical(v1, 25));
  116. v1 = Sse2.Shuffle(v1, 0x93);
  117. v2 = Sse2.Shuffle(v2, 0x4E);
  118. v3 = Sse2.Shuffle(v3, 0x39);
  119. }
  120. v0 = Sse2.Add(v0, x0);
  121. v1 = Sse2.Add(v1, x1);
  122. v2 = Sse2.Add(v2, x2);
  123. v3 = Sse2.Add(v3, x3);
  124. Store128_UInt32(v0, output.AsSpan());
  125. Store128_UInt32(v1, output.AsSpan(0x10));
  126. Store128_UInt32(v2, output.AsSpan(0x20));
  127. Store128_UInt32(v3, output.AsSpan(0x30));
  128. return;
  129. }
  130. #endif
  131. {
  132. uint x00 = input[0], x01 = input[1], x02 = input[2], x03 = input[3];
  133. uint x04 = input[4], x05 = input[5], x06 = input[6], x07 = input[7];
  134. uint x08 = input[8], x09 = input[9], x10 = input[10], x11 = input[11];
  135. uint x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
  136. for (int i = rounds; i > 0; i -= 2)
  137. {
  138. x00 += x04; x12 = Integers.RotateLeft(x12 ^ x00, 16);
  139. x01 += x05; x13 = Integers.RotateLeft(x13 ^ x01, 16);
  140. x02 += x06; x14 = Integers.RotateLeft(x14 ^ x02, 16);
  141. x03 += x07; x15 = Integers.RotateLeft(x15 ^ x03, 16);
  142. x08 += x12; x04 = Integers.RotateLeft(x04 ^ x08, 12);
  143. x09 += x13; x05 = Integers.RotateLeft(x05 ^ x09, 12);
  144. x10 += x14; x06 = Integers.RotateLeft(x06 ^ x10, 12);
  145. x11 += x15; x07 = Integers.RotateLeft(x07 ^ x11, 12);
  146. x00 += x04; x12 = Integers.RotateLeft(x12 ^ x00, 8);
  147. x01 += x05; x13 = Integers.RotateLeft(x13 ^ x01, 8);
  148. x02 += x06; x14 = Integers.RotateLeft(x14 ^ x02, 8);
  149. x03 += x07; x15 = Integers.RotateLeft(x15 ^ x03, 8);
  150. x08 += x12; x04 = Integers.RotateLeft(x04 ^ x08, 7);
  151. x09 += x13; x05 = Integers.RotateLeft(x05 ^ x09, 7);
  152. x10 += x14; x06 = Integers.RotateLeft(x06 ^ x10, 7);
  153. x11 += x15; x07 = Integers.RotateLeft(x07 ^ x11, 7);
  154. x00 += x05; x15 = Integers.RotateLeft(x15 ^ x00, 16);
  155. x01 += x06; x12 = Integers.RotateLeft(x12 ^ x01, 16);
  156. x02 += x07; x13 = Integers.RotateLeft(x13 ^ x02, 16);
  157. x03 += x04; x14 = Integers.RotateLeft(x14 ^ x03, 16);
  158. x10 += x15; x05 = Integers.RotateLeft(x05 ^ x10, 12);
  159. x11 += x12; x06 = Integers.RotateLeft(x06 ^ x11, 12);
  160. x08 += x13; x07 = Integers.RotateLeft(x07 ^ x08, 12);
  161. x09 += x14; x04 = Integers.RotateLeft(x04 ^ x09, 12);
  162. x00 += x05; x15 = Integers.RotateLeft(x15 ^ x00, 8);
  163. x01 += x06; x12 = Integers.RotateLeft(x12 ^ x01, 8);
  164. x02 += x07; x13 = Integers.RotateLeft(x13 ^ x02, 8);
  165. x03 += x04; x14 = Integers.RotateLeft(x14 ^ x03, 8);
  166. x10 += x15; x05 = Integers.RotateLeft(x05 ^ x10, 7);
  167. x11 += x12; x06 = Integers.RotateLeft(x06 ^ x11, 7);
  168. x08 += x13; x07 = Integers.RotateLeft(x07 ^ x08, 7);
  169. x09 += x14; x04 = Integers.RotateLeft(x04 ^ x09, 7);
  170. }
  171. Pack.UInt32_To_LE(x00 + input[0], output, 0);
  172. Pack.UInt32_To_LE(x01 + input[1], output, 4);
  173. Pack.UInt32_To_LE(x02 + input[2], output, 8);
  174. Pack.UInt32_To_LE(x03 + input[3], output, 12);
  175. Pack.UInt32_To_LE(x04 + input[4], output, 16);
  176. Pack.UInt32_To_LE(x05 + input[5], output, 20);
  177. Pack.UInt32_To_LE(x06 + input[6], output, 24);
  178. Pack.UInt32_To_LE(x07 + input[7], output, 28);
  179. Pack.UInt32_To_LE(x08 + input[8], output, 32);
  180. Pack.UInt32_To_LE(x09 + input[9], output, 36);
  181. Pack.UInt32_To_LE(x10 + input[10], output, 40);
  182. Pack.UInt32_To_LE(x11 + input[11], output, 44);
  183. Pack.UInt32_To_LE(x12 + input[12], output, 48);
  184. Pack.UInt32_To_LE(x13 + input[13], output, 52);
  185. Pack.UInt32_To_LE(x14 + input[14], output, 56);
  186. Pack.UInt32_To_LE(x15 + input[15], output, 60);
  187. }
  188. }
  189. #if NETCOREAPP3_0_OR_GREATER
  190. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  191. private static Vector128<uint> Load128_UInt32(ReadOnlySpan<uint> t)
  192. {
  193. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<uint>>() == 16)
  194. return MemoryMarshal.Read<Vector128<uint>>(MemoryMarshal.AsBytes(t));
  195. return Vector128.Create(t[0], t[1], t[2], t[3]);
  196. }
  197. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  198. private static void Store128_UInt32(Vector128<uint> s, Span<byte> t)
  199. {
  200. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<uint>>() == 16)
  201. {
  202. MemoryMarshal.Write(t, ref s);
  203. return;
  204. }
  205. var u = s.AsUInt64();
  206. BinaryPrimitives.WriteUInt64LittleEndian(t[..8], u.GetElement(0));
  207. BinaryPrimitives.WriteUInt64LittleEndian(t[8..], u.GetElement(1));
  208. }
  209. #endif
  210. }
  211. }
  212. #pragma warning restore
  213. #endif