123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Diagnostics;
- using Org.BouncyCastle.Crypto.Parameters;
- namespace Org.BouncyCastle.Crypto
- {
-
- public class BufferedBlockCipher
- : BufferedCipherBase
- {
- internal byte[] buf;
- internal int bufOff;
- internal bool forEncryption;
- internal IBlockCipher cipher;
-
- protected BufferedBlockCipher()
- {
- }
-
- public BufferedBlockCipher(
- IBlockCipher cipher)
- {
- if (cipher == null)
- throw new ArgumentNullException("cipher");
- this.cipher = cipher;
- buf = new byte[cipher.GetBlockSize()];
- bufOff = 0;
- }
- public override string AlgorithmName
- {
- get { return cipher.AlgorithmName; }
- }
-
-
- public override void Init(
- bool forEncryption,
- ICipherParameters parameters)
- {
- this.forEncryption = forEncryption;
- ParametersWithRandom pwr = parameters as ParametersWithRandom;
- if (pwr != null)
- parameters = pwr.Parameters;
- Reset();
- cipher.Init(forEncryption, parameters);
- }
-
- public override int GetBlockSize()
- {
- return cipher.GetBlockSize();
- }
-
- public override int GetUpdateOutputSize(
- int length)
- {
- int total = length + bufOff;
- int leftOver = total % buf.Length;
- return total - leftOver;
- }
-
- public override int GetOutputSize(
- int length)
- {
-
- return length + bufOff;
- }
-
- public override int ProcessByte(
- byte input,
- byte[] output,
- int outOff)
- {
- buf[bufOff++] = input;
- if (bufOff == buf.Length)
- {
- if ((outOff + buf.Length) > output.Length)
- throw new DataLengthException("output buffer too short");
- bufOff = 0;
- return cipher.ProcessBlock(buf, 0, output, outOff);
- }
- return 0;
- }
- public override byte[] ProcessByte(
- byte input)
- {
- int outLength = GetUpdateOutputSize(1);
- byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
- int pos = ProcessByte(input, outBytes, 0);
- if (outLength > 0 && pos < outLength)
- {
- byte[] tmp = new byte[pos];
- Array.Copy(outBytes, 0, tmp, 0, pos);
- outBytes = tmp;
- }
- return outBytes;
- }
- public override byte[] ProcessBytes(
- byte[] input,
- int inOff,
- int length)
- {
- if (input == null)
- throw new ArgumentNullException("input");
- if (length < 1)
- return null;
- int outLength = GetUpdateOutputSize(length);
- byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
- int pos = ProcessBytes(input, inOff, length, outBytes, 0);
- if (outLength > 0 && pos < outLength)
- {
- byte[] tmp = new byte[pos];
- Array.Copy(outBytes, 0, tmp, 0, pos);
- outBytes = tmp;
- }
- return outBytes;
- }
-
- public override int ProcessBytes(
- byte[] input,
- int inOff,
- int length,
- byte[] output,
- int outOff)
- {
- if (length < 1)
- {
- if (length < 0)
- throw new ArgumentException("Can't have a negative input length!");
- return 0;
- }
- int blockSize = GetBlockSize();
- int outLength = GetUpdateOutputSize(length);
- if (outLength > 0)
- {
- Check.OutputLength(output, outOff, outLength, "output buffer too short");
- }
- int resultLen = 0;
- int gapLen = buf.Length - bufOff;
- if (length > gapLen)
- {
- Array.Copy(input, inOff, buf, bufOff, gapLen);
- resultLen += cipher.ProcessBlock(buf, 0, output, outOff);
- bufOff = 0;
- length -= gapLen;
- inOff += gapLen;
- while (length > buf.Length)
- {
- resultLen += cipher.ProcessBlock(input, inOff, output, outOff + resultLen);
- length -= blockSize;
- inOff += blockSize;
- }
- }
- Array.Copy(input, inOff, buf, bufOff, length);
- bufOff += length;
- if (bufOff == buf.Length)
- {
- resultLen += cipher.ProcessBlock(buf, 0, output, outOff + resultLen);
- bufOff = 0;
- }
- return resultLen;
- }
- public override byte[] DoFinal()
- {
- byte[] outBytes = EmptyBuffer;
- int length = GetOutputSize(0);
- if (length > 0)
- {
- outBytes = new byte[length];
- int pos = DoFinal(outBytes, 0);
- if (pos < outBytes.Length)
- {
- byte[] tmp = new byte[pos];
- Array.Copy(outBytes, 0, tmp, 0, pos);
- outBytes = tmp;
- }
- }
- else
- {
- Reset();
- }
- return outBytes;
- }
- public override byte[] DoFinal(
- byte[] input,
- int inOff,
- int inLen)
- {
- if (input == null)
- throw new ArgumentNullException("input");
- int length = GetOutputSize(inLen);
- byte[] outBytes = EmptyBuffer;
- if (length > 0)
- {
- outBytes = new byte[length];
- int pos = (inLen > 0)
- ? ProcessBytes(input, inOff, inLen, outBytes, 0)
- : 0;
- pos += DoFinal(outBytes, pos);
- if (pos < outBytes.Length)
- {
- byte[] tmp = new byte[pos];
- Array.Copy(outBytes, 0, tmp, 0, pos);
- outBytes = tmp;
- }
- }
- else
- {
- Reset();
- }
- return outBytes;
- }
-
- public override int DoFinal(
- byte[] output,
- int outOff)
- {
- try
- {
- if (bufOff != 0)
- {
- Check.DataLength(!cipher.IsPartialBlockOkay, "data not block size aligned");
- Check.OutputLength(output, outOff, bufOff, "output buffer too short for DoFinal()");
-
- cipher.ProcessBlock(buf, 0, buf, 0);
- Array.Copy(buf, 0, output, outOff, bufOff);
- }
- return bufOff;
- }
- finally
- {
- Reset();
- }
- }
-
- public override void Reset()
- {
- Array.Clear(buf, 0, buf.Length);
- bufOff = 0;
- cipher.Reset();
- }
- }
- }
- #endif
|