-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsha1.h
177 lines (151 loc) · 5.12 KB
/
sha1.h
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
#pragma once
#include <algorithm>
#include <array>
#include <cstdint>
#include <string_view>
template <typename T>
inline constexpr std::uint8_t* bigendian_copy(T value, std::uint8_t* target) noexcept
{
std::size_t shift = (sizeof(T) - 1) * 8;
T mask = static_cast<T>(0xFF) << shift;
for (std::size_t i = 0; i < sizeof(T); ++i)
{
*(target++) = static_cast<std::uint8_t>((value & mask) >> shift);
mask >>= 8;
shift -= 8;
}
return target;
}
struct sha1
{
static constexpr std::size_t chunk_size_bits = 512;
static constexpr std::size_t chunk_size_bytes = chunk_size_bits / 8;
static constexpr std::size_t chunk_size_ints = chunk_size_bytes / 4;
constexpr void reset() noexcept
{
m_sizeBytes = 0;
m_nextChunkByte = 0;
m_state = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
}
constexpr void append(std::uint8_t const* data, std::uint64_t count) noexcept
{
while (count > 0)
{
auto bytesToCopy = (std::min)(count, chunk_size_bytes - m_nextChunkByte);
for (std::size_t i = 0; i < bytesToCopy; ++i)
{
append_byte(data[i]);
}
if (m_nextChunkByte == chunk_size_bytes)
{
process_chunk();
}
count -= bytesToCopy;
data += bytesToCopy;
}
}
void append(std::string_view str) noexcept
{
append(reinterpret_cast<std::uint8_t const*>(str.data()), str.length());
}
constexpr std::array<std::uint8_t, 20> finalize() noexcept
{
auto const sizeBits = m_sizeBytes * 8;
append_byte(0x80);
constexpr auto sizeOffset = chunk_size_bytes - 8;
while (m_nextChunkByte < sizeOffset)
{
append_byte(0);
}
// We need to append the length to the very end, which means that we may need to process a mostly empty
// additional chunk
if (m_nextChunkByte > sizeOffset)
{
while (m_nextChunkByte < chunk_size_bytes)
{
append_byte(0);
}
process_chunk();
}
m_currentChunk[chunk_size_ints - 2] = static_cast<std::uint32_t>(sizeBits >> 32);
m_currentChunk[chunk_size_ints - 1] = static_cast<std::uint32_t>(sizeBits);
m_nextChunkByte = chunk_size_bytes;
process_chunk();
std::array<std::uint8_t, 20> result = {};
auto dest = result.data();
for (auto value : m_state)
{
dest = bigendian_copy(value, dest);
}
reset();
return result;
}
private:
constexpr void append_byte(std::uint8_t byte) noexcept
{
auto index = static_cast<std::size_t>(m_nextChunkByte / 4);
auto pos = m_nextChunkByte % 4;
auto shift = (3 - pos) * 8;
m_currentChunk[index] |= (byte << shift);
++m_nextChunkByte;
++m_sizeBytes;
}
constexpr void process_chunk() noexcept
{
XLANG_ASSERT(m_nextChunkByte == chunk_size_bytes);
auto chunkState = m_state;
std::array<std::uint32_t, 80> w = {};
for (auto i = 0; i < 16; ++i) w[i] = m_currentChunk[i];
for (auto i = 16; i < 80; ++i)
{
w[i] = lrot(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
}
for (auto i = 0; i < 20; ++i)
{
auto f = (chunkState[1] & chunkState[2]) | (~chunkState[1] & chunkState[3]);
rotate(chunkState, w[i], f, 0x5A827999);
}
for (auto i = 20; i < 40; ++i)
{
auto f = chunkState[1] ^ chunkState[2] ^ chunkState[3];
rotate(chunkState, w[i], f, 0x6ED9EBA1);
}
for (auto i = 40; i < 60; ++i)
{
auto f = (chunkState[1] & chunkState[2]) | (chunkState[1] & chunkState[3]) | (chunkState[2] & chunkState[3]);
rotate(chunkState, w[i], f, 0x8F1BBCDC);
}
for (auto i = 60; i < 80; ++i)
{
auto f = chunkState[1] ^ chunkState[2] ^ chunkState[3];
rotate(chunkState, w[i], f, 0xCA62C1D6);
}
for (auto i = 0; i < 5; ++i)
{
m_state[i] += chunkState[i];
}
m_nextChunkByte = 0;
m_currentChunk = {};
}
template <typename T>
static constexpr T lrot(T value, std::size_t count) noexcept
{
using U = std::make_unsigned_t<T>;
auto result = value << count;
result |= static_cast<U>(value) >> ((sizeof(T) * 8) - count);
return result;
}
static constexpr void rotate(std::array<std::uint32_t, 5>& state, std::uint32_t value, std::uint32_t f, std::uint32_t k) noexcept
{
auto temp = lrot(state[0], 5) + state[4] + f + k + value;
state[4] = state[3];
state[3] = state[2];
state[2] = lrot(state[1], 30);
state[1] = state[0];
state[0] = temp;
}
std::uint64_t m_sizeBytes = 0;
std::array<std::uint32_t, 16> m_currentChunk = {};
std::uint64_t m_nextChunkByte = 0;
std::array<std::uint32_t, 5> m_state = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
};