RC6Engine.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  8. {
  9. /**
  10. * An RC6 engine.
  11. */
  12. public class RC6Engine
  13. : IBlockCipher
  14. {
  15. /*
  16. * the number of rounds to perform
  17. */
  18. private static readonly int _noRounds = 20;
  19. /*
  20. * the expanded key array of size 2*(rounds + 1)
  21. */
  22. private int [] _S;
  23. /*
  24. * our "magic constants" for wordSize 32
  25. *
  26. * Pw = Odd((e-2) * 2^wordsize)
  27. * Qw = Odd((o-2) * 2^wordsize)
  28. *
  29. * where e is the base of natural logarithms (2.718281828...)
  30. * and o is the golden ratio (1.61803398...)
  31. */
  32. private static readonly int P32 = unchecked((int) 0xb7e15163);
  33. private static readonly int Q32 = unchecked((int) 0x9e3779b9);
  34. private static readonly int LGW = 5; // log2(32)
  35. private bool forEncryption;
  36. /**
  37. * Create an instance of the RC6 encryption algorithm
  38. * and set some defaults
  39. */
  40. public RC6Engine()
  41. {
  42. }
  43. public virtual string AlgorithmName
  44. {
  45. get { return "RC6"; }
  46. }
  47. public virtual int GetBlockSize()
  48. {
  49. return 16;
  50. }
  51. /**
  52. * initialise a RC5-32 cipher.
  53. *
  54. * @param forEncryption whether or not we are for encryption.
  55. * @param parameters the parameters required to set up the cipher.
  56. * @exception ArgumentException if the parameters argument is
  57. * inappropriate.
  58. */
  59. public virtual void Init(bool forEncryption, ICipherParameters parameters)
  60. {
  61. if (!(parameters is KeyParameter keyParameter))
  62. throw new ArgumentException("invalid parameter passed to RC6 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  63. this.forEncryption = forEncryption;
  64. SetKey(keyParameter.GetKey());
  65. }
  66. public virtual int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  67. {
  68. if (_S == null)
  69. throw new InvalidOperationException("RC6 engine not initialised");
  70. int blockSize = GetBlockSize();
  71. Check.DataLength(input, inOff, blockSize, "input buffer too short");
  72. Check.OutputLength(output, outOff, blockSize, "output buffer too short");
  73. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  74. return forEncryption
  75. ? EncryptBlock(input.AsSpan(inOff), output.AsSpan(outOff))
  76. : DecryptBlock(input.AsSpan(inOff), output.AsSpan(outOff));
  77. #else
  78. return forEncryption
  79. ? EncryptBlock(input, inOff, output, outOff)
  80. : DecryptBlock(input, inOff, output, outOff);
  81. #endif
  82. }
  83. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  84. public virtual int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  85. {
  86. if (_S == null)
  87. throw new InvalidOperationException("RC6 engine not initialised");
  88. int blockSize = GetBlockSize();
  89. Check.DataLength(input, blockSize, "input buffer too short");
  90. Check.OutputLength(output, blockSize, "output buffer too short");
  91. return forEncryption
  92. ? EncryptBlock(input, output)
  93. : DecryptBlock(input, output);
  94. }
  95. #endif
  96. /**
  97. * Re-key the cipher.
  98. *
  99. * @param inKey the key to be used
  100. */
  101. private void SetKey(
  102. byte[] key)
  103. {
  104. //
  105. // KEY EXPANSION:
  106. //
  107. // There are 3 phases to the key expansion.
  108. //
  109. // Phase 1:
  110. // Copy the secret key K[0...b-1] into an array L[0..c-1] of
  111. // c = ceil(b/u), where u = wordSize/8 in little-endian order.
  112. // In other words, we fill up L using u consecutive key bytes
  113. // of K. Any unfilled byte positions in L are zeroed. In the
  114. // case that b = c = 0, set c = 1 and L[0] = 0.
  115. //
  116. // compute number of dwords
  117. int c = (key.Length + 3) / 4;
  118. if (c == 0)
  119. {
  120. c = 1;
  121. }
  122. int[] L = new int[(key.Length + 3) / 4];
  123. // load all key bytes into array of key dwords
  124. for (int i = key.Length - 1; i >= 0; i--)
  125. {
  126. L[i / 4] = (L[i / 4] << 8) + (key[i] & 0xff);
  127. }
  128. //
  129. // Phase 2:
  130. // Key schedule is placed in a array of 2+2*ROUNDS+2 = 44 dwords.
  131. // Initialize S to a particular fixed pseudo-random bit pattern
  132. // using an arithmetic progression modulo 2^wordsize determined
  133. // by the magic numbers, Pw & Qw.
  134. //
  135. _S = new int[2+2*_noRounds+2];
  136. _S[0] = P32;
  137. for (int i=1; i < _S.Length; i++)
  138. {
  139. _S[i] = (_S[i-1] + Q32);
  140. }
  141. //
  142. // Phase 3:
  143. // Mix in the user's secret key in 3 passes over the arrays S & L.
  144. // The max of the arrays sizes is used as the loop control
  145. //
  146. int iter;
  147. if (L.Length > _S.Length)
  148. {
  149. iter = 3 * L.Length;
  150. }
  151. else
  152. {
  153. iter = 3 * _S.Length;
  154. }
  155. int A = 0;
  156. int B = 0;
  157. int ii = 0, jj = 0;
  158. for (int k = 0; k < iter; k++)
  159. {
  160. A = _S[ii] = Integers.RotateLeft(_S[ii] + A + B, 3);
  161. B = L[jj] = Integers.RotateLeft( L[jj] + A + B, A + B);
  162. ii = (ii+1) % _S.Length;
  163. jj = (jj+1) % L.Length;
  164. }
  165. }
  166. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  167. private int EncryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
  168. {
  169. // load A,B,C and D registers from in.
  170. int A = (int)Pack.LE_To_UInt32(input);
  171. int B = (int)Pack.LE_To_UInt32(input[4..]);
  172. int C = (int)Pack.LE_To_UInt32(input[8..]);
  173. int D = (int)Pack.LE_To_UInt32(input[12..]);
  174. // Do pseudo-round #0: pre-whitening of B and D
  175. B += _S[0];
  176. D += _S[1];
  177. // perform round #1,#2 ... #ROUNDS of encryption
  178. for (int i = 1; i <= _noRounds; i++)
  179. {
  180. int t = 0, u = 0;
  181. t = B * (2 * B + 1);
  182. t = Integers.RotateLeft(t, 5);
  183. u = D * (2 * D + 1);
  184. u = Integers.RotateLeft(u, 5);
  185. A ^= t;
  186. A = Integers.RotateLeft(A, u);
  187. A += _S[2 * i];
  188. C ^= u;
  189. C = Integers.RotateLeft(C, t);
  190. C += _S[2 * i + 1];
  191. int temp = A;
  192. A = B;
  193. B = C;
  194. C = D;
  195. D = temp;
  196. }
  197. // do pseudo-round #(ROUNDS+1) : post-whitening of A and C
  198. A += _S[2 * _noRounds + 2];
  199. C += _S[2 * _noRounds + 3];
  200. // store A, B, C and D registers to out
  201. Pack.UInt32_To_LE((uint)A, output);
  202. Pack.UInt32_To_LE((uint)B, output[4..]);
  203. Pack.UInt32_To_LE((uint)C, output[8..]);
  204. Pack.UInt32_To_LE((uint)D, output[12..]);
  205. return 16;
  206. }
  207. private int DecryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
  208. {
  209. // load A,B,C and D registers from out.
  210. int A = (int)Pack.LE_To_UInt32(input);
  211. int B = (int)Pack.LE_To_UInt32(input[4..]);
  212. int C = (int)Pack.LE_To_UInt32(input[8..]);
  213. int D = (int)Pack.LE_To_UInt32(input[12..]);
  214. // Undo pseudo-round #(ROUNDS+1) : post whitening of A and C
  215. C -= _S[2 * _noRounds + 3];
  216. A -= _S[2 * _noRounds + 2];
  217. // Undo round #ROUNDS, .., #2,#1 of encryption
  218. for (int i = _noRounds; i >= 1; i--)
  219. {
  220. int t = 0, u = 0;
  221. int temp = D;
  222. D = C;
  223. C = B;
  224. B = A;
  225. A = temp;
  226. t = B * (2 * B + 1);
  227. t = Integers.RotateLeft(t, LGW);
  228. u = D * (2 * D + 1);
  229. u = Integers.RotateLeft(u, LGW);
  230. C -= _S[2 * i + 1];
  231. C = Integers.RotateRight(C, t);
  232. C ^= u;
  233. A -= _S[2 * i];
  234. A = Integers.RotateRight(A, u);
  235. A ^= t;
  236. }
  237. // Undo pseudo-round #0: pre-whitening of B and D
  238. D -= _S[1];
  239. B -= _S[0];
  240. Pack.UInt32_To_LE((uint)A, output);
  241. Pack.UInt32_To_LE((uint)B, output[4..]);
  242. Pack.UInt32_To_LE((uint)C, output[8..]);
  243. Pack.UInt32_To_LE((uint)D, output[12..]);
  244. return 16;
  245. }
  246. #else
  247. private int EncryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  248. {
  249. // load A,B,C and D registers from in.
  250. int A = (int)Pack.LE_To_UInt32(input, inOff);
  251. int B = (int)Pack.LE_To_UInt32(input, inOff + 4);
  252. int C = (int)Pack.LE_To_UInt32(input, inOff + 8);
  253. int D = (int)Pack.LE_To_UInt32(input, inOff + 12);
  254. // Do pseudo-round #0: pre-whitening of B and D
  255. B += _S[0];
  256. D += _S[1];
  257. // perform round #1,#2 ... #ROUNDS of encryption
  258. for (int i = 1; i <= _noRounds; i++)
  259. {
  260. int t = 0,u = 0;
  261. t = B*(2*B+1);
  262. t = Integers.RotateLeft(t,5);
  263. u = D*(2*D+1);
  264. u = Integers.RotateLeft(u,5);
  265. A ^= t;
  266. A = Integers.RotateLeft(A,u);
  267. A += _S[2*i];
  268. C ^= u;
  269. C = Integers.RotateLeft(C,t);
  270. C += _S[2*i+1];
  271. int temp = A;
  272. A = B;
  273. B = C;
  274. C = D;
  275. D = temp;
  276. }
  277. // do pseudo-round #(ROUNDS+1) : post-whitening of A and C
  278. A += _S[2*_noRounds+2];
  279. C += _S[2*_noRounds+3];
  280. // store A, B, C and D registers to out
  281. Pack.UInt32_To_LE((uint)A, outBytes, outOff);
  282. Pack.UInt32_To_LE((uint)B, outBytes, outOff + 4);
  283. Pack.UInt32_To_LE((uint)C, outBytes, outOff + 8);
  284. Pack.UInt32_To_LE((uint)D, outBytes, outOff + 12);
  285. return 16;
  286. }
  287. private int DecryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  288. {
  289. // load A,B,C and D registers from out.
  290. int A = (int)Pack.LE_To_UInt32(input, inOff);
  291. int B = (int)Pack.LE_To_UInt32(input, inOff + 4);
  292. int C = (int)Pack.LE_To_UInt32(input, inOff + 8);
  293. int D = (int)Pack.LE_To_UInt32(input, inOff + 12);
  294. // Undo pseudo-round #(ROUNDS+1) : post whitening of A and C
  295. C -= _S[2*_noRounds+3];
  296. A -= _S[2*_noRounds+2];
  297. // Undo round #ROUNDS, .., #2,#1 of encryption
  298. for (int i = _noRounds; i >= 1; i--)
  299. {
  300. int t=0,u = 0;
  301. int temp = D;
  302. D = C;
  303. C = B;
  304. B = A;
  305. A = temp;
  306. t = B*(2*B+1);
  307. t = Integers.RotateLeft(t, LGW);
  308. u = D*(2*D+1);
  309. u = Integers.RotateLeft(u, LGW);
  310. C -= _S[2*i+1];
  311. C = Integers.RotateRight(C,t);
  312. C ^= u;
  313. A -= _S[2*i];
  314. A = Integers.RotateRight(A,u);
  315. A ^= t;
  316. }
  317. // Undo pseudo-round #0: pre-whitening of B and D
  318. D -= _S[1];
  319. B -= _S[0];
  320. Pack.UInt32_To_LE((uint)A, outBytes, outOff);
  321. Pack.UInt32_To_LE((uint)B, outBytes, outOff + 4);
  322. Pack.UInt32_To_LE((uint)C, outBytes, outOff + 8);
  323. Pack.UInt32_To_LE((uint)D, outBytes, outOff + 12);
  324. return 16;
  325. }
  326. #endif
  327. }
  328. }
  329. #pragma warning restore
  330. #endif