5 * Paul E. Jones <paulej@arid.us>
8 * This software is licensed as "freeware." Permission to distribute
9 * this software in source and binary forms is hereby granted without
10 * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED
11 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING
14 * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING,
15 * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE.
17 *****************************************************************************
18 * $Id: sha1.c,v 1.2 2004/03/27 18:00:33 paulej Exp $
19 *****************************************************************************
22 * This file implements the Secure Hashing Standard as defined
23 * in FIPS PUB 180-1 published April 17, 1995.
25 * The Secure Hashing Standard, which uses the Secure Hashing
26 * Algorithm (SHA), produces a 160-bit message digest for a
27 * given data stream. In theory, it is highly improbable that
28 * two messages will produce the same message digest. Therefore,
29 * this algorithm can serve as a means of providing a "fingerprint"
33 * SHA-1 is defined in terms of 32-bit "words". This code was
34 * written with the expectation that the processor has at least
35 * a 32-bit machine word size. If the machine word size is larger,
36 * the code should still function properly. One caveat to that
37 * is that the input functions taking characters and character
38 * arrays assume that only 8 bits of information are stored in each
42 * SHA-1 is designed to work with messages less than 2^64 bits
43 * long. Although SHA-1 allows a message digest to be generated for
44 * messages of any number of bits less than 2^64, this
45 * implementation only works with messages with a length that is a
46 * multiple of the size of an 8-bit character.
53 * Define the circular shift macro
55 #define SHA1CircularShift(bits,word) \
56 ((((word) << (bits)) & 0xFFFFFFFF) | \
57 ((word) >> (32-(bits))))
59 /* Function prototypes */
60 void SHA1ProcessMessageBlock(SHA1Context
*);
61 void SHA1PadMessage(SHA1Context
*);
67 * This function will initialize the SHA1Context in preparation
68 * for computing a new message digest.
72 * The context to reset.
80 void SHA1Reset(SHA1Context
*context
)
82 context
->Length_Low
= 0;
83 context
->Length_High
= 0;
84 context
->Message_Block_Index
= 0;
86 context
->Message_Digest
[0] = 0x67452301;
87 context
->Message_Digest
[1] = 0xEFCDAB89;
88 context
->Message_Digest
[2] = 0x98BADCFE;
89 context
->Message_Digest
[3] = 0x10325476;
90 context
->Message_Digest
[4] = 0xC3D2E1F0;
92 context
->Computed
= 0;
93 context
->Corrupted
= 0;
100 * This function will return the 160-bit message digest into the
101 * Message_Digest array within the SHA1Context provided
105 * The context to use to calculate the SHA-1 hash.
108 * 1 if successful, 0 if it failed.
113 int SHA1Result(SHA1Context
*context
)
116 if (context
->Corrupted
)
121 if (!context
->Computed
)
123 SHA1PadMessage(context
);
124 context
->Computed
= 1;
134 * This function accepts an array of octets as the next portion of
139 * The SHA-1 context to update
140 * message_array: [in]
141 * An array of characters representing the next portion of the
144 * The length of the message in message_array
152 void SHA1Input( SHA1Context
*context
,
153 const unsigned char *message_array
,
161 if (context
->Computed
|| context
->Corrupted
)
163 context
->Corrupted
= 1;
167 while(length
-- && !context
->Corrupted
)
169 context
->Message_Block
[context
->Message_Block_Index
++] =
170 (*message_array
& 0xFF);
172 context
->Length_Low
+= 8;
173 /* Force it to 32 bits */
174 context
->Length_Low
&= 0xFFFFFFFF;
175 if (context
->Length_Low
== 0)
177 context
->Length_High
++;
178 /* Force it to 32 bits */
179 context
->Length_High
&= 0xFFFFFFFF;
180 if (context
->Length_High
== 0)
182 /* Message is too long */
183 context
->Corrupted
= 1;
187 if (context
->Message_Block_Index
== 64)
189 SHA1ProcessMessageBlock(context
);
197 * SHA1ProcessMessageBlock
200 * This function will process the next 512 bits of the message
201 * stored in the Message_Block array.
210 * Many of the variable names in the SHAContext, especially the
211 * single character names, were used because those were the names
212 * used in the publication.
216 void SHA1ProcessMessageBlock(SHA1Context
*context
)
218 const unsigned K
[] = /* Constants defined in SHA-1 */
225 int t
; /* Loop counter */
226 unsigned temp
; /* Temporary word value */
227 unsigned W
[80]; /* Word sequence */
228 unsigned A
, B
, C
, D
, E
; /* Word buffers */
231 * Initialize the first 16 words in the array W
233 for(t
= 0; t
< 16; t
++)
235 W
[t
] = ((unsigned) context
->Message_Block
[t
* 4]) << 24;
236 W
[t
] |= ((unsigned) context
->Message_Block
[t
* 4 + 1]) << 16;
237 W
[t
] |= ((unsigned) context
->Message_Block
[t
* 4 + 2]) << 8;
238 W
[t
] |= ((unsigned) context
->Message_Block
[t
* 4 + 3]);
241 for(t
= 16; t
< 80; t
++)
243 W
[t
] = SHA1CircularShift(1,W
[t
-3] ^ W
[t
-8] ^ W
[t
-14] ^ W
[t
-16]);
246 A
= context
->Message_Digest
[0];
247 B
= context
->Message_Digest
[1];
248 C
= context
->Message_Digest
[2];
249 D
= context
->Message_Digest
[3];
250 E
= context
->Message_Digest
[4];
252 for(t
= 0; t
< 20; t
++)
254 temp
= SHA1CircularShift(5,A
) +
255 ((B
& C
) | ((~B
) & D
)) + E
+ W
[t
] + K
[0];
259 C
= SHA1CircularShift(30,B
);
264 for(t
= 20; t
< 40; t
++)
266 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[1];
270 C
= SHA1CircularShift(30,B
);
275 for(t
= 40; t
< 60; t
++)
277 temp
= SHA1CircularShift(5,A
) +
278 ((B
& C
) | (B
& D
) | (C
& D
)) + E
+ W
[t
] + K
[2];
282 C
= SHA1CircularShift(30,B
);
287 for(t
= 60; t
< 80; t
++)
289 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[3];
293 C
= SHA1CircularShift(30,B
);
298 context
->Message_Digest
[0] =
299 (context
->Message_Digest
[0] + A
) & 0xFFFFFFFF;
300 context
->Message_Digest
[1] =
301 (context
->Message_Digest
[1] + B
) & 0xFFFFFFFF;
302 context
->Message_Digest
[2] =
303 (context
->Message_Digest
[2] + C
) & 0xFFFFFFFF;
304 context
->Message_Digest
[3] =
305 (context
->Message_Digest
[3] + D
) & 0xFFFFFFFF;
306 context
->Message_Digest
[4] =
307 (context
->Message_Digest
[4] + E
) & 0xFFFFFFFF;
309 context
->Message_Block_Index
= 0;
316 * According to the standard, the message must be padded to an even
317 * 512 bits. The first padding bit must be a '1'. The last 64
318 * bits represent the length of the original message. All bits in
319 * between should be 0. This function will pad the message
320 * according to those rules by filling the Message_Block array
321 * accordingly. It will also call SHA1ProcessMessageBlock()
322 * appropriately. When it returns, it can be assumed that the
323 * message digest has been computed.
335 void SHA1PadMessage(SHA1Context
*context
)
338 * Check to see if the current message block is too small to hold
339 * the initial padding bits and length. If so, we will pad the
340 * block, process it, and then continue padding into a second
343 if (context
->Message_Block_Index
> 55)
345 context
->Message_Block
[context
->Message_Block_Index
++] = 0x80;
346 while(context
->Message_Block_Index
< 64)
348 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
351 SHA1ProcessMessageBlock(context
);
353 while(context
->Message_Block_Index
< 56)
355 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
360 context
->Message_Block
[context
->Message_Block_Index
++] = 0x80;
361 while(context
->Message_Block_Index
< 56)
363 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
368 * Store the message length as the last 8 octets
370 context
->Message_Block
[56] = (context
->Length_High
>> 24) & 0xFF;
371 context
->Message_Block
[57] = (context
->Length_High
>> 16) & 0xFF;
372 context
->Message_Block
[58] = (context
->Length_High
>> 8) & 0xFF;
373 context
->Message_Block
[59] = (context
->Length_High
) & 0xFF;
374 context
->Message_Block
[60] = (context
->Length_Low
>> 24) & 0xFF;
375 context
->Message_Block
[61] = (context
->Length_Low
>> 16) & 0xFF;
376 context
->Message_Block
[62] = (context
->Length_Low
>> 8) & 0xFF;
377 context
->Message_Block
[63] = (context
->Length_Low
) & 0xFF;
379 SHA1ProcessMessageBlock(context
);