RC564Engine.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * The specification for RC5 came from the <code>RC5 Encryption Algorithm</code>
  11. * publication in RSA CryptoBytes, Spring of 1995.
  12. * <em>http://www.rsasecurity.com/rsalabs/cryptobytes</em>.
  13. * <p>
  14. * This implementation is set to work with a 64 bit word size.</p>
  15. */
  16. public class RC564Engine
  17. : IBlockCipher
  18. {
  19. /*
  20. * the number of rounds to perform
  21. */
  22. private int _noRounds;
  23. /*
  24. * the expanded key array of size 2*(rounds + 1)
  25. */
  26. private long [] _S;
  27. /*
  28. * our "magic constants" for wordSize 62
  29. *
  30. * Pw = Odd((e-2) * 2^wordsize)
  31. * Qw = Odd((o-2) * 2^wordsize)
  32. *
  33. * where e is the base of natural logarithms (2.718281828...)
  34. * and o is the golden ratio (1.61803398...)
  35. */
  36. private static readonly long P64 = unchecked( (long) 0xb7e151628aed2a6bL);
  37. private static readonly long Q64 = unchecked( (long) 0x9e3779b97f4a7c15L);
  38. private bool forEncryption;
  39. /**
  40. * Create an instance of the RC5 encryption algorithm
  41. * and set some defaults
  42. */
  43. public RC564Engine()
  44. {
  45. _noRounds = 12;
  46. // _S = null;
  47. }
  48. public virtual string AlgorithmName
  49. {
  50. get { return "RC5-64"; }
  51. }
  52. public virtual int GetBlockSize()
  53. {
  54. return 16;
  55. }
  56. /**
  57. * initialise a RC5-64 cipher.
  58. *
  59. * @param forEncryption whether or not we are for encryption.
  60. * @param parameters the parameters required to set up the cipher.
  61. * @exception ArgumentException if the parameters argument is
  62. * inappropriate.
  63. */
  64. public virtual void Init(bool forEncryption, ICipherParameters parameters)
  65. {
  66. if (!(parameters is RC5Parameters rc5Parameters))
  67. throw new ArgumentException("invalid parameter passed to RC564 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  68. this.forEncryption = forEncryption;
  69. _noRounds = rc5Parameters.Rounds;
  70. SetKey(rc5Parameters.GetKey());
  71. }
  72. public virtual int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  73. {
  74. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  75. return forEncryption
  76. ? EncryptBlock(input.AsSpan(inOff), output.AsSpan(outOff))
  77. : DecryptBlock(input.AsSpan(inOff), output.AsSpan(outOff));
  78. #else
  79. return forEncryption
  80. ? EncryptBlock(input, inOff, output, outOff)
  81. : DecryptBlock(input, inOff, output, outOff);
  82. #endif
  83. }
  84. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  85. public virtual int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  86. {
  87. return forEncryption
  88. ? EncryptBlock(input, output)
  89. : DecryptBlock(input, output);
  90. }
  91. #endif
  92. /**
  93. * Re-key the cipher.
  94. *
  95. * @param key the key to be used
  96. */
  97. private void SetKey(byte[] key)
  98. {
  99. //
  100. // KEY EXPANSION:
  101. //
  102. // There are 3 phases to the key expansion.
  103. //
  104. // Phase 1:
  105. // Copy the secret key K[0...b-1] into an array L[0..c-1] of
  106. // c = ceil(b/u), where u = wordSize/8 in little-endian order.
  107. // In other words, we fill up L using u consecutive key bytes
  108. // of K. Any unfilled byte positions in L are zeroed. In the
  109. // case that b = c = 0, set c = 1 and L[0] = 0.
  110. //
  111. long[] L = new long[(key.Length + 7) / 8];
  112. for (int i = 0; i != key.Length; i++)
  113. {
  114. L[i / 8] += (long)(key[i] & 0xff) << (8 * (i % 8));
  115. }
  116. //
  117. // Phase 2:
  118. // Initialize S to a particular fixed pseudo-random bit pattern
  119. // using an arithmetic progression modulo 2^wordsize determined
  120. // by the magic numbers, Pw & Qw.
  121. //
  122. _S = new long[2*(_noRounds + 1)];
  123. _S[0] = P64;
  124. for (int i=1; i < _S.Length; i++)
  125. {
  126. _S[i] = (_S[i-1] + Q64);
  127. }
  128. //
  129. // Phase 3:
  130. // Mix in the user's secret key in 3 passes over the arrays S & L.
  131. // The max of the arrays sizes is used as the loop control
  132. //
  133. int iter;
  134. if (L.Length > _S.Length)
  135. {
  136. iter = 3 * L.Length;
  137. }
  138. else
  139. {
  140. iter = 3 * _S.Length;
  141. }
  142. long A = 0, B = 0;
  143. int ii = 0, jj = 0;
  144. for (int k = 0; k < iter; k++)
  145. {
  146. A = _S[ii] = Longs.RotateLeft(_S[ii] + A + B, 3);
  147. B = L[jj] = Longs.RotateLeft(L[jj] + A + B, (int)(A + B));
  148. ii = (ii+1) % _S.Length;
  149. jj = (jj+1) % L.Length;
  150. }
  151. }
  152. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  153. private int EncryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
  154. {
  155. long A = (long)Pack.LE_To_UInt64(input) + _S[0];
  156. long B = (long)Pack.LE_To_UInt64(input[8..]) + _S[1];
  157. for (int i = 1; i <= _noRounds; i++)
  158. {
  159. A = Longs.RotateLeft(A ^ B, (int)B) + _S[2*i];
  160. B = Longs.RotateLeft(B ^ A, (int)A) + _S[2*i+1];
  161. }
  162. Pack.UInt64_To_LE((ulong)A, output);
  163. Pack.UInt64_To_LE((ulong)B, output[8..]);
  164. return 16;
  165. }
  166. private int DecryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
  167. {
  168. long A = (long)Pack.LE_To_UInt64(input);
  169. long B = (long)Pack.LE_To_UInt64(input[8..]);
  170. for (int i = _noRounds; i >= 1; i--)
  171. {
  172. B = Longs.RotateRight(B - _S[2*i+1], (int)A) ^ A;
  173. A = Longs.RotateRight(A - _S[2*i], (int)B) ^ B;
  174. }
  175. Pack.UInt64_To_LE((ulong)(A - _S[0]), output);
  176. Pack.UInt64_To_LE((ulong)(B - _S[1]), output[8..]);
  177. return 16;
  178. }
  179. #else
  180. private int EncryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  181. {
  182. long A = (long)Pack.LE_To_UInt64(input, inOff) + _S[0];
  183. long B = (long)Pack.LE_To_UInt64(input, inOff + 8) + _S[1];
  184. for (int i = 1; i <= _noRounds; i++)
  185. {
  186. A = Longs.RotateLeft(A ^ B, (int)B) + _S[2*i];
  187. B = Longs.RotateLeft(B ^ A, (int)A) + _S[2*i+1];
  188. }
  189. Pack.UInt64_To_LE((ulong)A, outBytes, outOff);
  190. Pack.UInt64_To_LE((ulong)B, outBytes, outOff + 8);
  191. return 16;
  192. }
  193. private int DecryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  194. {
  195. long A = (long)Pack.LE_To_UInt64(input, inOff);
  196. long B = (long)Pack.LE_To_UInt64(input, inOff + 8);
  197. for (int i = _noRounds; i >= 1; i--)
  198. {
  199. B = Longs.RotateRight(B - _S[2*i+1], (int)A) ^ A;
  200. A = Longs.RotateRight(A - _S[2*i], (int)B) ^ B;
  201. }
  202. Pack.UInt64_To_LE((ulong)(A - _S[0]), outBytes, outOff);
  203. Pack.UInt64_To_LE((ulong)(B - _S[1]), outBytes, outOff + 8);
  204. return 16;
  205. }
  206. #endif
  207. }
  208. }
  209. #pragma warning restore
  210. #endif