1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
This code is written by kerukuro and released into public domain.
*/
#ifndef DIGESTPP_PROVIDERS_WHIRLPOOL_HPP
#define DIGESTPP_PROVIDERS_WHIRLPOOL_HPP
#include "../../detail/functions.hpp"
#include "../../detail/absorb_data.hpp"
#include "constants/whirlpool_constants.hpp"
#include <array>
namespace digestpp
{
namespace detail
{
namespace whirlpool_functions
{
template<int a>
static inline uint64_t G(const uint64_t* ll)
{
return whirlpool_constants<void>::T[0][static_cast<unsigned char>(ll[a % 8])]
^ whirlpool_constants<void>::T[1][static_cast<unsigned char>(ll[(a - 1) % 8] >> 8)]
^ whirlpool_constants<void>::T[2][static_cast<unsigned char>(ll[(a - 2) % 8] >> 16)]
^ whirlpool_constants<void>::T[3][static_cast<unsigned char>(ll[(a - 3) % 8] >> 24)]
^ whirlpool_constants<void>::T[4][static_cast<unsigned char>(ll[(a - 4) % 8] >> 32)]
^ whirlpool_constants<void>::T[5][static_cast<unsigned char>(ll[(a - 5) % 8] >> 40)]
^ whirlpool_constants<void>::T[6][static_cast<unsigned char>(ll[(a - 6) % 8] >> 48)]
^ whirlpool_constants<void>::T[7][static_cast<unsigned char>(ll[(a - 7) % 8] >> 56)];
}
}
class whirlpool_provider
{
public:
static const bool is_xof = false;
whirlpool_provider()
{
}
~whirlpool_provider()
{
clear();
}
inline void init()
{
pos = 0;
total = 0;
memset(&h[0], 0, sizeof(uint64_t)*8);
}
inline void update(const unsigned char* data, size_t len)
{
detail::absorb_bytes(data, len, 64, 64, m.data(), pos, total,
[this](const unsigned char* data, size_t len) { transform(data, len); });
}
inline void final(unsigned char* hash)
{
total += pos * 8;
uint64_t mlen = byteswap(total);
m[pos++] = 0x80;
if (pos > 32)
{
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(m.data(), 1);
pos = 0;
}
memset(&m[pos], 0, 56 - pos);
memcpy(&m[64 - 8], &mlen, 64 / 8);
transform(m.data(), 1);
memcpy(hash, h.data(), hash_size() / 8);
}
inline void clear()
{
zero_memory(h);
zero_memory(m);
}
inline size_t hash_size() const { return 512; }
private:
inline void transform(const unsigned char* mp, size_t num_blks)
{
for (uint64_t b = 0; b < num_blks; b++)
{
uint64_t K[8], state[8];
memcpy(K, h.data(), sizeof(K));
for (int i = 0; i < 8; ++i)
state[i] = h[i] ^ (reinterpret_cast<const uint64_t*>(mp)[i]);
for (int r = 0; r < 10; ++r)
{
uint64_t L[8];
L[0] = whirlpool_functions::G<0 + 8>(K) ^ whirlpool_constants<void>::RC[r];
L[1] = whirlpool_functions::G<1 + 8>(K);
L[2] = whirlpool_functions::G<2 + 8>(K);
L[3] = whirlpool_functions::G<3 + 8>(K);
L[4] = whirlpool_functions::G<4 + 8>(K);
L[5] = whirlpool_functions::G<5 + 8>(K);
L[6] = whirlpool_functions::G<6 + 8>(K);
L[7] = whirlpool_functions::G<7 + 8>(K);
memcpy(K, L, sizeof(L));
L[0] ^= whirlpool_functions::G<0 + 8>(state);
L[1] ^= whirlpool_functions::G<1 + 8>(state);
L[2] ^= whirlpool_functions::G<2 + 8>(state);
L[3] ^= whirlpool_functions::G<3 + 8>(state);
L[4] ^= whirlpool_functions::G<4 + 8>(state);
L[5] ^= whirlpool_functions::G<5 + 8>(state);
L[6] ^= whirlpool_functions::G<6 + 8>(state);
L[7] ^= whirlpool_functions::G<7 + 8>(state);
memcpy(state, L, sizeof(L));
}
for (int i = 0; i < 8; ++i)
h[i] ^= state[i] ^ reinterpret_cast<const uint64_t*>(mp)[i];
mp += 64;
}
}
std::array<uint64_t, 8> h;
std::array<unsigned char, 64> m;
size_t pos;
uint64_t total;
};
} // namespace detail
} // namespace digestpp
#endif
|