Tables4kGcmMultiplier.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. public class Tables4kGcmMultiplier
  9. : IGcmMultiplier
  10. {
  11. private byte[] H;
  12. private ulong[] T;
  13. public void Init(byte[] H)
  14. {
  15. if (T == null)
  16. {
  17. T = new ulong[512];
  18. }
  19. else if (Arrays.AreEqual(this.H, H))
  20. {
  21. return;
  22. }
  23. this.H = Arrays.Clone(H);
  24. // T[0] = 0
  25. // T[1] = H.p^7
  26. GcmUtilities.AsUlongs(this.H, T, 2);
  27. GcmUtilities.MultiplyP7(T, 2, T, 2);
  28. for (int n = 2; n < 256; n += 2)
  29. {
  30. // T[2.n] = T[n].p^-1
  31. GcmUtilities.DivideP(T, n, T, n << 1);
  32. // T[2.n + 1] = T[2.n] + T[1]
  33. GcmUtilities.Xor(T, n << 1, T, 2, T, (n + 1) << 1);
  34. }
  35. }
  36. public void MultiplyH(byte[] x)
  37. {
  38. //ulong[] z = new ulong[2];
  39. //GcmUtilities.Copy(T, x[15] << 1, z, 0);
  40. //for (int i = 14; i >= 0; --i)
  41. //{
  42. // GcmUtilities.MultiplyP8(z);
  43. // GcmUtilities.Xor(z, 0, T, x[i] << 1);
  44. //}
  45. //Pack.UInt64_To_BE(z, x, 0);
  46. int pos = x[15] << 1;
  47. ulong z0 = T[pos + 0], z1 = T[pos + 1];
  48. for (int i = 14; i >= 0; --i)
  49. {
  50. pos = x[i] << 1;
  51. ulong c = z1 << 56;
  52. z1 = T[pos + 1] ^ ((z1 >> 8) | (z0 << 56));
  53. z0 = T[pos + 0] ^ (z0 >> 8) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  54. }
  55. Pack.UInt64_To_BE(z0, x, 0);
  56. Pack.UInt64_To_BE(z1, x, 8);
  57. }
  58. }
  59. }
  60. #pragma warning restore
  61. #endif