GOFBBlockCipher.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  7. {
  8. /**
  9. * implements the GOST 28147 OFB counter mode (GCTR).
  10. */
  11. public class GOfbBlockCipher
  12. : IBlockCipherMode
  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 UnderlyingCipher => cipher;
  49. /**
  50. * Initialise the cipher and, possibly, the initialisation vector (IV).
  51. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  52. * An IV which is too short is handled in FIPS compliant fashion.
  53. *
  54. * @param encrypting if true the cipher is initialised for
  55. * encryption, if false for decryption.
  56. * @param parameters the key and other data required by the cipher.
  57. * @exception ArgumentException if the parameters argument is inappropriate.
  58. */
  59. public void Init(
  60. bool forEncryption, //ignored by this CTR mode
  61. ICipherParameters parameters)
  62. {
  63. firstStep = true;
  64. N3 = 0;
  65. N4 = 0;
  66. if (parameters is ParametersWithIV)
  67. {
  68. ParametersWithIV ivParam = (ParametersWithIV)parameters;
  69. byte[] iv = ivParam.GetIV();
  70. if (iv.Length < IV.Length)
  71. {
  72. // prepend the supplied IV with zeros (per FIPS PUB 81)
  73. Array.Copy(iv, 0, IV, IV.Length - iv.Length, iv.Length);
  74. for (int i = 0; i < IV.Length - iv.Length; i++)
  75. {
  76. IV[i] = 0;
  77. }
  78. }
  79. else
  80. {
  81. Array.Copy(iv, 0, IV, 0, IV.Length);
  82. }
  83. parameters = ivParam.Parameters;
  84. }
  85. Reset();
  86. // if it's null, key is to be reused.
  87. if (parameters != null)
  88. {
  89. cipher.Init(true, parameters);
  90. }
  91. }
  92. /**
  93. * return the algorithm name and mode.
  94. *
  95. * @return the name of the underlying algorithm followed by "/GCTR"
  96. * and the block size in bits
  97. */
  98. public string AlgorithmName
  99. {
  100. get { return cipher.AlgorithmName + "/GCTR"; }
  101. }
  102. public bool IsPartialBlockOkay
  103. {
  104. get { return true; }
  105. }
  106. /**
  107. * return the block size we are operating at (in bytes).
  108. *
  109. * @return the block size we are operating at (in bytes).
  110. */
  111. public int GetBlockSize()
  112. {
  113. return blockSize;
  114. }
  115. public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  116. {
  117. Check.DataLength(input, inOff, blockSize, "input buffer too short");
  118. Check.OutputLength(output, outOff, blockSize, "output buffer too short");
  119. if (firstStep)
  120. {
  121. firstStep = false;
  122. cipher.ProcessBlock(ofbV, 0, ofbOutV, 0);
  123. N3 = (int)Pack.LE_To_UInt32(ofbOutV, 0);
  124. N4 = (int)Pack.LE_To_UInt32(ofbOutV, 4);
  125. }
  126. N3 += C2;
  127. N4 += C1;
  128. if (N4 < C1) // addition is mod (2**32 - 1)
  129. {
  130. if (N4 > 0)
  131. {
  132. N4++;
  133. }
  134. }
  135. Pack.UInt32_To_LE((uint)N3, ofbV, 0);
  136. Pack.UInt32_To_LE((uint)N4, ofbV, 4);
  137. cipher.ProcessBlock(ofbV, 0, ofbOutV, 0);
  138. //
  139. // XOR the ofbV with the plaintext producing the cipher text (and
  140. // the next input block).
  141. //
  142. for (int i = 0; i < blockSize; i++)
  143. {
  144. output[outOff + i] = (byte)(ofbOutV[i] ^ input[inOff + i]);
  145. }
  146. //
  147. // change over the input block.
  148. //
  149. Array.Copy(ofbV, blockSize, ofbV, 0, ofbV.Length - blockSize);
  150. Array.Copy(ofbOutV, 0, ofbV, ofbV.Length - blockSize, blockSize);
  151. return blockSize;
  152. }
  153. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  154. public int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  155. {
  156. Check.DataLength(input, blockSize, "input buffer too short");
  157. Check.OutputLength(output, blockSize, "output buffer too short");
  158. if (firstStep)
  159. {
  160. firstStep = false;
  161. cipher.ProcessBlock(ofbV, ofbOutV);
  162. N3 = (int)Pack.LE_To_UInt32(ofbOutV, 0);
  163. N4 = (int)Pack.LE_To_UInt32(ofbOutV, 4);
  164. }
  165. N3 += C2;
  166. N4 += C1;
  167. if (N4 < C1) // addition is mod (2**32 - 1)
  168. {
  169. if (N4 > 0)
  170. {
  171. N4++;
  172. }
  173. }
  174. Pack.UInt32_To_LE((uint)N3, ofbV, 0);
  175. Pack.UInt32_To_LE((uint)N4, ofbV, 4);
  176. cipher.ProcessBlock(ofbV, ofbOutV);
  177. //
  178. // XOR the ofbV with the plaintext producing the cipher text (and
  179. // the next input block).
  180. //
  181. for (int i = 0; i < blockSize; i++)
  182. {
  183. output[i] = (byte)(ofbOutV[i] ^ input[i]);
  184. }
  185. //
  186. // change over the input block.
  187. //
  188. Array.Copy(ofbV, blockSize, ofbV, 0, ofbV.Length - blockSize);
  189. Array.Copy(ofbOutV, 0, ofbV, ofbV.Length - blockSize, blockSize);
  190. return blockSize;
  191. }
  192. #endif
  193. /**
  194. * reset the feedback vector back to the IV and reset the underlying
  195. * cipher.
  196. */
  197. public void Reset()
  198. {
  199. Array.Copy(IV, 0, ofbV, 0, IV.Length);
  200. }
  201. }
  202. }
  203. #pragma warning restore
  204. #endif