-
Notifications
You must be signed in to change notification settings - Fork 0
/
shs.c
305 lines (261 loc) · 5.94 KB
/
shs.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
/*
* Secure Hash Standard
* proposed NIST SHS
* coded for byte strings: number of bits is a multiple of 8
*
* Copyright (c) 1992, 1994 AT&T Bell Laboratories
* Coded by Jim Reeds 5 Feb 1992
* Enhanced by Jack Lacy 1993
*/
/*
* unsigned long qshs(char *s, int n);
*
* input:
* s character array to be hashed
* n length of s in BYTES
* output:
* return value: address of 5 unsigned longs holding hash
*
* machine dependencies:
* assumes a char is 8 bits
*/
/*
* passes test on:
* gauss (vax)
* 3k (cray)
* slepian (MIPS)
* bird (sparcstation II)
*/
#include <sys/types.h>
#include <stdio.h>
#include "shs.h"
static long nbits;
static unsigned long *h;
static unsigned long *w;
static void shs1();
/*
static void packl (unsigned long);
static void pack (unsigned char, unsigned char, unsigned char, unsigned char);
static void shs1(void);
static void opack(unsigned char);
*/
#define MASK (unsigned long)0xffffffffL /* in case more than 32 bits per long */
/*
* stick one byte into the current block; process the block when full
*/
static void opack(c)
unsigned char c;
{
int n32, nd32, shiftbits;
register unsigned long x, mask, y;
nd32 = (int)(nbits >> 5); /* nbits/32 */
n32 = (int)(nbits & 0x1f); /* nbits%32 */
shiftbits = 24-n32;
x = (unsigned long)(c<<shiftbits);
mask = (unsigned long)(0xff << shiftbits);
mask = ~mask;
y = w[nd32];
y = (y & mask) + x;
w[nd32] = y;
nbits += 8;
if(nbits==512){
nbits = 0;
shs1();
}
}
static void pack(c0, c1, c2, c3)
unsigned char c0, c1, c2, c3;
{
int nd32;
nd32 = (int)(nbits >> 5);
w[nd32] = (u_long)(((u_long)c0<<24) | ((u_long)c1<<16) | ((u_long)c2<<8) | (u_long)c3);
nbits += 32;
if(nbits==512){
nbits = 0;
shs1();
}
}
/*
* stick a 4 byte number into the current block
*/
static void
packl(x)
unsigned long x;
{
pack((unsigned char)(x>>24), (unsigned char)(x>>16),
(unsigned char)(x>>8), (unsigned char)(x>>0));
}
/*
* process one block
*/
static void
shs1()
{
unsigned long *wp;
unsigned long temp;
unsigned long A, B, C, D, E;
int t;
#define S(n,x) (u_long)(((x)<<(n))|((MASK&(x))>>(32-(n))))
wp = w;
t = 8;
do {
wp[16] = S(1, (u_long)(wp[13]^wp[8]^wp[2]^wp[0]));
wp[17] = S(1, (u_long)(wp[14]^wp[9]^wp[3]^wp[1]));
wp[18] = S(1, (u_long)(wp[15]^wp[10]^wp[4]^wp[2]));
wp[19] = S(1, (u_long)(wp[16]^wp[11]^wp[5]^wp[3]));
wp[20] = S(1, (u_long)(wp[17]^wp[12]^wp[6]^wp[4]));
wp[21] = S(1, (u_long)(wp[18]^wp[13]^wp[7]^wp[5]));
wp[22] = S(1, (u_long)(wp[19]^wp[14]^wp[8]^wp[6]));
wp[23] = S(1, (u_long)(wp[20]^wp[15]^wp[9]^wp[7]));
wp += 8;
t--;
} while (t > 0);
A = h[0];
B = h[1];
C = h[2];
D = h[3];
E = h[4];
t = 0;
while (t<20) {
temp = S(5,A) + E + w[t++];
temp += (unsigned long)0x5a827999L + ((B&C)|(D&~B));
E = D; D = C; C = S(30,B); B = A; A = temp;
}
while (t<40) {
temp = S(5,A) + E + w[t++];
temp += (unsigned long)0x6ed9eba1L + (B^C^D);
E = D; D = C; C = S(30,B); B = A; A = temp;
}
while (t<60) {
temp = S(5,A) + E + w[t++];
temp += (unsigned long)0x8f1bbcdcL + ((B&C)|(B&D)|(C&D));
E = D; D = C; C = S(30,B); B = A; A = temp;
}
while (t<80) {
temp = S(5,A) + E + w[t++];
temp += (unsigned long)0xca62c1d6L + (B^C^D);
E = D; D = C; C = S(30,B); B = A; A = temp;
}
h[0] = MASK&(h[0] + A);
h[1] = MASK&(h[1] + B);
h[2] = MASK&(h[2] + C);
h[3] = MASK&(h[3] + D);
h[4] = MASK&(h[4] + E);
}
#define CHARSTOLONG(wp,s,i) {*wp++ = (u_long)((((u_long)(s[i])&0xff)<<24)|(((u_long)(s[i+1])&0xff)<<16)|(((u_long)(s[i+2])&0xff)<<8)|(u_long)(s[i+3]&0xff));}
void
shsInit(mdContext)
SHS_CTX *mdContext;
{
nbits = 0;
mdContext->h[0] = (unsigned long)0x67452301L;
mdContext->h[1] = (unsigned long)0xefcdab89L;
mdContext->h[2] = (unsigned long)0x98badcfeL;
mdContext->h[3] = (unsigned long)0x10325476L;
mdContext->h[4] = (unsigned long)0xc3d2e1f0L;
mdContext->totalLength = 0;
}
void
shsUpdate(mdContext, s, n)
SHS_CTX *mdContext;
unsigned char *s;
unsigned int n;
{
register unsigned long *wp;
long nn = n;
long i;
w = mdContext->w;
h = mdContext->h;
mdContext->totalLength += n;
nbits = 0;
n = n/(u_long)64;
wp = w;
while(n>0){
CHARSTOLONG(wp,s,0);
CHARSTOLONG(wp,s,4);
CHARSTOLONG(wp,s,8);
CHARSTOLONG(wp,s,12);
CHARSTOLONG(wp,s,16);
CHARSTOLONG(wp,s,20);
CHARSTOLONG(wp,s,24);
CHARSTOLONG(wp,s,28);
CHARSTOLONG(wp,s,32);
CHARSTOLONG(wp,s,36);
CHARSTOLONG(wp,s,40);
CHARSTOLONG(wp,s,44);
CHARSTOLONG(wp,s,48);
CHARSTOLONG(wp,s,52);
CHARSTOLONG(wp,s,56);
CHARSTOLONG(wp,s,60);
n--;
wp = w;
s = (s + 64);
shs1();
}
i=nn%64;
while(i>3) {
CHARSTOLONG(wp,s,0);
s = (s + 4);
nbits += (u_long)32;
i -= 4;
}
while (i) {
opack((unsigned char)*s++);
i--;
}
}
void
shsFinal(mdContext)
SHS_CTX *mdContext;
{
long nn = mdContext->totalLength;
w = mdContext->w;
h = mdContext->h;
opack(128);
while(nbits != 448)opack(0);
packl((unsigned long)(nn>>29));
packl((unsigned long)(nn<<3));
/* if(nbits != 0)
handle_exception(CRITICAL,"shsFinal(): nbits != 0\n");*/
}
unsigned char *
qshs(s, n)
unsigned char *s;
long n;
{
SHS_CTX *mdContext;
static SHS_CTX mdC;
static unsigned char ret[20];
int i;
mdContext = &mdC;
shsInit(mdContext);
shsUpdate(mdContext, s, n);
shsFinal(mdContext);
for (i=0; i<5; i++) {
ret[i*4] = (mdContext->h[i]>>24)&0xff;
ret[i*4+1] = (mdContext->h[i]>>16)&0xff;
ret[i*4+2] = (mdContext->h[i]>>8)&0xff;
ret[i*4+3] = (mdContext->h[i])&0xff;
}
return ret;
}
/*int fread(char *, int, int, FILE *);*/
unsigned long *
fShsDigest(in)
FILE *in;
{
SHS_CTX *mdContext;
SHS_CTX mdC;
unsigned char buffer[1024];
long length, total;
mdContext = &mdC;
bzero(buffer, 1024);
total = 0;
shsInit(mdContext);
while ((length = fread(buffer, 1, 1024, in)) != 0) {
total += length;
shsUpdate(mdContext, buffer, length);
}
shsFinal(mdContext);
return mdContext->h;
}