TEAEngine.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  8. {
  9. /**
  10. * An TEA engine.
  11. */
  12. public class TeaEngine
  13. : IBlockCipher
  14. {
  15. private const int
  16. rounds = 32,
  17. block_size = 8;
  18. // key_size = 16,
  19. private const uint
  20. delta = 0x9E3779B9,
  21. d_sum = 0xC6EF3720; // sum on decrypt
  22. /*
  23. * the expanded key array of 4 subkeys
  24. */
  25. private uint _a, _b, _c, _d;
  26. private bool _initialised;
  27. private bool _forEncryption;
  28. /**
  29. * Create an instance of the TEA encryption algorithm
  30. * and set some defaults
  31. */
  32. public TeaEngine()
  33. {
  34. _initialised = false;
  35. }
  36. public virtual string AlgorithmName
  37. {
  38. get { return "TEA"; }
  39. }
  40. public virtual bool IsPartialBlockOkay
  41. {
  42. get { return false; }
  43. }
  44. public virtual int GetBlockSize()
  45. {
  46. return block_size;
  47. }
  48. /**
  49. * initialise
  50. *
  51. * @param forEncryption whether or not we are for encryption.
  52. * @param params the parameters required to set up the cipher.
  53. * @exception ArgumentException if the params argument is
  54. * inappropriate.
  55. */
  56. public virtual void Init(
  57. bool forEncryption,
  58. ICipherParameters parameters)
  59. {
  60. if (!(parameters is KeyParameter))
  61. {
  62. throw new ArgumentException("invalid parameter passed to TEA init - "
  63. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  64. }
  65. _forEncryption = forEncryption;
  66. _initialised = true;
  67. KeyParameter p = (KeyParameter) parameters;
  68. setKey(p.GetKey());
  69. }
  70. public virtual int ProcessBlock(
  71. byte[] inBytes,
  72. int inOff,
  73. byte[] outBytes,
  74. int outOff)
  75. {
  76. if (!_initialised)
  77. throw new InvalidOperationException(AlgorithmName + " not initialised");
  78. Check.DataLength(inBytes, inOff, block_size, "input buffer too short");
  79. Check.OutputLength(outBytes, outOff, block_size, "output buffer too short");
  80. return _forEncryption
  81. ? encryptBlock(inBytes, inOff, outBytes, outOff)
  82. : decryptBlock(inBytes, inOff, outBytes, outOff);
  83. }
  84. public virtual void Reset()
  85. {
  86. }
  87. /**
  88. * Re-key the cipher.
  89. *
  90. * @param key the key to be used
  91. */
  92. private void setKey(
  93. byte[] key)
  94. {
  95. _a = Pack.BE_To_UInt32(key, 0);
  96. _b = Pack.BE_To_UInt32(key, 4);
  97. _c = Pack.BE_To_UInt32(key, 8);
  98. _d = Pack.BE_To_UInt32(key, 12);
  99. }
  100. private int encryptBlock(
  101. byte[] inBytes,
  102. int inOff,
  103. byte[] outBytes,
  104. int outOff)
  105. {
  106. // Pack bytes into integers
  107. uint v0 = Pack.BE_To_UInt32(inBytes, inOff);
  108. uint v1 = Pack.BE_To_UInt32(inBytes, inOff + 4);
  109. uint sum = 0;
  110. for (int i = 0; i != rounds; i++)
  111. {
  112. sum += delta;
  113. v0 += ((v1 << 4) + _a) ^ (v1 + sum) ^ ((v1 >> 5) + _b);
  114. v1 += ((v0 << 4) + _c) ^ (v0 + sum) ^ ((v0 >> 5) + _d);
  115. }
  116. Pack.UInt32_To_BE(v0, outBytes, outOff);
  117. Pack.UInt32_To_BE(v1, outBytes, outOff + 4);
  118. return block_size;
  119. }
  120. private int decryptBlock(
  121. byte[] inBytes,
  122. int inOff,
  123. byte[] outBytes,
  124. int outOff)
  125. {
  126. // Pack bytes into integers
  127. uint v0 = Pack.BE_To_UInt32(inBytes, inOff);
  128. uint v1 = Pack.BE_To_UInt32(inBytes, inOff + 4);
  129. uint sum = d_sum;
  130. for (int i = 0; i != rounds; i++)
  131. {
  132. v1 -= ((v0 << 4) + _c) ^ (v0 + sum) ^ ((v0 >> 5) + _d);
  133. v0 -= ((v1 << 4) + _a) ^ (v1 + sum) ^ ((v1 >> 5) + _b);
  134. sum -= delta;
  135. }
  136. Pack.UInt32_To_BE(v0, outBytes, outOff);
  137. Pack.UInt32_To_BE(v1, outBytes, outOff + 4);
  138. return block_size;
  139. }
  140. }
  141. }
  142. #pragma warning restore
  143. #endif