FastChaChaEngineHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. #if BESTHTTP_WITH_BURST
  6. using Unity.Burst;
  7. using Unity.Burst.Intrinsics;
  8. using static Unity.Burst.Intrinsics.X86;
  9. using static Unity.Burst.Intrinsics.Arm;
  10. #endif
  11. namespace Best.HTTP.Shared.TLS.Crypto.Impl
  12. {
  13. #if BESTHTTP_WITH_BURST
  14. [Unity.Burst.BurstCompile]
  15. #endif
  16. internal static class FastChaChaEngineHelper
  17. {
  18. internal unsafe static void ChachaCore(int rounds, uint[] input, byte[] output)
  19. {
  20. fixed (uint* pinput = input)
  21. fixed (byte* poutput = output)
  22. ChachaCoreImpl(rounds, pinput, poutput);
  23. }
  24. #if BESTHTTP_WITH_BURST
  25. [Unity.Burst.BurstCompile]
  26. [Unity.Burst.CompilerServices.SkipLocalsInit]
  27. #endif
  28. internal unsafe static void ChachaCoreImpl(int rounds,
  29. #if BESTHTTP_WITH_BURST
  30. [NoAlias]
  31. #endif
  32. uint* input,
  33. #if BESTHTTP_WITH_BURST
  34. [NoAlias]
  35. #endif
  36. byte* output)
  37. {
  38. uint* x = stackalloc uint[16];
  39. for (int i = 0; i < 16; i++)
  40. x[i] = input[i];
  41. uint tmp = 0;
  42. for (int i = rounds; i > 0; i -= 2)
  43. {
  44. x[00] += x[04]; tmp = x[12] ^ x[00]; x[12] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[12] ^ x[00], 16);
  45. x[01] += x[05]; tmp = x[13] ^ x[01]; x[13] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[13] ^ x[01], 16);
  46. x[02] += x[06]; tmp = x[14] ^ x[02]; x[14] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[14] ^ x[02], 16);
  47. x[03] += x[07]; tmp = x[15] ^ x[03]; x[15] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[15] ^ x[03], 16);
  48. x[08] += x[12]; tmp = x[04] ^ x[08]; x[04] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[04] ^ x[08], 12);
  49. x[09] += x[13]; tmp = x[05] ^ x[09]; x[05] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[05] ^ x[09], 12);
  50. x[10] += x[14]; tmp = x[06] ^ x[10]; x[06] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[06] ^ x[10], 12);
  51. x[11] += x[15]; tmp = x[07] ^ x[11]; x[07] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[07] ^ x[11], 12);
  52. x[00] += x[04]; tmp = x[12] ^ x[00]; x[12] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[12] ^ x[00], 8);
  53. x[01] += x[05]; tmp = x[13] ^ x[01]; x[13] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[13] ^ x[01], 8);
  54. x[02] += x[06]; tmp = x[14] ^ x[02]; x[14] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[14] ^ x[02], 8);
  55. x[03] += x[07]; tmp = x[15] ^ x[03]; x[15] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[15] ^ x[03], 8);
  56. x[08] += x[12]; tmp = x[04] ^ x[08]; x[04] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[04] ^ x[08], 7);
  57. x[09] += x[13]; tmp = x[05] ^ x[09]; x[05] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[05] ^ x[09], 7);
  58. x[10] += x[14]; tmp = x[06] ^ x[10]; x[06] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[06] ^ x[10], 7);
  59. x[11] += x[15]; tmp = x[07] ^ x[11]; x[07] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[07] ^ x[11], 7);
  60. x[00] += x[05]; tmp = x[15] ^ x[00]; x[15] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[15] ^ x[00], 16);
  61. x[01] += x[06]; tmp = x[12] ^ x[01]; x[12] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[12] ^ x[01], 16);
  62. x[02] += x[07]; tmp = x[13] ^ x[02]; x[13] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[13] ^ x[02], 16);
  63. x[03] += x[04]; tmp = x[14] ^ x[03]; x[14] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[14] ^ x[03], 16);
  64. x[10] += x[15]; tmp = x[05] ^ x[10]; x[05] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[05] ^ x[10], 12);
  65. x[11] += x[12]; tmp = x[06] ^ x[11]; x[06] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[06] ^ x[11], 12);
  66. x[08] += x[13]; tmp = x[07] ^ x[08]; x[07] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[07] ^ x[08], 12);
  67. x[09] += x[14]; tmp = x[04] ^ x[09]; x[04] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[04] ^ x[09], 12);
  68. x[00] += x[05]; tmp = x[15] ^ x[00]; x[15] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[15] ^ x[00], 8);
  69. x[01] += x[06]; tmp = x[12] ^ x[01]; x[12] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[12] ^ x[01], 8);
  70. x[02] += x[07]; tmp = x[13] ^ x[02]; x[13] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[13] ^ x[02], 8);
  71. x[03] += x[04]; tmp = x[14] ^ x[03]; x[14] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[14] ^ x[03], 8);
  72. x[10] += x[15]; tmp = x[05] ^ x[10]; x[05] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[05] ^ x[10], 7);
  73. x[11] += x[12]; tmp = x[06] ^ x[11]; x[06] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[06] ^ x[11], 7);
  74. x[08] += x[13]; tmp = x[07] ^ x[08]; x[07] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[07] ^ x[08], 7);
  75. x[09] += x[14]; tmp = x[04] ^ x[09]; x[04] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[04] ^ x[09], 7);
  76. }
  77. for (int i = 0; i < 16; i++)
  78. {
  79. uint n = x[i] + input[i];
  80. output[(i * 4)] = (byte)n;
  81. output[(i * 4) + 1] = (byte)(n >> 8);
  82. output[(i * 4) + 2] = (byte)(n >> 16);
  83. output[(i * 4) + 3] = (byte)(n >> 24);
  84. }
  85. }
  86. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. internal static unsafe void ImplProcessBlock(ReadOnlySpan<byte> input, Span<byte> output, byte[] keyStream)
  89. {
  90. fixed (byte* pinput = input)
  91. fixed (byte* poutput = output)
  92. fixed (byte* pkeyStream = keyStream)
  93. {
  94. #if UNITY_ANDROID && !UNITY_EDITOR
  95. #if BESTHTTP_WITH_BURST
  96. if (!ImplProcessBlock_Burst(pinput, poutput, pkeyStream))
  97. #endif
  98. {
  99. if ((long)pinput % sizeof(ulong) == 0)
  100. {
  101. #endif
  102. var pulinput = (ulong*)pinput;
  103. var puloutput = (ulong*)poutput;
  104. var pulkeyStream = (ulong*)pkeyStream;
  105. puloutput[7] = pulkeyStream[7] ^ pulinput[7];
  106. puloutput[6] = pulkeyStream[6] ^ pulinput[6];
  107. puloutput[5] = pulkeyStream[5] ^ pulinput[5];
  108. puloutput[4] = pulkeyStream[4] ^ pulinput[4];
  109. puloutput[3] = pulkeyStream[3] ^ pulinput[3];
  110. puloutput[2] = pulkeyStream[2] ^ pulinput[2];
  111. puloutput[1] = pulkeyStream[1] ^ pulinput[1];
  112. puloutput[0] = pulkeyStream[0] ^ pulinput[0];
  113. #if UNITY_ANDROID && !UNITY_EDITOR
  114. }
  115. else
  116. {
  117. for (int i = 0; i < 64; ++i)
  118. output[i] = (byte)(keyStream[i] ^ input[i]);
  119. }
  120. }
  121. #endif
  122. }
  123. }
  124. #if UNITY_ANDROID && !UNITY_EDITOR && BESTHTTP_WITH_BURST
  125. [BurstCompile]
  126. private unsafe static bool ImplProcessBlock_Burst(byte* pinput, byte* poutput, [NoAlias] byte* pkeyStream)
  127. {
  128. if (Neon.IsNeonSupported)
  129. {
  130. for (int offset = 0; offset < 64; offset += 16)
  131. {
  132. var vInput = Neon.vld1q_u8(pinput + offset);
  133. var vKeyStream = Neon.vld1q_u8(pkeyStream + offset);
  134. var vOut = Neon.veorq_u8(vKeyStream, vInput);
  135. Neon.vst1q_u8(poutput + offset, vOut);
  136. }
  137. return true;
  138. }
  139. return false;
  140. }
  141. #endif
  142. #endif
  143. }
  144. }
  145. #endif