]>
git.saurik.com Git - ldid.git/blob - sha1.c
5 * This file implements the Secure Hashing Algorithm 1 as
6 * defined in FIPS PUB 180-1 published April 17, 1995.
8 * The SHA-1, produces a 160-bit message digest for a given
9 * data stream. It should take about 2**n steps to find a
10 * message with the same digest as a given message and
11 * 2**(n/2) to find any two messages with the same digest,
12 * when n is the digest size in bits. Therefore, this
13 * algorithm can serve as a means of providing a
14 * "fingerprint" for a message.
17 * SHA-1 is defined in terms of 32-bit "words". This code
18 * uses <stdint.h> (included via "sha1.h" to define 32 and 8
19 * bit unsigned integer types. If your C compiler does not
20 * support 32 bit unsigned integers, this code is not
24 * SHA-1 is designed to work with messages less than 2^64 bits
25 * long. Although SHA-1 allows a message digest to be generated
26 * for messages of any number of bits less than 2^64, this
27 * implementation only works with messages with a length that is
28 * a multiple of the size of an 8-bit character.
35 * Define the SHA1 circular left shift macro
37 #define SHA1CircularShift(bits,word) \
38 (((word) << (bits)) | ((word) >> (32-(bits))))
40 /* Local Function Prototyptes */
41 void SHA1PadMessage(SHA1Context
*);
42 void SHA1ProcessMessageBlock(SHA1Context
*);
48 * This function will initialize the SHA1Context in preparation
49 * for computing a new SHA1 message digest.
53 * The context to reset.
59 int SHA1Reset(SHA1Context
*context
)
66 context
->Length_Low
= 0;
67 context
->Length_High
= 0;
68 context
->Message_Block_Index
= 0;
70 context
->Intermediate_Hash
[0] = 0x67452301;
71 context
->Intermediate_Hash
[1] = 0xEFCDAB89;
72 context
->Intermediate_Hash
[2] = 0x98BADCFE;
73 context
->Intermediate_Hash
[3] = 0x10325476;
74 context
->Intermediate_Hash
[4] = 0xC3D2E1F0;
76 context
->Computed
= 0;
77 context
->Corrupted
= 0;
86 * This function will return the 160-bit message digest into the
87 * Message_Digest array provided by the caller.
88 * NOTE: The first octet of hash is stored in the 0th element,
89 * the last octet of hash in the 19th element.
93 * The context to use to calculate the SHA-1 hash.
94 * Message_Digest: [out]
95 * Where the digest is returned.
101 int SHA1Result( SHA1Context
*context
,
102 uint8_t Message_Digest
[SHA1HashSize
])
106 if (!context
|| !Message_Digest
)
111 if (context
->Corrupted
)
113 return context
->Corrupted
;
116 if (!context
->Computed
)
118 SHA1PadMessage(context
);
121 /* message may be sensitive, clear it out */
122 context
->Message_Block
[i
] = 0;
124 context
->Length_Low
= 0; /* and clear length */
125 context
->Length_High
= 0;
126 context
->Computed
= 1;
130 for(i
= 0; i
< SHA1HashSize
; ++i
)
132 Message_Digest
[i
] = context
->Intermediate_Hash
[i
>>2]
133 >> 8 * ( 3 - ( i
& 0x03 ) );
143 * This function accepts an array of octets as the next portion
148 * The SHA context to update
149 * message_array: [in]
150 * An array of characters representing the next portion of
153 * The length of the message in message_array
159 int SHA1Input( SHA1Context
*context
,
160 const uint8_t *message_array
,
168 if (!context
|| !message_array
)
173 if (context
->Computed
)
175 context
->Corrupted
= shaStateError
;
177 return shaStateError
;
180 if (context
->Corrupted
)
182 return context
->Corrupted
;
184 while(length
-- && !context
->Corrupted
)
186 context
->Message_Block
[context
->Message_Block_Index
++] =
187 (*message_array
& 0xFF);
189 context
->Length_Low
+= 8;
190 if (context
->Length_Low
== 0)
192 context
->Length_High
++;
193 if (context
->Length_High
== 0)
195 /* Message is too long */
196 context
->Corrupted
= 1;
200 if (context
->Message_Block_Index
== 64)
202 SHA1ProcessMessageBlock(context
);
212 * SHA1ProcessMessageBlock
215 * This function will process the next 512 bits of the message
216 * stored in the Message_Block array.
226 * Many of the variable names in this code, especially the
227 * single character names, were used because those were the
228 * names used in the publication.
232 void SHA1ProcessMessageBlock(SHA1Context
*context
)
234 const uint32_t K
[] = { /* Constants defined in SHA-1 */
240 int t
; /* Loop counter */
241 uint32_t temp
; /* Temporary word value */
242 uint32_t W
[80]; /* Word sequence */
243 uint32_t A
, B
, C
, D
, E
; /* Word buffers */
246 * Initialize the first 16 words in the array W
248 for(t
= 0; t
< 16; t
++)
250 W
[t
] = context
->Message_Block
[t
* 4] << 24;
251 W
[t
] |= context
->Message_Block
[t
* 4 + 1] << 16;
252 W
[t
] |= context
->Message_Block
[t
* 4 + 2] << 8;
253 W
[t
] |= context
->Message_Block
[t
* 4 + 3];
256 for(t
= 16; t
< 80; t
++)
258 W
[t
] = SHA1CircularShift(1,W
[t
-3] ^ W
[t
-8] ^ W
[t
-14] ^ W
[t
-16]);
261 A
= context
->Intermediate_Hash
[0];
262 B
= context
->Intermediate_Hash
[1];
263 C
= context
->Intermediate_Hash
[2];
264 D
= context
->Intermediate_Hash
[3];
265 E
= context
->Intermediate_Hash
[4];
267 for(t
= 0; t
< 20; t
++)
269 temp
= SHA1CircularShift(5,A
) +
270 ((B
& C
) | ((~B
) & D
)) + E
+ W
[t
] + K
[0];
273 C
= SHA1CircularShift(30,B
);
279 for(t
= 20; t
< 40; t
++)
281 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[1];
284 C
= SHA1CircularShift(30,B
);
289 for(t
= 40; t
< 60; t
++)
291 temp
= SHA1CircularShift(5,A
) +
292 ((B
& C
) | (B
& D
) | (C
& D
)) + E
+ W
[t
] + K
[2];
295 C
= SHA1CircularShift(30,B
);
300 for(t
= 60; t
< 80; t
++)
302 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[3];
305 C
= SHA1CircularShift(30,B
);
310 context
->Intermediate_Hash
[0] += A
;
311 context
->Intermediate_Hash
[1] += B
;
312 context
->Intermediate_Hash
[2] += C
;
313 context
->Intermediate_Hash
[3] += D
;
314 context
->Intermediate_Hash
[4] += E
;
316 context
->Message_Block_Index
= 0;
324 * According to the standard, the message must be padded to an even
325 * 512 bits. The first padding bit must be a '1'. The last 64
326 * bits represent the length of the original message. All bits in
327 * between should be 0. This function will pad the message
328 * according to those rules by filling the Message_Block array
329 * accordingly. It will also call the ProcessMessageBlock function
330 * provided appropriately. When it returns, it can be assumed that
331 * the message digest has been computed.
336 * ProcessMessageBlock: [in]
337 * The appropriate SHA*ProcessMessageBlock function
343 void SHA1PadMessage(SHA1Context
*context
)
346 * Check to see if the current message block is too small to hold
347 * the initial padding bits and length. If so, we will pad the
348 * block, process it, and then continue padding into a second
351 if (context
->Message_Block_Index
> 55)
353 context
->Message_Block
[context
->Message_Block_Index
++] = 0x80;
354 while(context
->Message_Block_Index
< 64)
356 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
359 SHA1ProcessMessageBlock(context
);
361 while(context
->Message_Block_Index
< 56)
363 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
368 context
->Message_Block
[context
->Message_Block_Index
++] = 0x80;
369 while(context
->Message_Block_Index
< 56)
372 context
->Message_Block
[context
->Message_Block_Index
++] = 0;
377 * Store the message length as the last 8 octets
379 context
->Message_Block
[56] = context
->Length_High
>> 24;
380 context
->Message_Block
[57] = context
->Length_High
>> 16;
381 context
->Message_Block
[58] = context
->Length_High
>> 8;
382 context
->Message_Block
[59] = context
->Length_High
;
383 context
->Message_Block
[60] = context
->Length_Low
>> 24;
384 context
->Message_Block
[61] = context
->Length_Low
>> 16;
385 context
->Message_Block
[62] = context
->Length_Low
>> 8;
386 context
->Message_Block
[63] = context
->Length_Low
;
388 SHA1ProcessMessageBlock(context
);