123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Collections;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
- {
- public class Dstu7624WrapEngine
- : IWrapper
- {
- private KeyParameter param;
- private Dstu7624Engine engine;
- private bool forWrapping;
- private int blockSize;
- public Dstu7624WrapEngine(int blockSizeBits)
- {
- engine = new Dstu7624Engine(blockSizeBits);
- param = null;
- blockSize = blockSizeBits / 8;
- }
- public string AlgorithmName
- {
- get { return "Dstu7624WrapEngine"; }
- }
- public void Init(bool forWrapping, ICipherParameters parameters)
- {
- this.forWrapping = forWrapping;
- if (parameters is KeyParameter)
- {
- this.param = (KeyParameter)parameters;
- engine.Init(forWrapping, param);
- }
- else
- {
- throw new ArgumentException("Bad parameters passed to Dstu7624WrapEngine");
- }
- }
- public byte[] Wrap(byte[] input, int inOff, int length)
- {
- if (!forWrapping)
- throw new InvalidOperationException("Not set for wrapping");
- if (length % blockSize != 0)
- throw new ArgumentException("Padding not supported");
- int n = 2 * (1 + length / blockSize);
- int V = (n - 1) * 6;
- byte[] buffer = new byte[length + blockSize];
- Array.Copy(input, inOff, buffer, 0, length);
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- byte[] B = new byte[blockSize / 2];
- Array.Copy(buffer, 0, B, 0, blockSize / 2);
- //Console.WriteLine("B0: "+ BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
- IList bTemp = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- int bHalfBlocksLen = buffer.Length - blockSize / 2;
- int bufOff = blockSize / 2;
- while (bHalfBlocksLen != 0)
- {
- byte[] temp = new byte[blockSize / 2];
- Array.Copy(buffer, bufOff, temp, 0, blockSize / 2);
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- //Console.WriteLine(buffer.Length);
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(temp));
- bTemp.Add(temp);
- bHalfBlocksLen -= blockSize / 2;
- bufOff += blockSize / 2;
- }
- for (int j = 0; j < V; j++)
- {
- Array.Copy(B, 0, buffer, 0, blockSize / 2);
- Array.Copy((byte[])bTemp[0], 0, buffer, blockSize / 2, blockSize / 2);
- engine.ProcessBlock(buffer, 0, buffer, 0);
- byte[] intArray = Pack.UInt32_To_LE((uint)(j + 1));
- for (int byteNum = 0; byteNum < intArray.Length; byteNum++)
- {
- buffer[byteNum + blockSize / 2] ^= intArray[byteNum];
- }
- Array.Copy(buffer, blockSize / 2, B, 0, blockSize / 2);
- for (int i = 2; i < n; i++)
- {
- Array.Copy((byte[])bTemp[i - 1], 0, (byte[])bTemp[i - 2], 0, blockSize / 2);
- }
- Array.Copy(buffer, 0, (byte[])bTemp[n - 2], 0, blockSize / 2);
- //Console.WriteLine("B" + j.ToString() + ": " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[0]));
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[1]));
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[2]));
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- }
- Array.Copy(B, 0, buffer, 0, blockSize / 2);
- bufOff = blockSize / 2;
- for (int i = 0; i < n - 1; i++)
- {
- Array.Copy((byte[])bTemp[i], 0, buffer, bufOff, blockSize / 2);
- bufOff += blockSize / 2;
- }
- return buffer;
- }
- public byte[] Unwrap(byte[] input, int inOff, int length)
- {
- if (forWrapping)
- throw new InvalidOperationException("not set for unwrapping");
- if (length % blockSize != 0)
- throw new ArgumentException("Padding not supported");
- int n = 2 * length / blockSize;
- int V = (n - 1) * 6;
- byte[] buffer = new byte[length];
- Array.Copy(input, inOff, buffer, 0, length);
- byte[] B = new byte[blockSize / 2];
- Array.Copy(buffer, 0, B, 0, blockSize / 2);
- //Console.WriteLine("B18: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
- IList bTemp = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- int bHalfBlocksLen = buffer.Length - blockSize / 2;
- int bufOff = blockSize / 2;
- while (bHalfBlocksLen != 0)
- {
- byte[] temp = new byte[blockSize / 2];
- Array.Copy(buffer, bufOff, temp, 0, blockSize / 2);
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- //Console.WriteLine(buffer.Length);
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(temp));
- bTemp.Add(temp);
- bHalfBlocksLen -= blockSize / 2;
- bufOff += blockSize / 2;
- }
- for (int j = 0; j < V; j++)
- {
- Array.Copy((byte[])bTemp[n - 2], 0, buffer, 0, blockSize / 2);
- Array.Copy(B, 0, buffer, blockSize / 2, blockSize / 2);
- byte[] intArray = Pack.UInt32_To_LE((uint)(V - j));
- for (int byteNum = 0; byteNum < intArray.Length; byteNum++)
- {
- buffer[byteNum + blockSize / 2] ^= intArray[byteNum];
- }
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- engine.ProcessBlock(buffer, 0, buffer, 0);
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- Array.Copy(buffer, 0, B, 0, blockSize / 2);
- for (int i = 2; i < n; i++)
- {
- Array.Copy((byte[])bTemp[n - i - 1], 0, (byte[])bTemp[n - i], 0, blockSize / 2);
- }
- Array.Copy(buffer, blockSize / 2, (byte[])bTemp[0], 0, blockSize / 2);
- //Console.WriteLine("B" + (V - j - 1).ToString() + ": " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B));
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[0]));
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[1]));
- //Console.WriteLine("b: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[2]));
- //Console.WriteLine(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(buffer));
- }
- Array.Copy(B, 0, buffer, 0, blockSize / 2);
- bufOff = blockSize / 2;
- for (int i = 0; i < n - 1; i++)
- {
- Array.Copy((byte[])bTemp[i], 0, buffer, bufOff, blockSize / 2);
- bufOff += blockSize / 2;
- }
- byte diff = 0;
- for (int i = buffer.Length - blockSize; i < buffer.Length; ++i)
- {
- diff |= buffer[i];
- }
- if (diff != 0)
- throw new InvalidCipherTextException("checksum failed");
- return Arrays.CopyOfRange(buffer, 0, buffer.Length - blockSize);
- }
- }
- }
- #pragma warning restore
- #endif
|