CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
keccak.cpp
Go to the documentation of this file.
1
6// //////////////////////////////////////////////////////////
7// keccak.cpp
8// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
9// see http://create.stephan-brumme.com/disclaimer.html
10//
11
12#include "hash/keccak.h"
13
14// big endian architectures need #define __BYTE_ORDER __BIG_ENDIAN
15#ifndef _MSC_VER
16#ifndef __APPLE__
17#include <endian.h>
18#endif
19#endif
20
21// Added namespace to avoid clash with OpenSSL
22namespace hash {
23
26: m_blockSize(200 - 2 * (bits / 8)),
27 m_bits(bits)
28{
29 reset();
30}
31
32
35{
36 for (size_t i = 0; i < StateSize; i++)
37 m_hash[i] = 0;
38
39 m_numBytes = 0;
40 m_bufferSize = 0;
41}
42
43
45namespace
46{
47 const unsigned int KeccakRounds = 24;
48 const uint64_t XorMasks[KeccakRounds] =
49 {
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
58 };
59
61 inline uint64_t rotateLeft(uint64_t x, uint8_t numBits)
62 {
63 return (x << numBits) | (x >> (64 - numBits));
64 }
65
67 inline uint64_t swap(uint64_t x)
68 {
69#if defined(__GNUC__) || defined(__clang__)
70 return __builtin_bswap64(x);
71#endif
72#ifdef _MSC_VER
73 return _byteswap_uint64(x);
74#endif
75
76 return (x >> 56) |
77 ((x >> 40) & 0x000000000000FF00ULL) |
78 ((x >> 24) & 0x0000000000FF0000ULL) |
79 ((x >> 8) & 0x00000000FF000000ULL) |
80 ((x << 8) & 0x000000FF00000000ULL) |
81 ((x << 24) & 0x0000FF0000000000ULL) |
82 ((x << 40) & 0x00FF000000000000ULL) |
83 (x << 56);
84 }
85
86
88 unsigned int mod5(unsigned int x)
89 {
90 if (x < 5)
91 return x;
92
93 return x - 5;
94 }
95}
96
97
99void Keccak::processBlock(const void* data)
100{
101#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
102#define LITTLEENDIAN(x) swap(x)
103#else
104#define LITTLEENDIAN(x) (x)
105#endif
106
107 const uint64_t* data64 = (const uint64_t*) data;
108 // mix data into state
109 for (unsigned int i = 0; i < m_blockSize / 8; i++)
110 m_hash[i] ^= LITTLEENDIAN(data64[i]);
111
112 // re-compute state
113 for (unsigned int round = 0; round < KeccakRounds; round++)
114 {
115 // Theta
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];
119
120 for (unsigned int i = 0; i < 5; i++)
121 {
122 uint64_t one = coefficients[mod5(i + 4)] ^ rotateLeft(coefficients[mod5(i + 1)], 1);
123 m_hash[i ] ^= one;
124 m_hash[i + 5] ^= one;
125 m_hash[i + 10] ^= one;
126 m_hash[i + 15] ^= one;
127 m_hash[i + 20] ^= one;
128 }
129
130 // temporary
131 uint64_t one;
132
133 // Rho Pi
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);
159
160 // Chi
161 for (unsigned int j = 0; j < 25; j += 5)
162 {
163 // temporaries
164 uint64_t one = m_hash[j];
165 uint64_t two = m_hash[j + 1];
166
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;
172 }
173
174 // Iota
175 m_hash[0] ^= XorMasks[round];
176 }
177}
178
179
181void Keccak::add(const void* data, size_t numBytes)
182{
183 const uint8_t* current = (const uint8_t*) data;
184
185 if (m_bufferSize > 0)
186 {
187 while (numBytes > 0 && m_bufferSize < m_blockSize)
188 {
189 m_buffer[m_bufferSize++] = *current++;
190 numBytes--;
191 }
192 }
193
194 // full buffer
195 if (m_bufferSize == m_blockSize)
196 {
197 processBlock((void*)m_buffer);
198 m_numBytes += m_blockSize;
199 m_bufferSize = 0;
200 }
201
202 // no more data ?
203 if (numBytes == 0)
204 return;
205
206 // process full blocks
207 while (numBytes >= m_blockSize)
208 {
209 processBlock(current);
210 current += m_blockSize;
211 m_numBytes += m_blockSize;
212 numBytes -= m_blockSize;
213 }
214
215 // keep remaining bytes in buffer
216 while (numBytes > 0)
217 {
218 m_buffer[m_bufferSize++] = *current++;
219 numBytes--;
220 }
221}
222
223
225void Keccak::processBuffer()
226{
227 unsigned int blockSize = 200 - 2 * (m_bits / 8);
228
229 // add padding
230 size_t offset = m_bufferSize;
231 // add a "1" byte
232 m_buffer[offset++] = 1;
233 // fill with zeros
234 while (offset < blockSize)
235 m_buffer[offset++] = 0;
236
237 // and add a single set bit
238 m_buffer[blockSize - 1] |= 0x80;
239
240 processBlock(m_buffer);
241}
242
243
245std::string Keccak::getHash()
246{
247 // process remaining bytes
248 processBuffer();
249
250 // convert hash to string
251 static const char dec2hex[16 + 1] = "0123456789abcdef";
252
253 // number of significant elements in hash (uint64_t)
254 unsigned int hashLength = m_bits / 64;
255
256 std::string result;
257 for (unsigned int i = 0; i < hashLength; i++)
258 for (unsigned int j = 0; j < 8; j++) // 64 bits => 8 bytes
259 {
260 // convert a byte to hex
261 unsigned char oneByte = (unsigned char) (m_hash[i] >> (8 * j));
262 result += dec2hex[oneByte >> 4];
263 result += dec2hex[oneByte & 15];
264 }
265
266 // Keccak224's last entry in m_hash provides only 32 bits instead of 64 bits
267 unsigned int remainder = m_bits - hashLength * 64;
268 unsigned int processed = 0;
269 while (processed < remainder)
270 {
271 // convert a byte to hex
272 unsigned char oneByte = (unsigned char) (m_hash[hashLength] >> processed);
273 result += dec2hex[oneByte >> 4];
274 result += dec2hex[oneByte & 15];
275
276 processed += 8;
277 }
278
279 return result;
280}
281
282
284std::string Keccak::operator()(const void* data, size_t numBytes)
285{
286 reset();
287 add(data, numBytes);
288 return getHash();
289}
290
291
293std::string Keccak::operator()(const std::string& text)
294{
295 reset();
296 add(text.c_str(), text.size());
297 return getHash();
298}
299
300} // namespace hash
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
Definition keccak.cpp:181
std::string operator()(const void *data, size_t numBytes)
compute hash of a memory block
Definition keccak.cpp:284
std::string getHash()
return latest hash as hex characters
Definition keccak.cpp:245
Keccak(Bits bits=Keccak256)
same as reset()
Definition keccak.cpp:25
void reset()
restart
Definition keccak.cpp:34
Bits
algorithm variants
Definition keccak.h:47
#define LITTLEENDIAN(x)
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:...
Definition crc32.h:28