Fix compiler error due to uninitialized variable
[clumanager.git] / librhcm / sha1.c
blob1be2057f043ce329f14f854e3895aa5714acd603
1 /**
2 * @file SHA1 message digest algorithm.
4 * SHA-1 in C
5 * By Steve Reid <sreid@sea-to-sky.net>
6 * 100% Public Domain
8 * -----------------
9 * Modified 7/98
10 * By James H. Brown <jbrown@burgoyne.com>
11 * Still 100% Public Domain
13 * Corrected a problem which generated improper hash values on 16 bit machines
14 * Routine SHA1Update changed from
15 * void SHA1Update(SHA1_CTX* context, uint8_t* data, unsigned int
16 * len)
17 * to
18 * void SHA1Update(SHA1_CTX* context, uint8_t* data, unsigned
19 * long len)
21 * The 'len' parameter was declared an int which works fine on 32 bit machines.
22 * However, on 16 bit machines an int is too small for the shifts being done
23 * against
24 * it. This caused the hash function to generate incorrect values if len was
25 * greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
27 * Since the file IO in main() reads 16K at a time, any file 8K or larger would
28 * be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
29 * "a"s).
31 * I also changed the declaration of variables i & j in SHA1Update to
32 * unsigned long from unsigned int for the same reason.
34 * These changes should make no difference to any 32 bit implementations since
35 * an
36 * int and a long are the same size in those environments.
38 * --
39 * I also corrected a few compiler warnings generated by Borland C.
40 * 1. Added #include <process.h> for exit() prototype
41 * 2. Removed unused variable 'j' in SHA1Final
42 * 3. Changed exit(0) to return(0) at end of main.
44 * ALL changes I made can be located by searching for comments containing 'JHB'
45 * -----------------
46 * Modified 8/98
47 * By Steve Reid <sreid@sea-to-sky.net>
48 * Still 100% public domain
50 * 1- Removed #include <process.h> and used return() instead of exit()
51 * 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
52 * 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
54 * -----------------
55 * Modified 4/01
56 * By Saul Kravitz <Saul.Kravitz@celera.com>
57 * Still 100% PD
58 * Modified to run on Compaq Alpha hardware.
59 * Modified for use with Red Hat Cluster Manager <lhh at redhat.com> 10/9/2003
63 * Test Vectors (from FIPS PUB 180-1)
64 * "abc"
65 * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
66 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
67 * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
68 * A million repetitions of "a"
69 * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
72 #include <stdio.h>
73 #include <string.h>
74 #include <platform.h>
75 #include <sha1.h>
77 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
79 /* blk0() and blk() perform the initial expand. */
80 /* I got the idea of expanding during the round function from SSLeay */
82 #if __BYTE_ORDER == __LITTLE_ENDIAN
83 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
84 |(rol(block->l[i],8)&0x00FF00FF))
85 #else
86 #define blk0(i) block->l[i]
87 #endif
89 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
90 ^block->l[(i+2)&15]^block->l[i&15],1))
92 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
93 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
94 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
95 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
96 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
97 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
99 #ifdef DEBUG /* SAK */
100 void
101 SHAPrintContext(SHA1_CTX * context, char *msg)
103 printf("%s (%d,%d) %x %x %x %x %x\n",
104 msg,
105 context->count[0], context->count[1],
106 context->state[0],
107 context->state[1],
108 context->state[2], context->state[3], context->state[4]);
110 #endif
112 /* Hash a single 512-bit block. This is the core of the algorithm. */
114 void
115 SHA1Transform(uint32_t state[5], uint8_t buffer[64])
117 uint32_t a, b, c, d, e;
118 typedef union {
119 uint8_t c[64];
120 uint32_t l[16];
121 } CHAR64LONG16;
122 CHAR64LONG16 *block;
123 block = (CHAR64LONG16 *) buffer;
124 /* Copy context->state[] to working vars */
125 a = state[0];
126 b = state[1];
127 c = state[2];
128 d = state[3];
129 e = state[4];
130 /* 4 rounds of 20 operations each. Loop unrolled. */
131 R0(a, b, c, d, e, 0);
132 R0(e, a, b, c, d, 1);
133 R0(d, e, a, b, c, 2);
134 R0(c, d, e, a, b, 3);
135 R0(b, c, d, e, a, 4);
136 R0(a, b, c, d, e, 5);
137 R0(e, a, b, c, d, 6);
138 R0(d, e, a, b, c, 7);
139 R0(c, d, e, a, b, 8);
140 R0(b, c, d, e, a, 9);
141 R0(a, b, c, d, e, 10);
142 R0(e, a, b, c, d, 11);
143 R0(d, e, a, b, c, 12);
144 R0(c, d, e, a, b, 13);
145 R0(b, c, d, e, a, 14);
146 R0(a, b, c, d, e, 15);
147 R1(e, a, b, c, d, 16);
148 R1(d, e, a, b, c, 17);
149 R1(c, d, e, a, b, 18);
150 R1(b, c, d, e, a, 19);
151 R2(a, b, c, d, e, 20);
152 R2(e, a, b, c, d, 21);
153 R2(d, e, a, b, c, 22);
154 R2(c, d, e, a, b, 23);
155 R2(b, c, d, e, a, 24);
156 R2(a, b, c, d, e, 25);
157 R2(e, a, b, c, d, 26);
158 R2(d, e, a, b, c, 27);
159 R2(c, d, e, a, b, 28);
160 R2(b, c, d, e, a, 29);
161 R2(a, b, c, d, e, 30);
162 R2(e, a, b, c, d, 31);
163 R2(d, e, a, b, c, 32);
164 R2(c, d, e, a, b, 33);
165 R2(b, c, d, e, a, 34);
166 R2(a, b, c, d, e, 35);
167 R2(e, a, b, c, d, 36);
168 R2(d, e, a, b, c, 37);
169 R2(c, d, e, a, b, 38);
170 R2(b, c, d, e, a, 39);
171 R3(a, b, c, d, e, 40);
172 R3(e, a, b, c, d, 41);
173 R3(d, e, a, b, c, 42);
174 R3(c, d, e, a, b, 43);
175 R3(b, c, d, e, a, 44);
176 R3(a, b, c, d, e, 45);
177 R3(e, a, b, c, d, 46);
178 R3(d, e, a, b, c, 47);
179 R3(c, d, e, a, b, 48);
180 R3(b, c, d, e, a, 49);
181 R3(a, b, c, d, e, 50);
182 R3(e, a, b, c, d, 51);
183 R3(d, e, a, b, c, 52);
184 R3(c, d, e, a, b, 53);
185 R3(b, c, d, e, a, 54);
186 R3(a, b, c, d, e, 55);
187 R3(e, a, b, c, d, 56);
188 R3(d, e, a, b, c, 57);
189 R3(c, d, e, a, b, 58);
190 R3(b, c, d, e, a, 59);
191 R4(a, b, c, d, e, 60);
192 R4(e, a, b, c, d, 61);
193 R4(d, e, a, b, c, 62);
194 R4(c, d, e, a, b, 63);
195 R4(b, c, d, e, a, 64);
196 R4(a, b, c, d, e, 65);
197 R4(e, a, b, c, d, 66);
198 R4(d, e, a, b, c, 67);
199 R4(c, d, e, a, b, 68);
200 R4(b, c, d, e, a, 69);
201 R4(a, b, c, d, e, 70);
202 R4(e, a, b, c, d, 71);
203 R4(d, e, a, b, c, 72);
204 R4(c, d, e, a, b, 73);
205 R4(b, c, d, e, a, 74);
206 R4(a, b, c, d, e, 75);
207 R4(e, a, b, c, d, 76);
208 R4(d, e, a, b, c, 77);
209 R4(c, d, e, a, b, 78);
210 R4(b, c, d, e, a, 79);
211 /* Add the working vars back into context.state[] */
212 state[0] += a;
213 state[1] += b;
214 state[2] += c;
215 state[3] += d;
216 state[4] += e;
217 /* Wipe variables */
218 a = b = c = d = e = 0;
223 * Initialize new context.
225 * @param context Context to initialize.
227 void
228 SHA1Init(SHA1_CTX * context)
230 /* SHA1 initialization constants */
231 context->state[0] = 0x67452301;
232 context->state[1] = 0xEFCDAB89;
233 context->state[2] = 0x98BADCFE;
234 context->state[3] = 0x10325476;
235 context->state[4] = 0xC3D2E1F0;
236 context->count[0] = context->count[1] = 0;
241 * Run your data through this.
243 * @param context Initialized or previously updated context.
244 * @param data Your data.
245 * @param len Your data's length.
247 void
248 SHA1Update(SHA1_CTX * context, uint8_t * data, uint32_t len)
250 uint32_t i, j; /* JHB */
252 #ifdef DEBUG
253 SHAPrintContext(context, "before");
254 #endif
255 j = (context->count[0] >> 3) & 63;
256 if ((context->count[0] += len << 3) < (len << 3))
257 context->count[1]++;
258 context->count[1] += (len >> 29);
259 if ((j + len) > 63) {
260 memcpy(&context->buffer[j], data, (i = 64 - j));
261 SHA1Transform(context->state, context->buffer);
262 for (; i + 63 < len; i += 64) {
263 SHA1Transform(context->state, &data[i]);
265 j = 0;
266 } else
267 i = 0;
268 memcpy(&context->buffer[j], &data[i], len - i);
269 #ifdef DEBUG
270 SHAPrintContext(context, "after ");
271 #endif
276 * Add padding and return the message digest.
278 * @param digest Pre-allocated array which will contain the
279 * message digest.
280 * @param context SHA1 context on which to generate the
281 * digest.
283 void
284 SHA1Final(uint8_t digest[20], SHA1_CTX * context)
286 uint32_t i; /* JHB */
287 uint8_t finalcount[8];
289 for (i = 0; i < 8; i++) {
290 /* Endian independent */
291 finalcount[i] = (uint8_t) ((context->count[(i >= 4 ? 0 : 1)]
292 >> ((3 - (i & 3)) * 8)) & 255);
295 SHA1Update(context, (uint8_t *) "\200", 1);
296 while ((context->count[0] & 504) != 448) {
297 SHA1Update(context, (uint8_t *) "\0", 1);
300 /* Should cause a SHA1Transform() */
301 SHA1Update(context, finalcount, 8);
302 for (i = 0; i < 20; i++) {
303 digest[i] = (uint8_t)
304 ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
307 /* Wipe variables */
308 i = 0; /* JHB */
309 memset(context->buffer, 0, 64);
310 memset(context->state, 0, 20);
311 memset(context->count, 0, 8);
312 memset(finalcount, 0, 8); /* SWR */
316 void
317 clu_sha1(char *buf, uint32_t buflen, char *sha1sum)
319 SHA1_CTX sha1_ctx;
321 SHA1Init(&sha1_ctx);
322 SHA1Update(&sha1_ctx, (uint8_t *)buf, buflen);
323 SHA1Final((uint8_t*)sha1sum, &sha1_ctx);
326 /*************************************************************/
328 #ifdef STANDALONE
330 main(int argc, char **argv)
332 int i, j;
333 SHA1_CTX context;
334 uint8_t digest[20], buffer[16384];
335 FILE *file;
337 if (argc > 2) {
338 puts("Public domain SHA-1 implementation - by Steve Reid"
339 " <sreid@sea-to-sky.net>");
340 puts("Modified for 16 bit environments 7/98 - by James H."
341 " Brown <jbrown@burgoyne.com>"); /* JHB */
342 puts("Modified for Red Hat Cluster Manager - 10/2003 by"
343 " Lon Hohberger");
344 puts("Produces the SHA-1 hash of a file, or stdin if no "
345 "file is specified.");
346 return (0);
348 if (argc < 2) {
349 file = stdin;
350 } else {
351 if (!(file = fopen(argv[1], "rb"))) {
352 fputs("Unable to open file.", stderr);
353 return (-1);
356 SHA1Init(&context);
357 while (!feof(file)) { /* note: what if ferror(file) */
358 i = fread(buffer, 1, 16384, file);
359 SHA1Update(&context, buffer, i);
361 SHA1Final(digest, &context);
362 fclose(file);
363 for (i = 0; i < 5; i++) {
364 for (j = 0; j < 4; j++) {
365 printf("%02X", digest[i * 4 + j]);
367 putchar(' ');
369 putchar('\n');
370 return (0); /* JHB */
372 #endif