BufferedAsymmetricBlockCipher.cs 3.5 KB

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