Dstu7624WrapEngine.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  9. {
  10. public class Dstu7624WrapEngine
  11. : IWrapper
  12. {
  13. private KeyParameter param;
  14. private Dstu7624Engine engine;
  15. private bool forWrapping;
  16. private int blockSize;
  17. public Dstu7624WrapEngine(int blockSizeBits)
  18. {
  19. engine = new Dstu7624Engine(blockSizeBits);
  20. param = null;
  21. blockSize = blockSizeBits / 8;
  22. }
  23. public string AlgorithmName
  24. {
  25. get { return "Dstu7624WrapEngine"; }
  26. }
  27. public void Init(bool forWrapping, ICipherParameters parameters)
  28. {
  29. this.forWrapping = forWrapping;
  30. if (parameters is KeyParameter)
  31. {
  32. this.param = (KeyParameter)parameters;
  33. engine.Init(forWrapping, param);
  34. }
  35. else
  36. {
  37. throw new ArgumentException("Bad parameters passed to Dstu7624WrapEngine");
  38. }
  39. }
  40. public byte[] Wrap(byte[] input, int inOff, int length)
  41. {
  42. if (!forWrapping)
  43. throw new InvalidOperationException("Not set for wrapping");
  44. if (length % blockSize != 0)
  45. throw new ArgumentException("Padding not supported");
  46. int n = 2 * (1 + length / blockSize);
  47. int V = (n - 1) * 6;
  48. byte[] buffer = new byte[length + blockSize];
  49. Array.Copy(input, inOff, buffer, 0, length);
  50. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  51. byte[] B = new byte[blockSize / 2];
  52. Array.Copy(buffer, 0, B, 0, blockSize / 2);
  53. //Console.WriteLine("B0: "+ BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
  54. IList bTemp = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  55. int bHalfBlocksLen = buffer.Length - blockSize / 2;
  56. int bufOff = blockSize / 2;
  57. while (bHalfBlocksLen != 0)
  58. {
  59. byte[] temp = new byte[blockSize / 2];
  60. Array.Copy(buffer, bufOff, temp, 0, blockSize / 2);
  61. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  62. //Console.WriteLine(buffer.Length);
  63. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(temp));
  64. bTemp.Add(temp);
  65. bHalfBlocksLen -= blockSize / 2;
  66. bufOff += blockSize / 2;
  67. }
  68. for (int j = 0; j < V; j++)
  69. {
  70. Array.Copy(B, 0, buffer, 0, blockSize / 2);
  71. Array.Copy((byte[])bTemp[0], 0, buffer, blockSize / 2, blockSize / 2);
  72. engine.ProcessBlock(buffer, 0, buffer, 0);
  73. byte[] intArray = Pack.UInt32_To_LE((uint)(j + 1));
  74. for (int byteNum = 0; byteNum < intArray.Length; byteNum++)
  75. {
  76. buffer[byteNum + blockSize / 2] ^= intArray[byteNum];
  77. }
  78. Array.Copy(buffer, blockSize / 2, B, 0, blockSize / 2);
  79. for (int i = 2; i < n; i++)
  80. {
  81. Array.Copy((byte[])bTemp[i - 1], 0, (byte[])bTemp[i - 2], 0, blockSize / 2);
  82. }
  83. Array.Copy(buffer, 0, (byte[])bTemp[n - 2], 0, blockSize / 2);
  84. //Console.WriteLine("B" + j.ToString() + ": " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
  85. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[0]));
  86. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[1]));
  87. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[2]));
  88. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  89. }
  90. Array.Copy(B, 0, buffer, 0, blockSize / 2);
  91. bufOff = blockSize / 2;
  92. for (int i = 0; i < n - 1; i++)
  93. {
  94. Array.Copy((byte[])bTemp[i], 0, buffer, bufOff, blockSize / 2);
  95. bufOff += blockSize / 2;
  96. }
  97. return buffer;
  98. }
  99. public byte[] Unwrap(byte[] input, int inOff, int length)
  100. {
  101. if (forWrapping)
  102. throw new InvalidOperationException("not set for unwrapping");
  103. if (length % blockSize != 0)
  104. throw new ArgumentException("Padding not supported");
  105. int n = 2 * length / blockSize;
  106. int V = (n - 1) * 6;
  107. byte[] buffer = new byte[length];
  108. Array.Copy(input, inOff, buffer, 0, length);
  109. byte[] B = new byte[blockSize / 2];
  110. Array.Copy(buffer, 0, B, 0, blockSize / 2);
  111. //Console.WriteLine("B18: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
  112. IList bTemp = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  113. int bHalfBlocksLen = buffer.Length - blockSize / 2;
  114. int bufOff = blockSize / 2;
  115. while (bHalfBlocksLen != 0)
  116. {
  117. byte[] temp = new byte[blockSize / 2];
  118. Array.Copy(buffer, bufOff, temp, 0, blockSize / 2);
  119. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  120. //Console.WriteLine(buffer.Length);
  121. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(temp));
  122. bTemp.Add(temp);
  123. bHalfBlocksLen -= blockSize / 2;
  124. bufOff += blockSize / 2;
  125. }
  126. for (int j = 0; j < V; j++)
  127. {
  128. Array.Copy((byte[])bTemp[n - 2], 0, buffer, 0, blockSize / 2);
  129. Array.Copy(B, 0, buffer, blockSize / 2, blockSize / 2);
  130. byte[] intArray = Pack.UInt32_To_LE((uint)(V - j));
  131. for (int byteNum = 0; byteNum < intArray.Length; byteNum++)
  132. {
  133. buffer[byteNum + blockSize / 2] ^= intArray[byteNum];
  134. }
  135. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  136. engine.ProcessBlock(buffer, 0, buffer, 0);
  137. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  138. Array.Copy(buffer, 0, B, 0, blockSize / 2);
  139. for (int i = 2; i < n; i++)
  140. {
  141. Array.Copy((byte[])bTemp[n - i - 1], 0, (byte[])bTemp[n - i], 0, blockSize / 2);
  142. }
  143. Array.Copy(buffer, blockSize / 2, (byte[])bTemp[0], 0, blockSize / 2);
  144. //Console.WriteLine("B" + (V - j - 1).ToString() + ": " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
  145. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[0]));
  146. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[1]));
  147. //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[2]));
  148. //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
  149. }
  150. Array.Copy(B, 0, buffer, 0, blockSize / 2);
  151. bufOff = blockSize / 2;
  152. for (int i = 0; i < n - 1; i++)
  153. {
  154. Array.Copy((byte[])bTemp[i], 0, buffer, bufOff, blockSize / 2);
  155. bufOff += blockSize / 2;
  156. }
  157. byte diff = 0;
  158. for (int i = buffer.Length - blockSize; i < buffer.Length; ++i)
  159. {
  160. diff |= buffer[i];
  161. }
  162. if (diff != 0)
  163. throw new InvalidCipherTextException("checksum failed");
  164. return Arrays.CopyOfRange(buffer, 0, buffer.Length - blockSize);
  165. }
  166. }
  167. }
  168. #pragma warning restore
  169. #endif