-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlb_sha1.c
374 lines (305 loc) · 7.61 KB
/
mlb_sha1.c
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
/*
* mlb_sha1.c / 2013 Max Burke / Public Domain
*
* This module provides a SHA1 hash that works on buffers or on strings.
*
* You will need to define one of the symbols BIG_ENDIAN or LITTLE_ENDIAN
* for this to build.
*/
#ifdef _MSC_VER
# pragma warning(push, 0)
#endif
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#include "mlb_sha1.h"
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
# define inline
#endif
union uint64_to_bytes_t
{
uint64_t u8;
char bytes[8];
};
#if LITTLE_ENDIAN
static inline uint32_t
u32_to_big_endian(uint32_t p)
{
return ((p & 0x000000FF) << 24)
| ((p & 0x0000FF00) << 8)
| ((p & 0x00FF0000) >> 8)
| ((p & 0xFF000000) >> 24);
}
static inline void
write_length(char *ptr, uint64_t length)
{
union uint64_to_bytes_t c8;
size_t i;
length <<= 3;
c8.u8 = length;
for (i = 0; i < sizeof c8.bytes; ++i)
{
*ptr++ = c8.bytes[(sizeof c8.bytes) - i - 1];
}
}
#elif BIG_ENDIAN
# define u32_to_big_endian(x) (x)
static inline void
write_length(char *ptr, uint64_t length)
{
union uint64_to_bytes_t c8;
size_t i;
length <<= 3;
c8.u8 = length;
for (i = 0; i < sizeof c8.bytes; ++i)
{
*ptr++ = c8.bytes[i];
}
}
#else
# error Must define either M_LITTLE_ENDIAN to 1 or M_BIG_ENDIAN to 1
#endif
static inline uint32_t
left_rotate_1(uint32_t val)
{
return (val << 1) | (val >> 31);
}
static inline uint32_t
left_rotate_5(uint32_t val)
{
return (val << 5) | (val >> 27);
}
static inline uint32_t
left_rotate_30(uint32_t val)
{
return (val << 30) | (val >> 2);
}
static inline size_t
initialize_last_chunk(char last_chunk[128], const void *data, size_t length)
{
size_t remainder;
const char *ptr;
ptr = data;
remainder = length & 63;
memmove(last_chunk, ptr + (length & (~63)), remainder);
last_chunk[remainder] = (char)0x80;
if (remainder >= 55)
{
write_length(last_chunk + 120, length);
ptr = last_chunk;
return 2;
}
write_length(last_chunk + 56, length);
ptr = last_chunk;
return 1;
}
static inline struct mlb_sha1_hash_t
hash_chunk(struct mlb_sha1_hash_t h, const void *mem)
{
uint32_t buf[80];
int i;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t e;
uint32_t f;
uint32_t k;
uint32_t temp;
const uint32_t *ptr;
ptr = mem;
buf[0] = u32_to_big_endian(ptr[0]);
buf[1] = u32_to_big_endian(ptr[1]);
buf[2] = u32_to_big_endian(ptr[2]);
buf[3] = u32_to_big_endian(ptr[3]);
buf[4] = u32_to_big_endian(ptr[4]);
buf[5] = u32_to_big_endian(ptr[5]);
buf[6] = u32_to_big_endian(ptr[6]);
buf[7] = u32_to_big_endian(ptr[7]);
buf[8] = u32_to_big_endian(ptr[8]);
buf[9] = u32_to_big_endian(ptr[9]);
buf[10] = u32_to_big_endian(ptr[10]);
buf[11] = u32_to_big_endian(ptr[11]);
buf[12] = u32_to_big_endian(ptr[12]);
buf[13] = u32_to_big_endian(ptr[13]);
buf[14] = u32_to_big_endian(ptr[14]);
buf[15] = u32_to_big_endian(ptr[15]);
for (i = 16; i < 80; ++i)
{
buf[i] = left_rotate_1((buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16]));
}
a = h.h[0];
b = h.h[1];
c = h.h[2];
d = h.h[3];
e = h.h[4];
# define ITERATE() { \
temp = left_rotate_5(a) + f + e + k + buf[i]; \
e = d; \
d = c; \
c = left_rotate_30(b); \
b = a; \
a = temp; \
}
k = 0x5a827999;
for (i = 0; i < 20; ++i)
{
f = (b & c) | ((~b) & d);
ITERATE();
}
k = 0x6ed9eba1;
for (; i < 40; ++i)
{
f = b ^ c ^ d;
ITERATE();
}
k = 0x8f1bbcdc;
for (; i < 60; ++i)
{
f = (b & c) | (b & d) | (c & d);
ITERATE();
}
k = 0xca62c1d6;
for (; i < 80; ++i)
{
f = b ^ c ^ d;
ITERATE();
}
h.h[0] = h.h[0] + a;
h.h[1] = h.h[1] + b;
h.h[2] = h.h[2] + c;
h.h[3] = h.h[3] + d;
h.h[4] = h.h[4] + e;
return h;
}
static struct mlb_sha1_hash_t
mlb_sha1_set_initial_hash_values(void)
{
struct mlb_sha1_hash_t rv;
rv.h[0] = 0x67452301;
rv.h[1] = 0xefcdab89;
rv.h[2] = 0x98badcfe;
rv.h[3] = 0x10325476;
rv.h[4] = 0xc3d2e1f0;
return rv;
}
static void
mlb_sha1_reset_context_buf(struct mlb_sha1_hash_context_t *context)
{
memset(&context->buf[0], 0, sizeof context->buf);
context->buf_idx = 0;
}
void
mlb_sha1_hash_init(struct mlb_sha1_hash_context_t *context)
{
mlb_sha1_reset_context_buf(context);
context->hash = mlb_sha1_set_initial_hash_values();
}
static size_t
mlb_sha1_handle_leftover(struct mlb_sha1_hash_context_t *context, const void *data, size_t length)
{
char chunk[64];
size_t leftover_bytes;
size_t bytes_needed_for_chunk;
leftover_bytes = context->buf_idx;
if (leftover_bytes == 0)
{
return 0;
}
if (leftover_bytes + length < 64)
{
memcpy(&context->buf[leftover_bytes], data, length);
context->buf_idx = leftover_bytes + length;
return length;
}
assert(leftover_bytes < 64);
bytes_needed_for_chunk = 64 - leftover_bytes;
memcpy(&chunk[0], &context->buf[0], leftover_bytes);
memcpy(&chunk[leftover_bytes], data, bytes_needed_for_chunk);
context->hash = hash_chunk(context->hash, chunk);
mlb_sha1_reset_context_buf(context);
return bytes_needed_for_chunk;
}
void
mlb_sha1_hash_update(struct mlb_sha1_hash_context_t *context, const void *data, size_t length)
{
size_t i;
size_t num_chunks;
size_t leftover_bytes;
size_t remainder;
struct mlb_sha1_hash_t hash;
const char *string;
string = data;
leftover_bytes = mlb_sha1_handle_leftover(context, data, length);
hash = context->hash;
string += leftover_bytes;
length -= leftover_bytes;
if (length == 0)
{
return;
}
num_chunks = length / 64;
remainder = length & 63;
for (i = 0; i < num_chunks; ++i)
{
hash = hash_chunk(hash, string);
string += 64;
}
memcpy(&context->buf[0], string, remainder);
context->buf_idx = remainder;
}
struct mlb_sha1_hash_t
mlb_sha1_hash_finalize(struct mlb_sha1_hash_context_t *context)
{
const char *string;
char last_chunk[128];
size_t i;
size_t num_extra_chunks;
struct mlb_sha1_hash_t rv;
memset(last_chunk, 0, sizeof(last_chunk));
rv = context->hash;
string = last_chunk;
num_extra_chunks = initialize_last_chunk(last_chunk, context->buf, context->buf_idx);
for (i = 0; i < num_extra_chunks; ++i)
{
rv = hash_chunk(rv, string);
string += 64;
}
return rv;
}
struct mlb_sha1_hash_t
mlb_sha1_hash_buffer(const void *data, size_t length)
{
char last_chunk[128];
const char *string;
size_t i;
struct mlb_sha1_hash_t rv;
size_t num_chunks;
size_t num_extra_chunks;
string = data;
rv = mlb_sha1_set_initial_hash_values();
num_chunks = length / 64;
memset(last_chunk, 0, sizeof last_chunk);
num_extra_chunks = initialize_last_chunk(last_chunk, string, length);
for (i = 0; i < num_chunks; ++i)
{
rv = hash_chunk(rv, string);
string += 64;
}
string = last_chunk;
for (i = 0; i < num_extra_chunks; ++i)
{
rv = hash_chunk(rv, string);
string += 64;
}
return rv;
}
struct mlb_sha1_hash_t
mlb_sha1_hash_string(const char *string)
{
return mlb_sha1_hash_buffer(string, strlen(string));
}