BurstTables8kGcmMultiplier.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes.Gcm;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. #if BESTHTTP_WITH_BURST
  9. using Unity.Burst;
  10. using Unity.Collections.LowLevel.Unsafe;
  11. #endif
  12. namespace Best.HTTP.Shared.TLS.Crypto.Impl
  13. {
  14. [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  15. #if BESTHTTP_WITH_BURST
  16. [BurstCompile]
  17. #endif
  18. public sealed class BurstTables8kGcmMultiplier //: IGcmMultiplier
  19. {
  20. private byte[] H;
  21. private GcmUtilities.FieldElement[][] T;
  22. public void Init(byte[] H)
  23. {
  24. if (T == null)
  25. {
  26. T = new GcmUtilities.FieldElement[2][];
  27. }
  28. else if (Arrays.AreEqual(this.H, H))
  29. {
  30. return;
  31. }
  32. if (this.H == null)
  33. this.H = Arrays.Clone(H);
  34. else
  35. {
  36. if (this.H.Length != H.Length)
  37. Array.Resize(ref this.H, H.Length);
  38. Array.Copy(H, this.H, H.Length);
  39. }
  40. for (int i = 0; i < 2; ++i)
  41. {
  42. if (T[i] == null)
  43. T[i] = new GcmUtilities.FieldElement[256];
  44. GcmUtilities.FieldElement[] t = T[i];
  45. // t[0] = 0
  46. if (i == 0)
  47. {
  48. // t[1] = H.p^7
  49. GcmUtilities.AsFieldElement(this.H, out t[1]);
  50. GcmUtilities.MultiplyP7(ref t[1]);
  51. }
  52. else
  53. {
  54. // t[1] = T[i-1][1].p^8
  55. GcmUtilities.MultiplyP8(ref T[i - 1][1], out t[1]);
  56. }
  57. for (int n = 1; n < 128; ++n)
  58. {
  59. // t[2.n] = t[n].p^-1
  60. GcmUtilities.DivideP(ref t[n], out t[n << 1]);
  61. // t[2.n + 1] = t[2.n] + t[1]
  62. GcmUtilities.Xor(ref t[n << 1], ref t[1], out t[(n << 1) + 1]);
  63. }
  64. }
  65. }
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. public unsafe void MultiplyH(byte[] x)
  68. {
  69. fixed (byte* px = x)
  70. fixed (GcmUtilities.FieldElement* pT0 = this.T[0])
  71. fixed (GcmUtilities.FieldElement* pT1 = this.T[1])
  72. MultiplyHImpl(px, pT0, pT1);
  73. }
  74. #if BESTHTTP_WITH_BURST
  75. [BurstCompile]
  76. #endif
  77. private static unsafe void MultiplyHImpl(
  78. #if BESTHTTP_WITH_BURST
  79. [NoAlias]
  80. #endif
  81. byte* px,
  82. #if BESTHTTP_WITH_BURST
  83. [NoAlias]
  84. #endif
  85. GcmUtilities.FieldElement* pT0,
  86. #if BESTHTTP_WITH_BURST
  87. [NoAlias]
  88. #endif
  89. GcmUtilities.FieldElement* pT1)
  90. {
  91. int vPos = px[15];
  92. int uPos = px[14];
  93. ulong z1 = pT0[uPos].n1 ^ pT1[vPos].n1;
  94. ulong z0 = pT0[uPos].n0 ^ pT1[vPos].n0;
  95. for (int i = 12; i >= 0; i -= 2)
  96. {
  97. vPos = px[i + 1];
  98. uPos = px[i];
  99. ulong c = z1 << 48;
  100. z1 = pT0[uPos].n1 ^ pT1[vPos].n1 ^ ((z1 >> 16) | (z0 << 48));
  101. z0 = pT0[uPos].n0 ^ pT1[vPos].n0 ^ (z0 >> 16) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  102. }
  103. //GcmUtilities.AsBytes(z0, z1, x);
  104. //UInt32_To_BE((uint)(n >> 32), bs, off);
  105. uint n = (uint)(z0 >> 32);
  106. px[0] = (byte)(n >> 24);
  107. px[1] = (byte)(n >> 16);
  108. px[2] = (byte)(n >> 8);
  109. px[3] = (byte)(n);
  110. //UInt32_To_BE((uint)(n), bs, off + 4);
  111. n = (uint)(z0);
  112. px[4] = (byte)(n >> 24);
  113. px[5] = (byte)(n >> 16);
  114. px[6] = (byte)(n >> 8);
  115. px[7] = (byte)(n);
  116. n = (uint)(z1 >> 32);
  117. px[8] = (byte)(n >> 24);
  118. px[9] = (byte)(n >> 16);
  119. px[10] = (byte)(n >> 8);
  120. px[11] = (byte)(n);
  121. //UInt32_To_BE((uint)(n), bs, off + 4);
  122. n = (uint)(z1);
  123. px[12] = (byte)(n >> 24);
  124. px[13] = (byte)(n >> 16);
  125. px[14] = (byte)(n >> 8);
  126. px[15] = (byte)(n);
  127. }
  128. }
  129. }
  130. #pragma warning restore
  131. #endif