ChaChaEngine.cs 4.3 KB

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