-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsha1.h
284 lines (231 loc) · 6.66 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
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Evan Teran
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SHA1_20151007_H_
#define SHA1_20151007_H_
#include <algorithm>
#include <array>
#include <climits>
#include <cpp-utilities/bitwise.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
namespace hash {
class sha1 {
public:
struct state {
static constexpr int BlockSize = 64;
uint8_t block_[BlockSize]; // 512-bit message blocks
uint64_t length_ = 0; // message length in bits
std::size_t index_ = 0; // index into message block array
};
class digest {
friend class sha1;
public:
std::string to_string() const {
static const char hexchars[] = "0123456789abcdef";
std::string str;
str.reserve(40);
auto p = std::back_inserter(str);
for (int i = 0; i < 5; ++i) {
*p++ = hexchars[(h_[i] & 0xf0000000) >> 0x1c];
*p++ = hexchars[(h_[i] & 0x0f000000) >> 0x18];
*p++ = hexchars[(h_[i] & 0x00f00000) >> 0x14];
*p++ = hexchars[(h_[i] & 0x000f0000) >> 0x10];
*p++ = hexchars[(h_[i] & 0x0000f000) >> 0x0c];
*p++ = hexchars[(h_[i] & 0x00000f00) >> 0x08];
*p++ = hexchars[(h_[i] & 0x000000f0) >> 0x04];
*p++ = hexchars[(h_[i] & 0x0000000f) >> 0x00];
}
return str;
}
std::array<uint8_t, 20> bytes() const {
std::array<uint8_t, 20> b;
for (int i = 0; i < 5; ++i) {
b[3 + (i * 4)] = (h_[i] & 0x000000ff);
b[2 + (i * 4)] = (h_[i] & 0x0000ff00) >> 8;
b[1 + (i * 4)] = (h_[i] & 0x00ff0000) >> 16;
b[0 + (i * 4)] = (h_[i] & 0xff000000) >> 24;
}
return b;
}
bool operator==(const digest &rhs) const {
return std::memcmp(h_, rhs.h_, sizeof(h_)) == 0;
}
bool operator!=(const digest &rhs) const {
return !(*this == rhs);
}
private:
uint32_t h_[5] = {
0x67452301,
0xefcdab89,
0x98badcfe,
0x10325476,
0xc3d2e1f0,
};
};
public:
template <class In>
sha1(In first, In last) {
update(first, last);
}
sha1(const std::string &s) {
update(s);
}
sha1() = default;
sha1(const sha1 &other) = default;
sha1 &operator=(const sha1 &rhs) = default;
public:
template <class In>
sha1 &update(In first, In last) {
while (first != last) {
update(*first++);
}
return *this;
}
sha1 &update(uint8_t byte) {
state_.block_[state_.index_++] = byte;
state_.length_ += 8;
if (state_.index_ == state::BlockSize) {
process_block(&state_, &digest_);
}
return *this;
}
sha1 &update(const std::string &s) {
update(s.begin(), s.end());
return *this;
}
public:
void swap(sha1 &other) {
using std::swap;
swap(digest_, other.digest_);
swap(state_, other.state_);
}
void clear();
digest finalize() const {
// make copies so this isn't a mutating operation
state s = state_;
digest d = digest_;
s.block_[s.index_] = 0x80;
const size_t n = s.index_++;
if (n > 55) {
while (s.index_ < state::BlockSize) {
s.block_[s.index_++] = 0;
}
process_block(&s, &d);
}
while (s.index_ < 56) {
s.block_[s.index_++] = 0;
}
// Store the message length as the last 8 octets
s.block_[56] = (s.length_ >> 56) & 0xff;
s.block_[57] = (s.length_ >> 48) & 0xff;
s.block_[58] = (s.length_ >> 40) & 0xff;
s.block_[59] = (s.length_ >> 32) & 0xff;
s.block_[60] = (s.length_ >> 24) & 0xff;
s.block_[61] = (s.length_ >> 16) & 0xff;
s.block_[62] = (s.length_ >> 8) & 0xff;
s.block_[63] = (s.length_) & 0xff;
process_block(&s, &d);
return d;
}
private:
static void process_block(state *state, digest *digest) {
static constexpr uint32_t K[] = {
0x5a827999,
0x6ed9eba1,
0x8f1bbcdc,
0xca62c1d6};
uint32_t W[80]; // Word sequence
// Initialize the first 16 words in the array W
for (int t = 0; t < 16; ++t) {
W[t] = static_cast<uint32_t>(state->block_[t * 4 + 0]) << 24;
W[t] |= static_cast<uint32_t>(state->block_[t * 4 + 1]) << 16;
W[t] |= static_cast<uint32_t>(state->block_[t * 4 + 2]) << 8;
W[t] |= static_cast<uint32_t>(state->block_[t * 4 + 3]);
}
#if 0
for(int t = 16; t < 80; ++t) {
W[t] = bitwise::rotate_left(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
}
#else
// slight improvement if we attempt to do vectorization:
// http://software.intel.com/en-us/articles/improving-the-performance-of-the-secure-hash-algorithm-1/
for (int t = 16; t < 32; ++t) {
W[t] = bitwise::rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
}
for (int t = 32; t < 80; ++t) {
W[t] = bitwise::rotate_left(W[t - 6] ^ W[t - 16] ^ W[t - 28] ^ W[t - 32], 2);
}
#endif
uint32_t A = digest->h_[0];
uint32_t B = digest->h_[1];
uint32_t C = digest->h_[2];
uint32_t D = digest->h_[3];
uint32_t E = digest->h_[4];
for (int t = 0; t < 20; ++t) {
const uint32_t temp = bitwise::rotate_left(A, 5) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
E = D;
D = C;
C = bitwise::rotate_left(B, 30);
B = A;
A = temp;
}
for (int t = 20; t < 40; ++t) {
const uint32_t temp = bitwise::rotate_left(A, 5) + (B ^ C ^ D) + E + W[t] + K[1];
E = D;
D = C;
C = bitwise::rotate_left(B, 30);
B = A;
A = temp;
}
for (int t = 40; t < 60; ++t) {
const uint32_t temp = bitwise::rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
E = D;
D = C;
C = bitwise::rotate_left(B, 30);
B = A;
A = temp;
}
for (int t = 60; t < 80; ++t) {
const uint32_t temp = bitwise::rotate_left(A, 5) + (B ^ C ^ D) + E + W[t] + K[3];
E = D;
D = C;
C = bitwise::rotate_left(B, 30);
B = A;
A = temp;
}
digest->h_[0] += A;
digest->h_[1] += B;
digest->h_[2] += C;
digest->h_[3] += D;
digest->h_[4] += E;
state->index_ = 0;
}
private:
state state_;
digest digest_;
};
}
#endif