ChaChaEngine.cs 4.4 KB

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