RC532Engine.cs 8.0 KB

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