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