VMPCMac.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. namespace Org.BouncyCastle.Crypto.Macs
  5. {
  6. public class VmpcMac
  7. : IMac
  8. {
  9. private byte g;
  10. private byte n = 0;
  11. private byte[] P = null;
  12. private byte s = 0;
  13. private byte[] T;
  14. private byte[] workingIV;
  15. private byte[] workingKey;
  16. private byte x1, x2, x3, x4;
  17. public virtual int DoFinal(byte[] output, int outOff)
  18. {
  19. // Execute the Post-Processing Phase
  20. for (int r = 1; r < 25; r++)
  21. {
  22. s = P[(s + P[n & 0xff]) & 0xff];
  23. x4 = P[(x4 + x3 + r) & 0xff];
  24. x3 = P[(x3 + x2 + r) & 0xff];
  25. x2 = P[(x2 + x1 + r) & 0xff];
  26. x1 = P[(x1 + s + r) & 0xff];
  27. T[g & 0x1f] = (byte) (T[g & 0x1f] ^ x1);
  28. T[(g + 1) & 0x1f] = (byte) (T[(g + 1) & 0x1f] ^ x2);
  29. T[(g + 2) & 0x1f] = (byte) (T[(g + 2) & 0x1f] ^ x3);
  30. T[(g + 3) & 0x1f] = (byte) (T[(g + 3) & 0x1f] ^ x4);
  31. g = (byte) ((g + 4) & 0x1f);
  32. byte temp = P[n & 0xff];
  33. P[n & 0xff] = P[s & 0xff];
  34. P[s & 0xff] = temp;
  35. n = (byte) ((n + 1) & 0xff);
  36. }
  37. // Input T to the IV-phase of the VMPC KSA
  38. for (int m = 0; m < 768; m++)
  39. {
  40. s = P[(s + P[m & 0xff] + T[m & 0x1f]) & 0xff];
  41. byte temp = P[m & 0xff];
  42. P[m & 0xff] = P[s & 0xff];
  43. P[s & 0xff] = temp;
  44. }
  45. // Store 20 new outputs of the VMPC Stream Cipher input table M
  46. byte[] M = new byte[20];
  47. for (int i = 0; i < 20; i++)
  48. {
  49. s = P[(s + P[i & 0xff]) & 0xff];
  50. M[i] = P[(P[(P[s & 0xff]) & 0xff] + 1) & 0xff];
  51. byte temp = P[i & 0xff];
  52. P[i & 0xff] = P[s & 0xff];
  53. P[s & 0xff] = temp;
  54. }
  55. Array.Copy(M, 0, output, outOff, M.Length);
  56. Reset();
  57. return M.Length;
  58. }
  59. public virtual string AlgorithmName
  60. {
  61. get { return "VMPC-MAC"; }
  62. }
  63. public virtual int GetMacSize()
  64. {
  65. return 20;
  66. }
  67. public virtual void Init(ICipherParameters parameters)
  68. {
  69. if (!(parameters is ParametersWithIV))
  70. throw new ArgumentException("VMPC-MAC Init parameters must include an IV", "parameters");
  71. ParametersWithIV ivParams = (ParametersWithIV) parameters;
  72. KeyParameter key = (KeyParameter) ivParams.Parameters;
  73. if (!(ivParams.Parameters is KeyParameter))
  74. throw new ArgumentException("VMPC-MAC Init parameters must include a key", "parameters");
  75. this.workingIV = ivParams.GetIV();
  76. if (workingIV == null || workingIV.Length < 1 || workingIV.Length > 768)
  77. throw new ArgumentException("VMPC-MAC requires 1 to 768 bytes of IV", "parameters");
  78. this.workingKey = key.GetKey();
  79. Reset();
  80. }
  81. private void initKey(byte[] keyBytes, byte[] ivBytes)
  82. {
  83. s = 0;
  84. P = new byte[256];
  85. for (int i = 0; i < 256; i++)
  86. {
  87. P[i] = (byte) i;
  88. }
  89. for (int m = 0; m < 768; m++)
  90. {
  91. s = P[(s + P[m & 0xff] + keyBytes[m % keyBytes.Length]) & 0xff];
  92. byte temp = P[m & 0xff];
  93. P[m & 0xff] = P[s & 0xff];
  94. P[s & 0xff] = temp;
  95. }
  96. for (int m = 0; m < 768; m++)
  97. {
  98. s = P[(s + P[m & 0xff] + ivBytes[m % ivBytes.Length]) & 0xff];
  99. byte temp = P[m & 0xff];
  100. P[m & 0xff] = P[s & 0xff];
  101. P[s & 0xff] = temp;
  102. }
  103. n = 0;
  104. }
  105. public virtual void Reset()
  106. {
  107. initKey(this.workingKey, this.workingIV);
  108. g = x1 = x2 = x3 = x4 = n = 0;
  109. T = new byte[32];
  110. for (int i = 0; i < 32; i++)
  111. {
  112. T[i] = 0;
  113. }
  114. }
  115. public virtual void Update(byte input)
  116. {
  117. s = P[(s + P[n & 0xff]) & 0xff];
  118. byte c = (byte) (input ^ P[(P[(P[s & 0xff]) & 0xff] + 1) & 0xff]);
  119. x4 = P[(x4 + x3) & 0xff];
  120. x3 = P[(x3 + x2) & 0xff];
  121. x2 = P[(x2 + x1) & 0xff];
  122. x1 = P[(x1 + s + c) & 0xff];
  123. T[g & 0x1f] = (byte) (T[g & 0x1f] ^ x1);
  124. T[(g + 1) & 0x1f] = (byte) (T[(g + 1) & 0x1f] ^ x2);
  125. T[(g + 2) & 0x1f] = (byte) (T[(g + 2) & 0x1f] ^ x3);
  126. T[(g + 3) & 0x1f] = (byte) (T[(g + 3) & 0x1f] ^ x4);
  127. g = (byte) ((g + 4) & 0x1f);
  128. byte temp = P[n & 0xff];
  129. P[n & 0xff] = P[s & 0xff];
  130. P[s & 0xff] = temp;
  131. n = (byte) ((n + 1) & 0xff);
  132. }
  133. public virtual void BlockUpdate(byte[] input, int inOff, int len)
  134. {
  135. if ((inOff + len) > input.Length)
  136. throw new DataLengthException("input buffer too short");
  137. for (int i = 0; i < len; i++)
  138. {
  139. Update(input[inOff + i]);
  140. }
  141. }
  142. }
  143. }
  144. #endif