2 This file contains the reference implementation of SHA-1
3 from http://www.ietf.org/rfc/rfc3174.txt
9 * This file implements the Secure Hashing Algorithm 1 as
10 * defined in FIPS PUB 180-1 published April 17, 1995.
12 * The SHA-1, produces a 160-bit message digest for a given
13 * data stream. It should take about 2**n steps to find a
14 * message with the same digest as a given message and
15 * 2**(n/2) to find any two messages with the same digest,
16 * when n is the digest size in bits. Therefore, this
17 * algorithm can serve as a means of providing a
18 * "fingerprint" for a message.
21 * SHA-1 is defined in terms of 32-bit "words". This code
22 * uses <stdint.h> (included via "sha1.h" to define 32 and 8
23 * bit unsigned integer types. If your C compiler does not
24 * support 32 bit unsigned integers, this code is not
28 * SHA-1 is designed to work with messages less than 2^64 bits
29 * long. Although SHA-1 allows a message digest to be generated
30 * for messages of any number of bits less than 2^64, this
31 * implementation only works with messages with a length that is
32 * a multiple of the size of an 8-bit character.
41 * Define the SHA1 circular left shift macro
43 #define SHA1CircularShift(bits,word) \
44 (((word) << (bits)) | ((word) >> (32-(bits))))
46 /* Local Function Prototyptes */
47 static void SHA1PadMessage(struct SHA1Context
*);
48 static void SHA1ProcessMessageBlock(struct SHA1Context
*);
51 * SHA1Init (SHA1Reset in the rfc)
54 * This function will initialize the SHA1Context in preparation
55 * for computing a new SHA1 message digest.
59 * The context to reset.
65 int SHA1Init(struct SHA1Context
*context
)
72 context
->Length_Low
= 0;
73 context
->Length_High
= 0;
74 context
->Message_Block_Index
= 0;
76 context
->Intermediate_Hash
[0] = 0x67452301;
77 context
->Intermediate_Hash
[1] = 0xEFCDAB89;
78 context
->Intermediate_Hash
[2] = 0x98BADCFE;
79 context
->Intermediate_Hash
[3] = 0x10325476;
80 context
->Intermediate_Hash
[4] = 0xC3D2E1F0;
82 context
->Computed
= 0;
83 context
->Corrupted
= 0;
89 * SHA1Final (SHA1Result in the rfc)
92 * This function will return the 160-bit message digest into the
93 * Message_Digest array provided by the caller.
94 * NOTE: The first octet of hash is stored in the 0th element,
95 * the last octet of hash in the 19th element.
99 * The context to use to calculate the SHA-1 hash.
100 * Message_Digest: [out]
101 * Where the digest is returned.
107 int SHA1Final(uint8_t Message_Digest
[SHA1HashSize
],
108 struct SHA1Context
*context
)
112 if (!context
|| !Message_Digest
)
117 if (context
->Corrupted
)
119 return context
->Corrupted
;
122 if (!context
->Computed
)
124 SHA1PadMessage(context
);
127 /* message may be sensitive, clear it out */
128 context
->Message_Block
[i
] = 0;
130 context
->Length_Low
= 0; /* and clear length */
131 context
->Length_High
= 0;
132 context
->Computed
= 1;
135 for(i
= 0; i
< SHA1HashSize
; ++i
)
137 Message_Digest
[i
] = context
->Intermediate_Hash
[i
>>2]
138 >> 8 * ( 3 - ( i
& 0x03 ) );
145 * SHA1Update (SHA1Input in the rfc)
148 * This function accepts an array of octets as the next portion
153 * The SHA context to update
154 * message_array: [in]
155 * An array of characters representing the next portion of
158 * The length of the message in message_array
164 int SHA1Update(struct SHA1Context
*context
,
165 const uint8_t *message_array
,
173 if (!context
|| !message_array
)
178 if (context
->Computed
)
180 context
->Corrupted
= shaStateError
;
181 return shaStateError
;
184 if (context
->Corrupted
)
186 return context
->Corrupted
;
188 while(length
-- && !context
->Corrupted
)
190 context
->Message_Block
[context
->Message_Block_Index
++] =
191 (*message_array
& 0xFF);
193 context
->Length_Low
+= 8;
194 if (context
->Length_Low
== 0)
196 context
->Length_High
++;
197 if (context
->Length_High
== 0)
199 /* Message is too long */
200 context
->Corrupted
= 1;
204 if (context
->Message_Block_Index
== 64)
206 SHA1ProcessMessageBlock(context
);
216 * SHA1ProcessMessageBlock
219 * This function will process the next 512 bits of the message
220 * stored in the Message_Block array.
229 * Many of the variable names in this code, especially the
230 * single character names, were used because those were the
231 * names used in the publication.
235 static void SHA1ProcessMessageBlock(struct SHA1Context
*context
)
237 const uint32_t K
[] = { /* Constants defined in SHA-1 */
243 int t
; /* Loop counter */
244 uint32_t temp
; /* Temporary word value */
245 uint32_t W
[80]; /* Word sequence */
246 uint32_t A
, B
, C
, D
, E
; /* Word buffers */
249 * Initialize the first 16 words in the array W
251 for(t
= 0; t
< 16; t
++)
253 W
[t
] = context
->Message_Block
[t
* 4] << 24;
254 W
[t
] |= context
->Message_Block
[t
* 4 + 1] << 16;
255 W
[t
] |= context
->Message_Block
[t
* 4 + 2] << 8;
256 W
[t
] |= context
->Message_Block
[t
* 4 + 3];
259 for(t
= 16; t
< 80; t
++)
261 W
[t
] = SHA1CircularShift(1,W
[t
-3] ^ W
[t
-8] ^ W
[t
-14] ^ W
[t
-16]);
264 A
= context
->Intermediate_Hash
[0];
265 B
= context
->Intermediate_Hash
[1];
266 C
= context
->Intermediate_Hash
[2];
267 D
= context
->Intermediate_Hash
[3];
268 E
= context
->Intermediate_Hash
[4];
270 for(t
= 0; t
< 20; t
++)
272 temp
= SHA1CircularShift(5,A
) +
273 ((B
& C
) | ((~B
) & D
)) + E
+ W
[t
] + K
[0];
276 C
= SHA1CircularShift(30,B
);
281 for(t
= 20; t
< 40; t
++)
283 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[1];
286 C
= SHA1CircularShift(30,B
);
291 for(t
= 40; t
< 60; t
++)
293 temp
= SHA1CircularShift(5,A
) +
294 ((B
& C
) | (B
& D
) | (C
& D
)) + E
+ W
[t
] + K
[2];
297 C
= SHA1CircularShift(30,B
);
302 for(t
= 60; t
< 80; t
++)
304 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[3];
307 C
= SHA1CircularShift(30,B
);
312 context
->Intermediate_Hash
[0] += A
;
313 context
->Intermediate_Hash
[1] += B
;
314 context
->Intermediate_Hash
[2] += C
;
315 context
->Intermediate_Hash
[3] += D
;
316 context
->Intermediate_Hash
[4] += E
;
318 context
->Message_Block_Index
= 0;
326 * According to the standard, the message must be padded to an even
327 * 512 bits. The first padding bit must be a '1'. The last 64
328 * bits represent the length of the original message. All bits in
329 * between should be 0. This function will pad the message
330 * according to those rules by filling the Message_Block array
331 * accordingly. It will also call the ProcessMessageBlock function
332 * provided appropriately. When it returns, it can be assumed that
333 * the message digest has been computed.
338 * ProcessMessageBlock: [in]
339 * The appropriate SHA*ProcessMessageBlock function
345 static void SHA1PadMessage(struct SHA1Context
*context
)
348 * Check to see if the current message block is too small to hold
349 * the initial padding bits and length. If so, we will pad the
350 * block, process it, and then continue padding into a second
353 if (context
->Message_Block_Index
> 55)
355 context
->Message_Block
[context
->Message_Block_Index
++] = 0x80;
356 while(context
->Message_Block_Index
< 64)
358 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
361 SHA1ProcessMessageBlock(context
);
363 while(context
->Message_Block_Index
< 56)
365 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
370 context
->Message_Block
[context
->Message_Block_Index
++] = 0x80;
371 while(context
->Message_Block_Index
< 56)
373 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
378 * Store the message length as the last 8 octets
380 context
->Message_Block
[56] = context
->Length_High
>> 24;
381 context
->Message_Block
[57] = context
->Length_High
>> 16;
382 context
->Message_Block
[58] = context
->Length_High
>> 8;
383 context
->Message_Block
[59] = context
->Length_High
;
384 context
->Message_Block
[60] = context
->Length_Low
>> 24;
385 context
->Message_Block
[61] = context
->Length_Low
>> 16;
386 context
->Message_Block
[62] = context
->Length_Low
>> 8;
387 context
->Message_Block
[63] = context
->Length_Low
;
389 SHA1ProcessMessageBlock(context
);