sha2.hpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. #ifndef BOOST_HASH2_SHA2_HPP_INCLUDED
  2. #define BOOST_HASH2_SHA2_HPP_INCLUDED
  3. // Copyright 2024 Peter Dimov.
  4. // Copyright 2024 Christian Mazakas.
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // https://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // SHA2 message digest algorithm, https://csrc.nist.gov/pubs/fips/180-4/upd1/final, https://www.rfc-editor.org/rfc/rfc6234
  9. #include <boost/hash2/hmac.hpp>
  10. #include <boost/hash2/digest.hpp>
  11. #include <boost/hash2/detail/read.hpp>
  12. #include <boost/hash2/detail/rot.hpp>
  13. #include <boost/hash2/detail/write.hpp>
  14. #include <boost/hash2/detail/memcpy.hpp>
  15. #include <boost/hash2/detail/memset.hpp>
  16. #include <boost/hash2/detail/is_constant_evaluated.hpp>
  17. #include <boost/assert.hpp>
  18. #include <array>
  19. #include <cstdint>
  20. #include <cstring>
  21. namespace boost
  22. {
  23. namespace hash2
  24. {
  25. namespace detail
  26. {
  27. template<class Word, class Algo, int M>
  28. struct sha2_base
  29. {
  30. Word state_[ 8 ] = {};
  31. static constexpr int N = M;
  32. unsigned char buffer_[ N ] = {};
  33. std::size_t m_ = 0; // == n_ % N
  34. std::uint64_t n_ = 0;
  35. constexpr sha2_base() = default;
  36. void update( void const* pv, std::size_t n )
  37. {
  38. unsigned char const* p = static_cast<unsigned char const*>( pv );
  39. update( p, n );
  40. }
  41. BOOST_CXX14_CONSTEXPR void update( unsigned char const* p, std::size_t n )
  42. {
  43. BOOST_ASSERT( m_ == n_ % N );
  44. n_ += n;
  45. if( m_ > 0 )
  46. {
  47. std::size_t k = N - m_;
  48. if( n < k )
  49. {
  50. k = n;
  51. }
  52. detail::memcpy( buffer_ + m_, p, k );
  53. p += k;
  54. n -= k;
  55. m_ += k;
  56. if( m_ < N ) return;
  57. BOOST_ASSERT( m_ == N );
  58. Algo::transform( buffer_, state_ );
  59. m_ = 0;
  60. detail::memset( buffer_, 0, N );
  61. }
  62. BOOST_ASSERT( m_ == 0 );
  63. while( n >= N )
  64. {
  65. Algo::transform( p, state_ );
  66. p += N;
  67. n -= N;
  68. }
  69. BOOST_ASSERT( n < N );
  70. if( n > 0 )
  71. {
  72. detail::memcpy( buffer_, p, n );
  73. m_ = n;
  74. }
  75. BOOST_ASSERT( m_ == n_ % N );
  76. }
  77. };
  78. template<class = void>
  79. struct sha2_256_constants
  80. {
  81. constexpr static std::uint32_t const K[ 64 ] =
  82. {
  83. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  84. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  85. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  86. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  87. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  88. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  89. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  90. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  91. };
  92. };
  93. template<class = void>
  94. struct sha2_512_constants
  95. {
  96. constexpr static std::uint64_t const K[ 80 ] =
  97. {
  98. 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
  99. 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
  100. 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
  101. 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
  102. 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
  103. 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
  104. 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
  105. 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
  106. 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
  107. 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
  108. 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
  109. 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
  110. 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
  111. 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
  112. 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
  113. 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
  114. 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
  115. 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
  116. 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
  117. 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
  118. };
  119. };
  120. // copy-paste from Boost.Unordered's prime_fmod approach
  121. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  122. // https://en.cppreference.com/w/cpp/language/static#Constant_static_members
  123. // If a const non-inline (since C++17) static data member or a constexpr
  124. // static data member (since C++11)(until C++17) is odr-used, a definition
  125. // at namespace scope is still required, but it cannot have an
  126. // initializer.
  127. template<class T>
  128. constexpr std::uint32_t sha2_256_constants<T>::K[ 64 ];
  129. template<class T>
  130. constexpr std::uint64_t sha2_512_constants<T>::K[ 80 ];
  131. #endif
  132. struct sha2_256_base : public sha2_base<std::uint32_t, sha2_256_base, 64>
  133. {
  134. BOOST_CXX14_CONSTEXPR static std::uint32_t Sigma0( std::uint32_t x ) noexcept
  135. {
  136. return detail::rotr( x, 2 ) ^ detail::rotr( x, 13 ) ^ detail::rotr( x, 22 );
  137. }
  138. BOOST_CXX14_CONSTEXPR static std::uint32_t Sigma1( std::uint32_t x ) noexcept
  139. {
  140. return detail::rotr( x, 6 ) ^ detail::rotr( x, 11 ) ^ detail::rotr( x, 25 );
  141. }
  142. BOOST_CXX14_CONSTEXPR static std::uint32_t sigma0( std::uint32_t x ) noexcept
  143. {
  144. return detail::rotr( x, 7 ) ^ detail::rotr( x, 18 ) ^ ( x >> 3 );
  145. }
  146. BOOST_CXX14_CONSTEXPR static std::uint32_t sigma1( std::uint32_t x ) noexcept
  147. {
  148. return detail::rotr( x, 17 ) ^ detail::rotr( x, 19 ) ^ ( x >> 10 );
  149. }
  150. BOOST_CXX14_CONSTEXPR static std::uint32_t Ch( std::uint32_t x, std::uint32_t y, std::uint32_t z ) noexcept
  151. {
  152. return ( x & y ) ^ ( ~x & z );
  153. }
  154. BOOST_CXX14_CONSTEXPR static std::uint32_t Maj( std::uint32_t x, std::uint32_t y, std::uint32_t z ) noexcept
  155. {
  156. return ( x & y ) ^ ( x & z ) ^ ( y & z );
  157. }
  158. BOOST_CXX14_CONSTEXPR static std::uint32_t W( std::uint32_t w[], int t )
  159. {
  160. return w[ t ] = ( sigma1( w[ t - 2 ] ) + w[ t - 7] + sigma0( w[ t - 15 ] ) + w[ t - 16 ] );
  161. }
  162. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static void R1( std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t& d,
  163. std::uint32_t e, std::uint32_t f, std::uint32_t g, std::uint32_t& h,
  164. unsigned char const block[ 64 ], std::uint32_t const* K, std::uint32_t w[], int t )
  165. {
  166. w[ t ] = detail::read32be( block + t * 4);
  167. std::uint32_t T1 = h + Sigma1( e ) + Ch( e, f, g ) + K[ t ] + w[ t ];
  168. std::uint32_t T2 = Sigma0( a ) + Maj( a, b, c );
  169. d += T1;
  170. h = T1 + T2;
  171. }
  172. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static void R2( std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t& d,
  173. std::uint32_t e, std::uint32_t f, std::uint32_t g, std::uint32_t& h,
  174. unsigned char const block[ 64 ], std::uint32_t const* K, std::uint32_t w[], int t )
  175. {
  176. (void)block;
  177. std::uint32_t T1 = h + Sigma1( e ) + Ch( e, f, g ) + K[ t ] + W( w, t );
  178. std::uint32_t T2 = Sigma0( a ) + Maj( a, b, c );
  179. d += T1;
  180. h = T1 + T2;
  181. }
  182. BOOST_CXX14_CONSTEXPR static void transform( unsigned char const block[ 64 ], std::uint32_t state[ 8 ] )
  183. {
  184. auto K = sha2_256_constants<>::K;
  185. std::uint32_t w[ 64 ] = {};
  186. std::uint32_t a = state[ 0 ];
  187. std::uint32_t b = state[ 1 ];
  188. std::uint32_t c = state[ 2 ];
  189. std::uint32_t d = state[ 3 ];
  190. std::uint32_t e = state[ 4 ];
  191. std::uint32_t f = state[ 5 ];
  192. std::uint32_t g = state[ 6 ];
  193. std::uint32_t h = state[ 7 ];
  194. R1( a, b, c, d, e, f, g, h, block, K, w, 0 );
  195. R1( h, a, b, c, d, e, f, g, block, K, w, 1 );
  196. R1( g, h, a, b, c, d, e, f, block, K, w, 2 );
  197. R1( f, g, h, a, b, c, d, e, block, K, w, 3 );
  198. R1( e, f, g, h, a, b, c, d, block, K, w, 4 );
  199. R1( d, e, f, g, h, a, b, c, block, K, w, 5 );
  200. R1( c, d, e, f, g, h, a, b, block, K, w, 6 );
  201. R1( b, c, d, e, f, g, h, a, block, K, w, 7 );
  202. R1( a, b, c, d, e, f, g, h, block, K, w, 8 );
  203. R1( h, a, b, c, d, e, f, g, block, K, w, 9 );
  204. R1( g, h, a, b, c, d, e, f, block, K, w, 10 );
  205. R1( f, g, h, a, b, c, d, e, block, K, w, 11 );
  206. R1( e, f, g, h, a, b, c, d, block, K, w, 12 );
  207. R1( d, e, f, g, h, a, b, c, block, K, w, 13 );
  208. R1( c, d, e, f, g, h, a, b, block, K, w, 14 );
  209. R1( b, c, d, e, f, g, h, a, block, K, w, 15 );
  210. R2( a, b, c, d, e, f, g, h, block, K, w, 16 );
  211. R2( h, a, b, c, d, e, f, g, block, K, w, 17 );
  212. R2( g, h, a, b, c, d, e, f, block, K, w, 18 );
  213. R2( f, g, h, a, b, c, d, e, block, K, w, 19 );
  214. R2( e, f, g, h, a, b, c, d, block, K, w, 20 );
  215. R2( d, e, f, g, h, a, b, c, block, K, w, 21 );
  216. R2( c, d, e, f, g, h, a, b, block, K, w, 22 );
  217. R2( b, c, d, e, f, g, h, a, block, K, w, 23 );
  218. R2( a, b, c, d, e, f, g, h, block, K, w, 24 );
  219. R2( h, a, b, c, d, e, f, g, block, K, w, 25 );
  220. R2( g, h, a, b, c, d, e, f, block, K, w, 26 );
  221. R2( f, g, h, a, b, c, d, e, block, K, w, 27 );
  222. R2( e, f, g, h, a, b, c, d, block, K, w, 28 );
  223. R2( d, e, f, g, h, a, b, c, block, K, w, 29 );
  224. R2( c, d, e, f, g, h, a, b, block, K, w, 30 );
  225. R2( b, c, d, e, f, g, h, a, block, K, w, 31 );
  226. R2( a, b, c, d, e, f, g, h, block, K, w, 32 );
  227. R2( h, a, b, c, d, e, f, g, block, K, w, 33 );
  228. R2( g, h, a, b, c, d, e, f, block, K, w, 34 );
  229. R2( f, g, h, a, b, c, d, e, block, K, w, 35 );
  230. R2( e, f, g, h, a, b, c, d, block, K, w, 36 );
  231. R2( d, e, f, g, h, a, b, c, block, K, w, 37 );
  232. R2( c, d, e, f, g, h, a, b, block, K, w, 38 );
  233. R2( b, c, d, e, f, g, h, a, block, K, w, 39 );
  234. R2( a, b, c, d, e, f, g, h, block, K, w, 40 );
  235. R2( h, a, b, c, d, e, f, g, block, K, w, 41 );
  236. R2( g, h, a, b, c, d, e, f, block, K, w, 42 );
  237. R2( f, g, h, a, b, c, d, e, block, K, w, 43 );
  238. R2( e, f, g, h, a, b, c, d, block, K, w, 44 );
  239. R2( d, e, f, g, h, a, b, c, block, K, w, 45 );
  240. R2( c, d, e, f, g, h, a, b, block, K, w, 46 );
  241. R2( b, c, d, e, f, g, h, a, block, K, w, 47 );
  242. R2( a, b, c, d, e, f, g, h, block, K, w, 48 );
  243. R2( h, a, b, c, d, e, f, g, block, K, w, 49 );
  244. R2( g, h, a, b, c, d, e, f, block, K, w, 50 );
  245. R2( f, g, h, a, b, c, d, e, block, K, w, 51 );
  246. R2( e, f, g, h, a, b, c, d, block, K, w, 52 );
  247. R2( d, e, f, g, h, a, b, c, block, K, w, 53 );
  248. R2( c, d, e, f, g, h, a, b, block, K, w, 54 );
  249. R2( b, c, d, e, f, g, h, a, block, K, w, 55 );
  250. R2( a, b, c, d, e, f, g, h, block, K, w, 56 );
  251. R2( h, a, b, c, d, e, f, g, block, K, w, 57 );
  252. R2( g, h, a, b, c, d, e, f, block, K, w, 58 );
  253. R2( f, g, h, a, b, c, d, e, block, K, w, 59 );
  254. R2( e, f, g, h, a, b, c, d, block, K, w, 60 );
  255. R2( d, e, f, g, h, a, b, c, block, K, w, 61 );
  256. R2( c, d, e, f, g, h, a, b, block, K, w, 62 );
  257. R2( b, c, d, e, f, g, h, a, block, K, w, 63 );
  258. state[0] += a;
  259. state[1] += b;
  260. state[2] += c;
  261. state[3] += d;
  262. state[4] += e;
  263. state[5] += f;
  264. state[6] += g;
  265. state[7] += h;
  266. }
  267. };
  268. struct sha2_512_base : public sha2_base<std::uint64_t, sha2_512_base, 128>
  269. {
  270. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t Ch( std::uint64_t x, std::uint64_t y, std::uint64_t z ) noexcept
  271. {
  272. return ( x & y ) ^ ( ~x & z );
  273. }
  274. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t Maj( std::uint64_t x, std::uint64_t y, std::uint64_t z ) noexcept
  275. {
  276. return ( x & y ) ^ ( x & z ) ^ ( y & z );
  277. }
  278. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t Sigma0( std::uint64_t x ) noexcept
  279. {
  280. return detail::rotr( x, 28 ) ^ detail::rotr( x, 34 ) ^ detail::rotr( x, 39 );
  281. }
  282. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t Sigma1( std::uint64_t x ) noexcept
  283. {
  284. return detail::rotr( x, 14 ) ^ detail::rotr( x, 18 ) ^ detail::rotr( x, 41 );
  285. }
  286. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t sigma0( std::uint64_t x ) noexcept
  287. {
  288. return detail::rotr( x, 1 ) ^ detail::rotr( x, 8 ) ^ ( x >> 7 );
  289. }
  290. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t sigma1( std::uint64_t x ) noexcept
  291. {
  292. return detail::rotr( x, 19 ) ^ detail::rotr( x, 61 ) ^ ( x >> 6 );
  293. }
  294. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static std::uint64_t W( std::uint64_t w[], int t )
  295. {
  296. return w[ t ] = ( sigma1( w[ t - 2 ] ) + w[ t - 7] + sigma0( w[ t - 15 ] ) + w[ t - 16 ] );
  297. }
  298. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static void R1( std::uint64_t a, std::uint64_t b, std::uint64_t c, std::uint64_t& d,
  299. std::uint64_t e, std::uint64_t f, std::uint64_t g, std::uint64_t& h,
  300. unsigned char const block[ 64 ], std::uint64_t const* K, std::uint64_t w[], int t )
  301. {
  302. w[ t ] = detail::read64be( block + t * 8 );
  303. std::uint64_t T1 = h + Sigma1( e ) + Ch( e, f, g ) + K[ t ] + w[ t ];
  304. std::uint64_t T2 = Sigma0( a ) + Maj( a, b, c );
  305. d += T1;
  306. h = T1 + T2;
  307. }
  308. BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR static void R2( std::uint64_t a, std::uint64_t b, std::uint64_t c, std::uint64_t& d,
  309. std::uint64_t e, std::uint64_t f, std::uint64_t g, std::uint64_t& h,
  310. unsigned char const block[ 64 ], std::uint64_t const* K, std::uint64_t w[], int t )
  311. {
  312. (void)block;
  313. std::uint64_t T1 = h + Sigma1( e ) + Ch( e, f, g ) + K[ t ] + W( w, t );
  314. std::uint64_t T2 = Sigma0( a ) + Maj( a, b, c );
  315. d += T1;
  316. h = T1 + T2;
  317. }
  318. BOOST_CXX14_CONSTEXPR static void transform( unsigned char const block[ 128 ], std::uint64_t state[ 8 ] )
  319. {
  320. auto K = sha2_512_constants<>::K;
  321. std::uint64_t w[ 80 ] = {};
  322. std::uint64_t a = state[ 0 ];
  323. std::uint64_t b = state[ 1 ];
  324. std::uint64_t c = state[ 2 ];
  325. std::uint64_t d = state[ 3 ];
  326. std::uint64_t e = state[ 4 ];
  327. std::uint64_t f = state[ 5 ];
  328. std::uint64_t g = state[ 6 ];
  329. std::uint64_t h = state[ 7 ];
  330. R1( a, b, c, d, e, f, g, h, block, K, w, 0 );
  331. R1( h, a, b, c, d, e, f, g, block, K, w, 1 );
  332. R1( g, h, a, b, c, d, e, f, block, K, w, 2 );
  333. R1( f, g, h, a, b, c, d, e, block, K, w, 3 );
  334. R1( e, f, g, h, a, b, c, d, block, K, w, 4 );
  335. R1( d, e, f, g, h, a, b, c, block, K, w, 5 );
  336. R1( c, d, e, f, g, h, a, b, block, K, w, 6 );
  337. R1( b, c, d, e, f, g, h, a, block, K, w, 7 );
  338. R1( a, b, c, d, e, f, g, h, block, K, w, 8 );
  339. R1( h, a, b, c, d, e, f, g, block, K, w, 9 );
  340. R1( g, h, a, b, c, d, e, f, block, K, w, 10 );
  341. R1( f, g, h, a, b, c, d, e, block, K, w, 11 );
  342. R1( e, f, g, h, a, b, c, d, block, K, w, 12 );
  343. R1( d, e, f, g, h, a, b, c, block, K, w, 13 );
  344. R1( c, d, e, f, g, h, a, b, block, K, w, 14 );
  345. R1( b, c, d, e, f, g, h, a, block, K, w, 15 );
  346. R2( a, b, c, d, e, f, g, h, block, K, w, 16 );
  347. R2( h, a, b, c, d, e, f, g, block, K, w, 17 );
  348. R2( g, h, a, b, c, d, e, f, block, K, w, 18 );
  349. R2( f, g, h, a, b, c, d, e, block, K, w, 19 );
  350. R2( e, f, g, h, a, b, c, d, block, K, w, 20 );
  351. R2( d, e, f, g, h, a, b, c, block, K, w, 21 );
  352. R2( c, d, e, f, g, h, a, b, block, K, w, 22 );
  353. R2( b, c, d, e, f, g, h, a, block, K, w, 23 );
  354. R2( a, b, c, d, e, f, g, h, block, K, w, 24 );
  355. R2( h, a, b, c, d, e, f, g, block, K, w, 25 );
  356. R2( g, h, a, b, c, d, e, f, block, K, w, 26 );
  357. R2( f, g, h, a, b, c, d, e, block, K, w, 27 );
  358. R2( e, f, g, h, a, b, c, d, block, K, w, 28 );
  359. R2( d, e, f, g, h, a, b, c, block, K, w, 29 );
  360. R2( c, d, e, f, g, h, a, b, block, K, w, 30 );
  361. R2( b, c, d, e, f, g, h, a, block, K, w, 31 );
  362. R2( a, b, c, d, e, f, g, h, block, K, w, 32 );
  363. R2( h, a, b, c, d, e, f, g, block, K, w, 33 );
  364. R2( g, h, a, b, c, d, e, f, block, K, w, 34 );
  365. R2( f, g, h, a, b, c, d, e, block, K, w, 35 );
  366. R2( e, f, g, h, a, b, c, d, block, K, w, 36 );
  367. R2( d, e, f, g, h, a, b, c, block, K, w, 37 );
  368. R2( c, d, e, f, g, h, a, b, block, K, w, 38 );
  369. R2( b, c, d, e, f, g, h, a, block, K, w, 39 );
  370. R2( a, b, c, d, e, f, g, h, block, K, w, 40 );
  371. R2( h, a, b, c, d, e, f, g, block, K, w, 41 );
  372. R2( g, h, a, b, c, d, e, f, block, K, w, 42 );
  373. R2( f, g, h, a, b, c, d, e, block, K, w, 43 );
  374. R2( e, f, g, h, a, b, c, d, block, K, w, 44 );
  375. R2( d, e, f, g, h, a, b, c, block, K, w, 45 );
  376. R2( c, d, e, f, g, h, a, b, block, K, w, 46 );
  377. R2( b, c, d, e, f, g, h, a, block, K, w, 47 );
  378. R2( a, b, c, d, e, f, g, h, block, K, w, 48 );
  379. R2( h, a, b, c, d, e, f, g, block, K, w, 49 );
  380. R2( g, h, a, b, c, d, e, f, block, K, w, 50 );
  381. R2( f, g, h, a, b, c, d, e, block, K, w, 51 );
  382. R2( e, f, g, h, a, b, c, d, block, K, w, 52 );
  383. R2( d, e, f, g, h, a, b, c, block, K, w, 53 );
  384. R2( c, d, e, f, g, h, a, b, block, K, w, 54 );
  385. R2( b, c, d, e, f, g, h, a, block, K, w, 55 );
  386. R2( a, b, c, d, e, f, g, h, block, K, w, 56 );
  387. R2( h, a, b, c, d, e, f, g, block, K, w, 57 );
  388. R2( g, h, a, b, c, d, e, f, block, K, w, 58 );
  389. R2( f, g, h, a, b, c, d, e, block, K, w, 59 );
  390. R2( e, f, g, h, a, b, c, d, block, K, w, 60 );
  391. R2( d, e, f, g, h, a, b, c, block, K, w, 61 );
  392. R2( c, d, e, f, g, h, a, b, block, K, w, 62 );
  393. R2( b, c, d, e, f, g, h, a, block, K, w, 63 );
  394. R2( a, b, c, d, e, f, g, h, block, K, w, 64 );
  395. R2( h, a, b, c, d, e, f, g, block, K, w, 65 );
  396. R2( g, h, a, b, c, d, e, f, block, K, w, 66 );
  397. R2( f, g, h, a, b, c, d, e, block, K, w, 67 );
  398. R2( e, f, g, h, a, b, c, d, block, K, w, 68 );
  399. R2( d, e, f, g, h, a, b, c, block, K, w, 69 );
  400. R2( c, d, e, f, g, h, a, b, block, K, w, 70 );
  401. R2( b, c, d, e, f, g, h, a, block, K, w, 71 );
  402. R2( a, b, c, d, e, f, g, h, block, K, w, 72 );
  403. R2( h, a, b, c, d, e, f, g, block, K, w, 73 );
  404. R2( g, h, a, b, c, d, e, f, block, K, w, 74 );
  405. R2( f, g, h, a, b, c, d, e, block, K, w, 75 );
  406. R2( e, f, g, h, a, b, c, d, block, K, w, 76 );
  407. R2( d, e, f, g, h, a, b, c, block, K, w, 77 );
  408. R2( c, d, e, f, g, h, a, b, block, K, w, 78 );
  409. R2( b, c, d, e, f, g, h, a, block, K, w, 79 );
  410. state[ 0 ] += a;
  411. state[ 1 ] += b;
  412. state[ 2 ] += c;
  413. state[ 3 ] += d;
  414. state[ 4 ] += e;
  415. state[ 5 ] += f;
  416. state[ 6 ] += g;
  417. state[ 7 ] += h;
  418. }
  419. };
  420. } // namespace detail
  421. class sha2_256 : detail::sha2_256_base
  422. {
  423. private:
  424. BOOST_CXX14_CONSTEXPR void init()
  425. {
  426. state_[ 0 ] = 0x6a09e667;
  427. state_[ 1 ] = 0xbb67ae85;
  428. state_[ 2 ] = 0x3c6ef372;
  429. state_[ 3 ] = 0xa54ff53a;
  430. state_[ 4 ] = 0x510e527f;
  431. state_[ 5 ] = 0x9b05688c;
  432. state_[ 6 ] = 0x1f83d9ab;
  433. state_[ 7 ] = 0x5be0cd19;
  434. }
  435. public:
  436. using result_type = digest<32>;
  437. static constexpr std::size_t block_size = 64;
  438. BOOST_CXX14_CONSTEXPR sha2_256()
  439. {
  440. init();
  441. }
  442. BOOST_CXX14_CONSTEXPR explicit sha2_256( std::uint64_t seed )
  443. {
  444. init();
  445. if( seed != 0 )
  446. {
  447. unsigned char tmp[ 8 ] = {};
  448. detail::write64le( tmp, seed );
  449. update( tmp, 8 );
  450. result();
  451. }
  452. }
  453. BOOST_CXX14_CONSTEXPR sha2_256( unsigned char const * p, std::size_t n )
  454. {
  455. init();
  456. if( n != 0 )
  457. {
  458. update( p, n );
  459. result();
  460. }
  461. }
  462. sha2_256( void const * p, std::size_t n ): sha2_256( static_cast<unsigned char const*>( p ), n )
  463. {
  464. }
  465. using detail::sha2_256_base::update;
  466. BOOST_CXX14_CONSTEXPR result_type result()
  467. {
  468. unsigned char bits[ 8 ] = {};
  469. detail::write64be( bits, n_ * 8 );
  470. std::size_t k = m_ < 56 ? 56 - m_ : 64 + 56 - m_;
  471. unsigned char padding[ 64 ] = { 0x80 };
  472. update( padding, k );
  473. update( bits, 8 );
  474. BOOST_ASSERT( m_ == 0 );
  475. result_type digest;
  476. for( int i = 0; i < 8; ++i )
  477. {
  478. detail::write32be( &digest[ i * 4 ], state_[ i ] );
  479. }
  480. return digest;
  481. }
  482. };
  483. class sha2_224 : detail::sha2_256_base
  484. {
  485. private:
  486. BOOST_CXX14_CONSTEXPR void init()
  487. {
  488. state_[ 0 ] = 0xc1059ed8;
  489. state_[ 1 ] = 0x367cd507;
  490. state_[ 2 ] = 0x3070dd17;
  491. state_[ 3 ] = 0xf70e5939;
  492. state_[ 4 ] = 0xffc00b31;
  493. state_[ 5 ] = 0x68581511;
  494. state_[ 6 ] = 0x64f98fa7;
  495. state_[ 7 ] = 0xbefa4fa4;
  496. }
  497. public:
  498. using result_type = digest<28>;
  499. static constexpr std::size_t block_size = 64;
  500. BOOST_CXX14_CONSTEXPR sha2_224()
  501. {
  502. init();
  503. }
  504. BOOST_CXX14_CONSTEXPR explicit sha2_224( std::uint64_t seed )
  505. {
  506. init();
  507. if( seed != 0 )
  508. {
  509. unsigned char tmp[ 8 ] = {};
  510. detail::write64le( tmp, seed );
  511. update( tmp, 8 );
  512. result();
  513. }
  514. }
  515. BOOST_CXX14_CONSTEXPR sha2_224( unsigned char const * p, std::size_t n )
  516. {
  517. init();
  518. if( n != 0 )
  519. {
  520. update( p, n );
  521. result();
  522. }
  523. }
  524. sha2_224( void const * p, std::size_t n ): sha2_224( static_cast<unsigned char const*>( p ), n )
  525. {
  526. }
  527. using detail::sha2_256_base::update;
  528. BOOST_CXX14_CONSTEXPR result_type result()
  529. {
  530. unsigned char bits[ 8 ] = {};
  531. detail::write64be( bits, n_ * 8 );
  532. std::size_t k = m_ < 56 ? 56 - m_ : 64 + 56 - m_;
  533. unsigned char padding[ 64 ] = { 0x80 };
  534. update( padding, k );
  535. update( bits, 8 );
  536. BOOST_ASSERT( m_ == 0 );
  537. result_type digest;
  538. for( int i = 0; i < 7; ++i ) {
  539. detail::write32be( &digest[ i * 4 ], state_[ i ] );
  540. }
  541. return digest;
  542. }
  543. };
  544. class sha2_512 : detail::sha2_512_base
  545. {
  546. private:
  547. BOOST_CXX14_CONSTEXPR void init()
  548. {
  549. state_[ 0 ] = 0x6a09e667f3bcc908;
  550. state_[ 1 ] = 0xbb67ae8584caa73b;
  551. state_[ 2 ] = 0x3c6ef372fe94f82b;
  552. state_[ 3 ] = 0xa54ff53a5f1d36f1;
  553. state_[ 4 ] = 0x510e527fade682d1;
  554. state_[ 5 ] = 0x9b05688c2b3e6c1f;
  555. state_[ 6 ] = 0x1f83d9abfb41bd6b;
  556. state_[ 7 ] = 0x5be0cd19137e2179;
  557. }
  558. public:
  559. using result_type = digest<64>;
  560. using detail::sha2_512_base::update;
  561. static constexpr std::size_t block_size = 128;
  562. BOOST_CXX14_CONSTEXPR sha2_512()
  563. {
  564. init();
  565. }
  566. BOOST_CXX14_CONSTEXPR explicit sha2_512( std::uint64_t seed )
  567. {
  568. init();
  569. if( seed != 0 )
  570. {
  571. unsigned char tmp[ 8 ] = {};
  572. detail::write64le( tmp, seed );
  573. update( tmp, 8 );
  574. result();
  575. }
  576. }
  577. BOOST_CXX14_CONSTEXPR sha2_512( unsigned char const * p, std::size_t n )
  578. {
  579. init();
  580. if( n != 0 )
  581. {
  582. update( p, n );
  583. result();
  584. }
  585. }
  586. sha2_512( void const * p, std::size_t n ): sha2_512( static_cast<unsigned char const*>( p ), n )
  587. {
  588. }
  589. BOOST_CXX14_CONSTEXPR result_type result()
  590. {
  591. unsigned char bits[ 16 ] = { 0 };
  592. detail::write64be( bits + 8, n_ * 8 );
  593. std::size_t k = m_ < 112 ? 112 - m_ : 128 + 112 - m_;
  594. unsigned char padding[ 128 ] = { 0x80 };
  595. update( padding, k );
  596. update( bits, 16 );
  597. BOOST_ASSERT( m_ == 0 );
  598. result_type digest;
  599. for( int i = 0; i < 8; ++i )
  600. {
  601. detail::write64be( &digest[ i * 8 ], state_[ i ] );
  602. }
  603. return digest;
  604. }
  605. };
  606. class sha2_384 : detail::sha2_512_base
  607. {
  608. private:
  609. BOOST_CXX14_CONSTEXPR void init()
  610. {
  611. state_[ 0 ] = 0xcbbb9d5dc1059ed8;
  612. state_[ 1 ] = 0x629a292a367cd507;
  613. state_[ 2 ] = 0x9159015a3070dd17;
  614. state_[ 3 ] = 0x152fecd8f70e5939;
  615. state_[ 4 ] = 0x67332667ffc00b31;
  616. state_[ 5 ] = 0x8eb44a8768581511;
  617. state_[ 6 ] = 0xdb0c2e0d64f98fa7;
  618. state_[ 7 ] = 0x47b5481dbefa4fa4;
  619. }
  620. public:
  621. using result_type = digest<48>;
  622. static constexpr std::size_t block_size = 128;
  623. using detail::sha2_512_base::update;
  624. BOOST_CXX14_CONSTEXPR sha2_384()
  625. {
  626. init();
  627. }
  628. BOOST_CXX14_CONSTEXPR explicit sha2_384( std::uint64_t seed )
  629. {
  630. init();
  631. if( seed != 0 )
  632. {
  633. unsigned char tmp[ 8 ] = {};
  634. detail::write64le( tmp, seed );
  635. update( tmp, 8 );
  636. result();
  637. }
  638. }
  639. BOOST_CXX14_CONSTEXPR sha2_384( unsigned char const * p, std::size_t n )
  640. {
  641. init();
  642. if( n != 0 )
  643. {
  644. update( p, n );
  645. result();
  646. }
  647. }
  648. sha2_384( void const * p, std::size_t n ): sha2_384( static_cast<unsigned char const*>( p ), n )
  649. {
  650. }
  651. BOOST_CXX14_CONSTEXPR result_type result()
  652. {
  653. unsigned char bits[ 16 ] = { 0 };
  654. detail::write64be( bits + 8, n_ * 8 );
  655. std::size_t k = m_ < 112 ? 112 - m_ : 128 + 112 - m_;
  656. unsigned char padding[ 128 ] = { 0x80 };
  657. update( padding, k );
  658. update( bits, 16 );
  659. BOOST_ASSERT( m_ == 0 );
  660. result_type digest;
  661. for( int i = 0; i < 6; ++i )
  662. {
  663. detail::write64be( &digest[ i * 8 ], state_[ i ] );
  664. }
  665. return digest;
  666. }
  667. };
  668. class sha2_512_224 : detail::sha2_512_base
  669. {
  670. private:
  671. BOOST_CXX14_CONSTEXPR void init()
  672. {
  673. state_[ 0 ] = 0x8c3d37c819544da2;
  674. state_[ 1 ] = 0x73e1996689dcd4d6;
  675. state_[ 2 ] = 0x1dfab7ae32ff9c82;
  676. state_[ 3 ] = 0x679dd514582f9fcf;
  677. state_[ 4 ] = 0x0f6d2b697bd44da8;
  678. state_[ 5 ] = 0x77e36f7304c48942;
  679. state_[ 6 ] = 0x3f9d85a86a1d36c8;
  680. state_[ 7 ] = 0x1112e6ad91d692a1;
  681. }
  682. public:
  683. using result_type = digest<28>;
  684. static constexpr std::size_t block_size = 128;
  685. using detail::sha2_512_base::update;
  686. BOOST_CXX14_CONSTEXPR sha2_512_224()
  687. {
  688. init();
  689. }
  690. BOOST_CXX14_CONSTEXPR explicit sha2_512_224( std::uint64_t seed )
  691. {
  692. init();
  693. if( seed != 0 )
  694. {
  695. unsigned char tmp[ 8 ] = {};
  696. detail::write64le( tmp, seed );
  697. update( tmp, 8 );
  698. result();
  699. }
  700. }
  701. BOOST_CXX14_CONSTEXPR sha2_512_224( unsigned char const * p, std::size_t n )
  702. {
  703. init();
  704. if( n != 0 )
  705. {
  706. update( p, n );
  707. result();
  708. }
  709. }
  710. sha2_512_224( void const * p, std::size_t n ): sha2_512_224( static_cast<unsigned char const*>( p ), n )
  711. {
  712. }
  713. BOOST_CXX14_CONSTEXPR result_type result()
  714. {
  715. unsigned char bits[ 16 ] = { 0 };
  716. detail::write64be( bits + 8, n_ * 8 );
  717. std::size_t k = m_ < 112 ? 112 - m_ : 128 + 112 - m_;
  718. unsigned char padding[ 128 ] = { 0x80 };
  719. update( padding, k );
  720. update( bits, 16 );
  721. BOOST_ASSERT( m_ == 0 );
  722. result_type digest;
  723. for( int i = 0; i < 3; ++i )
  724. {
  725. detail::write64be( &digest[ i * 8 ], state_[ i ] );
  726. }
  727. detail::write32be( &digest[ 3 * 8 ], state_[ 3 ] >> 32 );
  728. return digest;
  729. }
  730. };
  731. class sha2_512_256 : detail::sha2_512_base
  732. {
  733. private:
  734. BOOST_CXX14_CONSTEXPR void init()
  735. {
  736. state_[ 0 ] = 0x22312194fc2bf72c;
  737. state_[ 1 ] = 0x9f555fa3c84c64c2;
  738. state_[ 2 ] = 0x2393b86b6f53b151;
  739. state_[ 3 ] = 0x963877195940eabd;
  740. state_[ 4 ] = 0x96283ee2a88effe3;
  741. state_[ 5 ] = 0xbe5e1e2553863992;
  742. state_[ 6 ] = 0x2b0199fc2c85b8aa;
  743. state_[ 7 ] = 0x0eb72ddc81c52ca2;
  744. }
  745. public:
  746. using result_type = digest<32>;
  747. static constexpr std::size_t block_size = 128;
  748. using detail::sha2_512_base::update;
  749. BOOST_CXX14_CONSTEXPR sha2_512_256()
  750. {
  751. init();
  752. }
  753. BOOST_CXX14_CONSTEXPR explicit sha2_512_256( std::uint64_t seed )
  754. {
  755. init();
  756. if( seed != 0 )
  757. {
  758. unsigned char tmp[ 8 ] = {};
  759. detail::write64le( tmp, seed );
  760. update( tmp, 8 );
  761. result();
  762. }
  763. }
  764. BOOST_CXX14_CONSTEXPR sha2_512_256( unsigned char const * p, std::size_t n )
  765. {
  766. init();
  767. if( n != 0 )
  768. {
  769. update( p, n );
  770. result();
  771. }
  772. }
  773. sha2_512_256( void const * p, std::size_t n ): sha2_512_256( static_cast<unsigned char const*>( p ), n )
  774. {
  775. }
  776. BOOST_CXX14_CONSTEXPR result_type result()
  777. {
  778. unsigned char bits[ 16 ] = { 0 };
  779. detail::write64be( bits + 8, n_ * 8 );
  780. std::size_t k = m_ < 112 ? 112 - m_ : 128 + 112 - m_;
  781. unsigned char padding[ 128 ] = { 0x80 };
  782. update( padding, k );
  783. update( bits, 16 );
  784. BOOST_ASSERT( m_ == 0 );
  785. result_type digest;
  786. for( int i = 0; i < 4; ++i )
  787. {
  788. detail::write64be( &digest[ i * 8 ], state_[ i ] );
  789. }
  790. return digest;
  791. }
  792. };
  793. // hmac wrappers
  794. using hmac_sha2_256 = hmac<sha2_256>;
  795. using hmac_sha2_224 = hmac<sha2_224>;
  796. using hmac_sha2_512 = hmac<sha2_512>;
  797. using hmac_sha2_384 = hmac<sha2_384>;
  798. using hmac_sha2_512_224 = hmac<sha2_512_224>;
  799. using hmac_sha2_512_256 = hmac<sha2_512_256>;
  800. } // namespace hash2
  801. } // namespace boost
  802. #endif // #ifndef BOOST_HASH2_SHA2_HPP_INCLUDED