You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chacha20.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2017 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. // Based on the public domain implementation 'merged' by D. J. Bernstein
  5. // See https://cr.yp.to/chacha.html.
  6. #include "crypto/common.h"
  7. #include "crypto/chacha20.h"
  8. #include <string.h>
  9. constexpr static inline uint32_t rotl32(uint32_t v, int c) { return (v << c) | (v >> (32 - c)); }
  10. #define QUARTERROUND(a,b,c,d) \
  11. a += b; d = rotl32(d ^ a, 16); \
  12. c += d; b = rotl32(b ^ c, 12); \
  13. a += b; d = rotl32(d ^ a, 8); \
  14. c += d; b = rotl32(b ^ c, 7);
  15. static const unsigned char sigma[] = "expand 32-byte k";
  16. static const unsigned char tau[] = "expand 16-byte k";
  17. void ChaCha20::SetKey(const unsigned char* k, size_t keylen)
  18. {
  19. const unsigned char *constants;
  20. input[4] = ReadLE32(k + 0);
  21. input[5] = ReadLE32(k + 4);
  22. input[6] = ReadLE32(k + 8);
  23. input[7] = ReadLE32(k + 12);
  24. if (keylen == 32) { /* recommended */
  25. k += 16;
  26. constants = sigma;
  27. } else { /* keylen == 16 */
  28. constants = tau;
  29. }
  30. input[8] = ReadLE32(k + 0);
  31. input[9] = ReadLE32(k + 4);
  32. input[10] = ReadLE32(k + 8);
  33. input[11] = ReadLE32(k + 12);
  34. input[0] = ReadLE32(constants + 0);
  35. input[1] = ReadLE32(constants + 4);
  36. input[2] = ReadLE32(constants + 8);
  37. input[3] = ReadLE32(constants + 12);
  38. input[12] = 0;
  39. input[13] = 0;
  40. input[14] = 0;
  41. input[15] = 0;
  42. }
  43. ChaCha20::ChaCha20()
  44. {
  45. memset(input, 0, sizeof(input));
  46. }
  47. ChaCha20::ChaCha20(const unsigned char* k, size_t keylen)
  48. {
  49. SetKey(k, keylen);
  50. }
  51. void ChaCha20::SetIV(uint64_t iv)
  52. {
  53. input[14] = iv;
  54. input[15] = iv >> 32;
  55. }
  56. void ChaCha20::Seek(uint64_t pos)
  57. {
  58. input[12] = pos;
  59. input[13] = pos >> 32;
  60. }
  61. void ChaCha20::Output(unsigned char* c, size_t bytes)
  62. {
  63. uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
  64. uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
  65. unsigned char *ctarget = nullptr;
  66. unsigned char tmp[64];
  67. unsigned int i;
  68. if (!bytes) return;
  69. j0 = input[0];
  70. j1 = input[1];
  71. j2 = input[2];
  72. j3 = input[3];
  73. j4 = input[4];
  74. j5 = input[5];
  75. j6 = input[6];
  76. j7 = input[7];
  77. j8 = input[8];
  78. j9 = input[9];
  79. j10 = input[10];
  80. j11 = input[11];
  81. j12 = input[12];
  82. j13 = input[13];
  83. j14 = input[14];
  84. j15 = input[15];
  85. for (;;) {
  86. if (bytes < 64) {
  87. ctarget = c;
  88. c = tmp;
  89. }
  90. x0 = j0;
  91. x1 = j1;
  92. x2 = j2;
  93. x3 = j3;
  94. x4 = j4;
  95. x5 = j5;
  96. x6 = j6;
  97. x7 = j7;
  98. x8 = j8;
  99. x9 = j9;
  100. x10 = j10;
  101. x11 = j11;
  102. x12 = j12;
  103. x13 = j13;
  104. x14 = j14;
  105. x15 = j15;
  106. for (i = 20;i > 0;i -= 2) {
  107. QUARTERROUND( x0, x4, x8,x12)
  108. QUARTERROUND( x1, x5, x9,x13)
  109. QUARTERROUND( x2, x6,x10,x14)
  110. QUARTERROUND( x3, x7,x11,x15)
  111. QUARTERROUND( x0, x5,x10,x15)
  112. QUARTERROUND( x1, x6,x11,x12)
  113. QUARTERROUND( x2, x7, x8,x13)
  114. QUARTERROUND( x3, x4, x9,x14)
  115. }
  116. x0 += j0;
  117. x1 += j1;
  118. x2 += j2;
  119. x3 += j3;
  120. x4 += j4;
  121. x5 += j5;
  122. x6 += j6;
  123. x7 += j7;
  124. x8 += j8;
  125. x9 += j9;
  126. x10 += j10;
  127. x11 += j11;
  128. x12 += j12;
  129. x13 += j13;
  130. x14 += j14;
  131. x15 += j15;
  132. ++j12;
  133. if (!j12) ++j13;
  134. WriteLE32(c + 0, x0);
  135. WriteLE32(c + 4, x1);
  136. WriteLE32(c + 8, x2);
  137. WriteLE32(c + 12, x3);
  138. WriteLE32(c + 16, x4);
  139. WriteLE32(c + 20, x5);
  140. WriteLE32(c + 24, x6);
  141. WriteLE32(c + 28, x7);
  142. WriteLE32(c + 32, x8);
  143. WriteLE32(c + 36, x9);
  144. WriteLE32(c + 40, x10);
  145. WriteLE32(c + 44, x11);
  146. WriteLE32(c + 48, x12);
  147. WriteLE32(c + 52, x13);
  148. WriteLE32(c + 56, x14);
  149. WriteLE32(c + 60, x15);
  150. if (bytes <= 64) {
  151. if (bytes < 64) {
  152. for (i = 0;i < bytes;++i) ctarget[i] = c[i];
  153. }
  154. input[12] = j12;
  155. input[13] = j13;
  156. return;
  157. }
  158. bytes -= 64;
  159. c += 64;
  160. }
  161. }