4 * Copyright (C) 1998, 2009
5 * Paul E. Jones <paulej@packetizer.com>
8 *****************************************************************************
9 * $Id: sha1.h 12 2009-06-22 19:34:25Z paulej $
10 *****************************************************************************
13 * This class implements the Secure Hashing Standard as defined
14 * in FIPS PUB 180-1 published April 17, 1995.
16 * Many of the variable names in the SHA1Context, especially the
17 * single character names, were used because those were the names
18 * used in the publication.
20 * Please read the file sha1.c for more information.
30 #include <string/stdstring.h>
36 * This structure will hold context information for the hashing
39 typedef struct SHA1Context
41 unsigned Message_Digest[5]; /* Message Digest (output) */
43 unsigned Length_Low; /* Message length in bits */
44 unsigned Length_High; /* Message length in bits */
46 unsigned char Message_Block[64]; /* 512-bit message blocks */
47 int Message_Block_Index; /* Index into message block array */
49 int Computed; /* Is the digest computed? */
50 int Corrupted; /* Is the message digest corruped? */
56 void SHA1Reset(SHA1Context *);
57 int SHA1Result(SHA1Context *);
58 void SHA1Input( SHA1Context *,
59 const unsigned char *,
67 * Copyright (C) 1998, 2009
68 * Paul E. Jones <paulej@packetizer.com>
71 *****************************************************************************
72 * $Id: sha1.c 12 2009-06-22 19:34:25Z paulej $
73 *****************************************************************************
76 * This file implements the Secure Hashing Standard as defined
77 * in FIPS PUB 180-1 published April 17, 1995.
79 * The Secure Hashing Standard, which uses the Secure Hashing
80 * Algorithm (SHA), produces a 160-bit message digest for a
81 * given data stream. In theory, it is highly improbable that
82 * two messages will produce the same message digest. Therefore,
83 * this algorithm can serve as a means of providing a "fingerprint"
87 * SHA-1 is defined in terms of 32-bit "words". This code was
88 * written with the expectation that the processor has at least
89 * a 32-bit machine word size. If the machine word size is larger,
90 * the code should still function properly. One caveat to that
91 * is that the input functions taking characters and character
92 * arrays assume that only 8 bits of information are stored in each
96 * SHA-1 is designed to work with messages less than 2^64 bits
97 * long. Although SHA-1 allows a message digest to be generated for
98 * messages of any number of bits less than 2^64, this
99 * implementation only works with messages with a length that is a
100 * multiple of the size of an 8-bit character.
104 /*#include "sha1.h"*/
107 * Define the circular shift macro
109 #define SHA1CircularShift(bits,word) \
110 ((((word) << (bits)) & 0xFFFFFFFF) | \
111 ((word) >> (32-(bits))))
113 /* Function prototypes */
114 void SHA1ProcessMessageBlock(SHA1Context *);
115 void SHA1PadMessage(SHA1Context *);
121 * This function will initialize the SHA1Context in preparation
122 * for computing a new message digest.
126 * The context to reset.
134 void SHA1Reset(SHA1Context *context)
136 context->Length_Low = 0;
137 context->Length_High = 0;
138 context->Message_Block_Index = 0;
140 context->Message_Digest[0] = 0x67452301;
141 context->Message_Digest[1] = 0xEFCDAB89;
142 context->Message_Digest[2] = 0x98BADCFE;
143 context->Message_Digest[3] = 0x10325476;
144 context->Message_Digest[4] = 0xC3D2E1F0;
146 context->Computed = 0;
147 context->Corrupted = 0;
154 * This function will return the 160-bit message digest into the
155 * Message_Digest array within the SHA1Context provided
159 * The context to use to calculate the SHA-1 hash.
162 * 1 if successful, 0 if it failed.
167 int SHA1Result(SHA1Context *context)
169 if (context->Corrupted)
172 if (!context->Computed)
174 SHA1PadMessage(context);
175 context->Computed = 1;
185 * This function accepts an array of octets as the next portion of
190 * The SHA-1 context to update
191 * message_array: [in]
192 * An array of characters representing the next portion of the
195 * The length of the message in message_array
203 void SHA1Input( SHA1Context *context,
204 const unsigned char *message_array,
210 if (context->Computed || context->Corrupted)
212 context->Corrupted = 1;
216 while(length-- && !context->Corrupted)
218 context->Message_Block[context->Message_Block_Index++] =
219 (*message_array & 0xFF);
221 context->Length_Low += 8;
222 /* Force it to 32 bits */
223 context->Length_Low &= 0xFFFFFFFF;
224 if (context->Length_Low == 0)
226 context->Length_High++;
227 /* Force it to 32 bits */
228 context->Length_High &= 0xFFFFFFFF;
229 if (context->Length_High == 0)
231 /* Message is too long */
232 context->Corrupted = 1;
236 if (context->Message_Block_Index == 64)
237 SHA1ProcessMessageBlock(context);
244 * SHA1ProcessMessageBlock
247 * This function will process the next 512 bits of the message
248 * stored in the Message_Block array.
257 * Many of the variable names in the SHAContext, especially the
258 * single character names, were used because those were the names
259 * used in the publication.
263 void SHA1ProcessMessageBlock(SHA1Context *context)
265 const unsigned K[] = /* Constants defined in SHA-1 */
272 int t; /* Loop counter */
273 unsigned temp; /* Temporary word value */
274 unsigned W[80]; /* Word sequence */
275 unsigned A, B, C, D, E; /* Word buffers */
278 * Initialize the first 16 words in the array W
280 for (t = 0; t < 16; t++)
282 W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
283 W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
284 W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
285 W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
288 for (t = 16; t < 80; t++)
289 W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
291 A = context->Message_Digest[0];
292 B = context->Message_Digest[1];
293 C = context->Message_Digest[2];
294 D = context->Message_Digest[3];
295 E = context->Message_Digest[4];
297 for (t = 0; t < 20; t++)
299 temp = SHA1CircularShift(5,A) +
300 ((B & C) | ((~B) & D)) + E + W[t] + K[0];
304 C = SHA1CircularShift(30,B);
309 for (t = 20; t < 40; t++)
311 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
315 C = SHA1CircularShift(30,B);
320 for (t = 40; t < 60; t++)
322 temp = SHA1CircularShift(5,A) +
323 ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
327 C = SHA1CircularShift(30,B);
332 for (t = 60; t < 80; t++)
334 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
338 C = SHA1CircularShift(30,B);
343 context->Message_Digest[0] =
344 (context->Message_Digest[0] + A) & 0xFFFFFFFF;
345 context->Message_Digest[1] =
346 (context->Message_Digest[1] + B) & 0xFFFFFFFF;
347 context->Message_Digest[2] =
348 (context->Message_Digest[2] + C) & 0xFFFFFFFF;
349 context->Message_Digest[3] =
350 (context->Message_Digest[3] + D) & 0xFFFFFFFF;
351 context->Message_Digest[4] =
352 (context->Message_Digest[4] + E) & 0xFFFFFFFF;
354 context->Message_Block_Index = 0;
361 * According to the standard, the message must be padded to an even
362 * 512 bits. The first padding bit must be a '1'. The last 64
363 * bits represent the length of the original message. All bits in
364 * between should be 0. This function will pad the message
365 * according to those rules by filling the Message_Block array
366 * accordingly. It will also call SHA1ProcessMessageBlock()
367 * appropriately. When it returns, it can be assumed that the
368 * message digest has been computed.
380 void SHA1PadMessage(SHA1Context *context)
383 * Check to see if the current message block is too small to hold
384 * the initial padding bits and length. If so, we will pad the
385 * block, process it, and then continue padding into a second
388 if (context->Message_Block_Index > 55)
390 context->Message_Block[context->Message_Block_Index++] = 0x80;
391 while(context->Message_Block_Index < 64)
392 context->Message_Block[context->Message_Block_Index++] = 0;
394 SHA1ProcessMessageBlock(context);
396 while(context->Message_Block_Index < 56)
397 context->Message_Block[context->Message_Block_Index++] = 0;
401 context->Message_Block[context->Message_Block_Index++] = 0x80;
402 while(context->Message_Block_Index < 56)
403 context->Message_Block[context->Message_Block_Index++] = 0;
407 * Store the message length as the last 8 octets
409 context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
410 context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
411 context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
412 context->Message_Block[59] = (context->Length_High) & 0xFF;
413 context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
414 context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
415 context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
416 context->Message_Block[63] = (context->Length_Low) & 0xFF;
418 SHA1ProcessMessageBlock(context);