diff --git a/hash/sha1.cpp b/hash/sha1.cpp index 7e07284..7140798 100644 --- a/hash/sha1.cpp +++ b/hash/sha1.cpp @@ -24,15 +24,15 @@ using cruft::crypto::hash::SHA1; /////////////////////////////////////////////////////////////////////////////// // Constant words for sequence of rounds -static constexpr uint32_t K_00 = 0x5A827999; -static constexpr uint32_t K_20 = 0x6ED9EBA1; -static constexpr uint32_t K_40 = 0x8F1BBCDC; -static constexpr uint32_t K_60 = 0xCA62C1D6; +static constexpr u32 K_00 = 0x5A827999; +static constexpr u32 K_20 = 0x6ED9EBA1; +static constexpr u32 K_40 = 0x8F1BBCDC; +static constexpr u32 K_60 = 0xCA62C1D6; //----------------------------------------------------------------------------- static constexpr -std::array INITIAL_H = { +std::array INITIAL_H = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, @@ -43,82 +43,53 @@ std::array INITIAL_H = { //----------------------------------------------------------------------------- static constexpr size_t BLOCK_WORDS = 16; -static constexpr size_t BLOCK_BYTES = BLOCK_WORDS * sizeof (uint32_t); +static constexpr size_t BLOCK_BYTES = BLOCK_WORDS * sizeof (u32); /////////////////////////////////////////////////////////////////////////////// // Logical function for sequence of rounds -static constexpr -uint32_t -f_00 (uint32_t B, uint32_t C, uint32_t D) -{ - return (B & C) | (~B & D); -} - - -//----------------------------------------------------------------------------- -static constexpr -uint32_t -f_20 (uint32_t B, uint32_t C, uint32_t D) -{ - return B ^ C ^ D; -} - - -//----------------------------------------------------------------------------- -static constexpr -uint32_t -f_40 (uint32_t B, uint32_t C, uint32_t D) -{ - return (B & C) | (B & D) | (C & D); -} - - -//----------------------------------------------------------------------------- -static constexpr -uint32_t -f_60 (uint32_t B, uint32_t C, uint32_t D) -{ - return B ^ C ^ D; -} +static constexpr u32 f_00 (u32 B, u32 C, u32 D) { return (B & C) | (~B & D); } +static constexpr u32 f_20 (u32 B, u32 C, u32 D) { return B ^ C ^ D; } +static constexpr u32 f_40 (u32 B, u32 C, u32 D) { return (B & C) | (B & D) | (C & D); } +static constexpr u32 f_60 (u32 B, u32 C, u32 D) { return B ^ C ^ D; } /////////////////////////////////////////////////////////////////////////////// static void -process (std::array &H, std::array &W) +process (std::array &H, std::array &W) { // Byteswap the raw input we have buffered ready for arithmetic std::transform (std::begin (W), std::end (W), std::begin (W), - [] (uint32_t x) { + [] (u32 x) { return cruft::ntoh (x); }); // Initialise the work buffer and the state variables - for (size_t t = 16; t < 80; ++t) + for (int t = 16; t < 80; ++t) W[t] = cruft::rotatel (W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1); - uint32_t A = H[0], + u32 A = H[0], B = H[1], C = H[2], D = H[3], E = H[4]; // Perform each of the four rounds -#define ROTATE_STATE(i) do { \ - uint32_t temp = cruft::rotatel (A, 5) + f_##i (B, C, D) + E + W[t] + K_##i; \ - E = D; \ - D = C; \ - C = cruft::rotatel (B, 30); \ - B = A; \ - A = temp; \ +#define ROTATE_STATE(i) do { \ + u32 temp = cruft::rotatel (A, 5) + f_##i (B, C, D) + E + W[t] + K_##i; \ + E = D; \ + D = C; \ + C = cruft::rotatel (B, 30); \ + B = A; \ + A = temp; \ } while (0) - for (size_t t = 0; t < 20; ++t) ROTATE_STATE(00); - for (size_t t = 20; t < 40; ++t) ROTATE_STATE(20); - for (size_t t = 40; t < 60; ++t) ROTATE_STATE(40); - for (size_t t = 60; t < 80; ++t) ROTATE_STATE(60); + for (int t = 0; t < 20; ++t) ROTATE_STATE(00); + for (int t = 20; t < 40; ++t) ROTATE_STATE(20); + for (int t = 40; t < 60; ++t) ROTATE_STATE(40); + for (int t = 60; t < 80; ++t) ROTATE_STATE(60); // Update the resulting hash state H[0] += A; @@ -131,7 +102,7 @@ process (std::array &H, std::array &W) /////////////////////////////////////////////////////////////////////////////// SHA1::digest_t -SHA1::operator() (cruft::view data) noexcept +SHA1::operator() (cruft::view data) noexcept { return (*this) (data, nullptr); } @@ -140,20 +111,20 @@ SHA1::operator() (cruft::view data) noexcept //----------------------------------------------------------------------------- SHA1::digest_t SHA1::operator() ( - cruft::view data_a, - cruft::view data_b + cruft::view data_a, + cruft::view data_b ) noexcept { /* RESET */ - uint64_t total = 0; + u64 total = 0; union { - std::array w; - std::array b; + std::array w; + std::array b; } H; union { - std::array c; - std::array W; + std::array c; + std::array W; } state; H.w = INITIAL_H; @@ -204,6 +175,6 @@ SHA1::operator() ( } /* DIGEST */ - std::transform (std::begin (H.w), std::end (H.w), std::begin (H.w), cruft::hton); + std::transform (std::begin (H.w), std::end (H.w), std::begin (H.w), cruft::hton); return H.b; }