sha1.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * sha1.c
  3. *
  4. * Copyright (C) 1998, 2009
  5. * Paul E. Jones <paulej@packetizer.com>
  6. * All Rights Reserved
  7. *
  8. *****************************************************************************
  9. * $Id: sha1.c 12 2009-06-22 19:34:25Z paulej $
  10. *****************************************************************************
  11. *
  12. * Description:
  13. * This file implements the Secure Hashing Standard as defined
  14. * in FIPS PUB 180-1 published April 17, 1995.
  15. *
  16. * The Secure Hashing Standard, which uses the Secure Hashing
  17. * Algorithm (SHA), produces a 160-bit message digest for a
  18. * given data stream. In theory, it is highly improbable that
  19. * two messages will produce the same message digest. Therefore,
  20. * this algorithm can serve as a means of providing a "fingerprint"
  21. * for a message.
  22. *
  23. * Portability Issues:
  24. * SHA-1 is defined in terms of 32-bit "words". This code was
  25. * written with the expectation that the processor has at least
  26. * a 32-bit machine word size. If the machine word size is larger,
  27. * the code should still function properly. One caveat to that
  28. * is that the input functions taking characters and character
  29. * arrays assume that only 8 bits of information are stored in each
  30. * character.
  31. *
  32. * Caveats:
  33. * SHA-1 is designed to work with messages less than 2^64 bits
  34. * long. Although SHA-1 allows a message digest to be generated for
  35. * messages of any number of bits less than 2^64, this
  36. * implementation only works with messages with a length that is a
  37. * multiple of the size of an 8-bit character.
  38. *
  39. */
  40. #include "sha1.h"
  41. /*
  42. * Define the circular shift macro
  43. */
  44. #define SHA1CircularShift(bits,word) \
  45. ((((word) << (bits)) & 0xFFFFFFFF) | \
  46. ((word) >> (32-(bits))))
  47. /* Function prototypes */
  48. void SHA1ProcessMessageBlock(SHA1Context *);
  49. void SHA1PadMessage(SHA1Context *);
  50. /*
  51. * SHA1Reset
  52. *
  53. * Description:
  54. * This function will initialize the SHA1Context in preparation
  55. * for computing a new message digest.
  56. *
  57. * Parameters:
  58. * context: [in/out]
  59. * The context to reset.
  60. *
  61. * Returns:
  62. * Nothing.
  63. *
  64. * Comments:
  65. *
  66. */
  67. void SHA1Reset(SHA1Context *context)
  68. {
  69. context->Length_Low = 0;
  70. context->Length_High = 0;
  71. context->Message_Block_Index = 0;
  72. context->Message_Digest[0] = 0x67452301;
  73. context->Message_Digest[1] = 0xEFCDAB89;
  74. context->Message_Digest[2] = 0x98BADCFE;
  75. context->Message_Digest[3] = 0x10325476;
  76. context->Message_Digest[4] = 0xC3D2E1F0;
  77. context->Computed = 0;
  78. context->Corrupted = 0;
  79. }
  80. /*
  81. * SHA1Result
  82. *
  83. * Description:
  84. * This function will return the 160-bit message digest into the
  85. * Message_Digest array within the SHA1Context provided
  86. *
  87. * Parameters:
  88. * context: [in/out]
  89. * The context to use to calculate the SHA-1 hash.
  90. *
  91. * Returns:
  92. * 1 if successful, 0 if it failed.
  93. *
  94. * Comments:
  95. *
  96. */
  97. int SHA1Result(SHA1Context *context)
  98. {
  99. if (context->Corrupted)
  100. {
  101. return 0;
  102. }
  103. if (!context->Computed)
  104. {
  105. SHA1PadMessage(context);
  106. context->Computed = 1;
  107. }
  108. return 1;
  109. }
  110. /*
  111. * SHA1Input
  112. *
  113. * Description:
  114. * This function accepts an array of octets as the next portion of
  115. * the message.
  116. *
  117. * Parameters:
  118. * context: [in/out]
  119. * The SHA-1 context to update
  120. * message_array: [in]
  121. * An array of characters representing the next portion of the
  122. * message.
  123. * length: [in]
  124. * The length of the message in message_array
  125. *
  126. * Returns:
  127. * Nothing.
  128. *
  129. * Comments:
  130. *
  131. */
  132. void SHA1Input( SHA1Context *context,
  133. const unsigned char *message_array,
  134. unsigned length)
  135. {
  136. if (!length)
  137. {
  138. return;
  139. }
  140. if (context->Computed || context->Corrupted)
  141. {
  142. context->Corrupted = 1;
  143. return;
  144. }
  145. while(length-- && !context->Corrupted)
  146. {
  147. context->Message_Block[context->Message_Block_Index++] =
  148. (*message_array & 0xFF);
  149. context->Length_Low += 8;
  150. /* Force it to 32 bits */
  151. context->Length_Low &= 0xFFFFFFFF;
  152. if (context->Length_Low == 0)
  153. {
  154. context->Length_High++;
  155. /* Force it to 32 bits */
  156. context->Length_High &= 0xFFFFFFFF;
  157. if (context->Length_High == 0)
  158. {
  159. /* Message is too long */
  160. context->Corrupted = 1;
  161. }
  162. }
  163. if (context->Message_Block_Index == 64)
  164. {
  165. SHA1ProcessMessageBlock(context);
  166. }
  167. message_array++;
  168. }
  169. }
  170. /*
  171. * SHA1ProcessMessageBlock
  172. *
  173. * Description:
  174. * This function will process the next 512 bits of the message
  175. * stored in the Message_Block array.
  176. *
  177. * Parameters:
  178. * None.
  179. *
  180. * Returns:
  181. * Nothing.
  182. *
  183. * Comments:
  184. * Many of the variable names in the SHAContext, especially the
  185. * single character names, were used because those were the names
  186. * used in the publication.
  187. *
  188. *
  189. */
  190. void SHA1ProcessMessageBlock(SHA1Context *context)
  191. {
  192. const unsigned K[] = /* Constants defined in SHA-1 */
  193. {
  194. 0x5A827999,
  195. 0x6ED9EBA1,
  196. 0x8F1BBCDC,
  197. 0xCA62C1D6
  198. };
  199. int t; /* Loop counter */
  200. unsigned temp; /* Temporary word value */
  201. unsigned W[80]; /* Word sequence */
  202. unsigned A, B, C, D, E; /* Word buffers */
  203. /*
  204. * Initialize the first 16 words in the array W
  205. */
  206. for(t = 0; t < 16; t++)
  207. {
  208. W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
  209. W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
  210. W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
  211. W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
  212. }
  213. for(t = 16; t < 80; t++)
  214. {
  215. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  216. }
  217. A = context->Message_Digest[0];
  218. B = context->Message_Digest[1];
  219. C = context->Message_Digest[2];
  220. D = context->Message_Digest[3];
  221. E = context->Message_Digest[4];
  222. for(t = 0; t < 20; t++)
  223. {
  224. temp = SHA1CircularShift(5,A) +
  225. ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  226. temp &= 0xFFFFFFFF;
  227. E = D;
  228. D = C;
  229. C = SHA1CircularShift(30,B);
  230. B = A;
  231. A = temp;
  232. }
  233. for(t = 20; t < 40; t++)
  234. {
  235. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  236. temp &= 0xFFFFFFFF;
  237. E = D;
  238. D = C;
  239. C = SHA1CircularShift(30,B);
  240. B = A;
  241. A = temp;
  242. }
  243. for(t = 40; t < 60; t++)
  244. {
  245. temp = SHA1CircularShift(5,A) +
  246. ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  247. temp &= 0xFFFFFFFF;
  248. E = D;
  249. D = C;
  250. C = SHA1CircularShift(30,B);
  251. B = A;
  252. A = temp;
  253. }
  254. for(t = 60; t < 80; t++)
  255. {
  256. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  257. temp &= 0xFFFFFFFF;
  258. E = D;
  259. D = C;
  260. C = SHA1CircularShift(30,B);
  261. B = A;
  262. A = temp;
  263. }
  264. context->Message_Digest[0] =
  265. (context->Message_Digest[0] + A) & 0xFFFFFFFF;
  266. context->Message_Digest[1] =
  267. (context->Message_Digest[1] + B) & 0xFFFFFFFF;
  268. context->Message_Digest[2] =
  269. (context->Message_Digest[2] + C) & 0xFFFFFFFF;
  270. context->Message_Digest[3] =
  271. (context->Message_Digest[3] + D) & 0xFFFFFFFF;
  272. context->Message_Digest[4] =
  273. (context->Message_Digest[4] + E) & 0xFFFFFFFF;
  274. context->Message_Block_Index = 0;
  275. }
  276. /*
  277. * SHA1PadMessage
  278. *
  279. * Description:
  280. * According to the standard, the message must be padded to an even
  281. * 512 bits. The first padding bit must be a '1'. The last 64
  282. * bits represent the length of the original message. All bits in
  283. * between should be 0. This function will pad the message
  284. * according to those rules by filling the Message_Block array
  285. * accordingly. It will also call SHA1ProcessMessageBlock()
  286. * appropriately. When it returns, it can be assumed that the
  287. * message digest has been computed.
  288. *
  289. * Parameters:
  290. * context: [in/out]
  291. * The context to pad
  292. *
  293. * Returns:
  294. * Nothing.
  295. *
  296. * Comments:
  297. *
  298. */
  299. void SHA1PadMessage(SHA1Context *context)
  300. {
  301. /*
  302. * Check to see if the current message block is too small to hold
  303. * the initial padding bits and length. If so, we will pad the
  304. * block, process it, and then continue padding into a second
  305. * block.
  306. */
  307. if (context->Message_Block_Index > 55)
  308. {
  309. context->Message_Block[context->Message_Block_Index++] = 0x80;
  310. while(context->Message_Block_Index < 64)
  311. {
  312. context->Message_Block[context->Message_Block_Index++] = 0;
  313. }
  314. SHA1ProcessMessageBlock(context);
  315. while(context->Message_Block_Index < 56)
  316. {
  317. context->Message_Block[context->Message_Block_Index++] = 0;
  318. }
  319. }
  320. else
  321. {
  322. context->Message_Block[context->Message_Block_Index++] = 0x80;
  323. while(context->Message_Block_Index < 56)
  324. {
  325. context->Message_Block[context->Message_Block_Index++] = 0;
  326. }
  327. }
  328. /*
  329. * Store the message length as the last 8 octets
  330. */
  331. context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
  332. context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
  333. context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
  334. context->Message_Block[59] = (context->Length_High) & 0xFF;
  335. context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
  336. context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
  337. context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
  338. context->Message_Block[63] = (context->Length_Low) & 0xFF;
  339. SHA1ProcessMessageBlock(context);
  340. }