BufferedAeadBlockCipher.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Modes;
  4. using Org.BouncyCastle.Crypto.Parameters;
  5. namespace Org.BouncyCastle.Crypto
  6. {
  7. /**
  8. * The AEAD block ciphers already handle buffering internally, so this class
  9. * just takes care of implementing IBufferedCipher methods.
  10. */
  11. public class BufferedAeadBlockCipher
  12. : BufferedCipherBase
  13. {
  14. private readonly IAeadBlockCipher cipher;
  15. public BufferedAeadBlockCipher(
  16. IAeadBlockCipher cipher)
  17. {
  18. if (cipher == null)
  19. throw new ArgumentNullException("cipher");
  20. this.cipher = cipher;
  21. }
  22. public override string AlgorithmName
  23. {
  24. get { return cipher.AlgorithmName; }
  25. }
  26. /**
  27. * initialise the cipher.
  28. *
  29. * @param forEncryption if true the cipher is initialised for
  30. * encryption, if false for decryption.
  31. * @param param the key and other data required by the cipher.
  32. * @exception ArgumentException if the parameters argument is
  33. * inappropriate.
  34. */
  35. public override void Init(
  36. bool forEncryption,
  37. ICipherParameters parameters)
  38. {
  39. if (parameters is ParametersWithRandom)
  40. {
  41. parameters = ((ParametersWithRandom) parameters).Parameters;
  42. }
  43. cipher.Init(forEncryption, parameters);
  44. }
  45. /**
  46. * return the blocksize for the underlying cipher.
  47. *
  48. * @return the blocksize for the underlying cipher.
  49. */
  50. public override int GetBlockSize()
  51. {
  52. return cipher.GetBlockSize();
  53. }
  54. /**
  55. * return the size of the output buffer required for an update
  56. * an input of len bytes.
  57. *
  58. * @param len the length of the input.
  59. * @return the space required to accommodate a call to update
  60. * with len bytes of input.
  61. */
  62. public override int GetUpdateOutputSize(
  63. int length)
  64. {
  65. return cipher.GetUpdateOutputSize(length);
  66. }
  67. /**
  68. * return the size of the output buffer required for an update plus a
  69. * doFinal with an input of len bytes.
  70. *
  71. * @param len the length of the input.
  72. * @return the space required to accommodate a call to update and doFinal
  73. * with len bytes of input.
  74. */
  75. public override int GetOutputSize(
  76. int length)
  77. {
  78. return cipher.GetOutputSize(length);
  79. }
  80. /**
  81. * process a single byte, producing an output block if necessary.
  82. *
  83. * @param in the input byte.
  84. * @param out the space for any output that might be produced.
  85. * @param outOff the offset from which the output will be copied.
  86. * @return the number of output bytes copied to out.
  87. * @exception DataLengthException if there isn't enough space in out.
  88. * @exception InvalidOperationException if the cipher isn't initialised.
  89. */
  90. public override int ProcessByte(
  91. byte input,
  92. byte[] output,
  93. int outOff)
  94. {
  95. return cipher.ProcessByte(input, output, outOff);
  96. }
  97. public override byte[] ProcessByte(
  98. byte input)
  99. {
  100. int outLength = GetUpdateOutputSize(1);
  101. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  102. int pos = ProcessByte(input, outBytes, 0);
  103. if (outLength > 0 && pos < outLength)
  104. {
  105. byte[] tmp = new byte[pos];
  106. Array.Copy(outBytes, 0, tmp, 0, pos);
  107. outBytes = tmp;
  108. }
  109. return outBytes;
  110. }
  111. public override byte[] ProcessBytes(
  112. byte[] input,
  113. int inOff,
  114. int length)
  115. {
  116. if (input == null)
  117. throw new ArgumentNullException("input");
  118. if (length < 1)
  119. return null;
  120. int outLength = GetUpdateOutputSize(length);
  121. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  122. int pos = ProcessBytes(input, inOff, length, outBytes, 0);
  123. if (outLength > 0 && pos < outLength)
  124. {
  125. byte[] tmp = new byte[pos];
  126. Array.Copy(outBytes, 0, tmp, 0, pos);
  127. outBytes = tmp;
  128. }
  129. return outBytes;
  130. }
  131. /**
  132. * process an array of bytes, producing output if necessary.
  133. *
  134. * @param in the input byte array.
  135. * @param inOff the offset at which the input data starts.
  136. * @param len the number of bytes to be copied out of the input array.
  137. * @param out the space for any output that might be produced.
  138. * @param outOff the offset from which the output will be copied.
  139. * @return the number of output bytes copied to out.
  140. * @exception DataLengthException if there isn't enough space in out.
  141. * @exception InvalidOperationException if the cipher isn't initialised.
  142. */
  143. public override int ProcessBytes(
  144. byte[] input,
  145. int inOff,
  146. int length,
  147. byte[] output,
  148. int outOff)
  149. {
  150. return cipher.ProcessBytes(input, inOff, length, output, outOff);
  151. }
  152. public override byte[] DoFinal()
  153. {
  154. byte[] outBytes = new byte[GetOutputSize(0)];
  155. int pos = DoFinal(outBytes, 0);
  156. if (pos < outBytes.Length)
  157. {
  158. byte[] tmp = new byte[pos];
  159. Array.Copy(outBytes, 0, tmp, 0, pos);
  160. outBytes = tmp;
  161. }
  162. return outBytes;
  163. }
  164. public override byte[] DoFinal(
  165. byte[] input,
  166. int inOff,
  167. int inLen)
  168. {
  169. if (input == null)
  170. throw new ArgumentNullException("input");
  171. byte[] outBytes = new byte[GetOutputSize(inLen)];
  172. int pos = (inLen > 0)
  173. ? ProcessBytes(input, inOff, inLen, outBytes, 0)
  174. : 0;
  175. pos += DoFinal(outBytes, pos);
  176. if (pos < outBytes.Length)
  177. {
  178. byte[] tmp = new byte[pos];
  179. Array.Copy(outBytes, 0, tmp, 0, pos);
  180. outBytes = tmp;
  181. }
  182. return outBytes;
  183. }
  184. /**
  185. * Process the last block in the buffer.
  186. *
  187. * @param out the array the block currently being held is copied into.
  188. * @param outOff the offset at which the copying starts.
  189. * @return the number of output bytes copied to out.
  190. * @exception DataLengthException if there is insufficient space in out for
  191. * the output, or the input is not block size aligned and should be.
  192. * @exception InvalidOperationException if the underlying cipher is not
  193. * initialised.
  194. * @exception InvalidCipherTextException if padding is expected and not found.
  195. * @exception DataLengthException if the input is not block size
  196. * aligned.
  197. */
  198. public override int DoFinal(
  199. byte[] output,
  200. int outOff)
  201. {
  202. return cipher.DoFinal(output, outOff);
  203. }
  204. /**
  205. * Reset the buffer and cipher. After resetting the object is in the same
  206. * state as it was after the last init (if there was one).
  207. */
  208. public override void Reset()
  209. {
  210. cipher.Reset();
  211. }
  212. }
  213. }
  214. #endif