sha1.ipp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_SHA1_IPP
  10. #define BOOST_BEAST_DETAIL_SHA1_IPP
  11. #include <boost/beast/core/detail/sha1.hpp>
  12. #include <algorithm>
  13. #include <cstdint>
  14. #include <cstring>
  15. // Based on https://github.com/vog/sha1
  16. /*
  17. Original authors:
  18. Steve Reid (Original C Code)
  19. Bruce Guenter (Small changes to fit into bglibs)
  20. Volker Grabsch (Translation to simpler C++ Code)
  21. Eugene Hopkinson (Safety improvements)
  22. Vincent Falco (beast adaptation)
  23. */
  24. namespace boost {
  25. namespace beast {
  26. namespace detail {
  27. namespace sha1 {
  28. inline
  29. std::uint32_t
  30. rol(std::uint32_t value, std::size_t bits)
  31. {
  32. return (value << bits) | (value >> (32 - bits));
  33. }
  34. inline
  35. std::uint32_t
  36. blk(std::uint32_t block[BLOCK_INTS], std::size_t i)
  37. {
  38. return rol(
  39. block[(i+13)&15] ^ block[(i+8)&15] ^
  40. block[(i+2)&15] ^ block[i], 1);
  41. }
  42. inline
  43. void
  44. R0(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
  45. std::uint32_t &w, std::uint32_t x, std::uint32_t y,
  46. std::uint32_t &z, std::size_t i)
  47. {
  48. z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
  49. w = rol(w, 30);
  50. }
  51. inline
  52. void
  53. R1(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
  54. std::uint32_t &w, std::uint32_t x, std::uint32_t y,
  55. std::uint32_t &z, std::size_t i)
  56. {
  57. block[i] = blk(block, i);
  58. z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
  59. w = rol(w, 30);
  60. }
  61. inline
  62. void
  63. R2(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
  64. std::uint32_t &w, std::uint32_t x, std::uint32_t y,
  65. std::uint32_t &z, std::size_t i)
  66. {
  67. block[i] = blk(block, i);
  68. z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5);
  69. w = rol(w, 30);
  70. }
  71. inline
  72. void
  73. R3(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
  74. std::uint32_t &w, std::uint32_t x, std::uint32_t y,
  75. std::uint32_t &z, std::size_t i)
  76. {
  77. block[i] = blk(block, i);
  78. z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5);
  79. w = rol(w, 30);
  80. }
  81. inline
  82. void
  83. R4(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
  84. std::uint32_t &w, std::uint32_t x, std::uint32_t y,
  85. std::uint32_t &z, std::size_t i)
  86. {
  87. block[i] = blk(block, i);
  88. z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5);
  89. w = rol(w, 30);
  90. }
  91. inline
  92. void
  93. make_block(std::uint8_t const* p,
  94. std::uint32_t block[BLOCK_INTS])
  95. {
  96. for(std::size_t i = 0; i < BLOCK_INTS; i++)
  97. block[i] =
  98. (static_cast<std::uint32_t>(p[4*i+3])) |
  99. (static_cast<std::uint32_t>(p[4*i+2]))<< 8 |
  100. (static_cast<std::uint32_t>(p[4*i+1]))<<16 |
  101. (static_cast<std::uint32_t>(p[4*i+0]))<<24;
  102. }
  103. inline
  104. void
  105. transform(
  106. std::uint32_t digest[], std::uint32_t block[BLOCK_INTS])
  107. {
  108. std::uint32_t a = digest[0];
  109. std::uint32_t b = digest[1];
  110. std::uint32_t c = digest[2];
  111. std::uint32_t d = digest[3];
  112. std::uint32_t e = digest[4];
  113. R0(block, a, b, c, d, e, 0);
  114. R0(block, e, a, b, c, d, 1);
  115. R0(block, d, e, a, b, c, 2);
  116. R0(block, c, d, e, a, b, 3);
  117. R0(block, b, c, d, e, a, 4);
  118. R0(block, a, b, c, d, e, 5);
  119. R0(block, e, a, b, c, d, 6);
  120. R0(block, d, e, a, b, c, 7);
  121. R0(block, c, d, e, a, b, 8);
  122. R0(block, b, c, d, e, a, 9);
  123. R0(block, a, b, c, d, e, 10);
  124. R0(block, e, a, b, c, d, 11);
  125. R0(block, d, e, a, b, c, 12);
  126. R0(block, c, d, e, a, b, 13);
  127. R0(block, b, c, d, e, a, 14);
  128. R0(block, a, b, c, d, e, 15);
  129. R1(block, e, a, b, c, d, 0);
  130. R1(block, d, e, a, b, c, 1);
  131. R1(block, c, d, e, a, b, 2);
  132. R1(block, b, c, d, e, a, 3);
  133. R2(block, a, b, c, d, e, 4);
  134. R2(block, e, a, b, c, d, 5);
  135. R2(block, d, e, a, b, c, 6);
  136. R2(block, c, d, e, a, b, 7);
  137. R2(block, b, c, d, e, a, 8);
  138. R2(block, a, b, c, d, e, 9);
  139. R2(block, e, a, b, c, d, 10);
  140. R2(block, d, e, a, b, c, 11);
  141. R2(block, c, d, e, a, b, 12);
  142. R2(block, b, c, d, e, a, 13);
  143. R2(block, a, b, c, d, e, 14);
  144. R2(block, e, a, b, c, d, 15);
  145. R2(block, d, e, a, b, c, 0);
  146. R2(block, c, d, e, a, b, 1);
  147. R2(block, b, c, d, e, a, 2);
  148. R2(block, a, b, c, d, e, 3);
  149. R2(block, e, a, b, c, d, 4);
  150. R2(block, d, e, a, b, c, 5);
  151. R2(block, c, d, e, a, b, 6);
  152. R2(block, b, c, d, e, a, 7);
  153. R3(block, a, b, c, d, e, 8);
  154. R3(block, e, a, b, c, d, 9);
  155. R3(block, d, e, a, b, c, 10);
  156. R3(block, c, d, e, a, b, 11);
  157. R3(block, b, c, d, e, a, 12);
  158. R3(block, a, b, c, d, e, 13);
  159. R3(block, e, a, b, c, d, 14);
  160. R3(block, d, e, a, b, c, 15);
  161. R3(block, c, d, e, a, b, 0);
  162. R3(block, b, c, d, e, a, 1);
  163. R3(block, a, b, c, d, e, 2);
  164. R3(block, e, a, b, c, d, 3);
  165. R3(block, d, e, a, b, c, 4);
  166. R3(block, c, d, e, a, b, 5);
  167. R3(block, b, c, d, e, a, 6);
  168. R3(block, a, b, c, d, e, 7);
  169. R3(block, e, a, b, c, d, 8);
  170. R3(block, d, e, a, b, c, 9);
  171. R3(block, c, d, e, a, b, 10);
  172. R3(block, b, c, d, e, a, 11);
  173. R4(block, a, b, c, d, e, 12);
  174. R4(block, e, a, b, c, d, 13);
  175. R4(block, d, e, a, b, c, 14);
  176. R4(block, c, d, e, a, b, 15);
  177. R4(block, b, c, d, e, a, 0);
  178. R4(block, a, b, c, d, e, 1);
  179. R4(block, e, a, b, c, d, 2);
  180. R4(block, d, e, a, b, c, 3);
  181. R4(block, c, d, e, a, b, 4);
  182. R4(block, b, c, d, e, a, 5);
  183. R4(block, a, b, c, d, e, 6);
  184. R4(block, e, a, b, c, d, 7);
  185. R4(block, d, e, a, b, c, 8);
  186. R4(block, c, d, e, a, b, 9);
  187. R4(block, b, c, d, e, a, 10);
  188. R4(block, a, b, c, d, e, 11);
  189. R4(block, e, a, b, c, d, 12);
  190. R4(block, d, e, a, b, c, 13);
  191. R4(block, c, d, e, a, b, 14);
  192. R4(block, b, c, d, e, a, 15);
  193. digest[0] += a;
  194. digest[1] += b;
  195. digest[2] += c;
  196. digest[3] += d;
  197. digest[4] += e;
  198. }
  199. } // sha1
  200. void
  201. init(sha1_context& ctx) noexcept
  202. {
  203. ctx.buflen = 0;
  204. ctx.blocks = 0;
  205. ctx.digest[0] = 0x67452301;
  206. ctx.digest[1] = 0xefcdab89;
  207. ctx.digest[2] = 0x98badcfe;
  208. ctx.digest[3] = 0x10325476;
  209. ctx.digest[4] = 0xc3d2e1f0;
  210. }
  211. void
  212. update(
  213. sha1_context& ctx,
  214. void const* message,
  215. std::size_t size) noexcept
  216. {
  217. auto p = static_cast<
  218. std::uint8_t const*>(message);
  219. for(;;)
  220. {
  221. auto const n = (std::min)(
  222. size, sizeof(ctx.buf) - ctx.buflen);
  223. std::memcpy(ctx.buf + ctx.buflen, p, n);
  224. ctx.buflen += n;
  225. if(ctx.buflen != 64)
  226. return;
  227. p += n;
  228. size -= n;
  229. ctx.buflen = 0;
  230. std::uint32_t block[sha1::BLOCK_INTS];
  231. sha1::make_block(ctx.buf, block);
  232. sha1::transform(ctx.digest, block);
  233. ++ctx.blocks;
  234. }
  235. }
  236. void
  237. finish(
  238. sha1_context& ctx,
  239. void* digest) noexcept
  240. {
  241. using sha1::BLOCK_INTS;
  242. using sha1::BLOCK_BYTES;
  243. std::uint64_t total_bits =
  244. (ctx.blocks*64 + ctx.buflen) * 8;
  245. // pad
  246. ctx.buf[ctx.buflen++] = 0x80;
  247. auto const buflen = ctx.buflen;
  248. while(ctx.buflen < 64)
  249. ctx.buf[ctx.buflen++] = 0x00;
  250. std::uint32_t block[BLOCK_INTS];
  251. sha1::make_block(ctx.buf, block);
  252. if(buflen > BLOCK_BYTES - 8)
  253. {
  254. sha1::transform(ctx.digest, block);
  255. for(size_t i = 0; i < BLOCK_INTS - 2; i++)
  256. block[i] = 0;
  257. }
  258. /* Append total_bits, split this uint64_t into two uint32_t */
  259. block[BLOCK_INTS - 1] = total_bits & 0xffffffff;
  260. block[BLOCK_INTS - 2] = (total_bits >> 32);
  261. sha1::transform(ctx.digest, block);
  262. for(std::size_t i = 0; i < sha1::DIGEST_BYTES/4; i++)
  263. {
  264. std::uint8_t* d =
  265. static_cast<std::uint8_t*>(digest) + 4 * i;
  266. d[3] = ctx.digest[i] & 0xff;
  267. d[2] = (ctx.digest[i] >> 8) & 0xff;
  268. d[1] = (ctx.digest[i] >> 16) & 0xff;
  269. d[0] = (ctx.digest[i] >> 24) & 0xff;
  270. }
  271. }
  272. } // detail
  273. } // beast
  274. } // boost
  275. #endif