Tables8kGcmMultiplier.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Utilities;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Modes.Gcm
  6. {
  7. public sealed class Tables8kGcmMultiplier
  8. : IGcmMultiplier
  9. {
  10. private byte[] H;
  11. private uint[][][] M;
  12. public void Init(byte[] H)
  13. {
  14. if (M == null)
  15. {
  16. M = new uint[32][][];
  17. }
  18. else if (Arrays.AreEqual(this.H, H))
  19. {
  20. return;
  21. }
  22. this.H = Arrays.Clone(H);
  23. M[0] = new uint[16][];
  24. M[1] = new uint[16][];
  25. M[0][0] = new uint[4];
  26. M[1][0] = new uint[4];
  27. M[1][8] = GcmUtilities.AsUints(H);
  28. for (int j = 4; j >= 1; j >>= 1)
  29. {
  30. uint[] tmp = (uint[])M[1][j + j].Clone();
  31. GcmUtilities.MultiplyP(tmp);
  32. M[1][j] = tmp;
  33. }
  34. {
  35. uint[] tmp = (uint[])M[1][1].Clone();
  36. GcmUtilities.MultiplyP(tmp);
  37. M[0][8] = tmp;
  38. }
  39. for (int j = 4; j >= 1; j >>= 1)
  40. {
  41. uint[] tmp = (uint[])M[0][j + j].Clone();
  42. GcmUtilities.MultiplyP(tmp);
  43. M[0][j] = tmp;
  44. }
  45. for (int i = 0; ; )
  46. {
  47. for (int j = 2; j < 16; j += j)
  48. {
  49. for (int k = 1; k < j; ++k)
  50. {
  51. uint[] tmp = (uint[])M[i][j].Clone();
  52. GcmUtilities.Xor(tmp, M[i][k]);
  53. M[i][j + k] = tmp;
  54. }
  55. }
  56. if (++i == 32) return;
  57. if (i > 1)
  58. {
  59. M[i] = new uint[16][];
  60. M[i][0] = new uint[4];
  61. for (int j = 8; j > 0; j >>= 1)
  62. {
  63. uint[] tmp = (uint[])M[i - 2][j].Clone();
  64. GcmUtilities.MultiplyP8(tmp);
  65. M[i][j] = tmp;
  66. }
  67. }
  68. }
  69. }
  70. #if true //!ENABLE_IL2CPP || UNITY_WEBGL
  71. public void MultiplyH(byte[] x)
  72. {
  73. uint[] z = new uint[4];
  74. for (int i = 15; i >= 0; --i)
  75. {
  76. //GcmUtilities.Xor(z, M[i + i][x[i] & 0x0f]);
  77. uint[] m = M[i + i][x[i] & 0x0f];
  78. z[0] ^= m[0];
  79. z[1] ^= m[1];
  80. z[2] ^= m[2];
  81. z[3] ^= m[3];
  82. //GcmUtilities.Xor(z, M[i + i + 1][(x[i] & 0xf0) >> 4]);
  83. m = M[i + i + 1][(x[i] & 0xf0) >> 4];
  84. z[0] ^= m[0];
  85. z[1] ^= m[1];
  86. z[2] ^= m[2];
  87. z[3] ^= m[3];
  88. }
  89. Pack.UInt32_To_BE(z, x, 0);
  90. }
  91. #else
  92. uint[] z = new uint[4];
  93. public unsafe void MultiplyH(byte[] x)
  94. {
  95. //Array.Clear(z, 0, z.Length);
  96. fixed (byte* px = x)
  97. fixed (uint* pz = z)
  98. {
  99. pz[0] = pz[1] = pz[2] = pz[3] = 0;
  100. for (int i = 15; i >= 0; --i)
  101. {
  102. //GcmUtilities.Xor(z, M[i + i][x[i] & 0x0f]);
  103. //uint[] m = M[i + i][x[i] & 0x0f];
  104. //z[0] ^= m[0];
  105. //z[1] ^= m[1];
  106. //z[2] ^= m[2];
  107. //z[3] ^= m[3];
  108. fixed (uint* pm = M[i + i][px[i] & 0x0f])
  109. {
  110. pz[0] ^= pm[0];
  111. pz[1] ^= pm[1];
  112. pz[2] ^= pm[2];
  113. pz[3] ^= pm[3];
  114. }
  115. //GcmUtilities.Xor(z, M[i + i + 1][(x[i] & 0xf0) >> 4]);
  116. //m = M[i + i + 1][(x[i] & 0xf0) >> 4];
  117. //z[0] ^= m[0];
  118. //z[1] ^= m[1];
  119. //z[2] ^= m[2];
  120. //z[3] ^= m[3];
  121. fixed (uint* pm = M[i + i + 1][(px[i] & 0xf0) >> 4])
  122. {
  123. pz[0] ^= pm[0];
  124. pz[1] ^= pm[1];
  125. pz[2] ^= pm[2];
  126. pz[3] ^= pm[3];
  127. }
  128. }
  129. //int off = 0;
  130. //for (int i = 0; i < 4; ++i)
  131. //{
  132. // uint n = pz[i];
  133. //
  134. // px[off + 0] = (byte)(n >> 24);
  135. // px[off + 1] = (byte)(n >> 16);
  136. // px[off + 2] = (byte)(n >> 8);
  137. // px[off + 3] = (byte)(n);
  138. //
  139. // off += 4;
  140. //}
  141. // i = 0
  142. uint n = pz[0];
  143. px[0] = (byte)(n >> 24);
  144. px[1] = (byte)(n >> 16);
  145. px[2] = (byte)(n >> 8);
  146. px[3] = (byte)(n);
  147. // i = 1
  148. n = pz[1];
  149. px[4] = (byte)(n >> 24);
  150. px[5] = (byte)(n >> 16);
  151. px[6] = (byte)(n >> 8);
  152. px[7] = (byte)(n);
  153. // i = 2
  154. n = pz[2];
  155. px[8] = (byte)(n >> 24);
  156. px[9] = (byte)(n >> 16);
  157. px[10] = (byte)(n >> 8);
  158. px[11] = (byte)(n);
  159. // i = 3
  160. n = pz[3];
  161. px[12] = (byte)(n >> 24);
  162. px[13] = (byte)(n >> 16);
  163. px[14] = (byte)(n >> 8);
  164. px[15] = (byte)(n);
  165. }
  166. //Pack.UInt32_To_BE(z, x, 0);
  167. }
  168. #endif
  169. }
  170. }
  171. #endif