BufferedAsymmetricBlockCipher.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto
  7. {
  8. /**
  9. * a buffer wrapper for an asymmetric block cipher, allowing input
  10. * to be accumulated in a piecemeal fashion until final processing.
  11. */
  12. public class BufferedAsymmetricBlockCipher
  13. : BufferedCipherBase
  14. {
  15. private readonly IAsymmetricBlockCipher cipher;
  16. private byte[] buffer;
  17. private int bufOff;
  18. /**
  19. * base constructor.
  20. *
  21. * @param cipher the cipher this buffering object wraps.
  22. */
  23. public BufferedAsymmetricBlockCipher(
  24. IAsymmetricBlockCipher cipher)
  25. {
  26. this.cipher = cipher;
  27. }
  28. /**
  29. * return the amount of data sitting in the buffer.
  30. *
  31. * @return the amount of data sitting in the buffer.
  32. */
  33. internal int GetBufferPosition()
  34. {
  35. return bufOff;
  36. }
  37. public override string AlgorithmName
  38. {
  39. get { return cipher.AlgorithmName; }
  40. }
  41. public override int GetBlockSize()
  42. {
  43. return cipher.GetInputBlockSize();
  44. }
  45. public override int GetOutputSize(
  46. int length)
  47. {
  48. return cipher.GetOutputBlockSize();
  49. }
  50. public override int GetUpdateOutputSize(
  51. int length)
  52. {
  53. return 0;
  54. }
  55. /**
  56. * initialise the buffer and the underlying cipher.
  57. *
  58. * @param forEncryption if true the cipher is initialised for
  59. * encryption, if false for decryption.
  60. * @param param the key and other data required by the cipher.
  61. */
  62. public override void Init(
  63. bool forEncryption,
  64. ICipherParameters parameters)
  65. {
  66. Reset();
  67. cipher.Init(forEncryption, parameters);
  68. //
  69. // we allow for an extra byte where people are using their own padding
  70. // mechanisms on a raw cipher.
  71. //
  72. this.buffer = new byte[cipher.GetInputBlockSize() + (forEncryption ? 1 : 0)];
  73. this.bufOff = 0;
  74. }
  75. public override byte[] ProcessByte(
  76. byte input)
  77. {
  78. if (bufOff >= buffer.Length)
  79. throw new DataLengthException("attempt to process message to long for cipher");
  80. buffer[bufOff++] = input;
  81. return null;
  82. }
  83. public override byte[] ProcessBytes(
  84. byte[] input,
  85. int inOff,
  86. int length)
  87. {
  88. if (length < 1)
  89. return null;
  90. if (input == null)
  91. throw new ArgumentNullException("input");
  92. if (bufOff + length > buffer.Length)
  93. throw new DataLengthException("attempt to process message to long for cipher");
  94. Array.Copy(input, inOff, buffer, bufOff, length);
  95. bufOff += length;
  96. return null;
  97. }
  98. /**
  99. * process the contents of the buffer using the underlying
  100. * cipher.
  101. *
  102. * @return the result of the encryption/decryption process on the
  103. * buffer.
  104. * @exception InvalidCipherTextException if we are given a garbage block.
  105. */
  106. public override byte[] DoFinal()
  107. {
  108. byte[] outBytes = bufOff > 0
  109. ? cipher.ProcessBlock(buffer, 0, bufOff)
  110. : EmptyBuffer;
  111. Reset();
  112. return outBytes;
  113. }
  114. public override byte[] DoFinal(
  115. byte[] input,
  116. int inOff,
  117. int length)
  118. {
  119. ProcessBytes(input, inOff, length);
  120. return DoFinal();
  121. }
  122. /// <summary>Reset the buffer</summary>
  123. public override void Reset()
  124. {
  125. if (buffer != null)
  126. {
  127. Array.Clear(buffer, 0, buffer.Length);
  128. bufOff = 0;
  129. }
  130. }
  131. }
  132. }
  133. #pragma warning restore
  134. #endif