<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />

CtsBlockCipher

using System; namespace Org.BouncyCastle.Crypto.Modes { public class CtsBlockCipher : BufferedBlockCipher { private readonly int blockSize; public CtsBlockCipher(IBlockCipher cipher) : this(EcbBlockCipher.GetBlockCipherMode(cipher)) { } public CtsBlockCipher(IBlockCipherMode cipherMode) { if (!(cipherMode is CbcBlockCipher) && !(cipherMode is EcbBlockCipher)) throw new ArgumentException("CtsBlockCipher can only accept ECB, or CBC ciphers"); m_cipherMode = cipherMode; blockSize = cipherMode.GetBlockSize(); buf = new byte[blockSize * 2]; bufOff = 0; } public override int GetUpdateOutputSize(int length) { int num = length + bufOff; int num2 = num % buf.Length; if (num2 == 0) return num - buf.Length; return num - num2; } public override int GetOutputSize(int length) { return length + bufOff; } public override int ProcessByte(byte input, byte[] output, int outOff) { int result = 0; if (bufOff == buf.Length) { result = m_cipherMode.ProcessBlock(buf, 0, output, outOff); Array.Copy(buf, blockSize, buf, 0, blockSize); bufOff = blockSize; } buf[bufOff++] = input; return result; } public override int ProcessByte(byte input, Span<byte> output) { int result = 0; if (bufOff == buf.Length) { result = m_cipherMode.ProcessBlock(buf, output); Array.Copy(buf, blockSize, buf, 0, blockSize); bufOff = blockSize; } buf[bufOff++] = input; return result; } public override int ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff) { if (length < 0) throw new ArgumentException("Can't have a negative input length!"); int num = GetBlockSize(); int updateOutputSize = GetUpdateOutputSize(length); if (updateOutputSize > 0) Check.OutputLength(output, outOff, updateOutputSize, "output buffer too short"); int num2 = 0; int num3 = buf.Length - bufOff; if (length > num3) { Array.Copy(input, inOff, buf, bufOff, num3); num2 = m_cipherMode.ProcessBlock(buf, 0, output, outOff); Array.Copy(buf, num, buf, 0, num); bufOff = num; length -= num3; inOff += num3; while (length > num) { Array.Copy(input, inOff, buf, bufOff, num); num2 += m_cipherMode.ProcessBlock(buf, 0, output, outOff + num2); Array.Copy(buf, num, buf, 0, num); length -= num; inOff += num; } } Array.Copy(input, inOff, buf, bufOff, length); bufOff += length; return num2; } public override int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output) { int num = GetBlockSize(); int updateOutputSize = GetUpdateOutputSize(input.Length); if (updateOutputSize > 0) Check.OutputLength(output, updateOutputSize, "output buffer too short"); int num2 = 0; int num3 = buf.Length - bufOff; if (input.Length > num3) { ReadOnlySpan<byte> readOnlySpan = input.Slice(0, num3); readOnlySpan.CopyTo(buf.AsSpan(bufOff)); num2 = m_cipherMode.ProcessBlock(buf, output); Array.Copy(buf, num, buf, 0, num); bufOff = num; int num4 = num3; input = input.Slice(num4, input.Length - num4); while (input.Length > num) { readOnlySpan = input.Slice(0, num); readOnlySpan.CopyTo(buf.AsSpan(bufOff)); int num5 = num2; IBlockCipherMode cipherMode = m_cipherMode; ReadOnlySpan<byte> input2 = buf; num4 = num2; num2 = num5 + cipherMode.ProcessBlock(input2, output.Slice(num4, output.Length - num4)); Array.Copy(buf, num, buf, 0, num); num4 = num; input = input.Slice(num4, input.Length - num4); } } input.CopyTo(buf.AsSpan(bufOff)); bufOff += input.Length; return num2; } public override int DoFinal(byte[] output, int outOff) { if (base.bufOff + outOff > output.Length) throw new DataLengthException("output buffer too small in DoFinal"); int num = m_cipherMode.GetBlockSize(); int length = base.bufOff - num; byte[] array = new byte[num]; if (forEncryption) { m_cipherMode.ProcessBlock(buf, 0, array, 0); if (base.bufOff < num) throw new DataLengthException("need at least one block of input for CTS"); for (int i = base.bufOff; i != buf.Length; i++) { buf[i] = array[i - num]; } for (int j = num; j != base.bufOff; j++) { buf[j] ^= array[j - num]; } m_cipherMode.UnderlyingCipher.ProcessBlock(buf, num, output, outOff); Array.Copy(array, 0, output, outOff + num, length); } else { byte[] array2 = new byte[num]; m_cipherMode.UnderlyingCipher.ProcessBlock(buf, 0, array, 0); for (int k = num; k != base.bufOff; k++) { array2[k - num] = (byte)(array[k - num] ^ buf[k]); } Array.Copy(buf, num, array, 0, length); m_cipherMode.ProcessBlock(array, 0, output, outOff); Array.Copy(array2, 0, output, outOff + num, length); } int bufOff = base.bufOff; Reset(); return bufOff; } public unsafe override int DoFinal(Span<byte> output) { if (base.bufOff > output.Length) throw new DataLengthException("output buffer too small in DoFinal"); int num = m_cipherMode.GetBlockSize(); int length = base.bufOff - num; Span<byte> span; if (num <= 64) { int num2 = num; span = new Span<byte>(stackalloc byte[(int)(uint)num2], num2); } else span = new byte[num]; Span<byte> span2 = span; if (forEncryption) { m_cipherMode.ProcessBlock(buf, span2); if (base.bufOff < num) throw new DataLengthException("need at least one block of input for CTS"); for (int i = base.bufOff; i != buf.Length; i++) { buf[i] = span2[i - num]; } for (int j = num; j != base.bufOff; j++) { buf[j] ^= span2[j - num]; } m_cipherMode.UnderlyingCipher.ProcessBlock(buf.AsSpan(num), output); span = span2.Slice(0, length); int num2 = num; span.CopyTo(output.Slice(num2, output.Length - num2)); } else { int num2; if (num <= 64) { num2 = num; span = new Span<byte>(stackalloc byte[(int)(uint)num2], num2); } else span = new byte[num]; Span<byte> span3 = span; m_cipherMode.UnderlyingCipher.ProcessBlock(buf, span2); for (int k = num; k != base.bufOff; k++) { span3[k - num] = (byte)(span2[k - num] ^ buf[k]); } span = buf.AsSpan(num, length); span.CopyTo(span2); m_cipherMode.ProcessBlock(span2, output); span = span3.Slice(0, length); num2 = num; span.CopyTo(output.Slice(num2, output.Length - num2)); } int bufOff = base.bufOff; Reset(); return bufOff; } } }