hash/blake2: avoid copying hash state from compression function

This commit is contained in:
Danny Robson 2019-02-04 20:34:57 +11:00
parent b6477214ad
commit 485cc52a7b

View File

@ -81,8 +81,8 @@ G (word_t v[16], int a, int b, int c, int d, word_t x, word_t y)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// compression function // compression function
static std::array<word_t,8> static void
F (std::array<word_t,8> h, const word_t m[16], u64 t, bool f) F (std::array<word_t,8> &h, const word_t m[16], u64 t, bool const f)
{ {
word_t v[16] { word_t v[16] {
h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
@ -119,8 +119,6 @@ F (std::array<word_t,8> h, const word_t m[16], u64 t, bool f)
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
h[i] ^= v[i] ^ v[i + 8]; h[i] ^= v[i] ^ v[i + 8];
return h;
} }
@ -153,20 +151,20 @@ blake2::operator() (cruft::view<const u08 *> data) const noexcept
h[0] ^= 0x01010000 ^ (m_keylen << 8) ^ sizeof (digest_t); h[0] ^= 0x01010000 ^ (m_keylen << 8) ^ sizeof (digest_t);
if (m_keylen) if (m_keylen)
h = F (h, reinterpret_cast<const word_t*> (m_salt.data ()), ::traits::block_bytes, data.empty ()); F (h, reinterpret_cast<const word_t*> (m_salt.data ()), ::traits::block_bytes, data.empty ());
// special case for the empty key and empty data // special case for the empty key and empty data
if (!m_keylen && data.empty ()) { if (!m_keylen && data.empty ()) {
std::array<word_t,16> zeroes {}; std::array<word_t,16> zeroes {};
h = F (h, zeroes.data (), 0, true); F (h, zeroes.data (), 0, true);
} }
u64 counter = m_keylen?::traits::block_bytes:0; u64 counter = m_keylen ? ::traits::block_bytes : 0;
auto cursor = data.begin (); auto cursor = data.begin ();
while (cursor + ::traits::block_bytes < data.end ()) { while (cursor + ::traits::block_bytes < data.end ()) {
counter += ::traits::block_bytes; counter += ::traits::block_bytes;
h = F (h, reinterpret_cast<const word_t*> (cursor), counter, false); F (h, reinterpret_cast<const word_t*> (cursor), counter, false);
cursor += ::traits::block_bytes; cursor += ::traits::block_bytes;
} }
@ -174,7 +172,7 @@ blake2::operator() (cruft::view<const u08 *> data) const noexcept
std::array<u64,16> tail {}; std::array<u64,16> tail {};
memcpy (tail.data(), data.data (), data.cend () - cursor); memcpy (tail.data(), data.data (), data.cend () - cursor);
counter += data.end () - cursor; counter += data.end () - cursor;
h = F (h, tail.data (), counter, true); F (h, tail.data (), counter, true);
} }
digest_t d; digest_t d;