forked from jasinb/sha1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1.c
196 lines (163 loc) · 4.79 KB
/
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
#include <assert.h>
#include <ctype.h>
#include "sha1.h"
#define ALIGN(x,a) (((x)+(a)-1ULL)&~((a)-1ULL))
static UINT32 rotl32(UINT32 x, int b)
{
return (x << b) | (x >> (32-b));
}
// switch endianness
static UINT32 get32 (const void* p)
{
const UINT8 *x = (const UINT8*)p;
return (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3];
}
static UINT32 f (int t, UINT32 b, UINT32 c, UINT32 d)
{
assert(0 <= t && t < 80);
if (t < 20)
return (b & c) | ((~b) & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
//if (t < 80)
return b ^ c ^ d;
}
static void processBlock (const UINT32* block, UINT32* h)
{
static const UINT32 k[4] =
{
0x5A827999,
0x6ED9EBA1,
0x8F1BBCDC,
0xCA62C1D6
};
UINT32 w[80];
UINT32 a = h[0];
UINT32 b = h[1];
UINT32 c = h[2];
UINT32 d = h[3];
UINT32 e = h[4];
int t;
for (t = 0; t < 16; t++)
w[t] = get32(block++);
/*
for (; t < 80; t++)
w[t] = rotl32(w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16], 1);
*/
for (t = 0; t < 80; t++)
{
int s = t & 0xf;
UINT32 temp;
if (t >= 16)
w[s] = rotl32(w[(s + 13) & 0xf] ^ w[(s + 8) & 0xf] ^ w[(s + 2) & 0xf] ^ w[s], 1);
temp = rotl32(a, 5) + f(t, b,c,d) + e + w[s] + k[t/20];
e = d; d = c; c = rotl32(b, 30); b = a; a = temp;
/*
UINT32 temp = rotl32(a, 5) + f(t, b,c,d) + e + w[t] + k[t/20];
e = d;
d = c;
c = rotl32(b, 30);
b = a;
a = temp;
*/
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
}
static int padMsg (const void* msg, UINT64 bytes, UINT8* lastBlock)
{
int msgBytesInLast = (int)(bytes & 63);
int extraBlocks = (msgBytesInLast + 9) > 64 ? 2 : 1;
int numZeroBytes = extraBlocks * 64 - 9 - msgBytesInLast;
// fill remaining msg bytes
const UINT8* msgLast = (UINT8*)msg + (bytes & ~63);
while (msgBytesInLast--)
*lastBlock++ = *msgLast++;
// separator
*lastBlock++ = 0x80;
while (numZeroBytes--)
*lastBlock++ = 0;
// original length in bits (!), switch endianness
bytes *= 8;
*lastBlock++ = (UINT8)(bytes >> 56 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 48 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 40 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 32 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 24 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 16 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 8 & 0xff);
*lastBlock++ = (UINT8)(bytes >> 0 & 0xff);
return extraBlocks;
}
SHA1_Digest SHA1_get (const void* msg, UINT64 bytes)
{
SHA1_Digest digest;
UINT32 h[5] =
{
0x67452301,
0xefcdab89,
0x98badcfe,
0x10325476,
0xc3d2e1f0
};
UINT64 totalBlocks = ALIGN(bytes + 9, 64) / 64; // including padding
const UINT32* block = (const UINT32*)msg;
UINT64 b;
// we could assume that msg is always required to
// hold padding, but let's not
UINT8 lastBlocks[128]; // either one or two blocks
int numLast = padMsg(msg, bytes, lastBlocks);
for (b = 0; b < totalBlocks-numLast; b++)
{
processBlock(block, h);
block += 16;
}
// process last block
for (b = 0; b < numLast; b++)
processBlock((const UINT32*)(lastBlocks + b*64), h);
*((UINT32*)&digest.digest[0]) = get32(&h[0]);
*((UINT32*)&digest.digest[4]) = get32(&h[1]);
*((UINT32*)&digest.digest[8]) = get32(&h[2]);
*((UINT32*)&digest.digest[12]) = get32(&h[3]);
*((UINT32*)&digest.digest[16]) = get32(&h[4]);
return digest;
}
SHA1_Digest SHA1_Digest_fromStr (const char* src)
{
// \todo why does msvc require this initializer to not issue C4701 ?
SHA1_Digest d = { "" };
int i;
assert(src); // also, src must be at least 40 bytes
for (i = 0; i < 20; i++)
{
// \todo just use atoi or something
int c0 = tolower(*src++);
int c1 = tolower(*src++);
c0 = '0' <= c0 && c0 <= '9' ? c0 - '0' : ('a' <= c0 && c0 <= 'f' ? 0xa + c0 - 'a' : -1);
c1 = '0' <= c1 && c1 <= '9' ? c1 - '0' : ('a' <= c1 && c1 <= 'f' ? 0xa + c1 - 'a' : -1);
d.digest[i] = (UINT8)((c0 << 4) | c1);
}
return d;
}
void SHA1_Digest_toStr (const SHA1_Digest* digest, char* dst)
{
int i;
assert(digest && dst); // dst must be at least 41 bytes (terminator)
for (i = 0; i < 20; i++)
{
int c0 = digest->digest[i] >> 4;
int c1 = digest->digest[i] & 0xf;
assert(0 <= c0 && c0 <= 0xf);
assert(0 <= c1 && c1 <= 0xf);
c0 = c0 <= 9 ? '0' + c0 : 'a' + c0 - 0xa;
c1 = c1 <= 9 ? '0' + c1 : 'a' + c1 - 0xa;
*dst++ = (char)c0;
*dst++ = (char)c1;
}
*dst = '\0';
}