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.

171 lines
6.4 KiB

4 months ago
  1. // Copyright (c) 2016 Alex Hultman and contributors
  2. // This software is provided 'as-is', without any express or implied
  3. // warranty. In no event will the authors be held liable for any damages
  4. // arising from the use of this software.
  5. // Permission is granted to anyone to use this software for any purpose,
  6. // including commercial applications, and to alter it and redistribute it
  7. // freely, subject to the following restrictions:
  8. // 1. The origin of this software must not be misrepresented; you must not
  9. // claim that you wrote the original software. If you use this software
  10. // in a product, an acknowledgement in the product documentation would be
  11. // appreciated but is not required.
  12. // 2. Altered source versions must be plainly marked as such, and must not be
  13. // misrepresented as being the original software.
  14. // 3. This notice may not be removed or altered from any source distribution.
  15. #pragma once
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include <string.h>
  19. #include <string>
  20. class WebSocketHandshakeKeyGen
  21. {
  22. template<int N, typename T>
  23. struct static_for
  24. {
  25. void operator()(uint32_t* a, uint32_t* b)
  26. {
  27. static_for<N - 1, T>()(a, b);
  28. T::template f<N - 1>(a, b);
  29. }
  30. };
  31. template<typename T>
  32. struct static_for<0, T>
  33. {
  34. void operator()(uint32_t* /*a*/, uint32_t* /*hash*/)
  35. {
  36. }
  37. };
  38. template<int state>
  39. struct Sha1Loop
  40. {
  41. static inline uint32_t rol(uint32_t value, size_t bits)
  42. {
  43. return (value << bits) | (value >> (32 - bits));
  44. }
  45. static inline uint32_t blk(uint32_t b[16], size_t i)
  46. {
  47. return rol(b[(i + 13) & 15] ^ b[(i + 8) & 15] ^ b[(i + 2) & 15] ^ b[i], 1);
  48. }
  49. template<int i>
  50. static inline void f(uint32_t* a, uint32_t* b)
  51. {
  52. switch (state)
  53. {
  54. case 1:
  55. a[i % 5] +=
  56. ((a[(3 + i) % 5] & (a[(2 + i) % 5] ^ a[(1 + i) % 5])) ^ a[(1 + i) % 5]) +
  57. b[i] + 0x5a827999 + rol(a[(4 + i) % 5], 5);
  58. a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
  59. break;
  60. case 2:
  61. b[i] = blk(b, i);
  62. a[(1 + i) % 5] +=
  63. ((a[(4 + i) % 5] & (a[(3 + i) % 5] ^ a[(2 + i) % 5])) ^ a[(2 + i) % 5]) +
  64. b[i] + 0x5a827999 + rol(a[(5 + i) % 5], 5);
  65. a[(4 + i) % 5] = rol(a[(4 + i) % 5], 30);
  66. break;
  67. case 3:
  68. b[(i + 4) % 16] = blk(b, (i + 4) % 16);
  69. a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) +
  70. b[(i + 4) % 16] + 0x6ed9eba1 + rol(a[(4 + i) % 5], 5);
  71. a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
  72. break;
  73. case 4:
  74. b[(i + 8) % 16] = blk(b, (i + 8) % 16);
  75. a[i % 5] += (((a[(3 + i) % 5] | a[(2 + i) % 5]) & a[(1 + i) % 5]) |
  76. (a[(3 + i) % 5] & a[(2 + i) % 5])) +
  77. b[(i + 8) % 16] + 0x8f1bbcdc + rol(a[(4 + i) % 5], 5);
  78. a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
  79. break;
  80. case 5:
  81. b[(i + 12) % 16] = blk(b, (i + 12) % 16);
  82. a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) +
  83. b[(i + 12) % 16] + 0xca62c1d6 + rol(a[(4 + i) % 5], 5);
  84. a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
  85. break;
  86. case 6: b[i] += a[4 - i];
  87. }
  88. }
  89. };
  90. static inline void sha1(uint32_t hash[5], uint32_t b[16])
  91. {
  92. uint32_t a[5] = {hash[4], hash[3], hash[2], hash[1], hash[0]};
  93. static_for<16, Sha1Loop<1>>()(a, b);
  94. static_for<4, Sha1Loop<2>>()(a, b);
  95. static_for<20, Sha1Loop<3>>()(a, b);
  96. static_for<20, Sha1Loop<4>>()(a, b);
  97. static_for<20, Sha1Loop<5>>()(a, b);
  98. static_for<5, Sha1Loop<6>>()(a, hash);
  99. }
  100. static inline void base64(unsigned char* src, char* dst)
  101. {
  102. const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  103. for (int i = 0; i < 18; i += 3)
  104. {
  105. *dst++ = b64[(src[i] >> 2) & 63];
  106. *dst++ = b64[((src[i] & 3) << 4) | ((src[i + 1] & 240) >> 4)];
  107. *dst++ = b64[((src[i + 1] & 15) << 2) | ((src[i + 2] & 192) >> 6)];
  108. *dst++ = b64[src[i + 2] & 63];
  109. }
  110. *dst++ = b64[(src[18] >> 2) & 63];
  111. *dst++ = b64[((src[18] & 3) << 4) | ((src[19] & 240) >> 4)];
  112. *dst++ = b64[((src[19] & 15) << 2)];
  113. *dst++ = '=';
  114. }
  115. public:
  116. static inline void generate(const std::string& inputStr, char output[28])
  117. {
  118. char input[25] = {};
  119. strncpy(input, inputStr.c_str(), 25 - 1);
  120. input[25 - 1] = '\0';
  121. uint32_t b_output[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
  122. uint32_t b_input[16] = {0,
  123. 0,
  124. 0,
  125. 0,
  126. 0,
  127. 0,
  128. 0x32353845,
  129. 0x41464135,
  130. 0x2d453931,
  131. 0x342d3437,
  132. 0x44412d39,
  133. 0x3543412d,
  134. 0x43354142,
  135. 0x30444338,
  136. 0x35423131,
  137. 0x80000000};
  138. for (int i = 0; i < 6; i++)
  139. {
  140. b_input[i] = (input[4 * i + 3] & 0xff) | (input[4 * i + 2] & 0xff) << 8 |
  141. (input[4 * i + 1] & 0xff) << 16 | (input[4 * i + 0] & 0xff) << 24;
  142. }
  143. sha1(b_output, b_input);
  144. uint32_t last_b[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480};
  145. sha1(b_output, last_b);
  146. for (int i = 0; i < 5; i++)
  147. {
  148. uint32_t tmp = b_output[i];
  149. char* bytes = (char*) &b_output[i];
  150. bytes[3] = tmp & 0xff;
  151. bytes[2] = (tmp >> 8) & 0xff;
  152. bytes[1] = (tmp >> 16) & 0xff;
  153. bytes[0] = (tmp >> 24) & 0xff;
  154. }
  155. base64((unsigned char*) b_output, output);
  156. }
  157. };