GOFBBlockCipher.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  7. {
  8. /**
  9. * implements the GOST 28147 OFB counter mode (GCTR).
  10. */
  11. public class GOfbBlockCipher
  12. : IBlockCipher
  13. {
  14. private byte[] IV;
  15. private byte[] ofbV;
  16. private byte[] ofbOutV;
  17. private readonly int blockSize;
  18. private readonly IBlockCipher cipher;
  19. bool firstStep = true;
  20. int N3;
  21. int N4;
  22. const int C1 = 16843012; //00000001000000010000000100000100
  23. const int C2 = 16843009; //00000001000000010000000100000001
  24. /**
  25. * Basic constructor.
  26. *
  27. * @param cipher the block cipher to be used as the basis of the
  28. * counter mode (must have a 64 bit block size).
  29. */
  30. public GOfbBlockCipher(
  31. IBlockCipher cipher)
  32. {
  33. this.cipher = cipher;
  34. this.blockSize = cipher.GetBlockSize();
  35. if (blockSize != 8)
  36. {
  37. throw new ArgumentException("GCTR only for 64 bit block ciphers");
  38. }
  39. this.IV = new byte[cipher.GetBlockSize()];
  40. this.ofbV = new byte[cipher.GetBlockSize()];
  41. this.ofbOutV = new byte[cipher.GetBlockSize()];
  42. }
  43. /**
  44. * return the underlying block cipher that we are wrapping.
  45. *
  46. * @return the underlying block cipher that we are wrapping.
  47. */
  48. public IBlockCipher GetUnderlyingCipher()
  49. {
  50. return cipher;
  51. }
  52. /**
  53. * Initialise the cipher and, possibly, the initialisation vector (IV).
  54. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  55. * An IV which is too short is handled in FIPS compliant fashion.
  56. *
  57. * @param encrypting if true the cipher is initialised for
  58. * encryption, if false for decryption.
  59. * @param parameters the key and other data required by the cipher.
  60. * @exception ArgumentException if the parameters argument is inappropriate.
  61. */
  62. public void Init(
  63. bool forEncryption, //ignored by this CTR mode
  64. ICipherParameters parameters)
  65. {
  66. firstStep = true;
  67. N3 = 0;
  68. N4 = 0;
  69. if (parameters is ParametersWithIV)
  70. {
  71. ParametersWithIV ivParam = (ParametersWithIV)parameters;
  72. byte[] iv = ivParam.GetIV();
  73. if (iv.Length < IV.Length)
  74. {
  75. // prepend the supplied IV with zeros (per FIPS PUB 81)
  76. Array.Copy(iv, 0, IV, IV.Length - iv.Length, iv.Length);
  77. for (int i = 0; i < IV.Length - iv.Length; i++)
  78. {
  79. IV[i] = 0;
  80. }
  81. }
  82. else
  83. {
  84. Array.Copy(iv, 0, IV, 0, IV.Length);
  85. }
  86. parameters = ivParam.Parameters;
  87. }
  88. Reset();
  89. // if it's null, key is to be reused.
  90. if (parameters != null)
  91. {
  92. cipher.Init(true, parameters);
  93. }
  94. }
  95. /**
  96. * return the algorithm name and mode.
  97. *
  98. * @return the name of the underlying algorithm followed by "/GCTR"
  99. * and the block size in bits
  100. */
  101. public string AlgorithmName
  102. {
  103. get { return cipher.AlgorithmName + "/GCTR"; }
  104. }
  105. public bool IsPartialBlockOkay
  106. {
  107. get { return true; }
  108. }
  109. /**
  110. * return the block size we are operating at (in bytes).
  111. *
  112. * @return the block size we are operating at (in bytes).
  113. */
  114. public int GetBlockSize()
  115. {
  116. return blockSize;
  117. }
  118. /**
  119. * Process one block of input from the array in and write it to
  120. * the out array.
  121. *
  122. * @param in the array containing the input data.
  123. * @param inOff offset into the in array the data starts at.
  124. * @param out the array the output data will be copied into.
  125. * @param outOff the offset into the out array the output will start at.
  126. * @exception DataLengthException if there isn't enough data in in, or
  127. * space in out.
  128. * @exception InvalidOperationException if the cipher isn't initialised.
  129. * @return the number of bytes processed and produced.
  130. */
  131. public int ProcessBlock(
  132. byte[] input,
  133. int inOff,
  134. byte[] output,
  135. int outOff)
  136. {
  137. if ((inOff + blockSize) > input.Length)
  138. {
  139. throw new DataLengthException("input buffer too short");
  140. }
  141. if ((outOff + blockSize) > output.Length)
  142. {
  143. throw new DataLengthException("output buffer too short");
  144. }
  145. if (firstStep)
  146. {
  147. firstStep = false;
  148. cipher.ProcessBlock(ofbV, 0, ofbOutV, 0);
  149. N3 = bytesToint(ofbOutV, 0);
  150. N4 = bytesToint(ofbOutV, 4);
  151. }
  152. N3 += C2;
  153. N4 += C1;
  154. if (N4 < C1) // addition is mod (2**32 - 1)
  155. {
  156. if (N4 > 0)
  157. {
  158. N4++;
  159. }
  160. }
  161. intTobytes(N3, ofbV, 0);
  162. intTobytes(N4, ofbV, 4);
  163. cipher.ProcessBlock(ofbV, 0, ofbOutV, 0);
  164. //
  165. // XOR the ofbV with the plaintext producing the cipher text (and
  166. // the next input block).
  167. //
  168. for (int i = 0; i < blockSize; i++)
  169. {
  170. output[outOff + i] = (byte)(ofbOutV[i] ^ input[inOff + i]);
  171. }
  172. //
  173. // change over the input block.
  174. //
  175. Array.Copy(ofbV, blockSize, ofbV, 0, ofbV.Length - blockSize);
  176. Array.Copy(ofbOutV, 0, ofbV, ofbV.Length - blockSize, blockSize);
  177. return blockSize;
  178. }
  179. /**
  180. * reset the feedback vector back to the IV and reset the underlying
  181. * cipher.
  182. */
  183. public void Reset()
  184. {
  185. Array.Copy(IV, 0, ofbV, 0, IV.Length);
  186. cipher.Reset();
  187. }
  188. //array of bytes to type int
  189. private int bytesToint(
  190. byte[] inBytes,
  191. int inOff)
  192. {
  193. return (int)((inBytes[inOff + 3] << 24) & 0xff000000) + ((inBytes[inOff + 2] << 16) & 0xff0000) +
  194. ((inBytes[inOff + 1] << 8) & 0xff00) + (inBytes[inOff] & 0xff);
  195. }
  196. //int to array of bytes
  197. private void intTobytes(
  198. int num,
  199. byte[] outBytes,
  200. int outOff)
  201. {
  202. outBytes[outOff + 3] = (byte)(num >> 24);
  203. outBytes[outOff + 2] = (byte)(num >> 16);
  204. outBytes[outOff + 1] = (byte)(num >> 8);
  205. outBytes[outOff] = (byte)num;
  206. }
  207. }
  208. }
  209. #pragma warning restore
  210. #endif