StreamBlockCipher.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto
  6. {
  7. /**
  8. * a wrapper for block ciphers with a single byte block size, so that they
  9. * can be treated like stream ciphers.
  10. */
  11. public class StreamBlockCipher
  12. : IStreamCipher
  13. {
  14. private readonly IBlockCipherMode m_cipherMode;
  15. private readonly byte[] oneByte = new byte[1];
  16. /**
  17. * basic constructor.
  18. *
  19. * @param cipher the block cipher to be wrapped.
  20. * @exception ArgumentException if the cipher has a block size other than
  21. * one.
  22. */
  23. public StreamBlockCipher(IBlockCipherMode cipherMode)
  24. {
  25. if (cipherMode == null)
  26. throw new ArgumentNullException(nameof(cipherMode));
  27. if (cipherMode.GetBlockSize() != 1)
  28. throw new ArgumentException("block cipher block size != 1.", nameof(cipherMode));
  29. m_cipherMode = cipherMode;
  30. }
  31. /**
  32. * initialise the underlying cipher.
  33. *
  34. * @param forEncryption true if we are setting up for encryption, false otherwise.
  35. * @param param the necessary parameters for the underlying cipher to be initialised.
  36. */
  37. public void Init(bool forEncryption, ICipherParameters parameters)
  38. {
  39. m_cipherMode.Init(forEncryption, parameters);
  40. }
  41. /**
  42. * return the name of the algorithm we are wrapping.
  43. *
  44. * @return the name of the algorithm we are wrapping.
  45. */
  46. public string AlgorithmName
  47. {
  48. get { return m_cipherMode.AlgorithmName; }
  49. }
  50. /**
  51. * encrypt/decrypt a single byte returning the result.
  52. *
  53. * @param in the byte to be processed.
  54. * @return the result of processing the input byte.
  55. */
  56. public byte ReturnByte(byte input)
  57. {
  58. oneByte[0] = input;
  59. m_cipherMode.ProcessBlock(oneByte, 0, oneByte, 0);
  60. return oneByte[0];
  61. }
  62. /**
  63. * process a block of bytes from in putting the result into out.
  64. *
  65. * @param in the input byte array.
  66. * @param inOff the offset into the in array where the data to be processed starts.
  67. * @param len the number of bytes to be processed.
  68. * @param out the output buffer the processed bytes go into.
  69. * @param outOff the offset into the output byte array the processed data stars at.
  70. * @exception DataLengthException if the output buffer is too small.
  71. */
  72. public void ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff)
  73. {
  74. Check.DataLength(input, inOff, length, "input buffer too short");
  75. Check.OutputLength(output, outOff, length, "output buffer too short");
  76. for (int i = 0; i != length; i++)
  77. {
  78. m_cipherMode.ProcessBlock(input, inOff + i, output, outOff + i);
  79. }
  80. }
  81. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  82. public virtual void ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
  83. {
  84. Check.OutputLength(output, input.Length, "output buffer too short");
  85. for (int i = 0; i != input.Length; i++)
  86. {
  87. m_cipherMode.ProcessBlock(input[i..], output[i..]);
  88. }
  89. }
  90. #endif
  91. /**
  92. * reset the underlying cipher. This leaves it in the same state
  93. * it was at after the last init (if there was one).
  94. */
  95. public void Reset()
  96. {
  97. m_cipherMode.Reset();
  98. }
  99. }
  100. }
  101. #pragma warning restore
  102. #endif