SicBlockCipher.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  8. {
  9. /**
  10. * Implements the Segmented Integer Counter (SIC) mode on top of a simple
  11. * block cipher.
  12. */
  13. public class SicBlockCipher
  14. : IBlockCipher
  15. {
  16. private readonly IBlockCipher cipher;
  17. private readonly int blockSize;
  18. private readonly byte[] counter;
  19. private readonly byte[] counterOut;
  20. private byte[] IV;
  21. /**
  22. * Basic constructor.
  23. *
  24. * @param c the block cipher to be used.
  25. */
  26. public SicBlockCipher(IBlockCipher cipher)
  27. {
  28. this.cipher = cipher;
  29. this.blockSize = cipher.GetBlockSize();
  30. this.counter = new byte[blockSize];
  31. this.counterOut = new byte[blockSize];
  32. this.IV = new byte[blockSize];
  33. }
  34. /**
  35. * return the underlying block cipher that we are wrapping.
  36. *
  37. * @return the underlying block cipher that we are wrapping.
  38. */
  39. public virtual IBlockCipher GetUnderlyingCipher()
  40. {
  41. return cipher;
  42. }
  43. public virtual void Init(
  44. bool forEncryption, //ignored by this CTR mode
  45. ICipherParameters parameters)
  46. {
  47. ParametersWithIV ivParam = parameters as ParametersWithIV;
  48. if (ivParam == null)
  49. throw new ArgumentException("CTR/SIC mode requires ParametersWithIV", "parameters");
  50. this.IV = Arrays.Clone(ivParam.GetIV());
  51. if (blockSize < IV.Length)
  52. throw new ArgumentException("CTR/SIC mode requires IV no greater than: " + blockSize + " bytes.");
  53. int maxCounterSize = System.Math.Min(8, blockSize / 2);
  54. if (blockSize - IV.Length > maxCounterSize)
  55. throw new ArgumentException("CTR/SIC mode requires IV of at least: " + (blockSize - maxCounterSize) + " bytes.");
  56. // if null it's an IV changed only.
  57. if (ivParam.Parameters != null)
  58. {
  59. cipher.Init(true, ivParam.Parameters);
  60. }
  61. Reset();
  62. }
  63. public virtual string AlgorithmName
  64. {
  65. get { return cipher.AlgorithmName + "/SIC"; }
  66. }
  67. public virtual bool IsPartialBlockOkay
  68. {
  69. get { return true; }
  70. }
  71. public virtual int GetBlockSize()
  72. {
  73. return cipher.GetBlockSize();
  74. }
  75. public virtual int ProcessBlock(
  76. byte[] input,
  77. int inOff,
  78. byte[] output,
  79. int outOff)
  80. {
  81. cipher.ProcessBlock(counter, 0, counterOut, 0);
  82. //
  83. // XOR the counterOut with the plaintext producing the cipher text
  84. //
  85. for (int i = 0; i < counterOut.Length; i++)
  86. {
  87. output[outOff + i] = (byte)(counterOut[i] ^ input[inOff + i]);
  88. }
  89. // Increment the counter
  90. int j = counter.Length;
  91. while (--j >= 0 && ++counter[j] == 0)
  92. {
  93. }
  94. return counter.Length;
  95. }
  96. public virtual void Reset()
  97. {
  98. Arrays.Fill(counter, (byte)0);
  99. Array.Copy(IV, 0, counter, 0, IV.Length);
  100. cipher.Reset();
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif