NoekeonEngine.cs 5.2 KB

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