FastChaChaEngineHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. #if BESTHTTP_WITH_BURST
  6. using Unity.Burst;
  7. #endif
  8. namespace BestHTTP.Connections.TLS.Crypto.Impl
  9. {
  10. #if BESTHTTP_WITH_BURST
  11. [Unity.Burst.BurstCompile]
  12. #endif
  13. internal static class FastChaChaEngineHelper
  14. {
  15. internal unsafe static void ChachaCore(int rounds, uint[] input, byte[] output)
  16. {
  17. fixed (uint* pinput = input)
  18. fixed (byte* poutput = output)
  19. ChachaCoreImpl(rounds, pinput, poutput);
  20. }
  21. #if BESTHTTP_WITH_BURST
  22. [Unity.Burst.BurstCompile]
  23. [Unity.Burst.CompilerServices.SkipLocalsInit]
  24. #endif
  25. internal unsafe static void ChachaCoreImpl(int rounds,
  26. #if BESTHTTP_WITH_BURST
  27. [NoAlias]
  28. #endif
  29. uint* input,
  30. #if BESTHTTP_WITH_BURST
  31. [NoAlias]
  32. #endif
  33. byte* output)
  34. {
  35. uint* x = stackalloc uint[16];
  36. for (int i = 0; i < 16; i++)
  37. x[i] = input[i];
  38. uint tmp = 0;
  39. for (int i = rounds; i > 0; i -= 2)
  40. {
  41. x[00] += x[04]; tmp = x[12] ^ x[00]; x[12] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[12] ^ x[00], 16);
  42. x[01] += x[05]; tmp = x[13] ^ x[01]; x[13] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[13] ^ x[01], 16);
  43. x[02] += x[06]; tmp = x[14] ^ x[02]; x[14] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[14] ^ x[02], 16);
  44. x[03] += x[07]; tmp = x[15] ^ x[03]; x[15] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[15] ^ x[03], 16);
  45. x[08] += x[12]; tmp = x[04] ^ x[08]; x[04] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[04] ^ x[08], 12);
  46. x[09] += x[13]; tmp = x[05] ^ x[09]; x[05] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[05] ^ x[09], 12);
  47. x[10] += x[14]; tmp = x[06] ^ x[10]; x[06] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[06] ^ x[10], 12);
  48. x[11] += x[15]; tmp = x[07] ^ x[11]; x[07] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[07] ^ x[11], 12);
  49. x[00] += x[04]; tmp = x[12] ^ x[00]; x[12] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[12] ^ x[00], 8);
  50. x[01] += x[05]; tmp = x[13] ^ x[01]; x[13] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[13] ^ x[01], 8);
  51. x[02] += x[06]; tmp = x[14] ^ x[02]; x[14] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[14] ^ x[02], 8);
  52. x[03] += x[07]; tmp = x[15] ^ x[03]; x[15] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[15] ^ x[03], 8);
  53. x[08] += x[12]; tmp = x[04] ^ x[08]; x[04] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[04] ^ x[08], 7);
  54. x[09] += x[13]; tmp = x[05] ^ x[09]; x[05] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[05] ^ x[09], 7);
  55. x[10] += x[14]; tmp = x[06] ^ x[10]; x[06] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[06] ^ x[10], 7);
  56. x[11] += x[15]; tmp = x[07] ^ x[11]; x[07] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[07] ^ x[11], 7);
  57. x[00] += x[05]; tmp = x[15] ^ x[00]; x[15] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[15] ^ x[00], 16);
  58. x[01] += x[06]; tmp = x[12] ^ x[01]; x[12] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[12] ^ x[01], 16);
  59. x[02] += x[07]; tmp = x[13] ^ x[02]; x[13] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[13] ^ x[02], 16);
  60. x[03] += x[04]; tmp = x[14] ^ x[03]; x[14] = (tmp << 16) | (tmp >> -16); // Integers.RotateLeft(x[14] ^ x[03], 16);
  61. x[10] += x[15]; tmp = x[05] ^ x[10]; x[05] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[05] ^ x[10], 12);
  62. x[11] += x[12]; tmp = x[06] ^ x[11]; x[06] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[06] ^ x[11], 12);
  63. x[08] += x[13]; tmp = x[07] ^ x[08]; x[07] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[07] ^ x[08], 12);
  64. x[09] += x[14]; tmp = x[04] ^ x[09]; x[04] = (tmp << 12) | (tmp >> -12); // Integers.RotateLeft(x[04] ^ x[09], 12);
  65. x[00] += x[05]; tmp = x[15] ^ x[00]; x[15] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[15] ^ x[00], 8);
  66. x[01] += x[06]; tmp = x[12] ^ x[01]; x[12] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[12] ^ x[01], 8);
  67. x[02] += x[07]; tmp = x[13] ^ x[02]; x[13] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[13] ^ x[02], 8);
  68. x[03] += x[04]; tmp = x[14] ^ x[03]; x[14] = (tmp << 8) | (tmp >> -8); // Integers.RotateLeft(x[14] ^ x[03], 8);
  69. x[10] += x[15]; tmp = x[05] ^ x[10]; x[05] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[05] ^ x[10], 7);
  70. x[11] += x[12]; tmp = x[06] ^ x[11]; x[06] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[06] ^ x[11], 7);
  71. x[08] += x[13]; tmp = x[07] ^ x[08]; x[07] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[07] ^ x[08], 7);
  72. x[09] += x[14]; tmp = x[04] ^ x[09]; x[04] = (tmp << 7) | (tmp >> -7); // Integers.RotateLeft(x[04] ^ x[09], 7);
  73. }
  74. for (int i = 0; i < 16; i++)
  75. {
  76. uint n = x[i] + input[i];
  77. output[(i * 4)] = (byte)n;
  78. output[(i * 4) + 1] = (byte)(n >> 8);
  79. output[(i * 4) + 2] = (byte)(n >> 16);
  80. output[(i * 4) + 3] = (byte)(n >> 24);
  81. }
  82. }
  83. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. internal static unsafe void ImplProcessBlock(ReadOnlySpan<byte> input, Span<byte> output, byte[] keyStream)
  86. {
  87. fixed (byte* pinput = input)
  88. fixed (byte* poutput = output)
  89. fixed (byte* pkeyStream = keyStream)
  90. {
  91. ulong* pulinput = (ulong*)pinput;
  92. ulong* puloutput = (ulong*)poutput;
  93. ulong* pulkeyStream = (ulong*)pkeyStream;
  94. puloutput[0] = pulkeyStream[0] ^ pulinput[0];
  95. puloutput[1] = pulkeyStream[1] ^ pulinput[1];
  96. puloutput[2] = pulkeyStream[2] ^ pulinput[2];
  97. puloutput[3] = pulkeyStream[3] ^ pulinput[3];
  98. puloutput[4] = pulkeyStream[4] ^ pulinput[4];
  99. puloutput[5] = pulkeyStream[5] ^ pulinput[5];
  100. puloutput[6] = pulkeyStream[6] ^ pulinput[6];
  101. puloutput[7] = pulkeyStream[7] ^ pulinput[7];
  102. }
  103. }
  104. #endif
  105. }
  106. }
  107. #endif