Tables8kGcmMultiplier.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes.Gcm
  7. {
  8. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.NullChecks, false)]
  9. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.ArrayBoundsChecks, false)]
  10. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.DivideByZeroChecks, false)]
  11. [BestHTTP.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  12. public sealed class Tables8kGcmMultiplier
  13. : IGcmMultiplier
  14. {
  15. private byte[] H;
  16. private ulong[][] T;
  17. public void Init(byte[] H)
  18. {
  19. if (T == null)
  20. {
  21. T = new ulong[32][];
  22. }
  23. else if (Arrays.AreEqual(this.H, H))
  24. {
  25. return;
  26. }
  27. this.H = Arrays.Clone(H);
  28. for (int i = 0; i < 32; ++i)
  29. {
  30. ulong[] t = T[i] = new ulong[32];
  31. // t[0] = 0
  32. if (i == 0)
  33. {
  34. // t[1] = H.p^3
  35. GcmUtilities.AsUlongs(this.H, t, 2);
  36. GcmUtilities.MultiplyP3(t, 2, t, 2);
  37. }
  38. else
  39. {
  40. // t[1] = T[i-1][1].p^4
  41. GcmUtilities.MultiplyP4(T[i - 1], 2, t, 2);
  42. }
  43. for (int n = 2; n < 16; n += 2)
  44. {
  45. // t[2.n] = t[n].p^-1
  46. GcmUtilities.DivideP(t, n, t, n << 1);
  47. // t[2.n + 1] = t[2.n] + t[1]
  48. GcmUtilities.Xor(t, n << 1, t, 2, t, (n + 1) << 1);
  49. }
  50. }
  51. }
  52. uint[] z = new uint[4];
  53. public void MultiplyH(byte[] x)
  54. {
  55. //ulong[] z = new ulong[2];
  56. //for (int i = 15; i >= 0; --i)
  57. //{
  58. // GcmUtilities.Xor(z, 0, T[i + i + 1], (x[i] & 0x0F) << 1);
  59. // GcmUtilities.Xor(z, 0, T[i + i], (x[i] & 0xF0) >> 3);
  60. //}
  61. //Pack.UInt64_To_BE(z, x, 0);
  62. ulong z0 = 0, z1 = 0;
  63. for (int i = 15; i >= 0; --i)
  64. {
  65. ulong[] tu = T[i + i + 1], tv = T[i + i];
  66. int uPos = (x[i] & 0x0F) << 1, vPos = (x[i] & 0xF0) >> 3;
  67. z0 ^= tu[uPos + 0] ^ tv[vPos + 0];
  68. z1 ^= tu[uPos + 1] ^ tv[vPos + 1];
  69. }
  70. Pack.UInt64_To_BE(z0, x, 0);
  71. Pack.UInt64_To_BE(z1, x, 8);
  72. }
  73. }
  74. }
  75. #pragma warning restore
  76. #endif