TEAEngine.cs 3.6 KB

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