26: m_blockSize(200 - 2 * (bits / 8)),
36 for (
size_t i = 0; i < StateSize; i++)
47 const unsigned int KeccakRounds = 24;
48 const uint64_t XorMasks[KeccakRounds] =
50 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
51 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
52 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
53 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
54 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
55 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
56 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
57 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
61 inline uint64_t rotateLeft(uint64_t x, uint8_t numBits)
63 return (x << numBits) | (x >> (64 - numBits));
67 inline uint64_t swap(uint64_t x)
69#if defined(__GNUC__) || defined(__clang__)
70 return __builtin_bswap64(x);
73 return _byteswap_uint64(x);
77 ((x >> 40) & 0x000000000000FF00ULL) |
78 ((x >> 24) & 0x0000000000FF0000ULL) |
79 ((x >> 8) & 0x00000000FF000000ULL) |
80 ((x << 8) & 0x000000FF00000000ULL) |
81 ((x << 24) & 0x0000FF0000000000ULL) |
82 ((x << 40) & 0x00FF000000000000ULL) |
88 unsigned int mod5(
unsigned int x)
99void Keccak::processBlock(
const void* data)
101#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
102#define LITTLEENDIAN(x) swap(x)
104#define LITTLEENDIAN(x) (x)
107 const uint64_t* data64 = (
const uint64_t*) data;
109 for (
unsigned int i = 0; i < m_blockSize / 8; i++)
113 for (
unsigned int round = 0; round < KeccakRounds; round++)
116 uint64_t coefficients[5];
117 for (
unsigned int i = 0; i < 5; i++)
118 coefficients[i] = m_hash[i] ^ m_hash[i + 5] ^ m_hash[i + 10] ^ m_hash[i + 15] ^ m_hash[i + 20];
120 for (
unsigned int i = 0; i < 5; i++)
122 uint64_t one = coefficients[mod5(i + 4)] ^ rotateLeft(coefficients[mod5(i + 1)], 1);
124 m_hash[i + 5] ^= one;
125 m_hash[i + 10] ^= one;
126 m_hash[i + 15] ^= one;
127 m_hash[i + 20] ^= one;
134 uint64_t last = m_hash[1];
135 one = m_hash[10]; m_hash[10] = rotateLeft(last, 1); last = one;
136 one = m_hash[ 7]; m_hash[ 7] = rotateLeft(last, 3); last = one;
137 one = m_hash[11]; m_hash[11] = rotateLeft(last, 6); last = one;
138 one = m_hash[17]; m_hash[17] = rotateLeft(last, 10); last = one;
139 one = m_hash[18]; m_hash[18] = rotateLeft(last, 15); last = one;
140 one = m_hash[ 3]; m_hash[ 3] = rotateLeft(last, 21); last = one;
141 one = m_hash[ 5]; m_hash[ 5] = rotateLeft(last, 28); last = one;
142 one = m_hash[16]; m_hash[16] = rotateLeft(last, 36); last = one;
143 one = m_hash[ 8]; m_hash[ 8] = rotateLeft(last, 45); last = one;
144 one = m_hash[21]; m_hash[21] = rotateLeft(last, 55); last = one;
145 one = m_hash[24]; m_hash[24] = rotateLeft(last, 2); last = one;
146 one = m_hash[ 4]; m_hash[ 4] = rotateLeft(last, 14); last = one;
147 one = m_hash[15]; m_hash[15] = rotateLeft(last, 27); last = one;
148 one = m_hash[23]; m_hash[23] = rotateLeft(last, 41); last = one;
149 one = m_hash[19]; m_hash[19] = rotateLeft(last, 56); last = one;
150 one = m_hash[13]; m_hash[13] = rotateLeft(last, 8); last = one;
151 one = m_hash[12]; m_hash[12] = rotateLeft(last, 25); last = one;
152 one = m_hash[ 2]; m_hash[ 2] = rotateLeft(last, 43); last = one;
153 one = m_hash[20]; m_hash[20] = rotateLeft(last, 62); last = one;
154 one = m_hash[14]; m_hash[14] = rotateLeft(last, 18); last = one;
155 one = m_hash[22]; m_hash[22] = rotateLeft(last, 39); last = one;
156 one = m_hash[ 9]; m_hash[ 9] = rotateLeft(last, 61); last = one;
157 one = m_hash[ 6]; m_hash[ 6] = rotateLeft(last, 20); last = one;
158 m_hash[ 1] = rotateLeft(last, 44);
161 for (
unsigned int j = 0; j < 25; j += 5)
164 uint64_t one = m_hash[j];
165 uint64_t two = m_hash[j + 1];
167 m_hash[j] ^= m_hash[j + 2] & ~two;
168 m_hash[j + 1] ^= m_hash[j + 3] & ~m_hash[j + 2];
169 m_hash[j + 2] ^= m_hash[j + 4] & ~m_hash[j + 3];
170 m_hash[j + 3] ^= one & ~m_hash[j + 4];
171 m_hash[j + 4] ^= two & ~one;
175 m_hash[0] ^= XorMasks[round];
183 const uint8_t* current = (
const uint8_t*) data;
185 if (m_bufferSize > 0)
187 while (numBytes > 0 && m_bufferSize < m_blockSize)
189 m_buffer[m_bufferSize++] = *current++;
195 if (m_bufferSize == m_blockSize)
197 processBlock((
void*)m_buffer);
198 m_numBytes += m_blockSize;
207 while (numBytes >= m_blockSize)
209 processBlock(current);
210 current += m_blockSize;
211 m_numBytes += m_blockSize;
212 numBytes -= m_blockSize;
218 m_buffer[m_bufferSize++] = *current++;
225void Keccak::processBuffer()
227 unsigned int blockSize = 200 - 2 * (m_bits / 8);
230 size_t offset = m_bufferSize;
232 m_buffer[offset++] = 1;
234 while (offset < blockSize)
235 m_buffer[offset++] = 0;
238 m_buffer[blockSize - 1] |= 0x80;
240 processBlock(m_buffer);
251 static const char dec2hex[16 + 1] =
"0123456789abcdef";
254 unsigned int hashLength = m_bits / 64;
257 for (
unsigned int i = 0; i < hashLength; i++)
258 for (
unsigned int j = 0; j < 8; j++)
261 unsigned char oneByte = (
unsigned char) (m_hash[i] >> (8 * j));
262 result += dec2hex[oneByte >> 4];
263 result += dec2hex[oneByte & 15];
267 unsigned int remainder = m_bits - hashLength * 64;
268 unsigned int processed = 0;
269 while (processed < remainder)
272 unsigned char oneByte = (
unsigned char) (m_hash[hashLength] >> processed);
273 result += dec2hex[oneByte >> 4];
274 result += dec2hex[oneByte & 15];
296 add(text.c_str(), text.size());
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
std::string operator()(const void *data, size_t numBytes)
compute hash of a memory block
std::string getHash()
return latest hash as hex characters
Keccak(Bits bits=Keccak256)
same as reset()
Third-party (vendored): Keccak (pre-standard SHA-3) hash from Stephan Brumme's portable hashing libra...
Usage: std::string msg = "The quick brown fox jumps over the lazy dog"; std::string key = "key"; std:...