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.
37 * Define the SHA1 circular left shift macro
39 #define SHA1CircularShift(bits,word) \
40 (((word) << (bits)) | ((word) >> (32-(bits))))
42 /* Local Function Prototyptes */
43 void SHA1PadMessage(SHA1Context *);
44 void SHA1ProcessMessageBlock(SHA1Context *);
50 * This function will initialize the SHA1Context in preparation
51 * for computing a new SHA1 message digest.
55 * The context to reset.
61 int SHA1Reset(SHA1Context * context)
67 context->Length_Low = 0;
68 context->Length_High = 0;
69 context->Message_Block_Index = 0;
71 context->Intermediate_Hash[0] = 0x67452301;
72 context->Intermediate_Hash[1] = 0xEFCDAB89;
73 context->Intermediate_Hash[2] = 0x98BADCFE;
74 context->Intermediate_Hash[3] = 0x10325476;
75 context->Intermediate_Hash[4] = 0xC3D2E1F0;
77 context->Computed = 0;
78 context->Corrupted = 0;
87 * This function will return the 160-bit message digest into the
88 * Message_Digest array provided by the caller.
89 * NOTE: The first octet of hash is stored in the 0th element,
90 * the last octet of hash in the 19th element.
94 * The context to use to calculate the SHA-1 hash.
95 * Message_Digest: [out]
96 * Where the digest is returned.
102 int SHA1Result(SHA1Context * context, uint8_t Message_Digest[SHA1HashSize])
106 if (!context || !Message_Digest) {
110 if (context->Corrupted) {
111 return context->Corrupted;
114 if (!context->Computed) {
115 SHA1PadMessage(context);
116 for (i = 0; i < 64; ++i) {
117 /* message may be sensitive, clear it out */
118 context->Message_Block[i] = 0;
120 context->Length_Low = 0; /* and clear length */
121 context->Length_High = 0;
122 context->Computed = 1;
125 for (i = 0; i < SHA1HashSize; ++i) {
126 Message_Digest[i] = context->Intermediate_Hash[i >> 2]
127 >> 8 * (3 - (i & 0x03));
137 * This function accepts an array of octets as the next portion
142 * The SHA context to update
143 * message_array: [in]
144 * An array of characters representing the next portion of
147 * The length of the message in message_array
153 int SHA1Input(SHA1Context * context,
154 const uint8_t * message_array, unsigned length)
160 if (!context || !message_array) {
164 if (context->Computed) {
165 context->Corrupted = shaStateError;
166 return shaStateError;
169 if (context->Corrupted) {
170 return context->Corrupted;
172 while (length-- && !context->Corrupted) {
173 context->Message_Block[context->Message_Block_Index++] =
174 (*message_array & 0xFF);
176 context->Length_Low += 8;
177 if (context->Length_Low == 0) {
178 context->Length_High++;
179 if (context->Length_High == 0) {
180 /* Message is too long */
181 context->Corrupted = 1;
185 if (context->Message_Block_Index == 64) {
186 SHA1ProcessMessageBlock(context);
196 * SHA1ProcessMessageBlock
199 * This function will process the next 512 bits of the message
200 * stored in the Message_Block array.
209 * Many of the variable names in this code, especially the
210 * single character names, were used because those were the
211 * names used in the publication.
215 void SHA1ProcessMessageBlock(SHA1Context * context)
217 const uint32_t K[] = { /* Constants defined in SHA-1 */
223 int t; /* Loop counter */
224 uint32_t temp; /* Temporary word value */
225 uint32_t W[80]; /* Word sequence */
226 uint32_t A, B, C, D, E; /* Word buffers */
229 * Initialize the first 16 words in the array W
231 for (t = 0; t < 16; t++) {
232 W[t] = context->Message_Block[t * 4] << 24;
233 W[t] |= context->Message_Block[t * 4 + 1] << 16;
234 W[t] |= context->Message_Block[t * 4 + 2] << 8;
235 W[t] |= context->Message_Block[t * 4 + 3];
238 for (t = 16; t < 80; t++) {
241 W[t - 3] ^ W[t - 8] ^ W[t -
246 A = context->Intermediate_Hash[0];
247 B = context->Intermediate_Hash[1];
248 C = context->Intermediate_Hash[2];
249 D = context->Intermediate_Hash[3];
250 E = context->Intermediate_Hash[4];
252 for (t = 0; t < 20; t++) {
253 temp = SHA1CircularShift(5, A) +
254 ((B & C) | ((~B) & D)) + E + W[t] + K[0];
257 C = SHA1CircularShift(30, B);
262 for (t = 20; t < 40; t++) {
265 A) + (B ^ C ^ D) + E + W[t] + K[1];
268 C = SHA1CircularShift(30, B);
273 for (t = 40; t < 60; t++) {
274 temp = SHA1CircularShift(5, A) +
275 ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
278 C = SHA1CircularShift(30, B);
283 for (t = 60; t < 80; t++) {
286 A) + (B ^ C ^ D) + E + W[t] + K[3];
289 C = SHA1CircularShift(30, B);
294 context->Intermediate_Hash[0] += A;
295 context->Intermediate_Hash[1] += B;
296 context->Intermediate_Hash[2] += C;
297 context->Intermediate_Hash[3] += D;
298 context->Intermediate_Hash[4] += E;
300 context->Message_Block_Index = 0;
308 * According to the standard, the message must be padded to an even
309 * 512 bits. The first padding bit must be a '1'. The last 64
310 * bits represent the length of the original message. All bits in
311 * between should be 0. This function will pad the message
312 * according to those rules by filling the Message_Block array
313 * accordingly. It will also call the ProcessMessageBlock function
314 * provided appropriately. When it returns, it can be assumed that
315 * the message digest has been computed.
320 * ProcessMessageBlock: [in]
321 * The appropriate SHA*ProcessMessageBlock function
327 void SHA1PadMessage(SHA1Context * context)
330 * Check to see if the current message block is too small to hold
331 * the initial padding bits and length. If so, we will pad the
332 * block, process it, and then continue padding into a second
335 if (context->Message_Block_Index > 55) {
336 context->Message_Block[context->Message_Block_Index++] =
338 while (context->Message_Block_Index < 64) {
339 context->Message_Block[context->
340 Message_Block_Index++] = 0;
343 SHA1ProcessMessageBlock(context);
345 while (context->Message_Block_Index < 56) {
346 context->Message_Block[context->
347 Message_Block_Index++] = 0;
350 context->Message_Block[context->Message_Block_Index++] =
352 while (context->Message_Block_Index < 56) {
353 context->Message_Block[context->
354 Message_Block_Index++] = 0;
359 * Store the message length as the last 8 octets
361 context->Message_Block[56] = context->Length_High >> 24;
362 context->Message_Block[57] = context->Length_High >> 16;
363 context->Message_Block[58] = context->Length_High >> 8;
364 context->Message_Block[59] = context->Length_High;
365 context->Message_Block[60] = context->Length_Low >> 24;
366 context->Message_Block[61] = context->Length_Low >> 16;
367 context->Message_Block[62] = context->Length_Low >> 8;
368 context->Message_Block[63] = context->Length_Low;
370 SHA1ProcessMessageBlock(context);