-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha1.cpp
More file actions
390 lines (327 loc) · 11.3 KB
/
sha1.cpp
File metadata and controls
390 lines (327 loc) · 11.3 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "sha1.hpp"
#include <algorithm>
#include <bit>
#include <cstring>
#include <future>
#ifdef ATOM_USE_BOOST
#include <boost/endian/conversion.hpp>
#endif
namespace atom::algorithm {
SHA1::SHA1() noexcept {
reset();
// Check if CPU supports SIMD instructions
#ifdef __AVX2__
useSIMD_ = true;
spdlog::debug("SHA1: Using AVX2 SIMD acceleration");
#else
spdlog::debug("SHA1: Using standard implementation (no SIMD)");
#endif
}
void SHA1::update(std::span<const u8> data) noexcept {
update(data.data(), data.size());
}
void SHA1::update(const u8* data, usize length) {
// Input validation
if (!data && length > 0) {
spdlog::error("SHA1: Null data pointer with non-zero length");
throw std::invalid_argument("Null data pointer with non-zero length");
}
usize remaining = length;
usize offset = 0;
while (remaining > 0) {
usize bufferOffset = (bitCount_ / 8) % BLOCK_SIZE;
usize bytesToFill = BLOCK_SIZE - bufferOffset;
usize bytesToCopy = std::min(remaining, bytesToFill);
// Use std::memcpy for better performance
std::memcpy(buffer_.data() + bufferOffset, data + offset, bytesToCopy);
offset += bytesToCopy;
remaining -= bytesToCopy;
bitCount_ += bytesToCopy * BITS_PER_BYTE;
if (bufferOffset + bytesToCopy == BLOCK_SIZE) {
// Choose between SIMD or standard processing method
#ifdef __AVX2__
if (useSIMD_) {
processBlockSIMD(buffer_.data());
} else {
processBlock(buffer_.data());
}
#else
processBlock(buffer_.data());
#endif
}
}
}
auto SHA1::digest() noexcept -> std::array<u8, SHA1::DIGEST_SIZE> {
u64 bitLength = bitCount_;
// Backup current state to ensure digest() operation doesn't affect object
// state
auto hashCopy = hash_;
auto bufferCopy = buffer_;
auto bitCountCopy = bitCount_;
// Padding
usize bufferOffset = (bitCountCopy / 8) % BLOCK_SIZE;
bufferCopy[bufferOffset] = PADDING_BYTE; // Append the bit '1'
// Fill the rest of the buffer with zeros
std::fill(bufferCopy.begin() + bufferOffset + 1,
bufferCopy.begin() + BLOCK_SIZE, 0);
if (bufferOffset >= BLOCK_SIZE - LENGTH_SIZE) {
// Process current block, create new block for storing length
processBlock(bufferCopy.data());
std::fill(bufferCopy.begin(), bufferCopy.end(), 0);
}
// Use C++20 bit operations to handle byte order
if constexpr (std::endian::native == std::endian::little) {
// Convert on little endian systems
bitLength = ((bitLength & 0xff00000000000000ULL) >> 56) |
((bitLength & 0x00ff000000000000ULL) >> 40) |
((bitLength & 0x0000ff0000000000ULL) >> 24) |
((bitLength & 0x000000ff00000000ULL) >> 8) |
((bitLength & 0x00000000ff000000ULL) << 8) |
((bitLength & 0x0000000000ff0000ULL) << 24) |
((bitLength & 0x000000000000ff00ULL) << 40) |
((bitLength & 0x00000000000000ffULL) << 56);
}
// Append message length
std::memcpy(bufferCopy.data() + BLOCK_SIZE - LENGTH_SIZE, &bitLength,
LENGTH_SIZE);
processBlock(bufferCopy.data());
// Generate final hash value
std::array<u8, DIGEST_SIZE> result;
for (usize i = 0; i < HASH_SIZE; ++i) {
u32 value = hashCopy[i];
if constexpr (std::endian::native == std::endian::little) {
// Byte order conversion needed on little endian systems
value = ((value & 0xff000000) >> 24) | ((value & 0x00ff0000) >> 8) |
((value & 0x0000ff00) << 8) | ((value & 0x000000ff) << 24);
}
std::memcpy(&result[i * 4], &value, 4);
}
return result;
}
auto SHA1::digestAsString() noexcept -> std::string {
return bytesToHex(digest());
}
void SHA1::reset() noexcept {
bitCount_ = 0;
hash_[0] = 0x67452301;
hash_[1] = 0xEFCDAB89;
hash_[2] = 0x98BADCFE;
hash_[3] = 0x10325476;
hash_[4] = 0xC3D2E1F0;
buffer_.fill(0);
}
void SHA1::processBlock(const u8* block) noexcept {
std::array<u32, SCHEDULE_SIZE> schedule{};
// Use C++20 bit operations to handle byte order
for (usize i = 0; i < 16; ++i) {
if constexpr (std::endian::native == std::endian::little) {
// Byte order conversion needed on little endian systems
const u8* ptr = block + i * 4;
schedule[i] = static_cast<u32>(ptr[0]) << 24 |
static_cast<u32>(ptr[1]) << 16 |
static_cast<u32>(ptr[2]) << 8 |
static_cast<u32>(ptr[3]);
} else {
// Direct copy on big endian systems
std::memcpy(&schedule[i], block + i * 4, 4);
}
}
// Calculate message schedule
for (usize i = 16; i < SCHEDULE_SIZE; ++i) {
schedule[i] = rotateLeft(schedule[i - 3] ^ schedule[i - 8] ^
schedule[i - 14] ^ schedule[i - 16],
1);
}
u32 a = hash_[0];
u32 b = hash_[1];
u32 c = hash_[2];
u32 d = hash_[3];
u32 e = hash_[4];
// Optimized main loop - unroll first 20 iterations
for (usize i = 0; i < 20; ++i) {
u32 f = (b & c) | (~b & d);
u32 k = 0x5A827999;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
// Next 20 iterations
for (usize i = 20; i < 40; ++i) {
u32 f = b ^ c ^ d;
u32 k = 0x6ED9EBA1;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
// Next 20 iterations
for (usize i = 40; i < 60; ++i) {
u32 f = (b & c) | (b & d) | (c & d);
u32 k = 0x8F1BBCDC;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
// Last 20 iterations
for (usize i = 60; i < 80; ++i) {
u32 f = b ^ c ^ d;
u32 k = 0xCA62C1D6;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
hash_[0] += a;
hash_[1] += b;
hash_[2] += c;
hash_[3] += d;
hash_[4] += e;
}
#ifdef __AVX2__
void SHA1::processBlockSIMD(const u8* block) noexcept {
// AVX2 optimized block processing
std::array<u32, SCHEDULE_SIZE> schedule{};
// Use SIMD to load data
for (usize i = 0; i < 16; i += 4) {
const u8* ptr = block + i * 4;
__m128i data = _mm_loadu_si128(reinterpret_cast<const __m128i*>(ptr));
// Handle byte order
if constexpr (std::endian::native == std::endian::little) {
const __m128i mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11, 4,
5, 6, 7, 0, 1, 2, 3);
data = _mm_shuffle_epi8(data, mask);
}
_mm_storeu_si128(reinterpret_cast<__m128i*>(&schedule[i]), data);
}
// Use AVX2 instructions for parallel message schedule calculation
for (usize i = 16; i < SCHEDULE_SIZE; i += 8) {
__m256i w1 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(&schedule[i - 3]));
__m256i w2 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(&schedule[i - 8]));
__m256i w3 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(&schedule[i - 14]));
__m256i w4 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(&schedule[i - 16]));
__m256i result = _mm256_xor_si256(w1, w2);
result = _mm256_xor_si256(result, w3);
result = _mm256_xor_si256(result, w4);
// Rotate left by 1 bit
const __m256i mask = _mm256_set1_epi32(0x01);
__m256i shift_left = _mm256_slli_epi32(result, 1);
__m256i shift_right = _mm256_srli_epi32(result, 31);
result = _mm256_or_si256(shift_left, shift_right);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(&schedule[i]), result);
}
// Start standard main loop from here
u32 a = hash_[0];
u32 b = hash_[1];
u32 c = hash_[2];
u32 d = hash_[3];
u32 e = hash_[4];
// Main loop same as in standard processBlock
for (usize i = 0; i < 20; ++i) {
u32 f = (b & c) | (~b & d);
u32 k = 0x5A827999;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
for (usize i = 20; i < 40; ++i) {
u32 f = b ^ c ^ d;
u32 k = 0x6ED9EBA1;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
for (usize i = 40; i < 60; ++i) {
u32 f = (b & c) | (b & d) | (c & d);
u32 k = 0x8F1BBCDC;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
for (usize i = 60; i < 80; ++i) {
u32 f = b ^ c ^ d;
u32 k = 0xCA62C1D6;
u32 temp = rotateLeft(a, 5) + f + e + k + schedule[i];
e = d;
d = c;
c = rotateLeft(b, 30);
b = a;
a = temp;
}
hash_[0] += a;
hash_[1] += b;
hash_[2] += c;
hash_[3] += d;
hash_[4] += e;
}
#endif
template <usize N>
auto bytesToHex(const std::array<u8, N>& bytes) noexcept -> std::string {
static constexpr char HEX_CHARS[] = "0123456789abcdef";
std::string result(N * 2, ' ');
for (usize i = 0; i < N; ++i) {
result[i * 2] = HEX_CHARS[(bytes[i] >> 4) & 0xF];
result[i * 2 + 1] = HEX_CHARS[bytes[i] & 0xF];
}
return result;
}
template <>
auto bytesToHex<SHA1::DIGEST_SIZE>(
const std::array<u8, SHA1::DIGEST_SIZE>& bytes) noexcept -> std::string {
static constexpr char HEX_CHARS[] = "0123456789abcdef";
std::string result(SHA1::DIGEST_SIZE * 2, ' ');
for (usize i = 0; i < SHA1::DIGEST_SIZE; ++i) {
result[i * 2] = HEX_CHARS[(bytes[i] >> 4) & 0xF];
result[i * 2 + 1] = HEX_CHARS[bytes[i] & 0xF];
}
return result;
}
template <ByteContainer... Containers>
auto computeHashesInParallel(const Containers&... containers)
-> std::vector<std::array<u8, SHA1::DIGEST_SIZE>> {
std::vector<std::array<u8, SHA1::DIGEST_SIZE>> results;
results.reserve(sizeof...(Containers));
auto hashComputation =
[](const auto& container) -> std::array<u8, SHA1::DIGEST_SIZE> {
SHA1 hasher;
hasher.update(container);
return hasher.digest();
};
std::vector<std::future<std::array<u8, SHA1::DIGEST_SIZE>>> futures;
futures.reserve(sizeof...(Containers));
spdlog::debug("Starting parallel hash computation for {} containers",
sizeof...(Containers));
// Launch all computation tasks
(futures.push_back(
std::async(std::launch::async, hashComputation, containers)),
...);
// Collect results
for (auto& future : futures) {
results.push_back(future.get());
}
spdlog::debug("Completed parallel hash computation");
return results;
}
} // namespace atom::algorithm