sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
sha3.cpp
1// //////////////////////////////////////////////////////////
2// sha3.cpp
3// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
4// see http://create.stephan-brumme.com/disclaimer.html
5//
6
7#include "sha3.h"
8
9// big endian architectures need #define __BYTE_ORDER __BIG_ENDIAN
10#if defined(__PS2__)
11 #include <machine/endian.h>
12#elif !defined(_MSC_VER)
13 #include <endian.h>
14#endif
15
16#include <iostream>
17
18
21: m_blockSize(200 - 2 * (bits / 8)),
22 m_bits(bits)
23{
24 reset();
25}
26
27
30{
31 for (size_t i = 0; i < StateSize; i++)
32 m_hash[i] = 0;
33
34 m_numBytes = 0;
35 m_bufferSize = 0;
36}
37
38
40namespace
41{
42 const unsigned int Rounds = 24;
43 const uint64_t XorMasks[Rounds] =
44 {
45 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
46 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
47 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
48 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
49 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
50 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
51 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
52 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
53 };
54
56 inline uint64_t rotateLeft(uint64_t x, uint8_t numBits)
57 {
58 return (x << numBits) | (x >> (64 - numBits));
59 }
60
62 inline uint64_t swap(uint64_t x)
63 {
64#if defined(__GNUC__) || defined(__clang__)
65 return __builtin_bswap64(x);
66#endif
67#ifdef _MSC_VER
68 return _byteswap_uint64(x);
69#endif
70
71 return (x >> 56) |
72 ((x >> 40) & 0x000000000000FF00ULL) |
73 ((x >> 24) & 0x0000000000FF0000ULL) |
74 ((x >> 8) & 0x00000000FF000000ULL) |
75 ((x << 8) & 0x000000FF00000000ULL) |
76 ((x << 24) & 0x0000FF0000000000ULL) |
77 ((x << 40) & 0x00FF000000000000ULL) |
78 (x << 56);
79 }
80
81
83 unsigned int mod5(unsigned int x)
84 {
85 if (x < 5)
86 return x;
87
88 return x - 5;
89 }
90}
91
92
94void SHA3::processBlock(const void* data)
95{
96#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
97#define LITTLEENDIAN(x) swap(x)
98#else
99#define LITTLEENDIAN(x) (x)
100#endif
101
102 const uint64_t* data64 = (const uint64_t*) data;
103 // mix data into state
104 for (unsigned int i = 0; i < m_blockSize / 8; i++)
105 m_hash[i] ^= LITTLEENDIAN(data64[i]);
106
107 // re-compute state
108 for (unsigned int round = 0; round < Rounds; round++)
109 {
110 // Theta
111 uint64_t coefficients[5];
112 for (unsigned int i = 0; i < 5; i++)
113 coefficients[i] = m_hash[i] ^ m_hash[i + 5] ^ m_hash[i + 10] ^ m_hash[i + 15] ^ m_hash[i + 20];
114
115 for (unsigned int i = 0; i < 5; i++)
116 {
117 uint64_t one = coefficients[mod5(i + 4)] ^ rotateLeft(coefficients[mod5(i + 1)], 1);
118 m_hash[i ] ^= one;
119 m_hash[i + 5] ^= one;
120 m_hash[i + 10] ^= one;
121 m_hash[i + 15] ^= one;
122 m_hash[i + 20] ^= one;
123 }
124
125 // temporary
126 uint64_t one;
127
128 // Rho Pi
129 uint64_t last = m_hash[1];
130 one = m_hash[10]; m_hash[10] = rotateLeft(last, 1); last = one;
131 one = m_hash[ 7]; m_hash[ 7] = rotateLeft(last, 3); last = one;
132 one = m_hash[11]; m_hash[11] = rotateLeft(last, 6); last = one;
133 one = m_hash[17]; m_hash[17] = rotateLeft(last, 10); last = one;
134 one = m_hash[18]; m_hash[18] = rotateLeft(last, 15); last = one;
135 one = m_hash[ 3]; m_hash[ 3] = rotateLeft(last, 21); last = one;
136 one = m_hash[ 5]; m_hash[ 5] = rotateLeft(last, 28); last = one;
137 one = m_hash[16]; m_hash[16] = rotateLeft(last, 36); last = one;
138 one = m_hash[ 8]; m_hash[ 8] = rotateLeft(last, 45); last = one;
139 one = m_hash[21]; m_hash[21] = rotateLeft(last, 55); last = one;
140 one = m_hash[24]; m_hash[24] = rotateLeft(last, 2); last = one;
141 one = m_hash[ 4]; m_hash[ 4] = rotateLeft(last, 14); last = one;
142 one = m_hash[15]; m_hash[15] = rotateLeft(last, 27); last = one;
143 one = m_hash[23]; m_hash[23] = rotateLeft(last, 41); last = one;
144 one = m_hash[19]; m_hash[19] = rotateLeft(last, 56); last = one;
145 one = m_hash[13]; m_hash[13] = rotateLeft(last, 8); last = one;
146 one = m_hash[12]; m_hash[12] = rotateLeft(last, 25); last = one;
147 one = m_hash[ 2]; m_hash[ 2] = rotateLeft(last, 43); last = one;
148 one = m_hash[20]; m_hash[20] = rotateLeft(last, 62); last = one;
149 one = m_hash[14]; m_hash[14] = rotateLeft(last, 18); last = one;
150 one = m_hash[22]; m_hash[22] = rotateLeft(last, 39); last = one;
151 one = m_hash[ 9]; m_hash[ 9] = rotateLeft(last, 61); last = one;
152 one = m_hash[ 6]; m_hash[ 6] = rotateLeft(last, 20); last = one;
153 m_hash[ 1] = rotateLeft(last, 44);
154
155 // Chi
156 for (unsigned int j = 0; j < StateSize; j += 5)
157 {
158 // temporaries
159 uint64_t one = m_hash[j];
160 uint64_t two = m_hash[j + 1];
161
162 m_hash[j] ^= m_hash[j + 2] & ~two;
163 m_hash[j + 1] ^= m_hash[j + 3] & ~m_hash[j + 2];
164 m_hash[j + 2] ^= m_hash[j + 4] & ~m_hash[j + 3];
165 m_hash[j + 3] ^= one & ~m_hash[j + 4];
166 m_hash[j + 4] ^= two & ~one;
167 }
168
169 // Iota
170 m_hash[0] ^= XorMasks[round];
171 }
172}
173
174
176void SHA3::add(const void* data, size_t numBytes)
177{
178 const uint8_t* current = (const uint8_t*) data;
179
180 // copy data to buffer
181 if (m_bufferSize > 0)
182 {
183 while (numBytes > 0 && m_bufferSize < m_blockSize)
184 {
185 m_buffer[m_bufferSize++] = *current++;
186 numBytes--;
187 }
188 }
189
190 // full buffer
191 if (m_bufferSize == m_blockSize)
192 {
193 processBlock((void*)m_buffer);
194 m_numBytes += m_blockSize;
195 m_bufferSize = 0;
196 }
197
198 // no more data ?
199 if (numBytes == 0)
200 return;
201
202 // process full blocks
203 while (numBytes >= m_blockSize)
204 {
205 processBlock(current);
206 current += m_blockSize;
207 m_numBytes += m_blockSize;
208 numBytes -= m_blockSize;
209 }
210
211 // keep remaining bytes in buffer
212 while (numBytes > 0)
213 {
214 m_buffer[m_bufferSize++] = *current++;
215 numBytes--;
216 }
217}
218
219
221void SHA3::processBuffer()
222{
223 // add padding
224 size_t offset = m_bufferSize;
225 // add a "1" byte
226 m_buffer[offset++] = 0x06;
227 // fill with zeros
228 while (offset < m_blockSize)
229 m_buffer[offset++] = 0;
230
231 // and add a single set bit
232 m_buffer[offset - 1] |= 0x80;
233
234 processBlock(m_buffer);
235}
236
237
239std::string SHA3::getHash()
240{
241 // save hash state
242 uint64_t oldHash[StateSize];
243 for (unsigned int i = 0; i < StateSize; i++)
244 oldHash[i] = m_hash[i];
245
246 // process remaining bytes
247 processBuffer();
248
249 // convert hash to string
250 static const char dec2hex[16 + 1] = "0123456789abcdef";
251
252 // number of significant elements in hash (uint64_t)
253 unsigned int hashLength = m_bits / 64;
254
255 std::string result;
256 result.reserve(m_bits / 4);
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 // SHA3-224'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 // restore state
280 for (unsigned int i = 0; i < StateSize; i++)
281 m_hash[i] = oldHash[i];
282
283 return result;
284}
285
286
288std::string SHA3::operator()(const void* data, size_t numBytes)
289{
290 reset();
291 add(data, numBytes);
292 return getHash();
293}
294
295
297std::string SHA3::operator()(const std::string& text)
298{
299 reset();
300 add(text.c_str(), text.size());
301 return getHash();
302}
void reset()
restart
Definition sha3.cpp:29
std::string operator()(const void *data, size_t numBytes)
compute hash of a memory block
Definition sha3.cpp:288
Bits
algorithm variants
Definition sha3.h:44
SHA3(Bits bits=Bits256)
same as reset()
Definition sha3.cpp:20
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
Definition sha3.cpp:176
std::string getHash()
return latest hash as hex characters
Definition sha3.cpp:239