sha1.c
6.16 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
/* NIST Secure Hash Algorithm */
/* heavily modified by Uwe Hollerbach uh@alumni.caltech edu */
/* from Peter C. Gutmann's implementation as found in */
/* Applied Cryptography by Bruce Schneier */
/* NIST's proposed modification to SHA of 7/11/94 may be */
/* activated by defining USE_MODIFIED_SHA */
#define USE_MODIFIED_SHA
//#define UNROLL_LOOPS
#ifdef __i386__
#define LITTLE_ENDIAN
#endif
#include "string.h"
#include "sha1.h"
#ifdef LITTLE_ENDIAN
#endif
/* SHA f()-functions */
#define f1(x,y,z) ((x & y) | (~x & z))
#define f2(x,y,z) (x ^ y ^ z)
#define f3(x,y,z) ((x & y) | (x & z) | (y & z))
#define f4(x,y,z) (x ^ y ^ z)
/* SHA constants */
#define CONST1 0x5a827999L
#define CONST2 0x6ed9eba1L
#define CONST3 0x8f1bbcdcL
#define CONST4 0xca62c1d6L
/* 32-bit rotate */
#define ROT32(x,n) ((x << n) | (x >> (32 - n)))
#define FUNC(n,i) \
temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
E = D; D = C; C = ROT32(B,30); B = A; A = temp
/* do SHA transformation */
static void SHA1Transform(SHA1Context *ctx)
{
int i;
SHA1_LONG temp, A, B, C, D, E, W[80];
for (i = 0; i < 16; ++i) {
W[i] = ctx->data[i];
}
for (i = 16; i < 80; ++i) {
W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
#ifdef USE_MODIFIED_SHA
W[i] = ROT32(W[i], 1);
#endif /* USE_MODIFIED_SHA */
}
A = ctx->digest[0];
B = ctx->digest[1];
C = ctx->digest[2];
D = ctx->digest[3];
E = ctx->digest[4];
#ifdef UNROLL_LOOPS
FUNC(1, 0); FUNC(1, 1); FUNC(1, 2); FUNC(1, 3); FUNC(1, 4);
FUNC(1, 5); FUNC(1, 6); FUNC(1, 7); FUNC(1, 8); FUNC(1, 9);
FUNC(1,10); FUNC(1,11); FUNC(1,12); FUNC(1,13); FUNC(1,14);
FUNC(1,15); FUNC(1,16); FUNC(1,17); FUNC(1,18); FUNC(1,19);
FUNC(2,20); FUNC(2,21); FUNC(2,22); FUNC(2,23); FUNC(2,24);
FUNC(2,25); FUNC(2,26); FUNC(2,27); FUNC(2,28); FUNC(2,29);
FUNC(2,30); FUNC(2,31); FUNC(2,32); FUNC(2,33); FUNC(2,34);
FUNC(2,35); FUNC(2,36); FUNC(2,37); FUNC(2,38); FUNC(2,39);
FUNC(3,40); FUNC(3,41); FUNC(3,42); FUNC(3,43); FUNC(3,44);
FUNC(3,45); FUNC(3,46); FUNC(3,47); FUNC(3,48); FUNC(3,49);
FUNC(3,50); FUNC(3,51); FUNC(3,52); FUNC(3,53); FUNC(3,54);
FUNC(3,55); FUNC(3,56); FUNC(3,57); FUNC(3,58); FUNC(3,59);
FUNC(4,60); FUNC(4,61); FUNC(4,62); FUNC(4,63); FUNC(4,64);
FUNC(4,65); FUNC(4,66); FUNC(4,67); FUNC(4,68); FUNC(4,69);
FUNC(4,70); FUNC(4,71); FUNC(4,72); FUNC(4,73); FUNC(4,74);
FUNC(4,75); FUNC(4,76); FUNC(4,77); FUNC(4,78); FUNC(4,79);
#else /* !UNROLL_LOOPS */
for (i = 0; i < 20; ++i) {
FUNC(1,i);
}
for (i = 20; i < 40; ++i) {
FUNC(2,i);
}
for (i = 40; i < 60; ++i) {
FUNC(3,i);
}
for (i = 60; i < 80; ++i) {
FUNC(4,i);
}
#endif /* !UNROLL_LOOPS */
ctx->digest[0] += A;
ctx->digest[1] += B;
ctx->digest[2] += C;
ctx->digest[3] += D;
ctx->digest[4] += E;
}
#ifdef LITTLE_ENDIAN
/* change endianness of data */
static void byte_reverse(SHA1_LONG *buffer, int count)
{
int i;
SHA1_BYTE ct[4], *cp;
count /= sizeof(SHA1_LONG);
cp = (SHA1_BYTE *) buffer;
for (i = 0; i < count; ++i) {
ct[0] = cp[0];
ct[1] = cp[1];
ct[2] = cp[2];
ct[3] = cp[3];
cp[0] = ct[3];
cp[1] = ct[2];
cp[2] = ct[1];
cp[3] = ct[0];
cp += sizeof(SHA1_LONG);
}
}
#endif /* LITTLE_ENDIAN */
/* initialize the SHA digest */
int SHA1Reset(SHA1Context *ctx)
{
ctx->digest[0] = 0x67452301L;
ctx->digest[1] = 0xefcdab89L;
ctx->digest[2] = 0x98badcfeL;
ctx->digest[3] = 0x10325476L;
ctx->digest[4] = 0xc3d2e1f0L;
ctx->count_lo = 0L;
ctx->count_hi = 0L;
return 0;
}
/* update the SHA digest */
int SHA1Input(SHA1Context *ctx, const SHA1_BYTE *buffer, int count)
{
int res = (ctx->count_lo>>3) & (SHA1_BLOCKSIZE-1);
if ((ctx->count_lo + ((SHA1_LONG) count << 3)) < ctx->count_lo) {
++ctx->count_hi;
}
ctx->count_lo += (SHA1_LONG) count << 3;
ctx->count_hi += (SHA1_LONG) count >> 29;
if (res && res+count >= SHA1_BLOCKSIZE) {
memcpy((SHA1_BYTE*)ctx->data+res, buffer, SHA1_BLOCKSIZE-res);
count += res;
buffer -= res;
res = 0;
goto process;
}
while (count >= SHA1_BLOCKSIZE) {
memcpy(ctx->data, buffer, SHA1_BLOCKSIZE);
process:
#ifdef LITTLE_ENDIAN
byte_reverse(ctx->data, SHA1_BLOCKSIZE);
#endif /* LITTLE_ENDIAN */
SHA1Transform(ctx);
buffer += SHA1_BLOCKSIZE;
count -= SHA1_BLOCKSIZE;
}
memcpy((SHA1_BYTE*)ctx->data+res, buffer, count);
return 0;
}
/* finish computing the SHA digest */
#ifdef __mips
#define memset(x,y,z) bzero(x,z)
extern void bzero(void *dst, size_t length);
#endif
int SHA1Result(SHA1Context *ctx, SHA1_BYTE digest[SHA1_DIGESTSIZE])
{
int count;
SHA1_LONG lo_bit_count, hi_bit_count;
lo_bit_count = ctx->count_lo;
hi_bit_count = ctx->count_hi;
count = (int) ((lo_bit_count >> 3) & 0x3f);
((SHA1_BYTE *) ctx->data)[count++] = 0x80;
if (count > 56) {
memset((SHA1_BYTE *) ctx->data + count, 0, 64 - count);
#ifdef LITTLE_ENDIAN
byte_reverse(ctx->data, SHA1_BLOCKSIZE);
#endif /* LITTLE_ENDIAN */
SHA1Transform(ctx);
memset(&ctx->data, 0, 56);
} else {
memset((SHA1_BYTE *) ctx->data + count, 0, 56 - count);
}
#ifdef LITTLE_ENDIAN
byte_reverse(ctx->data, SHA1_BLOCKSIZE);
#endif /* LITTLE_ENDIAN */
ctx->data[14] = hi_bit_count;
ctx->data[15] = lo_bit_count;
SHA1Transform(ctx);
#ifdef LITTLE_ENDIAN
byte_reverse(ctx->digest, SHA1_DIGESTSIZE);
#endif
memcpy(digest, ctx->digest, sizeof ctx->digest);
return 0;
}
#ifdef TEST
int main(int argc, char* argv[]) {
SHA1Context sha;
SHA1_BYTE digest[SHA1_DIGESTSIZE];
int i;
unsigned char x[63];
for(i = 0; i < sizeof x; i++)
x[i] = 'a';
SHA1Reset(&sha);
#if 1
for(i = 0; i < 1000000/sizeof x; i++)
SHA1Input(&sha, x, sizeof x);
if (1000000 % sizeof x)
SHA1Input(&sha, x, 1000000 % sizeof x);
#else
//SHA1Input(&sha, "abc", 3);
{
char *p = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
while(*p) {
SHA1Input(&sha, p, 4); p += 4;
}
}
#endif
SHA1Result(&sha, digest);
for(i = 0; i < 5; i++) printf("%08x\n", sha.digest[i]);
for(i = 0; i < SHA1_DIGESTSIZE; i++)
printf("%02x", digest[i]);
printf("\n");
}
#endif