SicBlockCipher.cs 3.4 KB

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