FastSalsa20EngineHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. #if BESTHTTP_WITH_BURST
  4. using Unity.Burst.Intrinsics;
  5. using static Unity.Burst.Intrinsics.X86;
  6. using static Unity.Burst.Intrinsics.Arm;
  7. #endif
  8. namespace Best.HTTP.Shared.TLS.Crypto.Impl
  9. {
  10. #if BESTHTTP_WITH_BURST
  11. [Unity.Burst.BurstCompile]
  12. #endif
  13. internal static class FastSalsa20EngineHelper
  14. {
  15. #if BESTHTTP_WITH_BURST
  16. [Unity.Burst.BurstCompile]
  17. public unsafe static void ProcessBytes([Unity.Burst.NoAlias] byte* outBytes, int outOff, [Unity.Burst.NoAlias] byte* inBytes, int inOff, [Unity.Burst.NoAlias] byte* keyStream)
  18. {
  19. //for (int i = 0; i < 64; ++i)
  20. // outBytes[idx + i + outOff] = (byte)(keyStream[i] ^ inBytes[idx + i + inOff]);
  21. #if !UNITY_ANDROID && !UNITY_IOS
  22. if (Sse2.IsSse2Supported)
  23. {
  24. for (int offset = 0; offset < 64; offset += 16)
  25. {
  26. var vin = Sse2.loadu_si128(inBytes + inOff + offset);
  27. //var vout = Sse2.loadu_si128(outBytes + outOff + offset);
  28. var vkeyStream = Sse2.loadu_si128(keyStream + offset);
  29. var vout = Sse2.xor_si128(vkeyStream, vin);
  30. Sse2.storeu_si128(outBytes + outOff + offset, vout);
  31. }
  32. }
  33. else
  34. #endif
  35. if (Neon.IsNeonSupported)
  36. {
  37. for (int offset = 0; offset < 64; offset += 16)
  38. {
  39. var vin = Neon.vld1q_u8(inBytes + inOff + offset);
  40. var vkeyStream = Neon.vld1q_u8(keyStream + offset);
  41. var vOut = Neon.veorq_u8(vkeyStream, vin);
  42. Neon.vst1q_u8(outBytes + outOff + offset, vOut);
  43. }
  44. }
  45. else
  46. {
  47. ulong* pulOut = (ulong*)&outBytes[outOff];
  48. ulong* pulIn = (ulong*)&inBytes[inOff];
  49. ulong* pulKeyStream = (ulong*)keyStream;
  50. pulOut[0] = pulKeyStream[0] ^ pulIn[0];
  51. pulOut[1] = pulKeyStream[1] ^ pulIn[1];
  52. pulOut[2] = pulKeyStream[2] ^ pulIn[2];
  53. pulOut[3] = pulKeyStream[3] ^ pulIn[3];
  54. }
  55. }
  56. #endif
  57. }
  58. }
  59. #endif