123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using Org.BouncyCastle.Crypto.Parameters;
- using Org.BouncyCastle.Utilities;
- namespace Org.BouncyCastle.Crypto.Engines
- {
-
- public class RC6Engine
- : IBlockCipher
- {
- private static readonly int wordSize = 32;
- private static readonly int bytesPerWord = wordSize / 8;
-
- private static readonly int _noRounds = 20;
-
- private int [] _S;
-
- private static readonly int P32 = unchecked((int) 0xb7e15163);
- private static readonly int Q32 = unchecked((int) 0x9e3779b9);
- private static readonly int LGW = 5;
- private bool forEncryption;
-
- public RC6Engine()
- {
- }
- public virtual string AlgorithmName
- {
- get { return "RC6"; }
- }
- public virtual bool IsPartialBlockOkay
- {
- get { return false; }
- }
- public virtual int GetBlockSize()
- {
- return 4 * bytesPerWord;
- }
-
- public virtual void Init(
- bool forEncryption,
- ICipherParameters parameters)
- {
- if (!(parameters is KeyParameter))
- throw new ArgumentException("invalid parameter passed to RC6 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
- this.forEncryption = forEncryption;
- KeyParameter p = (KeyParameter)parameters;
- SetKey(p.GetKey());
- }
- public virtual int ProcessBlock(
- byte[] input,
- int inOff,
- byte[] output,
- int outOff)
- {
- int blockSize = GetBlockSize();
- if (_S == null)
- throw new InvalidOperationException("RC6 engine not initialised");
- Check.DataLength(input, inOff, blockSize, "input buffer too short");
- Check.OutputLength(output, outOff, blockSize, "output buffer too short");
- return (forEncryption)
- ? EncryptBlock(input, inOff, output, outOff)
- : DecryptBlock(input, inOff, output, outOff);
- }
- public virtual void Reset()
- {
- }
-
- private void SetKey(
- byte[] key)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
- int c = (key.Length + (bytesPerWord - 1)) / bytesPerWord;
- if (c == 0)
- {
- c = 1;
- }
- int[] L = new int[(key.Length + bytesPerWord - 1) / bytesPerWord];
-
- for (int i = key.Length - 1; i >= 0; i--)
- {
- L[i / bytesPerWord] = (L[i / bytesPerWord] << 8) + (key[i] & 0xff);
- }
-
-
-
-
-
-
-
- _S = new int[2+2*_noRounds+2];
- _S[0] = P32;
- for (int i=1; i < _S.Length; i++)
- {
- _S[i] = (_S[i-1] + Q32);
- }
-
-
-
-
-
- int iter;
- if (L.Length > _S.Length)
- {
- iter = 3 * L.Length;
- }
- else
- {
- iter = 3 * _S.Length;
- }
- int A = 0;
- int B = 0;
- int ii = 0, jj = 0;
- for (int k = 0; k < iter; k++)
- {
- A = _S[ii] = RotateLeft(_S[ii] + A + B, 3);
- B = L[jj] = RotateLeft( L[jj] + A + B, A+B);
- ii = (ii+1) % _S.Length;
- jj = (jj+1) % L.Length;
- }
- }
- private int EncryptBlock(
- byte[] input,
- int inOff,
- byte[] outBytes,
- int outOff)
- {
-
- int A = BytesToWord(input, inOff);
- int B = BytesToWord(input, inOff + bytesPerWord);
- int C = BytesToWord(input, inOff + bytesPerWord*2);
- int D = BytesToWord(input, inOff + bytesPerWord*3);
-
- B += _S[0];
- D += _S[1];
-
- for (int i = 1; i <= _noRounds; i++)
- {
- int t = 0,u = 0;
- t = B*(2*B+1);
- t = RotateLeft(t,5);
- u = D*(2*D+1);
- u = RotateLeft(u,5);
- A ^= t;
- A = RotateLeft(A,u);
- A += _S[2*i];
- C ^= u;
- C = RotateLeft(C,t);
- C += _S[2*i+1];
- int temp = A;
- A = B;
- B = C;
- C = D;
- D = temp;
- }
-
- A += _S[2*_noRounds+2];
- C += _S[2*_noRounds+3];
-
- WordToBytes(A, outBytes, outOff);
- WordToBytes(B, outBytes, outOff + bytesPerWord);
- WordToBytes(C, outBytes, outOff + bytesPerWord*2);
- WordToBytes(D, outBytes, outOff + bytesPerWord*3);
- return 4 * bytesPerWord;
- }
- private int DecryptBlock(
- byte[] input,
- int inOff,
- byte[] outBytes,
- int outOff)
- {
-
- int A = BytesToWord(input, inOff);
- int B = BytesToWord(input, inOff + bytesPerWord);
- int C = BytesToWord(input, inOff + bytesPerWord*2);
- int D = BytesToWord(input, inOff + bytesPerWord*3);
-
- C -= _S[2*_noRounds+3];
- A -= _S[2*_noRounds+2];
-
- for (int i = _noRounds; i >= 1; i--)
- {
- int t=0,u = 0;
- int temp = D;
- D = C;
- C = B;
- B = A;
- A = temp;
- t = B*(2*B+1);
- t = RotateLeft(t, LGW);
- u = D*(2*D+1);
- u = RotateLeft(u, LGW);
- C -= _S[2*i+1];
- C = RotateRight(C,t);
- C ^= u;
- A -= _S[2*i];
- A = RotateRight(A,u);
- A ^= t;
- }
-
- D -= _S[1];
- B -= _S[0];
- WordToBytes(A, outBytes, outOff);
- WordToBytes(B, outBytes, outOff + bytesPerWord);
- WordToBytes(C, outBytes, outOff + bytesPerWord*2);
- WordToBytes(D, outBytes, outOff + bytesPerWord*3);
- return 4 * bytesPerWord;
- }
-
-
-
-
-
-
- private int RotateLeft(int x, int y)
- {
- return ((int)((uint)(x << (y & (wordSize-1)))
- | ((uint) x >> (wordSize - (y & (wordSize-1))))));
- }
-
- private int RotateRight(int x, int y)
- {
- return ((int)(((uint) x >> (y & (wordSize-1)))
- | (uint)(x << (wordSize - (y & (wordSize-1))))));
- }
- private int BytesToWord(
- byte[] src,
- int srcOff)
- {
- int word = 0;
- for (int i = bytesPerWord - 1; i >= 0; i--)
- {
- word = (word << 8) + (src[i + srcOff] & 0xff);
- }
- return word;
- }
- private void WordToBytes(
- int word,
- byte[] dst,
- int dstOff)
- {
- for (int i = 0; i < bytesPerWord; i++)
- {
- dst[i + dstOff] = (byte)word;
- word = (int) ((uint) word >> 8);
- }
- }
- }
- }
- #endif
|