GOFBBlockCipher.cs 5.5 KB

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