123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using Org.BouncyCastle.Crypto.Parameters;
- namespace Org.BouncyCastle.Crypto.Modes
- {
-
- public class CbcBlockCipher
- : IBlockCipher
- {
- private byte[] IV, cbcV, cbcNextV;
- private int blockSize;
- private IBlockCipher cipher;
- private bool encrypting;
-
- public CbcBlockCipher(
- IBlockCipher cipher)
- {
- this.cipher = cipher;
- this.blockSize = cipher.GetBlockSize();
- this.IV = new byte[blockSize];
- this.cbcV = new byte[blockSize];
- this.cbcNextV = new byte[blockSize];
- }
-
- public IBlockCipher GetUnderlyingCipher()
- {
- return cipher;
- }
-
- public void Init(
- bool forEncryption,
- ICipherParameters parameters)
- {
- bool oldEncrypting = this.encrypting;
- this.encrypting = forEncryption;
- if (parameters is ParametersWithIV)
- {
- ParametersWithIV ivParam = (ParametersWithIV)parameters;
- byte[] iv = ivParam.GetIV();
- if (iv.Length != blockSize)
- {
- throw new ArgumentException("initialisation vector must be the same length as block size");
- }
- Array.Copy(iv, 0, IV, 0, iv.Length);
- parameters = ivParam.Parameters;
- }
- Reset();
-
- if (parameters != null)
- {
- cipher.Init(encrypting, parameters);
- }
- else if (oldEncrypting != encrypting)
- {
- throw new ArgumentException("cannot change encrypting state without providing key.");
- }
- }
-
- public string AlgorithmName
- {
- get { return cipher.AlgorithmName + "/CBC"; }
- }
- public bool IsPartialBlockOkay
- {
- get { return false; }
- }
-
- public int GetBlockSize()
- {
- return cipher.GetBlockSize();
- }
-
- public int ProcessBlock(
- byte[] input,
- int inOff,
- byte[] output,
- int outOff)
- {
- return (encrypting)
- ? EncryptBlock(input, inOff, output, outOff)
- : DecryptBlock(input, inOff, output, outOff);
- }
-
- public void Reset()
- {
- Array.Copy(IV, 0, cbcV, 0, IV.Length);
- Array.Clear(cbcNextV, 0, cbcNextV.Length);
- cipher.Reset();
- }
-
- private int EncryptBlock(
- byte[] input,
- int inOff,
- byte[] outBytes,
- int outOff)
- {
- if ((inOff + blockSize) > input.Length)
- {
- throw new DataLengthException("input buffer too short");
- }
-
- for (int i = 0; i < blockSize; i++)
- {
- cbcV[i] ^= input[inOff + i];
- }
- int length = cipher.ProcessBlock(cbcV, 0, outBytes, outOff);
-
- Array.Copy(outBytes, outOff, cbcV, 0, cbcV.Length);
- return length;
- }
-
- private int DecryptBlock(
- byte[] input,
- int inOff,
- byte[] outBytes,
- int outOff)
- {
- if ((inOff + blockSize) > input.Length)
- {
- throw new DataLengthException("input buffer too short");
- }
- Array.Copy(input, inOff, cbcNextV, 0, blockSize);
- int length = cipher.ProcessBlock(input, inOff, outBytes, outOff);
-
- for (int i = 0; i < blockSize; i++)
- {
- outBytes[outOff + i] ^= cbcV[i];
- }
-
- byte[] tmp;
- tmp = cbcV;
- cbcV = cbcNextV;
- cbcNextV = tmp;
- return length;
- }
- }
- }
- #endif
|