NoekeonEngine.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * A Noekeon engine, using direct-key mode.
  11. */
  12. public class NoekeonEngine
  13. : IBlockCipher
  14. {
  15. // Block and key size, as well as the amount of rounds.
  16. private const int Size = 16;
  17. private static readonly byte[] RoundConstants = { 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e,
  18. 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4 };
  19. private readonly uint[] k = new uint[4];
  20. private bool _initialised, _forEncryption;
  21. /**
  22. * Create an instance of the Noekeon encryption algorithm
  23. * and set some defaults
  24. */
  25. public NoekeonEngine()
  26. {
  27. _initialised = false;
  28. }
  29. public virtual string AlgorithmName
  30. {
  31. get { return "Noekeon"; }
  32. }
  33. public virtual bool IsPartialBlockOkay
  34. {
  35. get { return false; }
  36. }
  37. public virtual int GetBlockSize()
  38. {
  39. return Size;
  40. }
  41. /**
  42. * initialise
  43. *
  44. * @param forEncryption whether or not we are for encryption.
  45. * @param params the parameters required to set up the cipher.
  46. * @exception ArgumentException if the params argument is
  47. * inappropriate.
  48. */
  49. public virtual void Init(bool forEncryption, ICipherParameters parameters)
  50. {
  51. if (!(parameters is KeyParameter))
  52. throw new ArgumentException("Invalid parameters passed to Noekeon init - "
  53. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters), "parameters");
  54. KeyParameter p = (KeyParameter) parameters;
  55. byte[] key = p.GetKey();
  56. if (key.Length != 16)
  57. throw new ArgumentException("Key length not 128 bits.");
  58. Pack.BE_To_UInt32(key, 0, k, 0, 4);
  59. if (!forEncryption)
  60. {
  61. // theta(k, new uint[]{ 0x00, 0x00, 0x00, 0x00 });
  62. {
  63. uint a0 = k[0], a1 = k[1], a2 = k[2], a3 = k[3];
  64. uint t02 = a0 ^ a2;
  65. t02 ^= Integers.RotateLeft(t02, 8) ^ Integers.RotateLeft(t02, 24);
  66. uint t13 = a1 ^ a3;
  67. t13 ^= Integers.RotateLeft(t13, 8) ^ Integers.RotateLeft(t13, 24);
  68. a0 ^= t13;
  69. a1 ^= t02;
  70. a2 ^= t13;
  71. a3 ^= t02;
  72. k[0] = a0; k[1] = a1; k[2] = a2; k[3] = a3;
  73. }
  74. }
  75. this._forEncryption = forEncryption;
  76. this._initialised = true;
  77. }
  78. public virtual int ProcessBlock(
  79. byte[] input,
  80. int inOff,
  81. byte[] output,
  82. int outOff)
  83. {
  84. if (!_initialised)
  85. throw new InvalidOperationException(AlgorithmName + " not initialised");
  86. Check.DataLength(input, inOff, Size, "input buffer too short");
  87. Check.OutputLength(output, outOff, Size, "output buffer too short");
  88. return _forEncryption
  89. ? EncryptBlock(input, inOff, output, outOff)
  90. : DecryptBlock(input, inOff, output, outOff);
  91. }
  92. public virtual void Reset()
  93. {
  94. }
  95. private int EncryptBlock(byte[] input, int inOff, byte[] output, int outOff)
  96. {
  97. uint a0 = Pack.BE_To_UInt32(input, inOff);
  98. uint a1 = Pack.BE_To_UInt32(input, inOff + 4);
  99. uint a2 = Pack.BE_To_UInt32(input, inOff + 8);
  100. uint a3 = Pack.BE_To_UInt32(input, inOff + 12);
  101. uint k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];
  102. int round = 0;
  103. for (;;)
  104. {
  105. a0 ^= RoundConstants[round];
  106. // theta(a, k);
  107. {
  108. uint t02 = a0 ^ a2;
  109. t02 ^= Integers.RotateLeft(t02, 8) ^ Integers.RotateLeft(t02, 24);
  110. a0 ^= k0;
  111. a1 ^= k1;
  112. a2 ^= k2;
  113. a3 ^= k3;
  114. uint t13 = a1 ^ a3;
  115. t13 ^= Integers.RotateLeft(t13, 8) ^ Integers.RotateLeft(t13, 24);
  116. a0 ^= t13;
  117. a1 ^= t02;
  118. a2 ^= t13;
  119. a3 ^= t02;
  120. }
  121. if (++round > Size)
  122. break;
  123. // pi1(a);
  124. {
  125. a1 = Integers.RotateLeft(a1, 1);
  126. a2 = Integers.RotateLeft(a2, 5);
  127. a3 = Integers.RotateLeft(a3, 2);
  128. }
  129. // gamma(a);
  130. {
  131. uint t = a3;
  132. a1 ^= a3 | a2;
  133. a3 = a0 ^ (a2 & ~a1);
  134. a2 = t ^ ~a1 ^ a2 ^ a3;
  135. a1 ^= a3 | a2;
  136. a0 = t ^ (a2 & a1);
  137. }
  138. // pi2(a);
  139. {
  140. a1 = Integers.RotateLeft(a1, 31);
  141. a2 = Integers.RotateLeft(a2, 27);
  142. a3 = Integers.RotateLeft(a3, 30);
  143. }
  144. }
  145. Pack.UInt32_To_BE(a0, output, outOff);
  146. Pack.UInt32_To_BE(a1, output, outOff + 4);
  147. Pack.UInt32_To_BE(a2, output, outOff + 8);
  148. Pack.UInt32_To_BE(a3, output, outOff + 12);
  149. return Size;
  150. }
  151. private int DecryptBlock(byte[] input, int inOff, byte[] output, int outOff)
  152. {
  153. uint a0 = Pack.BE_To_UInt32(input, inOff);
  154. uint a1 = Pack.BE_To_UInt32(input, inOff + 4);
  155. uint a2 = Pack.BE_To_UInt32(input, inOff + 8);
  156. uint a3 = Pack.BE_To_UInt32(input, inOff + 12);
  157. uint k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];
  158. int round = Size;
  159. for (;;)
  160. {
  161. // theta(a, k);
  162. {
  163. uint t02 = a0 ^ a2;
  164. t02 ^= Integers.RotateLeft(t02, 8) ^ Integers.RotateLeft(t02, 24);
  165. a0 ^= k0;
  166. a1 ^= k1;
  167. a2 ^= k2;
  168. a3 ^= k3;
  169. uint t13 = a1 ^ a3;
  170. t13 ^= Integers.RotateLeft(t13, 8) ^ Integers.RotateLeft(t13, 24);
  171. a0 ^= t13;
  172. a1 ^= t02;
  173. a2 ^= t13;
  174. a3 ^= t02;
  175. }
  176. a0 ^= RoundConstants[round];
  177. if (--round < 0)
  178. break;
  179. // pi1(a);
  180. {
  181. a1 = Integers.RotateLeft(a1, 1);
  182. a2 = Integers.RotateLeft(a2, 5);
  183. a3 = Integers.RotateLeft(a3, 2);
  184. }
  185. // gamma(a);
  186. {
  187. uint t = a3;
  188. a1 ^= a3 | a2;
  189. a3 = a0 ^ (a2 & ~a1);
  190. a2 = t ^ ~a1 ^ a2 ^ a3;
  191. a1 ^= a3 | a2;
  192. a0 = t ^ (a2 & a1);
  193. }
  194. // pi2(a);
  195. {
  196. a1 = Integers.RotateLeft(a1, 31);
  197. a2 = Integers.RotateLeft(a2, 27);
  198. a3 = Integers.RotateLeft(a3, 30);
  199. }
  200. }
  201. Pack.UInt32_To_BE(a0, output, outOff);
  202. Pack.UInt32_To_BE(a1, output, outOff + 4);
  203. Pack.UInt32_To_BE(a2, output, outOff + 8);
  204. Pack.UInt32_To_BE(a3, output, outOff + 12);
  205. return Size;
  206. }
  207. }
  208. }
  209. #pragma warning restore
  210. #endif