sha1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * sha1.c
  3. *
  4. * Description:
  5. * This file implements the Secure Hashing Algorithm 1 as
  6. * defined in FIPS PUB 180-1 published April 17, 1995.
  7. *
  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.
  15. *
  16. * Portability Issues:
  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
  21. * appropriate.
  22. *
  23. * Caveats:
  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.
  29. *
  30. */
  31. #include "../../inc/cs.h"
  32. #include "../../inc/cs_str.h"
  33. #ifndef uint8
  34. #define uint8 unsigned char
  35. #endif
  36. #ifndef uint32
  37. #define uint32 unsigned long int
  38. #endif
  39. #define uint32_t unsigned int
  40. #define uint8_t unsigned char
  41. #define int_least16_t int
  42. #ifndef _SHA_enum_
  43. #define _SHA_enum_
  44. enum
  45. {
  46. shaSuccess = 0,
  47. shaNull, /* Null pointer parameter */
  48. shaInputTooLong, /* input data too long */
  49. shaStateError /* called Input after Result */
  50. };
  51. #endif
  52. #define SHA1HashSize 20
  53. /*
  54. * This structure will hold context information for the SHA-1
  55. * hashing operation
  56. */
  57. typedef struct SHA1Context
  58. {
  59. uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
  60. uint32_t Length_Low; /* Message length in bits */
  61. uint32_t Length_High; /* Message length in bits */
  62. /* Index into message block array */
  63. int_least16_t Message_Block_Index;
  64. uint8_t Message_Block[64]; /* 512-bit message blocks */
  65. int Computed; /* Is the digest computed? */
  66. int Corrupted; /* Is the message digest corrupted? */
  67. } SHA1Context;
  68. /*
  69. * If you do not have the ISO standard stdint.h header file, then you
  70. * must typdef the following:
  71. * name meaning
  72. * uint32_t unsigned 32 bit integer
  73. * uint8_t unsigned 8 bit integer (i.e., unsigned char)
  74. * int_least16_t integer of >= 16 bits
  75. *
  76. */
  77. /*
  78. * Define the SHA1 circular left shift macro
  79. */
  80. #define SHA1CircularShift(bits,word) \
  81. (((word) << (bits)) | ((word) >> (32-(bits))))
  82. /* Local Function Prototyptes */
  83. static void SHA1PadMessage(SHA1Context *);
  84. static void SHA1ProcessMessageBlock(SHA1Context *);
  85. /*
  86. * SHA1Reset
  87. *
  88. * Description:
  89. * This function will initialize the SHA1Context in preparation
  90. * for computing a new SHA1 message digest.
  91. *
  92. * Parameters:
  93. * context: [in/out]
  94. * The context to reset.
  95. *
  96. * Returns:
  97. * sha Error Code.
  98. *
  99. */
  100. static int SHA1Reset(SHA1Context *context)
  101. {
  102. if (!context)
  103. {
  104. return shaNull;
  105. }
  106. context->Length_Low = 0;
  107. context->Length_High = 0;
  108. context->Message_Block_Index = 0;
  109. context->Intermediate_Hash[0] = 0x67452301;
  110. context->Intermediate_Hash[1] = 0xEFCDAB89;
  111. context->Intermediate_Hash[2] = 0x98BADCFE;
  112. context->Intermediate_Hash[3] = 0x10325476;
  113. context->Intermediate_Hash[4] = 0xC3D2E1F0;
  114. context->Computed = 0;
  115. context->Corrupted = 0;
  116. return shaSuccess;
  117. }
  118. /*
  119. * SHA1Result
  120. *
  121. * Description:
  122. * This function will return the 160-bit message digest into the
  123. * Message_Digest array provided by the caller.
  124. * NOTE: The first octet of hash is stored in the 0th element,
  125. * the last octet of hash in the 19th element.
  126. *
  127. * Parameters:
  128. * context: [in/out]
  129. * The context to use to calculate the SHA-1 hash.
  130. * Message_Digest: [out]
  131. * Where the digest is returned.
  132. *
  133. * Returns:
  134. * sha Error Code.
  135. *
  136. */
  137. static int SHA1Result( SHA1Context *context,
  138. uint8_t Message_Digest[SHA1HashSize])
  139. {
  140. int i;
  141. if (!context || !Message_Digest)
  142. {
  143. return shaNull;
  144. }
  145. if (context->Corrupted)
  146. {
  147. return context->Corrupted;
  148. }
  149. if (!context->Computed)
  150. {
  151. SHA1PadMessage(context);
  152. for(i=0; i<64; ++i)
  153. {
  154. /* message may be sensitive, clear it out */
  155. context->Message_Block[i] = 0;
  156. }
  157. context->Length_Low = 0; /* and clear length */
  158. context->Length_High = 0;
  159. context->Computed = 1;
  160. }
  161. for(i = 0; i < SHA1HashSize; ++i)
  162. {
  163. Message_Digest[i] = context->Intermediate_Hash[i>>2]
  164. >> 8 * ( 3 - ( i & 0x03 ) );
  165. }
  166. return shaSuccess;
  167. }
  168. /*
  169. * SHA1Input
  170. *
  171. * Description:
  172. * This function accepts an array of octets as the next portion
  173. * of the message.
  174. *
  175. * Parameters:
  176. * context: [in/out]
  177. * The SHA context to update
  178. * message_array: [in]
  179. * An array of characters representing the next portion of
  180. * the message.
  181. * length: [in]
  182. * The length of the message in message_array
  183. *
  184. * Returns:
  185. * sha Error Code.
  186. *
  187. */
  188. static int SHA1Input( SHA1Context *context,
  189. const uint8_t *message_array,
  190. unsigned length)
  191. {
  192. if (!length)
  193. {
  194. return shaSuccess;
  195. }
  196. if (!context || !message_array)
  197. {
  198. return shaNull;
  199. }
  200. if (context->Computed)
  201. {
  202. context->Corrupted = shaStateError;
  203. return shaStateError;
  204. }
  205. if (context->Corrupted)
  206. {
  207. return context->Corrupted;
  208. }
  209. while(length-- && !context->Corrupted)
  210. {
  211. context->Message_Block[context->Message_Block_Index++] =
  212. (*message_array & 0xFF);
  213. context->Length_Low += 8;
  214. if (context->Length_Low == 0)
  215. {
  216. context->Length_High++;
  217. if (context->Length_High == 0)
  218. {
  219. /* Message is too long */
  220. context->Corrupted = 1;
  221. }
  222. }
  223. if (context->Message_Block_Index == 64)
  224. {
  225. SHA1ProcessMessageBlock(context);
  226. }
  227. message_array++;
  228. }
  229. return shaSuccess;
  230. }
  231. /*
  232. * SHA1ProcessMessageBlock
  233. *
  234. * Description:
  235. * This function will process the next 512 bits of the message
  236. * stored in the Message_Block array.
  237. *
  238. * Parameters:
  239. * None.
  240. *
  241. * Returns:
  242. * Nothing.
  243. *
  244. * Comments:
  245. * Many of the variable names in this code, especially the
  246. * single character names, were used because those were the
  247. * names used in the publication.
  248. *
  249. *
  250. */
  251. static void SHA1ProcessMessageBlock(SHA1Context *context)
  252. {
  253. const uint32_t K[] = { /* Constants defined in SHA-1 */
  254. 0x5A827999,
  255. 0x6ED9EBA1,
  256. 0x8F1BBCDC,
  257. 0xCA62C1D6
  258. };
  259. int t; /* Loop counter */
  260. uint32_t temp; /* Temporary word value */
  261. uint32_t W[80]; /* Word sequence */
  262. uint32_t A, B, C, D, E; /* Word buffers */
  263. /*
  264. * Initialize the first 16 words in the array W
  265. */
  266. for(t = 0; t < 16; t++)
  267. {
  268. W[t] = context->Message_Block[t * 4] << 24;
  269. W[t] |= context->Message_Block[t * 4 + 1] << 16;
  270. W[t] |= context->Message_Block[t * 4 + 2] << 8;
  271. W[t] |= context->Message_Block[t * 4 + 3];
  272. }
  273. for(t = 16; t < 80; t++)
  274. {
  275. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  276. }
  277. A = context->Intermediate_Hash[0];
  278. B = context->Intermediate_Hash[1];
  279. C = context->Intermediate_Hash[2];
  280. D = context->Intermediate_Hash[3];
  281. E = context->Intermediate_Hash[4];
  282. for(t = 0; t < 20; t++)
  283. {
  284. temp = SHA1CircularShift(5,A) +
  285. ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  286. E = D;
  287. D = C;
  288. C = SHA1CircularShift(30,B);
  289. B = A;
  290. A = temp;
  291. }
  292. for(t = 20; t < 40; t++)
  293. {
  294. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  295. E = D;
  296. D = C;
  297. C = SHA1CircularShift(30,B);
  298. B = A;
  299. A = temp;
  300. }
  301. for(t = 40; t < 60; t++)
  302. {
  303. temp = SHA1CircularShift(5,A) +
  304. ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  305. E = D;
  306. D = C;
  307. C = SHA1CircularShift(30,B);
  308. B = A;
  309. A = temp;
  310. }
  311. for(t = 60; t < 80; t++)
  312. {
  313. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  314. E = D;
  315. D = C;
  316. C = SHA1CircularShift(30,B);
  317. B = A;
  318. A = temp;
  319. }
  320. context->Intermediate_Hash[0] += A;
  321. context->Intermediate_Hash[1] += B;
  322. context->Intermediate_Hash[2] += C;
  323. context->Intermediate_Hash[3] += D;
  324. context->Intermediate_Hash[4] += E;
  325. context->Message_Block_Index = 0;
  326. }
  327. /*
  328. * SHA1PadMessage
  329. *
  330. * Description:
  331. * According to the standard, the message must be padded to an even
  332. * 512 bits. The first padding bit must be a '1'. The last 64
  333. * bits represent the length of the original message. All bits in
  334. * between should be 0. This function will pad the message
  335. * according to those rules by filling the Message_Block array
  336. * accordingly. It will also call the ProcessMessageBlock function
  337. * provided appropriately. When it returns, it can be assumed that
  338. * the message digest has been computed.
  339. *
  340. * Parameters:
  341. * context: [in/out]
  342. * The context to pad
  343. * ProcessMessageBlock: [in]
  344. * The appropriate SHA*ProcessMessageBlock function
  345. * Returns:
  346. * Nothing.
  347. *
  348. */
  349. static void SHA1PadMessage(SHA1Context *context)
  350. {
  351. /*
  352. * Check to see if the current message block is too small to hold
  353. * the initial padding bits and length. If so, we will pad the
  354. * block, process it, and then continue padding into a second
  355. * block.
  356. */
  357. if (context->Message_Block_Index > 55)
  358. {
  359. context->Message_Block[context->Message_Block_Index++] = 0x80;
  360. while(context->Message_Block_Index < 64)
  361. {
  362. context->Message_Block[context->Message_Block_Index++] = 0;
  363. }
  364. SHA1ProcessMessageBlock(context);
  365. while(context->Message_Block_Index < 56)
  366. {
  367. context->Message_Block[context->Message_Block_Index++] = 0;
  368. }
  369. }
  370. else
  371. {
  372. context->Message_Block[context->Message_Block_Index++] = 0x80;
  373. while(context->Message_Block_Index < 56)
  374. {
  375. context->Message_Block[context->Message_Block_Index++] = 0;
  376. }
  377. }
  378. /*
  379. * Store the message length as the last 8 octets
  380. */
  381. context->Message_Block[56] = context->Length_High >> 24;
  382. context->Message_Block[57] = context->Length_High >> 16;
  383. context->Message_Block[58] = context->Length_High >> 8;
  384. context->Message_Block[59] = context->Length_High;
  385. context->Message_Block[60] = context->Length_Low >> 24;
  386. context->Message_Block[61] = context->Length_Low >> 16;
  387. context->Message_Block[62] = context->Length_Low >> 8;
  388. context->Message_Block[63] = context->Length_Low;
  389. SHA1ProcessMessageBlock(context);
  390. }
  391. CS_API u_char *cs_sha1(u_char *d, const u_char *s, size_t n) {
  392. SHA1Context sha;
  393. int i, err;
  394. uint8_t Message_Digest[20];
  395. static u_char hex[] = "0123456789abcdef";
  396. u_char *dst = d;
  397. err = SHA1Reset(&sha);
  398. if (err) {
  399. return (NULL);
  400. }
  401. err = SHA1Input(&sha, (const unsigned char *)s, n);
  402. if (err) {
  403. return(NULL);
  404. }
  405. err = SHA1Result(&sha, Message_Digest);
  406. if (err) {
  407. return(NULL);
  408. } else {
  409. for (i = 0; i < 20 ; ++i) {
  410. *d++ = hex[Message_Digest[i] >> 4];
  411. *d++ = hex[Message_Digest[i] & 0xf];
  412. }
  413. }
  414. return (dst);
  415. }