ChaChaEngine.cs 8.1 KB

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