ChaChaEngine.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  7. {
  8. /// <summary>
  9. /// Implementation of Daniel J. Bernstein's ChaCha stream cipher.
  10. /// </summary>
  11. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.NullChecks, false)]
  12. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.ArrayBoundsChecks, false)]
  13. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.DivideByZeroChecks, false)]
  14. [BestHTTP.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  15. public sealed class ChaChaEngine
  16. : Salsa20Engine
  17. {
  18. /// <summary>
  19. /// Creates a 20 rounds ChaCha engine.
  20. /// </summary>
  21. public ChaChaEngine()
  22. {
  23. }
  24. /// <summary>
  25. /// Creates a ChaCha engine with a specific number of rounds.
  26. /// </summary>
  27. /// <param name="rounds">the number of rounds (must be an even number).</param>
  28. public ChaChaEngine(int rounds)
  29. : base(rounds)
  30. {
  31. }
  32. public override string AlgorithmName
  33. {
  34. get { return "ChaCha" + rounds; }
  35. }
  36. protected override void AdvanceCounter()
  37. {
  38. if (++engineState[12] == 0)
  39. {
  40. ++engineState[13];
  41. }
  42. }
  43. protected override void ResetCounter()
  44. {
  45. engineState[12] = engineState[13] = 0;
  46. }
  47. protected override void SetKey(byte[] keyBytes, byte[] ivBytes)
  48. {
  49. if (keyBytes != null)
  50. {
  51. if ((keyBytes.Length != 16) && (keyBytes.Length != 32))
  52. throw new ArgumentException(AlgorithmName + " requires 128 bit or 256 bit key");
  53. PackTauOrSigma(keyBytes.Length, engineState, 0);
  54. // Key
  55. Pack.LE_To_UInt32(keyBytes, 0, engineState, 4, 4);
  56. Pack.LE_To_UInt32(keyBytes, keyBytes.Length - 16, engineState, 8, 4);
  57. }
  58. // IV
  59. Pack.LE_To_UInt32(ivBytes, 0, engineState, 14, 2);
  60. }
  61. protected override void GenerateKeyStream(byte[] output)
  62. {
  63. ChachaCore(rounds, engineState, x);
  64. Pack.UInt32_To_LE(x, output, 0);
  65. }
  66. /// <summary>
  67. /// ChaCha function.
  68. /// </summary>
  69. /// <param name="rounds">The number of ChaCha rounds to execute</param>
  70. /// <param name="input">The input words.</param>
  71. /// <param name="x">The ChaCha state to modify.</param>
  72. internal static void ChachaCore(int rounds, uint[] input, uint[] x)
  73. {
  74. if (input.Length != 16)
  75. throw new ArgumentException();
  76. if (x.Length != 16)
  77. throw new ArgumentException();
  78. if (rounds % 2 != 0)
  79. throw new ArgumentException("Number of rounds must be even");
  80. uint x00 = input[ 0];
  81. uint x01 = input[ 1];
  82. uint x02 = input[ 2];
  83. uint x03 = input[ 3];
  84. uint x04 = input[ 4];
  85. uint x05 = input[ 5];
  86. uint x06 = input[ 6];
  87. uint x07 = input[ 7];
  88. uint x08 = input[ 8];
  89. uint x09 = input[ 9];
  90. uint x10 = input[10];
  91. uint x11 = input[11];
  92. uint x12 = input[12];
  93. uint x13 = input[13];
  94. uint x14 = input[14];
  95. uint x15 = input[15];
  96. for (int i = rounds; i > 0; i -= 2)
  97. {
  98. x00 += x04; x12 = Integers.RotateLeft(x12 ^ x00, 16);
  99. x08 += x12; x04 = Integers.RotateLeft(x04 ^ x08, 12);
  100. x00 += x04; x12 = Integers.RotateLeft(x12 ^ x00, 8);
  101. x08 += x12; x04 = Integers.RotateLeft(x04 ^ x08, 7);
  102. x01 += x05; x13 = Integers.RotateLeft(x13 ^ x01, 16);
  103. x09 += x13; x05 = Integers.RotateLeft(x05 ^ x09, 12);
  104. x01 += x05; x13 = Integers.RotateLeft(x13 ^ x01, 8);
  105. x09 += x13; x05 = Integers.RotateLeft(x05 ^ x09, 7);
  106. x02 += x06; x14 = Integers.RotateLeft(x14 ^ x02, 16);
  107. x10 += x14; x06 = Integers.RotateLeft(x06 ^ x10, 12);
  108. x02 += x06; x14 = Integers.RotateLeft(x14 ^ x02, 8);
  109. x10 += x14; x06 = Integers.RotateLeft(x06 ^ x10, 7);
  110. x03 += x07; x15 = Integers.RotateLeft(x15 ^ x03, 16);
  111. x11 += x15; x07 = Integers.RotateLeft(x07 ^ x11, 12);
  112. x03 += x07; x15 = Integers.RotateLeft(x15 ^ x03, 8);
  113. x11 += x15; x07 = Integers.RotateLeft(x07 ^ x11, 7);
  114. x00 += x05; x15 = Integers.RotateLeft(x15 ^ x00, 16);
  115. x10 += x15; x05 = Integers.RotateLeft(x05 ^ x10, 12);
  116. x00 += x05; x15 = Integers.RotateLeft(x15 ^ x00, 8);
  117. x10 += x15; x05 = Integers.RotateLeft(x05 ^ x10, 7);
  118. x01 += x06; x12 = Integers.RotateLeft(x12 ^ x01, 16);
  119. x11 += x12; x06 = Integers.RotateLeft(x06 ^ x11, 12);
  120. x01 += x06; x12 = Integers.RotateLeft(x12 ^ x01, 8);
  121. x11 += x12; x06 = Integers.RotateLeft(x06 ^ x11, 7);
  122. x02 += x07; x13 = Integers.RotateLeft(x13 ^ x02, 16);
  123. x08 += x13; x07 = Integers.RotateLeft(x07 ^ x08, 12);
  124. x02 += x07; x13 = Integers.RotateLeft(x13 ^ x02, 8);
  125. x08 += x13; x07 = Integers.RotateLeft(x07 ^ x08, 7);
  126. x03 += x04; x14 = Integers.RotateLeft(x14 ^ x03, 16);
  127. x09 += x14; x04 = Integers.RotateLeft(x04 ^ x09, 12);
  128. x03 += x04; x14 = Integers.RotateLeft(x14 ^ x03, 8);
  129. x09 += x14; x04 = Integers.RotateLeft(x04 ^ x09, 7);
  130. }
  131. x[ 0] = x00 + input[ 0];
  132. x[ 1] = x01 + input[ 1];
  133. x[ 2] = x02 + input[ 2];
  134. x[ 3] = x03 + input[ 3];
  135. x[ 4] = x04 + input[ 4];
  136. x[ 5] = x05 + input[ 5];
  137. x[ 6] = x06 + input[ 6];
  138. x[ 7] = x07 + input[ 7];
  139. x[ 8] = x08 + input[ 8];
  140. x[ 9] = x09 + input[ 9];
  141. x[10] = x10 + input[10];
  142. x[11] = x11 + input[11];
  143. x[12] = x12 + input[12];
  144. x[13] = x13 + input[13];
  145. x[14] = x14 + input[14];
  146. x[15] = x15 + input[15];
  147. }
  148. }
  149. }
  150. #pragma warning restore
  151. #endif